It first does some pre-processing on the second file, so you only have to read it once and not once for every line in the first file. It does so by setting up an Array of Hashes. The keys of the inner hashes are the various combinations of the minor and major alleles and the values are -1, 0, 1 or 2 according to the information you gave. It directly maps the combinations of minor/major to its values so you do not have to run a number of if tests later.use Modern::Perl; # Process the second file only once my @alleles; { open my $secondfile, '<', './secondfile.txt' or die "Could not ope +n second file. $!"; while (<$secondfile>) { chomp; my ($minor, $major) = split; push @alleles, { "$minor$minor" => 0, "$minor$major" => 1, "$major$minor" => 1, "$major$major" => 2, '00' => -1, } } } #Process the first file open my $firstfile, '<', './firstfile.txt' or die "Could not open firs +t file. $!"; while (<$firstfile>) { chomp; say "Working on $_"; my @snps = split; my $index; my @results; while (@snps) { my $marker = shift(@snps) . shift(@snps); push @results, ($alleles[$index])->{$marker}; $index++; } say join ',', @results; }
By the way, when running this script on the data you provided it appears that the data for the second individual is quite wrong: it cannot have these alleles at these places, since they do not match what is in the second file.
Finally, writing the results to a file is left as an exercise for the readers.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
In reply to Re: Gurus, please point me in the right direction; complicated operations desired for DNA sequence formating
by CountZero
in thread Gurus, please point me in the right direction; complicated operations desired for DNA sequence formating
by Renyulb28
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |