cathyyihao has asked for the wisdom of the Perl Monks concerning the following question:
So I'm working with a kmer project and I needed to generate a 4**21 by 197 matrix to record the information I need for each kmer since I'm doing 21mer right now. However, it shows out of memory when I try to generate the matrix. Is there any way I can get around with it?
Here is the script I wrote:
#!/usr/bin/perl use strict; use warnings; #initialize matrix my @matrix; my $i; #column my $j; #roll my @sum; for ($i=0; $i<197; $i++){ for ($j=0; $j<4**21; $j++){ $matrix[$i][$j]=0; if($i==0) { $sum[$j]=0; } } } #inport file names my $fn = $ARGV[0]; chomp $fn; unless (open(FILE, $fn)){ print "Can't open file \"$fn\"\n\n"; exit; } chomp(my @line = <FILE>); my $n; for ($n=0; $n<scalar @line; $n++){ #open each kmer file unless (open(KMER, $line[$n])){ print "Can't open file \"$line[$n]\"\n\n"; exit; } my @kmers = <KMER>; my @all_kmer; my $kmer; foreach $kmer (@kmers){ my @split = split(/\s+/,$kmer); #obtain kmer reference + number $matrix[$n][$split[0]] = 1; #print OUT "$matrix[$n][$split[0]]\t"; $sum[$split[0]]=$sum[$split[0]]+1; } close $line[$n]; } close $fn; #creat outfile my $outfile = $ARGV[1]; unless(open OUT, '>' .$outfile){ die "\nUnable to create $outfile\n"; } #sum up for ($j=0; $j<4**21; $j++){ print OUT "$sum[$j]\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Out of Memory when generating large matrix
by BrowserUk (Patriarch) on Mar 05, 2018 at 04:56 UTC | |
by cathyyihao (Novice) on Mar 05, 2018 at 16:27 UTC | |
by BrowserUk (Patriarch) on Mar 05, 2018 at 16:39 UTC | |
by hippo (Archbishop) on Mar 05, 2018 at 16:45 UTC | |
Re: Out of Memory when generating large matrix
by alexander_lunev (Pilgrim) on Mar 05, 2018 at 07:07 UTC | |
by cathyyihao (Novice) on Mar 05, 2018 at 16:59 UTC | |
Re: Out of Memory when generating large matrix
by AnomalousMonk (Archbishop) on Mar 05, 2018 at 04:10 UTC | |
Re: Out of Memory when generating large matrix
by thomas895 (Deacon) on Mar 06, 2018 at 05:38 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. | |
A reply falls below the community's threshold of quality. You may see it by logging in. |