Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

PrimarySeq validation speedup

Small optimization to make validation of sequences faster:
  ~9 % faster for large sequences (10 Mbp)
  ~27% faster for small sequences (100 bp)
  • Loading branch information...
commit cbcf1731904baee92ac03978be50b49401e4f77d 1 parent 27f9a62
@fangly fangly authored
Showing with 9 additions and 10 deletions.
  1. +5 −7 Bio/PrimarySeq.pm
  2. +4 −3 t/Seq/PrimarySeq.t
View
12 Bio/PrimarySeq.pm
@@ -324,20 +324,18 @@ sub _set_seq_by_ref {
sequences are considered valid.
Returns : 1 if the supplied sequence string is valid, 0 otherwise.
Args : - Sequence string to be validated
- - Boolean to throw an error if the sequence is invalid
+ - Boolean to optionally throw an error if the sequence is invalid
=cut
sub validate_seq {
my ($self, $seqstr, $throw) = @_;
- $seqstr = '' if not defined $seqstr;
- $throw = 0 if not defined $throw ; # 0 for backward compatiblity
- if ( (CORE::length $seqstr > 0 ) &&
- ($seqstr !~ /^([$MATCHPATTERN]+)$/) ) {
+ if ( (defined $seqstr ) &&
+ ($seqstr !~ /^[$MATCHPATTERN]*$/) ) {
if ($throw) {
$self->throw("Failed validation of sequence '".(defined($self->id) ||
- '[unidentified sequence]')."'. Invalid characters were: " .
- join('',($seqstr =~ /([^$MATCHPATTERN]+)/g)));
+ '[unidentified sequence]')."'. Invalid characters were: " .
+ join('',($seqstr =~ /[^$MATCHPATTERN]/g)));
}
return 0;
}
View
7 t/Seq/PrimarySeq.t
@@ -7,7 +7,7 @@ use Data::Dumper;
BEGIN {
use lib '.';
use Bio::Root::Test;
- test_begin( -tests => 177 );
+ test_begin( -tests => 178 );
use_ok('Bio::PrimarySeq');
use_ok('Bio::Location::Simple');
@@ -366,10 +366,11 @@ is $seq->validate_seq( 'ACGT' ), 1;
is $seq->validate_seq( 'XFRH' ), 1;
is $seq->validate_seq( '-~' ), 1; # gap symbols
is $seq->validate_seq( '-.*?=~' ), 1; # other valid symbols
+is $seq->validate_seq( '0' ), 0;
is $seq->validate_seq( 'AAAA$' ), 0;
-is $seq->validate_seq( 'tt&tt' ), 0;
+is $seq->validate_seq( 'tt&t!' ), 0;
-throws_ok { $seq->validate_seq('tt&tt', 1); } qr/.+/;
+throws_ok { $seq->validate_seq('tt&t!', 1); } qr/.+/;
# Test direct option (no sequence validation)
Please sign in to comment.
Something went wrong with that request. Please try again.