Bio/SearchIO/blast_pull.pm

filter: Statementstmt 83.5% 56 / 67 Branchbran 55.0% 11 / 20 Conditioncond 47.3% 9 / 19 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 77.7% 7 / 9 Podpod 100% 3 / 3 totaltotal 67.4% 87 / 129 CC 18 SCAR 23.7
CC
18
Cov
72%
_discover_next_result
19.8
_initialize
9.4
_discover_header
2.0
CRAP 10.7
« Bio/AlignIO/metafasta.pm ↑ Summary Bio/WebAgent.pm »
1 #
2 # BioPerl module for Bio::SearchIO::blast_pull
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Sendu Bala <bix@sendu.me.uk>
7 #
8 # Copyright Sendu Bala
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 Bio::SearchIO::blast_pull - A parser for BLAST output
17  
18 =head1 SYNOPSIS
19  
20 # do not use this class directly it is available through Bio::SearchIO
21 use Bio::SearchIO;
22 my $in = Bio::SearchIO->new(-format => 'blast_pull',
23 -file => 't/data/new_blastn.txt');
24 while (my $result = $in->next_result) {
25 # this is a Bio::Search::Result::BlastPullResult object
26 print "Results for ", $result->query_name(), "\n";
27 while (my $hit = $result->next_hit) {
28 print $hit->name(), "\n";
29 while (my $hsp = $hit->next_hsp) {
30 print "length is ", $hsp->length(), "\n";
31 }
32 }
33 }
34  
35 =head1 DESCRIPTION
36  
37 This object implements a pull-parser for BLAST output. It is fast since it
38 only does work on request (hence 'pull').
39  
40 Currently only NCBI BLASTN and BLASTP are supported.
41  
42 =head1 FEEDBACK
43  
44 =head2 Mailing Lists
45  
46 User feedback is an integral part of the evolution of this and other
47 Bioperl modules. Send your comments and suggestions preferably to
48 the Bioperl mailing list. Your participation is much appreciated.
49  
50 bioperl-l@bioperl.org - General discussion
51 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
52  
53 =head2 Support
54  
55 Please direct usage questions or support issues to the mailing list:
56  
57 I<bioperl-l@bioperl.org>
58  
59 rather than to the module maintainer directly. Many experienced and
60 reponsive experts will be able look at the problem and quickly
61 address it. Please include a thorough description of the problem
62 with code and data examples if at all possible.
63  
64 =head2 Reporting Bugs
65  
66 Report bugs to the Bioperl bug tracking system to help us keep track
67 of the bugs and their resolution. Bug reports can be submitted via the
68 web:
69  
70 https://github.com/bioperl/bioperl-live/issues
71  
72 =head1 AUTHOR - Sendu Bala
73  
74 Email bix@sendu.me.uk
75  
76 =head1 APPENDIX
77  
78 The rest of the documentation details each of the object methods.
79 Internal methods are usually preceded with a _
80  
81 =cut
82  
83 # Let the code begin...
84  
85 package Bio::SearchIO::blast_pull;
86 2 $Bio::SearchIO::blast_pull::VERSION = '1.7.8';
87 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use Bio::Search::Result::BlastPullResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 2 ▶use base qw(Bio::SearchIO Bio::PullParserI);
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 =head2 new
93  
94 Title : new
95 Usage : my $obj = Bio::SearchIO::blast_pull->new();
96 Function: Builds a new Bio::SearchIO::blast_pull object
97 Returns : Bio::SearchIO::blast_pull
98 Args : -fh/-file => BLAST output filename
99 -format => 'blast_pull'
100 -evalue => float or scientific notation number to be used
101 as an evalue cutoff for hits
102 -score => integer or scientific notation number to be used
103 as a score value cutoff for hits
104 -piped_behaviour => 'temp_file'|'memory'|'sequential_read'
105  
106 -piped_behaviour defines what the parser should do if the input is
107 an unseekable filehandle (eg. piped input), see
108 Bio::PullParserI::chunk for details. Default is 'memory'.
109  
110 =cut
111  
112 12 ▶sub _initialize {
Subroutine _initialize: called CC=7 SCAR=22.4
113 12 my ($self, @args) = @_;
114
115 # don't do normal SearchIO initialization
116
117 12 my ($writer, $file, $fh, $piped_behaviour, $evalue, $score) =
118 $self->_rearrange([qw(WRITER
119 FILE FH
120 PIPED_BEHAVIOUR
121 EVALUE
122 SCORE)], @args);
123 12 ▶ $self->writer($writer) if $writer;
Branch: 1 branch
BranchTrueFalseTotal
if $writer 0 12 12
124
125 12 $self->_fields( { ( header => undef,
126 algorithm => undef,
127 algorithm_version => undef,
128 algorithm_reference => '',
129 database_name => undef,
130 database_letters => undef,
131 database_entries => undef,
132 next_result => undef,
133 evalue_cutoff => '[unset]',
134 score_cutoff => '[unset]' ) } );
135
136 12 ▶ $self->_fields->{evalue_cutoff} = $evalue if $evalue;
Branch: 1 branch
BranchTrueFalseTotal
if $evalue 0 12 12
137 12 ▶ $self->_fields->{score_cutoff} = $score if $score;
Branch: 1 branch
BranchTrueFalseTotal
if $score 0 12 12
138
139 12 $self->_dependencies( { ( algorithm => 'header',
140 algorithm_version => 'header',
141 database_name => 'header',
142 database_letters => 'header',
143 database_entries => 'header' ) } );
144
145 12 ▶ $self->chunk($file || $fh || $self->throw("-file or -fh must be supplied"),
Condition37%3/8
$file || $fh || $self->throw('-file or -fh must be supplied')
l!l&&r!l&&!r
1200
$file or $fh
l!l&&r!l&&!r
1200
$piped_behaviour || 'memory'
l!l
012
MC/DC0%0/3
$file || $fh || $self->throw('-file or -fh must be supplied')
$file $fh $self->throw('-file or -fh must be supplied')
MC/DC0%0/1
$piped_behaviour || 'memory'
$piped_behaviour
Truth table25%1/4
A or B || C
A= $fileB= $fhC= $self->throw('-file or -fh must be supplied')
ABCresult
1XX1
01X1
0011
0000
Truth table50%1/2
A
A= $piped_behaviour
Aresult
11
00
146 -piped_behaviour => $piped_behaviour || 'memory');
147 }
148  
149 12 ▶sub _discover_header {
Subroutine _discover_header: called CC=2 SCAR=6.9
150 12 my $self = shift;
151 12 $self->_chunk_seek(0);
152 12 my $header = $self->_get_chunk_by_end("\nQuery=");
153 12 $self->{_after_header} = $self->_chunk_tell;
154
155 #*** won't catch all types? only support blastn/p now anyway
156 12 $header =~ /^(\S+) (\S+\s+\S+)/;
157 12 $self->_fields->{algorithm} = $1;
158 12 $self->_fields->{algorithm_version} = $2;
159
160 12 my ($database) = $header =~ /^Database: (.+)/sm;
161
162 12 ▶ unless ($database) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($database) 7 5 12
163 # earlier versions put query before database?
164 7 my $header2 = $self->_get_chunk_by_end(".done\n");
165 7 ($database) = $header2 =~ /^Database: (.+)/sm;
166 }
167
168 12 $database =~ s/\s+(\d\S+) sequences; (\d\S+) total letters.*//s;
169 12 my $entries = $1;
170 12 my $letters = $2;
171 12 $database =~ s/\n//g;
172 12 $entries =~ s/,//g;
173 12 $letters =~ s/,//g;
174 12 $self->_fields->{database_name} = $database;
175 12 $self->_fields->{database_entries} = $entries;
176 12 $self->_fields->{database_letters} = $letters;
177
178 12 $self->_fields->{header} = 1;
179 }
180  
181 19 ▶sub _discover_next_result {
Subroutine _discover_next_result: called CC=10 SCAR=29.9
182 19 my $self = shift;
183 19 ▶ return if $self->{_after_results};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_after_results"} 0 19 19
184 19 my $type = $self->get_field('algorithm'); # also sets _after_header if not set
185
186 19 ▶ if ($type eq 'BLASTN' || $type eq 'BLASTP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'BLASTN' or $type eq 'BLASTP') { } 19 0 19
Condition66%2/3
$type eq "BLASTN" or $type eq "BLASTP"
l!l&&r!l&&!r
8110
MC/DC0%0/2
$type eq "BLASTN" or $type eq "BLASTP"
$type eq "BLASTN" $type eq "BLASTP"
Truth table66%2/3
A or B
A= $type eq "BLASTN"B= $type eq "BLASTP"
ABresult
1X1
011
000
187 19 ▶ unless ($self->_sequential) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_sequential) { } 0 19 19
188 19 ▶ $self->_chunk_seek($self->{_end_of_previous_result} || $self->{_after_header});
Condition66%2/3
$self->{'_end_of_previous_result'} || $self->{'_after_header'}
l!l&&r!l&&!r
7120
MC/DC0%0/2
$self->{'_end_of_previous_result'} || $self->{'_after_header'}
$self->{'_end_of_previous_result'} $self->{'_after_header'}
Truth table66%2/3
A || B
A= $self->{'_end_of_previous_result'}B= $self->{'_after_header'}
ABresult
1X1
011
000
189
190 19 my ($start, $end) = $self->_find_chunk_by_end("\nQuery=");
191 19 ▶ return if ($start == $end);
Branch: 1 branch
BranchTrueFalseTotal
if $start == $end 2 17 19
192
193 17 ▶ unless ($end) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($end) 0 17 17
194 0 ▶ $start = $self->{_end_of_previous_result} || $self->{_after_header};
Condition0%0/3
$self->{'_end_of_previous_result'} || $self->{'_after_header'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_end_of_previous_result'} || $self->{'_after_header'}
$self->{'_end_of_previous_result'} $self->{'_after_header'}
Truth table0%0/3
A || B
A= $self->{'_end_of_previous_result'}B= $self->{'_after_header'}
ABresult
1X1
011
000
195 0 $end = undef;
196 }
197
198 17 $self->_fields->{next_result} = Bio::Search::Result::BlastPullResult->new(-chunk => [($self->chunk, $start, $end)],
199 -parent => $self);
200
201 17 $self->{_end_of_previous_result} = $end;
202 }
203 else {
204 #*** doesn't work for the last result, needs fixing - try getting the database end chunk on failure?...
205 0 $self->throw("sequential mode not yet implemented");
206 0 my $chunk = $self->_get_chunk_by_end("\nQuery=");
207 0 ▶ $chunk || return;
Branch: 1 branch
BranchTrueFalseTotal
$chunk or return 0 0 0
208 0 $self->_fields->{next_result} = Bio::Search::Result::BlastPullResult->new(-chunk => [$chunk],
209 -parent => $self);
210 }
211 }
212 else {
213 0 $self->throw("Can only handle NCBI BLASTN and BLASTP right now");
214 }
215 }
216  
217 =head2 next_result
218  
219 Title : next_result
220 Usage : my $hit = $searchio->next_result;
221 Function: Returns the next Result from a search
222 Returns : Bio::Search::Result::ResultI object
223 Args : none
224  
225 =cut
226  
227 19 ▶sub next_result {
Subroutine next_result: called CC=2 SCAR=6.9
Pod documented
228 19 my $self = shift;
229 19 ▶ my $result = $self->get_field('next_result') || return;
Condition100%2/2
$self->get_field('next_result') || (return)
l!l
172
MC/DC100%1/1
$self->get_field('next_result') || (return)
$self->get_field('next_result')
Truth table100%2/2
A
A= $self->get_field('next_result')
Aresult
11
00
230
231 17 undef $self->_fields->{next_result};
232
233 17 $self->{'_result_count'}++;
234 17 return $result;
235 }
236  
237 =head2 result_count
238  
239 Title : result_count
240 Usage : my $count = $searchio->result_count
241 Function: Returns the number of results we have processed.
242 Returns : integer
243 Args : none
244  
245 =cut
246  
247 0 ▶sub result_count {
Subroutine result_count: not called CC=1 SCAR=6.9
Pod documented
248 0 my $self = shift;
249 0 return $self->{'_result_count'};
250 }
251  
252 =head2 rewind
253  
254 Title : rewind
255 Usage : $searchio->rewind;
256 Function: Allow one to reset the Result iterator to the beginning, so that
257 next_result() will subsequently return the first result and so on.
258  
259 NB: result objects are not cached, so you will get new result objects
260 each time you rewind. Also, note that result_count() counts the
261 number of times you have called next_result(), so will not be able
262 tell you how many results there were in the file if you use rewind().
263  
264 Returns : n/a
265 Args : none
266  
267 =cut
268  
269 0 ▶sub rewind {
Subroutine rewind: not called CC=1 SCAR=6.9
Pod documented
270 0 my $self = shift;
271 0 delete $self->{_end_of_previous_result};
272 }
273  
274 2 1;
« Bio/AlignIO/metafasta.pm ↑ Summary Bio/WebAgent.pm »