Skip to content

Instantly share code, notes, and snippets.

@ivan-krukov
Created May 15, 2013 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivan-krukov/5587337 to your computer and use it in GitHub Desktop.
Save ivan-krukov/5587337 to your computer and use it in GitHub Desktop.
Solution to Rosalind CORR problem
#!/usr/bin/env perl
use v5.14;
use warnings;
sub hamming {
my @a = split "", shift;
my @b = split "", shift;
my $distance = 0;
for (my $n = 0; $n < scalar(@a); $n++) {
unless (($a[$n]) eq ($b[$n])) {
$distance++;
}
}
return $distance;
}
sub revcomp {
my $rc = reverse(shift);
$rc =~ tr/ACTG/TGAC/;
return $rc;
}
my %bins = ();
while (<>) {
unless (/>.*/) {
chomp;
my $seq = $_;
if (exists $bins{revcomp($seq)}) {
$bins{revcomp($seq)} ++;
} else {
$bins{$seq} ++;
}
}
}
my (@correct, @ambiguous) = (),();
while (my ($key,$value)= each(%bins)) {
if ($value >= 2){
push @correct, $key;
}else {
push @ambiguous, $key;
}
}
my %corrected_reads = ();
for my $error (@ambiguous) {
for my $seq (@correct) {
if (hamming($seq,$error) == 1) {
$corrected_reads{$error} = $seq;
say "$error->$seq";
last;
}
if (hamming(revcomp($seq),$error) == 1) {
my $rc = revcomp($seq);
$corrected_reads{$error}=$rc;
say "$error->$rc";
last;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment