<?xml version='1.0'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
	<title><![CDATA[BOL: Perl script to create a consensus of nucleotide sequences !]]></title>
	<link>https://bioinformaticsonline.com/snippets/view/37924/perl-script-to-create-a-consensus-of-nucleotide-sequences?</link>
	<atom:link href="https://bioinformaticsonline.com/snippets/view/37924/perl-script-to-create-a-consensus-of-nucleotide-sequences?" rel="self" type="application/rss+xml" />
	<description><![CDATA[]]></description>
	
	<item>
	<guid isPermaLink="true">https://bioinformaticsonline.com/snippets/view/37924/perl-script-to-create-a-consensus-of-nucleotide-sequences</guid>
	<pubDate>Fri, 12 Oct 2018 10:01:22 -0500</pubDate>
	<link>https://bioinformaticsonline.com/snippets/view/37924/perl-script-to-create-a-consensus-of-nucleotide-sequences</link>
	<title><![CDATA[Perl script to create a consensus of nucleotide sequences !]]></title>
	<description><![CDATA[<code>use strict;
use warnings;

my @instances  = qw ( AAAAA ATCGA ATAAA );
my @instances2 = qw ( AAAAA AACGA ATAAA AGAAA AGAAA);

print consensus(@instances),&quot;\n&quot;;        # ATAAA
print consensus(@instances2),&quot;\n&quot;;       # ATAAA
exit;

sub consensus{
 my @mi = @_;
 chomp(@mi);
 my $motif_count=0;
 my @words =();

  my %H = ( A=&gt;[], T=&gt;[], C=&gt;[], G=&gt;[] );

  s/\s//g for @mi;
  my ($w) = sort {$b &lt;=&gt; $a} map {length} @mi;    # set w to the length of the longest element

    foreach my $j ( 0 .. $w-1 ){
        # Initialize the base counts.
        my %h = ( a=&gt;0, t=&gt;0, c=&gt;0, g=&gt;0 );
        my @mi_letters = map { [split &#039;&#039;, uc $_] } @mi;
  	foreach my $j ( 0 .. $w-1 ){
    		$H{ $_-&gt;[$j] }-&gt;[$j]++ for @mi_letters;
  	}
        push @{$H{ uc $_ }}, $h{$_} for keys %h;   # example:  push @{$H{G}}, $g;
    }

    my @cons = ();
    my %prefOrder = ( A=&gt;1, T=&gt;2, C=&gt;3, G=&gt;4 );
    foreach my $B ( 0 .. $w-1 ){
      push @cons, [ sort { ($H{$b}-&gt;[$B]||0) &lt;=&gt; ($H{$a}-&gt;[$B]||0) || $prefOrder{$b} &lt;=&gt; $prefOrder{$a} } qw/A T G C/ ]-&gt;[0];
    }

    return @cons;
}

#reference https://www.perlmonks.org/bare/?node_id=500962</code>]]></description>
	<dc:creator>Neel</dc:creator>
</item>

</channel>
</rss>