11.1 Subroutines. 11.2 A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr

Slides:



Advertisements
Similar presentations
Lecture 6 More advanced Perl…. Substitute Like s/// function in vi: #cut with EcoRI and chew back $linker = “GGCCAATTGGAAT”; $linker =~ s/CAATTG/CG/g;
Advertisements

Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
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 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
11.1 Variable types in PERL ScalarArrayHash $number $string %hash $array[0] $hash{key}
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.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
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");
5.1 Previously on... PERL course (let ’ s practice some more loops)
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}
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,
13.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
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.
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 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
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.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
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
subroutines and references
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Bioinformatics 生物信息学理论和实践 唐继军
Introduction to Perl Yupu Liang cbio at MSKCC
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
5 1 Data Files CGI/Perl Programming By Diane Zak.
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.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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),
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
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.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
 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.
The Scripting Programming Language
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
References and Data Structures
Modules and BioPerl.
Subroutines Web Programming.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Lesson 2. Control structures File IO - reading and writing Subroutines
COMPUTER PROGRAMMING SKILLS
Presentation transcript:

11.1 Subroutines

11.2 A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr = split (/\t/,$line); push $num); Takes a string and returns a sub-string Splits a line into an array Pushes a scalar to the end of an array

11.3 A function is a portion of code that performs a specific task. Functions Functions have arguments and return values: $start = substr ($str,1,4); Arguments: (STRING, OFFSET, LENGTH) Return value: This function returns a string

11.4 A subroutine is a user-defined function. sub SUB_NAME { # Do something... } Subroutines sub printHello { print "Hello World!\n"; } sub bark { print "Woof-woof\n"; } Subroutines can be placed anywhere, but are usually stacked together at the beginning or the end

11.5 To invoke (execute) a subroutine: SUB_NAME(ARGUMENTS); Subroutines For example: bark(); Woof-woof print reverseComplement("GCAGTG"); CACTGC

11.6  Code in a subroutine is reusable. For example: a subroutine that reverse-complement a DNA sequence  A subroutine can provide a general solution for different situations. For example: read a FASTA file  Encapsulation: A well defined task can be done in a subroutine, making the main script simpler and easier to read and understand. Why use subroutines?

11.7 my filename = $ARGV[0]; # Read fasta sequence from file $seq = readFastaFile($fileName); # Reverse complement the sequence $revSeq = reverseComplement($seq); # Print the reverse complement in fasta format printFasta($revSeq); # Subroutines definition Why use subroutines? - Example A general solution: works with different files Can be invoked from many points in the code And the program is beautiful

11.8 # Read fasta sequence from file open (IN, "<$filename") or die "Can't open file"; = ; my ($seq, $line); foreach my $line { if ($line =~ m/^>/) {next;} $seq = $seq.$line; } close (IN); # Reverse complement the sequence $seq =~ tr/ACGTacgt/TGCAtgca/; $revSeq = reverse ($seq); # Print the reverse complement in fasta format my $i = 0; while (($i+1) * 75 < length ($revSeq)) { my $fastaLine = substr($revSeq, $i * 75, 75); print $fastaLine."\n"; $i++; } $fastaLine = substr($revSeq, $i*75); print $fastaLine."\n" Why use subroutines? - Example Much better than this

11.9 A subroutine may be given arguments through the special array : my $bart4today = "I do not have diplomatic immunity"; bartFunc($bart4today,100); sub bartFunc { my ($string, $times) print $string x $times; } Subroutine arguments I do not have diplomatic immunity I do not have diplomatic immunity... We pass arguments to the subroutine Inside the subroutine block they are saved in the special

11.10 Definition: sub reverseComplement { my ($seq) $seq =~ tr/ACGT/TGCA/; $seq = reverse $seq; return $seq; } Usage: my $revSeq = reverseComplement("GCAGTG"); CACTGC Return value The return statement ends the execution of the subroutine and returns a value.

11.11 Definition: sub reverseComplement { my ($seq) $seq =~ tr/ACGT/TGCA/; $seq = reverse $seq; return $seq; print "I am the walrus!" } Usage: my $revSeq = reverseComplement("GCAGTG"); CACTGC Return value Everything after the return statement will be ignored

11.12 Definition: sub reverseComplement { my ($seq) $seq =~ tr/ACGT/TGCA/; $seq = reverse $seq; } Usage: my $revSeq = reverseComplement("GCAGTG"); CACTGC Return value If there is no return statement, the value of the last statement in the subroutine is returned.

11.13 A subroutine may also return an list value: sub firstLastChar{ my ($string) $string =~ m\^(.).*(.)$\; return ($1,$2); } my ($firstChar,$lastChar) = firstLastChar("Yellow"); print "First char: $firstChar, last one: $lastChar.\n"; First char: Y, last one: w. Return list Our subroutine returns a list of two elements. We pass an argument And receive a list of two return values

11.14 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 = "Liko"; printHello("Emma"); print "Bye $name\n"; Variable scope Hello Emma Bye Liko

11.15 Debugging subroutines Step into a subroutine (F5) to debug the internal work of the sub Step over a subroutine (F6) to skip the whole operation of the sub Step out of a subroutine (F7) when inside a sub – run it all the way to its end and return to the main script Resume (F8) run till end or next break point Step into Step out Step over

11ex.16 Class exercise 11a 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!

11.17 Arrays and hashes can be very big. That's why we want to pass a direct reference and not create a copy. If we want to pass arrays or hashes to a subroutine, we should pass a reference: Passing variables by reference Passing array references: subRoutine Passing hash references: subRoutine (\%hash);

11.18 If we want to pass arrays or hashes to a subroutine, we should pass a reference: Passing variables by reference Passing array references: subRoutine Passing hash references: subRoutine (\%hash); Dereferencing arrays: sub subRoutine { my Dereferencing hashes: sub subRoutine { my ($hashRef) %hash = %{$hashRef};...

11.19 If we want to pass arrays or hashes to a subroutine, we should pass a reference: Passing variables by reference = ('Liko','Emma','Louis'); printPets sub printPets { my ($petRef) foreach my $pet { print "Good $pet\n";} Reference De-reference of $petRef

11.20 If we want to pass arrays or hashes to a subroutine, we should pass a reference: my %newDetails; $newDetails{"name"} = "Eyal"; $newDetails{"address"} = = (98,72,86); $newDetails{"grades"} = printGeneInfo(\%newDetails); sub printDetails { my ($detailRef) my %details = %{$detailRef}; print "Name: ".$details{"name"}."\n"; print "Adr.: ".$details{"address"}."\n"; $details{"grades"} } print } Passing variables by reference Reference to %newDetail De-reference of $detailRef

11.21 Similarly, to return a hash use a reference: sub getDetails { my %geneInfo; $geneInfo{"name"} = ; $geneInfo{"address"} = ;... return \%geneInfo; } $geneRef = getGeneInfo(); In this case the hash continue to exists outside the subroutine! To dereference use: my %geneHashInfo = %{$geneRef} Returning variables by reference

11.22 We learned the default sort, which is lexicographic: = ("Liko","Emma","Louis"); = print Emma Liko Louis Sort revision

11.23 We learned the default sort, which is lexicographic: = (8,3,45,8.5); = print To sort by a different order rule we need to give a comparison subroutine – a subroutine that compares two scalars and says which comes first sort COMPARE_SUB Sort revision no comma here

11.24 sort COMPARE_SUB (LIST); COMPARE_SUB is a special subroutine that compares two scalars $a and $b, and says which comes first (by returning 1, 0 or -1 ). For example: sub compareNumber { if ($a > $b){return 1;} elsif ($a == $b){return 0;} else{return -1;} } = sort compareNumber (8,3,45,8.5); print Sorting numbers no comma here

11.25 The operator does exactly that – it returns 1 for “greater than”, 0 for “equal” and -1 for “less than”: sub compareNumber { return $a $b; } print sort compareNumber (8,3,45,8.5); For easier use, you can use a temporary subroutine definition in the same line: print sort {return $a $b;} (8,3,45,8.5); or just: print sort {$a $b;} (8,3,45,8.5); The operator

11.26 open (IN,"<fight club.txt"); = ; = sort sub compareLength{ return (length($a) length($b)); } Sorting example © th Century Fox - All Rights Reserved Welcome to Fight Club. Sixth rule: no shirt, no shoes. Fourth rule: only two guys to a fight. Fifth rule: one fight at a time, fellas....

11ex.27 Class exercise 11b 1.Solve ex11a.2 again, this time use references to pass the arguments and return their values. 2.Write a script that reads a file with a list of protein names and lengths: (such as proteinLengths) AP_ AP_ AP_ Print them sorted according to their lengthproteinLengths 3.Modify the solution for class_ex8.1: Make a subroutine that takes the name of an input file, builds the hash of protein lengths and returns a reference to the hash. Test it – see that you get the same results as the original class_ex.8.1. Feel free to use our solution of class_ex8.1… 4.Now do class_ex. 8.2 by adding another subroutine that takes: (1) a protein accession, (2) a protein length and (3) a reference to such a hash, and returns 0 if the accession is not found, 1 if the length is identical to the one in the hash, and 2 otherwise.

11ex.28 Class exercise 8 1.Write a script that reads a file with a list of protein names and lengths: AP_ AP_ AP_ stores the names of the sequences as hash keys, with the length of the sequence as the value. Print the keys of the hash. 2.Add to Q1: Read another file, and print the names that appeared in both files with the same length. Print a warning if the name is the same but the length is different. 3.Write a script that reads a GenPept file (you may use the preproinsulin record), finds all JOURNAL lines, and stores in a hash the journal name (as key) and year of publication (as value):the preproinsulin record a. Store only one year value for each journal name b*.Store all years for each journal name Then print the names and years, sorted by the journal name (no need to sort the years for the same journal in b*, unless you really want to do so … )