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

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

Welcome to lecture 5: Object – Oriented Programming in Perl IGERT – Sponsored Bioinformatics Workshop Series Michael Janis and Max Kopelevich, Ph.D. Dept.
INTRODUCTION TO BIOPERL Gautier Sarah & Gaëtan Droc.
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
The BioPerl project is an international association of developers of open source Perl tools for bioinformatics, genomics and life science research.
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");
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.
Sup.1 Supplemental Material (NOT part of the material for the exam)
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
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.
1 Perl Programming for Biology The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel October 2009 By Eyal Privman and Dudu.
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 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
12ex.1. 12ex.2 The BioPerl project is an international association of developers of open source Perl tools for bioinformatics, genomics and life science.
Bioperl modules.
13.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
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";
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.
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);
Python quick start guide
13.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
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
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
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,
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Functions and subroutines – Computer and Programming.
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
13.1 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Beginning BioPerl for Biologists MPI Ploen Jun Wang.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Introduction to Perl Yupu Liang cbio at MSKCC
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
Python Functions.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
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.
BioPerl Ketan Mane SLIS, IU. BioPerl Perl and now BioPerl -- Why ??? Availability Advantages for Bioinformatics.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
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.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
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.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
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.
Character (String) Data
Subroutines Web Programming.
Presentation transcript:

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

12.2 Subroutines

12.3 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"; }

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

12.5  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?

12.6 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

12.7 A subroutine may be given arguments through the special array sub printString_N_times { my ($string, $times) print $string x $times; } my $bart4today = "I will not eat things for money\n"; printString_N_times($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

12.8 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

12.9 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 ($mana,$sheerit) = integerDivide(7,3); print "mana= $mana, sheerit= $sheerit"; mana= 2, sheerit= 1 Return value a list

12.10 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

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

12.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! To dereference use: my %geneHashInfo = %{$geneRef} Returning variables by reference

12.13 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

12.14 Class exercise 12a 1.Write a subroutine that takes two numbers and return a list of their sum, difference, and average. For = mubersFunc(5,7); print 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 9a.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.9a.1 4.Now do ex. 9a.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.

12.15 Modules

12.16 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. Fasta.pm: may contain subroutines that read and write FASTA files: readFasta(), writeFasta(), getHeaders(), getSeqNo(). What are modules

12.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 Fasta; sub getHeaders {... } sub getSeqNo {... } The last line of the module must be a true value, so usually we just add: 1; Writing a module

12.18 In order to write a script that uses a module add a “ use ” line at the beginning of the script: use Fasta; Note #1: for basic use of modules put the module file is in the same directory as your script, otherwise Perl won ’ t find it! Note #2: You can “ use ” inside a module another module and you can have as many “ use ” as you want. Using modules * If you want to learn how to “ use ” a module from a different directory read about “ use lib ”

12.19 use Fasta; Now we can invoke a subroutine from within the namespace of that package: PACKAGE::SUBROUTINE(...) e.g. $seq = Fasta::getSeqNo(3); Note that we cannot access it without specifying the namespace: $seq = getSeqNo(3); Undefined subroutine &main::getSeqNo 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 - namespaces

12.20 Class exercise 12b 1. Change the solution for class ex12a.4 (the protein-lengths hash) – move the two subroutines to a module by the name proteinLengths.pm, and make the necessary changes in the script. (you are welcome to use our suggested solution)

12.21

12.22 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

12.23 BioPerl modules are called Bio::XXX You can use the BioPerl wiki: with documentation and examples for how to use them – which is the best way to learn this. We recommend beginning with the "How-tos": To a more hard-core inspection of BioPerl modules: BioPerl Module Documentation BioPerl

12.24 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. To create an object from a certain package use “ new ” : my $obj = new PACKAGE; e.g. my $in = new FileHandle; New returns a reference to a data structure, which acts as a FileHandle object. New can also receive arguments: my $obj = new PACKAGE; my $in = new FileHandle(">$inFile"); Object-oriented use of packages $obj 0x225d14 func() anotherFunc()

12.25 To invoke a subroutine from the package for a specific object we use the “ -> ” notation again: $line = $in->getLine(); Note that this is different from accessing elements of a reference to an array or hash, because we don ’ t have brackets around “ getLine ” : $length = $proteinLengths->{AP_000081}; $grade = $gradesRef->[0]; Object-oriented use of packages $obj 0x225d14 func() anotherFunc()

12.26 The Bio::SeqIO module allows input/output of sequences from/to files, in many formats: use Bio::SeqIO; $in = new Bio::SeqIO("-file" => " "EMBL"); $out = new Bio::SeqIO("-file" => ">seq2.fasta", "-format" => "Fasta"); while ( my $seq = $in->next_seq() ) { $out->write_seq($seq); } A list of all the formats BioPerl can Handle can be found in: BioPerl: the SeqIO module