in reply to Combining 3 files
First of all, please post your code together with your problem. The Monks usually help fixing problems, not write your scripts for you.
Depending on the amount of data, you may use a hash to preload the data:
for my $nr (1..2) { for my $line (read_file('file'.$nr)) { my @cols = split(/\t/,$line); push @{$data[$nr - 1]->{shift(@cols)}},\@cols; } }
This code reads both file1 and file2 (outer for loop) line by line (inner for loop), splits the lines into columns and stores the data in a tree referenced by file number and contig*-key (first column). It's using File::Slurp and I suggest that you look at the tree using Data::Dumper.
Next, compare it to your third file. Given you read and splitted the file already using a while (<$fh>) loop or using read_file:
# expecting file3 line in @col my @results = ($col[0],$col[3]); for my $dataset (@data) { push @results,(sort { my $diff_a = $col[2] - $a->[1]; $diff_a *= -1 if $diff_a < 0; my $diff_b = $col[2] - $b->[1]; $diff_b *= -1 if $diff_b < 0; $diff_a <=> $diff_b; } @{$dataset->{$col[0]}})[0]->[2]; }
This block sorts the preloaded data sets using the difference to the comparison value of the current row and adds the alpha-key to the @result list which has been preloaded with the key and the file3 alpha string.
You could easily print out the @result data tab-delimited using join().
This is no complete script, but the code samples should give you an idea how to handle your data, merging them is now easy.
If you got too much data to reasonable load it into memory, think about using a database (maybe SQLite) to handle the problem, it might be better than a pure perl solution.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Combining 3 files
by garyboyd (Acolyte) on Jun 23, 2011 at 14:52 UTC | |
|
Re^2: Combining 3 files
by garyboyd (Acolyte) on Jun 24, 2011 at 08:22 UTC | |
by Anonymous Monk on Jun 24, 2011 at 10:06 UTC | |
by garyboyd (Acolyte) on Jun 27, 2011 at 09:11 UTC | |
by Anonymous Monk on Jun 27, 2011 at 10:27 UTC | |
by garyboyd (Acolyte) on Jun 27, 2011 at 14:47 UTC | |
|