10.1 Sorting and Modules. 10.2 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.

Slides:



Advertisements
Similar presentations
Advanced Perl WUC Perl Poetry Variable Scope.
Advertisements

Lecture 6 More advanced Perl…. Substitute Like s/// function in vi: #cut with EcoRI and chew back $linker = “GGCCAATTGGAAT”; $linker =~ s/CAATTG/CG/g;
Welcome to lecture 5: Object – Oriented Programming in Perl IGERT – Sponsored Bioinformatics Workshop Series Michael Janis and Max Kopelevich, Ph.D. Dept.
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 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Programming and Perl for Bioinformatics Part III.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
VBA Modules, Functions, Variables, and Constants
11.1 Variable types in PERL ScalarArrayHash $number $string %hash $array[0] $hash{key}
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)
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.
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 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
7.1 Some Eclipse Tips Try Ctrl+Shift+L Quick help (keyboard shortcuts) Try Ctrl+SPACE Auto-complete Source→Format ( Ctrl+Shift+F ) Correct indentation.
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.
I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){
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";
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.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
13r.1 Revision (Q&A). 13r.2 $scalar 13r.3 Multiple assignment my ($a,$b) = ('cow','dog'); = = (6,7,8,9,10);
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
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.
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.
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
13.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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.
How to write & use Perl Modules. What is a Module? A separate Namespace in a separate file with related functions/variables.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
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.
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
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
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.
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.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
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.
Advanced Perl For Bioinformatics Part 1 2/23/06 1-4pm Module structure Module path Module export Object oriented programming Part 2 2/24/06 1-4pm Bioperl.
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.
Perl Ed Finegan. Overview of Pearl Perl is a high-level programming language written by Larry Wall. It derives from the C programming language and to.
Modules and BioPerl.
Subroutines Web Programming.
SPL – PS1 Introduction to C++.
Presentation transcript:

10.1 Sorting and Modules

10.2 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה

10.3 sub reverseComplement { my ($seq) $seq =~ tr/ACGT/TGCA/; $seq = reverse $seq; return $seq; } my $revSeq = reverseComplement("GCAGTG"); CACTGC A subroutine receives its arguments and may return a scalar or a list value: Subroutine revision

10.4 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

10.5 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]}

10.6 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

10.7 Debugging subroutines Step into a subroutine (F7) to debug the internal work of the sub Step over a subroutine (F8) to skip the whole operation of the sub Step out of a subroutine (Ctrl+F7) when inside a sub – run it all the way to its end and return to the main script

10.8 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

10.9 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;} } print sort compareNumber (8,3,45,8.5); Sorting numbers no comma here

10.10 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

10.11 Now we can also sort complex = sort sub compareGenes { return $a->{"CDS"}[0] $b->{"CDS"}[0]; {protein_id => PROTEIN_ID strand => STRAND CDS => [START, END]}

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

10.13 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.

10.14 Modules

10.15 A module or a package is a collection of subroutines, usually stored in a separate file with a “.pm” suffix (Perl Module). The subroutines of a module should deal with a well-defined task. e.g. The file FileHandle.pm may contain a module of subroutines that read and write files, such as open, getLine, and print. In order to write a script that uses a module add a “ use ” line at the beginning of the script: use FileHandle; Note: for basic use of modules put the module file is in the same directory as your script, otherwise Perl won’t find it! Using modules

10.16 use FileHandle; Now we can invoke a subroutine from within the namespace of that package: PACKAGE::SUBROUTINE(...) e.g. FileHandle::getLine(); Note that we cannot access it without specifying the namespace: GetLine(); Undefined subroutine &main::getLine called at... Perl tells us that no subroutine by that name is defined in the “main” namespace (the global namespace) There is a way to avoid this by using the “Exporter” module that allows a package to export it’s subroutine names. You can read about it here: Using modules

10.17 A module is usually written in a separate file with a “.pm” suffix. The name of the module is defined by a “ package ” line at the beginning of the file: package FileHandle; use strict;  We may use inside the module… sub getLine { The last line of the module must be a true value, so usually we just add: 1; Writing a module

10.18 Class exercise Complete class ex11.4 (the protein-lengths hash) and then – move the two subroutines to a module by the name proteinLengths.pm and use it in your script. 2. Create a module called readSeq.pm with the following functions: readFastaSeq: Reads sequences from a FASTA file. Return a hash – the header lines are the keys and the sequences are the values. readGenbank: Reads a genome annotations file and extract CDS information, as in class ex. 10, and in home ex. 4 question 5. Return the complex data structure. Test with an appropriate script! 3.* Use the readSeq.pm module to solve home exercise 4 question 6.