Presentation is loading. Please wait.

Presentation is loading. Please wait.

Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.

Similar presentations


Presentation on theme: "Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released."— Presentation transcript:

1 Books

2 Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released in 1994. Low Level lang. (C/C++,Pascal) – hard to write, fast runtime, unlimited. High Level lang. (shell, awk, …) – hard, slow, very limited. Perl – easy, mostly fast, nearly unlimited.

3 Perl Optimized to work with text Free source code, very good support www.cpan.orgwww.cpan.org (Perl source, docs, extensions) www.perldoc.com/ Perl for Windows www.activestate.comwww.activestate.com

4 Just for Start Returns a list of strings from the input files Sorts the list Prints the list >sort.pl *.txt #!/usr/bin/perl print sort ; sort.pl

5 Hello Perl On Unix create text file, “hello.pl” (any name/extension is ok): #!/usr/bin/perl print “Hello, Perl!\n”; #comment your code $var1= “Hello, World!”. ”\n”; #No variable declaration is needed print $var1; $ivar += 10; #scalar variables initialized with 0 # (strings with empty string “” ) print “This is $ivar \n”;

6 Hello Perl (2) Set on executable flag: >chmod +x hello.pl Run it: >hello.pl #!/usr/bin/perl always use this header (without any spaces)

7 Strings ‘Hello Perl\n’ not equal to “Hello Perl\n” ‘Hello $var’ not equal to “Hello $var” Concatenation : “Hello”. ”Perl\n” = “Hello Perl\n” Copy: “Perl” x 3 = “PerlPerlPerl”

8 Print print “hello ”; print 3*$ivar; print “\n”; print “hello “, 3*$ivar, “\n”; $world=“world”; print “hello “, $world, “\n”; print “hello “. $world. “\n”; print “hello $world \n”; print “hello ${world}s \n”;

9 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( $ivar != 5) … If( $str ne “hello”) …

10 Binary Operators a + b a – b a * b a / b a ** b a b a % bmodulus Unary Operators Changing Sign +a positive operand -a negative operand Changing Value Before Usage ++a $a=3; $b= ++$a; #b=4, a=4 --a Changing Value After Usage a++ $a=3; $b= $a++; #b=3, a=4 a--

11 User Input $line= ; while ($line= ){ chomp($line); #remove new line char \n if($line eq “quit”){ exit(0); }

12 Arrays $rocks[0]=“bedrock”; $rocks[1]=“lava”; $rocks[99]=“rock”; # now there are 100 elements print “$rocks[ $#rocks ] \n”; # prints last element ‘rock’ $size = $#rocks + 1; #number of elements

13 Lists (0,5,6,7,8); (0,5..8); # (..) range operator (0,”5..8”); # contains two elements 0 and string “5…8”

14 List Assignment ($color, $tree, $list) = (“green”, “red-black”, “linked”); #swap ($color, $tree) = ($tree, $color); ($i, $j)=(1..3); # 3 – is ignored ($i, $j, $k)=(1,2); # $k gets undef

15 List Assignment (2) ($color[0], $color[1], $color[2])=(“red”, “blue”, “green”); @colors=(“red”, “blue”, “green”); @more_colors=(“white”, @colors, “yellow”); # more_colors contains “white”, “red”, “blue”, “green”, “yellow” print “Five colors: @more_colors. \n”; #five colors: white red blue green yellow.

16 Foreach @colors=(“red”, “blue”, “green”); for($i=0;$i <= $#colors; $i++){ print “$colors[$i] \n”; } #other way to do the same #but array is changed foreach $col (@colors){ $col.= “\n”; } print @colors; #much better foreach $col (@colors){ print “$col \n”; }

17 Push and Pop @array= 1..5; push @array, 6; #array contains 1..6 $six= pop @array; #array contains 1..5 @others= 6..10; push @array, @others; #array contains 1..10

18 Shift and Unshift Push and Pop – for the end of an array Shift and Unshift – for the start of an array @array= 1..5; $five = shift @array; #array contains 2…5 unshift @array, 1; #array contains 1…5 unshift @array, (-2,-1,0); #array contains –2…5 ! Shift and Unshift, unlike Push and Pop, change indices of all array elements

19 Default variable $_ @colors=(“red”, “blue”, “green”); foreach (@colors){ $_.= “ \n”; } $_ = “default variable\n”; print; #prints “default variable\n”

20 Reverse, Sort operators @array= 1..100; print reverse(@array); @array= reverse (@array); #array contains 100..1 #sorts in ASCII order @array= sort (@array); #array contains 1, 100,11,12 … 19,2,20,…,9,90…99

21 in List Context open FILE, “readme.txt” or die “Cannot open file: $!”; while ($line= ){ chomp($line); #remove new line char \n push @lines, $line; } # or this way chomp ( @lines = ); #better way @lines = ; chomp( @lines );

22 Perl is Context Dependent @sorted_array= sort @array; #list context $ii = 3 + @array; #scalar context: 3+ArraySize ($ii) = @array; #list context: $ii=$array[0] @array = 38; #list context: @array=(38) $str= ; #return next line form FILE @lines= ; #returns all remaining lines

23 Example Task: Write a program that prints each line in a right- justified 20 character column. First print a “ruler-line” of digits. 1234567890 123456789012345678901234567890 hello test-test 20

24 Example (solution) chomp( @lines= ); print “1234567890” x 7, “\n”; foreach (@lines){ printf “%20s\n”, $_; } $format = “%20s\n” x @lines; printf $format, @lines; For more info on printf run: perldoc –f sprintf

25 Control Statements: unless, until if( $a != $b){ } unless ( $a== $b){ } while( $a > $b){ } until( $a<=$b ){ }

26 Control Statements: elsif if ( expression1 ){ }elsif(expression2 ){ }elsif(expression3 ){ }else{ } Only the block of the first true conditional expression is executed, or else otherwise.

27 Control Statements: next, last while( ){ if( /Protein ID/ ){ print ; next; } ……….. if( /Remarks/ ){ last; }

28 Control Statements: redo while ( … ) { if( … ){ redo; #starts the current iteration from the beginning (control # statement is not evaluated) #notice the difference with the next (starts the next iteration) }

29 Control Statements: logical operator $proteinID = $proteins{ $id } or “Unknown”; #or like in C - expression ? TrueExpression : FalseExpression; $proteinID = $proteins{ $id } ? $proteins{ $id } : “Unknown”;

30 Example: Parsing FASTA file >roa1_drome Rea guano receptor type III >> 0.1 MVNSNQNQNGNSNGHDDDFPQDSITEPEHMRKLFIGGLDYRTTDENLKAHEKWGNIVDVV VMKDPRTKRSRGFGFITYSHSSMIDEAQKSRPHKIDGRVEPKRAVPRQDIDSPNAGATVK KLFVGALKDDHDEQSIRDYFQHFGNIVDNIVIDKETGKKRGFAFVEFDDYDPVDKVVLQK QHQLNGKMVDVKKALPKNDQQGGGGGRGGPGGRAGGNRGNMGGGNYGNQNGGGNWNNGGN NWGNNRGNDNWGNNSFGGGGGGGGGYGGGNNSWGNNNPWDNGNGGGNFGGGGNNWNGGND FGGYQQNYGGGPQRGGGNFNNNRMQPYQGGGGFKAGGGNQGNYGNNQGFNNGGNNRRY >roa2_drome Rea guano ligand MVNSNQNQNGNSNGHDDDFPQDSITEPEHMRKLFIGGLDYRTTDENLKAHEKWGNIVDVV VMKDPTSTSTSTSTSTSTSTSTMIDEAQKSRPHKIDGRVEPKRAVPRQDIDSPNAGATVK KLFVGALKDDHDEQSIRDYFQHLLLLLLLDLLLLDLLLLDLLLFVEFDDYDPVDKVVLQK QHQLNGKMVDVKKALPKNDQQGGGGGRGGPGGRAGGNRGNMGGGNYGNQNGGGNWNNGGN NWGNNRGNDNWGNNSFGGGGGGGGGYGGGNNSWGNNNPWDNGNGGGNFGGGGNNWNGGND FGGYQQNYGGGPQRGGGNFNNNRMQPYQGGGGFKAGGGNQGNYGNNQGFNNGGNNRRY

31 Example: Parsing FASTA file (2) print “Input file name:”; $filename= ; open FASTA, $filename; @lines= ; foreach $line (@lines){ f($line =~ /^\s*$/){ next; #continue if empty line } if($line =~ />/){ print "Header:", $line; }else{ print "Seq:", $line; } Parses file in FASTA format. Filename is input to the program.

32 Example: Parsing FASTA file (3) die "Can't open file:$ARGV[0]" unless open( FASTA, $ARGV[0]); @lines= ; foreach (@lines){ unless( /^\s*$/ ){ #continue if empty line if( />/ ){ print "Header:", $_; }else{ print "Seq:", $_; } Parses file in FASTA format. Filename is argument to the program.

33 Example: Parsing FASTA file (4) @lines= <>; foreach (@lines){ unless( /^\s*$/ ){ #continue if empty line if( />/ ){ print "Header:", $_; }else{ print "Seq:", $_; } Parses file in FASTA format. Filenames are arguments to the program. Automatic error message in case of invalid filename. Reads all input files (from the argument list)

34 Example: Parsing FASTA file (5) while ( <> ){ unless( /^\s*$/ ){ #continue if empty line if( />/ ){ print "Header:", $_; }else{ print "Seq:", $_; } Parses file in FASTA format. Filenames are arguments to the program.

35 HomeWork (a)Parsing FASTA file. Write two separate programs. Read FASTA file. Output reverse sequence in FASTA format. Hint: use split function (perldoc –f split). For DNA sequence output reverse complement. Hint: use substr function (perldoc –f substr) (c) Create your personal I-net Home Page.


Download ppt "Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released."

Similar presentations


Ads by Google