Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: how to read input from a file, one section at a time?

by davi54 (Sexton)
on Feb 21, 2019 at 23:38 UTC ( #1230341=note: print w/replies, xml ) Need Help??


in reply to Re: how to read input from a file, one section at a time?
in thread how to read input from a file, one section at a time?

Hi, thank you so much. It works.

I have a following question. Once I have the output in the output file, I want to read the number of alphabets present in the output file for each sequence.

Example:

for the output: "A=27 C=3 D=16 E=14 F=1 G=11 H=4 I=12 K=10 L=13 M=2 N=6 P=10 Q=6 R=6 S=14 T=20 V=11 W=4 Y=10"

I want it to count the alphabets in each result and return how many alphabets are present. In the above example, 20 alphabets are present. So I want my final output to look something like: number of alphabets = 20. How can I do it?
  • Comment on Re^2: how to read input from a file, one section at a time?

Replies are listed 'Best First'.
Re^3: how to read input from a file, one section at a time?
by Cristoforo (Curate) on Feb 22, 2019 at 00:48 UTC
    The line below will give you those counts.

    print $out_file "Number of proteins = ", scalar keys %prot, "\n";

      Hi, Thank you. It works perfectly.

      One last thing I would like my script to do is tho look at those "number of proteins" and tell me which entry in the original input file has the smallest number of proteins. So, it should look like following in the output:

      A=11 D=5 E=12 F=1 G=5 I=6 K=3 L=7 M=2 N=4 P=2 Q=9 R=10 T=4 V=10

      Number of proteins = 15

      Entry ">sp|Q2M7X4|YICS_ECOLI Uncharacterized protein YicS OS=Escherichia coli (strain K12) OX=83333 GN=yicS PE=4 SV=1" has the least number of proteins

        #!/usr/bin/perl use strict; use warnings; my $report_name = 'aa_report.txt'; open my $out_file, '>', $report_name or die "Cannot open '$report_name' because: $!"; print 'PLEASE ENTER THE FILENAME OF THE PROTEIN SEQUENCE: '; chomp( my $prot_filename = <STDIN> ); open my $PROTFILE, '<', $prot_filename or die "Cannot open '$prot_filename' because: $!"; $/ = ''; # Set paragraph mode my @count=(); my $name; while ( my $para = <$PROTFILE> ) { # Remove fasta header line if ( $para =~ s/^>(.*)//m ){ $name = $1; }; # Remove comment line(s) $para =~ s/^\s*#.*//mg; my %prot; $para =~ s/([A-Z])/ ++$prot{ $1 } /eg; my $num = scalar keys %prot; push @count,[$num,$name]; printf "Counted %d for %s ..\n",$num,substr($name,0,50); print $out_file "$name\n"; print $out_file join( ' ', map "$_=$prot{$_}", sort keys %prot ), +"\n"; printf $out_file "Number of proteins = %d\n\n",$num ; } # sort names by count in ascending order to get lowest my @sorted = sort { $a->[0] <=> $b->[0] } @count; my $lowest = $sorted[0]->[0]; # maybe more than 1 lowest printf $out_file "Least number of proteins is %d in these entries\n",$ +lowest; my @lowest = grep { $_->[0] == $lowest } @sorted; print $out_file "$_->[1]\n" for @lowest; # show all results print $out_file "\nAll results in ascending count\n"; for (@sorted){ printf $out_file "%d %s\n",@$_; }; close $out_file; print "Results in $report_name\n"
        poj
Re^3: how to read input from a file, one section at a time?
by BillKSmith (Prior) on Feb 22, 2019 at 03:06 UTC
    >type davi54.pl use strict; use warnings; my $string = "A=27 C=3 D=16 E=14 " . "F=1 G=11 H=4 I=12 " . "K=10 L=13 M=2 N=6 " . "P=10 Q=6 R=6 S=14 " . "T=20 V=11 W=4 Y=10" ; print $string =~ tr/A-Z/A-Z/; >perl davi54.pl 20
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1230341]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2020-10-22 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (230 votes). Check out past polls.

    Notices?