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

Hi monks! I am trying to count all the elements using hash but i get the error 'out of memory'. I have a fasta file as input, and i create an array with individual aminoacids as elements. Then i create an hash and count the elements of this array. But is there a more efficient way of doing this without much memory usage? The input file is 32Mb. Thanks
#!/usr/bin/perl use strict; use warnings; use diagnostics; open PROTEOME,'human_complete_proteome_without_isoforms.fasta'; my @proteome=<PROTEOME>; chomp @proteome; my @aminoacid; my @hold; my $peptide; foreach my $j (@proteome) { if($j!=/^>/) { push @hold,$j; } } print "The proteome is loaded\n"; $peptide= join ('', @hold); undef @hold; @aminoacid=split(//,$peptide); my %count; foreach (@aminoacid) { if(exists $count{$_}) { $count{$_}++; } else { $count{$_}=1; } } print "hash has finished searching the document\n"; foreach (keys %count) { print "$_ \t occurs $count{$_} times \n"; }

Replies are listed 'Best First'.
Re: Count the occurrence of an element in an array
by hdb (Monsignor) on Feb 20, 2014 at 07:10 UTC

    I would not think that a 32MB file should be a problem unless you have a tiny computer. But you could count the aminoacids while reading the file:

    #!/usr/bin/perl use strict; use warnings; use diagnostics; my %count; open PROTEOME, '<', 'human_complete_proteome_without_isoforms.fasta'; while(<PROTEOME>){ next if /^>/; chomp; $count{$_}++ for split //; } print "hash has finished searching the document\n"; foreach (keys %count) { print "$_ \t occurs $count{$_} times \n"; }
Re: Count the occurrence of an element in an array
by davido (Cardinal) on Feb 20, 2014 at 07:30 UTC

    There are two big issues, and one bug. The first issue is that you're slurping the entire file into memory at once. The second issue is that you're splitting the resultant string into individual single-character array elements. Perl's array elements internally consume more than the size of the primary data, so what might have been a 32MB string might become a 320MB array (just as a shooting from the hip example).

    The bug is this line: if($j!=/^>/).... You probably intended to use the !~ operator instead.

    What follows is a solution that avoids holding more than a single copy of the data set, and avoids splitting it out into a big array. ...it is, however, untested since I don't have your data set handy. ;)

    use strict; use warnings; use diagnostics; open PROTEOME, 'human_complete_proteome_without_isoforms.fasta'; my $peptide; while( <PROTEOME> ) { chomp; $peptide .= $_ unless /^>/; } print "The proteome is loaded\n"; my %count; my $aminoacid_ix = 0; for( my $aminoacid_ix = 0; $aminoacid_ix < length $peptide; ++$aminoac +id_ix ) { my $aminoacid = substr $peptide, $aminoacid_ix, 1; $count{$aminoacid}++; } print "Finished searching the document\n"; foreach my $aminoacid ( keys %count ) { print "$aminoacid\t occurs $count{$aminoacid} times \n"; }

    There are probably additional opportunities to improve on memory efficiency if I knew more about the data. In particular, it's probably safe to do all the reduction work in the same loop that reads the file (and that's exactly what hdb did in his solution), but I am hesitant to incorporate that into my solution without seeing a small representative sample of the data (even though hdb's solution probably works just fine).

    Updated: Improved efficiency of file reading loop.


    Dave

Re: Count the occurrence of an element in an array
by Laurent_R (Canon) on Feb 20, 2014 at 07:11 UTC
    You don't need to load your whole file into memory, especially not twice (if I understand correctly your code). Simply read your file line by line, count your aninoacids on the current line and then go to the next line and continue the coun ting. This way you'll have no memory problem counting elements even in GB files. One small additional point:
    if($j!=/^>/)
    I am not exactly sure what you intend there, but it probably does not do what you want. Also:
    if(exists $count{$_}) { $count{$_}++; } else { $count{$_}=1; }
    can be replaced simply by:
    $count{$_}++;
Re: Count the occurrence of an element in an array
by Discipulus (Canon) on Feb 20, 2014 at 07:54 UTC
    only few words to add: i think all bioinformatic (due to the biggness of their data) need to learn a new word/concept: Iterator.
    High Order Perl has a wonderful chapter about and in CPAN there is an implementation module: Iterator.

    hth
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.