Bio/SearchIO/megablast.pm

filter: Statementstmt 75.5% 99 / 131 Branchbran 61.5% 32 / 52 Conditioncond 33.3% 4 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 72.2% 13 / 18 Podpod 91.6% 11 / 12 totaltotal 69.4% 161 / 232 CC 32 SCAR 24.7
CC
32
Cov
69%
within_element
42.0
next_result
16.6
end_element
6.6
CRAP 11.8
« Bio/Tools/Est2Genome.pm ↑ Summary Bio/Index/AbstractSeq.pm »
1 #
2 # BioPerl module for Bio::SearchIO::megablast
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich <jason@bioperl.org>
7 #
8 # Copyright Jason Stajich
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::megablast - a driver module for Bio::SearchIO to parse
17 megablast reports (format 0)
18  
19 =head1 SYNOPSIS
20  
21 # do not use this module directly
22  
23 use Bio::SearchIO;
24 # for default format output from megablast
25 my $in = Bio::SearchIO->new(-file => 'file.mbl',
26 -format => 'megablast',
27 -report_format => 0);
28  
29 while( my $r = $in->next_result ) {
30 while( my $hit = $r->next_hit ) {
31 while( my $hsp = $hit->next_hsp ) {
32 }
33 }
34 }
35  
36 =head1 DESCRIPTION
37  
38 Beware!
39  
40 Because of the way megablast report format 0 is coded, realize that score
41 means # gap characters + # mismatches for a HSP.
42  
43 The docs from NCBI regarding FORMAT 0
44 # 0: Produce one-line output for each alignment, in the form
45 #
46 # 'subject-id'=='[+-]query-id' (s_off q_off s_end q_end) score
47 #
48 # Here subject(query)-id is a gi number, an accession or some other type of
49 # identifier found in the FASTA definition line of the respective sequence.
50 #
51 # + or - corresponds to same or different strand alignment.
52 #
53 # Score for non-affine gapping parameters means the total number of
54 # differences (mismatches + gap characters). For affine case it is the
55 # actual (raw) score of the alignment.
56  
57 FORMAT 1 parsing has not been implemented
58 FORMAT 2 parsing should work with the SearchIO 'blast' parser
59  
60 =head1 FEEDBACK
61  
62 =head2 Mailing Lists
63  
64 User feedback is an integral part of the evolution of this and other
65 Bioperl modules. Send your comments and suggestions preferably to
66 the Bioperl mailing list. Your participation is much appreciated.
67  
68 bioperl-l@bioperl.org - General discussion
69 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
70  
71 =head2 Support
72  
73 Please direct usage questions or support issues to the mailing list:
74  
75 I<bioperl-l@bioperl.org>
76  
77 rather than to the module maintainer directly. Many experienced and
78 reponsive experts will be able look at the problem and quickly
79 address it. Please include a thorough description of the problem
80 with code and data examples if at all possible.
81  
82 =head2 Reporting Bugs
83  
84 Report bugs to the Bioperl bug tracking system to help us keep track
85 of the bugs and their resolution. Bug reports can be submitted via the
86 web:
87  
88 https://github.com/bioperl/bioperl-live/issues
89  
90 =head1 AUTHOR - Jason Stajich
91  
92 Email jason-at-bioperl.org
93  
94 =head1 APPENDIX
95  
96 The rest of the documentation details each of the object methods.
97 Internal methods are usually preceded with a _
98  
99 =cut
100  
101  
102 # Let the code begin...
103  
104  
105 package Bio::SearchIO::megablast;
106 2 $Bio::SearchIO::megablast::VERSION = '1.7.8';
107 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 2 ▶use vars qw(%MAPPING %MODEMAP $DEFAULT_BLAST_WRITER_CLASS);
Subroutine BEGIN: called CC=1 SCAR=0.0
109  
110 2 ▶use base qw(Bio::SearchIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
113 # mapping of MegaBlast terms to Bioperl hash keys
114 2 %MODEMAP = ('MegaBlastOutput' => 'result',
115 'Hit' => 'hit',
116 'Hsp' => 'hsp'
117 );
118  
119 # This should really be done more intelligently, like with
120 # XSLT
121  
122 2 %MAPPING =
123 (
124 'Hsp_query-from' => 'HSP-query_start',
125 'Hsp_query-to' => 'HSP-query_end',
126 'Hsp_hit-from' => 'HSP-hit_start',
127 'Hsp_hit-to' => 'HSP-hit_end',
128 'Hit_score' => 'HIT-score',
129 'Hsp_score' => 'HSP-score',
130
131 'Hsp_identity' => 'HSP-identical',
132 'Hsp_positive' => 'HSP-conserved',
133  
134 'Hit_id' => 'HIT-name',
135
136 'MegaBlastOutput_program' => 'RESULT-algorithm_name',
137 'MegaBlastOutput_query-def'=> 'RESULT-query_name',
138 );
139  
140  
141 2 $DEFAULT_BLAST_WRITER_CLASS = 'Bio::SearchIO::Writer::HitTableWriter';
142 }
143  
144  
145 =head2 new
146  
147 Title : new
148 Usage : my $obj = Bio::SearchIO::blast->new();
149 Function: Builds a new Bio::SearchIO::blast object
150 Returns : Bio::SearchIO::blast
151 Args : -fh/-file => filehandle/filename to BLAST file
152 -format => 'blast'
153  
154 =cut
155  
156 1 ▶sub _initialize {
Subroutine _initialize: called CC=2 SCAR=7.0
157 1 my ($self,@args) = @_;
158 1 $self->SUPER::_initialize(@args);
159 1 my ($fmt) = $self->_rearrange([qw(REPORT_FORMAT)], @args);
160  
161 1 ▶ $self->throw("Must provide a value for -report_format when initializing a megablast parser") unless defined $fmt ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fmt 0 1 1
162 1 $self->report_format($fmt);
163 1 return 1;
164 }
165  
166 =head2 next_result
167  
168 Title : next_result
169 Usage : my $hit = $searchio->next_result;
170 Function: Returns the next Result from a search
171 Returns : Bio::Search::Result::ResultI object
172 Args : none
173  
174 =cut
175  
176 1 ▶sub next_result{
Subroutine next_result: called CC=14 SCAR=28.1
Pod documented
177 1 my ($self) = @_;
178
179 1 local $/ = "\n";
180 1 local $_;
181  
182 1 my $fmt = $self->report_format;
183 1 my ($lastquery,$lasthit);
184 1 ▶ while( defined($_ = $self->_readline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->_readline)) 21 1 22
185 21 ▶ if( $fmt == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmt == 0) 21 0 21
186 21 ▶ if( /^\'(\S+)\'\=\=\'(\+|\-)(\S+)\'\s+
Branch: 1 branch
BranchTrueFalseTotal
if (/^\'(\S+)\'\=\=\'(\+|\-)(\S+)\'\s+ \((\d+)\s+(\d+)\s+(\d+)\s+(\d+)\)\s+ (\d+)/ox) { } 21 0 21
187 \((\d+)\s+(\d+)\s+(\d+)\s+(\d+)\)\s+
188 (\d+)/ox )
189 {
190 21 my ($hit,$strand,$query,
191 $h_start,$q_start,$h_end,$q_end,
192 $score) = ($1,$2,$3,$4,$5,$6,$7,$8);
193 21 ▶ if( ! defined $lastquery ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $lastquery) { } 1 20 21
elsif ($lastquery ne $query) { } 0 20 20
194 1 $self->start_element({'Name' => 'MegaBlastOutput'});
195 1 $self->element({'Name' => 'MegaBlastOutput_program',
196 'Data' => 'MEGABLAST'});
197 1 $self->element({'Name' => 'MegaBlastOutput_query-def',
198 'Data' => $query});
199 } elsif( $lastquery ne $query ) {
200 0 $self->_pushback($_);
201 0 ▶ $self->end_element({'Name' => 'Hit'}) if( defined $lasthit);
Branch: 1 branch
BranchTrueFalseTotal
if defined $lasthit 0 0 0
202 0 $self->end_element({ 'Name' => 'MegaBlastOutput'});
203 0 $lasthit = undef;
204 0 $lastquery = undef;
205 0 return $self->end_document();
206 }
207  
208 21 ▶ if( ! defined $lasthit || $lasthit ne $hit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lasthit or $lasthit ne $hit) 4 17 21
Condition100%3/3
not defined $lasthit or $lasthit ne $hit
l!l&&r!l&&!r
1317
MC/DC100%2/2
not defined $lasthit or $lasthit ne $hit
not defined $lasthit $lasthit ne $hit
Truth table100%3/3
not(A) or B
A= not defined $lasthitB= $lasthit ne $hit
ABresult
1X1
011
000
209 4 ▶ $self->end_element({'Name' => 'Hit'}) if( defined $lasthit);
Branch: 1 branch
BranchTrueFalseTotal
if defined $lasthit 3 1 4
210 4 $self->start_element({'Name' => 'Hit'});
211 4 $self->element({'Name' => 'Hit_id',
212 'Data' => $hit});
213 }
214 21 $self->start_element({'Name' => 'Hsp'});
215 21 $self->element({'Name' => 'Hsp_score',
216 'Data' => $score});
217  
218 # flip flop start/end if strand is < 0
219 # since strandedness is inferred from the query
220 # because of the way it is coded all queries will
221 # be on the forward strand and hits will be either
222 # +/-
223  
224 # also the NCBI docs state:
225 # 0: Produce one-line output for each alignment, in the form
226 #
227 # 'subject-id'=='[+-]query-id' (s_off q_off s_end q_end) score
228 #
229 # Here subject(query)-id is a gi number, an accession or some other type of
230 # identifier found in the FASTA definition line of the respective sequence.
231 #
232 # + or - corresponds to same or different strand alignment.
233 #
234 # Score for non-affine gapping parameters means the total number of
235 # differences (mismatches + gap characters). For affine case it is the
236 # actual (raw) score of the alignment.
237
238 # and yet when rev strand hits are made I see
239 # (MBL 2.2.4)
240 # 'Contig634'=='-503384' (1 7941 321 7620) 19
241 # so the query is on the rev strand and the
242 # subject is on the fwd strand
243 # so I am flip-flopping everything when I see a '-'
244 21 ▶ if( $strand eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strand eq "-") 18 3 21
245 18 ($h_start,$h_end) = ( $h_end,$h_start);
246 18 ($q_start,$q_end) = ( $q_end,$q_start);
247 }
248 21 $self->element({'Name' => 'Hsp_hit-from',
249 'Data' => $h_start});
250 21 $self->element({'Name' => 'Hsp_hit-to',
251 'Data' => $h_end});
252 21 $self->element({'Name' => 'Hsp_query-from',
253 'Data' => $q_start});
254 21 $self->element({'Name' => 'Hsp_query-to',
255 'Data' => $q_end});
256 # might not be quite right -- need to know length of the HSP
257 21 my $numid = (abs($q_end - $q_start) - $score);
258  
259 21 $self->element({'Name' => 'Hsp_identity',
260 'Data' => $numid});
261 21 $self->element({'Name' => 'Hsp_positive',
262 'Data' => $numid});
263  
264 21 $self->end_element({'Name' => 'Hsp'});
265 21 $lasthit = $hit;
266 21 $lastquery = $query;
267 } else {
268 0 $self->debug("Unknown line in fmt0 parsing: $_");
269 }
270 }
271 }
272 1 ▶ if( defined $lastquery && $fmt == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastquery and $fmt == 0) 1 0 1
Condition33%1/3
defined $lastquery and $fmt == 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $lastquery and $fmt == 0
defined $lastquery $fmt == 0
Truth table33%1/3
A and B
A= defined $lastqueryB= $fmt == 0
ABresult
0X0
100
111
273 1 ▶ $self->end_element({'Name' => 'Hit'}) if( defined $lasthit);
Branch: 1 branch
BranchTrueFalseTotal
if defined $lasthit 1 0 1
274 1 $self->end_element({ 'Name' => 'MegaBlastOutput'});
275 1 return $self->end_document();
276 }
277 0 return;
278 }
279  
280 =head2 report_format
281  
282 Title : report_format
283 Usage : $obj->report_format($newval)
284 Function: Get/Set the report_format value
285 Returns : value of report_format (a scalar)
286 Args : on set, new value (a scalar or undef, optional)
287  
288  
289 =cut
290  
291 2 ▶sub report_format{
Subroutine report_format: called CC=2 SCAR=6.9
Pod documented
292 2 my $self = shift;
293 2 ▶ return $self->{'_report_format'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
294 1 return $self->{'_report_format'};
295 }
296  
297  
298 =head2 start_element
299  
300 Title : start_element
301 Usage : $eventgenerator->start_element
302 Function: Handles a start element event
303 Returns : none
304 Args : hashref with at least 2 keys 'Data' and 'Name'
305  
306  
307 =cut
308  
309 179 ▶sub start_element{
Subroutine start_element: called CC=4 SCAR=13.9
Pod documented
310 179 my ($self,$data) = @_;
311 # we currently do not care about attributes
312 179 my $nm = $data->{'Name'};
313 179 ▶ if( my $type = $MODEMAP{$nm} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $Bio::SearchIO::megablast::MODEMAP{$nm}) 26 153 179
314 26 $self->_mode($type);
315 26 ▶ if( $self->_eventHandler->will_handle($type) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_eventHandler->will_handle($type)) 26 0 26
316 26 my $func = sprintf("start_%s",lc $type);
317 26 $self->_eventHandler->$func($data->{'Attributes'});
318 }
319 26 unshift @{$self->{'_elements'}}, $type;
320 }
321  
322 179 ▶ if($nm eq 'MegaBlastOutput') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nm eq "MegaBlastOutput") 1 178 179
323 1 $self->{'_values'} = {};
324 1 $self->{'_result'}= undef;
325 1 $self->{'_mode'} = '';
326 }
327  
328 }
329  
330 =head2 end_element
331  
332 Title : start_element
333 Usage : $eventgenerator->end_element
334 Function: Handles an end element event
335 Returns : none
336 Args : hashref with at least 2 keys 'Data' and 'Name'
337  
338  
339 =cut
340  
341 179 ▶sub end_element {
Subroutine end_element: called CC=6 SCAR=18.8
Pod documented
342 179 my ($self,$data) = @_;
343 179 my $nm = $data->{'Name'};
344 179 my $rc;
345  
346 179 ▶ if( my $type = $MODEMAP{$nm} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $type = $Bio::SearchIO::megablast::MODEMAP{$nm}) { } 26 153 179
elsif ($Bio::SearchIO::megablast::MAPPING{$nm}) { } 153 0 153
347 26 ▶ if( $self->_eventHandler->will_handle($type) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_eventHandler->will_handle($type)) 26 0 26
348 26 my $func = sprintf("end_%s",lc $type);
349 $rc = $self->_eventHandler->$func($self->{'_reporttype'},
350 26 $self->{'_values'});
351 }
352 26 shift @{$self->{'_elements'}};
353  
354 } elsif( $MAPPING{$nm} ) {
355 153 ▶ if ( ref($MAPPING{$nm}) =~ /hash/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($Bio::SearchIO::megablast::MAPPING{$nm}) =~ /hash/i) { } 0 153 153
356 0 my $key = (keys %{$MAPPING{$nm}})[0];
357 0 $self->{'_values'}->{$key}->{$MAPPING{$nm}->{$key}} = $self->{'_last_data'};
358 } else {
359 153 $self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
360 }
361 } else {
362 0 $self->warn( "unknown nm $nm ignoring\n");
363 }
364 179 $self->{'_last_data'} = ''; # remove read data if we are at
365 # end of an element
366 179 ▶ $self->{'_result'} = $rc if( $nm eq 'MegaBlastOutput' );
Branch: 1 branch
BranchTrueFalseTotal
if $nm eq "MegaBlastOutput" 1 178 179
367 179 return $rc;
368  
369 }
370  
371 =head2 element
372  
373 Title : element
374 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
375 Function: Convience method that calls start_element, characters, end_element
376 Returns : none
377 Args : Hash ref with the keys 'Name' and 'Data'
378  
379  
380 =cut
381  
382 153 ▶sub element{
Subroutine element: called CC=1 SCAR=0.0
Pod documented
383 153 my ($self,$data) = @_;
384 153 $self->start_element($data);
385 153 $self->characters($data);
386 153 $self->end_element($data);
387 }
388  
389  
390 =head2 characters
391  
392 Title : characters
393 Usage : $eventgenerator->characters($str)
394 Function: Send a character events
395 Returns : none
396 Args : string
397  
398  
399 =cut
400  
401 153 ▶sub characters{
Subroutine characters: called CC=2 SCAR=7.1
Pod documented
402 153 my ($self,$data) = @_;
403 153 ▶ return unless defined $data->{'Data'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{"Data"} 0 153 153
404 153 $self->{'_last_data'} = $data->{'Data'};
405 }
406  
407 =head2 _mode
408  
409 Title : _mode
410 Usage : $obj->_mode($newval)
411 Function:
412 Example :
413 Returns : value of _mode
414 Args : newvalue (optional)
415  
416  
417 =cut
418  
419 26 ▶sub _mode{
Subroutine _mode: called CC=2 SCAR=7.0
420 26 my ($self,$value) = @_;
421 26 ▶ if( defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 26 0 26
422 26 $self->{'_mode'} = $value;
423 }
424 26 return $self->{'_mode'};
425 }
426  
427 =head2 within_element
428  
429 Title : within_element
430 Usage : if( $eventgenerator->within_element($element) ) {}
431 Function: Test if we are within a particular element
432 This is different than 'in' because within can be tested
433 for a whole block.
434 Returns : boolean
435 Args : string element name
436  
437  
438 =cut
439  
440 0 ▶sub within_element{
Subroutine within_element: not called CC=6 SCAR=37.4
Pod documented
441 0 my ($self,$name) = @_;
442 return 0 if ( ! defined $name &&
443 ! defined $self->{'_elements'} ||
444 0 ▶ scalar @{$self->{'_elements'}} == 0) ;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name || defined $self->{"_elements"} or scalar @{$self->{"_elements"};} == 0 0 0 0
Condition0%0/6
not defined $name || defined $self->{"_elements"} or scalar @{$self->{"_elements"};} == 0
l!l&&r!l&&!r
000
defined $name || defined $self->{'_elements'}
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $name || defined $self->{"_elements"} or scalar @{$self->{"_elements"};} == 0
defined $name defined $self->{'_elements'} scalar @{$self->{"_elements"};} == 0
Truth table0%0/5
not(A || B) or C
A= defined $nameB= defined $self->{'_elements'}C= scalar @{$self->{"_elements"};} == 0
ABCresult
00X1
1X11
0111
1X00
0100
445 0 foreach ( @{$self->{'_elements'}} ) {
446 0 ▶ if( $_ eq $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $name) 0 0 0
447 0 return 1;
448 }
449 }
450 0 return 0;
451 }
452  
453 =head2 in_element
454  
455 Title : in_element
456 Usage : if( $eventgenerator->in_element($element) ) {}
457 Function: Test if we are in a particular element
458 This is different than 'in' because within can be tested
459 for a whole block.
460 Returns : boolean
461 Args : string element name
462  
463  
464 =cut
465  
466 0 ▶sub in_element{
Subroutine in_element: not called CC=2 SCAR=17.9
Pod documented
467 0 my ($self,$name) = @_;
468 0 ▶ return 0 if ! defined $self->{'_elements'}->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_elements"}[0] 0 0 0
469 0 return ( $self->{'_elements'}->[0] eq $name)
470 }
471  
472  
473 =head2 start_document
474  
475 Title : start_document
476 Usage : $eventgenerator->start_document
477 Function: Handles a start document event
478 Returns : none
479 Args : none
480  
481  
482 =cut
483  
484 0 ▶sub start_document{
Subroutine start_document: not called CC=1 SCAR=6.9
Pod documented
485 0 my ($self) = @_;
486 0 $self->{'_lasttype'} = '';
487 0 $self->{'_values'} = {};
488 0 $self->{'_result'}= undef;
489 0 $self->{'_mode'} = '';
490 0 $self->{'_elements'} = [];
491 }
492  
493  
494 =head2 end_document
495  
496 Title : end_document
497 Usage : $eventgenerator->end_document
498 Function: Handles an end document event
499 Returns : Bio::Search::Result::ResultI object
500 Args : none
501  
502  
503 =cut
504  
505 1 ▶sub end_document{
Subroutine end_document: called CC=1 SCAR=0.0
Pod documented
506 1 my ($self,@args) = @_;
507 1 return $self->{'_result'};
508 }
509  
510 =head2 result_count
511  
512 Title : result_count
513 Usage : my $count = $searchio->result_count
514 Function: Returns the number of results we have processed
515 Returns : integer
516 Args : none
517  
518  
519 =cut
520  
521 0 ▶sub result_count {
Subroutine result_count: not called CC=1 SCAR=6.9
Pod documented
522 0 my $self = shift;
523 0 return $self->{'_result_count'};
524 }
525  
526 0 ▶sub report_count { shift->result_count }
Subroutine report_count: not called CC=1 SCAR=6.9
Pod undocumented
527  
528 2 1;
« Bio/Tools/Est2Genome.pm ↑ Summary Bio/Index/AbstractSeq.pm »