Here is a possible solution:
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; }
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.