FAST/Bio/SearchIO/blast.pm untested

filter: Statementstmt 0.0% 0 / 961 Branchbran 0.0% 0 / 338 Conditioncond 0.0% 0 / 201 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 1541 SCAR 102.8
CC
170
Cov
0%
next_result
21170.0
end_element
110.0
start_element
30.0
CRAP 29070.0
1 #
2 # BioPerl module for FAST::Bio::SearchIO::blast
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 # 20030409 - sac
15 # PSI-BLAST full parsing support. Rollout of new
16 # model which will remove Steve's old psiblast driver
17 # 20030424 - jason
18 # Megablast parsing fix as reported by Neil Saunders
19 # 20030427 - jason
20 # Support bl2seq parsing
21 # 20031124 - jason
22 # Parse more blast statistics, lambda, entropy, etc
23 # from WU-BLAST in frame-specific manner
24 # 20060216 - cjf - fixed blast parsing for BLAST v2.2.13 output
25 # 20071104 - dmessina - added support for WUBLAST -echofilter
26 # 20071121 - cjf - fixed several bugs (bugs 2391, 2399, 2409)
27  
28 =head1 NAME
29  
30 FAST::Bio::SearchIO::blast - Event generator for event based parsing of
31 blast reports
32  
33 =head1 SYNOPSIS
34  
35 # Do not use this object directly - it is used as part of the
36 # FAST::Bio::SearchIO system.
37  
38 use FAST::Bio::SearchIO;
39 my $searchio = FAST::Bio::SearchIO->new(-format => 'blast',
40 -file => 't/data/ecolitst.bls');
41 while( my $result = $searchio->next_result ) {
42 while( my $hit = $result->next_hit ) {
43 while( my $hsp = $hit->next_hsp ) {
44 # ...
45 }
46 }
47 }
48  
49 =head1 DESCRIPTION
50  
51 This object encapsulated the necessary methods for generating events
52 suitable for building FAST::Bio::Search objects from a BLAST report file.
53 Read the L<FAST::Bio::SearchIO> for more information about how to use this.
54  
55 This driver can parse:
56  
57 =over 4
58  
59 =item *
60  
61 NCBI produced plain text BLAST reports from blastall, this also
62 includes PSIBLAST, PSITBLASTN, RPSBLAST, and bl2seq reports. NCBI XML
63 BLAST output is parsed with the blastxml SearchIO driver
64  
65 =item *
66  
67 WU-BLAST all reports
68  
69 =item *
70  
71 Jim Kent's BLAST-like output from his programs (BLASTZ, BLAT)
72  
73 =item *
74  
75 BLAST-like output from Paracel BTK output
76  
77 =back
78  
79 =head2 bl2seq parsing
80  
81 Since I cannot differentiate between BLASTX and TBLASTN since bl2seq
82 doesn't report the algorithm used - I assume it is BLASTX by default -
83 you can supply the program type with -report_type in the SearchIO
84 constructor i.e.
85  
86 my $parser = FAST::Bio::SearchIO->new(-format => 'blast',
87 -file => 'bl2seq.tblastn.report',
88 -report_type => 'tblastn');
89  
90 This only really affects where the frame and strand information are
91 put - they will always be on the $hsp-E<gt>query instead of on the
92 $hsp-E<gt>hit part of the feature pair for blastx and tblastn bl2seq
93 produced reports. Hope that's clear...
94  
95 =head1 FEEDBACK
96  
97 =head2 Mailing Lists
98  
99 User feedback is an integral part of the evolution of this and other
100 Bioperl modules. Send your comments and suggestions preferably to
101 the Bioperl mailing list. Your participation is much appreciated.
102  
103 bioperl-l@bioperl.org - General discussion
104 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
105  
106 =head2 Support
107  
108 Please direct usage questions or support issues to the mailing list:
109  
110 I<bioperl-l@bioperl.org>
111  
112 rather than to the module maintainer directly. Many experienced and
113 reponsive experts will be able look at the problem and quickly
114 address it. Please include a thorough description of the problem
115 with code and data examples if at all possible.
116  
117 =head2 Reporting Bugs
118  
119 Report bugs to the Bioperl bug tracking system to help us keep track
120 of the bugs and their resolution. Bug reports can be submitted via the
121 web:
122  
123 https://redmine.open-bio.org/projects/bioperl/
124  
125 =head1 AUTHOR - Jason Stajich
126  
127 Email Jason Stajich jason-at-bioperl.org
128  
129 =head1 CONTRIBUTORS
130  
131 Steve Chervitz sac-at-bioperl.org
132  
133 =head1 APPENDIX
134  
135 The rest of the documentation details each of the object methods.
136 Internal methods are usually preceded with a _
137  
138 =cut
139  
140 # Let the code begin...'
141  
142 package FAST::Bio::SearchIO::blast;
143  
144 use FAST::Bio::SearchIO::IteratedSearchResultEventBuilder;
145 use strict;
146 use vars qw(%MAPPING %MODEMAP
147 $DEFAULT_BLAST_WRITER_CLASS
148 $MAX_HSP_OVERLAP
149 $DEFAULT_SIGNIF
150 $DEFAULT_SCORE
151 $DEFAULTREPORTTYPE
152 );
153  
154  
155 use base qw(FAST::Bio::SearchIO);
156 use Data::Dumper;
157  
158 BEGIN {
159  
160 # mapping of NCBI Blast terms to Bioperl hash keys
161 %MODEMAP = (
162 'BlastOutput' => 'result',
163 'Iteration' => 'iteration',
164 'Hit' => 'hit',
165 'Hsp' => 'hsp'
166 );
167  
168 # This should really be done more intelligently, like with
169 # XSLT
170  
171 %MAPPING = (
172 'Hsp_bit-score' => 'HSP-bits',
173 'Hsp_score' => 'HSP-score',
174 'Hsp_evalue' => 'HSP-evalue',
175 'Hsp_n', => 'HSP-n',
176 'Hsp_pvalue' => 'HSP-pvalue',
177 'Hsp_query-from' => 'HSP-query_start',
178 'Hsp_query-to' => 'HSP-query_end',
179 'Hsp_hit-from' => 'HSP-hit_start',
180 'Hsp_hit-to' => 'HSP-hit_end',
181 'Hsp_positive' => 'HSP-conserved',
182 'Hsp_identity' => 'HSP-identical',
183 'Hsp_gaps' => 'HSP-hsp_gaps',
184 'Hsp_hitgaps' => 'HSP-hit_gaps',
185 'Hsp_querygaps' => 'HSP-query_gaps',
186 'Hsp_qseq' => 'HSP-query_seq',
187 'Hsp_hseq' => 'HSP-hit_seq',
188 'Hsp_midline' => 'HSP-homology_seq',
189 'Hsp_align-len' => 'HSP-hsp_length',
190 'Hsp_query-frame' => 'HSP-query_frame',
191 'Hsp_hit-frame' => 'HSP-hit_frame',
192 'Hsp_links' => 'HSP-links',
193 'Hsp_group' => 'HSP-hsp_group',
194 'Hsp_features' => 'HSP-hit_features',
195  
196 'Hit_id' => 'HIT-name',
197 'Hit_len' => 'HIT-length',
198 'Hit_accession' => 'HIT-accession',
199 'Hit_def' => 'HIT-description',
200 'Hit_signif' => 'HIT-significance',
201 # For NCBI blast, the description line contains bits.
202 # For WU-blast, the description line contains score.
203 'Hit_score' => 'HIT-score',
204 'Hit_bits' => 'HIT-bits',
205  
206 'Iteration_iter-num' => 'ITERATION-number',
207 'Iteration_converged' => 'ITERATION-converged',
208  
209 'BlastOutput_program' => 'RESULT-algorithm_name',
210 'BlastOutput_version' => 'RESULT-algorithm_version',
211 'BlastOutput_algorithm-reference' => 'RESULT-algorithm_reference',
212 'BlastOutput_rid' => 'RESULT-rid',
213 'BlastOutput_query-def' => 'RESULT-query_name',
214 'BlastOutput_query-len' => 'RESULT-query_length',
215 'BlastOutput_query-acc' => 'RESULT-query_accession',
216 'BlastOutput_query-gi' => 'RESULT-query_gi',
217 'BlastOutput_querydesc' => 'RESULT-query_description',
218 'BlastOutput_db' => 'RESULT-database_name',
219 'BlastOutput_db-len' => 'RESULT-database_entries',
220 'BlastOutput_db-let' => 'RESULT-database_letters',
221 'BlastOutput_inclusion-threshold' => 'RESULT-inclusion_threshold',
222  
223 'Parameters_matrix' => { 'RESULT-parameters' => 'matrix' },
224 'Parameters_expect' => { 'RESULT-parameters' => 'expect' },
225 'Parameters_include' => { 'RESULT-parameters' => 'include' },
226 'Parameters_sc-match' => { 'RESULT-parameters' => 'match' },
227 'Parameters_sc-mismatch' => { 'RESULT-parameters' => 'mismatch' },
228 'Parameters_gap-open' => { 'RESULT-parameters' => 'gapopen' },
229 'Parameters_gap-extend' => { 'RESULT-parameters' => 'gapext' },
230 'Parameters_filter' => { 'RESULT-parameters' => 'filter' },
231 'Parameters_allowgaps' => { 'RESULT-parameters' => 'allowgaps' },
232 'Parameters_full_dbpath' => { 'RESULT-parameters' => 'full_dbpath' },
233 'Statistics_db-len' => { 'RESULT-statistics' => 'dbentries' },
234 'Statistics_db-let' => { 'RESULT-statistics' => 'dbletters' },
235 'Statistics_hsp-len' =>
236 { 'RESULT-statistics' => 'effective_hsplength' },
237 'Statistics_query-len' => { 'RESULT-statistics' => 'querylength' },
238 'Statistics_eff-space' => { 'RESULT-statistics' => 'effectivespace' },
239 'Statistics_eff-spaceused' =>
240 { 'RESULT-statistics' => 'effectivespaceused' },
241 'Statistics_eff-dblen' =>
242 { 'RESULT-statistics' => 'effectivedblength' },
243 'Statistics_kappa' => { 'RESULT-statistics' => 'kappa' },
244 'Statistics_lambda' => { 'RESULT-statistics' => 'lambda' },
245 'Statistics_entropy' => { 'RESULT-statistics' => 'entropy' },
246 'Statistics_gapped_kappa' => { 'RESULT-statistics' => 'kappa_gapped' },
247 'Statistics_gapped_lambda' =>
248 { 'RESULT-statistics' => 'lambda_gapped' },
249 'Statistics_gapped_entropy' =>
250 { 'RESULT-statistics' => 'entropy_gapped' },
251  
252 'Statistics_framewindow' =>
253 { 'RESULT-statistics' => 'frameshiftwindow' },
254 'Statistics_decay' => { 'RESULT-statistics' => 'decayconst' },
255  
256 'Statistics_hit_to_db' => { 'RESULT-statistics' => 'Hits_to_DB' },
257 'Statistics_num_suc_extensions' =>
258 { 'RESULT-statistics' => 'num_successful_extensions' },
259 'Statistics_length_adjustment' => { 'RESULT-statistics' => 'length_adjustment' },
260 'Statistics_number_of_hsps_better_than_expect_value_cutoff_without_gapping' =>
261 { 'RESULT-statistics' => 'number_of_hsps_better_than_expect_value_cutoff_without_gapping' },
262 'Statistics_number_of_hsps_gapped' => { 'RESULT-statistics' => 'number_of_hsps_gapped' },
263 'Statistics_number_of_hsps_successfully_gapped' => { 'RESULT-statistics' => 'number_of_hsps_successfully_gapped' },
264  
265 # WU-BLAST stats
266 'Statistics_DFA_states' => { 'RESULT-statistics' => 'num_dfa_states' },
267 'Statistics_DFA_size' => { 'RESULT-statistics' => 'dfa_size' },
268 'Statistics_noprocessors' =>
269 { 'RESULT-statistics' => 'no_of_processors' },
270 'Statistics_neighbortime' =>
271 { 'RESULT-statistics' => 'neighborhood_generate_time' },
272 'Statistics_starttime' => { 'RESULT-statistics' => 'start_time' },
273 'Statistics_endtime' => { 'RESULT-statistics' => 'end_time' },
274 );
275  
276 # add WU-BLAST Frame-Based Statistics
277 for my $frame ( 0 .. 3 ) {
278 for my $strand ( '+', '-' ) {
279 for my $ind (
280 qw(length efflength E S W T X X_gapped E2
281 E2_gapped S2)
282 )
283 {
284 $MAPPING{"Statistics_frame$strand$frame\_$ind"} =
285 { 'RESULT-statistics' => "Frame$strand$frame\_$ind" };
286 }
287 for my $val (qw(lambda kappa entropy )) {
288 for my $type (qw(used computed gapped)) {
289 my $key = "Statistics_frame$strand$frame\_$val\_$type";
290 my $val =
291 { 'RESULT-statistics' =>
292 "Frame$strand$frame\_$val\_$type" };
293 $MAPPING{$key} = $val;
294 }
295 }
296 }
297 }
298  
299 # add Statistics
300 for my $stats (
301 qw(T A X1 X2 X3 S1 S2 X1_bits X2_bits X3_bits
302 S1_bits S2_bits num_extensions
303 num_successful_extensions
304 seqs_better_than_cutoff
305 posted_date
306 search_cputime total_cputime
307 search_actualtime total_actualtime
308 no_of_processors ctxfactor)
309 )
310 {
311 my $key = "Statistics_$stats";
312 my $val = { 'RESULT-statistics' => $stats };
313 $MAPPING{$key} = $val;
314 }
315  
316 # add WU-BLAST Parameters
317 for my $param (
318 qw(span span1 span2 links warnings notes hspsepsmax
319 hspsepqmax topcomboN topcomboE postsw cpus wordmask
320 filter sort_by_pvalue sort_by_count sort_by_highscore
321 sort_by_totalscore sort_by_subjectlength noseqs gi qtype
322 qres V B Z Y M N)
323 )
324 {
325 my $key = "Parameters_$param";
326 my $val = { 'RESULT-parameters' => $param };
327 $MAPPING{$key} = $val;
328 }
329  
330 $DEFAULT_BLAST_WRITER_CLASS = 'FAST::Bio::SearchIO::Writer::HitTableWriter';
331 $MAX_HSP_OVERLAP = 2; # Used when tiling multiple HSPs.
332 $DEFAULTREPORTTYPE = 'BLASTP'; # for bl2seq
333 }
334  
335 =head2 new
336  
337 Title : new
338 Usage : my $obj = FAST::Bio::SearchIO::blast->new(%args);
339 Function: Builds a new FAST::Bio::SearchIO::blast object
340 Returns : FAST::Bio::SearchIO::blast
341 Args : Key-value pairs:
342 -fh/-file => filehandle/filename to BLAST file
343 -format => 'blast'
344 -report_type => 'blastx', 'tblastn', etc -- only for bl2seq
345 reports when you want to distinguish between
346 tblastn and blastx reports (this only controls
347 where the frame information is put - on the query
348 or subject object.
349 -inclusion_threshold => e-value threshold for inclusion in the
350 PSI-BLAST score matrix model (blastpgp)
351 -signif => float or scientific notation number to be used
352 as a P- or Expect value cutoff
353 -score => integer or scientific notation number to be used
354 as a blast score value cutoff
355 -bits => integer or scientific notation number to be used
356 as a bit score value cutoff
357 -hit_filter => reference to a function to be used for
358 filtering hits based on arbitrary criteria.
359 All hits of each BLAST report must satisfy
360 this criteria to be retained.
361 If a hit fails this test, it is ignored.
362 This function should take a
363 FAST::Bio::Search::Hit::BlastHit.pm object as its first
364 argument and return true
365 if the hit should be retained.
366 Sample filter function:
367 -hit_filter => sub { $hit = shift;
368 $hit->gaps == 0; },
369 (Note: -filt_func is synonymous with -hit_filter)
370 -overlap => integer. The amount of overlap to permit between
371 adjacent HSPs when tiling HSPs. A reasonable value is 2.
372 Default = $FAST::Bio::SearchIO::blast::MAX_HSP_OVERLAP.
373  
374 The following criteria are not yet supported:
375 (these are probably best applied within this module rather than in the
376 event handler since they would permit the parser to take some shortcuts.)
377  
378 -check_all_hits => boolean. Check all hits for significance against
379 significance criteria. Default = false.
380 If false, stops processing hits after the first
381 non-significant hit or the first hit that fails
382 the hit_filter call. This speeds parsing,
383 taking advantage of the fact that the hits
384 are processed in the order they appear in the report.
385 -min_query_len => integer to be used as a minimum for query sequence length.
386 Reports with query sequences below this length will
387 not be processed. Default = no minimum length.
388 -best => boolean. Only process the best hit of each report;
389 default = false.
390  
391 =cut
392  
393 sub _initialize {
394 my ( $self, @args ) = @_;
395 $self->SUPER::_initialize(@args);
396  
397 # Blast reports require a specialized version of the SREB due to the
398 # possibility of iterations (PSI-BLAST). Forwarding all arguments to it. An
399 # issue here is that we want to set new default object factories if none are
400 # supplied.
401  
402 my $handler = FAST::Bio::SearchIO::IteratedSearchResultEventBuilder->new(@args);
403 $self->attach_EventHandler($handler);
404
405 # 2006-04-26 move this to the attach_handler function in this module so we
406 # can really reset the handler
407 # Optimization: caching
408 # the EventHandler since it is used a lot during the parse.
409
410 # $self->{'_handler_cache'} = $handler;
411  
412 my ($rpttype ) = $self->_rearrange(
413 [
414 qw(
415 REPORT_TYPE)
416 ],
417 @args
418 );
419 defined $rpttype && ( $self->{'_reporttype'} = $rpttype );
420 }
421  
422 sub attach_EventHandler {
423 my ($self,$handler) = @_;
424  
425 $self->SUPER::attach_EventHandler($handler);
426  
427 # Optimization: caching the EventHandler since it is used a lot
428 # during the parse.
429  
430 $self->{'_handler_cache'} = $handler;
431 return;
432 }
433  
434 =head2 next_result
435  
436 Title : next_result
437 Usage : my $hit = $searchio->next_result;
438 Function: Returns the next Result from a search
439 Returns : FAST::Bio::Search::Result::ResultI object
440 Args : none
441  
442 =cut
443  
444 sub next_result {
445 my ($self) = @_;
446 my $v = $self->verbose;
447 my $data = '';
448 my $flavor = '';
449 $self->{'_seentop'} = 0; # start next report at top
450 $self->{'_seentop'} = 0;
451 my ( $reporttype, $seenquery, $reportline );
452 my ( $seeniteration, $found_again );
453 my $incl_threshold = $self->inclusion_threshold;
454 my $bl2seq_fix;
455 $self->start_document(); # let the fun begin...
456 my (@hit_signifs);
457 my $gapped_stats = 0; # for switching between gapped/ungapped
458 # lambda, K, H
459 local $_ = "\n"; #consistency
460 PARSER:
461 while ( defined( $_ = $self->_readline ) ) {
462 next if (/^\s+$/); # skip empty lines
463 next if (/CPU time:/);
464 next if (/^>\s*$/);
465 next if (/\Q***** No hits found ******\E/);
466 if (
467 /^((?:\S+?)?BLAST[NPX]?)\s+(.+)$/i # NCBI BLAST, PSIBLAST
468 # RPSBLAST, MEGABLAST
469 || /^(P?GENEWISE|HFRAME|SWN|TSWN)\s+(.+)/i #Paracel BTK
470 )
471 {
472 ($reporttype, my $reportversion) = ($1, $2);
473 # need to keep track of whether this is WU-BLAST
474 if ($reportversion && $reportversion =~ m{WashU$}) {
475 $self->{'_wublast'}++;
476 }
477 $self->debug("blast.pm: Start of new report: $reporttype, $reportversion\n");
478 if ( $self->{'_seentop'} ) {
479 # This handles multi-result input streams
480 $self->_pushback($_);
481 last PARSER;
482 }
483 $self->_start_blastoutput;
484 if ($reporttype =~ /RPS-BLAST/) {
485 $reporttype .= '(BLASTP)'; # default RPS-BLAST type
486 }
487 $reportline = $_; # to fix the fact that RPS-BLAST output is wrong
488 $self->element(
489 {
490 'Name' => 'BlastOutput_program',
491 'Data' => $reporttype
492 }
493 );
494  
495 $self->element(
496 {
497 'Name' => 'BlastOutput_version',
498 'Data' => $reportversion
499 }
500 );
501 $self->element(
502 {
503 'Name' => 'BlastOutput_inclusion-threshold',
504 'Data' => $incl_threshold
505 }
506 );
507 }
508 # parse the BLAST algorithm reference
509 elsif(/^Reference:\s+(.*)$/) {
510 # want to preserve newlines for the BLAST algorithm reference
511 my $algorithm_reference = "$1\n";
512 $_ = $self->_readline;
513 # while the current line, does not match an empty line, a RID:, a Database:, or a query definition line (Query=)
514 # we are still looking at the algorithm_reference, append it to what we parsed so far
515 while($_ !~ /^$/ && $_ !~ /^RID:/ && $_ !~ /^Database:/ && $_ !~ /^Query=/) {
516 $algorithm_reference .= "$_";
517 $_ = $self->_readline;
518 }
519 # if we exited the while loop, we saw an empty line, a RID:, or a Database:, so push it back
520 $self->_pushback($_);
521 $self->element(
522 {
523 'Name' => 'BlastOutput_algorithm-reference',
524 'Data' => $algorithm_reference
525 }
526 );
527 }
528 # parse BLAST RID (Request ID)
529 elsif(/^RID:\s+(.*)$/) {
530 my $rid = $1;
531 $self->element(
532 {
533 'Name' => 'BlastOutput_rid',
534 'Data' => $rid
535 }
536 );
537 }
538 # added Windows workaround for bug 1985
539 elsif (/^(Searching|Results from round)/) {
540 next unless $1 =~ /Results from round/;
541 $self->debug("blast.pm: Possible psi blast iterations found...\n");
542
543 $self->in_element('hsp')
544 && $self->end_element( { 'Name' => 'Hsp' } );
545 $self->in_element('hit')
546 && $self->end_element( { 'Name' => 'Hit' } );
547 if ( defined $seeniteration ) {
548 $self->within_element('iteration')
549 && $self->end_element( { 'Name' => 'Iteration' } );
550 $self->start_element( { 'Name' => 'Iteration' } );
551 }
552 else {
553 $self->start_element( { 'Name' => 'Iteration' } );
554 }
555 $seeniteration = 1;
556 }
557 elsif (/^Query=\s*(.*)$/) {
558 my $q = $1;
559 $self->debug("blast.pm: Query= found...$_\n");
560 my $size = 0;
561 if ( defined $seenquery ) {
562 $self->_pushback($reportline) if $reportline;
563 $self->_pushback($_);
564 last PARSER;
565 }
566 else {
567 if ( !defined $reporttype ) {
568 $self->_start_blastoutput;
569 if ( defined $seeniteration ) {
570 $self->in_element('iteration')
571 && $self->end_element( { 'Name' => 'Iteration' } );
572 $self->start_element( { 'Name' => 'Iteration' } );
573 }
574 else {
575 $self->start_element( { 'Name' => 'Iteration' } );
576 }
577 $seeniteration = 1;
578 }
579 }
580 $seenquery = $q;
581 $_ = $self->_readline;
582 while ( defined($_) ) {
583 if (/^Database:/) {
584 $self->_pushback($_);
585 last;
586 }
587 # below line fixes length issue with BLAST v2.2.13; still works
588 # with BLAST v2.2.12
589 if ( /\((\-?[\d,]+)\s+letters.*\)/ || /^Length=(\-?[\d,]+)/ ) {
590 $size = $1;
591 $size =~ s/,//g;
592 last;
593 }
594 else {
595 # bug 2391
596 $q .= ($q =~ /\w$/ && $_ =~ /^\w/) ? " $_" : $_;
597 $q =~ s/\s+/ /g; # this catches the newline as well
598 $q =~ s/^ | $//g;
599 }
600  
601 $_ = $self->_readline;
602 }
603 chomp($q);
604 my ( $nm, $desc ) = split( /\s+/, $q, 2 );
605 $self->element(
606 {
607 'Name' => 'BlastOutput_query-def',
608 'Data' => $nm
609 }
610 ) if $nm;
611 $self->element(
612 {
613 'Name' => 'BlastOutput_query-len',
614 'Data' => $size
615 }
616 );
617 defined $desc && $desc =~ s/\s+$//;
618 $self->element(
619 {
620 'Name' => 'BlastOutput_querydesc',
621 'Data' => $desc
622 }
623 );
624 my ( $gi, $acc, $version ) = $self->_get_seq_identifiers($nm);
625 $version = defined($version) && length($version) ? ".$version" : "";
626 $self->element(
627 {
628 'Name' => 'BlastOutput_query-acc',
629 'Data' => "$acc$version"
630 }
631 ) if $acc;
632 }
633 # added check for WU-BLAST -echofilter option (bug 2388)
634 elsif (/^>Unfiltered[+-]1$/) {
635 # skip all of the lines of unfiltered sequence
636 while($_ !~ /^Database:/) {
637 $self->debug("Bypassing features line: $_");
638 $_ = $self->_readline;
639 }
640 $self->_pushback($_);
641 }
642 elsif (/Sequences producing significant alignments:/) {
643 $self->debug("blast.pm: Processing NCBI-BLAST descriptions\n");
644 $flavor = 'ncbi';
645
646 # PSI-BLAST parsing needs to be fixed to specifically look
647 # for old vs new per iteration, as sorting based on duplication
648 # leads to bugs, see bug 1986
649
650 # The next line is not necessarily whitespace in psiblast reports.
651 # Also note that we must look for the end of this section by testing
652 # for a line with a leading >. Blank lines occur with this section
653 # for psiblast.
654 if ( !$self->in_element('iteration') ) {
655 $self->start_element( { 'Name' => 'Iteration' } );
656 }
657 # these elements are dropped with some multiquery reports; add
658 # back here
659 $self->element(
660 {
661 'Name' => 'BlastOutput_db-len',
662 'Data' => $self->{'_blsdb_length'}
663 }
664 ) if $self->{'_blsdb_length'};
665 $self->element(
666 {
667 'Name' => 'BlastOutput_db-let',
668 'Data' => $self->{'_blsdb_letters'}
669 }
670 ) if $self->{'_blsdb_letters'};
671 $self->element(
672 {
673 'Name' => 'BlastOutput_db',
674 'Data' => $self->{'_blsdb'}
675 }
676 ) if $self->{'_blsdb_letters'};
677
678 # changed 8/28/2008 to exit hit table if blank line is found after an
679 # appropriate line
680 my $h_regex;
681 my $seen_block;
682 DESCLINE:
683 while ( defined( my $descline = $self->_readline() ) ) {
684 if ($descline =~ m{^\s*$}) {
685 last DESCLINE if $seen_block;
686 next DESCLINE;
687 }
688 # any text match is part of block...
689 $seen_block++;
690 # GCG multiline oddness...
691 if ($descline =~ /^(\S+)\s+Begin:\s\d+\s+End:\s+\d+/xms) {
692 my ($id, $nextline) = ($1, $self->_readline);
693 $nextline =~ s{^!}{};
694 $descline = "$id $nextline";
695 }
696 # NCBI style hit table (no N)
697 if ($descline =~ /(?<!cor) # negative lookahead
698 (\d*\.?(?:[\+\-eE]+)?\d+) # number (float or scientific notation)
699 \s+ # space
700 (\d*\.?(?:[\+\-eE]+)?\d+) # number (float or scientific notation)
701 \s*$/xms) {
702
703 my ( $score, $evalue ) = ($1, $2);
704
705 # Some data clean-up so e-value will appear numeric to perl
706 $evalue =~ s/^e/1e/i;
707  
708 # This to handle no-HSP case
709 my @line = split ' ',$descline;
710
711 # we want to throw away the score, evalue
712 pop @line, pop @line;
713  
714 # and N if it is present (of course they are not
715 # really in that order, but it doesn't matter
716 if ($3) { pop @line }
717  
718 # add the last 2 entries s.t. we can reconstruct
719 # a minimal Hit object at the end of the day
720 push @hit_signifs, [ $evalue, $score, shift @line, join( ' ', @line ) ];
721 } elsif ($descline =~ /^CONVERGED/i) {
722 $self->element(
723 {
724 'Name' => 'Iteration_converged',
725 'Data' => 1
726 }
727 );
728 } else {
729 $self->_pushback($descline); # Catch leading > (end of section)
730 last DESCLINE;
731 }
732 }
733 }
734 elsif (/Sequences producing High-scoring Segment Pairs:/) {
735  
736 # This block is for WU-BLAST, so we don't have to check for psi-blast stuff
737 # skip the next line
738 $self->debug("blast.pm: Processing WU-BLAST descriptions\n");
739 $_ = $self->_readline();
740 $flavor = 'wu';
741  
742 if ( !$self->in_element('iteration') ) {
743 $self->start_element( { 'Name' => 'Iteration' } );
744 }
745  
746 while ( defined( $_ = $self->_readline() )
747 && !/^\s+$/ )
748 {
749 my @line = split;
750 pop @line; # throw away first number which is for 'N'col
751  
752 # add the last 2 entries to array s.t. we can reconstruct
753 # a minimal Hit object at the end of the day
754 push @hit_signifs,
755 [ pop @line, pop @line, shift @line, join( ' ', @line ) ];
756 }
757
758 }
759 elsif (/^Database:\s*(.+)$/) {
760  
761 $self->debug("blast.pm: Database: $1\n");
762 my $db = $1;
763 while ( defined( $_ = $self->_readline ) ) {
764 if (
765 /^\s+(\-?[\d\,]+|\S+)\s+sequences\;
766 \s+(\-?[\d,]+|\S+)\s+ # Deal with NCBI 2.2.8 OSX problems
767 total\s+letters/ox
768 )
769 {
770 my ( $s, $l ) = ( $1, $2 );
771 $s =~ s/,//g;
772 $l =~ s/,//g;
773 $self->element(
774 {
775 'Name' => 'BlastOutput_db-len',
776 'Data' => $s
777 }
778 );
779 $self->element(
780 {
781 'Name' => 'BlastOutput_db-let',
782 'Data' => $l
783 }
784 );
785 # cache for next round in cases with multiple queries
786 $self->{'_blsdb'} = $db;
787 $self->{'_blsdb_length'} = $s;
788 $self->{'_blsdb_letters'} = $l;
789 last;
790 }
791 else {
792 chomp;
793 $db .= $_;
794 }
795 }
796 $self->element(
797 {
798 'Name' => 'BlastOutput_db',
799 'Data' => $db
800 }
801 );
802 }
803
804 # move inside of a hit
805 elsif (/^>\s*(\S+)\s*(.*)?/) {
806 chomp;
807  
808 $self->debug("blast.pm: Hit: $1\n");
809 $self->in_element('hsp')
810 && $self->end_element( { 'Name' => 'Hsp' } );
811 $self->in_element('hit')
812 && $self->end_element( { 'Name' => 'Hit' } );
813  
814 # special case when bl2seq reports don't have a leading
815 # Query=
816 if ( !$self->within_element('result') ) {
817 $self->_start_blastoutput;
818 $self->start_element( { 'Name' => 'Iteration' } );
819 }
820 elsif ( !$self->within_element('iteration') ) {
821 $self->start_element( { 'Name' => 'Iteration' } );
822 }
823 $self->start_element( { 'Name' => 'Hit' } );
824 my $id = $1;
825 my $restofline = $2;
826  
827 $self->debug("Starting a hit: $1 $2\n");
828 $self->element(
829 {
830 'Name' => 'Hit_id',
831 'Data' => $id
832 }
833 );
834 my ($gi, $acc, $version ) = $self->_get_seq_identifiers($id);
835 $self->element(
836 {
837 'Name' => 'Hit_accession',
838 'Data' => $acc
839 }
840 );
841 # add hit significance (from the hit table)
842 # this is where Bug 1986 went awry
843
844 # Changed for Bug2409; hit->significance and hit->score/bits derived
845 # from HSPs, not hit table unless necessary
846
847 HITTABLE:
848 while (my $v = shift @hit_signifs) {
849 my $tableid = $v->[2];
850 if ($tableid !~ m{\Q$id\E}) {
851 $self->debug("Hit table ID $tableid doesn't match current hit id $id, checking next hit table entry...\n");
852 next HITTABLE;
853 } else {
854 last HITTABLE;
855 }
856 }
857 while ( defined( $_ = $self->_readline() ) ) {
858 next if (/^\s+$/);
859 chomp;
860 if (/Length\s*=\s*([\d,]+)/) {
861 my $l = $1;
862 $l =~ s/\,//g;
863 $self->element(
864 {
865 'Name' => 'Hit_len',
866 'Data' => $l
867 }
868 );
869 last;
870 }
871 else {
872 s/^\s(?!\s)/\x01/; #new line to concatenate desc lines with <soh>
873 $restofline .= $_;
874 }
875 }
876 $restofline =~ s/\s+/ /g;
877 $self->element(
878 {
879 'Name' => 'Hit_def',
880 'Data' => $restofline
881 }
882 );
883 }
884 elsif (/\s+(Plus|Minus) Strand HSPs:/i) {
885 next;
886 }
887 elsif (
888 ( $self->in_element('hit') || $self->in_element('hsp') )
889 && # paracel genewise BTK
890 m/Score\s*=\s*(\S+)\s*bits\s* # Bit score
891 (?:\((\d+)\))?, # Raw score
892 \s+Log\-Length\sScore\s*=\s*(\d+) # Log-Length score
893 /ox
894 )
895 {
896 $self->in_element('hsp')
897 && $self->end_element( { 'Name' => 'Hsp' } );
898 $self->start_element( { 'Name' => 'Hsp' } );
899  
900 $self->debug( "Got paracel genewise HSP score=$1\n");
901  
902 # Some data clean-up so e-value will appear numeric to perl
903 my ( $bits, $score, $evalue ) = ( $1, $2, $3 );
904 $evalue =~ s/^e/1e/i;
905 $self->element(
906 {
907 'Name' => 'Hsp_score',
908 'Data' => $score
909 }
910 );
911 $self->element(
912 {
913 'Name' => 'Hsp_bit-score',
914 'Data' => $bits
915 }
916 );
917 $self->element(
918 {
919 'Name' => 'Hsp_evalue',
920 'Data' => $evalue
921 }
922 );
923 }
924 elsif (
925 ( $self->in_element('hit') || $self->in_element('hsp') )
926 && # paracel hframe BTK
927 m/Score\s*=\s*([^,\s]+), # Raw score
928 \s*Expect\s*=\s*([^,\s]+), # E-value
929 \s*P(?:\(\S+\))?\s*=\s*([^,\s]+) # P-value
930 /ox
931 )
932 {
933 $self->in_element('hsp')
934 && $self->end_element( { 'Name' => 'Hsp' } );
935 $self->start_element( { 'Name' => 'Hsp' } );
936  
937 $self->debug( "Got paracel hframe HSP score=$1\n");
938  
939 # Some data clean-up so e-value will appear numeric to perl
940 my ( $score, $evalue, $pvalue ) = ( $1, $2, $3 );
941 $evalue = "1$evalue" if $evalue =~ /^e/;
942 $pvalue = "1$pvalue" if $pvalue =~ /^e/;
943  
944 $self->element(
945 {
946 'Name' => 'Hsp_score',
947 'Data' => $score
948 }
949 );
950 $self->element(
951 {
952 'Name' => 'Hsp_evalue',
953 'Data' => $evalue
954 }
955 );
956 $self->element(
957 {
958 'Name' => 'Hsp_pvalue',
959 'Data' => $pvalue
960 }
961 );
962 }
963 elsif (
964 ( $self->in_element('hit') || $self->in_element('hsp') )
965 && # wublast
966 m/Score\s*=\s*(\S+)\s* # Bit score
967 \(([\d\.]+)\s*bits\), # Raw score
968 \s*Expect\s*=\s*([^,\s]+), # E-value
969 \s*(?:Sum)?\s* # SUM
970 P(?:\(\d+\))?\s*=\s*([^,\s]+) # P-value
971 (?:\s*,\s+Group\s*\=\s*(\d+))? # HSP Group
972 /ox
973 )
974 { # wu-blast HSP parse
975 $self->in_element('hsp')
976 && $self->end_element( { 'Name' => 'Hsp' } );
977 $self->start_element( { 'Name' => 'Hsp' } );
978  
979 # Some data clean-up so e-value will appear numeric to perl
980 my ( $score, $bits, $evalue, $pvalue, $group ) =
981 ( $1, $2, $3, $4, $5 );
982 $evalue =~ s/^e/1e/i;
983 $pvalue =~ s/^e/1e/i;
984  
985 $self->element(
986 {
987 'Name' => 'Hsp_score',
988 'Data' => $score
989 }
990 );
991 $self->element(
992 {
993 'Name' => 'Hsp_bit-score',
994 'Data' => $bits
995 }
996 );
997 $self->element(
998 {
999 'Name' => 'Hsp_evalue',
1000 'Data' => $evalue
1001 }
1002 );
1003 $self->element(
1004 {
1005 'Name' => 'Hsp_pvalue',
1006 'Data' => $pvalue
1007 }
1008 );
1009  
1010 if ( defined $group ) {
1011 $self->element(
1012 {
1013 'Name' => 'Hsp_group',
1014 'Data' => $group
1015 }
1016 );
1017 }
1018  
1019 }
1020 elsif (
1021 ( $self->in_element('hit') || $self->in_element('hsp') )
1022 && # ncbi blast, works with 2.2.17
1023 m/^\sFeatures\s\w+\sthis\spart/xmso
1024 # If the line begins with "Features in/flanking this part of subject sequence:"
1025 )
1026 {
1027 $self->in_element('hsp')
1028 && $self->end_element( { 'Name' => 'Hsp' } );
1029 my $featline;
1030 $_ = $self->_readline;
1031 while($_ !~ /^\s*$/) {
1032 chomp;
1033 $featline .= $_;
1034 $_ = $self->_readline;
1035 }
1036 $self->_pushback($_);
1037 $featline =~ s{(?:^\s+|\s+^)}{}g;
1038 $featline =~ s{\n}{;}g;
1039 $self->start_element( { 'Name' => 'Hsp' } );
1040 $self->element(
1041 {
1042 'Name' => 'Hsp_features',
1043 'Data' => $featline
1044 }
1045 );
1046 $self->{'_seen_hsp_features'} = 1;
1047 }
1048 elsif (
1049 ( $self->in_element('hit') || $self->in_element('hsp') )
1050 && # ncbi blast, works with 2.2.17
1051 m/Score\s*=\s*(\S+)\s*bits\s* # Bit score
1052 (?:\((\d+)\))?, # Missing for BLAT pseudo-BLAST fmt
1053 \s*Expect(?:\((\d+\+?)\))?\s*=\s*([^,\s]+) # E-value
1054 /ox
1055 )
1056 { # parse NCBI blast HSP
1057 if( !$self->{'_seen_hsp_features'} ) {
1058 $self->in_element('hsp')
1059 && $self->end_element( { 'Name' => 'Hsp' } );
1060 $self->start_element( { 'Name' => 'Hsp' } );
1061 }
1062  
1063 # Some data clean-up so e-value will appear numeric to perl
1064 my ( $bits, $score, $n, $evalue ) = ( $1, $2, $3, $4 );
1065 $evalue =~ s/^e/1e/i;
1066 $self->element(
1067 {
1068 'Name' => 'Hsp_score',
1069 'Data' => $score
1070 }
1071 );
1072 $self->element(
1073 {
1074 'Name' => 'Hsp_bit-score',
1075 'Data' => $bits
1076 }
1077 );
1078 $self->element(
1079 {
1080 'Name' => 'Hsp_evalue',
1081 'Data' => $evalue
1082 }
1083 );
1084 $self->element(
1085 {
1086 'Name' => 'Hsp_n',
1087 'Data' => $n
1088 }
1089 ) if defined $n;
1090 $score = '' unless defined $score; # deal with BLAT which
1091 # has no score only bits
1092 $self->debug("Got NCBI HSP score=$score, evalue $evalue\n");
1093 }
1094 elsif (
1095 $self->in_element('hsp')
1096 && m/Identities\s*=\s*(\d+)\s*\/\s*(\d+)\s*[\d\%\(\)]+\s*
1097 (?:,\s*Positives\s*=\s*(\d+)\/(\d+)\s*[\d\%\(\)]+\s*)? # pos only valid for Protein alignments
1098 (?:\,\s*Gaps\s*=\s*(\d+)\/(\d+))? # Gaps
1099 /oxi
1100 )
1101 {
1102 $self->element(
1103 {
1104 'Name' => 'Hsp_identity',
1105 'Data' => $1
1106 }
1107 );
1108 $self->element(
1109 {
1110 'Name' => 'Hsp_align-len',
1111 'Data' => $2
1112 }
1113 );
1114 if ( defined $3 ) {
1115 $self->element(
1116 {
1117 'Name' => 'Hsp_positive',
1118 'Data' => $3
1119 }
1120 );
1121 }
1122 else {
1123 $self->element(
1124 {
1125 'Name' => 'Hsp_positive',
1126 'Data' => $1
1127 }
1128 );
1129 }
1130 if ( defined $6 ) {
1131 $self->element(
1132 {
1133 'Name' => 'Hsp_gaps',
1134 'Data' => $5
1135 }
1136 );
1137 }
1138  
1139 $self->{'_Query'} = { 'begin' => 0, 'end' => 0 };
1140 $self->{'_Sbjct'} = { 'begin' => 0, 'end' => 0 };
1141  
1142 if (/(Frame\s*=\s*.+)$/) {
1143  
1144 # handle wu-blast Frame listing on same line
1145 $self->_pushback($1);
1146 }
1147 }
1148 elsif ( $self->in_element('hsp')
1149 && /Strand\s*=\s*(Plus|Minus)\s*\/\s*(Plus|Minus)/i )
1150 {
1151  
1152 # consume this event ( we infer strand from start/end)
1153 unless ($reporttype) {
1154 $self->{'_reporttype'} = $reporttype = 'BLASTN';
1155 $bl2seq_fix = 1; # special case to resubmit the algorithm
1156 # reporttype
1157 }
1158 next;
1159 }
1160 elsif ( $self->in_element('hsp')
1161 && /Links\s*=\s*(\S+)/ox )
1162 {
1163 $self->element(
1164 {
1165 'Name' => 'Hsp_links',
1166 'Data' => $1
1167 }
1168 );
1169 }
1170 elsif ( $self->in_element('hsp')
1171 && /Frame\s*=\s*([\+\-][1-3])\s*(\/\s*([\+\-][1-3]))?/ )
1172 {
1173  
1174 # this is for bl2seq only
1175 unless ( defined $reporttype ) {
1176 $bl2seq_fix = 1;
1177 if ( $1 && $2 ) { $reporttype = 'TBLASTX' }
1178 else {
1179 $reporttype = 'BLASTX';
1180  
1181 # we can't distinguish between BLASTX and TBLASTN straight from the report }
1182 }
1183 $self->{'_reporttype'} = $reporttype;
1184 }
1185  
1186 my ( $queryframe, $hitframe );
1187 if ( $reporttype eq 'TBLASTX' ) {
1188 ( $queryframe, $hitframe ) = ( $1, $2 );
1189 $hitframe =~ s/\/\s*//g;
1190 }
1191 elsif ( $reporttype eq 'TBLASTN' || $reporttype eq 'PSITBLASTN') {
1192 ( $hitframe, $queryframe ) = ( $1, 0 );
1193 }
1194 elsif ( $reporttype eq 'BLASTX' || $reporttype eq 'RPS-BLAST(BLASTP)') {
1195 ( $queryframe, $hitframe ) = ( $1, 0 );
1196 # though NCBI doesn't report it, this is a special BLASTX-like
1197 # RPS-BLAST; should be handled differently
1198 if ($reporttype eq 'RPS-BLAST(BLASTP)') {
1199 $self->element(
1200 {
1201 'Name' => 'BlastOutput_program',
1202 'Data' => 'RPS-BLAST(BLASTX)'
1203 }
1204 );
1205 }
1206 }
1207 $self->element(
1208 {
1209 'Name' => 'Hsp_query-frame',
1210 'Data' => $queryframe
1211 }
1212 );
1213  
1214 $self->element(
1215 {
1216 'Name' => 'Hsp_hit-frame',
1217 'Data' => $hitframe
1218 }
1219 );
1220 }
1221 elsif (/^Parameters:/
1222 || /^\s+Database:\s+?/
1223 || /^\s+Subset/
1224 || /^\s*Lambda/
1225 || /^\s*Histogram/
1226 || ( $self->in_element('hsp') && /WARNING|NOTE/ ) )
1227 {
1228  
1229 # Note: Lambda check was necessary to parse
1230 # t/data/ecoli_domains.rpsblast AND to parse bl2seq
1231 $self->debug("blast.pm: found parameters section \n");
1232  
1233 $self->in_element('hsp')
1234 && $self->end_element( { 'Name' => 'Hsp' } );
1235 $self->in_element('hit')
1236 && $self->end_element( { 'Name' => 'Hit' } );
1237  
1238 # This is for the case when we specify -b 0 (or B=0 for WU-BLAST)
1239 # and still want to construct minimal Hit objects
1240 $self->_cleanup_hits(\@hit_signifs) if scalar(@hit_signifs);
1241 $self->within_element('iteration')
1242 && $self->end_element( { 'Name' => 'Iteration' } );
1243  
1244 next if /^\s+Subset/;
1245 my $blast = (/^(\s+Database\:)|(\s*Lambda)/) ? 'ncbi' : 'wublast';
1246 if (/^\s*Histogram/) {
1247 $blast = 'btk';
1248 }
1249  
1250 my $last = '';
1251  
1252 # default is that gaps are allowed
1253 $self->element(
1254 {
1255 'Name' => 'Parameters_allowgaps',
1256 'Data' => 'yes'
1257 }
1258 );
1259 while ( defined( $_ = $self->_readline ) ) {
1260 if (
1261 /^((?:\S+)?BLAST[NPX]?)\s+(.+)$/i # NCBI BLAST, PSIBLAST
1262 # RPSBLAST, MEGABLAST
1263 || /^(P?GENEWISE|HFRAME|SWN|TSWN)\s+(.+)/i #Paracel BTK
1264 )
1265 {
1266 $self->_pushback($_);
1267  
1268 # let's handle this in the loop
1269 last;
1270 }
1271 elsif (/^Query=/) {
1272 $self->_pushback($reportline) if $reportline;
1273 $self->_pushback($_);
1274 last PARSER;
1275 }
1276  
1277 # here is where difference between wublast and ncbiblast
1278 # is better handled by different logic
1279 if ( /Number of Sequences:\s+([\d\,]+)/i
1280 || /of sequences in database:\s+(\-?[\d,]+)/i )
1281 {
1282 my $c = $1;
1283 $c =~ s/\,//g;
1284 $self->element(
1285 {
1286 'Name' => 'Statistics_db-len',
1287 'Data' => $c
1288 }
1289 );
1290 }
1291 elsif (/letters in database:\s+(\-?[\d,]+)/i) {
1292 my $s = $1;
1293 $s =~ s/,//g;
1294 $self->element(
1295 {
1296 'Name' => 'Statistics_db-let',
1297 'Data' => $s
1298 }
1299 );
1300 }
1301 elsif ( $blast eq 'btk' ) {
1302 next;
1303 }
1304 elsif ( $blast eq 'wublast' ) {
1305  
1306 # warn($_);
1307 if (/E=(\S+)/) {
1308 $self->element(
1309 {
1310 'Name' => 'Parameters_expect',
1311 'Data' => $1
1312 }
1313 );
1314 }
1315 elsif (/nogaps/) {
1316 $self->element(
1317 {
1318 'Name' => 'Parameters_allowgaps',
1319 'Data' => 'no'
1320 }
1321 );
1322 }
1323 elsif (/ctxfactor=(\S+)/) {
1324 $self->element(
1325 {
1326 'Name' => 'Statistics_ctxfactor',
1327 'Data' => $1
1328 }
1329 );
1330 }
1331 elsif (
1332 /(postsw|links|span[12]?|warnings|notes|gi|noseqs|qres|qype)/
1333 )
1334 {
1335 $self->element(
1336 {
1337 'Name' => "Parameters_$1",
1338 'Data' => 'yes'
1339 }
1340 );
1341 }
1342 elsif (/(\S+)=(\S+)/) {
1343 $self->element(
1344 {
1345 'Name' => "Parameters_$1",
1346 'Data' => $2
1347 }
1348 );
1349 }
1350 elsif ( $last =~ /(Frame|Strand)\s+MatID\s+Matrix name/i ) {
1351 my $firstgapinfo = 1;
1352 my $frame = undef;
1353 while ( defined($_) && !/^\s+$/ ) {
1354 s/^\s+//;
1355 s/\s+$//;
1356 if ( $firstgapinfo
1357 && s/Q=(\d+),R=(\d+)\s+//x )
1358 {
1359 $firstgapinfo = 0;
1360  
1361 $self->element(
1362 {
1363 'Name' => 'Parameters_gap-open',
1364 'Data' => $1
1365 }
1366 );
1367 $self->element(
1368 {
1369 'Name' => 'Parameters_gap-extend',
1370 'Data' => $2
1371 }
1372 );
1373 my @fields = split;
1374  
1375 for my $type (
1376 qw(lambda_gapped
1377 kappa_gapped
1378 entropy_gapped)
1379 )
1380 {
1381 next if $type eq 'n/a';
1382 if ( !@fields ) {
1383 warn "fields is empty for $type\n";
1384 next;
1385 }
1386 $self->element(
1387 {
1388 'Name' =>
1389 "Statistics_frame$frame\_$type",
1390 'Data' => shift @fields
1391 }
1392 );
1393 }
1394 }
1395 else {
1396 my ( $frameo, $matid, $matrix, @fields ) =
1397 split;
1398 if ( !defined $frame ) {
1399  
1400 # keep some sort of default feature I guess
1401 # even though this is sort of wrong
1402 $self->element(
1403 {
1404 'Name' => 'Parameters_matrix',
1405 'Data' => $matrix
1406 }
1407 );
1408 $self->element(
1409 {
1410 'Name' => 'Statistics_lambda',
1411 'Data' => $fields[0]
1412 }
1413 );
1414 $self->element(
1415 {
1416 'Name' => 'Statistics_kappa',
1417 'Data' => $fields[1]
1418 }
1419 );
1420 $self->element(
1421 {
1422 'Name' => 'Statistics_entropy',
1423 'Data' => $fields[2]
1424 }
1425 );
1426 }
1427 $frame = $frameo;
1428 my $ii = 0;
1429 for my $type (
1430 qw(lambda_used
1431 kappa_used
1432 entropy_used
1433 lambda_computed
1434 kappa_computed
1435 entropy_computed)
1436 )
1437 {
1438 my $f = $fields[$ii];
1439 next unless defined $f; # deal with n/a
1440 if ( $f eq 'same' ) {
1441 $f = $fields[ $ii - 3 ];
1442 }
1443 $ii++;
1444 $self->element(
1445 {
1446 'Name' =>
1447 "Statistics_frame$frame\_$type",
1448 'Data' => $f
1449 }
1450 );
1451  
1452 }
1453 }
1454  
1455 # get the next line
1456 $_ = $self->_readline;
1457 }
1458 $last = $_;
1459 }
1460 elsif ( $last =~ /(Frame|Strand)\s+MatID\s+Length/i ) {
1461 my $frame = undef;
1462 while ( defined($_) && !/^\s+/ ) {
1463 s/^\s+//;
1464 s/\s+$//;
1465 my @fields = split;
1466 if ( @fields <= 3 ) {
1467 for my $type (qw(X_gapped E2_gapped S2)) {
1468 last unless @fields;
1469 $self->element(
1470 {
1471 'Name' =>
1472 "Statistics_frame$frame\_$type",
1473 'Data' => shift @fields
1474 }
1475 );
1476 }
1477 }
1478 else {
1479  
1480 for my $type (
1481 qw(length
1482 efflength
1483 E S W T X E2 S2)
1484 )
1485 {
1486 $self->element(
1487 {
1488 'Name' =>
1489 "Statistics_frame$frame\_$type",
1490 'Data' => shift @fields
1491 }
1492 );
1493 }
1494 }
1495 $_ = $self->_readline;
1496 }
1497 $last = $_;
1498 }
1499 elsif (/(\S+\s+\S+)\s+DFA:\s+(\S+)\s+\((.+)\)/) {
1500 if ( $1 eq 'states in' ) {
1501 $self->element(
1502 {
1503 'Name' => 'Statistics_DFA_states',
1504 'Data' => "$2 $3"
1505 }
1506 );
1507 }
1508 elsif ( $1 eq 'size of' ) {
1509 $self->element(
1510 {
1511 'Name' => 'Statistics_DFA_size',
1512 'Data' => "$2 $3"
1513 }
1514 );
1515 }
1516 }
1517 elsif (
1518 m/^\s+Time to generate neighborhood:\s+
1519 (\S+\s+\S+\s+\S+)/x
1520 )
1521 {
1522 $self->element(
1523 {
1524 'Name' => 'Statistics_neighbortime',
1525 'Data' => $1
1526 }
1527 );
1528 }
1529 elsif (/processors\s+used:\s+(\d+)/) {
1530 $self->element(
1531 {
1532 'Name' => 'Statistics_noprocessors',
1533 'Data' => $1
1534 }
1535 );
1536 }
1537 elsif (
1538 m/^\s+(\S+)\s+cpu\s+time:\s+# cputype
1539 (\S+\s+\S+\s+\S+) # cputime
1540 \s+Elapsed:\s+(\S+)/x
1541 )
1542 {
1543 my $cputype = lc($1);
1544 $self->element(
1545 {
1546 'Name' => "Statistics_$cputype\_cputime",
1547 'Data' => $2
1548 }
1549 );
1550 $self->element(
1551 {
1552 'Name' => "Statistics_$cputype\_actualtime",
1553 'Data' => $3
1554 }
1555 );
1556 }
1557 elsif (/^\s+Start:/) {
1558 my ( $junk, $start, $stime, $end, $etime ) =
1559 split( /\s+(Start|End)\:\s+/, $_ );
1560 chomp($stime);
1561 $self->element(
1562 {
1563 'Name' => 'Statistics_starttime',
1564 'Data' => $stime
1565 }
1566 );
1567 chomp($etime);
1568 $self->element(
1569 {
1570 'Name' => 'Statistics_endtime',
1571 'Data' => $etime
1572 }
1573 );
1574 }
1575 elsif (/^\s+Database:\s+(.+)$/) {
1576 $self->element(
1577 {
1578 'Name' => 'Parameters_full_dbpath',
1579 'Data' => $1
1580 }
1581 );
1582  
1583 }
1584 elsif (/^\s+Posted:\s+(.+)/) {
1585 my $d = $1;
1586 chomp($d);
1587 $self->element(
1588 {
1589 'Name' => 'Statistics_posted_date',
1590 'Data' => $d
1591 }
1592 );
1593 }
1594 }
1595 elsif ( $blast eq 'ncbi' ) {
1596  
1597 if (m/^Matrix:\s+(.+)\s*$/oxi) {
1598 $self->element(
1599 {
1600 'Name' => 'Parameters_matrix',
1601 'Data' => $1
1602 }
1603 );
1604 }
1605 elsif (/^Gapped/) {
1606 $gapped_stats = 1;
1607 }
1608 elsif (/^Lambda/) {
1609 $_ = $self->_readline;
1610 s/^\s+//;
1611 my ( $lambda, $kappa, $entropy ) = split;
1612 if ($gapped_stats) {
1613 $self->element(
1614 {
1615 'Name' => "Statistics_gapped_lambda",
1616 'Data' => $lambda
1617 }
1618 );
1619 $self->element(
1620 {
1621 'Name' => "Statistics_gapped_kappa",
1622 'Data' => $kappa
1623 }
1624 );
1625 $self->element(
1626 {
1627 'Name' => "Statistics_gapped_entropy",
1628 'Data' => $entropy
1629 }
1630 );
1631 }
1632 else {
1633 $self->element(
1634 {
1635 'Name' => "Statistics_lambda",
1636 'Data' => $lambda
1637 }
1638 );
1639 $self->element(
1640 {
1641 'Name' => "Statistics_kappa",
1642 'Data' => $kappa
1643 }
1644 );
1645 $self->element(
1646 {
1647 'Name' => "Statistics_entropy",
1648 'Data' => $entropy
1649 }
1650 );
1651 }
1652 }
1653 elsif (m/effective\s+search\s+space\s+used:\s+(\d+)/oxi) {
1654 $self->element(
1655 {
1656 'Name' => 'Statistics_eff-spaceused',
1657 'Data' => $1
1658 }
1659 );
1660 }
1661 elsif (m/effective\s+search\s+space:\s+(\d+)/oxi) {
1662 $self->element(
1663 {
1664 'Name' => 'Statistics_eff-space',
1665 'Data' => $1
1666 }
1667 );
1668 }
1669 elsif (
1670 m/Gap\s+Penalties:\s+Existence:\s+(\d+)\,
1671 \s+Extension:\s+(\d+)/ox
1672 )
1673 {
1674 $self->element(
1675 {
1676 'Name' => 'Parameters_gap-open',
1677 'Data' => $1
1678 }
1679 );
1680 $self->element(
1681 {
1682 'Name' => 'Parameters_gap-extend',
1683 'Data' => $2
1684 }
1685 );
1686 }
1687 elsif (/effective\s+HSP\s+length:\s+(\d+)/) {
1688 $self->element(
1689 {
1690 'Name' => 'Statistics_hsp-len',
1691 'Data' => $1
1692 }
1693 );
1694 }
1695 elsif (/Number\s+of\s+HSP's\s+better\s+than\s+(\S+)\s+without\s+gapping:\s+(\d+)/) {
1696 $self->element(
1697 {
1698 'Name' => 'Statistics_number_of_hsps_better_than_expect_value_cutoff_without_gapping',
1699 'Data' => $2
1700 }
1701 );
1702 }
1703 elsif (/Number\s+of\s+HSP's\s+gapped:\s+(\d+)/) {
1704 $self->element(
1705 {
1706 'Name' => 'Statistics_number_of_hsps_gapped',
1707 'Data' => $1
1708 }
1709 );
1710 }
1711 elsif (/Number\s+of\s+HSP's\s+successfully\s+gapped:\s+(\d+)/) {
1712 $self->element(
1713 {
1714 'Name' => 'Statistics_number_of_hsps_successfully_gapped',
1715 'Data' => $1
1716 }
1717 );
1718 }
1719 elsif (/Length\s+adjustment:\s+(\d+)/) {
1720 $self->element(
1721 {
1722 'Name' => 'Statistics_length_adjustment',
1723 'Data' => $1
1724 }
1725 );
1726 }
1727 elsif (/effective\s+length\s+of\s+query:\s+([\d\,]+)/i) {
1728 my $c = $1;
1729 $c =~ s/\,//g;
1730 $self->element(
1731 {
1732 'Name' => 'Statistics_query-len',
1733 'Data' => $c
1734 }
1735 );
1736 }
1737 elsif (/effective\s+length\s+of\s+database:\s+([\d\,]+)/i) {
1738 my $c = $1;
1739 $c =~ s/\,//g;
1740 $self->element(
1741 {
1742 'Name' => 'Statistics_eff-dblen',
1743 'Data' => $c
1744 }
1745 );
1746 }
1747 elsif (
1748 /^(T|A|X1|X2|X3|S1|S2):\s+(\d+(\.\d+)?)\s+(?:\(\s*(\d+\.\d+) bits\))?/
1749 )
1750 {
1751 my $v = $2;
1752 chomp($v);
1753 $self->element(
1754 {
1755 'Name' => "Statistics_$1",
1756 'Data' => $v
1757 }
1758 );
1759 if ( defined $4 ) {
1760 $self->element(
1761 {
1762 'Name' => "Statistics_$1_bits",
1763 'Data' => $4
1764 }
1765 );
1766 }
1767 }
1768 elsif (
1769 m/frameshift\s+window\,
1770 \s+decay\s+const:\s+(\d+)\,\s+([\.\d]+)/x
1771 )
1772 {
1773 $self->element(
1774 {
1775 'Name' => 'Statistics_framewindow',
1776 'Data' => $1
1777 }
1778 );
1779 $self->element(
1780 {
1781 'Name' => 'Statistics_decay',
1782 'Data' => $2
1783 }
1784 );
1785 }
1786 elsif (m/^Number\s+of\s+Hits\s+to\s+DB:\s+(\S+)/ox) {
1787 $self->element(
1788 {
1789 'Name' => 'Statistics_hit_to_db',
1790 'Data' => $1
1791 }
1792 );
1793 }
1794 elsif (m/^Number\s+of\s+extensions:\s+(\S+)/ox) {
1795 $self->element(
1796 {
1797 'Name' => 'Statistics_num_extensions',
1798 'Data' => $1
1799 }
1800 );
1801 }
1802 elsif (
1803 m/^Number\s+of\s+successful\s+extensions:\s+
1804 (\S+)/ox
1805 )
1806 {
1807 $self->element(
1808 {
1809 'Name' => 'Statistics_num_suc_extensions',
1810 'Data' => $1
1811 }
1812 );
1813 }
1814 elsif (
1815 m/^Number\s+of\s+sequences\s+better\s+than\s+
1816 (\S+):\s+(\d+)/ox
1817 )
1818 {
1819 $self->element(
1820 {
1821 'Name' => 'Parameters_expect',
1822 'Data' => $1
1823 }
1824 );
1825 $self->element(
1826 {
1827 'Name' => 'Statistics_seqs_better_than_cutoff',
1828 'Data' => $2
1829 }
1830 );
1831 }
1832 elsif (/^\s+Posted\s+date:\s+(.+)/) {
1833 my $d = $1;
1834 chomp($d);
1835 $self->element(
1836 {
1837 'Name' => 'Statistics_posted_date',
1838 'Data' => $d
1839 }
1840 );
1841 }
1842 elsif ( !/^\s+$/ ) {
1843 #$self->debug( "unmatched stat $_");
1844 }
1845 }
1846 $last = $_;
1847 }
1848 } elsif ( $self->in_element('hsp') ) {
1849 $self->debug("blast.pm: Processing HSP\n");
1850 # let's read 3 lines at a time;
1851 # bl2seq hackiness... Not sure I like
1852 $self->{'_reporttype'} ||= $DEFAULTREPORTTYPE;
1853 my %data = (
1854 'Query' => '',
1855 'Mid' => '',
1856 'Hit' => ''
1857 );
1858 my $len;
1859 for ( my $i = 0 ; defined($_) && $i < 3 ; $i++ ) {
1860 # $self->debug("$i: $_") if $v;
1861 if ( ( $i == 0 && /^\s+$/) ||
1862 /^\s*(?:Lambda|Minus|Plus|Score)/i )
1863 {
1864 $self->_pushback($_) if defined $_;
1865 $self->end_element( { 'Name' => 'Hsp' } );
1866 last;
1867 }
1868 chomp;
1869 if (/^((Query|Sbjct):?\s+(\-?\d+)?\s*)(\S+)\s+(\-?\d+)?/) {
1870 my ( $full, $type, $start, $str, $end ) =
1871 ( $1, $2, $3, $4, $5 );
1872  
1873 if ( $str eq '-' ) {
1874 $i = 3 if $type eq 'Sbjct';
1875 }
1876 else {
1877 $data{$type} = $str;
1878 }
1879 $len = length($full);
1880 $self->{"\_$type"}->{'begin'} = $start
1881 unless $self->{"_$type"}->{'begin'};
1882 $self->{"\_$type"}->{'end'} = $end;
1883 } elsif (/^((Query|Sbjct):?\s+(\-?0+)\s*)/) {
1884 # Bug from NCBI's BLAST: unaligned output
1885 $_ = $self->_readline() for 0..1;
1886 last;
1887 } else {
1888 $self->throw("no data for midline $_")
1889 unless ( defined $_ && defined $len );
1890 $data{'Mid'} = substr( $_, $len );
1891 }
1892 $_ = $self->_readline();
1893 }
1894 $self->characters(
1895 {
1896 'Name' => 'Hsp_qseq',
1897 'Data' => $data{'Query'}
1898 }
1899 );
1900 $self->characters(
1901 {
1902 'Name' => 'Hsp_hseq',
1903 'Data' => $data{'Sbjct'}
1904 }
1905 );
1906 $self->characters(
1907 {
1908 'Name' => 'Hsp_midline',
1909 'Data' => $data{'Mid'}
1910 }
1911 );
1912 }
1913 else {
1914 #$self->debug("blast.pm: unrecognized line $_");
1915 }
1916 }
1917  
1918 $self->debug("blast.pm: End of BlastOutput\n");
1919 if ( $self->{'_seentop'} ) {
1920 $self->within_element('hsp')
1921 && $self->end_element( { 'Name' => 'Hsp' } );
1922 $self->within_element('hit')
1923 && $self->end_element( { 'Name' => 'Hit' } );
1924 # cleanup extra hits
1925 $self->_cleanup_hits(\@hit_signifs) if scalar(@hit_signifs);
1926 $self->within_element('iteration')
1927 && $self->end_element( { 'Name' => 'Iteration' } );
1928 if ($bl2seq_fix) {
1929 $self->element(
1930 {
1931 'Name' => 'BlastOutput_program',
1932 'Data' => $reporttype
1933 }
1934 );
1935 }
1936 $self->end_element( { 'Name' => 'BlastOutput' } );
1937 }
1938 return $self->end_document();
1939 }
1940  
1941 # Private method for internal use only.
1942 sub _start_blastoutput {
1943 my $self = shift;
1944 $self->start_element( { 'Name' => 'BlastOutput' } );
1945 $self->{'_seentop'} = 1;
1946 $self->{'_result_count'}++;
1947 $self->{'_handler_rc'} = undef;
1948 }
1949  
1950 =head2 _will_handle
1951  
1952 Title : _will_handle
1953 Usage : Private method. For internal use only.
1954 if( $self->_will_handle($type) ) { ... }
1955 Function: Provides an optimized way to check whether or not an element of a
1956 given type is to be handled.
1957 Returns : Reference to EventHandler object if the element type is to be handled.
1958 undef if the element type is not to be handled.
1959 Args : string containing type of element.
1960  
1961 Optimizations:
1962  
1963 =over 2
1964  
1965 =item 1
1966  
1967 Using the cached pointer to the EventHandler to minimize repeated
1968 lookups.
1969  
1970 =item 2
1971  
1972 Caching the will_handle status for each type that is encountered so
1973 that it only need be checked by calling
1974 handler-E<gt>will_handle($type) once.
1975  
1976 =back
1977  
1978 This does not lead to a major savings by itself (only 5-10%). In
1979 combination with other optimizations, or for large parse jobs, the
1980 savings good be significant.
1981  
1982 To test against the unoptimized version, remove the parentheses from
1983 around the third term in the ternary " ? : " operator and add two
1984 calls to $self-E<gt>_eventHandler().
1985  
1986 =cut
1987  
1988 sub _will_handle {
1989 my ( $self, $type ) = @_;
1990 my $handler = $self->{'_handler_cache'};
1991 my $will_handle =
1992 defined( $self->{'_will_handle_cache'}->{$type} )
1993 ? $self->{'_will_handle_cache'}->{$type}
1994 : ( $self->{'_will_handle_cache'}->{$type} =
1995 $handler->will_handle($type) );
1996  
1997 return $will_handle ? $handler : undef;
1998 }
1999  
2000 =head2 start_element
2001  
2002 Title : start_element
2003 Usage : $eventgenerator->start_element
2004 Function: Handles a start element event
2005 Returns : none
2006 Args : hashref with at least 2 keys 'Data' and 'Name'
2007  
2008 =cut
2009  
2010 sub start_element {
2011 my ( $self, $data ) = @_;
2012  
2013 # we currently don't care about attributes
2014 my $nm = $data->{'Name'};
2015 my $type = $MODEMAP{$nm};
2016 if ($type) {
2017 my $handler = $self->_will_handle($type);
2018 if ($handler) {
2019 my $func = sprintf( "start_%s", lc $type );
2020 $self->{'_handler_rc'} = $handler->$func( $data->{'Attributes'} );
2021 }
2022 #else {
2023 #$self->debug( # changed 4/29/2006 to play nice with other event handlers
2024 # "FAST::Bio::SearchIO::InternalParserError ".
2025 # "\nCan't handle elements of type \'$type.\'"
2026 #);
2027 #}
2028 unshift @{ $self->{'_elements'} }, $type;
2029 if ( $type eq 'result' ) {
2030 $self->{'_values'} = {};
2031 $self->{'_result'} = undef;
2032 } else {
2033 # cleanup some things
2034 if ( defined $self->{'_values'} ) {
2035 foreach my $k (
2036 grep { /^\U$type\-/ }
2037 keys %{ $self->{'_values'} }
2038 )
2039 {
2040 delete $self->{'_values'}->{$k};
2041 }
2042 }
2043 }
2044 }
2045 }
2046  
2047 =head2 end_element
2048  
2049 Title : end_element
2050 Usage : $eventgenerator->end_element
2051 Function: Handles an end element event
2052 Returns : hashref with an element's worth of data
2053 Args : hashref with at least 2 keys 'Data' and 'Name'
2054  
2055  
2056 =cut
2057  
2058 sub end_element {
2059 my ( $self, $data ) = @_;
2060
2061 my $nm = $data->{'Name'};
2062 my $type;
2063 my $rc;
2064 if ( $nm eq 'BlastOutput_program' ) {
2065 if ( $self->{'_last_data'} =~ /(t?blast[npx])/i ) {
2066 $self->{'_reporttype'} = uc $1;
2067 }
2068 $self->{'_reporttype'} ||= $DEFAULTREPORTTYPE;
2069 }
2070  
2071 # Hsps are sort of weird, in that they end when another
2072 # object begins so have to detect this in end_element for now
2073 if ( $nm eq 'Hsp' ) {
2074 foreach (qw(Hsp_qseq Hsp_midline Hsp_hseq Hsp_features)) {
2075 $self->element(
2076 {
2077 'Name' => $_,
2078 'Data' => $self->{'_last_hspdata'}->{$_}
2079 }
2080 ) if defined $self->{'_last_hspdata'}->{$_};
2081 }
2082 $self->{'_last_hspdata'} = {};
2083 $self->element(
2084 {
2085 'Name' => 'Hsp_query-from',
2086 'Data' => $self->{'_Query'}->{'begin'}
2087 }
2088 );
2089 $self->element(
2090 {
2091 'Name' => 'Hsp_query-to',
2092 'Data' => $self->{'_Query'}->{'end'}
2093 }
2094 );
2095  
2096 $self->element(
2097 {
2098 'Name' => 'Hsp_hit-from',
2099 'Data' => $self->{'_Sbjct'}->{'begin'}
2100 }
2101 );
2102 $self->element(
2103 {
2104 'Name' => 'Hsp_hit-to',
2105 'Data' => $self->{'_Sbjct'}->{'end'}
2106 }
2107 );
2108  
2109 # } elsif( $nm eq 'Iteration' ) {
2110 # Nothing special needs to be done here.
2111 }
2112 if ( $type = $MODEMAP{$nm} ) {
2113 my $handler = $self->_will_handle($type);
2114 if ($handler) {
2115 my $func = sprintf( "end_%s", lc $type );
2116 $rc = $handler->$func( $self->{'_reporttype'}, $self->{'_values'} );
2117 }
2118 shift @{ $self->{'_elements'} };
2119  
2120 }
2121 elsif ( $MAPPING{$nm} ) {
2122 if ( ref( $MAPPING{$nm} ) =~ /hash/i ) {
2123  
2124 # this is where we shove in the data from the
2125 # hashref info about params or statistics
2126 my $key = ( keys %{ $MAPPING{$nm} } )[0];
2127 $self->{'_values'}->{$key}->{ $MAPPING{$nm}->{$key} } =
2128 $self->{'_last_data'};
2129 }
2130 else {
2131 $self->{'_values'}->{ $MAPPING{$nm} } = $self->{'_last_data'};
2132 }
2133 }
2134 else {
2135 #$self->debug("blast.pm: unknown nm $nm, ignoring\n");
2136 }
2137 $self->{'_last_data'} = ''; # remove read data if we are at
2138 # end of an element
2139 $self->{'_result'} = $rc if ( defined $type && $type eq 'result' );
2140 $self->{'_seen_hsp_features'} = 0;
2141 return $rc;
2142 }
2143  
2144 =head2 element
2145  
2146 Title : element
2147 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
2148 Function: Convenience method that calls start_element, characters, end_element
2149 Returns : none
2150 Args : Hash ref with the keys 'Name' and 'Data'
2151  
2152  
2153 =cut
2154  
2155 sub element {
2156 my ( $self, $data ) = @_;
2157 # Note that start element isn't needed for character data
2158 # Not too SAX-y, though
2159 #$self->start_element($data);
2160 $self->characters($data);
2161 $self->end_element($data);
2162 }
2163  
2164 =head2 characters
2165  
2166 Title : characters
2167 Usage : $eventgenerator->characters($str)
2168 Function: Send a character events
2169 Returns : none
2170 Args : string
2171  
2172  
2173 =cut
2174  
2175 sub characters {
2176 my ( $self, $data ) = @_;
2177 if ( $self->in_element('hsp')
2178 && $data->{'Name'} =~ /^Hsp\_(qseq|hseq|midline)$/ )
2179 {
2180 $self->{'_last_hspdata'}->{ $data->{'Name'} } .= $data->{'Data'}
2181 if defined $data->{'Data'};
2182 }
2183 return unless ( defined $data->{'Data'} && $data->{'Data'} !~ /^\s+$/ );
2184 $self->{'_last_data'} = $data->{'Data'};
2185 }
2186  
2187 =head2 within_element
2188  
2189 Title : within_element
2190 Usage : if( $eventgenerator->within_element($element) ) {}
2191 Function: Test if we are within a particular element
2192 This is different than 'in' because within can be tested
2193 for a whole block.
2194 Returns : boolean
2195 Args : string element name
2196  
2197 See Also: L<in_element>
2198  
2199 =cut
2200  
2201 sub within_element {
2202 my ( $self, $name ) = @_;
2203 return 0
2204 if ( !defined $name && !defined $self->{'_elements'}
2205 || scalar @{ $self->{'_elements'} } == 0 );
2206 foreach ( @{ $self->{'_elements'} } ) {
2207 if ( $_ eq $name ) {
2208 return 1;
2209 }
2210 }
2211 return 0;
2212 }
2213  
2214 =head2 in_element
2215  
2216 Title : in_element
2217 Usage : if( $eventgenerator->in_element($element) ) {}
2218 Function: Test if we are in a particular element
2219 This is different than 'within_element' because within
2220 can be tested for a whole block.
2221 Returns : boolean
2222 Args : string element name
2223  
2224 See Also: L<within_element>
2225  
2226 =cut
2227  
2228 sub in_element {
2229 my ( $self, $name ) = @_;
2230 return 0 if !defined $self->{'_elements'}->[0];
2231 return ( $self->{'_elements'}->[0] eq $name );
2232 }
2233  
2234 =head2 start_document
2235  
2236 Title : start_document
2237 Usage : $eventgenerator->start_document
2238 Function: Handle a start document event
2239 Returns : none
2240 Args : none
2241  
2242  
2243 =cut
2244  
2245 sub start_document {
2246 my ($self) = @_;
2247 $self->{'_lasttype'} = '';
2248 $self->{'_values'} = {};
2249 $self->{'_result'} = undef;
2250 $self->{'_elements'} = [];
2251 }
2252  
2253 =head2 end_document
2254  
2255 Title : end_document
2256 Usage : $eventgenerator->end_document
2257 Function: Handles an end document event
2258 Returns : FAST::Bio::Search::Result::ResultI object
2259 Args : none
2260  
2261  
2262 =cut
2263  
2264 sub end_document {
2265 my ( $self, @args ) = @_;
2266  
2267 #$self->debug("blast.pm: end_document\n");
2268 return $self->{'_result'};
2269 }
2270  
2271 sub write_result {
2272 my ( $self, $blast, @args ) = @_;
2273  
2274 if ( not defined( $self->writer ) ) {
2275 $self->warn("Writer not defined. Using a $DEFAULT_BLAST_WRITER_CLASS");
2276 $self->writer( $DEFAULT_BLAST_WRITER_CLASS->new() );
2277 }
2278 $self->SUPER::write_result( $blast, @args );
2279 }
2280  
2281 sub result_count {
2282 my $self = shift;
2283 return $self->{'_result_count'};
2284 }
2285  
2286 sub report_count { shift->result_count }
2287  
2288 =head2 inclusion_threshold
2289  
2290 Title : inclusion_threshold
2291 Usage : my $incl_thresh = $isreb->inclusion_threshold;
2292 : $isreb->inclusion_threshold(1e-5);
2293 Function: Get/Set the e-value threshold for inclusion in the PSI-BLAST
2294 score matrix model (blastpgp) that was used for generating the reports
2295 being parsed.
2296 Returns : number (real)
2297 Default value: $FAST::Bio::SearchIO::IteratedSearchResultEventBuilder::DEFAULT_INCLUSION_THRESHOLD
2298 Args : number (real) (e.g., 0.0001 or 1e-4 )
2299  
2300 =cut
2301  
2302 =head2 max_significance
2303  
2304 Usage : $obj->max_significance();
2305 Purpose : Set/Get the P or Expect value used as significance screening cutoff.
2306 This is the value of the -signif parameter supplied to new().
2307 Hits with P or E-value above this are skipped.
2308 Returns : Scientific notation number with this format: 1.0e-05.
2309 Argument : Scientific notation number or float (when setting)
2310 Comments : Screening of significant hits uses the data provided on the
2311 : description line. For NCBI BLAST1 and WU-BLAST, this data
2312 : is P-value. for NCBI BLAST2 it is an Expect value.
2313  
2314 =cut
2315  
2316 =head2 signif
2317  
2318 Synonym for L<max_significance()|max_significance>
2319  
2320 =cut
2321  
2322 =head2 min_score
2323  
2324 Usage : $obj->min_score();
2325 Purpose : Set/Get the Blast score used as screening cutoff.
2326 This is the value of the -score parameter supplied to new().
2327 Hits with scores below this are skipped.
2328 Returns : Integer or scientific notation number.
2329 Argument : Integer or scientific notation number (when setting)
2330 Comments : Screening of significant hits uses the data provided on the
2331 : description line.
2332  
2333 =cut
2334  
2335 =head2 min_query_length
2336  
2337 Usage : $obj->min_query_length();
2338 Purpose : Gets the query sequence length used as screening criteria.
2339 This is the value of the -min_query_len parameter supplied to new().
2340 Hits with sequence length below this are skipped.
2341 Returns : Integer
2342 Argument : n/a
2343  
2344 =cut
2345  
2346 =head2 best_hit_only
2347  
2348 Title : best_hit_only
2349 Usage : print "only getting best hit.\n" if $obj->best_hit_only;
2350 Purpose : Set/Get the indicator for whether or not to process only
2351 : the best BlastHit.
2352 Returns : Boolean (1 | 0)
2353 Argument : Boolean (1 | 0) (when setting)
2354  
2355 =cut
2356  
2357 =head2 check_all_hits
2358  
2359 Title : check_all_hits
2360 Usage : print "checking all hits.\n" if $obj->check_all_hits;
2361 Purpose : Set/Get the indicator for whether or not to process all hits.
2362 : If false, the parser will stop processing hits after the
2363 : the first non-significance hit or the first hit that fails
2364 : any hit filter.
2365 Returns : Boolean (1 | 0)
2366 Argument : Boolean (1 | 0) (when setting)
2367  
2368 =cut
2369  
2370 # general private method used to make minimal hits from leftover
2371 # data in the hit table
2372  
2373 sub _cleanup_hits {
2374 my ($self, $hits) = @_;
2375 while ( my $v = shift @{ $hits }) {
2376 next unless defined $v;
2377 $self->start_element( { 'Name' => 'Hit' } );
2378 my $id = $v->[2];
2379 my $desc = $v->[3];
2380 $self->element(
2381 {
2382 'Name' => 'Hit_id',
2383 'Data' => $id
2384 }
2385 );
2386 my ($gi, $acc, $version ) = $self->_get_seq_identifiers($id);
2387 $self->element(
2388 {
2389 'Name' => 'Hit_accession',
2390 'Data' => $acc
2391 }
2392 );
2393 if ( defined $v ) {
2394 $self->element(
2395 {
2396 'Name' => 'Hit_signif',
2397 'Data' => $v->[0]
2398 }
2399 );
2400 if (exists $self->{'_wublast'}) {
2401 $self->element(
2402 {
2403 'Name' => 'Hit_score',
2404 'Data' => $v->[1]
2405 }
2406 );
2407 } else {
2408 $self->element(
2409 {
2410 'Name' => 'Hit_bits',
2411 'Data' => $v->[1]
2412 }
2413 );
2414 }
2415
2416 }
2417 $self->element(
2418 {
2419 'Name' => 'Hit_def',
2420 'Data' => $desc
2421 }
2422 );
2423 $self->end_element( { 'Name' => 'Hit' } );
2424 }
2425 }
2426  
2427  
2428 1;
2429  
2430 __END__