Database/Seq.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 170 CC 10 SCAR 18.7
CC
10
Cov
0%
alphabet
20.0
trunc
12.0
_parse_defline
12.0
CRAP 6.5
1 use strict;
2 use warnings;
3  
4 package Bio::BLAST::Database::Seq;
5 BEGIN {
6 $Bio::BLAST::Database::Seq::AUTHORITY = 'cpan:RBUELS';
7 }
8 BEGIN {
9 $Bio::BLAST::Database::Seq::VERSION = '0.4';
10 }
11  
12 # ABSTRACT: lazy-loading sequence from a BLAST database
13  
14  
15 use Carp;
16  
17 use Bio::PrimarySeq;
18 use Bio::Seq::LargePrimarySeq;
19  
20 use base 'Bio::PrimarySeqI';
21  
22  
23 sub new {
24 my ( $class, %args ) = @_;
25 my $self = bless {}, $class;
26  
27 # strip leading '-' on args
28 for my $k ( keys %args ) {
29 ( my $nk = $k ) =~ s/^-//;
30 $self->{ $nk } = $args{$k}
31 }
32  
33 $self->{$_} or croak "must give $_!"
34 for 'bdb', 'id';
35  
36 #read the defline out of the bdb
37 my $s = $self->trunc(1,1)
38 or return;
39 $self->{desc} = $s->desc;
40  
41 return $self;
42 }
43  
44  
45 sub seq {
46 my $self = shift;
47 croak ref($self)." objects are immutable, cannot set seq" if @_;
48  
49 return $self->whole_seq->seq;
50 }
51 sub length {
52 my $self = shift;
53 croak ref($self)." objects are immutable, cannot set length" if @_;
54  
55 return $self->whole_seq->length;
56 }
57  
58  
59 sub whole_seq {
60 my ( $self ) = @_;
61  
62 return $self->{seq_obj} ||= do {
63 my $ffbn = $self->_bdb->full_file_basename;
64 my $seqname = $self->id;
65 CORE::open my $fc, "fastacmd -d '$ffbn' -s '$seqname' 2>&1 |";
66  
67 # we can't know how big this sequence is. fastacmd has no way (or
68 # at least no documented way) to query seq lengths. so we need to
69 # treat the sequence as if it were big. cause sometimes it is.
70 # so we parse as if it is going to be large
71 $self->_parse_large_seq( $fc )
72 };
73 }
74  
75 sub trunc {
76 my ($self, $start, $end ) = @_;
77  
78 croak "start ($start) is greater than end ($end)" if $start > $end;
79  
80 $_ += 0 for $start, $end; # ensure numeric
81  
82 my $length = $end - $start + 1;
83  
84 my $ffbn = $self->_bdb->full_file_basename;
85 my $seqname = $self->id;
86 open my $fc, "fastacmd -d '$ffbn' -s '$seqname' -L $start,$end 2>&1 |";
87  
88 return $length > 4_000_000
89 ? $self->_parse_large_seq( $fc )
90 : $self->_parse_small_seq( $fc );
91 }
92  
93 sub subseq {
94 shift->trunc( @_ )->seq;
95 }
96  
97 sub alphabet {
98 my $self = shift;
99 croak ref($self)." objects are immutable, cannot set seq" if @_;
100  
101 my $type = $self->_bdb->type
102 or return;
103  
104 return 'dna' if $type eq 'nucleotide';
105 return 'protein' if $type eq 'protein';
106  
107 die "invalid type '$type' for blast database ".$self->_bdb->full_file_basename;
108 }
109  
110  
111 sub id { $_[0]->{id} }
112  
113  
114 sub desc { $_[0]->{desc} }
115 *description = \&desc;
116  
117 sub accession_number { 'unknown' }
118 sub display_id { $_[0]->id }
119 sub primary_id { $_[0]->id }
120 sub _bdb { $_[0]->{bdb} }
121  
122 ########### helpers ################3
123  
124 # takes fastacmd filehandle that is going to be emitting a big
125 # sequence and makes a LargePrimarySeq out of it
126 sub _parse_large_seq {
127 my ( $self, $fc ) = @_;
128  
129 my ( $id, $defline ) = $self->_parse_defline( $fc ) or return;
130  
131 # stream the sequence into a LargePrimarySeq, read up to 4 MiB of
132 # sequence at a time
133 my $seq = Bio::Seq::LargePrimarySeq->new( -id => $id, -desc => $defline );
134 local $/ = \4_000_000;
135 while( my $seq_chunk = <$fc> ) {
136 $seq_chunk =~ s/\s//g;
137 $seq->add_sequence_as_string( $seq_chunk );
138 }
139  
140 return $seq;
141 }
142  
143 sub _parse_small_seq {
144 my ( $self, $fc ) = @_;
145  
146 my ( $id, $defline ) = $self->_parse_defline( $fc ) or return;
147  
148 # slurp the rest of the sequence and return a PrimarySeq
149 local $/;
150 my $seq = <$fc>;
151 $seq =~ s/\s//g;
152  
153 return Bio::PrimarySeq->new(
154 -id => $id,
155 -desc => $defline,
156 -seq => $seq,
157 );
158 }
159  
160 sub _parse_defline {
161 my ( $self, $fc ) = @_;
162  
163 my $defline = <$fc>;
164 return if $defline =~ /ERROR:\s+Entry\s*"[^"]+"\s+not found/;
165 ( my $id, $defline ) = $defline =~ m(
166 >(?:lcl\|)?(\S+) \s+ (.*)
167 )x
168 or die "could not parse fastacmd output\n:$defline";
169  
170 undef $defline if $defline =~ /^No definition line found\.?$/;
171  
172 return ( $id, $defline );
173 }
174  
175  
176 1;
177  
178 __END__