Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
CSC 4630 Perl 1. Perl Practical Extraction and Support Language A glue language under UNIX Written by Larry Wall Claimed to be the most portable of scripting.
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Practical Extraction & Report Language PERL Joseph Beltran.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Bioinformatics 生物信息学理论和实践 唐继军
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.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Computer Programming for Biologists Class 6 Nov 21 th, 2014 Karsten Hokamp
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
– Introduction to Perl 12/13/ Introduction to Perl - Strings, Truth and Regex Introduction to Perl Session 2 · manipulating.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 4 Karsten Hokamp, PhD Genetics TCD, 01/12/2015.
Working with Loops, Conditional Statements, and Arrays.
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
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Part 4 Arrays: Stacks foreach command Regular expressions: String structure analysis and substrings extractions and substitutions Command line arguments:
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Programming Perl in UNIX Course Number : CIT 370 Week 2 Prof. Daniel Chen.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Built-In Functions. Notations For each function, I will give its name and prototype. –prototype = number and type of arguments ARRAY means an actual named.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
String and Lists Dr. José M. Reyes Álamo.
Chapter 5 - Control Structures: Part 2
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Perl Variables: Array Web Programming.
Control Structures: if Conditional
CIT 383: Administrative Scripting
Control Structures: for & while Loops
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
String and Lists Dr. José M. Reyes Álamo.
The Selection Structure
CSC 352– Unix Programming, Fall 2012
Presentation transcript:

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