Presentation is loading. Please wait.

Presentation is loading. Please wait.

Where to get Perl ? www.perl.com / www.perl.org Unix/Linux/Solaris Unix/Linux/Solaris Interpreter (called “perl”) comes pre-installed usually Interpreter.

Similar presentations


Presentation on theme: "Where to get Perl ? www.perl.com / www.perl.org Unix/Linux/Solaris Unix/Linux/Solaris Interpreter (called “perl”) comes pre-installed usually Interpreter."— Presentation transcript:

1 Where to get Perl ? www.perl.com / www.perl.org Unix/Linux/Solaris Unix/Linux/Solaris Interpreter (called “perl”) comes pre-installed usually Interpreter (called “perl”) comes pre-installed usually Get source from www.perl.com Get source from www.perl.comwww.perl.com Win32 Win32 Get source distribution from www.perl.com Get source distribution from www.perl.comwww.perl.com Have to compile Have to compile Get binary from www.activestate.com Get binary from www.activestate.comwww.activestate.com No need to compile, runs pretty well No need to compile, runs pretty well Macintosh Macintosh www.macperl.com www.macperl.com www.macperl.com Perl IDE (integrated development environment) Perl IDE (integrated development environment) Open Perl IDE (Jurgen Guntherodt) Open Perl IDE (Jurgen Guntherodt) It’s mainly an editor. It’s mainly an editor. ActiveState Komodo 3.0 (IDE for Perl, Python, Tcl, …) ActiveState Komodo 3.0 (IDE for Perl, Python, Tcl, …) $29.95 for a student license $29.95 for a student license Visit the class web site for the links!

2 Running Perl: Windows Open a text editor such as Notepad Open a text editor such as Notepad 1. write your perl code 2. save your code with an extension “.pl” Open MS-DOS/Command window Open MS-DOS/Command window 1. go to your folder in which your code is saved 2. make a shortcut to the command window (enter “cmd” to make the shortcut) 3. right click on the icon and go to properties 4. clear the “start in” field 5. Run your code as “perl ” or 1. go to Start → run → “cmd”, … 2. run your code as “perl ”

3 Programming and Perl for Bioinformatics Part II

4 Basic Data Types Perl has three basic data types: Perl has three basic data types: scalar scalar array (list) array (list) associative array (hash) associative array (hash)

5 Arrays An array (list) is an ordered group of scalar values. ‘@’ is used to refer to the entire array ‘@’ is used to refer to the entire array Example: Example: (1,2,3) # Array of three values 1, 2, and 3 ("one","two","three") # Array of 3 values "one", "two", "three" @names = ("mary", "tom", "mark", "john", "jane"); @names = ("mary", "tom", "mark", "john", "jane"); $names [1] ;? $names [1] ;? @names [1..4]; @names [1..4]; Extract 2 nd item from @namesExtract the sublist from @names # “tom”

6 More on Arrays @a = ();# empty list @a = ();# empty list @b = (1,2,3);# three numbers @b = (1,2,3);# three numbers @c = ("Jan","Joe","Marie");# three strings @c = ("Jan","Joe","Marie");# three strings @d = ("Dirk",1.92,46,"20-03-1977");# a mixed list @d = ("Dirk",1.92,46,"20-03-1977");# a mixed list Variables and sublists are interpolated in a list Variables and sublists are interpolated in a list @b = ($a,$a+1,$a+2);# variable interpolation @b = ($a,$a+1,$a+2);# variable interpolation @c = ("Jan",("Joe","Marie"));# list interpolation @c = ("Jan",("Joe","Marie"));# list interpolation @d = ("Dirk",1.92,46,(),"20-03-1977");# empty list interpolation @d = ("Dirk",1.92,46,(),"20-03-1977");# empty list interpolation @e = ( @b, @c );# same as (1,2,3,"Jan","Joe","Marie") @e = ( @b, @c );# same as (1,2,3,"Jan","Joe","Marie") Practical construction operators ($x..$y) Practical construction operators ($x..$y) @x = (1..6) # same as (1, 2, 3, 4, 5, 6) @x = (1..6) # same as (1, 2, 3, 4, 5, 6) @y = (1.2..4.2) # same as (1.2, 2.2, 3.2, 4.2, 5.2) @y = (1.2..4.2) # same as (1.2, 2.2, 3.2, 4.2, 5.2) @z = (2..5,8,11..13) # same as (2,3,4,5,8,11,12,13) @z = (2..5,8,11..13) # same as (2,3,4,5,8,11,12,13)

7 Array Manipulations reverse Reverses the order of array elements @a = (1, 2, 3); @b = reverse @a; # @b = (3, 2, 1); splitSplits a string into a list/array $line = "John Smith 28"; ($first, $last, $age) = split /\s/, $line; $DNA = "ACGTTTGA"; @DNA = split ('', $DNA); joinJoins a list/array into a string $gene = join "", ($exon1, $exon3); $gene = join "", ($exon1, $exon3); $name = join "-", ("Zhong", "Hui"); scalar Returns the number of elements in @array scalar @array; sort Return sorted elements sort { $a $b } @not_sorted # numerical sort sort { $a cmp $b } @not_sorted # ASCII-betical sort

8 Exercise #Determine freq of nucleotides $dna ="gaTtACataCACTgttca"; ?

9 Ex: Determine freq of nucleotides $dna ="gaTtACataCACTgttca"; $dna = uc($dna); #GATTACATACACTGTTCA $count_A = 0; $count_C = 0; $count_G = 0; $count_T = 0; @dna = split '', $dna; foreach $base (@dna) { if ($base eq 'A') {$count_A++;} elsif ($base eq 'C') {$count_C++;} elsif ($base eq 'G') {$count_G++;} elsif ($base eq 'T') {$count_T++;} else {print "error!\n";} } print "count of A = $count_A \n"; print "count of C = $count_C \n"; print "count of G = $count_G \n"; print "count of T = $count_T \n";

10 Filehandles File I/O (input/output): reading from/writing to files Files represented in Perl by a filehandle variable Files represented in Perl by a filehandle variable (for clarity, usu. written as a bare word in UPPERCASE) Open a file on a filehandle using the open function Open a file on a filehandle using the open function for reading (input): for reading (input): open INFILE, “< datafile.txt”; or open (INFILE, “< datafile.txt”); or open (INFILE, “< datafile.txt”); for writing (output), overwriting the file: for writing (output), overwriting the file: open OUTFILE, “> output”; for appending to the end of the file: for appending to the end of the file: open OUTFILE, “>> output”; Close a file on a filehandle Close a file on a filehandle Close (OUTFILE); Close (OUTFILE);

11 Special Filehandles Special “files” that are always “open” STDIN (standard input) STDIN (standard input) input from command window read only input from command window read only STDOUT (standard output) STDOUT (standard output) output to command window write only output to command window write only print STDOUT “Have fun with Perl!\n”; or just print “Have fun with Perl!\n”;

12 Input from Filehandles “Angle Bracket” input operator reads one line of input (up to newline/carriage return) reads one line of input (up to newline/carriage return) from STDIN: from STDIN: print "Enter name of protein: "; $line = ; chomp $line; # removes \n from end of $line print “\nYou entered $line.\n”; from a file: from a file: open (INPUT, “aminos.txt”); $amino1 = ; $amino2 = ; chomp ($amino1, $amino2);

13 sequences.fasta >gi|145536|gb|L04574.1|Escherichia coli DNA polymerase III chi subunit (holC) gene, complete cds TAACGGCGAAGAGTAATTGCGTCAGGCAAGGCTGTTATTGCCGGATGCGGCGTGAACGCCTTATCCGACCTACACAGCACTGAACTCGTAGGCCTGATAAGACACAACAGCGTCGCATCAGGCGCTGCGGTGTATACCTGATGCGTATTTAAATCCACCACAAGAAGCCCCATTTATGAAAAACGCGACGTTCTACCTTCTGGACAATGACACCACCGTCGATGGCTTAAGCGCCGTTGAGCAACTGGTGTGTGAAATTGCCGCAGAACGTTGGCGCAGCGGTAAGCGCGTGCTCATCGCCTGTGAAGATGAAAAGCAGGCTTACCGGCTGGATGAAGCCCTGTGGGCGCGTCCGGCAGAAAGCTTTGTTCCGCATAATTTAGCGGGAGAAGGACCGCGCGGCGGTGCACCGGTGGAGATCGCCTGGCCGCAAAAGCGTAGCAGCAGCCGGCGCGATATATTGATTAGTCTGCGAACAAGCTTTGCAGATTTTGCCACCGCTTTCACAGAAGTGGTAGACTTCGTTCCTTATGAAGATTCTCTGAAACAACTGGCGCGCGAACGCTATAAAGCCTACCGCGTGGCTGGTTTCAACCTGAATACGGCAACCTGGAAATAATGGAAAAGACATATAACCCACAAGATATCGAACAGCCGCTTTACGAGCACTGGGAAAAGCAGGGCTACTTTAAGCCTAATGGCGATGAAAGCCAGGAAAGTTTCTGCATCATGATCCCGCCGCCGAA

14 Determine frequency of nucleotides Input file: Input file: open (INPUT, "sequences.fasta"); #open file for sequence $line1 = ; $line2 = ; $line3 = ; chomp ($line2, $line3); $dna = $line2.$line3; $count_A = 0; $count_C = 0; $count_G = 0; $count_T = 0; @dna = split '', $dna; foreach $base (@dna) { if ($base eq 'A') {$count_A++;} elsif ($base eq 'C') {$count_C++;} elsif ($base eq 'G') {$count_G++;} elsif ($base eq 'T') {$count_T++;} else {print "error!\n";} } print "count of A = $count_A \n"; print "count of C = $count_C \n"; print "count of G = $count_G \n"; print "count of T = $count_T \n";


Download ppt "Where to get Perl ? www.perl.com / www.perl.org Unix/Linux/Solaris Unix/Linux/Solaris Interpreter (called “perl”) comes pre-installed usually Interpreter."

Similar presentations


Ads by Google