Since you're dealing with frequency, I'd use a hash to store the occurrences of A, C, G, and T in each position of the word. I'd loop over the DNA sequences for each character, looking at character 1 first in all strands, then character 2, etc.
sub consensus {
my $len = length($_[0]); # all strands are of one length, right?
my @cons;
# loop over the positions in the strands
for my $i (0 .. $len - 1) {
my %freq;
my $max = 0;
# loop over character $i in each of the strands
for (map substr($_, $i, 1), @_) {
$freq{$_}++;
# save the max value
$max = $freq{$_} if $freq{$_} > $max;
}
# store only those who occur the most times
push @cons, [grep $freq{$_} == $max, keys %freq];
}
return \@cons;
}
Then it's your task to turn the array of array references into the string you want.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.