Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, a small question have made me confuse.i want the result of dividing in the last part of script but i dont have it.please bring me out from this hell.thanks.

#!/usr/bin/perl -w use strict; use Bio::SeqIO; my $filename; print "enter file name="; $filename=<STDIN>; chomp $filename; open(RESULT,'>>result')or die "there isnt this file!"; #protein file bringing in. my $proteinio=Bio::SeqIO->new (-file=>"$filename",-format=>'fast +a'); while( my $seq=$proteinio ->next_seq() ){ foreach(my $protein=$seq->seq){ + my ($count_of_A,$count_of_C,$count_of_D,$count_of_E,$count_of_F,$count_of +_G,$count_of_H,$count_of_I,$count_of_K,$count_of_L,$co unt_of_M,$count_of_N,$count_of_P,$count_of_Q,$count_of_R,$count_of_S,$ +count_of_T,$count_of_V,$count_of_W,$count_of_Y)=0; my $id=$seq->id; # amino acid counting per sequence. my @prot=split("",$protein); my $tt=scalar (@prot); foreach my $aa(@prot){ if ( $aa=~ /A/ ){ $count_of_A++;} elsif ($aa=~/C/ig){ $count_of_C++;} elsif ($aa=~/D/ig){ $count_of_D++;} elsif ($aa=~/E/ig){ $count_of_E++;} elsif ($aa=~/F/ig){ $count_of_F++;} elsif ($aa=~/G/ig){ $count_of_G++;} elsif ($aa=~/H/ig){ $count_of_H++;} elsif ($aa=~/I/ig){ $count_of_I++;} elsif ($aa=~/K/ig){ $count_of_K++;} elsif ($aa=~/L/ig){ $count_of_L++;} elsif ($aa=~/M/ig){ $count_of_M++;} elsif ($aa=~/N/ig){ $count_of_N++;} elsif ($aa=~/P/ig){ $count_of_P++;} elsif ($aa=~/Q/ig){ $count_of_Q++;} elsif ($aa=~/R/ig){ $count_of_R++;} elsif ($aa=~/S/ig){ $count_of_S++;} elsif ($aa=~/T/ig){ $count_of_T++;} elsif ($aa=~/V/ig){ $count_of_V++;} elsif ($aa=~/W/ig){ $count_of_W++;} elsif ($aa=~/Y/ig){ $count_of_Y++;} } print RESULT "$id $count_of_A/$tt $count_of_C/$tt $count_of_D/$tt $co +unt_of_E/$tt $count_of_F/$tt $count_of_G/$tt $count_of_H/$tt $count_of_I/$tt $count_of_K/$tt $count_of_L/$tt $count +_of_M/$tt $count_of_N/$tt $count_of_P/$tt $count_of_Q/$tt $count_of_R/$tt $count_of_S/$tt $count_of_T/$tt $count +_of_V/$tt $count_of_W/$tt $count_of_Y/$tt \n"; } } close RESULT;

Replies are listed 'Best First'.
Re: dividing numbers
by choroba (Cardinal) on Sep 20, 2011 at 16:38 UTC
    You can replace the whole if-elsif paragraph with just
    my %count_of; if (/([ACDEFGHIKLMNPQRSTVWY])/) { $count_of{$1}++ }
    As for the result, do not use double quotes around arithmetical operations. With the hash suggested above you can just:
    print RESULT join ' ', $id, map $count_of{$_}/$tt, qw/A C D E F G H I K L M N P Q R S T V W Y/ +; print "\n";
Re: dividing numbers
by derby (Abbot) on Sep 20, 2011 at 16:35 UTC

    Well ... other issues aside ... you need to pull the arithmetic out of the string:

    print RESULT "$id ", $count_of_A/$tt, " ", $count_of_C/$tt, " ", ....
    -derby
Re: dividing numbers
by Not_a_Number (Prior) on Sep 20, 2011 at 18:49 UTC

    Well, just to show the Power of Perl, you could replace the entire contents of your foreach(my $protein=$seq->seq){ loop with:

    my %count; $count{$_}++ for map uc, split '', $protein; for ( 'A' .. 'Z' ) { say RESULT "$_: ", $count{$_} / length $protein if $count{$_}; }