5.1 Previously on... PERL course (let ’ s practice some more loops)

Slides:



Advertisements
Similar presentations
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Introduction to C Programming
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
4.1 Reading and writing files. 4.2 Open a file for reading, and link it to a filehandle: open(IN, "
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.
6a.1 More about files: Globbing. 6a.2 Open a file for reading, and link it to a filehandle: open(IN, "
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
Perl File I/O Learning Objectives: 1. To understand the usage of file handler in Perl 2. To learn the file error handling / testing in Perl.
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) {
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.
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
Chapter 5: Loops and Files.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Sup.1 Supplemental Material (NOT part of the material for the exam)
Perl File I/O Software Tools. Slide 2 Filehandles l A filehandle is the name for an I/O connection between your Perl program and the outside world. STDIN.
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 בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
Introduction to C Programming
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";
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.
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.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Python programs How can I run a program? Input and output.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
MCB 5472 Assignment #6: HMMER and using perl to perform repetitive tasks February 26, 2014.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Bioinformatics 生物信息学理论和实践 唐继军
Introduction to Perl Yupu Liang cbio at MSKCC
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.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
3.1 Revision: variables & arrays Array declaration Array = ('A','B','C','D'); = = (); Array.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
5 1 Data Files CGI/Perl Programming By Diane Zak.
Iteration While / until/ for loop. While/ Do-while loops Iteration continues until condition is false: 3 important points to remember: 1.Initialise condition.
Computer Programming for Biologists Class 6 Nov 21 th, 2014 Karsten Hokamp
©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.
Parsing BLAST output. Output of a local BLAST search “less” program Full path to the BLAST output file.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Files Tutor: You will need ….
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";
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
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.
Learners Support Publications Working with Files.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
Fundamentals of Python: First Programs
Input from STDIN STDIN, standard input, comes from the keyboard.
Presentation transcript:

5.1 Previously on... PERL course (let ’ s practice some more loops)

5.2 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. Compute G+C content 2.2. Print header and G+C content Let’s see how it’s done… Do something End of input? No End Start Save header Read line Header or end of input Yes Concatenate to sequence No Read line

5.3 # 1. Read FASTA sequece $fastaLine = ; while (defined $fastaLine) { # 1.1. Read FASTA header $header = substr($fastaLine,1); $fastaLine = ; # 1.2. Read sequence until next FASTA header while ((defined $fastaLine) and (substr($fastaLine,0,1) ne ">" )) { $seq.= $fastaLine; $fastaLine = ; } # 2. Do something... # 2.1 compute $gcContent print "$header: $gcContent\n"; } Do something End of input? No End Start Save header Read line Header or end of input Yes Concatenate to sequence No Read line

5.4 Class exercise 4a 1.Write a script that reads lines of names and expenses: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 6.10,24.00,7.00,8.00 END For each line print the name and the sum. Stop when you reach "END" 2.Change your script to read names and expenses on separate lines, Identify lines with numbers by a "+" sign as the first character in the string: Yossi Dana Refael END Sum the numbers while there is a '+' sign before them. Output: Yossi 27.6 Dana 27 Refael 45.1 Output: Yossi 27.6 Dana 27 Refael 45.1

5.5 Class exercise 4a 3.(Home Ex. 2 Q. 5) Write a script that reads several protein sequences in FASTA format, and prints the name and length of each sequence. Start with the example code from the last lesson. 4*.Write a script that reads several DNA sequences in FASTA format, and prints FASTA output of the sequences whose header starts with ' Chr07 '. 5**.Write a script that reads several DNA sequences in FASTA format, and prints FASTA output of the sequences whose header contains 'Chr07'.

5.6 Reading and writing files

5.7 Open a file for reading, and link it to a filehandle: open(IN, "<EHD.fasta"); And then read lines from the filehandle, exactly like you would from : my $line = ; = ; foreach $line Every filehandle opened should be closed: close(IN); Always check the open didn ’ t fail (e.g. if a file by that name doesn ’ t exists): open(IN, "<$file") or die "can't open file $file"; Reading files

5.8 Open a file for writing, and link it to a filehandle: open(OUT, ">EHD.analysis") or die... NOTE: If a file by that name already exists it will be overwriten! You could append lines to the end of an existing file: open(OUT, ">>EHD.analysis") or die.. Print to a file (in both cases): print OUT "The mutation is in exon $exonNumber\n"; Writing to files no comma here

5.9 You can ask questions about a file or a directory name (not filehandle): if (-e $name) { print "The file $name exists!\n"; } -e$name exists -r$name is readable -w$name is writable by you -z$name has zero size -s$name has non-zero size (returns size) -f$name is a file -d$name is a directory -l$name is a symbolic link -T$name is a text file -B$name is a binary file (opposite of -T). File Test Operators

5.10 open( IN, '<D:\workspace\Perl\p53.fasta' ); Always use a full path name, it is safer and clearer to read Remember to use \\ in double quotes open( IN, "<D:\\workspace\\Perl\\$name.fasta" ); (usually) you can also use / open( IN, "<D:/workspace/Perl/$name.fasta" ); Working with paths

5.11 Reading files: example $line = ; chomp $line; # loop processes one input line and print output for line while ($line ne "END") { # Separate name and = split(/ /, $line); $name = = split(/,/, $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print "$name $sum\n"; # Read next line $line = ; chomp $line; } Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

5.12 Reading files: example open(IN, '<D:\perl_ex\in.txt') or die "can't open input file"; $line = ; chomp $line; # loop processes one input line and print output for line while ($line ne "END") { # Separate name and = split(/ /, $line); $name = = split(/,/, $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print "$name $sum\n"; # Read next line $line = ; chomp $line; } close(IN); Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

5.13 Reading files: example open(IN, '<D:\perl_ex\in.txt') or die "can't open input file"; open(OUT,'>D:\perl_ex\out.txt') or die "can't open output file"; $line = ; chomp $line; # loop processes one input line and print output for line while ($line ne "END") { # Separate name and = split(/ /, $line); $name = = split(/,/, $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print OUT "$name $sum\n"; # Read next line $line = ; chomp $line; } close(IN); close(OUT); Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

5.14 Class exercise 5a 1.Change the script for class exercise 4a.2 to read the lines from an input file (instead of reading lines from keyboard). 2.Now, in addition, write the output of the previous question to a file named 'D:\perl_ex\class.ex.4a2.out' (instead of printing to the screen). 3*.Now, before opening 'D:\perl_ex\class.ex.4a2.out ‘, check if it exists, and if so – print a message that the output file already exist, and exit the script. 4*. Change the script for class exercise 4.a3 to receive from the user two strings: 1) a name of FASTA file 2) a name of an output file. And then - read from a FASTA file given by the user, and write to an output file also supplied by the user.

5.15 Passing information using command-line arguments

5.16 It is common to give arguments (separated by spaces) within the command-line for a program or a script: They will be stored in the : foreach my $arg print "$arg\n"; } Command line arguments > perl -w findProtein.pl D:\perl_ex\in.fasta D:\perl_ex\in.fasta 2 'D:\perl_ex\in.fasta' '2' '430'

5.17 It is common to give arguments (separated by spaces) within the command-line for a program or a script: They will be stored in the : foreach my $arg print "$arg\n"; } > perl -w findProtein.pl D:\my perl\in.fasta Command line arguments D:\my perl\in.fasta 2 'D:\my' 'perl\in.fasta' '2' '430'

5.18 It is common to give arguments (separated by spaces) within the command-line for a program or a script: They will be stored in the : foreach my $arg print "$arg\n"; } > perl -w findProtein.pl "D:\my perl\in.fasta" Command line arguments D:\my perl\in.fasta 2 'D:\my perl\in.fasta' '2' '430'

5.19 It is common to give arguments (separated by spaces) within the command-line for a program or a script: They will be stored in the : my $inFile = $ARGV[0]; my $outFile = $ARGV[1]; Or more simply: my ($inFile,$outFile) Command line arguments > perl -w findProtein.pl D:\perl_ex\in.fasta D:\perl_ex\out.txt

5.20 Command line arguments in Eclispe

5.21 Command line arguments in Eclispe

5.22 Reminder: the class exercise of 3 days ago. Reading files - example Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

5.23 Reading files: example $line = ; chomp $line; # loop processes one input line and print output for line while ($line ne "END") { # Separate name and = split(/ /, $line); $name = = split(/,/, $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print "$name $sum\n"; # Read next line $line = ; chomp $line; } Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

5.24 Reading files: example my ($inFileName) open(IN, "<$inFileName") or die "can't open $inFileName"; $line = ; chomp $line; # loop processes one input line and print output for line while ($line ne "END") { # Separate name and = split(/ /, $line); $name = = split(/,/, $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print "$name $sum\n"; # Read next line $line = ; chomp $line; } close(IN); Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

5.25 Reading files: example my ($inFileName, $outFileName) open(IN, "<$inFileName") or die "can't open $inFileName"; open(OUT, ">$outFileName") or die "can't open $outFileName"; $line = ; chomp $line; # loop processes one input line and print output for line while ($line ne "END") { # Separate name and = split(/ /, $line); $name = = split(/,/, $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print OUT "$name $sum\n"; # Read next line $line = ; chomp $line; } close(IN); close(OUT); Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 END Output: Yossi 27.6 Dana 27 Refael 45.1

5.26 Reading files: example my ($inFileName, $outFileName) open(IN, "<$inFileName") or die "can't open $inFileName"; open(OUT, ">$outFileName") or die "can't open $outFileName"; $line = ; chomp $line; # loop processes one input line and print output for line while (defined $line) { # Separate name and = split(/ /, $line); $name = = split(/,/, $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print OUT "$name $sum\n"; # Read next line $line = ; chomp $line; } close(IN); close(OUT); Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 Output: Yossi 27.6 Dana 27 Refael 45.1

5.27 Reading files: example my ($inFileName, $outFileName) open(IN, "<$inFileName") or die "can't open $inFileName"; open(OUT, ">$outFileName") or die "can't open $outFileName"; $line = ; # loop processes one input line and print output for line while (defined $line) { chomp $line; # Separate name and = split(/ /, $line); $name = = split(/,/, $nameAndNums[1]); $sum = 0; # Sum numbers foreach $num { $sum = $sum + $num; } print OUT "$name $sum\n"; # Read next line $line = ; } close(IN); close(OUT); Input: Yossi 6.10,16.50,5.00 Dana 21.00,6.00 Refael 24.00,7.00,8.00 Output: Yossi 27.6 Dana 27 Refael 45.1

5.28 Class exercise 5b 1.Change the script of class exercise 5a.2 such that script receive the input and output file names as arguments. 2*.Write a script receives a number of numeric arguments and prints its sum. For example: output: 100