ide/bioperl-mode: into its own repository to be developed separately
[bioperl-live.git] / Bio / SearchIO / blasttable.pm
bloba0db3245eb9f335070fadb9b2708d5e994a1ef92
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
14 =head1 NAME
16 Bio::SearchIO::blasttable - Driver module for SearchIO for parsing NCBI -m 8/9 format
18 =head1 SYNOPSIS
20 # do not use this module directly
21 use Bio::SearchIO;
22 my $parser = Bio::SearchIO->new(-file => $file,
23 -format => 'blasttable');
25 while( my $result = $parser->next_result ) {
28 =head1 DESCRIPTION
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.
33 =head1 FEEDBACK
35 =head2 Mailing Lists
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
44 =head2 Support
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.
55 =head2 Reporting Bugs
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
59 the web:
61 https://github.com/bioperl/bioperl-live/issues
63 =head1 AUTHOR - Jason Stajich
65 Email jason-at-bioperl-dot-org
67 =head1 APPENDIX
69 The rest of the documentation details each of the object methods.
70 Internal methods are usually preceded with a _
72 =cut
74 # Let the code begin...
77 package Bio::SearchIO::blasttable;
78 use vars qw(%MAPPING %MODEMAP $DEFAULT_WRITER_CLASS $DefaultProgramName);
79 use strict;
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
88 %MODEMAP = (
89 'Result' => 'result',
90 'Hit' => 'hit',
91 'Hsp' => 'hsp'
94 %MAPPING = (
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);
135 =head2 new
137 Title : new
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
141 Args :
144 =cut
146 sub _initialize {
147 my ($self,@args) = @_;
148 $self->SUPER::_initialize(@args);
150 my ($pname) = $self->_rearrange([qw(PROGRAM_NAME)],
151 @args);
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'));
159 =head2 next_result
161 Title : next_result
162 Usage : my $result = $parser->next_result
163 Function: Parse the next result from the data stream
164 Returns : L<Bio::Search::Result::ResultI>
165 Args : none
168 =cut
170 sub next_result{
171 my ($self) = @_;
172 my ($lastquery,$lasthit);
173 local $/ = "\n";
174 local $_;
175 my ($alg, $ver);
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
182 # each result
183 $self->program_name($alg) if $alg;
184 $self->element({'Name' => 'Result_version',
185 'Data' => $ver}) if $ver;
186 next;
188 # -m 9 only
189 elsif(m{^#\s+((?:\S+?)?BLAST[NPX])\s+(.+)}) {
190 ($alg, $ver) = ($1, $2);
191 next;
193 next if /^#/ || /^\s*$/;
195 my @fields = split;
196 next if @fields == 1;
197 my ($qname,$hname, $percent_id, $hsp_len, $mismatches,$gapsm,
198 $qstart,$qend,$hstart,$hend,$evalue,$bits);
199 # WU-BLAST-specific
200 my ($num_scores, $raw_score, $identities, $positives, $percent_pos,
201 $qgap_blocks,$qgaps, $sgap_blocks, $sgaps, $qframe,
202 $sframe);
203 # NCBI -m8 and -m9
204 if (@fields == 12) {
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',
247 'Data' => $qname});
248 $self->start_element({'Name' => 'Hit'});
249 $self->element({'Name' => 'Hit_id',
250 'Data' => $hname});
251 # we'll store the 1st hsp bits as the hit bits
252 $self->element({'Name' => 'Hit_bits',
253 'Data' => $bits});
254 # we'll store the 1st hsp value as the hit evalue
255 $self->element({'Name' => 'Hit_signif',
256 'Data' => $evalue});
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',
264 'Data' => $hname});
265 # we'll store the 1st hsp bits as the hit bits
266 $self->element({'Name' => 'Hit_bits',
267 'Data' => $bits});
268 # we'll store the 1st hsp value as the hit evalue
269 $self->element({'Name' => 'Hit_signif',
270 'Data' => $evalue});
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',
280 'Data' => $evalue});
281 $self->element({'Name' => 'Hsp_bit-score',
282 'Data' => $bits});
283 $self->element({'Name' => 'Hsp_identity',
284 'Data' => $identical});
285 $self->element({'Name' => 'Hsp_positive',
286 'Data' => $positives});
287 $self->element({'Name' => 'Hsp_gaps',
288 'Data' => $gapsm});
289 $self->element({'Name' => 'Hsp_query-from',
290 'Data' => $qstart});
291 $self->element({'Name' => 'Hsp_query-to',
292 'Data' => $qend});
294 $self->element({'Name' => 'Hsp_hit-from',
295 'Data' => $hstart });
296 $self->element({'Name' => 'Hsp_hit-to',
297 'Data' => $hend });
298 $self->element({'Name' => 'Hsp_align-len',
299 'Data' => $hsp_len});
300 $self->end_element({'Name' => 'Hsp'});
301 $lastquery = $qname;
302 $lasthit = $hname;
304 # fencepost
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;
314 =head2 start_element
316 Title : start_element
317 Usage : $eventgenerator->start_element
318 Function: Handles a start element event
319 Returns : none
320 Args : hashref with at least 2 keys 'Data' and 'Name'
323 =cut
325 sub start_element{
326 my ($self,$data) = @_;
327 # we currently don't care about attributes
328 my $nm = $data->{'Name'};
329 if( my $type = $MODEMAP{$nm} ) {
330 $self->_mode($type);
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'} = '';
345 =head2 end_element
347 Title : start_element
348 Usage : $eventgenerator->end_element
349 Function: Handles an end element event
350 Returns : none
351 Args : hashref with at least 2 keys 'Data' and 'Name'
354 =cut
356 sub end_element {
357 my ($self,$data) = @_;
358 my $nm = $data->{'Name'};
359 my $rc;
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'},
367 $self->{'_values'});
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'};
375 } else {
376 $self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
378 } else {
379 $self->warn( "unknown nm $nm ignoring\n");
381 $self->{'_last_data'} = ''; # remove read data if we are at
382 # end of an element
383 $self->{'_result'} = $rc if( $nm eq 'Result' );
384 return $rc;
388 =head2 element
390 Title : element
391 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
392 Function: Convience method that calls start_element, characters, end_element
393 Returns : none
394 Args : Hash ref with the keys 'Name' and 'Data'
397 =cut
399 sub element{
400 my ($self,$data) = @_;
401 $self->start_element($data);
402 $self->characters($data);
403 $self->end_element($data);
407 =head2 characters
409 Title : characters
410 Usage : $eventgenerator->characters($str)
411 Function: Send a character events
412 Returns : none
413 Args : string
416 =cut
418 sub characters{
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}.
427 #/maj
428 # return unless ( defined $data->{'Data'} );
429 return unless ( grep /Data/, keys %$data );
430 if ( !defined $data->{'Data'} ) {
431 $self->{'_last_data'} = undef;
432 return;
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'};
447 =head2 _mode
449 Title : _mode
450 Usage : $obj->_mode($newval)
451 Function:
452 Example :
453 Returns : value of _mode
454 Args : newvalue (optional)
457 =cut
459 sub _mode{
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
473 for a whole block.
474 Returns : boolean
475 Args : string element name
478 =cut
480 sub within_element{
481 my ($self,$name) = @_;
482 return 0 if ( ! defined $name &&
483 ! defined $self->{'_elements'} ||
484 scalar @{$self->{'_elements'}} == 0) ;
485 foreach ( @{$self->{'_elements'}} ) {
486 if( $_ eq $name ) {
487 return 1;
490 return 0;
493 =head2 in_element
495 Title : in_element
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
499 for a whole block.
500 Returns : boolean
501 Args : string element name
504 =cut
506 sub in_element{
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
518 Returns : none
519 Args : none
522 =cut
524 sub start_document{
525 my ($self) = @_;
526 $self->{'_lasttype'} = '';
527 $self->{'_values'} = {};
528 $self->{'_result'}= undef;
529 $self->{'_mode'} = '';
530 $self->{'_elements'} = [];
534 =head2 end_document
536 Title : end_document
537 Usage : $eventgenerator->end_document
538 Function: Handles an end document event
539 Returns : Bio::Search::Result::ResultI object
540 Args : none
543 =cut
545 sub end_document{
546 my ($self,@args) = @_;
547 return $self->{'_result'};
550 =head2 result_count
552 Title : result_count
553 Usage : my $count = $searchio->result_count
554 Function: Returns the number of results we have processed
555 Returns : integer
556 Args : none
559 =cut
561 sub result_count {
562 my $self = shift;
563 return $self->{'_result_count'};
566 sub report_count { shift->result_count }
569 =head2 program_name
571 Title : program_name
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)
578 =cut
580 sub program_name{
581 my $self = shift;
583 $self->{'program_name'} = shift if @_;
584 return $self->{'program_name'} || $DefaultProgramName;
588 =head2 _will_handle
590 Title : _will_handle
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.
599 Optimizations:
601 =over 2
603 =item 1
605 Using the cached pointer to the EventHandler to minimize repeated
606 lookups.
608 =item 2
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.
614 =back
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().
624 =cut
626 sub _will_handle {
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;