11.1 Variable types in PERL ScalarArrayHash $number -3.54 $string %hash $array[0] $hash{key}

Slides:



Advertisements
Similar presentations
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
Advertisements

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
12.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Programming and Perl for Bioinformatics Part III.
10.1 References & Complex Data Structures Variable types in PERL ScalarArrayHash $number $string %hash $reference
PHP (2) – Functions, Arrays, Databases, and sessions.
4ex.1 More loops. 4ex.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) {
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
11ex.1 Modules and BioPerl. 11ex.2 sub reverseComplement { my ($seq) $seq =~ tr/ACGT/TGCA/; $seq = reverse $seq; return $seq; } my $revSeq = reverseComplement("GCAGTG");
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
7ex.1 Hashes. 7ex.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Eyal Enter a phone number:
9.1 Hash revision. 9.2 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
for($i=0; $i/)
8.1 References and complex data structures. 8.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
Sup.1 Supplemental Material (NOT part of the material for the exam)
Sorting. Simple Sorting As you are probably aware, there are many different sorting algorithms: selection sort, insertion sort, bubble sort, heap sort,
14.1 Wrapping up Revision 14.3 References are your friends…
8.1 Hashes (associative arrays). 8.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Dudi Enter a phone.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
8.1 Last time on: Pattern Matching. 8.2 Finding a sub string (match) somewhere: if ($line =~ m/he/)... remember to use slash( / ) and not back-slash Will.
10.1 Variable types in PERL ScalarArrayHash $number $string %hash => $array[0] $hash{key}
8ex.1 References and complex data structures. 8ex.2 An associative array (or simply – a hash) is an unordered set of key=>value pairs. Each key is associated.
4.1 Revision. 4.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n"; my $number.
10.1 Sorting and Modules בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Physical Mapping II + Perl CIS 667 March 2, 2004.
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
9.1 Hashes. 9.2 Let's say we want to create a phone book... Enter a name that will be added to the phone book: Ofir Enter a phone number: Enter.
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
C++ fundamentals.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
13r.1 Revision (Q&A). 13r.2 $scalar 13r.3 Multiple assignment my ($a,$b) = ('cow','dog'); = = (6,7,8,9,10);
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
MCB 5472 Assignment #6: HMMER and using perl to perform repetitive tasks February 26, 2014.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Prof. Alfred J Bird, Ph.D., NBCT -bird.wikispaces.umb.edu/ Office – McCormick 3rd floor.
Built-in Data Structures in Python An Introduction.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
8.1 Common Errors – Exercise #3 Assuming something on the variable part of the input file. When parsing a format file (genebank, fasta or any other format),
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Perl Day 5. Arrays vs Hash Arrays are one way to store multiple things in a variable. Hashes are another. Arrays are one way to store multiple things.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Lecture 9: Basic concepts of Perl Modules. Functions (Subs) In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements.
Modules and BioPerl.
Subroutines Web Programming.
The structure of programming
Presentation transcript:

11.1 Variable types in PERL ScalarArrayHash $number $string %hash $array[0] $hash{key}

11.2 An associative array (or simply – a hash) is an unordered set of pairs of keys and values. Each key is associated with a value. A hash variable name starts with a “%”: my %hash; Initialization: %hash = ("a"=>5, "bob"=>"zzz", 50=>"John"); Accessing: you can access a value by its key: print $hash{50};John modifying: $hash{bob} = "aaa"; (modifying an existing value) adding: $hash{555} = "z"; (adding a new key-value pair) Hash – an associative array %hash 5"a" "zzz""bob" "John"50 "aaa" %hash 5"a" "aaa""bob" "John"50 "z"555

11.3 It is possible to get a list of all the keys in %hash = keys(%hash); Similarly you can get an array of the values in %hash = values(%hash); Obtaining a list of the elements in a 5 "John" 50 "bob" "a" %hash 5"a" "zzz""bob" "John"50

11.4 You can use combinations of hashes (and arrays) together to construct more complex data structures. If the information is best represented in two levels it is useful to use a hash within a hash: my %hash; $hash{Key_level_1}{Key_level_2}; Hash within Hash

11.5 Example: each name in the phone book, has both phone number and address: my %phoneBook; $phoneBook{"Eyal"}{"Phone"} = " "; $phoneBook{"Eyal"}{"Address"} = "115 Menora St., Hulun"; $phoneBook{"Ofir"}{"Phone"} = " "; $phoneBook{"Ofir"}{"Address"} = "31 Horkanus St., Eilat"; Hash within Hash %phoneBook "Eyal" "Ofir" "Dudu" "09-9…""Phone" "Hulun""Addrs" "054-…""Phone" "Eilat""Addrs" 9245"Phone" TAU"Addrs" %phoneBook NAME => {Phone => PHONE Address => ADDRESS} Key I: nameKey II: dataType Corresponding Value

11.6 Assignment: my %phoneBook; $phoneBook{"Eyal"}{"Phone"} = " "; $phoneBook{"Eyal"}{"Address"} = "115 Menora St., Hulun"; Access: print $phoneBook{"Eyal"}{"Phone"}; Modify: $phoneBook{"Eyal"}{"Phone"} = ; Hash within Hash %phoneBook "Eyal" "Ofir" "Dudu" "09-9…""Phone" "Hulun""Addrs" "054-…""Phone" "Eilat""Addrs" 9245"Phone" TAU"Addrs" %phoneBook NAME => {Phone => PHONE Address => ADDRESS}

11.7 More Complex Data Structures

11.8 “How to keep the phone number, address and list of grades for each student in a course?” $phoneBook{"Eyal"}{"Phone"} = 7693; $phoneBook{"Eyal"}{"Address"} = "34 HaShalom St."; $phoneBook{"Eyal"}{"Grades"}[0]= 93; $phoneBook{"Eyal"}{"Grades"}[1]= 72; $phoneBook{"Eyal"}{"Grades"}[2]= 87; print $phoneBook{"Eyal"}{"Grades"}[2]; 87 Array within hash within hash… %phoneBook NAME => { " Phone " => PHONE " Address " => ADDRESS " Grades " => [GRADES]} %phoneBook "Eyal" "Ofir" 7693"Phone" "34…""Addrs" "Grades" "Phone" TAU"Addrs" "Grades"

11.9 An alternative way to insert the array of grades: $phoneBook{"Eyal"}{"Phone"} = 7693; $phoneBook{"Eyal"}{"Address"} = "34 HaShalom = (93,72,87); $phoneBook{"Eyal"}{"Grades"} = print $phoneBook{"Eyal"}{"Grades"}[2]; 87 Array within hash within hash… %phoneBook NAME => { " Phone " => PHONE " Address " => ADDRESS " Grades " => [GRADES]} %phoneBook "Eyal" "Ofir" 7693"Phone" "34…""Addrs" "Grades" "Phone" TAU"Addrs" "Grades" The will create a copy

11.10 How would you make a matrix? matrix[0][0] = 23; matrix[0][1] = 5;... How would you make a 3D matrix?? How would you make a array of hashes??? To Infinity and Beyond!!

11ex.11 %genes PRODUCT => { " protein_id " => PROTEIN_ID " strand " => STRAND " CDS " => [START, END]} %genes PRODUCT => { " protein_id " => PROTEIN_ID " strand " => STRAND} %genes PRODUCT => { " protein_id " => PROTEIN_ID} Class exercise 11a 1. Read the adenovirus genome file and build a hash of genes, where the key is the "product" name: For each gene store a hash with the protein ID. Print all keys (names) in the hash. 2. Add to the hash the strand of the gene on the genome: “ + ” for the sense strand and “ - ” for the antisense strand. Print all antisense genes. 3. Add to the hash an array of two coordinates – the start and end of the CDS. Print genes shorter than 500bp. 4*. Print the product name of all genes on the sense strand whose CDS spans more than 1kbp, and all genes on the antisense strand whose CDS spans less than 500bp.

11.12 Subroutines

11.13 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { # Do something... } Note: Subroutine definitions may be placed anywhere in a script, but they are usually placed together at the beginning or the end. Subroutines For example: sub printHello { print "Hello world\n"; }

11.14 To invoke (execute) a subroutine: SUB_NAME(PARAMETERS); Subroutines For example: printHello(); Hello world print reverseComplement("GCAGTG"); CGTCAC

11.15  Code in a subroutine is reusable (i.e. it can be invoked from several points in the script, no code duplication) e.g. a subroutine that reverse-complement a DNA sequence  A subroutine can provide a general solution that may be applied in different situations. e.g. read a FASTA file Why use subroutines?

11.16  Encapsulation: A well defined task can be done in a subroutine, making the main script simpler and easier to read and understand. For example: $seq = readFastaFile($fileName); # reads a FASTA sequence $revSeq = reverseComplement($seq); # reverse complement the sequnce printFasta($revSeq); # prints the sequence in FASTA format Why use subroutines? - Example

11.17 A subroutine may be given arguments through the special array sub bartFunc { my ($string, $times) print $string x $times; } my $bart4today = "I will not eat things for money\n"; bartFunc($bart4today,100); I will not eat things for money I will not eat things for money I will not eat things for money I will not eat things for money I will not eat things for money Subroutine arguments

11.18 Definition: sub reverseComplement { my ($seq) $seq =~ tr/ACGT/TGCA/; $seq = reverse $seq; return $seq; } Usage: my $revSeq = reverseComplement("GCAGTG"); CACTGC Notes:  The return function ends the execution of the subroutine and returns a value.  If there is no (explicit) return statement, the value of the last statement in the subroutine is returned. Return value

11.19 A subroutine may also return a list value: sub integerDivide { my ($a,$b) my $mana = int($a/$b); my $sheerit = $a % $b; return ($mana,$sheerit); } my ($myMana,$mySheerit) = integerDivide(7,3); print "mana= $mana, sheerit= $sheerit"; mana= 2, sheerit= 1 Return value a list

11.20 When a variable is defined using my inside a subroutine: * It does not conflict with a variable by the same name outside the subroutine * It’s existence is limited to the scope of the subroutine sub printHello { my ($name) print "Hello $name\n"; } my $name = "Yossi"; printHello("Moshe"); print "Bye $name\n"; Note: This effect also holds for my variables in any other “block” of statements in curly brackets – {…} (such as in if-else controls and in loops) Variable scope Hello Moshe Bye Yossi

11ex.21 Class exercise 11b 1. Write a subroutine that takes two numbers and prints their sum to the screen (and test it with an appropriate script!) 2. Write a subroutine that takes two numbers and return a list of their sum, difference, and average. For = numbersFunc(5,7); print a. Write a subroutine that takes a sentence and returns the last word. b.* Return the longest word!