Skip to content

Commit

Permalink
get gap translation working
Browse files Browse the repository at this point in the history
  • Loading branch information
cjfields committed Sep 2, 2014
1 parent 485c1d2 commit 837b7ba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
21 changes: 8 additions & 13 deletions lib/Bio/Tools/CodonTable.pm6
Expand Up @@ -129,37 +129,32 @@ method tables() {
method translate($seq is copy,
:$terminator? is copy,
:$unknown is copy) {
# my ($self, $seq) = @_;
# $self->throw("Calling translate without a seq argument!") unless defined $seq;
return '' unless $seq;

my $tbl = @!TABLES[self.id - 1];

# my $dna = "ttaagg"; sub translate($dna) { "FFLLSSSSYY!!CC!WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG".comb[map { :4($_) }, $dna.trans("tcag" => "0123").comb(/.../)] }; say translate($dna)

my $protein = '';

# grab each non-gapped 3-mer
# TODO: should we deal with gaps?
if $seq ~~ /<-[actugACTUG]>/ { #ambiguous chars
for $seq.comb( /<-[-]>**3/) -> $codon {
if ($codon ~~ /<-[ATUGCatugc]>/) {
if $seq ~~ /^^<[actugACTUG]>$$/ {
$protein = $tbl.comb[ map { :4($_) }, $seq.trans('TUCAGtucag' => '0012300123').comb( /.**3/ ) ].join('');
} else { #ambiguous chars, gaps
for $seq.comb( /.**3/) -> $codon {
if $codon eq $.CODONGAP {
$protein ~= '-';
} elsif $codon ~~ /<-[ATUGCatugc]>/ {
# TODO: rewrite this to be more consistent
$protein ~= self!translate_ambiguous_codon($codon);
} else {
$protein ~= $tbl.substr( unbase( 4, $codon.trans('TUCAGtucag' => '0012300123') ), 1 );
}
}
} else {
$protein = $tbl.comb[ map { :4($_) }, $seq.trans('TUCAGtucag' => '0012300123').comb( /<-[-]>**3/ ) ].join('');
}

# any leftover? TODO: this doesn't account for possible gaps
my $partial = $seq.chars % CODONSIZE;

if $partial == 2 {
my $codon = $seq.substr(*-2, 2).lc ~ 'n';
$protein ~= self!translate_ambiguous_codon($codon);
$protein ~= self!translate_ambiguous_codon( $seq.substr(*-2, 2).lc ~ 'n' );
}

return $protein;
Expand Down
2 changes: 1 addition & 1 deletion t/Tools/CodonTable.t
Expand Up @@ -44,7 +44,7 @@ my @ii = <ACT acu ATN gt ytr sar>;
my @res = <T T X V L Z >;
my $test = 1;
for @ii Z @res -> $dna,$aa {
is($myCodonTable.translate($dna), $aa, $dna ~ ":" ~ $aa);
is($myCodonTable.translate($dna), $aa, "$dna: $aa");
}
ok ($test);
is $myCodonTable.translate('ag'), '';
Expand Down

0 comments on commit 837b7ba

Please sign in to comment.