260 likes | 449 Views
Reverse FASTA sequence. @input=<>; @seq=(); for($i=0; $i<= $#input; ++$i){ $line=$input[$i]; chomp $line; if($line =~ /^s*$/ and $i != $#input) { next; } if(! ($line =~ />/) ){ push @seq, split(//, $line); }
E N D
Reverse FASTA sequence @input=<>; @seq=(); for($i=0; $i<= $#input; ++$i){ $line=$input[$i]; chomp $line; if($line =~ /^\s*$/ and $i != $#input) { next; } if(! ($line =~ />/) ){ push @seq, split(//, $line); } if( ($line =~ />/) or ($i == $#input)) { #new sequence #print previous sequence if($#seq >0 ){ print reverse (@seq), "\n\n"; } if($i != $#input){ @seq=(); print $line, "\n"; #print header } } }
Utilities.pm some.pl package Utilities; sub checkInput{ my ($input)=@_; … } our$path=“/home/usr/local/”; my $var=“”; #local definition #last evaluated value must be #positive = #successful package load 1; use Utilities; Utilities::checkInput(“for check”); print $Utilities::path; my $path=“/locale/”; #local definition Packages
Debugger On Unix: “perldoc perldebug” Invoke Perl with the -d switch: perl –d your_code.pl arg1 arg2 …
Debugger (2) • always displays the line it's about to execute • Any command not recognized by the debugger is directly executed (eval'd) as Perl code (for example you can print out some variables). p expr (as “print expr”) x expr - Nested data structures are printed out recursively, unlike the real print function in Perl
Debugger (3) • s [expr] • Single step. Executes until the beginning of another statement, descending into subroutine calls. If an expression is supplied that includes function calls, it too will be single-stepped. • n [expr] • Next. Executes over subroutine calls, until the beginning of the next statement. If an expression is supplied that includes function calls, those functions will be executed with stops before each statement. • <CR> • Repeat last n or s command.
Debugger (4) • r • Continue until the return from the current subroutine. • c [line|sub] • Continue, optionally inserting a one-time-only breakpoint at the specified line or subroutine. • w [line] • List window (a few lines) around the current/[line] line
Debugger (5) b subname [condition] b [line] [condition] Set a breakpoint before the given line. If line isomitted, set a breakpoint on the line about to be executed. If a condition is specified, it's evaluated each time the statement is reached: a breakpoint is taken only if the condition is true. Breakpoints may only be set on lines that begin an executable statement.
Debugger (6) • W expr • Add a global watch-expression.
Associative Arrays (Hashes) • Keys are strings • For each unique key there is one element (data) Array - $color[1]=“green”; Assoc. Array- $color{“green”}=1;
Associative Arrays (Hashes) (2) # list of pairs can be transformed into hash (and vice versa) % assray =(“one”, 1, “refresh”, “ctrl-alt-del”, “search”,”google”); #or % assray =(“one” => 1, “refresh” => “ctrl-alt-del”, “search” => ”google”); @list = %assray;
Associative Arrays (Hashes) (3) % assray =(“one” => 1, “refresh” => “ctrl-alt-del”, “search” => ”google”); @assray_keys = keys %assray; @assray_values = values %assray; #the keys and values are in corresponding order $keys_num = keys %assray; #returns 3 #remember – Perl is context dependent
Associative Arrays (Hashes) (4) % assray =(“one” => 1, “refresh” => “ctrl-alt-del”, “search” => ”google”); while ( ($key, $value) = each (%assray) ){ print “$key => $value \n”; } #access elements in sorted order foreach $key (sort keys %assray){ $value = $assray{$key}; }
Associative Arrays (5) # this sorts the %table hash by # value instead of key @sorted = sort { $table{$b} <=> $table{$a} } keys %table;
Associative Arrays (Hashes) (6) #Exists function if(exists $assray{“circle”}){ … } #delete function (according to key) delete $assray{“circle”};
Associative Arrays (7) defined $i; defined $hash{$key}; # is not the same as exists( $hash{$key} ) undef $bar{'blurfl'}; # Compare to: delete $bar{'blurfl'};
Nested Data Structures • Lists of Lists (arrays of arrays) • Hashes of Arrays • Arrays of Hashes • Hashes of Hashes
Lists of Lists my @lol; while( <> ){ @tmp= split; #split /\s/, $_; push @lol, [ @tmp ]; } print $lol[ $i ][ $j ]; push @lol, @tmp ;
Lists of Lists (2) my @lol=( [ “red”, “white”, “green” ], [ “perl”], [ “list”, “of”, “lists” ] ); print $lol[ 0 ][ 1 ]; print ${ $lol[ 0 ] }[ 1 ]; print $lol[ 0 ]->[ 1 ]; #but not print $lol->[ 0 ][ 1 ];
Lists of Lists (3) my @lol=( [ “red”, “white”, “green” ], [ “perl”], [ “list”, “of”, “lists” ]); print “@lol”; ARRAY(0x80d5010) ARRAY(0x80d5070) ARRAY(0x80d5094) foreach $list (@lol){ print "@$list \n"; } foreach (@lol){ print "@$_ \n"; }
Lists of Lists (4) my @lol=( [ “red”, “white”, “green” ], [ “perl”], [ “list”, “of”, “lists” ] ); my $rlol=[ [ “red”, “white”, “green” ], [ “perl”], [ “list”, “of”, “lists” ] ]; print “@lol”; ARRAY(0x80d5010) ARRAY(0x80d5070) ARRAY(0x80d5094) print $rlol; ARRAY(0x80d4eb4) $lol[0][1]; $rlol->[0][1]; $lol[0]->[1]; $rlol->[0]->[1];
Lists of Lists (5) foreach ( 0..9 ){ @array = split /\s/, <>; $lol[ $_ ]= @array; #wrong, @array size assignment } foreach ( 0..9 ){ @array = split /\s/, <>; $lol[ $_ ]= [ @array ]; } foreach ( 0..9 ){ @array = split /\s/, <>; $lol[ $_ ]= \@array; #wrong, assignment of reference to a local value }
Hashes of Arrays my %hoa=( color => [ “red”, “white”, “green” ], lang => [ “perl”], record => [ “list”, “of”, “lists” ] ); $hoa{ “another” } = [ @array ]; print $hoa{ “color” }[0];
Arrays of Hashes my @hoa=( { color => “red”, lang => “perl” }, { color => “green”, lang => “c++” } ); print $hoa[0]{ “color” }; $hoa[0] = { %hash };
Hashes of Hashes print $hoa{“record1”}{ “color” }; $hoa{“newrecord”} = { %hash }; my %hoa=( record1 => { color => “red”, lang => “perl” }, record2 => { color => “green”, lang => “c++” } );
HomeWork (a) Compute the percentage of nucleotides/amino-acids. Input: file(s) in the FASTA format. Output: percentage of sequence symbols. (b) Sort protein/dna sequences according to sequence length. Input: file(s) in the FASTA format. Output: The same sequences (with descriptors) ordered from the longest sequence to the shortest. [use ‘sort’ perl function]