Permalink
Browse files

Same revcom() and length() approach in Bio::DB::Qual as in Bio::DB::F…

…asta
  • Loading branch information...
1 parent a75eeea commit fac6f33a8e8fb343e1c4bc4daaef45e20cd0b0dc @fangly fangly committed Nov 6, 2012
Showing with 22 additions and 12 deletions.
  1. +17 −9 Bio/DB/Qual.pm
  2. +5 −3 t/LocalDB/Qual.t
View
@@ -431,23 +431,24 @@ sub subqual {
sub trunc {
+ # Override Bio::Seq::QualI trunc() method. This way, we create an object
+ # that does not store the quality array in memory.
my ($self, $start, $stop) = @_;
$self->throw(
"$stop is smaller than $stop. If you want to truncate and reverse ".
"complement, you must call trunc followed by revcom."
) if $start > $stop;
- my ($left, $right);
if ($self->{start} <= $self->{stop}) {
- $left = $self->{start}+$start-1;
- $right = $self->{start}+$stop-1;
+ $start = $self->{start}+$start-1;
+ $stop = $self->{start}+$stop-1;
} else {
- $left = $self->{start}-($start-1);
- $right = $self->{start}-($stop-1);
+ $start = $self->{start}-($start-1);
+ $stop = $self->{start}-($stop-1);
}
my $obj = $self->new( -database => $self->{db},
-id => $self->{id},
- -start => $left,
- -stop => $right
+ -start => $start,
+ -stop => $stop
);
return $obj;
}
@@ -464,11 +465,18 @@ sub primary_id {
return overload::StrVal($self);
}
+sub revcom {
+ # Override Bio::QualI revcom() with optimized method.
+ my $self = shift;
+ return $self->new(@{$self}{'db', 'id', 'stop', 'start'});
+}
sub length {
- # number of quality scores
+ # Get length from quality location, not the quality array (too expensive)
my $self = shift;
- return scalar(@{$self->qual});
+ return $self->{start} < $self->{stop} ?
+ $self->{stop} - $self->{start} + 1 :
+ $self->{start} - $self->{stop} + 1 ;
}
View
@@ -2,7 +2,7 @@ BEGIN {
use lib '.';
use Bio::Root::Test;
- test_begin( -tests => 54,
+ test_begin( -tests => 56,
-requires_module => 'Bio::DB::Qual');
use_ok('Bio::Root::IO');
@@ -42,7 +42,7 @@ is_deeply $db->qual($seqid, 11, 2, -1), [32, 24, 27, 26, 27, 27, 27, 28, 23, 28]
is $db->get_Qual_by_id('foobarbaz'), undef;
ok my $obj = $db->get_Qual_by_id($seqid);
isa_ok $obj, 'Bio::Seq::PrimaryQual::Qual';
-isa_ok $obj, 'Bio::Seq::PrimaryQual';
+isa_ok $obj, 'Bio::Seq::QualI';
is ref($obj->qual($seqid)), 'ARRAY';
is $obj->length, 14;
is $obj->id, '17601979';
@@ -56,10 +56,12 @@ is_deeply $obj->subqual(2, 11), [32, 24, 27, 26, 27, 27, 27, 28, 23, 28];
is $obj->header, undef;
is $obj->desc, undef;
ok my $truncobj = $obj->trunc(1,3);
-isa_ok $truncobj, 'Bio::Seq::PrimaryQual';
+isa_ok $truncobj, 'Bio::Seq::PrimaryQual::Qual';
+isa_ok $obj, 'Bio::Seq::QualI';
is ref($truncobj->qual($seqid)), 'ARRAY';
is $truncobj->length, 3;
ok my $revobj = $obj->revcom;
+isa_ok $revobj, 'Bio::Seq::PrimaryQual::Qual';
isa_ok $revobj, 'Bio::Seq::PrimaryQual';
is ref($revobj->qual), 'ARRAY';
is $revobj->length, 14;

0 comments on commit fac6f33

Please sign in to comment.