Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "13.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה."— Presentation transcript:

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

2 12.2 Defining a subroutine Subroutines Revision sub reverseComplement { # read string # reverseComplement it # return it } sub SUB_NAME { # Do something... }

3 12.3 $retval = SUB_NAME (ARGS); $reversed = reverseComplement ("ACGTTA"); Invoking a subroutine Subroutines Revision sub reverseComplement { # read string # reverseComplement it # return it }

4 12.4 $reversed = reverseComplement ("ACGTTA"); Invoking a subroutine Subroutines Revision sub reverseComplement { # read string # reverseComplement it # return it } When a subroutine is invoked, perl begins to run it separately from the main program $reversed

5 12.5 $reversed = reverseComplement( ); Passing arguments: Subroutines Revision sub reverseComplement { # read string # reverseComplement it # return it } Arguments are passed using the special array @_ @_ "ACGTTA" $reversed

6 12.6 $reversed = reverseComplement( ); Passing arguments: Subroutines Revision sub reverseComplement { ($seq) = @_; # reverseComplement it # return it } We can then read the arguments from @_ "ACGTTA" $reversed @_ "ACGTTA" $seq "ACGTTA"

7 12.7 $seq "ACGTTA" $reversed = reverseComplement( ); Subroutines Revision sub reverseComplement { my ($seq) = @_; $seq =~ tr/ACGT/TGCA/; my $revSeq = reverse $seq; # return it } We can now add whatever content we want to the subroutine "ACGTTA" @_ "ACGTTA" $reversed $seq "TGCAAT" $revSeq "TAACGT"

8 12.8 $reversed = reverseComplement( ); Returning return values: Subroutines Revision sub reverseComplement { my ($seq) = @_; $seq =~ tr/ACGT/TGCA/; my $revSeq = reverse $seq; return $revSeq; } "ACGTTA" We return values using the word return $reversed @_ "ACGTTA" $seq "ACGTTA" $seq "TGCAAT" $revSeq "TAACGT"

9 12.9 $last my ($first, $last) = firstLastChar("Yellow"); Another example: Subroutines Revision sub firstLastChar{ my ($string) = @_; $string =~ m/^(.).*(.)$/; return ($1,$2); } @_ $string "Yellow" $first "Y" $1 "Y" "w" $2 "w"

10 12.10 my @ourPets = ('Liko','Emma','Louis'); printPets (\@ourPets); Passing references: Subroutines Revision sub printPets { my ($petRef) = @_; foreach my $pet (@{$petRef}) { print "Good $pet\n";} $ourPets We create a reference to the array 'Emma''Liko''Louis'

11 12.11 my @ourPets = ('Liko','Emma','Louis'); printPets (\@ourPets); Passing references: Subroutines Revision sub printPets { my ($petRef) = @_; foreach my $pet (@{$petRef}) { print "Good $pet\n";} @_ $ourPets Then we pass the reference to the subroutine

12 12.12 my @ourPets = ('Liko','Emma','Louis'); printPets (\@ourPets); Passing references: Subroutines Revision sub printPets { my ($petRef) = @_; foreach my $pet (@{$petRef}) { print "Good $pet\n";} @_ $petRef $ourPets

13 12.13 my @ourPets = ('Liko','Emma','Louis'); printPets (\@ourPets); Passing references: Subroutines Revision sub printPets { my ($petRef) = @_; foreach my $pet (@{$petRef}) { print "Good $pet\n";} @_ $petRef $ourPets Good Liko Good Emma Good Louis

14 12.14 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

15 12.15 Modules

16 12.16 A module or a package is a collection of subroutines, stored in a separate file with a “.pm ” suffix (Perl Module). The subroutines of a module should deal with a well-defined task. What are modules? sub getHeaders {... } sub getSeqNo {... } sub readFasta {... } Fasta.pm For example: manipulate fasta files.

17 12.17 A module is 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: Writing a module package Fasta; sub getHeaders { # Get all the fasta headers } sub getSeqNo { # Return the number of # sequences in the file } Fasta.pm

18 12.18 The last line of the module has to have true value, so we add: 1; Writing a module package Fasta; sub getHeaders { # Get all the fasta headers } sub getSeqNo { # Return the number of # sequences in the file } 1; Fasta.pm

19 12.19 In order to use a module, we write the line: use MODULE_NAME; at the beginning of our script. Using modules package Fasta; sub readFasta {... } sub getSeqNo {... } 1; Fasta.pm use strict; use Fasta; # Here we will use the subroutines # defined in the Fasta.pm module MyScript.pl We use the subroutines that we defined in the module Fasta

20 12.20 use strict; use Fasta; # Here we will use the subroutines # defined in the Fasta.pm module MyScript.pl Tips for using modules package Fasta; sub readFasta {... } sub getSeqNo {... } Fasta.pm Both files must be in the same directory. To use files from different directories, read about use lib. use strict; use Fasta; use geneBank; # Here we will use the subroutines # defined in the Fasta.pm module MyScript.pl We can use as many modules as we want in a single script package Fasta; use geneBank; sub readFasta {... } sub getSeqNo {... } 1; Fasta.pm We can even use modules inside modules.

21 12.21 Now we can invoke a subroutine from within the namespace of that module: PACKAGE::SUBROUTINE(ARGUMENTS) Using modules - namespaces use strict; use Fasta; use geneBank; @seqs = Fasta::readFasta("ec.fasta"); Fasta::printFasta(@seqs); MyScript.pl package Fasta; use geneBank; sub readFasta {... } sub printFasta {... } 1; Fasta.pm

22 12.22 We can't access the module without specifying the namespace Using modules - namespaces use strict; use Fasta; use geneBank; @seqs = readFasta("ec.fasta"); printFasta(@seqs) MyScript.pl package Fasta; use geneBank; sub readFasta {... } sub printFasta {... } 1; Fasta.pm Undefined subroutine &main::getSeqNo called at...

23 12.23 Class exercise 12a 1.a)Write a module Fasta.pm that contains a subroutine headers( ): receive a Fasta filename and returns a reference to an array containing all the headers. Write a script that test your module on EHD_nucleotide.richFasta. b) Add to the module the following subroutine: seqLengths( ): receive a Fasta filename and returns a reference to an array containing all the sequences lengths. Test this subroutine as well.EHD_nucleotide.richFasta 2.(Home ex6 q3) Create a module readSeq.pm with the following functions: a) readFastaSeq( ): Reads sequences from a FASTA file. Return a hash – the header lines are the keys and the sequences are the values (similar to ex5.1b). b) readGenbak( ): Reads a genome annotations (such as adeno12.gbs) file and extract CDS information (similar to class_ex. 10b.2a) as follows: $genes{$name}{"protein_id"} = PROTEIN_ID $genes{$name}{"strand"} = STRAND $genes{$name}{"CDS"} = [START, END] Return a reference to the complex data structure. Test the module with an appropriate script!adeno12.gb

24 12.24 BioPerl

25 12.25 The BioPerl project is an international association of developers of open source Perl tools for bioinformatics, genomics and life science research. Things you can do with BioPerl: Read and write sequence files of different format, including: Fasta, GenBank, EMBL, SwissProt and more … Extract gene annotation from GenBank, EMBL, SwissProt files Read and analyse BLAST results. Read and process phylogenetic trees and multiple sequence alignments. Analysing SNP data. And more … BioPerl

26 12.26 Many packages are meant to be used as objects. In Perl, an object is a data structure that can use subroutines that are associated with it. We will not learn object oriented programming, but we will learn how to create and use objects defined by BioPerl packages. Object-oriented use of packages $obj 0x225d14 func() anotherFunc()

27 12.27 BioPerl modules are named Bio::xxxx The Bio::SeqIO module deals with Seq uences I nput and O utput: In order to create a new SeqIO object, use new Bio::SeqIO as follows: use Bio::SeqIO; my $in = new Bio::SeqIO(); BioPerl: the SeqIO module

28 12.28 BioPerl modules are named Bio::xxxx The Bio::SeqIO module deals with Seq uences I nput and O utput: We will pass arguments to the new argument of the file name and format use Bio::SeqIO; my $in = new Bio::SeqIO( "-file" => " "GenBank"); BioPerl: the SeqIO module File argument (filename as would be in open ) A list of all the sequence formats BioPerl can read is in: http://www.bioperl.org/wiki/HOWTO:SeqIO#Formats http://www.bioperl.org/wiki/HOWTO:SeqIO#Formats Format argument $in 0x25e211 next_seq() write_seq()

29 12.29 use Bio::SeqIO; my $in = new Bio::SeqIO( "-file" => " "GenBank"); my $seqObj = $in->next_seq(); BioPerl: the SeqIO module $in 0x25e211 next_seq() write_seq() next_seq() returns the next sequence in the file as a Bio::Seq object (we will talk about them soon) Perform next_seq() subroutine on $in You could think of it as: SeqIO::next_seq($in)

30 12.30 use Bio::SeqIO; my $in = new Bio::SeqIO( "-file" => "<seq.gb", "-format" => "GenBank"); my $out = new Bio::SeqIO("-file" => ">seq2.fasta", "-format" => "Fasta"); my $seqObj = $in->next_seq(); while (defined $seqObj){ $out->write_seq($seqObj); $seqObj = $in->next_seq(); } BioPerl: the SeqIO module $in 0x25e211 next_seq() write_seq() $out 0x3001a3 next_seq() write_seq() write_seq() write a Bio::Seq object to $out according to its format

31 12.31 use Bio::SeqIO; my $in = new Bio::SeqIO( "-file" => "<seq.fasta", "-format" => "Fasta"); my $seqObj = $in->next_seq(); while (defined $seqObj) { print "ID:".$seqObj->id()."\n"; #1st word in header print "Desc:".$seqObj->desc()."\n"; #rest of header print "Sequence: ".$seqObj->seq()."\n"; #seq string print "Length:".$seqObj->length()."\n";#seq length $seqObj = $in->next_seq() } You can read more about the Bio::Seq subroutines in: http://www.bioperl.org/wiki/HOWTO:Beginners#The_Sequence_Object BioPerl: the Seq module Bio:seqIO includes use Bio::Seq This one is extremely useful...

32 12.32 Printing last 30aa of each sequence open (IN, "<seq.fasta") or die "Cannot open seq.fasta..."; my $fastaLine = ; while (defined $fastaLine) { chomp $fastaLine; # Read first word of header if (fastaLine =~ m/^>(\S*)/) { my $header = substr($fastaLine,1); $fastaLine = ; } # Read seq until next header my $seq = ""; while ((defined $fastaLine) and(substr($fastaLine,0,1) ne ">" )) { chomp $fastaLine; $seq = $seq.$fastaLine; $fastaLine = ; } # print last 30aa my $subseq = substr($seq,-30); print "$header\n"; print "$subseq\n"; }

33 12.33 Now using BioPerl use Bio::SeqIO; my $in = new Bio::SeqIO("-file" => " "Fasta"); my $seqObj = $in->next_seq(); while (defined $seqObj) { # Read first word of header my $header = $seqObj->id(); # print last 30aa my $seq = $seqObj->seq(); my $subseq = substr($seq,-30); print "$header\n"; print "$subseq\n"; $seqObj = $in->next_seq(); }

34 12.34 Class exercise 12b 1.Write a script that uses Bio::SeqIO to read a FASTA file (use the EHD nucleotide FASTA from the webpage) and print to an output FASTA file only sequences shorter than 3,000 bases. 2.Write a script that uses Bio::SeqIO to read a FASTA file, and print (to the screen) all header lines that contain the words " Mus musculus " (you may use the same file). 3.Write a script that uses Bio::SeqIO to read a GenPept file (use preProInsulinRecords.gp from the webpage), and convert it to FASTA. preProInsulinRecords.gp 4*.Same as Q1, but print to the FASTA the reverse complement of each sequence. (Do not use the reverse or tr// functions! BioPerl can do it for you - read the BioPerl documentation).

35 12.35 Installing BioPerl – how to add a repository to the PPM Start  All Programs  Active Perl…  Perl Package manager You might need to add a repository to the PPM before installing BioPerl:

36 12.36 Installing BioPerl – how to add a repository to the PPM Click the “Repositories” tab, enter “bioperl” in the “Name” field and http://bioperl.org/DIST in the “Location” field, click “Add”, and finally “OK”:

37 12.37 Installing modules from the internet The best place to search for Perl modules that can make your life easier is: http://www.cpan.org/ http://www.cpan.org/ The easiest way to download and install a module is to use the Perl Package Manager (part of the ActivePerl installation) Note: ppm installs the packages under the directory “site\lib\” in the ActivePerl directory. You can put packages there manually if you would like to download them yourself from the net, instead of using ppm. 1.Choose “ View all packages ” 2. Enter module name (e.g. bioperl) 3. Choose module (e.g. bioperl) 5. Install! 4. Add it to the installation list

38 12.38 BioPerl installation In order to add BioPerl packages you need to download and execute the bioperl10.bat file from the course website. If that that does not work – follow the instruction in the last three slides of the BioPerl presentation. Reminder: BioPerl warnings about: Subroutine... redefined at... Should not trouble you, it is a known issue – it is not your fault and won't effect your script's performances.

39 12.39 Installing modules from the internet Alternatively in older Active Perl versions- Note: ppm installs the packages under the directory “site\lib\” in the ActivePerl directory. You can put packages there manually if you would like to download them yourself from the net, instead of using ppm.


Download ppt "13.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה."

Similar presentations


Ads by Google