Skip to content

Commit

Permalink
Bio::Tools::CodonTable: handle negative table ids as invalid (#386)
Browse files Browse the repository at this point in the history
Without this extra check, negative table IDs were being counted from
the end of the tables array.
  • Loading branch information
Juke34 authored and carandraug committed Apr 26, 2024
1 parent 62ee9df commit 9f6beda
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/Bio/Tools/CodonTable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ sub new {
sub id{
my ($self,$value) = @_;
if( defined $value) {
if ( not defined $TABLES[$value] or $TABLES[$value] eq '') {
if (! defined $TABLES[$value] || $TABLES[$value] eq '' || $value < 0) {
$self->warn("Not a valid codon table ID [$value], using [1] instead ");
$value = 1;
}
Expand Down
22 changes: 13 additions & 9 deletions t/SeqTools/CodonTable.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use strict;
BEGIN {
use Bio::Root::Test;

test_begin(-tests => 87);
test_begin(-tests => 89);

use_ok('Bio::Tools::CodonTable');
use_ok('Bio::CodonUsage::IO');
Expand All @@ -27,16 +27,20 @@ $myCodonTable = Bio::Tools::CodonTable->new();
is $myCodonTable->id(), 1;
is $myCodonTable->name(), "Standard";

# invalid table should produce a warn and set default table (1)
my $stderr = '';

# Test invalid IDs warn and return table id=1
{
# capture stderr output
local *STDERR;
open STDERR, '>', \$stderr;
$myCodonTable->id(99);
foreach my $invalid_id (99, -2) {
my $stderr = '';
# capture stderr output
local *STDERR;
open STDERR, '>', \$stderr;
$myCodonTable = Bio::Tools::CodonTable->new(-id => $invalid_id);
like $stderr, qr/Not a valid codon table ID/;
is $myCodonTable->id, 1;
}
}
like $stderr, qr/Not a valid codon table ID/;
is $myCodonTable->id, 1;


# change codon table
$myCodonTable->id(10);
Expand Down

0 comments on commit 9f6beda

Please sign in to comment.