in reply to Re: Finding Consensus String
in thread Finding Consensus String

Dear japhy,
Thanks so much for your answer to my posting. I just have a question. Your code works well in generalize version of array like this:
my @instances = ( 'AAAAA ATCGA', 'ATCGA AAAAA', 'ATAAA AAAAA', );
But how can I make it more generalize for cases like this?
my @instances2 = ( 'AAAAA ATCGA', 'ATCGA', 'ATAAA AAAAA', ); # consensus : ATAAA A[AT][CA][GA]A
Or this:
my @instances3 = ( 'AAAAA ATCGA TTTT', 'ATCGA TATA ', 'ATAAA AAAAA ATTA ', ); # consensus : ATAAA A[AT][CA][GA]A TTTA
Cause, currently it gives warning when encountering two cases like that.

Regards,
Edward

Replies are listed 'Best First'.
Re^3: Finding Consensus String
by japhy (Canon) on Oct 19, 2005 at 19:18 UTC
    I don't get any warnings. What I do notice is that space is included in the [...] parts. So here's a small fix:
    sub consensus { ... # code from before, until: # store only those who occur the most times my @most = grep $freq{$_} == $max, keys %freq; @most = grep $_ ne " ", @most if @most > 1; push @cons, \@most; } return \@cons; }
    And to get the string version, I use:
    sub consensus_to_string { my $con = shift; my $str; for (@$con) { if (@$_ > 1) { $str .= "[" . join("", @$_) . "]" } else { $str .= $_->[0] } } return $str; }

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart