2 # BioPerl module for Bio::SearchIO::blasttable
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Jason Stajich <jason-at-bioperl-dot-org>
8 # Copyright Jason Stajich
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::SearchIO::blasttable - Driver module for SearchIO for parsing NCBI -m 8/9 format
20 # do not use this module directly
22 my $parser = Bio::SearchIO->new(-file => $file,
23 -format => 'blasttable');
25 while( my $result = $parser->next_result ) {
30 This module will support parsing NCBI -m 8 or -m 9 tabular output
31 and WU-BLAST -mformat 2 or -mformat 3 tabular output.
37 User feedback is an integral part of the evolution of this and other
38 Bioperl modules. Send your comments and suggestions preferably to
39 the Bioperl mailing list. Your participation is much appreciated.
41 bioperl-l@bioperl.org - General discussion
42 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
46 Please direct usage questions or support issues to the mailing list:
48 I<bioperl-l@bioperl.org>
50 rather than to the module maintainer directly. Many experienced and
51 reponsive experts will be able look at the problem and quickly
52 address it. Please include a thorough description of the problem
53 with code and data examples if at all possible.
57 Report bugs to the Bioperl bug tracking system to help us keep track
58 of the bugs and their resolution. Bug reports can be submitted via
61 https://github.com/bioperl/bioperl-live/issues
63 =head1 AUTHOR - Jason Stajich
65 Email jason-at-bioperl-dot-org
69 The rest of the documentation details each of the object methods.
70 Internal methods are usually preceded with a _
74 # Let the code begin...
77 package Bio
::SearchIO
::blasttable
;
78 use vars
qw(%MAPPING %MODEMAP $DEFAULT_WRITER_CLASS $DefaultProgramName);
80 use Bio::Search::Result::ResultFactory;
81 use Bio::Search::Hit::HitFactory;
82 use Bio::Search::HSP::HSPFactory;
84 $DefaultProgramName = 'BLASTN';
85 $DEFAULT_WRITER_CLASS = 'Bio::SearchIO::Writer::HitTableWriter';
87 # mapping of terms to Bioperl hash keys
95 'Hsp_bit-score' => 'HSP-bits',
96 'Hsp_score' => 'HSP-score',
97 'Hsp_evalue' => 'HSP-evalue',
98 'Hsp_query-from' => 'HSP-query_start',
99 'Hsp_query-to' => 'HSP-query_end',
100 'Hsp_hit-from' => 'HSP-hit_start',
101 'Hsp_hit-to' => 'HSP-hit_end',
102 'Hsp_positive' => 'HSP-conserved',
103 'Hsp_identity' => 'HSP-identical',
104 'Hsp_mismatches' => 'HSP-mismatches',
105 'Hsp_qgapblocks' => 'HSP-query_gapblocks',
106 'Hsp_hgapblocks' => 'HSP-hit_gapblocks',
107 'Hsp_gaps' => 'HSP-hsp_gaps',
108 'Hsp_hitgaps' => 'HSP-hit_gaps',
109 'Hsp_querygaps' => 'HSP-query_gaps',
110 'Hsp_align-len' => 'HSP-hsp_length',
111 'Hsp_query-frame'=> 'HSP-query_frame',
112 'Hsp_hit-frame' => 'HSP-hit_frame',
114 'Hit_id' => 'HIT-name',
115 'Hit_len' => 'HIT-length',
116 'Hit_accession' => 'HIT-accession',
117 'Hit_def' => 'HIT-description',
118 'Hit_signif' => 'HIT-significance',
119 'Hit_score' => 'HIT-score',
120 'Hit_bits' => 'HIT-bits',
122 'Result_program' => 'RESULT-algorithm_name',
123 'Result_version' => 'RESULT-algorithm_version',
124 'Result_query-def'=> 'RESULT-query_name',
125 'Result_query-len'=> 'RESULT-query_length',
126 'Result_query-acc'=> 'RESULT-query_accession',
127 'Result_querydesc'=> 'RESULT-query_description',
128 'Result_db' => 'RESULT-database_name',
129 'Result_db-len' => 'RESULT-database_entries',
130 'Result_db-let' => 'RESULT-database_letters',
133 use base qw(Bio::SearchIO);
138 Usage : my $obj = Bio::SearchIO::blasttable->new();
139 Function: Builds a new Bio::SearchIO::blasttable object
140 Returns : an instance of Bio::SearchIO::blasttable
147 my ($self,@args) = @_;
148 $self->SUPER::_initialize
(@args);
150 my ($pname) = $self->_rearrange([qw(PROGRAM_NAME)],
152 $self->program_name($pname || $DefaultProgramName);
153 $self->_eventHandler->register_factory('result', Bio
::Search
::Result
::ResultFactory
->new(-type
=> 'Bio::Search::Result::GenericResult'));
154 $self->_eventHandler->register_factory('hit', Bio
::Search
::Hit
::HitFactory
->new(-type
=> 'Bio::Search::Hit::GenericHit'));
155 $self->_eventHandler->register_factory('hsp', Bio
::Search
::HSP
::HSPFactory
->new(-type
=> 'Bio::Search::HSP::GenericHSP'));
162 Usage : my $result = $parser->next_result
163 Function: Parse the next result from the data stream
164 Returns : L<Bio::Search::Result::ResultI>
172 my ($lastquery,$lasthit);
176 while( defined ($_ = $self->_readline) ) {
177 # WU-BLAST -mformat 3 only
178 if(m{^#\s((?:\S+?)?BLAST[NPX])\s(\d+\.\d+.+\d{4}\
])}) {
179 ($alg, $ver) = ($1, $2);
180 # only one header for whole file with WU-BLAST
181 # so $alg and $ver won't get set properly for
183 $self->program_name($alg) if $alg;
184 $self->element({'Name' => 'Result_version',
185 'Data' => $ver}) if $ver;
189 elsif(m{^#\s+((?:\S+?)?BLAST[NPX])\s+(.+)}) {
190 ($alg, $ver) = ($1, $2);
193 next if /^#/ || /^\s*$/;
196 next if @fields == 1;
197 my ($qname,$hname, $percent_id, $hsp_len, $mismatches,$gapsm,
198 $qstart,$qend,$hstart,$hend,$evalue,$bits);
200 my ($num_scores, $raw_score, $identities, $positives, $percent_pos,
201 $qgap_blocks,$qgaps, $sgap_blocks, $sgaps, $qframe,
205 ($qname,$hname, $percent_id, $hsp_len, $mismatches,$gapsm,
206 $qstart,$qend,$hstart,$hend,$evalue,$bits) = @fields;
207 # NCBI -m8 and -m9, v 2.2.18+
208 } elsif (@fields == 13) {
209 ($qname, $hname, $percent_id, $percent_pos, $hsp_len, $mismatches, $gapsm,
210 $qstart,$qend,$hstart,$hend,$evalue,$bits) = @fields;
212 # WU-BLAST -mformat 2 and 3
213 elsif ((@fields == 22) or (@fields == 24)) {
214 ($qname,$hname,$evalue,$num_scores, $bits, $raw_score, $hsp_len,
215 $identities, $positives,$mismatches, $percent_id, $percent_pos,
216 $qgap_blocks, $qgaps, $sgap_blocks, $sgaps, $qframe, $qstart,
217 $qend, $sframe, $hstart,$hend,) = @fields;
218 # we need total gaps in the alignment
219 $gapsm=$qgaps+$sgaps;
222 if (@fields == 12 || @fields == 13) {
223 # need to determine total gaps in the alignment for NCBI output
224 # since NCBI reports number of gapopens and NOT total gaps
225 my $qlen = abs($qstart - $qend) + 1;
226 my $querygaps = $hsp_len - $qlen;
227 my $hlen = abs($hstart - $hend) + 1;
228 my $hitgaps = $hsp_len - $hlen;
229 $gapsm = $querygaps + $hitgaps;
232 # Remember Jim's code is 0 based
233 if( defined $lastquery &&
234 $lastquery ne $qname ) {
235 $self->end_element({'Name' => 'Hit'});
236 $self->end_element({'Name' => 'Result'});
237 $self->_pushback($_);
238 return $self->end_document;
239 } elsif( ! defined $lastquery ) {
240 $self->{'_result_count'}++;
241 $self->start_element({'Name' => 'Result'});
242 $self->element({'Name' => 'Result_program',
243 'Data' => $alg || $self->program_name});
244 $self->element({'Name' => 'Result_version',
245 'Data' => $ver}) if $ver;
246 $self->element({'Name' => 'Result_query-def',
248 $self->start_element({'Name' => 'Hit'});
249 $self->element({'Name' => 'Hit_id',
251 # we'll store the 1st hsp bits as the hit bits
252 $self->element({'Name' => 'Hit_bits',
254 # we'll store the 1st hsp value as the hit evalue
255 $self->element({'Name' => 'Hit_signif',
258 } elsif( $lasthit ne $hname ) {
259 if( $self->in_element('hit') ) {
260 $self->end_element({'Name' => 'Hit'});
262 $self->start_element({'Name' => 'Hit'});
263 $self->element({'Name' => 'Hit_id',
265 # we'll store the 1st hsp bits as the hit bits
266 $self->element({'Name' => 'Hit_bits',
268 # we'll store the 1st hsp value as the hit evalue
269 $self->element({'Name' => 'Hit_signif',
272 my $identical = $hsp_len - $mismatches - $gapsm;
273 # If $positives value is absent, try to recover it from $percent_pos,
274 # this is better than letting the program to assume "conserved == identical"
275 if (not defined $positives and defined $percent_pos) {
276 $positives = sprintf "%d", ($percent_pos * $hsp_len / 100);
278 $self->start_element({'Name' => 'Hsp'});
279 $self->element({'Name' => 'Hsp_evalue',
281 $self->element({'Name' => 'Hsp_bit-score',
283 $self->element({'Name' => 'Hsp_identity',
284 'Data' => $identical});
285 $self->element({'Name' => 'Hsp_positive',
286 'Data' => $positives});
287 $self->element({'Name' => 'Hsp_gaps',
289 $self->element({'Name' => 'Hsp_query-from',
291 $self->element({'Name' => 'Hsp_query-to',
294 $self->element({'Name' => 'Hsp_hit-from',
295 'Data' => $hstart });
296 $self->element({'Name' => 'Hsp_hit-to',
298 $self->element({'Name' => 'Hsp_align-len',
299 'Data' => $hsp_len});
300 $self->end_element({'Name' => 'Hsp'});
305 if( defined $lasthit && defined $lastquery ) {
306 if( $self->in_element('hit') ) {
307 $self->end_element({'Name' => 'Hit'});
309 $self->end_element({'Name' => 'Result'});
310 return $self->end_document;
316 Title : start_element
317 Usage : $eventgenerator->start_element
318 Function: Handles a start element event
320 Args : hashref with at least 2 keys 'Data' and 'Name'
326 my ($self,$data) = @_;
327 # we currently don't care about attributes
328 my $nm = $data->{'Name'};
329 if( my $type = $MODEMAP{$nm} ) {
331 if( $self->_will_handle($type) ) {
332 my $func = sprintf("start_%s",lc $type);
333 $self->_eventHandler->$func($data->{'Attributes'});
335 unshift @
{$self->{'_elements'}}, $type;
337 if($nm eq 'Result') {
338 $self->{'_values'} = {};
339 $self->{'_result'}= undef;
340 $self->{'_mode'} = '';
347 Title : start_element
348 Usage : $eventgenerator->end_element
349 Function: Handles an end element event
351 Args : hashref with at least 2 keys 'Data' and 'Name'
357 my ($self,$data) = @_;
358 my $nm = $data->{'Name'};
360 # Hsp are sort of weird, in that they end when another
361 # object begins so have to detect this in end_element for now
363 if( my $type = $MODEMAP{$nm} ) {
364 if( $self->_will_handle($type) ) {
365 my $func = sprintf("end_%s",lc $type);
366 $rc = $self->_eventHandler->$func($self->{'_reporttype'},
369 shift @
{$self->{'_elements'}};
371 } elsif( $MAPPING{$nm} ) {
372 if ( ref($MAPPING{$nm}) =~ /hash/i ) {
373 my $key = (keys %{$MAPPING{$nm}})[0];
374 $self->{'_values'}->{$key}->{$MAPPING{$nm}->{$key}} = $self->{'_last_data'};
376 $self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
379 $self->warn( "unknown nm $nm ignoring\n");
381 $self->{'_last_data'} = ''; # remove read data if we are at
383 $self->{'_result'} = $rc if( $nm eq 'Result' );
391 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
392 Function: Convience method that calls start_element, characters, end_element
394 Args : Hash ref with the keys 'Name' and 'Data'
400 my ($self,$data) = @_;
401 $self->start_element($data);
402 $self->characters($data);
403 $self->end_element($data);
410 Usage : $eventgenerator->characters($str)
411 Function: Send a character events
419 my ($self,$data) = @_;
421 # deep bug fix: set $self->{'_last_data'} to undef if $$data{Data} is
422 # a valid slot, whose value is undef --
423 # allows an undef to be propagated to object constructors and
424 # handled there as desired; in particular, when Hsp_postive => -conserved
425 # is not defined (in BLASTN, e.g.), the value of hsp's {CONSERVED} property is
426 # set to the value of {IDENTICAL}.
428 # return unless ( defined $data->{'Data'} );
429 return unless ( grep /Data/, keys %$data );
430 if ( !defined $data->{'Data'} ) {
431 $self->{'_last_data'} = undef;
434 if( $data->{'Data'} =~ /^\s+$/ ) {
435 return unless $data->{'Name'} =~ /Hsp\_(midline|qseq|hseq)/;
438 if( $self->in_element('hsp') &&
439 $data->{'Name'} =~ /Hsp\_(qseq|hseq|midline)/ ) {
441 $self->{'_last_hspdata'}->{$data->{'Name'}} .= $data->{'Data'};
444 $self->{'_last_data'} = $data->{'Data'};
450 Usage : $obj->_mode($newval)
453 Returns : value of _mode
454 Args : newvalue (optional)
460 my ($self,$value) = @_;
461 if( defined $value) {
462 $self->{'_mode'} = $value;
464 return $self->{'_mode'};
467 =head2 within_element
469 Title : within_element
470 Usage : if( $eventgenerator->within_element($element) ) {}
471 Function: Test if we are within a particular element
472 This is different than 'in' because within can be tested
475 Args : string element name
481 my ($self,$name) = @_;
482 return 0 if ( ! defined $name &&
483 ! defined $self->{'_elements'} ||
484 scalar @
{$self->{'_elements'}} == 0) ;
485 foreach ( @
{$self->{'_elements'}} ) {
496 Usage : if( $eventgenerator->in_element($element) ) {}
497 Function: Test if we are in a particular element
498 This is different than 'in' because within can be tested
501 Args : string element name
507 my ($self,$name) = @_;
508 return 0 if ! defined $self->{'_elements'}->[0];
509 return ( $self->{'_elements'}->[0] eq $name)
513 =head2 start_document
515 Title : start_document
516 Usage : $eventgenerator->start_document
517 Function: Handles a start document event
526 $self->{'_lasttype'} = '';
527 $self->{'_values'} = {};
528 $self->{'_result'}= undef;
529 $self->{'_mode'} = '';
530 $self->{'_elements'} = [];
537 Usage : $eventgenerator->end_document
538 Function: Handles an end document event
539 Returns : Bio::Search::Result::ResultI object
546 my ($self,@args) = @_;
547 return $self->{'_result'};
553 Usage : my $count = $searchio->result_count
554 Function: Returns the number of results we have processed
563 return $self->{'_result_count'};
566 sub report_count
{ shift->result_count }
572 Usage : $obj->program_name($newval)
573 Function: Get/Set the program name
574 Returns : value of program_name (a scalar)
575 Args : on set, new value (a scalar or undef, optional)
583 $self->{'program_name'} = shift if @_;
584 return $self->{'program_name'} || $DefaultProgramName;
591 Usage : Private method. For internal use only.
592 if( $self->_will_handle($type) ) { ... }
593 Function: Provides an optimized way to check whether or not an element of a
594 given type is to be handled.
595 Returns : Reference to EventHandler object if the element type is to be handled.
596 undef if the element type is not to be handled.
597 Args : string containing type of element.
605 Using the cached pointer to the EventHandler to minimize repeated
610 Caching the will_handle status for each type that is encountered so
611 that it only need be checked by calling
612 handler-E<gt>will_handle($type) once.
616 This does not lead to a major savings by itself (only 5-10%). In
617 combination with other optimizations, or for large parse jobs, the
618 savings good be significant.
620 To test against the unoptimized version, remove the parentheses from
621 around the third term in the ternary " ? : " operator and add two
622 calls to $self-E<gt>_eventHandler().
627 my ($self,$type) = @_;
628 my $handler = $self->{'_handler'};
629 my $will_handle = defined($self->{'_will_handle_cache'}->{$type})
630 ?
$self->{'_will_handle_cache'}->{$type}
631 : ($self->{'_will_handle_cache'}->{$type} =
632 $handler->will_handle($type));
634 return $will_handle ?
$handler : undef;