5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";

Slides:



Advertisements
Similar presentations
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.
Advertisements

Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Introduction to Perl Bioinformatics. What is Perl? Practical Extraction and Report Language A scripting language Components an interpreter scripts: text.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
11.1 Variable types in PERL ScalarArrayHash $number $string %hash $array[0] $hash{key}
4ex.1 More loops. 4ex.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) {
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
5.1 Previously on... PERL course (let ’ s practice some more loops)
Sup.1 Supplemental Material (NOT part of the material for the exam)
Perl Basics Software Tools. Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until.
6.1 Short foreach revision. 6.2 $arr[2]$arr[1]$arr[3]$arr[4] Loops: foreach The foreach loop passes through all the elements of an array = (2,3,4,5,6);
Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl.
4.1 Revision. 4.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n"; my $number.
10.1 Sorting and Modules בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
3.1 Ifs and Loops. 3.2 Revision: variables Scalar variables can store scalar values: Variable declaration my ($priority); Numerical assignment $priority.
4.1 More loops. 4.2 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) { $num.
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
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.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
MCB 5472 Assignment #6: HMMER and using perl to perform repetitive tasks February 26, 2014.
Perl By Gabe and Ted. History Perl was created by Larry Wall while working at NASA’s Jet Propulsion Labs. Larry Wall also created patch which is in widespread.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
If statements while loop for loop
Control Structures. if-elsif-else semantically the same as C/C++ syntactically, slightly different. if ($a > 0){ print “\$a is positive\n”; } elsif ($a.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
6.1 Before we start ( צילום : איתן שור ) Let’s talk a bit about the last exercise, and Eclipse…
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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 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.
8.1 Common Errors – Exercise #3 Assuming something on the variable part of the input file. When parsing a format file (genebank, fasta or any other format),
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.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl Scripting II Conditionals, Logical operators, Loops, and File handles Suzi Lewis Genome Informatics.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
CSI605 perl. Perl Facts Perl = Pathologically Eclectic Rubbish Lister Perl is highly portable across many different platforms and operating systems Perl.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
The Scripting Programming Language
Line Continuation, Output Formatting, and Decision Structures
Teaching Materials by Ivan Ovcharenko
Perl for Bioinformatics
Line Continuation, Output Formatting, and Decision Structures
Control Structures: for & while Loops
Lesson 2. Control structures File IO - reading and writing Subroutines
2.6 The if/else Selection Structure
INTRODUCTION to PERL PART 1.
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

5.1 Revision: Ifs and Loops

5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n"; my $number = ; chomp $number; if ($number 100) { print "ERROR: The average must be between 0 and 100.\n"; } elsif ($number > 90) { print "wow!\n"; } elsif ($number > 80) { print "well done.\n"; } else { print "oh well...\n"; } Note the indentation: a single tab in each line of new block ‘ } ’ that ends the block should be in the same indentation as where it started True if at least one condition is true

5.3 if, elsif, else my $number = ; chomp $number; $number 100 “ERROR” Yes > 90 > 80 No “wow!” “well done” “oh well…” Yes No Yes if ($number 100) { print "ERROR"; } elsif ($number > 90) { print "wow!\n"; } elsif ($number > 80) { print "well done.\n"; } else { print "oh well...\n"; }

5.4 Comparison operators StringNumericComparison eq== Equal ne!= Not equal lt< Less than gt< Greater than le<= Less than or equal to ge>= Greater than or equal to if ($age == 18)... if ($name eq "Yossi")... if ($name ne "Yossi")... if ($name lt "n")... if ($age = 18)... Found = in conditional, should be == at... if ($name == "Yossi")... Argument "Yossi" isn't numeric in numeric eq (==) at...

5.5 If my $luckyNum = 42; print "Guess a number\n"; my $num = ; chomp $num; if ($num != $luckyNum) { print "Wrong...\n"; } else { print "Correct!!\n"; } $num != 42 Correct!! Guess a number Wrong… No Yes

5.6 Loops: while Commands inside a loop are executed repeatedly (iteratively): my $luckyNum = 42; print "Guess a number\n"; my $num = ; chomp $num; while ($num != $luckyNum) { print "Wrong. Guess again.\n"; $num = ; chomp $num; } print "Correct!!\n"; $num != 42 Correct!! Guess a number Wrong… $num No Yes

5.7 Loops: while (defined …) Let's observe the following code : open (my $in, "<", "numbers.txt"); my $line = ; while (defined $line) { chomp $line; if ($line > 10) { print $line; } $line = ; } close ($in); read $line defined ? >10 print $line End Start read $line Yes No Yes

5.8 $arr[2]$arr[1]$arr[3]$arr[4] Loops: foreach The foreach loop passes through all the elements of an array = (1,1,2,3,5); Note: The array is actually $arr[0] foreach my $num { $num++; } undef

5.9 Loops: while = (1,1,2,3,5); my $num; my $i = 0; while ($i { $num = $arr[$i]; $num++; $arr[$i] = $num; $i++; } = (1,1,2,3,5); foreach my $num $num++; } $i = 0 Yes $num++ $num = $arr[$i] $arr[$i] = $num End Start Yes No

5.10 Breaking out of loops next – skip to the next iteration last – skip out of the loop open (my $in, "<", "numbers.txt"); = ; foreach my $num { if ($num <= 10) { next; } print $num; } close ($in);

5.11 Breaking out of loops next – skip to the next iteration last – skip out of the loop open (my $in, "<", "numbers.txt"); = ; foreach my $num { if ($num <= 10) { last; } print $num; } close ($in);

5.12 Class exercise 4b (from last week) 1.Read a file containing several proteins sequences in FASTA format, and print only their header lines using a while loop (see example FASTA file on the course webpage). 2.Read a file containing several proteins sequences in FASTA format, and print only their header lines using a foreach loop (see example FASTA file on the course webpage). 3.(From Home assignment) Read a file containing numbers, one in each line and print the sum of these numbers. (use number.txt from the website as an example). 4*.Read the "fight club.txt" file and print the 1 st word of the 1 st line, the 2 nd word of the 2 nd line, and so on, until the last line. (If the i-th line does not have i words, print nothing).

5.13 More loops

5.14 Loops: for The for loop is similar to while for repeating block of code for (my $num=0; $num<3 ;$num++){ print "Take $num!\n"; } my $num=0; while ($num<3) { print “Take $num!\n"; $num++; } init condition increase

5.15 Scope of variable declaration If you declare a variable inside a loop it will only exist in that loop This is true for every { block } : my $name=""; while ($name ne "Nimrod") { $name = chomp($name); print "Hello $name, what is your age?\n"; my $age; $age = ; chomp $age; } print $name; print $age; Global symbol "$age" requires explicit package name

5.16 Don’t declare the same variable name twice If you declare a variable name twice, outside and inside a block – you are creating two distinct variables… don’t do it! my $name = "Ruti"; print "Hello $name!\n"; my $num; = (1,2,3); foreach $num { my $name = "Nimrod"; print "$num. Hello $name!\n"; } print "Hello $name!\n"; Hello Ruti! 1. Hello Nimrod! 2. Hello Nimrod! 3. Hello Nimrod! Hello Ruti!

5.17 Don’t declare the same variable name twice If you declare a variable name twice, outside and inside – you are creating two distinct variables… don’t do it! my $name = "Ruti"; print "Hello $name!\n"; my $num; = (1,2,3); foreach $num { $name = "Nimrod"; print "$num. Hello $name!\n"; } print "Hello $name!\n"; Hello Ruti! 1. Hello Nimrod! 2. Hello Nimrod! 3. Hello Nimrod! Hello Nimrod!

5.18 Fasta format - reminder Fasta format sequence begins with a single-line description, which starts with ' > ', followed by lines of sequence data that contain new-lines after a fixed number of characters: >gi| |ref|NP_ | thr operon leader peptide… MKRISTTITTTITITTGNGAG >gi| |ref|NP_ | fused aspartokinase I and homoserine… MG1655]MRVLKFGGTSVANAERFLRVADILESNARQGQVATVLSAPAKITNHLVAMIEKTISGQDALPN AKFFAALARANINIVAIAQGSSERSISVVVNNDDATTGVRVTHQMLFNTDQVIEVFVIGVGGVGGALLEQ NAGDELMKFSGILSGSLSYIFGKLDEGMSFSEATTLAREMGYTEPDPRDDLSGMDVARKLLILARETGRE LELADIEIEPVLPAEFNAEGDVAAFMANLSQLDDLFAARVAKARDEGKVLRYVGNIDEDGVCRVKIAEVD GNDPLFKVKNGENALAFYSHYYQPLPLVLRGYGAGNDVTAAGVFADLLRTLSWKLGV >gi| |ref|NP_ | homoserine kinase [Escherichia coli… MG1655]MVKVYAPASSANMSVGFDVLGAAVTPVDGALLGDVVTVEAAETFSLNNLGRFADKLPSEPREN IVYQCWERFCQELGKQIPVAMTLEKNMPIGSGLGSSACSVVAALMAMNEHCGKPLNDTRLLALMGELEGR ISGSIHYDNVAPCFLGGMQLMIEENDIISQQVPGFDEWLWVLAYPGIKVSTAEARAILPAQYRRQDCIAH GRHLAGFIHACYSRQPELAAKLMKDVIAEPYRERLLPGFRQARQAVAEIGAVASGISGSGPTLFALCDKP ETAQRVADWLGKNYLQNQEGFVHICRLDTAGARVLEN

5.19 GenBank files… GenBank and GenPept are two NCBI formats for representing information of genes and proteins (respectively). Here is a sample recordsample record

5.20 Class exercise 5a 1.Read the "fight club.txt" file and print for each line the number of words in the line. 2*. Read a file containing several proteins sequences in FASTA format, and print only the gi numbers (the gi number appears in the following format: ' >gi|XXXXXXX|ref|… '). Note that the number of digits in the gi number may vary. 3*. Read the "fight club.txt" file and print for each line the number of times the letter ' i ' appears in it.

5.21 FASTA: Analyzing complex input Assignment: Write a script that reads several protein sequences in FASTA format, and prints for each sequence its header and its 30 C-terminal (last) amino-acids. | Obtain from the assignment:  Input  Required Output  Required processes (functions)

5.22 FASTA: Analyzing complex input Let's start with something easier: Print header and last 30 aa of the first protein: 1.Read the first FASTA sequence: 1.1. Read FASTA header 1.2. Read each line until next FASTA header 2.Do something (print output) 2.1. Get last 30aa Print header last 30aa Let’s see how it’s done… Do something Start Read line End Save header Read line Concatenate to sequence defined and not header No Read line Yes

5.23 ## 1.1. Read FASTA header and save it my $fastaLine = ; chomp $fastaLine; my $header = substr($fastaLine,1); ## 1.2. Read sequence until next FASTA header $fastaLine = ; my $seq = ""; while ((defined $fastaLine) and (substr($fastaLine,0,1) ne ">" )){ chomp $fastaLine; $seq = $seq.$fastaLine; $fastaLine = ; } ## 2.1 get last 30aa my $subseq = substr($seq,-30); ## 2.2 print header and last 30aa print "$header\n$subseq\n"; Do something End Start Save header Read line No Read line Concatenate to sequence defined and not header Read line Yes

5.24 FASTA: Analyzing complex input Overall design: Read the FASTA file (several sequences). For each sequence: 1.Read the FASTA sequence 1.1. Read FASTA header 1.2. Read each line until next FASTA header 2.For each sequence: Do something 2.1. Get last 30aa Print header and last 30aa. Let’s see how it’s done… Do something End Start Save header Read line Concatenate to sequence defined and not header No Read line Yes defined? No Yes

5.25 ## 1.1. Read FASTA header and save it my $fastaLine = ; while (defined $fastaLine) { chomp $fastaLine; my $header = substr($fastaLine,1); ## 1.2. Read seq until next header $fastaLine = ; my $seq = ""; while ((defined $fastaLine) and (substr($fastaLine,0,1) ne ">" )) { chomp $fastaLine; $seq = $seq.$fastaLine; $fastaLine = ; } ## 2.1 get last 30aa my $subseq = substr($seq,-30); ## 2.2 print header and last 30aa print "$header\n$subseq\n"; } Do something End Start Save header Read line No Read line Concatenate to sequence defined and not header Read line Yes defined? No Yes

5.26 Class exercise 5b 1.(HW Ex 3.2) Read a Fasta file (you can use as an example Ecoli.prot.fasta from the course’s website) and print for each sequence the header and the sequence length. 2.Read a Fasta file (such as Ecoli.prot.fasta from) and print the headers of the proteins that their sequence starts with MAD or MAN. 3*.Write a script that reads a file containing names and expenses on separate lines (such as expenses.txt from the course’s website). Sum the numbers while there is a '+' sign before them, and print for each name the total of expenses. For example: Input:Output: NimrodNimrod Dana Dana