Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
P is for Practical
 
PerlMonks  

Re: How to deal with Huge data

by Thelonius (Curate)
on Jan 23, 2007 at 13:56 UTC ( #596093=note: print w/ replies, xml ) Need Help??


in reply to How to deal with Huge data

The key thing to do is to have your data sorted by your key (in this case the first column of your data). Then you only need to keep a small amount of data in memory.

By the way, in the case where the column headers of your input files are not all different (except for the first), your program's output depends on the order the files are named in the command line.

Here's one way to process the data. I haven't tested this at all except to check that it passes perl -c.

#!/usr/bin/perl -w use strict; use warnings; my @fileHeaders; my %usedHeaders; my $sortname = "sortfile.tmp"; # If you have more than one disk, consider putting # a disk different from most of your data. # Beware of disk space. open SORTFILE, ">", $sortname or die "Cannot create $sortname: $!\n"; # first pass, write to sortfile foreach my $filename (@ARGV) { open GSE, "<", $filename or die "Cannot open $filename: $!\n"; my $headerline = <GSE>; chomp($headerline); $headerline =~ s/\r$//; my @thisfileHeaders = split /\t/, $headerline; shift @thisfileHeaders; push @fileHeaders, \@thisfileHeaders; my $filekey = sprintf "%04d", $#fileHeaders; while(<GSE>) { y/\r//d; s/^([^\t]*)/$1\t$filekey/; print SORTFILE $_ or die "print SORTFILE failed: $!\n"; } if ($. > 1) { # if any data past headers $usedHeaders{$_}++ for @thisfileHeaders; } close GSE; } close SORTFILE or die "close SORTFILE failed: $!\n"; # Yes, close can fail. Buffered I/O, for one thing system("sort -o sortfile.tmp sortfile.tmp") == 0 or die "Sort failed!\n"; # second pass; consolidate data from multiple files my $prevkey; open SORTFILE, "<", $sortname or die "Cannot open $sortname: $!\n"; my @samples = sort keys %usedHeaders; print "Probe\t".join("\t",@samples)."\n"; my %data; while (my $ligne = <SORTFILE>) { chomp($ligne); my @t = split(/\t/, $ligne); my $probe = shift @t; my $filenum = shift @t; if (!$prevkey || $probe ne $prevkey) { dumpdata(); $prevkey = $probe; } @data{@{$fileHeaders[$filenum]}} = @t; } dumpdata(); sub dumpdata { if (defined($prevkey)) { print "$prevkey\t", join("\t", @data{@samples}), "\n"; } $data{$_} = "" for @samples; #initialize to blanks so no warnings on printing undef }
Update: small edits.


Comment on Re: How to deal with Huge data
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://596093]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-04-24 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls