My most respected monks,
I am looking a way to find consensus string from given set of strings (length is assumed to be the same within the set). Basically consensus string mean single string that represent most common occurence from the given set. Here is the example of it:
Set 1: Set 2: AAAAA AAAAA ATCGA AACGA ATAAA ATAAA ATAAA Consensus: ATAAA Consensus: A[AT]AAA Note: like Set 2, there can be other cases that has more than two consensus in a particular position. They need to be captured within square bracket [].
Below is my incredibly naive, inefficient and inflexible code. There are two issues which I still unable to solve here:
So here I am again, humbly seeking for your wisdom.

My current code:
use strict; use warnings; my @instances = qw ( AAAAA ATCGA ATAAA ); my @instances2 = qw ( AAAAA AACGA ATAAA ATAAA ); print consensus(@instances),"\n"; sub consensus{ my @mi = @_; my $motif_count=0; my @words =(); my $L = undef; my @A = (); my @T = (); my @C = (); my @G = (); my $a = 0; my $c = 0; my $g = 0; my $t = 0; my $w = 0; foreach my $mi ( @mi ) { chomp($mi); $mi =~ s/\s//g; $w = length($mi); #for motif instances count @words = split( /\W+/, $mi ); $motif_count += @words; } for ( my $j = 0 ; $j < $w ; $j++ ) { # Initialize the base counts. my $L =0; foreach my $mi ( @mi ) { chomp($mi); my $L = $mi; my $sb = substr( $L, $j, 1 ); while ( $sb =~ /a/ig ) { $a++ } while ( $sb =~ /t/ig ) { $t++ } while ( $sb =~ /c/ig ) { $c++ } while ( $sb =~ /g/ig ) { $g++ } } push( @A, $a ); push( @T, $t ); push( @C, $c ); push( @G, $g ); $a = 0; $c = 0; $g = 0; $t = 0; } my @cons = (); #print "$w\n"; for (my $b=0; $b <$w ;$b++) { if ( $A[$b] > $T[$b] && $A[$b] > $C[$b] && $A[$b] > $G[$b] ) { push( @cons, 'A'); } elsif ( $T[$b] > $C[$b] && $T[$b] > $A[$b] && $T[$b] > $G[$b] ) { push( @cons, 'T' ); } elsif ( $C[$b] > $G[$b] && $C[$b] > $A[$b] && $C[$b] > $T[$b] ) { push( @cons, 'C' ); } elsif ( $G[$b] > $A[$b] && $G[$b] > $C[$b] && $G[$b] > $T[$b] ) { push( @cons, 'G' ); } elsif ( $A[$b] = $T[$b] ) { push( @cons, 'T' ); } elsif ( $A[$b] = $G[$b] ) { push( @cons, 'G' ); } elsif ( $A[$b] = $C[$b] ) { push( @cons, 'C' ); } elsif ( $T[$b] = $C[$b] ) { push( @cons, 'C' ); } elsif ( $T[$b] = $G[$b] ) { push( @cons, 'G' ); } else { push @cons, 'G'; } } return @cons; }

Regards,
Edward

In reply to Finding Consensus String by monkfan

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.