Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Computer Programming for Biologists Revision Loop control Project Overview
Computer Programming for Biologists expressions 42 $base eq 'T' $num – 1 == 0 statements $seq = 'atgaacgt'; print "hello world!\n"; Revision: program components operators +, -, *, /, +=, ++, … built-in functions print, shift, length, … key words foreach, while, if, …
Computer Programming for Biologists ordered list of elements indicated by 'at' symbol index starts at = ('a'..'z'); # (a, b, c, d, e, …, x, y, z) Use $ and [] when working on individual elements: $letters[0] = 'A'; $first = $letters[0]; $last = $letters[-1]; Revision: Arrays Index:
Computer Programming for Biologists $upper = uc($in); $upper_first = uc_first($in); $backwards = reverse($in); $len = length($sequence); $num = $file = $aa; Revision: built-in = split //, $seq; $out = = keys = sort { $a $b $codon = substr $seq, 0, 3, ''; print "Hello world!\n";
Computer Programming for Biologists branching: if ($base eq 't') { $base = 'u'; } also unless () if ($base eq 't') { $base = 'a'; } elsif ($base eq 'a') { $base = 't'; } else { … } Revision: structures loops: while ($in = <>) { $out.= uc($in); } also until () foreach $element { $i++; print "$i) $element"; … }
Computer Programming for Biologists Representative values: Examples: while (1) {... } # endless loop if ($text) {... } # true if text neither '' nor 0 if {... } # true if array is not empty until ($i > 100) {... } # comparison or expression while ($out = substr $seq, 0, 3, '') {... } Revision: conditions truefalse 10 'some characters''' ('some element')()
Computer Programming for Biologists word(s) from list of command line parameters: $in = shift; equivalent to the following: $in = Revision: data input (command line arguments)
Reading from STDIN, default input stream: $in = <>; equivalent to $in = ; Shell tries to stream from file(s) if command line argument(s) present: $ perl prog.pl input.txt STDIN Computer Programming for Biologists Revision: data input
Computer Programming for Biologists Ways of breaking the loops: next; # continues with next loop last; # continues after loop exit; # exits program Structures: Breaks example print "Type 'y' to continue, 'q' to quit: "; while (<>) { if ($_ eq 'y') { last; } elsif ($_ eq 'q') { exit; } else { next; }
Computer Programming for Biologists start with pseudo code (comments) code small bits and run watch for warnings and errors dare to try things out check Perl documentation Programming Strategy
Computer Programming for Biologists Implement the following in a program: 1. Print a welcome message 2. Read input from a file 3. Separate header from sequence 4. Report length of sequence 5. Make sequence all upper case 6. Reformat sequence into 60 bp width 7. Reverse-complement the sequence 8. Provide position numbers at each line Go to Project