3 # BioPerl module for Bio::SearchIO::blasttable
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Jason Stajich <jason-at-bioperl-dot-org>
9 # Copyright Jason Stajich
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
17 Bio::SearchIO::blasttable - Driver module for SearchIO for parsing NCBI -m 8/9 format
21 # do not use this module directly
23 my $parser = Bio::SearchIO->new(-file => $file,
24 -format => 'blasttable');
26 while( my $result = $parser->next_result ) {
31 This module will support parsing NCBI -m 8 or -m 9 tabular output
32 and WU-BLAST -mformat 2 or -mformat 3 tabular output.
38 User feedback is an integral part of the evolution of this and other
39 Bioperl modules. Send your comments and suggestions preferably to
40 the Bioperl mailing list. Your participation is much appreciated.
42 bioperl-l@bioperl.org - General discussion
43 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
47 Please direct usage questions or support issues to the mailing list:
49 L<bioperl-l@bioperl.org>
51 rather than to the module maintainer directly. Many experienced and
52 reponsive experts will be able look at the problem and quickly
53 address it. Please include a thorough description of the problem
54 with code and data examples if at all possible.
58 Report bugs to the Bioperl bug tracking system to help us keep track
59 of the bugs and their resolution. Bug reports can be submitted via
62 http://bugzilla.open-bio.org/
64 =head1 AUTHOR - Jason Stajich
66 Email jason-at-bioperl-dot-org
70 The rest of the documentation details each of the object methods.
71 Internal methods are usually preceded with a _
75 # Let the code begin...
78 package Bio
::SearchIO
::blasttable
;
79 use vars
qw(%MAPPING %MODEMAP $DEFAULT_WRITER_CLASS $DefaultProgramName);
81 use Bio::Search::Result::ResultFactory;
82 use Bio::Search::Hit::HitFactory;
83 use Bio::Search::HSP::HSPFactory;
85 $DefaultProgramName = 'BLASTN';
86 $DEFAULT_WRITER_CLASS = 'Bio::Search::Writer::HitTableWriter';
88 # mapping of terms to Bioperl hash keys
96 'Hsp_bit-score' => 'HSP-bits',
97 'Hsp_score' => 'HSP-score',
98 'Hsp_evalue' => 'HSP-evalue',
99 'Hsp_query-from' => 'HSP-query_start',
100 'Hsp_query-to' => 'HSP-query_end',
101 'Hsp_hit-from' => 'HSP-hit_start',
102 'Hsp_hit-to' => 'HSP-hit_end',
103 'Hsp_positive' => 'HSP-conserved',
104 'Hsp_identity' => 'HSP-identical',
105 'Hsp_mismatches' => 'HSP-mismatches',
106 'Hsp_qgapblocks' => 'HSP-query_gapblocks',
107 'Hsp_hgapblocks' => 'HSP-hit_gapblocks',
108 'Hsp_gaps' => 'HSP-hsp_gaps',
109 'Hsp_hitgaps' => 'HSP-hit_gaps',
110 'Hsp_querygaps' => 'HSP-query_gaps',
111 'Hsp_align-len' => 'HSP-hsp_length',
112 'Hsp_query-frame'=> 'HSP-query_frame',
113 'Hsp_hit-frame' => 'HSP-hit_frame',
115 'Hit_id' => 'HIT-name',
116 'Hit_len' => 'HIT-length',
117 'Hit_accession' => 'HIT-accession',
118 'Hit_def' => 'HIT-description',
119 'Hit_signif' => 'HIT-significance',
120 'Hit_score' => 'HIT-score',
121 'Hit_bits' => 'HIT-bits',
123 'Result_program' => 'RESULT-algorithm_name',
124 'Result_version' => 'RESULT-algorithm_version',
125 'Result_query-def'=> 'RESULT-query_name',
126 'Result_query-len'=> 'RESULT-query_length',
127 'Result_query-acc'=> 'RESULT-query_accession',
128 'Result_querydesc'=> 'RESULT-query_description',
129 'Result_db' => 'RESULT-database_name',
130 'Result_db-len' => 'RESULT-database_entries',
131 'Result_db-let' => 'RESULT-database_letters',
134 use base qw(Bio::SearchIO);
139 Usage : my $obj = Bio::SearchIO::blasttable->new();
140 Function: Builds a new Bio::SearchIO::blasttable object
141 Returns : an instance of Bio::SearchIO::blasttable
148 my ($self,@args) = @_;
149 $self->SUPER::_initialize
(@args);
151 my ($pname) = $self->_rearrange([qw(PROGRAM_NAME)],
153 $self->program_name($pname || $DefaultProgramName);
154 $self->_eventHandler->register_factory('result', Bio
::Search
::Result
::ResultFactory
->new(-type
=> 'Bio::Search::Result::GenericResult'));
155 $self->_eventHandler->register_factory('hit', Bio
::Search
::Hit
::HitFactory
->new(-type
=> 'Bio::Search::Hit::GenericHit'));
156 $self->_eventHandler->register_factory('hsp', Bio
::Search
::HSP
::HSPFactory
->new(-type
=> 'Bio::Search::HSP::GenericHSP'));
163 Usage : my $result = $parser->next_result
164 Function: Parse the next result from the data stream
165 Returns : L<Bio::Search::Result::ResultI>
173 my ($lastquery,$lasthit);
177 while( defined ($_ = $self->_readline) ) {
178 # WU-BLAST -mformat 3 only
179 if(m{^#\s((?:\S+?)?BLAST[NPX])\s(\d+\.\d+.+\d{4}\
])}) {
180 ($alg, $ver) = ($1, $2);
181 # only one header for whole file with WU-BLAST
182 # so $alg and $ver won't get set properly for
184 $self->program_name($alg) if $alg;
185 $self->element({'Name' => 'Result_version',
186 'Data' => $ver}) if $ver;
190 elsif(m{^#\s+((?:\S+?)?BLAST[NPX])\s+(.+)}) {
191 ($alg, $ver) = ($1, $2);
194 next if /^#/ || /^\s*$/;
197 next if @fields == 1;
198 my ($qname,$hname, $percent_id, $hsp_len, $mismatches,$gapsm,
199 $qstart,$qend,$hstart,$hend,$evalue,$bits);
201 my ($num_scores, $raw_score, $identities, $positives, $percent_pos,
202 $qgap_blocks,$qgaps, $sgap_blocks, $sgaps, $qframe,
206 ($qname,$hname, $percent_id, $hsp_len, $mismatches,$gapsm,
207 $qstart,$qend,$hstart,$hend,$evalue,$bits) = @fields;
208 # NCBI -m8 and -m9, v 2.2.18+
209 } elsif (@fields == 13) {
210 ($qname, $hname, $percent_id, $percent_pos, $hsp_len, $mismatches, $gapsm,
211 $qstart,$qend,$hstart,$hend,$evalue,$bits) = @fields;
213 # WU-BLAST -mformat 2 and 3
214 elsif ((@fields == 22) or (@fields == 24)) {
215 ($qname,$hname,$evalue,$num_scores, $bits, $raw_score, $hsp_len,
216 $identities, $positives,$mismatches, $percent_id, $percent_pos,
217 $qgap_blocks, $qgaps, $sgap_blocks, $sgaps, $qframe, $qstart,
218 $qend, $sframe, $hstart,$hend,) = @fields;
219 # we need total gaps in the alignment
220 $gapsm=$qgaps+$sgaps;
223 # Remember Jim's code is 0 based
224 if( defined $lastquery &&
225 $lastquery ne $qname ) {
226 $self->end_element({'Name' => 'Hit'});
227 $self->end_element({'Name' => 'Result'});
228 $self->_pushback($_);
229 return $self->end_document;
230 } elsif( ! defined $lastquery ) {
231 $self->{'_result_count'}++;
232 $self->start_element({'Name' => 'Result'});
233 $self->element({'Name' => 'Result_program',
234 'Data' => $alg || $self->program_name});
235 $self->element({'Name' => 'Result_version',
236 'Data' => $ver}) if $ver;
237 $self->element({'Name' => 'Result_query-def',
239 $self->start_element({'Name' => 'Hit'});
240 $self->element({'Name' => 'Hit_id',
242 # we'll store the 1st hsp bits as the hit bits
243 $self->element({'Name' => 'Hit_bits',
245 # we'll store the 1st hsp value as the hit evalue
246 $self->element({'Name' => 'Hit_signif',
249 } elsif( $lasthit ne $hname ) {
250 if( $self->in_element('hit') ) {
251 $self->end_element({'Name' => 'Hit'});
253 $self->start_element({'Name' => 'Hit'});
254 $self->element({'Name' => 'Hit_id',
256 # we'll store the 1st hsp bits as the hit bits
257 $self->element({'Name' => 'Hit_bits',
259 # we'll store the 1st hsp value as the hit evalue
260 $self->element({'Name' => 'Hit_signif',
263 my $identical = $hsp_len - $mismatches - $gapsm;
264 $self->start_element({'Name' => 'Hsp'});
265 $self->element({'Name' => 'Hsp_evalue',
267 $self->element({'Name' => 'Hsp_bit-score',
269 $self->element({'Name' => 'Hsp_identity',
270 'Data' => $identical});
271 $self->element({'Name' => 'Hsp_positive',
272 'Data' => $positives});
273 $self->element({'Name' => 'Hsp_gaps',
275 $self->element({'Name' => 'Hsp_query-from',
277 $self->element({'Name' => 'Hsp_query-to',
280 $self->element({'Name' => 'Hsp_hit-from',
281 'Data' => $hstart });
282 $self->element({'Name' => 'Hsp_hit-to',
284 $self->element({'Name' => 'Hsp_align-len',
285 'Data' => $hsp_len});
286 $self->end_element({'Name' => 'Hsp'});
291 if( defined $lasthit && defined $lastquery ) {
292 if( $self->in_element('hit') ) {
293 $self->end_element({'Name' => 'Hit'});
295 $self->end_element({'Name' => 'Result'});
296 return $self->end_document;
302 Title : start_element
303 Usage : $eventgenerator->start_element
304 Function: Handles a start element event
306 Args : hashref with at least 2 keys 'Data' and 'Name'
312 my ($self,$data) = @_;
313 # we currently don't care about attributes
314 my $nm = $data->{'Name'};
315 if( my $type = $MODEMAP{$nm} ) {
317 if( $self->_will_handle($type) ) {
318 my $func = sprintf("start_%s",lc $type);
319 $self->_eventHandler->$func($data->{'Attributes'});
321 unshift @
{$self->{'_elements'}}, $type;
323 if($nm eq 'Result') {
324 $self->{'_values'} = {};
325 $self->{'_result'}= undef;
326 $self->{'_mode'} = '';
333 Title : start_element
334 Usage : $eventgenerator->end_element
335 Function: Handles an end element event
337 Args : hashref with at least 2 keys 'Data' and 'Name'
343 my ($self,$data) = @_;
344 my $nm = $data->{'Name'};
346 # Hsp are sort of weird, in that they end when another
347 # object begins so have to detect this in end_element for now
349 if( my $type = $MODEMAP{$nm} ) {
350 if( $self->_will_handle($type) ) {
351 my $func = sprintf("end_%s",lc $type);
352 $rc = $self->_eventHandler->$func($self->{'_reporttype'},
355 shift @
{$self->{'_elements'}};
357 } elsif( $MAPPING{$nm} ) {
358 if ( ref($MAPPING{$nm}) =~ /hash/i ) {
359 my $key = (keys %{$MAPPING{$nm}})[0];
360 $self->{'_values'}->{$key}->{$MAPPING{$nm}->{$key}} = $self->{'_last_data'};
362 $self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
365 $self->warn( "unknown nm $nm ignoring\n");
367 $self->{'_last_data'} = ''; # remove read data if we are at
369 $self->{'_result'} = $rc if( $nm eq 'Result' );
377 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
378 Function: Convience method that calls start_element, characters, end_element
380 Args : Hash ref with the keys 'Name' and 'Data'
386 my ($self,$data) = @_;
387 $self->start_element($data);
388 $self->characters($data);
389 $self->end_element($data);
396 Usage : $eventgenerator->characters($str)
397 Function: Send a character events
405 my ($self,$data) = @_;
407 return unless ( defined $data->{'Data'} );
408 if( $data->{'Data'} =~ /^\s+$/ ) {
409 return unless $data->{'Name'} =~ /Hsp\_(midline|qseq|hseq)/;
412 if( $self->in_element('hsp') &&
413 $data->{'Name'} =~ /Hsp\_(qseq|hseq|midline)/ ) {
415 $self->{'_last_hspdata'}->{$data->{'Name'}} .= $data->{'Data'};
418 $self->{'_last_data'} = $data->{'Data'};
424 Usage : $obj->_mode($newval)
427 Returns : value of _mode
428 Args : newvalue (optional)
434 my ($self,$value) = @_;
435 if( defined $value) {
436 $self->{'_mode'} = $value;
438 return $self->{'_mode'};
441 =head2 within_element
443 Title : within_element
444 Usage : if( $eventgenerator->within_element($element) ) {}
445 Function: Test if we are within a particular element
446 This is different than 'in' because within can be tested
449 Args : string element name
455 my ($self,$name) = @_;
456 return 0 if ( ! defined $name &&
457 ! defined $self->{'_elements'} ||
458 scalar @
{$self->{'_elements'}} == 0) ;
459 foreach ( @
{$self->{'_elements'}} ) {
470 Usage : if( $eventgenerator->in_element($element) ) {}
471 Function: Test if we are in 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 $self->{'_elements'}->[0];
483 return ( $self->{'_elements'}->[0] eq $name)
487 =head2 start_document
489 Title : start_document
490 Usage : $eventgenerator->start_document
491 Function: Handles a start document event
500 $self->{'_lasttype'} = '';
501 $self->{'_values'} = {};
502 $self->{'_result'}= undef;
503 $self->{'_mode'} = '';
504 $self->{'_elements'} = [];
511 Usage : $eventgenerator->end_document
512 Function: Handles an end document event
513 Returns : Bio::Search::Result::ResultI object
520 my ($self,@args) = @_;
521 return $self->{'_result'};
527 Usage : my $count = $searchio->result_count
528 Function: Returns the number of results we have processed
537 return $self->{'_result_count'};
540 sub report_count
{ shift->result_count }
546 Usage : $obj->program_name($newval)
547 Function: Get/Set the program name
548 Returns : value of program_name (a scalar)
549 Args : on set, new value (a scalar or undef, optional)
557 $self->{'program_name'} = shift if @_;
558 return $self->{'program_name'} || $DefaultProgramName;
565 Usage : Private method. For internal use only.
566 if( $self->_will_handle($type) ) { ... }
567 Function: Provides an optimized way to check whether or not an element of a
568 given type is to be handled.
569 Returns : Reference to EventHandler object if the element type is to be handled.
570 undef if the element type is not to be handled.
571 Args : string containing type of element.
579 Using the cached pointer to the EventHandler to minimize repeated
584 Caching the will_handle status for each type that is encountered so
585 that it only need be checked by calling
586 handler-E<gt>will_handle($type) once.
590 This does not lead to a major savings by itself (only 5-10%). In
591 combination with other optimizations, or for large parse jobs, the
592 savings good be significant.
594 To test against the unoptimized version, remove the parentheses from
595 around the third term in the ternary " ? : " operator and add two
596 calls to $self-E<gt>_eventHandler().
601 my ($self,$type) = @_;
602 my $handler = $self->{'_handler'};
603 my $will_handle = defined($self->{'_will_handle_cache'}->{$type})
604 ?
$self->{'_will_handle_cache'}->{$type}
605 : ($self->{'_will_handle_cache'}->{$type} =
606 $handler->will_handle($type));
608 return $will_handle ?
$handler : undef;