Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Advanced Perl WUC Perl Poetry Variable Scope.
Lecture 6 More advanced Perl…. Substitute Like s/// function in vi: #cut with EcoRI and chew back $linker = “GGCCAATTGGAAT”; $linker =~ s/CAATTG/CG/g;
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
A Level Computing#BristolMet Session Objectives#U2 S8 MUST identify the difference between a procedure and a function SHOULD explain the need for parameters.
Programming and Perl for Bioinformatics Part III.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
11.1 Variable types in PERL ScalarArrayHash $number $string %hash $array[0] $hash{key}
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
11ex.1 Modules and BioPerl. 11ex.2 sub reverseComplement { my ($seq) $seq =~ tr/ACGT/TGCA/; $seq = reverse $seq; return $seq; } my $revSeq = reverseComplement("GCAGTG");
for($i=0; $i/)
Sup.1 Supplemental Material (NOT part of the material for the exam)
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
10.1 Sorting and Modules בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
Python programs How can I run a program? Input and output.
Introduction to Perl Thaddeus Aid IT Learning Programme University of Oxford 15/04/2014.
PERL Variables and data structures Andrew Emerson, High Performance Systems, CINECA.
subroutines and references
Computer Programming for Biologists Class 7 Nov 27 th, 2014 Karsten Hokamp
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Introduction of C++ language. C++ Predecessors Early high level languages or programming languages were written to address a particular kind of computing.
Introduction. 2COMPSCI Computer Science Fundamentals.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
Input, Output, and Processing
CMPS 211 JavaScript Topic 2 Functions and Arrays.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Computer Programming for Biologists Class 6 Nov 21 th, 2014 Karsten Hokamp
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Digital Text and Data Processing Tokenisation. Today’s class □ Tokenisation and creation of frequency lists □ Keyword in context lists □ Moretti and distant.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
GE3M25: Computer Programming for Biologists Python, Class 5
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
Part 4 Arrays: Stacks foreach command Regular expressions: String structure analysis and substrings extractions and substitutions Command line arguments:
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
PERL SCRIPTING. COMPUTER BASICS CPU, RAM, Hard drive CPU can only use data in the register directly CPU RAM HARD DRIVE.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Introduction to Python
User-Written Functions
Modules and BioPerl.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Subroutines Web Programming.
Introduction to Python
The structure of programming
Presentation transcript:

Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp

Computer Programming for Biologists  Revision  Subroutines Overview

Computer Programming for Biologists my %seq = (); # initialisation $freq{$char} = 0; # storing a value $freq{$char}++; # changing a value my $aa = $code{$codon}; # extracting foreach my $header (sort keys %seq) { my $seq = $seq{$header}; … } Revision - Hashes

Hash Variables Scalars vs Hash my $A = 0; A 0 my $C = 0; C 0 my $G = 0; G 0 my $T = 0; T 0 Initialisation of values: my %frequency = ();

Hash Variables if ($char eq 'A') { $A++; } elsif ($char eq 'C') { $C++ } elsif ($char eq 'G') { $G++; } elsif ($char eq 'T') { $T++; } Scalars vs Hash C 1 %frequency C 1 Increment: $frequency{$char}++;

Hash Variables Scalars vs Hash G T C A 9 %frequency A 5 C 9 G 7 T 5

Hash Variables Scalars vs Hash G T C A 9 %frequency print "Frequency of A: $A"\n; print "Frequency of C: $C"\n; print "Frequency of G: $G"\n; print "Frequency of T: $T"\n; A 5 C 9 G 7 T 5 foreach my $char (keys %frequency) { print "Frequency of $char: $frequency{$char}\n"; } Output:

Computer Programming for Biologists  write your own functions  run "programs" within a program Subroutines

Computer Programming for Biologists Definition: sub name_of_routine { # optional arguments e.g. my ($arg1, $arg2) # specify statements statement1; statement2; … # optionally return scalar or list, e.g. return $result1, $result2; } Subroutines special array with arguments to subroutine

Computer Programming for Biologists Subroutines &: (optional) symbol indicating subroutine Usage: name_of_routine; or $rv = &name_of_routine(); = &name_of_routine($arg1, $arg2); (optionally) capture return value(s) (optionally) submit list of arguments

Computer Programming for Biologists Subroutines Example: my $dna = shift; my $rev_comp = &reverse_complement($dna); print "reverse complement:\n".&format($rev_comp, 60); # sub routines: sub reverse_complement { my $out = reverse $out =~ tr/acgtACGT/tgcaTGCA/; return $out; } sub format { my ($sequence, $width) …

Computer Programming for Biologists Subroutines Example: my $dna = shift; my $rev_comp = &reverse_complement($dna); print "reverse complement:\n".&format($rev_comp, 60); # sub routines: sub reverse_complement { my $out = reverse $out =~ tr/acgtACGT/tgcaTGCA/; return $out; } sub format { my ($sequence, $width) … A copy of $dna is passed on Main area stays tidy and Details hidden towards end of script Code is re-usable, can be applied multiple times

Computer Programming for Biologists 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 Subroutines

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

Computer Programming for Biologists Inefficient coding: # frame 1: $sequence = $orig_seq; # Block of translation code, e.g. $prot = ''; while ($sequence) { $codon = substr $sequence, 0, 3, ''; $aa = $genetic_code{$codon}; $prot.= $aa; } print "translation: $prot\n"; Subroutines

Computer Programming for Biologists Inefficient coding: # frame 1: $sequence = $orig_seq; # Block of translation code, e.g. $prot = ''; while ($sequence) { $codon = substr $sequence, 0, 3, ''; $aa = $genetic_code{$codon}; $prot.= $aa; } print "translation: $prot\n"; # frame 2: # remove first base substr $sequence, 0, 1, '' Subroutines

Computer Programming for Biologists 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: … Subroutines the same block of code specified 6 times

Computer Programming for Biologists 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 "translation: $prot\n"; } Subroutines 6 times use of subroutine 1 specification of translation code

Computer Programming for Biologists 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; } Subroutines print return value return translated sequence

Computer Programming for Biologists 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; } Subroutines call subroutine within itself

Computer Programming for Biologists 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; Subroutines

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

Computer Programming for Biologists Extend your sequence analysis tool: -add translation into protein as subroutine into your script  me at with questions or problems Exercises

Computer Programming for Biologists Mock exam! Next week