6. The „structure‟ of a Hash
• An array looks something like this:
0
@array =
1
2
'val1' 'val2' 'val3'
Index
Value
• A hash looks something like this:
Rob
Matt
Joe_A
%phone =
353-7236 353-7122 555-1212
Key (name)
Value
7. Printing a hash (continued)
• First, create a list of keys. Fortunately, there is
a function for that:
– keys %hash (returns a list of keys)
• Next, visit each key and print its associated
value:
foreach (keys %hash){
print “The key $_ has the value $hash{$_}n”;
}
• One complication. Hashes do not maintain any
sort of order. In other words, if you put
key/value pairs into a hash in a particular
order, you will not get them out in that order!!
9. Programming in general and Perl in particular
• There is more than one right way to do it. Unfortunately, there are also
many wrong ways.
– 1. Always check and make sure the output is correct and logical
• Consider what errors might occur, and take steps to ensure that you are
accounting for them.
– 2. Check to make sure you are using every variable you declare.
• Use Strict !
– 3. Always go back to a script once it is working and see if you can
eliminate unnecessary steps.
• Concise code is good code.
• You will learn more if you optimize your code.
• Concise does not mean comment free. Please use as many comments as
you think are necessary.
• Sometimes you want to leave easy to understand code in, rather than
short but difficult to understand tricks. Use your judgment.
• Remember that in the future, you may wish to use or alter the code you
wrote today. If you don‟t understand it today, you won‟t tomorrow.
10. Programming in general and Perl in particular
Develop your program in stages. Once part of it works, save the
working version to another file (or use a source code control
system like RCS) before continuing to improve it.
When running interactively, show the user signs of activity.
There is no need to dump everything to the screen (unless
requested to), but a few words or a number change every few
minutes will show that your program is doing something.
Comment your script. Any information on what it is doing or why
might be useful to you a few months later.
Decide on a coding convention and stick to it. For example,
– for variable names, begin globals with a capital letter and privates
(my) with a lower case letter
– indent new control structures with (say) 2 spaces
– line up closing braces, as in: if (....) { ... ... }
– Add blank lines between sections to improve readibility
12. File input / output
Opening a filehandle
• In order to use a filehandle other than STDIN,
STDOUT and STDERR, the filehandle needs to be
opened. The open function opens a file or device and
associates it with a filehandle.
• It returns 1 upon success and undef otherwise.
Examples
• # open a filehandle for reading: open
(SOURCE_FILE, "filename");
• # or open (SOURCE_FILE, "<filename");
• # open a filehandle for writing: open (RESULT_FILE,
">filename");
• # open a filehandle for appending: open (LOGFILE,
">>filename";
13. File input / output
Closing a filehandle
• When you are finished with a filehandle, you
may close it with the close function. The close
function closes the file or device associated
with the filehandle.
Example:
• close (MY_FILE_HANDLE); Filehandles are
automatically closed when the program exits,
or when the filehandle is reopened.
14. File input / output
The die function
• Sometimes the open function fails. For example, opening a file
for input might fail because the file does not exist, and opening a
file for output might fail because the file does not have a write
permission. A perl program will nevertheless use the filehandle,
and will not warn you that all input and output activities are
actually meaningless.
• Therefore, it is recommended to explicitly check the result of the
open command, and if it fails to print an error message and exit
the program.
• This is easily done using the die function.
Example:
• my $k = open (FILEHANDLE, "filename"); unless ($k) { die
("cannot open file filename: $!"); } # in case file "filename"
cannot be opened, # the argument of die will be printed on # the
screen and the program will exit. # $! is a special variable that
contains the respective # error message sent by the operating
system.. A short hand:
• open (FILEHANDLE, "filename") || die "cannot open file
filename: $!";
15. Using filehandles for writing
Example:
#!/usr/local/bin/perl use strict;
use warnings;
open (OUTF, ">out_file") || die "cannot open out_file:
$!"; open (LOGF, ">>log_file") || die "cannot open
log_file: $!";
print OUTF "Here is my program outputn";
print LOGF "First task of my program completedn";
print "Nice, isn't it?n"; # will be printed on the screen
close (OUTF);
close (LOGF);
16. Using filehandles for reading (2/3)
When <FILEHANDLE> is assigned into an array variable, all lines up to the end of
the file are read at once. Each line becomes a separate element of the array.
#!/usr/local/bin/perl
use strict;
use warnings;
my $infile = "CEACAM3.txt";
open (FH, $infile) || die "cannot open "$infile": $!";
my @lines = <FH>;
chomp (@lines); # chomp each element of @lines
close (FH);
# to process the lines you might wish to iterate
# over the @lines array with a foreach loop:
my $line;
foreach $line (@lines) {
# process $line. here we just print it.
print "$linen";
}
17. Using filehandles for reading (1/3)
#!/usr/local/bin/perl
use strict;
use warnings;
my $infile = "CEACAM3.txt";
my ($line1, $line2, $line3);
open (FH, $infile) || die "cannot open "$infile": $!";
$line1 = <FH>; # read first line
print $line1; # proccess line (here we only print it)
$line2 = <FH>; # read next line
print $line2; # proccess line (here we only print it)
$line3 = <FH>; # read next line
print $line3; # proccess line (here we only print it)
close (FH);
18. Using filehandles for reading (3/3)
Using a while loop, read one line at a time and assign it into a scalar variable, as
long as the variable is not an empty string (which will happen at end-of-file).
Note that a blank line read from the file will not result in an empty string, since it still
contains the terminating n.
#!/usr/local/bin/perl
use strict;
use warnings;
my $infile = "CEACAM3.txt";
open (FH, $infile) || die "cannot open "$infile": $!";
my $line;
# or, in one line:
while ($line = <FH>) { # while (my $line = <FH>) {
chomp ($line);
print "$linen"; # process line. here we just print it.
}
close (FH);
20. 1. Swiss-Knife.pl
• Database
– http://www.ebi.ac.uk/swissprot/FTP/ftp.html
– How many entries are there ?
– Average Protein Length (in aa and MW)
– Relative frequency of amino acids
• Compare to the ones used to construct the PAM
scoring matrixes from 1978 – 1991
21. Amino acid frequencies
Secondstep: Frequencies of Occurence
L
A
G
S
V
E
T
K
I
D
R
P
N
Q
F
Y
M
H
C
W
1978
0.085
0.087
0.089
0.070
0.065
0.050
0.058
0.081
0.037
0.047
0.041
0.051
0.040
0.038
0.040
0.030
0.015
0.034
0.033
0.010
1991
0.091
0.077
0.074
0.069
0.066
0.062
0.059
0.059
0.053
0.052
0.051
0.051
0.043
0.041
0.040
0.032
0.024
0.023
0.020
0.014
22. Parser.pl
•
•
#! C:Perlbinperl.exe -w
# (Vergeet niet het pad van perl.exe hierboven aan te passen aan de plaats op je eigen computer)
•
•
# Voorbeeld van het gebruik van substrings en files
# in een parser van sequentie-informatie-records
•
•
use strict;
use warnings;
•
my ($sp_file,$line,$id,$ac,$de);
•
•
$sp_file= "sp.txt";
open (SP,$sp_file) || die "cannot open "$sp_file":$!";
•
•
while ($line=<SP>){
chomp($line);
•
•
my $field = substr ($line,0,2);
my $value = substr ($line,5);
•
•
•
•
•
•
•
•
•
•
if ($field eq "ID"){e
$id = $value
}
if ($field eq "AC"){
$ac = $value
}
if ($field eq "DE"){
$de = $value
}
}
•
•
•
print "Identification: $idn";
print "Accession No.: $acn";
print "Description: $den";
23. 2. PAM-simulator.pl
– Check transition matrix with and without randomizing the
rows of evolutions
– Adapt the program to simulate evolving DNA
– Adapt the program so it generates random proteins taking
into account the relative frequences found in step 1
– Write the output to a multi-fasta file
>PAM1
AHFALKJHFDLKFJHALSKJFH
>PAM2
AHGALKJHFDLKFJHALSKJFH
>PAM3
AHGALKJHFDLKFJHALSKJFH
24. Experiment: pam-simulator.pl
• Initialize:
– Generate Random protein (1000 aa)
• Simulate evolution (eg 250 for PAM250)
– Apply PAM1 Transition matrix to each amino
acid
– Use Weighted Random Selection
• Iterate
– Measure difference to orginal protein
25. Dayhoff’s PAM1 mutation probability matrix (Transition Matrix)
A
Ala
A
R
N
D
C
Q
E
G
H
I
R
N
D
C
Q
E
G
H
Arg Asn Asp Cys Gln Glu Gly His
I
Ile
9867
2
9
10
3
8
17
21
2
6
1
9913
1
0
1
10
0
0
10
3
4
1
9822
36
0
4
6
6
21
3
6
0
42
9859
0
6
53
6
4
1
1
1
0
0
9973
0
0
0
1
1
3
9
4
5
0
9876
27
1
23
1
10
0
7
56
0
35
9865
4
2
3
21
1
12
11
1
3
7
9935
1
0
1
8
18
3
1
20
1
0
9912
0
2
2
3
1
2
1
2
0
0
9872
28. 3. Palindromes
What is the longest palindroom in palin.fasta ?
Why are restriction sites palindromic ?
How long is the longest palindroom in the genome ?
Hints:
http://www.man.poznan.pl/cmst/papers/5/art_2/vol5a
rt2.html
Palingram.pl