140 likes | 323 Views
Computer Programming for Biologists. Class 8 Nov 29 th , 2013 Karsten Hokamp http://bioinf.gen.tcd.ie/GE3M25/programming. Computer Programming for Biologists. Overview. Extend class project Subroutines. Computer Programming for Biologists. Subroutines. write your own functions
E N D
Computer Programming for Biologists Class 8 Nov 29th, 2013 Karsten Hokamp http://bioinf.gen.tcd.ie/GE3M25/programming
Computer Programming for Biologists Overview • Extend class project • Subroutines
Computer Programming for Biologists Subroutines • write your own functions • run "programs" within a program
Computer Programming for Biologists Subroutines Definition: sub name_of_routine { # optional arguments in @_, e.g. my @args = @_; # specify statements # optionally return scalar or list, e.g. return @results; } special array with arguments to sub Usage: name_of_routine; or $rv =&name_of_routine($arg1, $arg2); (optionally) capture return value(s) (optionally) submit list of arguments &: (optional) symbol indicating subroutine
Computer Programming for Biologists Subroutines • Can be placed anywhere in the program • Normally all subroutines located after main block of text • Definition starts with 'sub' followed by name • Statements enclosed in curly brackets • Text normally written indented • Optionally provide arguments • Optionally return values • Can be nested
Computer Programming for Biologists Subroutines Scenario: Read in DNA sequence Translate in all six reading frames • 6 x translation of a sequence
Computer Programming for Biologists Subroutines Inefficient coding: # frame 1: $sequence = $orig_seq; Block of translation code # frame 2: # remove first base substr $sequence, 0, 1, '' Block of translation code # frame 3: … # frame -1: … the same block of code specified 6 times
Computer Programming for Biologists Subroutines More efficient coding: # frame 1: $sequence = $orig_seq; &translate($sequence); # frame 2: # remove first base substr $sequence, 0, 1, '' &translate($sequence); # frame 3: … # frame -1: … sub translate { $input = shift; … print "$protein\n"; } 6 times use of subroutine 1 specification of translation code
Computer Programming for Biologists Subroutines Alternative: # frame 1: $sequence = $orig_seq; print &translate($sequence), "\n"; # frame 2: # remove first base substr $sequence, 0, 1, '' print &translate($sequence), "\n"; # frame 3: … # frame -1: … sub translate { $input = shift; … return $protein; } print return value return translated sequence
Computer Programming for Biologists Subroutines Other uses – improve flow and clarity: # read sequence and print reverse complement: $sequence = <>; $rev_comp = &reverse_complement($sequence); print "reverse complement: \n$rev_comp\n"; exit; sub reverse_complement { $input = shift; # reverse and complement input … return $out; } move large and complex code into subroutine
Computer Programming for Biologists Subroutines Other uses – recursion: # calculate factorial value for a given number: $fv = &fact(10);print "factorial 10 is $fv\n"; sub fact { my $val = shift; $fact = 1; if ($val > 1) { $fact = $val * &fact($val-1); } return $fact; } call subroutine within itself
Computer Programming for Biologists Subroutines Other uses – recursion: $val = 10;$fact = $val * &fact($val-1); $fact = 10 * fact(9); $fact = 10 * 9 * fact(8); … $fact = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * fact(1); $fact = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1;
Computer Programming for Biologists Subroutines reduce programming effort • improve flow • increase clarity • enable recursion
Computer Programming for Biologists Exercises Extend your sequence analysis tool: • add warnings and strict pragmas • add reporting the GC content • add reporting frequencies • add translation into protein • e-mail me kahokamp@tcd.ie • with questions or problems