Commits

Sarah Richardson committed 7991e20

trying to speed up Codons.pm

  • Participants
  • Parent commits 2b5853b

Comments (0)

Files changed (1)

lib/Bio/GeneDesign/Codons.pm

 use Math::Combinatorics qw(combine);
 use List::Util qw(max first);
 use File::Basename;
+use autodie qw(open close);
 use Carp;
 
 use strict;
 sub _parse_codon_file
 {
   my ($path) = @_;
-  open (my $CFILE, "<", $path) || croak "Can't open $path : $!";
+  open (my $CFILE, '<', $path);
   my $ref = do { local $/ = <$CFILE> };
   close $CFILE;
   my @lines = split(/\n/x, $ref);
 sub _translate
 {
   my ($nucseq, $frame, $codon_t) = @_;
-  croak ("translate requires an unambiguous nucleotide sequence\n")
-    if ($nucseq =~ $ambnt);
   $nucseq = _complement($nucseq, 1) if ($frame < 0);
   my $peptide = q{};
-  for (my $offset = abs($frame)-1; $offset < length($nucseq); $offset += 3)
+  my $limit = length $nucseq;
+  my $offset = abs($frame) - 1;
+  $limit-- while (($limit - $offset) % 3 != 0);
+  while ($offset < $limit)
   {
-    my $codon = substr($nucseq, $offset, 3);
-    $peptide .= $codon_t->{$codon} if (exists $codon_t->{$codon});
+    my $codon = substr $nucseq, $offset, 3;
+    if (exists $codon_t->{$codon})
+    {
+      $peptide .= $codon_t->{$codon};
+    }
+    else
+    {
+      carp("GDWarning: $codon is untranslatable\n");
+    }
+    $offset += 3;
   }
   return $peptide;
 }