1 / 14

Computer Programming for Biologists

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

elijah
Télécharger la présentation

Computer Programming for Biologists

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. Computer Programming for Biologists Class 8 Nov 29th, 2013 Karsten Hokamp http://bioinf.gen.tcd.ie/GE3M25/programming

  2. Computer Programming for Biologists Overview • Extend class project • Subroutines

  3. Computer Programming for Biologists Subroutines • write your own functions • run "programs" within a program

  4. 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

  5. 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

  6. Computer Programming for Biologists Subroutines Scenario: Read in DNA sequence Translate in all six reading frames • 6 x translation of a sequence

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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;

  13. Computer Programming for Biologists Subroutines  reduce programming effort • improve flow • increase clarity • enable recursion

  14. 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

More Related