diff options
author | Jesse Luehrs <doy@tozt.net> | 2012-10-20 15:00:56 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2012-10-20 15:00:56 -0500 |
commit | ff3a114540a01c0a8a3eafe50068006ac1ce1075 (patch) | |
tree | 370a97225e1566762a27f9353c5e0361eb197a93 | |
parent | 9aa75a97b9db70afaba2bc99bbb85ecb3d3c9ab2 (diff) | |
download | rosalind-ff3a114540a01c0a8a3eafe50068006ac1ce1075.tar.gz rosalind-ff3a114540a01c0a8a3eafe50068006ac1ce1075.zip |
another solution
-rw-r--r-- | CONS.pl | 44 |
1 files changed, 44 insertions, 0 deletions
@@ -0,0 +1,44 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use 5.016; + +use List::Util 'max'; +use List::MoreUtils 'firstidx'; + +my %map = ( + A => 0, + C => 1, + G => 2, + T => 3, +); +my %reverse_map = reverse %map; + +my @profile; + +my $length; +while (<>) { + chomp; + if (!$length) { + $length = length; + for my $base (0..3) { + $profile[$base] = [(0) x $length]; + } + } + my $i = 0; + for my $base (split '') { + $profile[$map{$base}][$i++]++; + } +} + +my $consensus = ''; +for my $index (0..$#{ $profile[0] }) { + my @vals = map { $profile[$_][$index] } 0..3; + my $max = max @vals; + $consensus .= $reverse_map{(firstidx { $_ == $max } @vals)}; +} + +say $consensus; +for my $base (0..3) { + say "$reverse_map{$base}: " . join ' ', @{ $profile[$base] }; +} |