1 / 30

Week 3 Lecture 1: Structure, flow and Control Structure #!/ usr /bin/ perl # sequence_process.pl

Week 3 Lecture 1: Structure, flow and Control Structure #!/ usr /bin/ perl # sequence_process.pl # declaration first Use strict; Use warnings; # Initialization code, global scope my ($ i , $j, $ str ); My $ global_var =“I love bioinformatics” #here is the main program MAIN: {

talasi
Download Presentation

Week 3 Lecture 1: Structure, flow and Control Structure #!/ usr /bin/ perl # sequence_process.pl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 3 Lecture 1: Structure, flow and Control Structure #!/usr/bin/perl # sequence_process.pl # declaration first Use strict; Use warnings; # Initialization code, global scope my ($i, $j, $str); My $global_var=“I love bioinformatics” #here is the main program MAIN: { My $main_var = “but it is challenging” sub_4_print ($main_var); } # end of main Sub_4_print ( print “global_var: $global_var\n”; print “main’s var: $_[0]\n” }

  2. Special BLOCKs: BEGIN ----good for adding module to @INC END----good for cleaning up the directory The BEGIB Block are compiled and run before the rest of code is compiled. #!/usr/bin/perl BEGIN { unshift (@INC,"/usr/local/biobin"); # Add the modules stored to the @INC } use Oligo_biobin; if (@ARGV < 1) { die "Usage: $0 <tbl_file> Tbl_file: OligoidOligosequence“ ;} ( $oligofile ) = @ARGV; open (TBL,"$oligofile") || die "Can't open $oligofile"; while (<TBL>) { chomp; $temp = $_; @line = split "\t",$_; print "$line[0]\t$line[1]\t"; print sprintf "%5.1f\n",&calc_tm($line[1]);

  3. II. If-else #!/usr/bin/perl use strict; use warnings; if (some_expression) { do somehting here; } elsif { do second thing; } elsif { do third thing; } else { do something finally; }

  4. III. for-loop and foreach #!/usr/bin/perl use strict; use warnings; my ($i, $len, @arr); @arr = (1..30); $len=@arr; # for-loop for ($i=0; $i < $len; $i++) { print “$arr[$i]\n”; } # foreach foreach $item (@arr) { print "$item\n"; } Are they the same? When we should use for-loop and foreach?

  5. IV: unless == “ if not” unless (open FILE, $filename) { print “Failed to open the file\n”; } This is a trapping error condition, it is like If (not open File, $filename) { print “Failed to open $filename: $!”; }

  6. When to use unless? If (/^>/) { # do nothing } else { print “$_\n”; } unless (/^>/) { print “$_\n”; } Remember that “unless == if not”

  7. V. The ternary operator #/usr/bin/perl -W use warnings; use strict; my (@words, $count, $total ); $total=0; while (<>) { chomp; @words =split; $count =scalar (@words); $total = $total + $count; } print "There "; if ($total == 1) { print "is "; } else { print "are "; } print "$total word"; unless ($total ==1) { print "s"; } print " in the file.\n"; print "There ", ($total ==1)? "is": "are", " $total word", ($total ==1)?"":"s"," in the file. \n"

  8. Last, next and re-do while (something) { something; something; something; if (somecondition) { somepart; somepart; next; } otherpart; otherpart; # next comes here } while (something) { something; something; something; if (somecondition) { something or other; something or other; last; } morethings; morethings; # } # last comes here Next one within while loop last: Get out of while loop

  9. while (somecondition) { # redo comes here something; something; something; if (somecondition) { somestuff; somestuff; redo; } morething; morething; morething; } jump to the beginning of the current block

  10. SOMELABEL: while (condition) { statement; statement; statement; if (somecondition) { last SOMELABEL; } } This tells Perl to exit the block named SOMELABEL,

  11. OUTER: for ($i = 1; $i <= 10; $i++) { INNER: for ($j = 1; $j <= 10; $j++) { if ($i * $j == 63) { print "$i times $j is 63!\n"; last OUTER; } if ($j >= $i) { next OUTER; } } }

  12. How do you skip a blank line in a file? @line = (); #declare a empty array LINE: foreach (<>) # remember what diamond means? chomp; next LINE if /^$/; # skip a blank line push @lines, $_; } What is different between /^$/ and /\S*/?

  13. until is simply the negation of while while (expression) { # while expression is true execute this block statement(s); } until (expression) { # until expression is false execute this block statement(s); }

  14. #!/usr/bin/perl use strict; use warnings; my ($i, $len, @arr, $item); @arr = (1..20); $len=@arr; print "while:\n"; $i = 0; while ( $i < 10 ) { print "$arr[$i]\n"; ++$i; } $i = 0; print "until:\n"; until ( $i > 10 ) { # How about $i <10? print "$arr[$i]\n"; ++$i; }

  15. For while/until, Before the loop is entered, If the condition is already false, the loop won’t be executed.

  16. do {} until/while Statement do { statement 1; statement 2; } while ( some condition ) What if the condition is not true? do { statement 1; statement 2; } until ( some condition ) What if the condition is true?

  17. Regular Expression 1. What is regular expression? An expression of a pattern in a string using special characters and words. 2. When and where we use it? Regular expression is used to parse an output from a software , for example, BLAST, or used to extract information you need from a text file. When a string | line matches the pattern, it is extracted. Therefore, it is extremely useful.

  18. String match: Two different formats while (<>){ chomp; $line = $_; if (/drought/) { # check if current line $_ contains “drought”; print “$_\n”; } # how to check if a variable string contains “drought” if ($line =~m/drought/) { print “$line\n”; } }

  19. #!/usr/bin/perl use strict; my $infile=shift; open (IN, "$infile") || die "Can not open input file -- $infile \n"; while (<IN>){ chomp; if ((/drought/) && (/salt/)) { print "drought_salt\t$_\n"; } elsif (/calcium/) { print "calcium:\t$_\n"; } elsif (/cold/){ print "cold:\t$_\n"; } } close (IN);

  20. .   Match any character\w  Match "word" character (alphanumeric plus "_")\W  Match non-word character\s  Match whitespace character\S  Match non-whitespace character\d  Match digit character\D  Match non-digit character\t  Match tab\n  Match newline

  21. If (/\d+/) { print “match_digit: $_\n”; } elsif (/\w+/) { print “match_character:$_\n”; } +: one or more *: zero or more

  22. *      Match 0 or more times+      Match 1 or more times?      Match 1 or 0 times{n}    Match exactly n times{n,}   Match at least n times{n,m}  Match at least n but not more than m times

  23. if($str =~m/(A|E|I|O|U|a|e|i|o|u)/) { print "String contains a vowel!\n” } if($string =~ /[^AEIOUYaeiouy]/){ print “String contains a non-vowel\n“; }

  24.  Volume in drive D has no label Volume Serial Number is 4547-15E0 Directory of D:\polo\marco.              <DIR>        12-18-97 11:14a ...             <DIR>        12-18-97 11:14a ..INDEX    HTM         3,237  02-06-98  3:12p index.htmAPPDEV   HTM         6,388  12-24-97  5:13p appdev.htmNORM     HTM         5,297  12-24-97  5:13p norm.htmIMAGES         <DIR>        12-18-97 11:14a imagesTCBK     GIF           532  06-02-97  3:14p tcbk.gifLSQL     HTM         5,027  12-24-97  5:13p lsql.htmCRASHPRF HTM        11,403  12-24-97  5:13p crashprf.htmWS_FTP   LOG         5,416  12-24-97  5:24p WS_FTP.LOGFIBB     HTM        10,234  12-24-97  5:13p fibb.htmMEMLEAK  HTM        19,736  12-24-97  5:13p memleak.htmLITTPERL       <DIR>        02-06-98  1:58p littperl         9 file(s)         67,270 bytes         4 dir(s)     132,464,640 bytes free

  25. $_= “Forests are important to Human”; If (/(\w+)\W+(\w+)/) { print “$1\n$2\n”; } Or you can do this ($first, $second) = (/(\w+)\W+(\w+)/; print “$first\n$second\n”;

  26. Translation: Translations are like substitutions, except they happen on a letter by letter basis instead of substituting a single phrase for another single phrase. For instance, what if you wanted to make all vowels upper case: # Change DNA sequence from low case to upper case: $string =~ tr/[a,t,c,g]/[A,T,C,G]/; # Change everything to upper case: $string =~ tr/[a-z]/[A-Z]/; Change everything to lower case $string =~ tr/[A-Z]/[a-z]/;

  27. Perl regular expressions normally match the longest string possible. For instance: my($text) = "mississippi"; $text =~ m/(i.*s)/;print $1 . "\n"; Run the preceding code, and here's what you get: ississ It matches the first i, the last s, and everything in between them. But what if you want to match the first i to the s most closely following it? Use this code: my($text) = "mississippi"; $text =~ m/(i.*?s)/;print $1 . "\n"; Now look what the code produces: is

  28. \b  Match a word boundary    \B  Match a non-(word boundary)    \A  Match only at beginning of string    \Z  Match only at end of string, or before newline at the end    \z  Match only at end of string    \G  Match only where previous m//g left off (works only with /g) For example If (/Fred\b/) ---matches Fred, but not Frederick / \bTech\b ---matches Tech, but not MichiganTech or Technological \B requires that there not a word boundary /\bFred\B/ ----matches Frederick but not Fred Christopher

  29. Substitute $str = “foot fool buffoon”; $str = s/foo/bar/g; #str now is “bartbarlbufbarn” g (global ) tells Perl to replace on all matches. $str = “foot Fool buffoon”; $str = s/foo/bar/gi; #str now is “bartbarlbufbarn

  30. #!/usr/bin/perl use warnings;   $_ = "my test string goes here";   while (/(\w+)/gi) {       $word = $&;       while ($word =~ /e/gi) {           $count++;           if ($count == 3 and $word ne 'the') {               print "$word\n";               $count = 0;           }       }   }  

More Related