From fa9366f3a2f48fd051343d488cfce26655f842b3 Mon Sep 17 00:00:00 2001 From: David Miguel Susano Pinto Date: Fri, 26 Apr 2024 15:56:53 +0100 Subject: [PATCH] Bio::Tools::CodonTable::_codon_is: simplify with use all() 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. --- lib/Bio/Tools/CodonTable.pm | 18 ++++------ t/SeqTools/CodonTable.t | 65 ++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 45 deletions(-) diff --git a/lib/Bio/Tools/CodonTable.pm b/lib/Bio/Tools/CodonTable.pm index 8c05aee37..a377b2fc5 100644 --- a/lib/Bio/Tools/CodonTable.pm +++ b/lib/Bio/Tools/CodonTable.pm @@ -4,6 +4,8 @@ use utf8; use strict; use warnings; +use List::Util; + use Bio::Tools::IUPAC; use Bio::SeqUtils; @@ -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 diff --git a/t/SeqTools/CodonTable.t b/t/SeqTools/CodonTable.t index 8f2e28609..7ce9f3de2 100644 --- a/t/SeqTools/CodonTable.t +++ b/t/SeqTools/CodonTable.t @@ -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'; @@ -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.