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 = <STDIN>;
if ($number < 0 or $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
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
ComparisonNumericString
Equal==eq
Not equal!=ne
Less than<lt
Greater than>gt
Less than or equal to
>=le
Greater than or equal to
<=ge
if ($age == 18){
...
}
if ($name eq "Yossi")...
if ($name ne "Yossi")...
if ($name lt "n")...
ComparisonNumeric
Equal==
Not equal!=
Less than<
Greater than>
Less than or equal to
>=
Greater than or equal to
<=
4.4Loops
Commands inside a loop are executed repeatedly (iteratively):my $num=0;
print "Guess a number.\n";
while ($num != 31) {
$num = <STDIN>;
}
print "correct!\n";
my @names = <STDIN>;
chomp(@names);
my $name;
foreach $name (@names) {
print "Hello $name!\n";
}
4.5 Loops: foreachThe 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
4.6Fasta 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 AAACATGGCGGCGCCCTGCGCGGCTTCCCGTCGCCGCAACCGTGGGGCCGGCCCTGCCTTGGAGCGGAGCCGAAGCATCCCTTGCTGCACGCAGGGCAGAGCAGGCGAGGGCTGGGGGCCGTATAACTTATTTTATATCCATATTCAGACTATATAGAGAATATTCTATGCATCTATGACGTGCTTAC>gi|197099147|ref|NM_001131576.1| Pongo abelii EH-domain AGAGCTGAGCGCCTGCCCACAAACATGGCGGCGCCCTGCGCGGCTTCCCTTCGCCGGGACCGCCTGGGGCTGCAGGATGCTGCTGCGGATGCTGAGCTGTCCGCGGGTTGGGCAGCGTCGCTGCGCGGCTTCCCTT>gi|55742034|ref|NM_001006733.1| Xenopus tropicalis EH-domain CGGGCAAGACCACCTTCATCCGCCACCTCATAGAGCAGGACTTCCCCGGCATGAGGATCGGGCCCGAACCGGGGACTTCCTCTGCGCGCCGGCTTCCTGCCCAGCTGGCATTTAAACCACACATGGCGGCGCCCTGCGCGGCTTCCCGTCGCCGCAACCGTGGGGCCGGCC
4.7Breaking out of loops
next – skip to the next iteration last – skip out of the loop
my @lines = <STDIN>;foreach $line (@lines) {
if (substr($line,0,1) eq ">") { next; }if (substr($line,0,8) eq "**stop**") { last; }print($line);
}
4.9Scope 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 = <STDIN>);
print "Hello $name, what is your age?\n";
my $age;
$age = <STDIN>;
}
print $name;
print $age;
Global symbol "$age" requires explicit package name
4.10Never 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!
4.11Never 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";
}
print "Hello $name!\n";
Hello Ruti!
Hello Nimrod!
Hello Nimrod!
Hello Nimrod!
Hello Nimrod!
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
4.13
Is this variables defined?
defined check whether a variable was defined.
my $a;
if (defined $a){
print($a+3);
}
my $line = <STDIN>;
while (defined $line){
print "line is $line";
$line = <STDIN>;
}
print "done!!!\n"ctrl-z to
indicate end of input
4.14
Is this variables defined?
defined check whether a variable was defined.
my $line = <STDIN>;
while (defined $line){
if (substr($line,0,1) eq ">"){
print "$line";
}
$line = <STDIN>;
}
4.15FASTA: 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)
4.16FASTA: 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 orend of input
Yes
Concatenate to sequence
No
Read line
Read line
4.17
# 1. Read FASTA sequece
$fastaLine = <STDIN>;
while (defined $fastaLine) {# 1.1. Read FASTA header
$header = substr($fastaLine,1);
$fastaLine = <STDIN>;# 1.2. Read sequence until next FASTA header
while ((defined $fastaLine) and
(substr($fastaLine,0,1) ne ">" ))
{
$seq .= $fastaLine;
$fastaLine = <STDIN>;
}
# 2. Do something
... # 2.1 compute $gcContent
print "$header: $gcContent\n";
}
Do something
End of input? No
End
Start
Save header
Read line
Header orend of input
Yes
Concatenate to sequence
No
Read line
Read line
4.18Class exercise 4a
1. Write a script that reads lines of names and expenses:Yossi 6.10,16.50,5.00Dana 21.00,6.00Refael 6.10,24.00,7.00,8.00ENDFor 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.00Dana+21.00+6.00Refael +6.10+24.00+7.00+8.00END
Sum the numbers while there is a '+' sign before them.
Sum the numbers while there is a '+' sign before them.
Output:Yossi 27.6Dana 27Refael 45.1
Output:Yossi 27.6Dana 27Refael 45.1
4.19Class 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'.
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 <STDIN>:my $line = <IN>;
my @inputLines = <IN>;foreach $line (@inputLines) ...
Every filehandle opened should be closed:close(IN);
Reading files
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
4.23Class 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.