http://www.perlmonks.org?node_id=1210340


in reply to Out of Memory when generating large matrix

Hello!

It's hard to see whole picture without sample of data, but i will try.

As far as i can see, you have files with data something like

1 the only 2 significant 3 part 4 is 5 a 6 number 7 before 8 the 9 first 10 space 11 character

Then you pick the leading number in every string and use it as an index of array @sum to sum the number of occurrences of this number. I don't see why you even need the @matrix array at this point, maybe you will use it later. Will you really need 4**21 size of an array? Will there be 4**21 different numbers in those files? And if not, if your memory actually can contain all numbers in your files (but not the range of these numbers), then maybe you should use a hash instead of an array? Something like this:

#!/usr/bin/perl use strict; use warnings; my %matrix; my %sum; #inport file names my $fn = $ARGV[0]; chomp $fn; open (my $FILE, $fn) or die "Can't open file \"$fn\"\n\n"; chomp(my @line = <$FILE>); my $n; for ($n=0; $n<scalar @line; $n++){ #open each kmer file open(my $KMER, $line[$n]) or die "Can't open file \"$line[$n]\"\n\ +n"; my @kmers = <$KMER>; my @all_kmer; my $kmer; foreach $kmer (@kmers){ my @split = split(/\s+/,$kmer); #obtain kmer reference number # for now we don't have to populate the big %matrix, commented + # $matrix{$n}{split[0]} = 1; $sum{$split[0]}++; } close $KMER; } close $FILE; #creat outfile my $outfile = $ARGV[1]; open (my $OUT, '>' .$outfile) or die "\nUnable to create $outfile\n"; #sum up foreach my $k (keys %sum){ print $OUT "$sum{$k}\n"; }

Still, it's hard to tell will it suffice without the data. And if not, and count of numbers will not fit in your RAM, then you will have to use the interim DB, like MySQL or similar. I will use MySQL queries. First, create DB and table in it:

CREATE DATABASE kmers; CREATE TABLE matrix (file int, number int, key (file), key (number));

Then you will have to populate matrix table with SQL query like this:

use DBI; my $dbh = DBI->new('DBI:mysql:database=kmers;host=127.0.0.1','mysql_us +er',''); my $query = "INSERT INTO matrix VALUES (?,?)"; my $sth = $dbh->prepare($query); for ($n=0; $n<scalar @line; $n++){ #open each kmer file open(my $KMER, $line[$n]) or die "Can't open file \"$line[$n]\"\n\ +n"; my @kmers = <$KMER>; my @all_kmer; my $kmer; foreach $kmer (@kmers){ my @split = split(/\s+/,$kmer); #obtain kmer reference number # for now we don't have to populate the big %matrix, commented + # $matrix{$n}{split[0]} = 1; # $sum{$split[0]}++; $sth->execute($n,$split[0]); } close $KMER; } $sth->finish();

And after matrix table populated you can query sum from it

my $count_query = "SELECT number,COUNT(number) FROM matrix GROUP BY nu +mber ORDER BY number"; $sth = $dbh->prepare($count_query);

But still, even with interim DB, if the count of numbers is greater than your memory, you will have to fetch and print result to file by one row at a time:

#sum up #foreach my $k (keys %sum){ # print $OUT "$sum{$k}\n"; #} $sth->execute(); while (my $res = $sth->fetchrow_arrayref()) { print $OUT $res->[1]\n"; }

Replies are listed 'Best First'.
Re^2: Out of Memory when generating large matrix
by cathyyihao (Novice) on Mar 05, 2018 at 16:59 UTC

    Hi! the hash actually resolves my problem!! I was thinking too complicated but what I actually needed is just the reference number and how many time it has appeared!! Thank you so much!