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"; }
In reply to Out of Memory when generating large matrix by cathyyihao
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |