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"; }

In reply to Re: Out of Memory when generating large matrix by alexander_lunev
in thread Out of Memory when generating large matrix by cathyyihao

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.