Presentation is loading. Please wait.

Presentation is loading. Please wait.

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) {

Similar presentations


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

1 4ex.1 More loops

2 4ex.2 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"; }

3 4ex.3 Loops: for The for loop is controlled by three statements: 1 st is executed before the first iteration 2 nd is the stop condition 3 rd is executed before every re-iteration for (my $i=0; $i<10; $i++) { print "$i\n"; } my $i=0; while ($i<10){ print "$i\n"; $i++; } These are equivalent

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

5 4ex.5 Breaking out of loops die – end the program and print an error message to the standard error if ($score < 0) { die "score must be positive"; } score must be positive at test.pl line 8. Note: if you end the string with a "\n" then only your message will be printed * warn does the same thing as die without ending the program

6 4ex.6 Scope of variable declaration If you declare a variable inside a loop it will only exist in that loop: 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

7 4ex.7 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"; } print "Hello $name!\n"; Hello Ruti! Hello Nimrod! Hello Nimrod! Hello Nimrod! Hello Ruti!

8 4ex.8 Naming variables We encourage using meaningful names (even if they are long). For multi-word variables use no spaces, and each word, except for the first, is capitalized. examples: $proteinHeader $studentNameArray We will see conventions for other kinds of variables and names as we continue.

9 4ex.9 >gi|24646380|ref|NM_079608.2| Mus musculus EH-domain containing 4 (EHD4), mRNA GTGGTATTTCTTCGTTGTCTCTGGCGTGGTCACGTTGATTGGTCCGCTATCTGGACCGAAAAAAGTCGTA...... GTCGACGGCGATGGGTTCCTGGACTCTGACGAGTTCGCGCTGGCCTTGCACTTAATCAACGTCAAGCTGG AAGGCTGCGAGCTGCCCACCGTGCTGCCGGAGCACTTAGTACCGCCGTCGAAGCGCTATGACTAGTGTCC TGTAGCATACGCATACGCACACTAGATCACACAGCCTCACAATTCCCAAAAAAAAAAAAAAAA >gi|71895640|ref|NM_001031040.1| Mus musculus EH-domain containing 3 (EHD3), mRNA GGTAGGGCGCTACCGCCTCCGCCCGCCTCTCGCGCTGTTCCTCCGCGGTATGCCCGCGCCGGCAGCCGGC...... TATTATATAGAGAAATATATTGTGTATGTAGGATGTGCTTATTGCATTACATTTATCACTTGTCTTAACT AGAATGCATTAACCTTTTTTGTACCCTGGTCCTAAAACATTATTAAAAAGAAAGGCTAAAAAAAAAAAAA AAAA >gi|55742710|ref|NM_153068.2| Mus musculus EH-domain containing 2 (Ehd2), mRNA TGAGGGGGCCTGGGGCCCGCCCTGCTCGCCGCTCCTAGCGCACGCGGCCCCACCCGTCTCACTCCACTGC...... Read sequence Find most downstream CTAG Get the 10bp tag Print the tag End of input? No End Start FASTA: Analyzing complex input

10 4ex.10 FASTA: Analyzing complex input Overall design: 1.Read the sequence 2.Do something Let’s see how it’s done… Do something End of input? No End Start Save header Read line Header? Yes Concatenate to sequence No Read line

11 4ex.11 $line = ; my $endOfInput = 0; while ($endOfInput==0) { # 1.1. Read sequence name from FASTA header if (substr($line,0,1) eq ">") { $name = substr($line,1); } else... # 1.2. Read sequence until next FASTA header $seq = ""; $line = ; while (substr($line,0,1) ne ">") { $seq = $seq. $line; $line = ; if (!defined($line)) { $endOfInput = 1; last; } # 2. Do something... } Do something End of input? No End Start Save header Read line Header? Yes Concatenate to sequence No Read line

12 4ex.12 ################################### # 1. Foreach sequence in the input my (@lines, $line, $name, $seq); $line = ; chomp $line; my $endOfInput = 0; while ($endOfInput==0) { ################################ # 1.1. Read sequence name from FASTA header if (substr($line,0,1) eq ">") { $name = substr($line,1); } else { die "bad FASTA format"; } # 1.2. Read sequence until next FASTA header $seq = ""; $line = ; chomp $line; # Read until next header or end of input while (substr($line,0,1) ne ">") { $seq = $seq. $line; $line = ; if (!defined($line)) { $endOfInput = 1; last; } chomp $line; } ################################ # 2. Do something... } Do something End of input? No End Start Save header Read line Header? Yes Concatenate to sequence No Read line

13 4ex.13 Class exercise 4 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 Hint: while … { $sum = $sum + $num }

14 4ex.14 Class exercise 4 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 '. (Use the example “ genomic FASTA ” from the webpage) 5*.As in Q4, but now concatenate all the sequences whose header starts with ' Chr07 '. 6**.(Home Ex. 3) Write a script that reads several DNA sequences in FASTA format, and print for each FASTA record its header and its G+C content.


Download ppt "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) {"

Similar presentations


Ads by Google