Skip to content

Commit

Permalink
Bio::Tools::CodonTable::_codon_is: simplify with use all()
Browse files Browse the repository at this point in the history
Using List::Util::all makes code clearer.  But we now return ''
instead of 0 for false.  Tests are adjusted to check for truthness has
documented.
  • Loading branch information
carandraug committed Apr 26, 2024
1 parent 17f4c3b commit fa9366f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 45 deletions.
18 changes: 6 additions & 12 deletions lib/Bio/Tools/CodonTable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use utf8;
use strict;
use warnings;

use List::Util;

use Bio::Tools::IUPAC;
use Bio::SeqUtils;

Expand Down Expand Up @@ -687,24 +689,16 @@ sub is_ter_codon{
# position corresponding to $value. In the case of ambiguous
# codons, only returns true if all possibilities match $key.
sub _codon_is {
my ($self, $value, $table, $key ) = @_;
my ($self, $value, $tables, $key ) = @_;

return 0 unless length $value == 3;

$value = lc $value;
$value =~ tr/u/t/;

my $id = $self->{'id'};
my $result = 0;
for my $c ( $self->unambiguous_codons($value) ) {
my $m = substr( $table->[$id], $CODONS->{$c}, 1 );
if ($m eq $key) {
$result = 1;
} else {
return 0;
}
}
return $result;
my $table = $tables->[$self->{'id'}];
return List::Util::all {substr($table, $CODONS->{$_}, 1) eq $key}
$self->unambiguous_codons($value);
}

=head2 is_unknown_codon
Expand Down
65 changes: 32 additions & 33 deletions t/SeqTools/CodonTable.t
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,29 @@ ok $test;
# boolean tests
$myCodonTable->id(1); # Standard table

ok $myCodonTable->is_start_codon('ATG'), 'is_start_codon, ATG';
is $myCodonTable->is_start_codon('GGH'), 0, 'is_start_codon, GGH';
ok $myCodonTable->is_start_codon('HTG'), 'is_start_codon, HTG';
is $myCodonTable->is_start_codon('CCC'), 0, 'is_start_codon, CCC';
ok( $myCodonTable->is_start_codon('ATG'), 'is_start_codon, ATG');
ok( ! $myCodonTable->is_start_codon('GGH'), 'is_start_codon, GGH');
ok( $myCodonTable->is_start_codon('HTG'), 'is_start_codon, HTG');
ok( ! $myCodonTable->is_start_codon('CCC'), 'is_start_codon, CCC');

ok $myCodonTable->is_ter_codon('UAG'), 'is_ter_codon, U should map to T, UAG';
ok $myCodonTable->is_ter_codon('TaG'), 'is_ter_codon,TaG';
ok $myCodonTable->is_ter_codon('TaR'), 'is_ter_codon,TaR';
ok $myCodonTable->is_ter_codon('tRa'), 'is_ter_codon,tRa';
is $myCodonTable->is_ter_codon('ttA'), 0, 'is_ter_codon,ttA';
ok( $myCodonTable->is_ter_codon('UAG'), 'is_ter_codon, U should map to T, UAG');
ok( $myCodonTable->is_ter_codon('TaG'), 'is_ter_codon,TaG');
ok( $myCodonTable->is_ter_codon('TaR'), 'is_ter_codon,TaR');
ok( $myCodonTable->is_ter_codon('tRa'), 'is_ter_codon,tRa');
ok( ! $myCodonTable->is_ter_codon('ttA'), 'is_ter_codon,ttA');

# Ambiguous codons should fail
is $myCodonTable->is_ter_codon('NNN'), 0, 'is_ter_codon, ambiguous codons should fail, NNN';
is $myCodonTable->is_ter_codon('TAN'), 0, 'is_ter_codon, ambiguous codons should fail, TAN';
is $myCodonTable->is_ter_codon('CC'), 0, 'is_ter_codon, incomplete codons should fail, CC';
ok ! $myCodonTable->is_ter_codon('NNN'), 'is_ter_codon, ambiguous codons should fail, NNN';
ok ! $myCodonTable->is_ter_codon('TAN'), 'is_ter_codon, ambiguous codons should fail, TAN';
ok ! $myCodonTable->is_ter_codon('CC'), 'is_ter_codon, incomplete codons should fail, CC';

is $myCodonTable->is_start_codon('NNN'), 0, 'is_start_codon, ambiguous codons should fail, NNN';
is $myCodonTable->is_start_codon('NTG'), 0, 'is_start_codon, ambiguous codons should fail, NTG';
is $myCodonTable->is_start_codon('N'), 0, 'is_start_codon, incomplete codons should fail, NN';
ok ! $myCodonTable->is_start_codon('NNN'), 'is_start_codon, ambiguous codons should fail, NNN';
ok ! $myCodonTable->is_start_codon('NTG'), 'is_start_codon, ambiguous codons should fail, NTG';
ok ! $myCodonTable->is_start_codon('N'), 'is_start_codon, incomplete codons should fail, NN';

ok $myCodonTable->is_unknown_codon('jAG');
ok $myCodonTable->is_unknown_codon('jg');
is $myCodonTable->is_unknown_codon('UAG'), 0;
ok ! $myCodonTable->is_unknown_codon('UAG');

is $myCodonTable->translate_strict('ATG'), 'M';

Expand Down Expand Up @@ -245,31 +245,30 @@ is $myCodonTable->reverse_translate_all($seq, $cut, 15), 'GCNTGYGAYGARTTYGGVCAYA
$myCodonTable = Bio::Tools::CodonTable->new(); # Default Standard table

# boolean tests
is $myCodonTable->is_start_codon('ATG'), 1;
is $myCodonTable->is_start_codon('GTG'), 0;
is $myCodonTable->is_start_codon('TTG'), 1;
is $myCodonTable->is_start_codon('CTG'), 1;
is $myCodonTable->is_start_codon('CCC'), 0;
ok( $myCodonTable->is_start_codon('ATG'));
ok( ! $myCodonTable->is_start_codon('GTG'));
ok( $myCodonTable->is_start_codon('TTG'));
ok( $myCodonTable->is_start_codon('CTG'));
ok( ! $myCodonTable->is_start_codon('CCC'));

$myCodonTable->id(0); # Special 'Strict' table (ATG-only start)

is $myCodonTable->is_start_codon('ATG'), 1;
is $myCodonTable->is_start_codon('GTG'), 0;
is $myCodonTable->is_start_codon('TTG'), 0;
is $myCodonTable->is_start_codon('CTG'), 0;
is $myCodonTable->is_start_codon('CCC'), 0;
ok( $myCodonTable->is_start_codon('ATG'));
ok( ! $myCodonTable->is_start_codon('GTG'));
ok( ! $myCodonTable->is_start_codon('TTG'));
ok( ! $myCodonTable->is_start_codon('CTG'));
ok( ! $myCodonTable->is_start_codon('CCC'));

# Pterobranchia Mitochondrial codon table
$myCodonTable->id(24);
is $myCodonTable->is_start_codon('GTG'), 1;
is $myCodonTable->is_start_codon('CTG'), 1;
is $myCodonTable->translate_strict('TGA'), 'W';
ok( $myCodonTable->is_start_codon('GTG'));
ok( $myCodonTable->is_start_codon('CTG'));
is( $myCodonTable->translate_strict('TGA'), 'W');

# Candidate Division SR1 and Gracilibacteria codon table
$myCodonTable->id(25);
is $myCodonTable->is_start_codon('GTG'), 1;
is $myCodonTable->is_start_codon('CTG'), 0;
is $myCodonTable->translate_strict('TGA'), 'G';
ok( $myCodonTable->is_start_codon('GTG'));
ok( !$myCodonTable->is_start_codon('CTG'));
is($myCodonTable->translate_strict('TGA'), 'G');

# The name for this is pretty long and spans mutliple lines. Confirm
# it was parsed correctly from the ASN.1 file.
Expand Down

0 comments on commit fa9366f

Please sign in to comment.