Perl scripting Bas E Dutilh Marc van Driel
Perl scripting Bas E. Dutilh, Marc van Driel This course is licensed under the Creative. Commons Attribution-Non. Commercial-Share. Alike 3. 0 Unported (CC BY-NC-SA 3. 0) license For more information: http: //creativecommons. org/licenses/by-nc-sa/3. 0/
Programming n Analysis What is the problem you want to solve? n Modeling Which steps do you need? n Implementation Write the steps in code n Debugging Removing the errors
Why program?
Perl n n Scripting language Rapid development For Unix / Windows / … Big community ¡ n n Google answers most scripting questions! Fast / efficient Fun
Layout #!/usr/bin/perl # My first Perl script print "My first Perl scriptn"; n Shebang ¡ n n /usr/bin/perl Comment lines indicated with a hash Functions are used to do things ¡ ¡ ¡ n Location of the interpreter (Perl program) #! Functions are case sensitive Built-in (functions) or write yourself (subroutines) End each operation with a semicolon String of characters delimited by quotes ¡ Newline character # print ; "" n
Variables: scalars n Number ¡ Operators n n n n Be Add Subtract Multiply Divide Modulus Exponent + * / % ** = += -= *= /= %= **= . =. = $n = $a = $n + $b = $a $c = $b * $d = $c / $e = $d % $f = $e ** 3; 2; 1; 4; 2; 3; 2; String ¡ Operators n n n $ Be Concatenate Pointer $s = "hell"; $g = $s. "o";
Strings n Single quotes ¡ ¡ ¡ n Escape special characters print 'Single quote: ''; ' print 'Backslash: \'; Double quotes ¡ ¡ ¡ print 'String'; Tab Newline " $ print "Stringn"; print "column 1tcolumn 2n"; print "line 1n$line 2n"; print "Double quote: "n"; print "Backslash: \n"; print "Dollar $ignn";
Conditions Number $n = 3; n if ($n == 3) { } If ¡ ¡ ¡ Equal to Not equal to Greater than n ¡ ¡ n n $g eq "hello" $g ne "bye" $g gt "bye" $n >= 3 $n < 4 $g ge "hello" $g lt "yes" $n <= 3 $g le "hello" Strings: alphabetically Equal to or smaller than Else if Else $n == 3 $n != 2 $n > -2 Strings: alphabetically Equal to or greater than Smaller than n ¡ String $g = "hello"; elsif ($g == "hello") { } else { }
Logical operators n FALSE ¡ ¡ n ¡ Non-zero numbers Strings if (("true") && (1)) { } TRUE if condition 1 AND condition 2 are true OR ¡ if (! ("")) { } TRUE if condition is FALSE AND ¡ n if (-488) { } NOT ¡ n Number: 0 Empty string: "" TRUE ¡ n if (0) { } if (("false") || (0)) { } TRUE if condition 1 AND / OR condition 2 are true
Some string functions $g = "hello"; n Length $l = length ($g); print "$ln"; n Reverse $r = reverse ($s); print "$rn"; n Sub-string ¡ $s = substr ($g, 0, 4); Arguments: string, start-position, length print "$sn"; n Split @a = split //, $s; print "$a[4]n";
Variables: arrays n @ Array (= list) of scalars: @a = (1, "A", "yes"); $a[3] = 5. 66; $a[4] = 3; $a[5] = $a[3] - $a[0] ** $a[4]; $a[6] = $a[4]. $a[3]; n 1 A yes 5. 66 3 101. 19 35. 66 0 1 2 3 4 5 6 Special elements $b = $a[-1] - $a[-4]; $c = $#a;
Some array functions n Scalar @a = (1, "A", "yes", 5. 66, 3); $s = scalar (@a); print "$sn"; n n n Join $j = join (@a, "; "); print "$jn"; Push Splice ¡ push (@a, "next"); print "$a[-1]n"; @b = splice (@a, 3, 2, "no"); Arguments: array, start, length, replace print "$a[3]t$b[-1]n"; n Sort ¡ n Default: alphabetically Reverse @c = sort (@a); print "$c[0]n"; @r = reverse (@a); print "$r[0]n";
Variables: Hashes % @a Array: $a[0] n $a[5] 1 A yes 0 1 2 5. 66 3 3 101. 19 4 5 35. 66 $#a 6 A hash is a list with a string as index n No order %h $h{"I am last"} Hash: "a "s yes 5. 66 3 101. 19 35. 66 "I A "m 1 $h{"a"} am " st la o" to e" e "m 3" "2 " ob " "B do un eg "
Some hash functions %h = ("a" => 1, "segundo" => "A", "Bob" => "yes", "23" => 5. 66, "me too" => 3, "me" => 101. 19, "I am last" => 35. 66); n Keys n Values @k = keys (%h); print "$k[2]n"; @v = values (%h); print "$v[2]n";
Loops n For each Note the indent: keep your script readable! @a = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9); foreach $i (0 (@a) (0, . . 1, {9) 2, {3, 4, 5, 6, 7, 8, 9) { print "$in"; } n While $i = 0; while ($i < 10) { print "$in"; ++$i; } n For for ($i = 0; $i < 10; $i += 1) { print "$in"; } ¡ ¡ ¡ Start Condition Iterate
Jumping loops n Next ID: foreach $i (keys %seqs) { if (substr ($i, 0, 1) ne ">") { next ID; } print "$in$seqs{$i}n"; } n Last LOOP: for ($i = 0; $i < 10; $i += 1) { if ($i >= 5) { last LOOP; } print "$in"; }
Reading from files n n Open for reading Read if (!(IN, open (open "<genes. txt"); (IN, "<genes. txt"))) { die "Can't open genes. txt: $!n"; } $line = <IN>; print "The first line is: $line"; while ($line = <IN>){ n Chomp } n Close chomp $line; print $line. "n"; close IN;
Writing to files n n Open for writing > overwrite if (! (open (G, ">genes. txt"))) { Write die "Can't write to genes. txt: } @genes = ("gene 1", "gene 2", "gene 3"); print G "$genes[0]n"; close G; n Close n Open for writing >> append $!n"; if (! (open (G, ">>genes. txt"))) { die "Can't append to genes. txt: $!n“; } for ($i = 1; $i < 3; ++$i) { print G "$genes[$i]n"; } close G;
Further reading Programming Perl Larry Wall Beginning perl for bioinformatics James Tisdall CPAN - Comprehensive Perl Archive Network http: //search. cpan. org/
- Slides: 19