9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.

Slides:



Advertisements
Similar presentations
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Advertisements

Advanced Perl WUC Perl Poetry Variable Scope.
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
12.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
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}
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
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) {
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)
Subroutines sub { –#parameters are placed – –. –return; }
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:
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,
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
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 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
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";
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
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.
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
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
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.
subroutines and references
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
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.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
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
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
CPTG286K Programming - Perl Chapter 4: Control Structures.
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
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Introduction to PERL Genetics PERL is a language that is easy to use and was designed to do certain tasks (like reading, writing, moving text.
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)
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
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.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Modules and BioPerl.
Subroutines Web Programming.
The structure of programming
COMPUTER PROGRAMMING SKILLS
REPETITION Why Repetition?
Presentation transcript:

9.1 Subroutines and sorting

9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } 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"; }

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

9.4 Code in a subroutine is reusable (i.e. it can be invoked from several points in the script, preventing the need to duplicate code) 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 Encapsulation: A well defined task can be done in a subroutine, making the main script simpler and easier to read and understand. For example… Why use subroutines?

9.5 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?

9.6 A subroutine may be given arguments through the special array sub printName { my ($name, $isFriend) if ($isFriend eq "yes") { print "Hello $name!"; } } printName("Yossi","yes"); printName("Moshe","no"); Subroutine arguments Hello Yossi!

9.7 A subroutine may return a scalar value or a list value: sub reverseComplement { my ($seq) $seq =~ tr/ACGT/TGCA/; $seq = reverse $seq; return $seq; } my $revSeq = reverseComplement("GCAGTG"); CACTGC The return function ends the execution of the subroutine and returns a value. If there is no return statement, the return value will be the value of the last statement in the subroutine. Return value

9.8 A subroutine may return a scalar value or a list value: sub integerDivide { my ($a,$b) my $mana = int($a/$b); my $sheerit = $a % $b; return ($mana,$sheerit); } my ($mana,$sheerit) = integerDivide(7,3); print "mana= $mana, sheerit= $sheerit"; mana= 2, sheerit= 1 The return function ends the execution of the subroutine and returns a value. If there is no return statement, the return value will be the value of the last statement in the subroutine. Return value

9.9 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"; 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

9.10 If we want to pass arrays or hashes to a subroutine, we must pass a reference: %gene = ("protein_id" => "E4a", "strand" => "-", "CDS" => [126,523]); printGeneInfo(\%gene); sub printGeneInfo { my ($geneRef) print "Protein $geneRef->{'protein_id'}\n"; print "Strand $geneRef->{'strand'}\n"; print "From: $geneRef->{'CDS'}[0] "; print "to: $geneRef->{'CDS'}[1]\n"; } Passing variables by reference

9.11 What if we wanted to invoke this subroutine on every gene in the hash of genes that we created in The previous exercise? foreach $geneRef (values(%genes)) { printGeneInfo($geneRef); } Passing variables by reference %genes NAME => {protein_id => PROTEIN_ID strand => STRAND CDS => [START, END]}

9.12 Similarly, to return a hash use a reference: sub getGeneInfo { my %geneInfo; (fill hash with info) return \%geneInfo; } $geneRef = getGeneInfo(..); In this case the hash will continue to exists outside the scope of the subroutine! Returning variables by reference

9.13 Class exercise Write a subroutine that takes two numbers and prints their sum to the screen (and test it with an appropriate script!) 2. a. Write a subroutine that takes a sentence and returns the last word. b.* Return the longest word! 3. Modify your solution for class exercise 9.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 ex Now do ex. 9.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. 5.* Now add a third input file and check if all three are in agreement – print a list of all proteins that have the same length in all three files, and print a warning for every protein with a disagreement between any two files.

9.14 We learned the default sort, which is lexicographic: print sort("Yossi","Bracha","Moshe"); Bracha Moshe Yossi print sort(8,3,45,8.5); 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 (LIST); Advanced sorting no comma here

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

9.16 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 {$a $b} (8,3,45,8.5); The operator

9.17 Now we can also sort complex = sort sub compareGenes { if ($a->{"CDS"}[0] > $b->{"CDS"}[0]) {return 1;} elsif ($a->{"CDS"}[0] == $b->{"CDS"}[0]){return 0;} else {return -1;} {protein_id => PROTEIN_ID strand => STRAND CDS => [START, END]}

9.18 Now we can also sort complex = sort sub compareGenes { if ($a->{"CDS"}[0] > $b->{"CDS"}[0]) {return 1;} elsif ($a->{"CDS"}[0] == $b->{"CDS"}[0]) { if ($a->{"CDS"}[1] > $b->{"CDS"}[1]){return 1;} elsif ($a->{"CDS"}[1] == $b->{"CDS"}[1]){return 0;} else{return -1;} } else {return -1;} {protein_id => PROTEIN_ID strand => STRAND CDS => [START, END]}

9.19 Now we can also sort complex = sort sub compareGenes { if ($a->{"CDS"}[0] > $b->{"CDS"}[0]) {return 1;} elsif ($a->{"CDS"}[0] == $b->{"CDS"}[0]) { return ($a->{"CDS"}[1] $b->{"CDS"}[1]); } else {return -1;} {protein_id => PROTEIN_ID strand => STRAND CDS => [START, END]}

9.20 Class exercise 12 Write scripts that read an input file with the following data, sort them and print them in a sorted order to the screen: 1. Sort a file of grades and names, according to the grades (e.g. grades.txt from the course website). 2. Sort a file where each line is a date. e.g. 24/7/2003 (e.g. dates.txt). 3. Sort the proteins in the file from ex. 9.1 by their lengths (create an array of keys sorted by the protein lengths). 4.* From the home exercise 4: Sort the CDSs from the adeno genome file: - First by the number of the exons - Then by the length of the CDS (without the introns!) e.g. E1B 55K (1 exon, 1449bp) comes before E1A (2 exons, 801), but after E1B 19K (1 exon, 492bp). Use an array of gene hashes as in class ex. 10, and an appropriate comparison subroutine. Print the sorted protein IDs with their number of exons and lengths of CDS.