A primer on Perl programming First structures (with examples) http://www.shlomifish.org/lecture/Perl/Newbies/lecture1/
Mar 26, 2015
A primer on Perl programming
First structures (with examples)
http://www.shlomifish.org/lecture/Perl/Newbies/lecture1/
A primer on Perl programming
Thanks for using the blog
A primer on Perl programming
Thanks for using the blog
A primer on Perl programming
• Perl is popular among bioinformaticists because it’s easy, has a huge amount of modules and was born to parse text.
• A program takes an input and returns some elaborated output.
• We need to: test conditions, and iterate blocks of instructions.
A primer on Perl programming
To reverse complement a multifasta file we need to:
•Input: filename•Open the file and parse it (headers, sequences)
• E.g.: IF line starts with ‘>’ it’s a header (one line), else concatenate lines…
•Output: Print header as is, reverse and complement the sequence
Recall: variables
$scalar contains a single value (number or string
@array is an ordered list of scalars %hash is a unordered list of values with a
name (“key”). %age = ('Andrea' => 29, 'Cesare' => 80); $age{'Carlo'} = 39; print “$name is $age{$name} old.\n”;
The foreach cycle
Performs a block of instruction for each element of an array
@names = ('Aldo', 'John', 'Jack');
foreach $name (@names) {
$count++;
print “Hello $name!\n”;
}
print “There were $count people in the array!\n”;
The for cycle
When you have to repeat a set of instruction a defined number of times.
for (initial condition; until; ending instuction ) {
...block...
}
for ($i = 1; $i<=10; $i++) {
print “\$i now is $i\n”;
}
Iterations
#print numbers 1-10 in three different ways
$i = 1;
while ($i<=10) {
print "$i\n";
$i++;
}
for ($i=1; $i<=10; $i++) {
print "$i\n";
}
foreach $i (1,2,3,4,5,6,7,8,9,10) {
print "$i\n";
}
Logical operators
They are different for numbers and strings!!!
Equal: == or eq $a == 10 $a eq 'hello'
Not equal: != or ne Greater than: >, >= Less than: <, <=
Operators
• Same precedence as in math
• +, -, *, /
• Modulus (division remainder): %
• Power: **
• Concatenate strings: .
$seqlength = 18;
print ‘There are ’.$seqlength**4.‘ combinations\n’;
Math functions
• Some functions:
• abs($x): absolute value
• int($x): integer part
• rand($x): random number up to $x
• sqrt($x): square root
$percentage = int(100*$done/$todo);
$dice = int(1+rand(6));
String functions
• Simple as:$length = length(‘Put a string here’);
• Less simple as:$portion = substr($string, $start, $length);
@pieces = split(/;/, $string);
• A world apart:
Pattern matching$string=~/TATA/;
If statement
Executes a block of code if a condition is TRUE. You can use boolean logic (and, or).
Quite simple:if (length($a) == 10) {
print “Yes, its ten characters long!\n”;
} else {
print “No, \$a is not as long as I want.\n”;
}
IF… ELSE…
Only one condition:if (condition) {
block…
} else {
block…
}
Multiple conditions: elsif (other condition)
Talking to scripts
Parameters passed from command line are stored in @ARGV (uppercase!)
Eg:$ perl myscript.pl 100 Genomics
print “First parameter: $ARGV[0]\n”;
print “Last one: GUESS\n”;
Can you write a script that prints the number of parameters passed by the users plus a list of them?
Reading a file
open(NAME, “Filename”) || die “Unable to read\n”;
while (<NAME>) {
print “$_”;
}
Don’t be scared!