Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
quiz, recap operator short-cuts arrays control structures comparisons project Overview Computer Programming for Biologists
Scalar variables: $name = value; right-to-left assignment value: numbers or strings of characters default variable $_ special character: \n (newline) Evaluation within double quotes e.g.: $text = "$input\n"; $text = '$input\n'; no evaluation Recap Computer Programming for Biologists
Variable name delimiter: $text; # same as ${text}; useful if text follows variable name, e.g.: $item = 'apple'; print "I bought 4 $items"; # wrong print "I bought 4 ${item}s"; # correct print "I bought 4 ". $item. "s"; # correct Addition to scalars
functions for scalars:. (concatenation).= (extension) lc, uc, lcfirst, ucfirst (capitalisation) chop, chomp (removing characters) length reverse substr (extracting parts of a string) tr (transliteration) Recap Computer Programming for Biologists
increase value by one: $counter = $counter + 1; same as $counter++; same as $counter += 1; Operator shortcuts other shortcuts: $num += 10; $num -= 5; $num1 *= $num2; $num1 /= $num2; $num1 **= 3 $first_name.= $last; operator combined with '=' applies operation to variable on the left Shortcuts
Computer Programming for Biologists Practical session: Go to and try the 'Operators' exercises Operators
Computer Programming for Biologists Variables
Computer Programming for Biologists Array examples: = ('a', 'b', 'c', 'd', 'e', 'f'); = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); = ('Karsten', 'Devin', 'Ken'); = ('19/01/2007', '26/01/2007'); = ($start, $middle, $end); Variables ordered list array variable
Computer Programming for = ('a', 'b', 'c', 'd', 'e', = (a..f); # = (1..10); # only works ascending use variables as range limits: $start = 1; $end = = ($start.. $end); combine # (1..10, a..f) Array Construction range operator
Computer Programming for Biologists Each element is an individual scalar identified by its index $numbers[0]; Use an expression as index: $i = 0; print $numbers[$i]; # prints first element print $numbers[$i + 1]; # prints second element Array Access array nameindex changes to $
Special indices and length length of an = (1..10); $length = last index: $#numbers (one less than length) $numbers[$#numbers] refers to last element negative index: $numbers[-1] refers to last element $numbers[-2] refers to second last element t Computer Programming for Biologists
Scalar and List = ('a', 'c', 'g', 't'); # = # list $bases # scalar # list print # scalar print # scalar Computer Programming for Biologists The context determines how a list is treated:
Computer Programming for Biologists Practical session: Go to and try the 'Arrays' exercises Arrays
Basic Built-in functions for arrays Adding and removing elements: push, pop (apply to end of array): 11; $last = shift, unshift (apply to start of array): 0; $first = Computer Programming for Biologists
@array, add-on 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 removed Basic Built-in functions for arrays Computer Programming for Biologists shift unshiftpush pop Special command line arguments $next_arg = $next_arg = shift;
More Built-in functions for arrays You will practise these in class! join (joins a list into a string) scalar (returns the length of an array) sort (sorts a list) reverse (reverses a list) splice (removing or adding slices) example: $sequence = join '-', (a..f); $sequence: 'a-b-c-d-e-f' Computer Programming for Biologists
Practical session: Go to and try the 'Arrays' exercises Arrays
Control Structures Computer Programming for Biologists
Loops cycle through elements of a list apply same processing steps each time Computer Programming for Biologists foreach $letter (a..z) { print "$letter\n"; } foreach element (list) { statements } (a..z) list of characters $letter single element foreach type of loop () contains list {} contains statement(s)
Loops cycle while an expression returns something true or content Computer Programming for Biologists while { $last = } Cycle through all elements of an array until (length($seq) < 3) { $cod = substr $seq, 0, 3, ''; } Cycle through all codons of a sequence while ($in = <>) { $input.= $in; } Go through all lines from standard input
if ($num > 100) { die "$num is too high!"; } elsif ($num < 1) { die "$num is too low!"; } Branching if / else / elsif structure Pseudo code: Computer Programming for Biologists Perl code: if (expression) { statements; } else { statements; } if number is too big stop with error message else if number is too small stop with error message if a condition is true then do one thing else do another thing
Branching if / else / elsif structure test for multiple conditions: Computer Programming for Biologists if ($response eq 'y') { print "ok!\n"; } elsif ($response eq 'n') { print "maybe next time\n"; } elsif ($response eq '') { print "please try again: "; } else { print "don't know what you mean\n"; }
Comparisons compare one scalar (or expression) to another numericalalphabetical >, >=gt <, <=lt ==eq !=ne Computer Programming for Biologists common mistake: $num = 10 is NOT a comparison!
True and False false 0 or empty string ('') true value different from 0 (1 by default) Computer Programming for Biologists ComparisonEvaluationBoolean 5 > 2 2 > 5 a > b 'ACTG' eq 'actg' '1' ne '1.0' 2+2 == 8/2 Comparisons are last in order of execution!
True and False false 0 or empty string ('') true value different from 0 (1 by default) Computer Programming for Biologists ComparisonEvaluationBoolean 5 > 21TRUE 2 > 50FALSE a > b0FALSE 'ACTG' eq 'actg'0FALSE '1' ne '1.0'1TRUE 2+2 == 8/21TRUE Comparisons are last in order of execution!
Computer Programming for Biologists Practical session: Go to and try the 'Controls' exercises Control Structures
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. Print reverse-complement 8. Provide position numbers at each line Go to Project