summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-10-20 15:00:56 -0500
committerJesse Luehrs <doy@tozt.net>2012-10-20 15:00:56 -0500
commitff3a114540a01c0a8a3eafe50068006ac1ce1075 (patch)
tree370a97225e1566762a27f9353c5e0361eb197a93
parent9aa75a97b9db70afaba2bc99bbb85ecb3d3c9ab2 (diff)
downloadrosalind-ff3a114540a01c0a8a3eafe50068006ac1ce1075.tar.gz
rosalind-ff3a114540a01c0a8a3eafe50068006ac1ce1075.zip
another solution
-rw-r--r--CONS.pl44
1 files changed, 44 insertions, 0 deletions
diff --git a/CONS.pl b/CONS.pl
new file mode 100644
index 0000000..6e3200b
--- /dev/null
+++ b/CONS.pl
@@ -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] };
+}