CS 360 Perl Part 2. Remember  Assignments are Due Today  Make a web page for all of your assignments  You get to design it so it works well for you.

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
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
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.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
1 Writing Perl 5 Modules: An Introduction Keith Arner.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
PERL Variables and data structures Andrew Emerson, High Performance Systems, CINECA.
subroutines and references
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
An Introduction to Unix Shell Scripting
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
1 Object Oriented Programming (OOP). Exam Friday Paper questions –6 Subroutines BLAT/BLAST Hash Modules Sorting 2.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
96-Summer 生物資訊程式設計實習 ( 二 ) Bioinformatics with Perl 8/13~8/22 蘇中才 8/24~8/29 張天豪 8/31 曾宇鳯.
Introduction to Perl Yupu Liang cbio at MSKCC
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Optimizing in Perl By Peter Wad Sackett. Optimizing the code – minor gains 1 ++$i and $st.= $data instead of $i = $i+1 and $st = $st. $data Use index.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Perl II Part III: Motifs and Loops. Objectives Search for motifs in DNA or Proteins Interact with users at the keyboard Write data to files Use loops.
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.
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
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Week Four Agenda Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
CS 360 Internet Programming. Objectives for this class period Web programming is important Administrative organization of the course Introduction to first.
Perl Subroutines User Input Perl on linux Forks and Pipes.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Midterm Review October Closed book one hand written page of notes of your own making October Closed book one hand written page of notes of.
Lecture 9: Basic concepts of Perl Modules. Functions (Subs) In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements.
CS 330 Class 7 Comments on Exam Programming plan for today:
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Perl Reliability Workshop
Perl Modules.
Subroutines Web Programming.
Lesson 2. Control structures File IO - reading and writing Subroutines
Subroutines.
Presentation transcript:

CS 360 Perl Part 2

Remember  Assignments are Due Today  Make a web page for all of your assignments  You get to design it so it works well for you  the TA with the URL  Lab is due Monday  Clean out your directory  We are asking for more space

Example  dna1.txt has the following in it.  hopper acgtacacactgca  flick acgcacacattgca  spot acgcacaccttgca  How would you write a perl script to read this file in and print it (assume that the command line has the file name in it)

Whoa!! What about the command line  How  #/usr/bin/perl  print "\n";  print "ARGV[0]=",$ARGV[0], "\n";  print "ARGV[1]=",$ARGV[1], "\n";  open(DNAFILE, $ARGV[0]) or die "cant open $ARGV[0]: $!";  while( ) {  print "line $_\n";  }

Now search for the taxa “hopper” Whoa!! How do I search!!  print "line $_\n"; = split /\s+/, $_;  print "$words[0] \n";  print "$words[1] \n";  if($words[0] eq "hopper") {  print "found hopper: dna $words[1]\n";  } else {  print "not hopper";  }

search for the string “hopper” Whoa!! How do I search!!  print "line $_\n"; = split /\s+/, $_;  print "$words[0] \n";  print "$words[1] \n";  if($words[0] eq "hopper") {  print "found hopper: dna $words[1]\n";  } else {  print "not hopper";  }

Perl Regular Expresions  Extremely Powerful Text Processing.  One of Perls most useful yet most misunderstood features  ‘=~’ indicates a regexp match  if ($var =~ /BLAH/) – Match string  if ($var =~ /^BLAH/) – Start of String  if ($var =~ /BLAH$/) – End of String  if ($var =~ /\w+/) – Any letters  \w - Letters  \d - Numbers  \s - Whitespace . - Anything reg.pl

Now search for the taxa “hopper” Whoa!! How do I search!!  print "line $_\n"; = split /\s+/, $_;  print "$words[0] \n";  print "$words[1] \n";  if($words=~/^hop|^fli/) {  print "found hopper or flick: dna $words[1]\n";  } else {  print "not either";  }

Perl – Everything else  Supplied Perl Docs  man perlfunc  man perlfaq  man perlsyn  man perlre (fun)  The Perl Bible  “Perl In a Nutshell” – O’Reilly

Now for some Magic  $dna = $words[1];  print "dna $dna \n";  $reversed = scalar reverse $dna;  print "reversed dna $reversed \n";  $rna = $reversed;  $rna =~ tr/acgt/UGCA/;

Hash Tables  Index using a variable # Hashes my %organisms = ( grasshoppers => "gh", fleas => "fs", lobster => "lb", ); = keys %organisms; = values %organisms; print "names ", abbrev "\n"; print "fleas = ", $organisms{"fleas"}, "\n"; print %organisms, "\n";

Nucleotide Translation  $codonMap{"gct"}="A"; #Alanine  $codonMap{"tgt"}="C"; #Cysteine  $codonMap{"gcc"}="A"; #Alanine  $codonMap{"tgc"}="C"; #Cysteine  $mrna="gcttgtgcctgc";  my $pro="";  while ($mrna=~s/(...)//) { # Dots match any character  print "codon $1\n";  $pro=$pro.$codonMap{$1};  print "pro $pro\n";  }

Now for the fun stuff !!  cd ~/public_html  vi hits.cgi  :r ~clement/public_html/hits.cgi  :wq  chmod a+x hits.cgi  Open in a web browser 

Why use subroutines?  They will make your program  Shorter, since you're reusing the code.  Easier to test, since you can test the subroutine separately.  Easier to understand, since it reduces clutter and better organizes programs.  More reliable, since you have less code when you reuse subroutines, so there are fewer opportunities for something to go wrong.  Faster to write, since you may, for example, have already written some subroutines that handle basic statistics and can just call the one that calculates the mean without having to write it again. Or better yet, you found a good statistics library someone else wrote, and you never had to write it at all.

Description  Like many languages, Perl provides for user-defined subroutines.  The Perl model for subroutine call and return values is simple: all subroutines are passed as parameters one single flat list of scalars, and all subroutines likewise return to their caller one single flat list of scalars.  Any arguments passed to the subroutine come in as the  The return value of the subroutine is the value of the last expression evaluated. Alternatively, a return statement may be used to exit the subroutine

To declare subroutines sub NAME; # A "forward" declaration. sub NAME(PROTO); # ditto, but with prototypes sub NAME BLOCK # A declaration and a definition. sub NAME(PROTO) BLOCK # ditto, but with prototypes

To call subroutines NAME(LIST); OR &NAME(LIST); # & is optional with parentheses. NAME LIST; # Parentheses optional if predeclared/imported. &NAME; # Makes visible to called subroutine.

Example (similarity.pl) sub percent_identity { $seq1 = $_[0]; $len1 = length $seq1; $seq2 = $_[1]; $len2 = length $seq2; $num_mismatches = 0; for $i (0..$len1-1) { if (substr($seq1, $i,1) ne substr($seq2, $i, 1)) { $num_mismatches++; } if($len2 > $len1) { $num_mismatches += ($len2-$len1); } return (($len1-$num_mismatches)*100/$len1); } $seq1="acctgaatg"; $seq2="atcgtgagtg"; print "percent identity = ". percent_identity($seq1, $seq2). "\n";

Scoping and Arguments  The variables declared with a my belong only to the block in which they are declared. In out example, $DNA has effect only in the subroutine.  You don’t have to worry about name conflicts outside the subroutine  You don’t have to worry about accidentally change the values of some variables  All the arguments passed to the subroutine are stored in the You can access parameters with  my $DNA = $_[0];

Another simple example #!/usr/bin/perl -w # Counting the number of G's in some DNA my($DNA) = "ACGAGCTGCGAGGCGACTAGCGAGCTAGCGATCAGCTA"; # Call the routine that does the real work and collect the result. my($number_of_Gs) = countG($DNA); print "\nThe DNA sequence $DNA has $number_of_Gs G\'s in it.\n\n"; exit; ######################################################### # Subroutines ######################################################### sub countG { my($DNA) my($count) = 0; $count = ($DNA =~ tr/Gg//); return $count; }

Pass by value  The values of these arguments are copied and passed to the subroutines.  whatever happens to those values in the subroutine doesn't affect the values of the arguments in the main program

Pass-by-reference (reference.pl) #!/usr/bin/perl # Example of pass-by-reference (a.k.a. call-by-reference) use strict; use warnings; = ('1', '2', '3'); = ('a', 'b', 'c'); print "In main program after calling subroutine: i = ". print "In main program after calling subroutine: j = ". exit; ############################################################ # Subroutine ############################################################ sub reference_sub { my ($i, $j) print "In subroutine : i = ". print "In subroutine : j = ". # push and shift are built-in functions on arrays '4'); } In main program before calling subroutine: i = 1 2 3, j = a b c In subroutine : i = 1 2 3, j = a b c In main program after calling subroutine: i = In main program after calling subroutine: j = b c

Pass-by-reference  To pass a parameter by reference, you have to preface the name of the parameter with a backslash.  is a reference to  In the subroutine, $i gets the value of So it is also a reference to  When argument variables are passed in this fashion, anything you do to the values of the argument variables in the subroutine also affects the values of the arguments in the main program.

Arrays  Two Dimensional Arrays #!/usr/bin/perl $gap = -2; $st1="acgtactacg"; $st2="acctaccacgt"; $n1=length($st1); $n2=length($st2); # Allocate the array for(my $i = $n1-1; $i >= 0; $i--) { $M[$i][$n2-1] = 0; }

Accessing the Matrix sub printmatrix { print "n1 $n1, n2 $n2\n"; for(my $i = 0; $i < $n1; $i++) { for(my $j = 0; $j < $n2; $j++) { print "M[$i][$j]= $M[$i][$j],"; } print "\n"; }

Perl Modules  Allow you to make your code modular  Create an Object Oriented interface  Separate your code into separate files so changes wont be made to working code

Modules  Similar idea to libraries in C.  use CGI;  Useful Modules  CGI – CGI routines.  DBI – Database Connectivity.  strict – Makes you code all proper like.  Data::Dumper – Debugging large objects.  XML::Simple – Simple XML Parsing.  Always ‘use strict’!

What is a Module?  A module is a.pm file that defines a library of related functions  Modules are conceptually similar to old-fashioned Perl libraries (.pl files), but have a cleaner implementation  selective namespace cluttering  simpler function invocation

Example (pasture1.pl) sub Cow::speak { print "a Cow goes moooo!\n"; } sub Horse::speak { print "a Horse goes neigh!\n"; } sub Sheep::speak { print "a Sheep goes baaaah!\n" = qw(Cow Cow Horse Sheep Sheep); foreach $animal { $animal->speak; }

Arguments attempts to invoke subroutine "Class::method" as:

Simplifying sub Sheep::speak { my $class = shift; print "a $class goes baaaah!\n"; }

A second method (pasture2.pl) { package Cow; sub sound { "moooo" } sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n" = qw(Cow Cow Horse Sheep Sheep); foreach $animal { $animal->speak; }

Inheritance (pasture3.pl) { package Animal; sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n" } { package = qw(Animal); sub sound { "moooo" } } On $animal->speak, Perl looks for "Cow::speak". But that’s not there, so Perl checks for the inheritance It’s there, and contains the single name "Animal".

Overriding (pasture4.pl) { package = qw(Animal); sub sound { "squeak" } sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n"; print "[but you can barely hear it!]\n"; }

How to use a Module  test.pl use Foo; Foo:bar();  Foo.pm package = qw (bar); sub bar { print “ hello\n ” ; }

Package Names and Filenames  Package name is declared on line 1  This should be the same as the filename, without the.pm extension  If it is different, your functions will not be exported correctly  Should begin with a capital letter to avoid possible conflict with pragmas

Summary  Modules are libraries of functions  A simple module just exports a set of functions  Perl modules can be expanded in many directions for arbitrarily sophisticated libraries