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")... NumericComparison ==Equal !=Not equal Greater than >= Less than or equal to <= Greater than or equal to"> 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")... NumericComparison ==Equal !=Not equal Greater than >= Less than or equal to <= Greater than or equal to">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 4.1 Revision

2 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 = ; 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

3 4.3 if ($age = 18)... Found = in conditional, should be == at... if ($name == "Yossi")... Argument "Yossi" isn't numeric in numeric eq (==) at... Comparison operators StringNumericComparison eq==Equal ne!=Not equal lt<Less than gtgt>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")... NumericComparison ==Equal !=Not equal <Less than >Greater than >= Less than or equal to <= Greater than or equal to

4 4.4 Loops Commands inside a loop are executed repeatedly (iteratively): my $num=0; print "Guess a number.\n"; while ($num != 31) { $num = ; } print "correct!\n"; my @names = ; chomp(@names); my $name; foreach $name (@names) { print "Hello $name!\n"; }

5 4.5 Loops: foreach The foreach loop passes through all the elements of an array my @numArr = (1,1,2,3,5); foreach my $number (@numArr) { $number++; } Note: The array is actually changed

6 4.6 Fasta format Fasta format sequence begins with a single-line description, that start with ' > ', followed by lines of sequence data that contains new-lines after a fixed number of characters: >gi|229608964|ref|NM_014600.2| Homo sapiens EH-domain AAACATGGCGGCGCCCTGCGCGGCTTCCCGTCGCCGCAACCGTGGGGCCGGCCCTGCCTT GGAGCGGAGCCGAAGCATCCCTTGCTGCACGCAGGGCAGAGCAGGCGAGGGCTGGGGGCC GTATAACTTATTTTATATCCATATTCAGACTATATAGAGAATATTCTATGCATCTATGAC GTGCTTAC >gi|197099147|ref|NM_001131576.1| Pongo abelii EH-domain AGAGCTGAGCGCCTGCCCACAAACATGGCGGCGCCCTGCGCGGCTTCCCTTCGCCGGGAC CGCCTGGGGCTGCAGGATGCTGCTGCGGATGCTGAGCTGTCCGCGGGTTGGGCAGCGTCG CTGCGCGGCTTCCCTT >gi|55742034|ref|NM_001006733.1| Xenopus tropicalis EH-domain CGGGCAAGACCACCTTCATCCGCCACCTCATAGAGCAGGACTTCCCCGGCATGAGGATCG GGCCCGAACCGGGGACTTCCTCTGCGCGCCGGCTTCCTGCCCAGCTGGCATTTAAACCAC ACATGGCGGCGCCCTGCGCGGCTTCCCGTCGCCGCAACCGTGGGGCCGGCC

7 4.7 Breaking out of loops next – skip to the next iteration last – skip out of the loop my @lines = ; foreach $line (@lines) { if (substr($line,0,1) eq ">") { next; } if (substr($line,0,8) eq "**stop**") { last; } print($line); }

8 4.8 More loops

9 4.9 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 "Yossi") { chomp($name = ); print "Hello $name, what is your age?\n"; my $age; $age = ; } print $name; print $age; Global symbol "$age" requires explicit package name

10 4.10 Never 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 $number; foreach $number (1,2,3) { my $name = "Nimrod"; print "Hello $name!\n"; } Hello Ruti! Hello Nimrod! Hello Ruti!

11 4.11 Never 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 $number; foreach $number (1,2,3) { $name = "Nimrod"; print "Hello $name!\n"; } Hello Ruti! Hello Nimrod!

12 4.12 Reminder: Uninitialized (undefined) variables If uninitialized variables are used (before assignment) awarnings is issued: my $a; print($a+3); Use of uninitialized value in addition (+) 3 my $line; print("line is $line"); Use of uninitialized value in concatenation (.) or string line is

13 4.13 Is this variables defined? defined check whether a variable was defined. my $a; if (defined $a){ print($a+3); } my $line = ; while (defined $line){ print "line is $line"; $line = ; } print "done!!!\n" ctrl-z to indicate end of input

14 4.14 Is this variables defined? defined check whether a variable was defined. my $line = ; while (defined $line){ if (substr($line,0,1) eq ">"){ print "$line"; } $line = ; }

15 4.15 FASTA: Analyzing complex input Assignment: Write a script that reads several DNA sequences in FASTA format, and prints for each sequence print its header and its G+C content | Obtain from the assignment:  Input  Required Output  Required processes (functions)

16 4.16 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

17 4.17 # 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

18 4.18 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 +6.10 +16.50 +5.00 Dana +21.00 +6.00 Refael +6.10 +24.00 +7.00 +8.00 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

19 4.19 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*.As in Q4, but now concatenate all the sequences whose header starts with ' Chr07 '.

20 4.20 A bit about Reading and writing files

21 4.21 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 = ; my @inputLines = ; foreach $line (@inputLines)... Every filehandle opened should be closed: close(IN); Reading files

22 4.22 Open a file for writing, and link it to a filehandle: open(OUT, ">EHD.analysis") NOTE: If a file by that name already exists it will be overwritten! Print to a file: print OUT "The mutation is in exon $exonNumber\n"; And don't forget to: close(IN); Writing to files no comma here

23 4.23 Class exercise 4b 1.Change the script for class exercise 4a.1 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 "class.ex.4a1.out" (instead of printing to the screen). 3*. 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.


Download ppt "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."

Similar presentations


Ads by Google