3 # BioPerl module for Bio::SearchIO::blasttable
5 # Cared for by Jason Stajich <jason-at-bioperl-dot-org>
7 # Copyright Jason Stajich
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
15 Bio::SearchIO::blasttable - Driver module for SearchIO for parsing NCBI -m 8/9 format
19 # do not use this module directly
21 my $parser = Bio::SearchIO->new(-file => $file,
22 -format => 'blasttable');
24 while( my $result = $parser->next_result ) {
29 This module will support parsing NCBI -m 8 or -m 9 tabular output
30 and WU-BLAST -mformat 2 or -mformat 3 tabular output.
36 User feedback is an integral part of the evolution of this and other
37 Bioperl modules. Send your comments and suggestions preferably to
38 the Bioperl mailing list. Your participation is much appreciated.
40 bioperl-l@bioperl.org - General discussion
41 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
45 Report bugs to the Bioperl bug tracking system to help us keep track
46 of the bugs and their resolution. Bug reports can be submitted via
49 http://bugzilla.open-bio.org/
51 =head1 AUTHOR - Jason Stajich
53 Email jason-at-bioperl-dot-org
57 The rest of the documentation details each of the object methods.
58 Internal methods are usually preceded with a _
62 # Let the code begin...
65 package Bio
::SearchIO
::blasttable
;
66 use vars
qw(%MAPPING %MODEMAP $DEFAULT_WRITER_CLASS $DefaultProgramName);
68 use Bio::Search::Result::ResultFactory;
69 use Bio::Search::Hit::HitFactory;
70 use Bio::Search::HSP::HSPFactory;
72 $DefaultProgramName = 'BLASTN';
73 $DEFAULT_WRITER_CLASS = 'Bio::Search::Writer::HitTableWriter';
75 # mapping of terms to Bioperl hash keys
83 'Hsp_bit-score' => 'HSP-bits',
84 'Hsp_score' => 'HSP-score',
85 'Hsp_evalue' => 'HSP-evalue',
86 'Hsp_query-from' => 'HSP-query_start',
87 'Hsp_query-to' => 'HSP-query_end',
88 'Hsp_hit-from' => 'HSP-hit_start',
89 'Hsp_hit-to' => 'HSP-hit_end',
90 'Hsp_positive' => 'HSP-conserved',
91 'Hsp_identity' => 'HSP-identical',
92 'Hsp_mismatches' => 'HSP-mismatches',
93 'Hsp_qgapblocks' => 'HSP-query_gapblocks',
94 'Hsp_hgapblocks' => 'HSP-hit_gapblocks',
95 'Hsp_gaps' => 'HSP-hsp_gaps',
96 'Hsp_hitgaps' => 'HSP-hit_gaps',
97 'Hsp_querygaps' => 'HSP-query_gaps',
98 'Hsp_align-len' => 'HSP-hsp_length',
99 'Hsp_query-frame'=> 'HSP-query_frame',
100 'Hsp_hit-frame' => 'HSP-hit_frame',
102 'Hit_id' => 'HIT-name',
103 'Hit_len' => 'HIT-length',
104 'Hit_accession' => 'HIT-accession',
105 'Hit_def' => 'HIT-description',
106 'Hit_signif' => 'HIT-significance',
107 'Hit_score' => 'HIT-score',
108 'Hit_bits' => 'HIT-bits',
110 'Result_program' => 'RESULT-algorithm_name',
111 'Result_version' => 'RESULT-algorithm_version',
112 'Result_query-def'=> 'RESULT-query_name',
113 'Result_query-len'=> 'RESULT-query_length',
114 'Result_query-acc'=> 'RESULT-query_accession',
115 'Result_querydesc'=> 'RESULT-query_description',
116 'Result_db' => 'RESULT-database_name',
117 'Result_db-len' => 'RESULT-database_entries',
118 'Result_db-let' => 'RESULT-database_letters',
121 use base qw(Bio::SearchIO);
126 Usage : my $obj = Bio::SearchIO::blasttable->new();
127 Function: Builds a new Bio::SearchIO::blasttable object
128 Returns : an instance of Bio::SearchIO::blasttable
135 my ($self,@args) = @_;
136 $self->SUPER::_initialize
(@args);
138 my ($pname) = $self->_rearrange([qw(PROGRAM_NAME)],
140 $self->program_name($pname || $DefaultProgramName);
141 $self->_eventHandler->register_factory('result', Bio
::Search
::Result
::ResultFactory
->new(-type
=> 'Bio::Search::Result::GenericResult'));
142 $self->_eventHandler->register_factory('hit', Bio
::Search
::Hit
::HitFactory
->new(-type
=> 'Bio::Search::Hit::GenericHit'));
143 $self->_eventHandler->register_factory('hsp', Bio
::Search
::HSP
::HSPFactory
->new(-type
=> 'Bio::Search::HSP::GenericHSP'));
150 Usage : my $result = $parser->next_result
151 Function: Parse the next result from the data stream
152 Returns : L<Bio::Search::Result::ResultI>
160 my ($lastquery,$lasthit);
164 while( defined ($_ = $self->_readline) ) {
165 # WU-BLAST -mformat 3 only
166 if(m{^#\s((?:\S+?)?BLAST[NPX])\s(\d+\.\d+.+\d{4}\
])}) {
167 ($alg, $ver) = ($1, $2);
168 # only one header for whole file with WU-BLAST
169 # so $alg and $ver won't get set properly for
171 $self->program_name($alg) if $alg;
172 $self->element({'Name' => 'Result_version',
173 'Data' => $ver}) if $ver;
177 elsif(m{^#\s+((?:\S+?)?BLAST[NPX])\s+(.+)}) {
178 ($alg, $ver) = ($1, $2);
181 next if /^\#/ || /^\s+$/;
184 my ($qname,$hname, $percent_id, $hsp_len, $mismatches,$gapsm,
185 $qstart,$qend,$hstart,$hend,$evalue,$bits);
187 my ($num_scores, $raw_score, $identities, $positives, $percent_pos,
188 $qgap_blocks,$qgaps, $sgap_blocks, $sgaps, $qframe,
192 ($qname,$hname, $percent_id, $hsp_len, $mismatches,$gapsm,
193 $qstart,$qend,$hstart,$hend,$evalue,$bits) = @fields;
195 # WU-BLAST -mformat 2 and 3
196 elsif ((@fields == 22) or (@fields == 24)) {
197 ($qname,$hname,$evalue,$num_scores, $bits, $raw_score, $hsp_len,
198 $identities, $positives,$mismatches, $percent_id, $percent_pos,
199 $qgap_blocks, $qgaps, $sgap_blocks, $sgaps, $qframe, $qstart,
200 $qend, $sframe, $hstart,$hend,) = @fields;
201 # we need total gaps in the alignment
202 $gapsm=$qgaps+$sgaps;
206 # Remember Jim's code is 0 based
207 if( defined $lastquery &&
208 $lastquery ne $qname ) {
209 $self->end_element({'Name' => 'Hit'});
210 $self->end_element({'Name' => 'Result'});
211 $self->_pushback($_);
212 return $self->end_document;
213 } elsif( ! defined $lastquery ) {
214 $self->{'_result_count'}++;
215 $self->start_element({'Name' => 'Result'});
216 $self->element({'Name' => 'Result_program',
217 'Data' => $alg || $self->program_name});
218 $self->element({'Name' => 'Result_version',
219 'Data' => $ver}) if $ver;
220 $self->element({'Name' => 'Result_query-def',
222 $self->start_element({'Name' => 'Hit'});
223 $self->element({'Name' => 'Hit_id',
225 # we'll store the 1st hsp bits as the hit bits
226 $self->element({'Name' => 'Hit_bits',
228 # we'll store the 1st hsp value as the hit evalue
229 $self->element({'Name' => 'Hit_signif',
232 } elsif( $lasthit ne $hname ) {
233 if( $self->in_element('hit') ) {
234 $self->end_element({'Name' => 'Hit'});
236 $self->start_element({'Name' => 'Hit'});
237 $self->element({'Name' => 'Hit_id',
239 # we'll store the 1st hsp bits as the hit bits
240 $self->element({'Name' => 'Hit_bits',
242 # we'll store the 1st hsp value as the hit evalue
243 $self->element({'Name' => 'Hit_signif',
246 my $identical = $hsp_len - $mismatches - $gapsm;
247 $self->start_element({'Name' => 'Hsp'});
248 $self->element({'Name' => 'Hsp_evalue',
250 $self->element({'Name' => 'Hsp_bit-score',
252 $self->element({'Name' => 'Hsp_identity',
253 'Data' => $identical});
254 $self->element({'Name' => 'Hsp_positive',
255 'Data' => $identical});
256 $self->element({'Name' => 'Hsp_gaps',
258 $self->element({'Name' => 'Hsp_query-from',
260 $self->element({'Name' => 'Hsp_query-to',
263 $self->element({'Name' => 'Hsp_hit-from',
264 'Data' => $hstart });
265 $self->element({'Name' => 'Hsp_hit-to',
267 $self->element({'Name' => 'Hsp_align-len',
268 'Data' => $hsp_len});
269 $self->end_element({'Name' => 'Hsp'});
274 if( defined $lasthit && defined $lastquery ) {
275 if( $self->in_element('hit') ) {
276 $self->end_element({'Name' => 'Hit'});
278 $self->end_element({'Name' => 'Result'});
279 return $self->end_document;
285 Title : start_element
286 Usage : $eventgenerator->start_element
287 Function: Handles a start element event
289 Args : hashref with at least 2 keys 'Data' and 'Name'
295 my ($self,$data) = @_;
296 # we currently don't care about attributes
297 my $nm = $data->{'Name'};
298 if( my $type = $MODEMAP{$nm} ) {
300 if( $self->_will_handle($type) ) {
301 my $func = sprintf("start_%s",lc $type);
302 $self->_eventHandler->$func($data->{'Attributes'});
304 unshift @
{$self->{'_elements'}}, $type;
306 if($nm eq 'Result') {
307 $self->{'_values'} = {};
308 $self->{'_result'}= undef;
309 $self->{'_mode'} = '';
316 Title : start_element
317 Usage : $eventgenerator->end_element
318 Function: Handles an end element event
320 Args : hashref with at least 2 keys 'Data' and 'Name'
326 my ($self,$data) = @_;
327 my $nm = $data->{'Name'};
329 # Hsp are sort of weird, in that they end when another
330 # object begins so have to detect this in end_element for now
332 if( my $type = $MODEMAP{$nm} ) {
333 if( $self->_will_handle($type) ) {
334 my $func = sprintf("end_%s",lc $type);
335 $rc = $self->_eventHandler->$func($self->{'_reporttype'},
338 shift @
{$self->{'_elements'}};
340 } elsif( $MAPPING{$nm} ) {
341 if ( ref($MAPPING{$nm}) =~ /hash/i ) {
342 my $key = (keys %{$MAPPING{$nm}})[0];
343 $self->{'_values'}->{$key}->{$MAPPING{$nm}->{$key}} = $self->{'_last_data'};
345 $self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
348 $self->warn( "unknown nm $nm ignoring\n");
350 $self->{'_last_data'} = ''; # remove read data if we are at
352 $self->{'_result'} = $rc if( $nm eq 'Result' );
360 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
361 Function: Convience method that calls start_element, characters, end_element
363 Args : Hash ref with the keys 'Name' and 'Data'
369 my ($self,$data) = @_;
370 $self->start_element($data);
371 $self->characters($data);
372 $self->end_element($data);
379 Usage : $eventgenerator->characters($str)
380 Function: Send a character events
388 my ($self,$data) = @_;
390 return unless ( defined $data->{'Data'} );
391 if( $data->{'Data'} =~ /^\s+$/ ) {
392 return unless $data->{'Name'} =~ /Hsp\_(midline|qseq|hseq)/;
395 if( $self->in_element('hsp') &&
396 $data->{'Name'} =~ /Hsp\_(qseq|hseq|midline)/ ) {
398 $self->{'_last_hspdata'}->{$data->{'Name'}} .= $data->{'Data'};
401 $self->{'_last_data'} = $data->{'Data'};
407 Usage : $obj->_mode($newval)
410 Returns : value of _mode
411 Args : newvalue (optional)
417 my ($self,$value) = @_;
418 if( defined $value) {
419 $self->{'_mode'} = $value;
421 return $self->{'_mode'};
424 =head2 within_element
426 Title : within_element
427 Usage : if( $eventgenerator->within_element($element) ) {}
428 Function: Test if we are within a particular element
429 This is different than 'in' because within can be tested
432 Args : string element name
438 my ($self,$name) = @_;
439 return 0 if ( ! defined $name &&
440 ! defined $self->{'_elements'} ||
441 scalar @
{$self->{'_elements'}} == 0) ;
442 foreach ( @
{$self->{'_elements'}} ) {
453 Usage : if( $eventgenerator->in_element($element) ) {}
454 Function: Test if we are in a particular element
455 This is different than 'in' because within can be tested
458 Args : string element name
464 my ($self,$name) = @_;
465 return 0 if ! defined $self->{'_elements'}->[0];
466 return ( $self->{'_elements'}->[0] eq $name)
470 =head2 start_document
472 Title : start_document
473 Usage : $eventgenerator->start_document
474 Function: Handles a start document event
483 $self->{'_lasttype'} = '';
484 $self->{'_values'} = {};
485 $self->{'_result'}= undef;
486 $self->{'_mode'} = '';
487 $self->{'_elements'} = [];
494 Usage : $eventgenerator->end_document
495 Function: Handles an end document event
496 Returns : Bio::Search::Result::ResultI object
503 my ($self,@args) = @_;
504 return $self->{'_result'};
510 Usage : my $count = $searchio->result_count
511 Function: Returns the number of results we have processed
520 return $self->{'_result_count'};
523 sub report_count
{ shift->result_count }
529 Usage : $obj->program_name($newval)
530 Function: Get/Set the program name
531 Returns : value of program_name (a scalar)
532 Args : on set, new value (a scalar or undef, optional)
540 $self->{'program_name'} = shift if @_;
541 return $self->{'program_name'} || $DefaultProgramName;
548 Usage : Private method. For internal use only.
549 if( $self->_will_handle($type) ) { ... }
550 Function: Provides an optimized way to check whether or not an element of a
551 given type is to be handled.
552 Returns : Reference to EventHandler object if the element type is to be handled.
553 undef if the element type is not to be handled.
554 Args : string containing type of element.
562 Using the cached pointer to the EventHandler to minimize repeated
567 Caching the will_handle status for each type that is encountered so
568 that it only need be checked by calling
569 handler-E<gt>will_handle($type) once.
573 This does not lead to a major savings by itself (only 5-10%). In
574 combination with other optimizations, or for large parse jobs, the
575 savings good be significant.
577 To test against the unoptimized version, remove the parentheses from
578 around the third term in the ternary " ? : " operator and add two
579 calls to $self-E<gt>_eventHandler().
584 my ($self,$type) = @_;
585 my $handler = $self->{'_handler'};
586 my $will_handle = defined($self->{'_will_handle_cache'}->{$type})
587 ?
$self->{'_will_handle_cache'}->{$type}
588 : ($self->{'_will_handle_cache'}->{$type} =
589 $handler->will_handle($type));
591 return $will_handle ?
$handler : undef;