3 # BioPerl module for Bio::SearchIO::fasta
5 # Cared for by Jason Stajich <jason-at-bioperl.org>
7 # Copyright Jason Stajich
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
15 Bio::SearchIO::fasta - A SearchIO parser for FASTA results
19 # Do not use this object directly, use it through the SearchIO system
21 my $searchio = Bio::SearchIO->new(-format => 'fasta',
22 -file => 'report.FASTA');
23 while( my $result = $searchio->next_result ) {
24 # ... do what you would normally doi with Bio::SearchIO.
29 This object contains the event based parsing code for FASTA format
30 reports. It creates L<Bio::Search::HSP::FastaHSP> objects instead of
31 L<Bio::Search::HSP::GenericHSP> for the HSP objects.
33 This module will parse -m 9 -d 0 output as well as default m 1 output
34 from FASTA as well as SSEARCH.
36 Also see the SearchIO HOWTO:
37 L<http://bioperl.open-bio.org/wiki/HOWTO:SearchIO>.
43 User feedback is an integral part of the evolution of this and other
44 Bioperl modules. Send your comments and suggestions preferably to
45 the Bioperl mailing list. Your participation is much appreciated.
47 bioperl-l@bioperl.org - General discussion
48 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
52 Report bugs to the Bioperl bug tracking system to help us keep track
53 of the bugs and their resolution. Bug reports can be submitted via the
56 http://bugzilla.open-bio.org/
58 =head1 AUTHOR - Jason Stajich, Aaron Mackey
60 Email jason-at-bioperl.org
64 The rest of the documentation details each of the object methods.
65 Internal methods are usually preceded with a _
70 # Let the code begin...
73 package Bio
::SearchIO
::fasta
;
74 use vars
qw(%MODEMAP %MAPPING $IDLENGTH);
77 # Object preamble - inherits from Bio::Root::RootI
79 use Bio::Factory::ObjectFactory;
83 # Set IDLENGTH to a new value if you have
84 # compile FASTA with a different ID length
85 # (actually newest FASTA allows the setting of this
86 # via -C parameter, default is 6)
89 # mapping of NCBI Blast terms to Bioperl hash keys
90 %MODEMAP = ('FastaOutput' => 'result',
95 # This should really be done more intelligently, like with
100 'Hsp_bit-score' => 'HSP-bits',
101 'Hsp_score' => 'HSP-score',
102 'Hsp_sw-score' => 'HSP-swscore',
103 'Hsp_evalue' => 'HSP-evalue',
104 'Hsp_query-from'=> 'HSP-query_start',
105 'Hsp_query-to' => 'HSP-query_end',
106 'Hsp_hit-from' => 'HSP-hit_start',
107 'Hsp_hit-to' => 'HSP-hit_end',
108 'Hsp_positive' => 'HSP-conserved',
109 'Hsp_identity' => 'HSP-identical',
110 'Hsp_gaps' => 'HSP-hsp_gaps',
111 'Hsp_hitgaps' => 'HSP-hit_gaps',
112 'Hsp_querygaps' => 'HSP-query_gaps',
113 'Hsp_qseq' => 'HSP-query_seq',
114 'Hsp_hseq' => 'HSP-hit_seq',
115 'Hsp_midline' => 'HSP-homology_seq',
116 'Hsp_align-len' => 'HSP-hsp_length',
117 'Hsp_query-frame'=> 'HSP-query_frame',
118 'Hsp_hit-frame' => 'HSP-hit_frame',
120 'Hit_id' => 'HIT-name',
121 'Hit_len' => 'HIT-length',
122 'Hit_accession' => 'HIT-accession',
123 'Hit_def' => 'HIT-description',
124 'Hit_signif' => 'HIT-significance',
125 'Hit_score' => 'HIT-score',
127 'FastaOutput_program' => 'RESULT-algorithm_name',
128 'FastaOutput_version' => 'RESULT-algorithm_version',
129 'FastaOutput_query-def'=> 'RESULT-query_name',
130 'FastaOutput_querydesc'=> 'RESULT-query_description',
131 'FastaOutput_query-len'=> 'RESULT-query_length',
132 'FastaOutput_db' => 'RESULT-database_name',
133 'FastaOutput_db-len' => 'RESULT-database_entries',
134 'FastaOutput_db-let' => 'RESULT-database_letters',
136 'Parameters_matrix' => { 'RESULT-parameters' => 'matrix'},
137 'Parameters_expect' => { 'RESULT-parameters' => 'expect'},
138 'Parameters_include' => { 'RESULT-parameters' => 'include'},
139 'Parameters_sc-match' => { 'RESULT-parameters' => 'match'},
140 'Parameters_sc-mismatch' => { 'RESULT-parameters' => 'mismatch'},
141 'Parameters_gap-open' => { 'RESULT-parameters' => 'gapopen'},
142 'Parameters_gap-ext' => { 'RESULT-parameters' => 'gapext'},
143 'Parameters_word-size' => { 'RESULT-parameters' => 'wordsize'},
144 'Parameters_ktup' => { 'RESULT-parameters' => 'ktup'},
145 'Parameters_filter' => {'RESULT-parameters' => 'filter'},
146 'Statistics_db-num' => { 'RESULT-statistics' => 'dbentries'},
147 'Statistics_db-len' => { 'RESULT-statistics' => 'dbletters'},
148 'Statistics_hsp-len' => { 'RESULT-statistics' => 'hsplength'},
149 'Statistics_eff-space' => { 'RESULT-statistics' => 'effectivespace'},
150 'Statistics_kappa' => { 'RESULT-statistics' => 'kappa' },
151 'Statistics_lambda' => { 'RESULT-statistics' => 'lambda' },
152 'Statistics_entropy' => { 'RESULT-statistics' => 'entropy'},
157 use base qw(Bio::SearchIO);
162 Usage : my $obj = Bio::SearchIO::fasta->new();
163 Function: Builds a new Bio::SearchIO::fasta object
164 Returns : Bio::SearchIO::fasta
165 Args : -idlength - set ID length to something other
166 than the default (7), this is only
167 necessary if you have compiled FASTA
168 with a new default id length to display
169 in the HSP alignment blocks
174 my($self,@args) = @_;
175 $self->SUPER::_initialize
(@args);
177 my ($idlength) = $self->_rearrange([qw(IDLENGTH)],@args);
178 $self->idlength($idlength || $IDLENGTH);
179 $self->_eventHandler->register_factory('hsp',
180 Bio
::Factory
::ObjectFactory
->new(
181 -type
=> 'Bio::Search::HSP::FastaHSP',
182 -interface
=> 'Bio::Search::HSP::HSPI'));
189 Usage : my $hit = $searchio->next_result;
190 Function: Returns the next Result from a search
191 Returns : Bio::Search::Result::ResultI object
204 $self->start_document();
206 while( defined ($_ = $self->_readline )) {
207 next if( ! $self->in_element('hsp') &&
208 /^\s+$/); # skip empty lines
209 if( m/(\S+)\s+searches\s+a\s+(protein\s+or\s+DNA\s+)?sequence/oxi ||
210 /(\S+)\s+compares\s+a/ ||
212 ($_ = $self->_readline) &&
213 /(\S+)\s+searches\s+a\s+(protein\s+or\s+DNA\s+)?sequence/oxi ||
214 /(\S+)\s+compares\s+a/
218 $self->_pushback($_);
219 $self->end_element({ 'Name' => 'FastaOutput'});
220 return $self->end_document();
222 $self->{'_reporttype'} = $1;
223 $self->start_element({ 'Name' => 'FastaOutput' } );
224 $self->{'_result_count'}++;
226 $self->debug("reporttype is ".$self->{'_reporttype'}."\n") if $self->verbose > 0;
227 $self->element({ 'Name' => 'FastaOutput_program',
228 'Data' => $self->{'_reporttype'}});
229 $_ = $self->_readline();
230 my ($version) = (/version\s+(\S+)/);
231 $version = '' unless defined $version;
232 $self->{'_version'} = $version;
233 $self->element({ 'Name' => 'FastaOutput_version',
234 'Data' => $version});
236 my ($last, $leadin, $type, $querylen, $querytype, $querydef);
238 while( defined($_ = $self->_readline()) ) {
240 (?
:\s
+>) | # fa33 lead-in
241 (?
:\s
*\d
+\s
*>>>) # fa34 mlib lead-in
246 ($leadin, $querydef) = ($1, $2);
247 if ($leadin =~ m/>>>/) {
248 if($querydef =~ /^(.*?)\s+(?:\-\s+)?(\d+)\s+(aa|nt).*$/o ) {
249 ($querydef, $querylen, $querytype) = ($1, $2, $3);
253 if( $last =~ /(\S+)[:,]\s*(\d+)\s+(aa|nt)/ ) {
254 ($querylen, $querytype) = ($2, $3);
259 } elsif ( m/^\s*vs\s+\S+/o ) {
260 if ( $last =~ /(\S+)[,:]\s+(\d+)\s+(aa|nt)/o) {
261 ($querydef, $querylen, $querytype) = ($1, $2, $3);
268 if( $self->{'_reporttype'} &&
269 $self->{'_reporttype'} eq 'FASTA'
271 if( $querytype eq 'nt') {
272 $self->{'_reporttype'} = 'FASTN' ;
273 } elsif( $querytype eq 'aa' ) {
274 $self->{'_reporttype'} = 'FASTP' ;
277 my ($name, $descr) = $querydef =~ m/^(\S+)\s*(.*?)\s*$/o;
278 $self->element({'Name' => 'FastaOutput_query-def',
280 $self->element({'Name' => 'FastaOutput_querydesc',
283 $self->element({'Name' => 'FastaOutput_query-len',
284 'Data' => $querylen});
286 $self->warn("unable to find and set query length");
288 if( $last =~ /^\s*vs\s+(\S+)/ ||
289 ( $last =~ /^searching\s+(\S+)\s+library/ ) ||
290 ( $last =~ /^Library:\s+(\S+)\s+/ ) ||
291 (defined $_ && ( /^\s*vs\s+(\S+)/ ||
292 /^Library:\s+(\S+)\s+/ )) ||
293 (defined ($_ = $self->_readline()) &&
294 ( /^\s*vs\s+(\S+)/ ||/^Library:\s+(\S+)/ ))
296 $self->element({'Name' => 'FastaOutput_db',
298 } elsif (m/^\s+opt(?:\s+E\(\))?$/o) {
299 # histogram ... read over it more rapidly than the larger outer loop:
300 while (defined($_ = $self->_readline)) {
305 } elsif( /(\d+)\s+residues\s+in\s+(\d+)\s+(?:library\s+)?sequences/ ) {
306 $self->element({'Name' => 'FastaOutput_db-let',
308 $self->element({'Name' => 'FastaOutput_db-len',
310 $self->element({'Name' => 'Statistics_db-len',
312 $self->element({'Name' => 'Statistics_db-num',
314 } elsif( /Lambda=\s*(\S+)/ ) {
315 $self->element({'Name' => 'Statistics_lambda',
317 } elsif (/K=\s*(\S+)/) {
318 $self->element({'Name' => 'Statistics_kappa',
320 } elsif( /^\s*(Smith-Waterman).+(\S+)\s*matrix [^\]]*?(xS)?\]/ ) {
321 $self->element({'Name' => 'Parameters_matrix',
323 $self->element({'Name' => 'Parameters_filter',
324 'Data' => defined $3 ?
1 : 0,
326 $self->{'_reporttype'} = $1;
328 $self->element({ 'Name' => 'FastaOutput_program',
329 'Data' => $self->{'_reporttype'}});
331 } elsif( /The best( related| unrelated)? scores are:/ ) {
335 if ($_ =~ m/^E\((\d+)\)$/o) {
336 $self->element({'Name' => 'Statistics_eff-space', 'Data' => $1});
341 } @labels[$rel ?
5 : 4 .. $#labels];
343 while( defined ($_ = $self->_readline() ) &&
347 if ($line[-1] =~ m/\=/o && $labels[-1] eq 'fs') {
348 # unlabelled alignment hit;
349 push @labels, "aln_code";
353 @data{@labels} = splice(@line, @line - @labels);
354 if ($line[-1] =~ m/\[([1-6rf])\]/o) {
356 $data{lframe
} = ($fr =~ /\d/o ?
357 ($fr <= 3 ?
"+$fr" : "-@{[$fr-3]}") :
358 ($fr eq 'f' ?
'+1' : '-1')
365 if ($line[-1] =~ m/^\(?(\d+)\)$/) {
368 if ($line[-1] =~ m/^\($/) {
375 # rebuild the first part of the line, preserving spaces:
376 ($_) = m/^(\S+(?:\s+\S+){$#line})/;
378 my ($id, $desc) = split(/\s+/,$_,2);
379 my @pieces = split(/\|/,$id);
380 my $acc = pop @pieces;
383 @data{qw(id desc acc)} = ($id, $desc, $acc);
385 push @hit_signifs, \
%data;
387 } elsif( /^\s*([T]?FAST[XYAF]).+,\s*(\S+)\s*matrix[^\]]+?(xS)?\]\s*ktup:\s*(\d+)/ ) {
388 $self->element({'Name' => 'Parameters_matrix',
390 $self->element({'Name' => 'Parameters_filter',
391 'Data' => defined $3 ?
1 : 0,
393 $self->element({'Name' => 'Parameters_ktup',
395 $self->{'_reporttype'} = $1 if( $self->{'_reporttype'} !~ /FAST[PN]/i ) ;
397 $self->element({ 'Name' => 'FastaOutput_program',
398 'Data' => $self->{'_reporttype'}});
400 } elsif( /(?:gap\-pen|open\/ext
):\s
+([\
-\
+]?\d
+)\s
*\
/\s*([\-\+]?\d+).+width:\s+(\d+)/ ) {
401 $self->element({'Name' => 'Parameters_gap-open',
403 $self->element({'Name' => 'Parameters_gap-ext',
405 $self->element({'Name' => 'Parameters_word-size',
407 } elsif( /^>>(.+?)\s+\((\d+)\s*(aa|nt)\)$/ ) {
408 if( $self->in_element('hsp') ) {
409 $self->end_element({ 'Name' => 'Hsp'});
411 if( $self->in_element('hit') ) {
412 $self->end_element({ 'Name' => 'Hit'});
415 $self->start_element({'Name' => 'Hit'});
416 $self->element({ 'Name' => 'Hit_len',
418 my ($id,$desc) = split(/\s+/,$1,2);
419 $self->element({ 'Name' => 'Hit_id',
422 $self->debug("Hit ID is $id\n") if $self->verbose > 0;
423 my @pieces = split(/\|/,$id);
424 my $acc = pop @pieces;
426 $self->element({ 'Name' => 'Hit_accession',
428 $self->element({ 'Name' => 'Hit_def',
431 $_ = $self->_readline();
432 my ($score,$bits,$e) = /Z
-score
: \s
* (\S
+) \s
*
433 (?
: bits
: \s
* (\S
+) \s
+ )?
434 (?
: E
|expect
) \s
* \
(\
) :? \s
*(\S
+)/ox
;
435 $bits = $score unless defined $bits;
437 my $v = shift @hit_signifs;
439 @
{$v}{qw(evalue bits z-sc)} = ($e, $bits, $score);
441 $self->element({'Name' => 'Hit_signif',
442 'Data' => $v ?
$v->{evalue
} : $e });
443 $self->element({'Name' => 'Hit_score',
444 'Data' => $v ?
$v->{bits
} : $bits });
445 $self->start_element({'Name' => 'Hsp'});
447 $self->element({'Name' => 'Hsp_score',
448 'Data' => $v ?
$v->{'z-sc'} : $score });
449 $self->element({'Name' => 'Hsp_evalue',
450 'Data' => $v ?
$v->{evalue
} : $e });
451 $self->element({'Name' => 'Hsp_bit-score',
452 'Data' => $v ?
$v->{bits
} : $bits });
453 $_ = $self->_readline();
454 if( s/Smith-Waterman score:\s*(\d+)\;?// ) {
455 $self->element({'Name' => 'Hsp_sw-score',
458 if( / (\d
*\
.?\d
+)\
% \s
* identity
459 (?
:\s
* \
(\s
*(\S
+)\
% \s
* (?
:ungapped
|similar
) \
) )?
460 \s
* in \s
* (\d
+) \s
+ (?
:aa
|nt
) \s
+ overlap \s
*
461 \
( (\d
+) \
- (\d
+) : (\d
+) \
- (\d
+) \
)
463 my ($identper,$gapper,$len,$querystart,
464 $queryend,$hitstart,$hitend) = ($1,$2,$3,$4,$5,$6,$7);
466 my $ident = POSIX
::ceil
(($identper/100) * $len);
467 my $gaps = POSIX
::ceil
( ($gapper/100) * $len);
468 $self->element({'Name' => 'Hsp_gaps',
470 my $positive = POSIX
::ceil
(($gapper/100) * $len);
472 $self->element({'Name' => 'Hsp_identity',
474 $self->element({'Name' => 'Hsp_positive',
475 'Data' => $positive});
476 $self->element({'Name' => 'Hsp_align-len',
479 $self->debug( "query_start = $querystart, query_end = $queryend\n");
480 $self->element({'Name' => 'Hsp_query-from',
481 'Data' => $querystart});
482 $self->element({'Name' => 'Hsp_query-to',
483 'Data' => $queryend});
484 $self->element({'Name' => 'Hsp_hit-from',
485 'Data' => $hitstart});
486 $self->element({'Name' => 'Hsp_hit-to',
492 $self->element({'Name' => 'Hsp_querygaps', 'Data' => $v->{qgaps
} }) if exists $v->{qgaps
};
493 $self->element({'Name' => 'Hsp_hitgaps', 'Data' => $v->{lgaps
} }) if exists $v->{lgaps
};
495 if ($self->{'_reporttype'} =~ m/^FAST[NXY]$/o) {
496 if( 8 == scalar grep { exists $v->{$_} } qw(an0 ax0 pn0 px0 an1 ax1 pn1 px1) ) {
497 if ($v->{ax0
} < $v->{an0
}) {
498 $self->element({'Name' => 'Hsp_query-frame', 'Data' => "-@{[(($v->{px0} - $v->{ax0}) % 3) + 1]}" });
500 $self->element({'Name' => 'Hsp_query-frame', 'Data' => "+@{[(($v->{an0} - $v->{pn0}) % 3) + 1]}" });
502 if ($v->{ax1
} < $v->{an1
}) {
503 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => "-@{[(($v->{px1} - $v->{ax1}) % 3) + 1]}" });
505 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => "+@{[(($v->{an1} - $v->{pn1}) % 3) + 1]}" });
508 $self->element({'Name' => 'Hsp_query-frame', 'Data' => $v->{lframe
} });
509 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => 0 });
512 $self->element({'Name' => 'Hsp_query-frame', 'Data' => 0 });
513 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => $v->{lframe
} });
517 $self->warn( "unable to parse FASTA score line: $_");
519 } elsif( /\d+\s*residues\s*in\s*\d+\s*query\s*sequences/ ) {
520 if( $self->in_element('hsp') ) {
521 $self->end_element({'Name' => 'Hsp'});
523 if( $self->in_element('hit') ) {
524 $self->end_element({'Name' => 'Hit'});
527 # $_ = $self->_readline();
528 # my ( $liblen,$libsize) = /(\d+)\s+residues\s*in(\d+)\s*library/;
529 # fast forward to the end of the file as there is
530 # nothing else left to do with this file and want to be sure and
532 while(defined($_ = $self->_readline() ) ) {
533 last if( /^Function used was/);
534 if( /(\S
+)\s
+searches\s
+a\s
+(protein\s
+or\s
+DNA\s
+)?
536 /(\S+)\s+compares\s+a/oi ) {
537 $self->_pushback($_);
542 # process remaining best hits
543 for my $h (@hit_signifs) {
544 # Hsp_score Hsp_evalue Hsp_bit-score
545 # Hsp_sw-score Hsp_gaps Hsp_identity Hsp_positive
546 # Hsp_align-len Hsp_query-from Hsp_query-to
547 # Hsp_hit-from Hsp_hit-to Hsp_qseq Hsp_midline
549 $self->start_element({'Name' => 'Hit'});
550 $self->element({ 'Name' => 'Hit_len',
551 'Data' => $h->{hit_len
}
552 }) if exists $h->{hit_len
};
553 $self->element({ 'Name' => 'Hit_id',
555 }) if exists $h->{id
};
556 $self->element({ 'Name' => 'Hit_accession',
558 }) if exists $h->{acc
};
559 $self->element({ 'Name' => 'Hit_def',
561 }) if exists $h->{desc
};
562 $self->element({'Name' => 'Hit_signif',
563 'Data' => $h->{evalue
}
564 }) if exists $h->{evalue
};
565 $self->element({'Name' => 'Hit_score',
567 }) if exists $h->{bits
};
569 $self->start_element({'Name' => 'Hsp'});
570 $self->element({'Name' => 'Hsp_score', 'Data' => $h->{'z-sc'} }) if exists $h->{'z-sc'};
571 $self->element({'Name' => 'Hsp_evalue', 'Data' => $h->{evalue
} }) if exists $h->{evalue
};
572 $self->element({'Name' => 'Hsp_bit-score', 'Data' => $h->{bits
} }) if exists $h->{bits
};
573 $self->element({'Name' => 'Hsp_sw-score', 'Data' => $h->{sw
} }) if exists $h->{sw
};
574 $self->element({'Name' => 'Hsp_gaps', 'Data' => $h->{'%_gid'} }) if exists $h->{'%_gid'};
575 $self->element({'Name' => 'Hsp_identity', 'Data' => POSIX
::ceil
($h->{'%_id'} * $h->{alen
}) })
576 if (exists $h->{'%_id'} && exists $h->{alen
});
577 if( exists $h->{'%_gid'} ) {
578 $self->element({'Name' => 'Hsp_positive', 'Data' => POSIX
::ceil
($h->{'%_gid'} * $h->{alen
})}) if exists $h->{'%_gid'} && exists $h->{alen
};
580 $self->element({'Name' => 'Hsp_positive', 'Data' => POSIX
::ceil
($h->{'%_id'} * $h->{alen
}) })
581 if (exists $h->{'%_id'} && exists $h->{alen
});
583 $self->element({'Name' => 'Hsp_align-len', 'Data' => $h->{alen
} }) if exists $h->{alen
};
584 $self->element({'Name' => 'Hsp_query-from', 'Data' => $h->{an0
} }) if exists $h->{an0
};
585 $self->element({'Name' => 'Hsp_query-to', 'Data' => $h->{ax0
} }) if exists $h->{ax0
};
586 $self->element({'Name' => 'Hsp_hit-from', 'Data' => $h->{an1
} }) if exists $h->{an1
};
587 $self->element({'Name' => 'Hsp_hit-to', 'Data' => $h->{ax1
} }) if exists $h->{ax1
};
589 $self->element({'Name' => 'Hsp_querygaps', 'Data' => $h->{qgaps
} }) if exists $h->{qgaps
};
590 $self->element({'Name' => 'Hsp_hitgaps', 'Data' => $h->{lgaps
} }) if exists $h->{lgaps
};
592 if ($self->{'_reporttype'} =~ m/^FAST[NXY]$/o) {
593 if( 8 == scalar grep { exists $h->{$_} } qw(an0 ax0 pn0 px0 an1 ax1 pn1 px1) ) {
594 if ($h->{ax0
} < $h->{an0
}) {
595 $self->element({'Name' => 'Hsp_query-frame', 'Data' => "-@{[(($h->{px0} - $h->{ax0}) % 3) + 1]}" });
597 $self->element({'Name' => 'Hsp_query-frame', 'Data' => "+@{[(($h->{an0} - $h->{pn0}) % 3) + 1]}" });
599 if ($h->{ax1
} < $h->{an1
}) {
600 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => "-@{[(($h->{px1} - $h->{ax1}) % 3) + 1]}" });
602 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => "+@{[(($h->{an1} - $h->{pn1}) % 3) + 1]}" });
605 $self->element({'Name' => 'Hsp_query-frame', 'Data' => $h->{lframe
} });
606 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => 0 });
609 $self->element({'Name' => 'Hsp_query-frame', 'Data' => 0 });
610 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => $h->{lframe
} });
613 $self->end_element({'Name' => 'Hsp'});
614 $self->end_element({'Name' => 'Hit'});
617 $self->end_element({ 'Name' => 'FastaOutput'});
618 return $self->end_document();
619 } elsif( /^\s*\d+\s*>>>/) {
620 if ($self->within_element('FastaOutput')) {
621 if( $self->in_element('hsp') ) {
622 $self->end_element({'Name' => 'Hsp'});
624 if( $self->in_element('hit') ) {
625 $self->end_element({'Name' => 'Hit'});
629 # process remaining best hits
630 for my $h (@hit_signifs) {
631 $self->start_element({'Name' => 'Hit'});
632 $self->element({ 'Name' => 'Hit_len',
633 'Data' => $h->{hit_len
}
634 }) if exists $h->{hit_len
};
635 $self->element({ 'Name' => 'Hit_id',
637 }) if exists $h->{id
};
638 $self->element({ 'Name' => 'Hit_accession',
640 }) if exists $h->{acc
};
641 $self->element({ 'Name' => 'Hit_def',
643 }) if exists $h->{desc
};
644 $self->element({'Name' => 'Hit_signif',
645 'Data' => $h->{evalue
}
646 }) if exists $h->{evalue
};
647 $self->element({'Name' => 'Hit_score',
649 }) if exists $h->{bits
};
651 $self->start_element({'Name' => 'Hsp'});
652 $self->element({'Name' => 'Hsp_score', 'Data' => $h->{'z-sc'} }) if exists $h->{'z-sc'};
653 $self->element({'Name' => 'Hsp_evalue', 'Data' => $h->{evalue
} }) if exists $h->{evalue
};
654 $self->element({'Name' => 'Hsp_bit-score', 'Data' => $h->{bits
} }) if exists $h->{bits
};
655 $self->element({'Name' => 'Hsp_sw-score', 'Data' => $h->{sw
} }) if exists $h->{sw
};
656 $self->element({'Name' => 'Hsp_gaps', 'Data' => $h->{'%_gid'} }) if exists $h->{'%_gid'};
657 $self->element({'Name' => 'Hsp_identity', 'Data' => POSIX
::ceil
($h->{'%_id'} * $h->{alen
}) })
658 if (exists $h->{'%_id'} && exists $h->{alen
});
659 if( exists $h->{'%_gid'} ) {
660 $self->element({'Name' => 'Hsp_positive', 'Data' => POSIX
::ceil
($h->{'%_gid'} * $h->{alen
})}) if exists $h->{'%_gid'} && exists $h->{alen
};
662 $self->element({'Name' => 'Hsp_positive', 'Data' => POSIX
::ceil
($h->{'%_id'} * $h->{alen
}) })
663 if (exists $h->{'%_id'} && exists $h->{alen
});
665 $self->element({'Name' => 'Hsp_align-len', 'Data' => $h->{alen
} }) if exists $h->{alen
};
666 $self->element({'Name' => 'Hsp_query-from', 'Data' => $h->{an0
} }) if exists $h->{an0
};
667 $self->element({'Name' => 'Hsp_query-to', 'Data' => $h->{ax0
} }) if exists $h->{ax0
};
668 $self->element({'Name' => 'Hsp_hit-from', 'Data' => $h->{an1
} }) if exists $h->{an1
};
669 $self->element({'Name' => 'Hsp_hit-to', 'Data' => $h->{ax1
} }) if exists $h->{ax1
};
671 $self->element({'Name' => 'Hsp_querygaps', 'Data' => $h->{qgaps
} }) if exists $h->{qgaps
};
672 $self->element({'Name' => 'Hsp_hitgaps', 'Data' => $h->{lgaps
} }) if exists $h->{lgaps
};
674 if ($self->{'_reporttype'} =~ m/^FAST[NXY]$/o) {
675 if( 8 == scalar grep { exists $h->{$_} } qw(an0 ax0 pn0 px0 an1 ax1 pn1 px1) ) {
676 if ($h->{ax0
} < $h->{an0
}) {
677 $self->element({'Name' => 'Hsp_query-frame', 'Data' => "-@{[(($h->{px0} - $h->{ax0}) % 3) + 1]}" });
679 $self->element({'Name' => 'Hsp_query-frame', 'Data' => "+@{[(($h->{an0} - $h->{pn0}) % 3) + 1]}" });
681 if ($h->{ax1
} < $h->{an1
}) {
682 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => "-@{[(($h->{px1} - $h->{ax1}) % 3) + 1]}" });
684 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => "+@{[(($h->{an1} - $h->{pn1}) % 3) + 1]}" });
687 $self->element({'Name' => 'Hsp_query-frame', 'Data' => $h->{lframe
} });
688 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => 0 });
691 $self->element({'Name' => 'Hsp_query-frame', 'Data' => 0 });
692 $self->element({'Name' => 'Hsp_hit-frame', 'Data' => $h->{lframe
} });
695 $self->end_element({'Name' => 'Hsp'});
696 $self->end_element({'Name' => 'Hit'});
699 $self->end_element({ 'Name' => 'FastaOutput' });
700 $self->_pushback($_);
701 return $self->end_document();
703 $self->start_element({ 'Name' => 'FastaOutput' });
704 $self->{'_result_count'}++;
706 $self->element({ 'Name' => 'FastaOutput_program',
707 'Data' => $self->{'_reporttype'} });
708 $self->element({ 'Name' => 'FastaOutput_version',
709 'Data' => $self->{'_version'} });
711 my ($type, $querylen, $querytype, $querydef);
713 if( /^\s*\d+\s*>>>(.*)/ ) {
715 if($querydef =~ /^(.*?)\s+(?:\-\s+)?(\d+)\s+(aa|nt).*$/o ) {
716 ($querydef, $querylen, $querytype) = ($1, $2, $3);
720 if( $self->{'_reporttype'} && $self->{'_reporttype'} eq 'FASTA' ) {
721 if( $querytype eq 'nt') {
722 $self->{'_reporttype'} = 'FASTN' ;
723 } elsif( $querytype eq 'aa' ) {
724 $self->{'_reporttype'} = 'FASTP' ;
727 my ($name,$descr) = ($querydef =~ m/^(\S+)(?:\s+(.*))?\s*$/o);
728 $self->element({'Name' => 'FastaOutput_query-def',
730 $self->element({'Name' => 'FastaOutput_querydesc',
733 $self->element({'Name' => 'FastaOutput_query-len',
734 'Data' => $querylen});
736 $self->warn("unable to find and set query length");
739 if( defined ($_ = $self->_readline()) && ( /^\s*vs\s+(\S+)/ ||/^Library:\s+(\S+)/ )) {
740 $self->element({'Name' => 'FastaOutput_db',
745 } elsif( $self->in_element('hsp' ) ) {
747 my @data = ( [],[],[]);
749 my $len = $self->idlength + 1;
751 while( defined($_ ) ) {
753 $self->debug( "$count $_\n");
755 if( /residues in \d+\s+query\s+sequences/o) {
756 $self->_pushback($_);
758 } elsif (/^>>>\*\*\*/o) {
759 $self->end_element({Name
=> "Hsp"});
762 $self->_pushback($_);
764 } elsif (/^\s*\d+\s*>>>/o) {
765 $self->_pushback($_);
770 $self->_pushback($_);
772 } elsif( /^\s+\d+/ || /^\s+$/ ) {
773 # do nothing, this is really a 0 line
774 } elsif( length($_) == 0 ) {
777 $self->_pushback($_);
780 } elsif( $count == 1 || $count == 3 ) {
782 $len = CORE
::length($1) if $len < CORE
::length($1);
783 s/\s+$//; # trim trailing spaces,we don't want them
784 push @
{$data[$count-1]},substr($_,$len);
785 } elsif( /^\s+(\d+)/ ) {
787 $self->_pushback($_);
788 } elsif( /^\s+$/ || length($_) == 0) {
790 # going to skip these
792 $self->warn("Unrecognized alignment line ($count) '$_'");
794 } elsif( $count == 2 ) {
796 $self->warn("$_\n") if $self->verbose > 0;
797 # we are on a Subject part of the alignment
798 # but we THOUGHT we were on the Query
799 # move that last line to the proper place
800 push @
{$data[2]}, pop @
{$data[0]};
803 # toss the first IDLENGTH characters of the line
804 if( length($_) >= $len ) {
805 push @
{$data[$count-1]}, substr($_,$len);
809 last if( $count++ >= 5);
810 $_ = $self->_readline();
812 if( @
{$data[0]} || @
{$data[2]}) {
813 $self->characters({'Name' => 'Hsp_qseq',
814 'Data' => join('',@
{$data[0]}) });
815 $self->characters({'Name' => 'Hsp_midline',
816 'Data' => join('',@
{$data[1]}) });
817 $self->characters({'Name' => 'Hsp_hseq',
818 'Data' => join('',@
{$data[2]}) });
823 $self->warn("unrecognized FASTA Family report file!");
828 if( $self->in_element('result') ) {
829 if( $self->in_element('hsp') ) {
830 $self->end_element({'Name' => 'Hsp'});
832 if( $self->in_element('hit') ) {
833 $self->end_element({'Name' => 'Hit'});
835 $self->end_element({ 'Name' => 'FastaOutput' });
837 return $self->end_document();
843 Title : start_element
844 Usage : $eventgenerator->start_element
845 Function: Handles a start element event
847 Args : hashref with at least 2 keys 'Data' and 'Name'
853 my ($self,$data) = @_;
854 # we currently don't care about attributes
855 my $nm = $data->{'Name'};
856 if( my $type = $MODEMAP{$nm} ) {
858 if(my $handler = $self->_will_handle($type) ) {
859 my $func = sprintf("start_%s",lc $type);
860 $handler->$func($data->{'Attributes'});
862 unshift @
{$self->{'_elements'}}, $type;
864 if($nm eq 'FastaOutput') {
865 $self->{'_values'} = {};
866 $self->{'_result'}= undef;
867 $self->{'_mode'} = '';
874 Title : start_element
875 Usage : $eventgenerator->end_element
876 Function: Handles an end element event
878 Args : hashref with at least 2 keys 'Data' and 'Name'
884 my ($self,$data) = @_;
885 my $nm = $data->{'Name'};
887 # Hsp are sort of weird, in that they end when another
888 # object begins so have to detect this in end_element for now
890 foreach ( qw(Hsp_qseq Hsp_midline Hsp_hseq) ) {
891 $self->element({'Name' => $_,
892 'Data' => $self->{'_last_hspdata'}->{$_}});
894 $self->{'_last_hspdata'} = {}
897 if( my $type = $MODEMAP{$nm} ) {
898 if( my $handler = $self->_will_handle($type) ) {
899 my $func = sprintf("end_%s",lc $type);
900 $rc = $handler->$func($self->{'_reporttype'},
903 shift @
{$self->{'_elements'}};
905 } elsif( $MAPPING{$nm} ) {
906 if ( ref($MAPPING{$nm}) =~ /hash/i ) {
907 my $key = (keys %{$MAPPING{$nm}})[0];
908 $self->{'_values'}->{$key}->{$MAPPING{$nm}->{$key}} = $self->{'_last_data'};
910 $self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
913 $self->warn( "unknown nm $nm, ignoring\n");
915 $self->{'_last_data'} = ''; # remove read data if we are at
917 $self->{'_result'} = $rc if( $nm eq 'FastaOutput' );
925 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
926 Function: Convience method that calls start_element, characters, end_element
928 Args : Hash ref with the keys 'Name' and 'Data'
934 my ($self,$data) = @_;
935 $self->start_element($data);
936 $self->characters($data);
937 $self->end_element($data);
944 Usage : $eventgenerator->characters($str)
945 Function: Send a character events
953 my ($self,$data) = @_;
955 return unless ( defined $data->{'Data'} );
956 if( $data->{'Data'} =~ /^\s+$/ ) {
957 return unless $data->{'Name'} =~ /Hsp\_(midline|qseq|hseq)/;
960 if( $self->in_element('hsp') &&
961 $data->{'Name'} =~ /Hsp\_(qseq|hseq|midline)/ ) {
963 $self->{'_last_hspdata'}->{$data->{'Name'}} .= $data->{'Data'};
966 $self->{'_last_data'} = $data->{'Data'};
972 Usage : $obj->_mode($newval)
975 Returns : value of _mode
976 Args : newvalue (optional)
982 my ($self,$value) = @_;
983 if( defined $value) {
984 $self->{'_mode'} = $value;
986 return $self->{'_mode'};
989 =head2 within_element
991 Title : within_element
992 Usage : if( $eventgenerator->within_element($element) ) {}
993 Function: Test if we are within a particular element
994 This is different than 'in' because within can be tested
997 Args : string element name
1003 my ($self,$name) = @_;
1004 return 0 if ( ! defined $name &&
1005 ! defined $self->{'_elements'} ||
1006 scalar @
{$self->{'_elements'}} == 0) ;
1007 foreach ( @
{$self->{'_elements'}} ) {
1008 if( $_ eq $name || $_ eq $MODEMAP{$name} ) {
1018 Usage : if( $eventgenerator->in_element($element) ) {}
1019 Function: Test if we are in a particular element
1020 This is different than 'in' because within can be tested
1023 Args : string element name
1029 my ($self,$name) = @_;
1030 return 0 if ! defined $self->{'_elements'}->[0];
1031 return ( $self->{'_elements'}->[0] eq $name ||
1032 (exists $MODEMAP{$name} && $self->{'_elements'}->[0] eq $MODEMAP{$name})
1037 =head2 start_document
1039 Title : start_document
1040 Usage : $eventgenerator->start_document
1041 Function: Handles a start document event
1050 $self->{'_lasttype'} = '';
1051 $self->{'_values'} = {};
1052 $self->{'_result'}= undef;
1053 $self->{'_mode'} = '';
1054 $self->{'_elements'} = [];
1060 Title : end_document
1061 Usage : $eventgenerator->end_document
1062 Function: Handles an end document event
1063 Returns : Bio::Search::Result::ResultI object
1070 my ($self,@args) = @_;
1071 return $self->{'_result'};
1077 Usage : $obj->idlength($newval)
1078 Function: Internal storage of the length of the ID desc
1079 in the HSP alignment blocks. Defaults to
1080 $IDLENGTH class variable value
1081 Returns : value of idlength
1082 Args : newvalue (optional)
1088 my ($self,$value) = @_;
1089 if( defined $value) {
1090 $self->{'_idlength'} = $value;
1092 return $self->{'_idlength'} || $IDLENGTH;
1098 Title : result_count
1099 Usage : my $count = $searchio->result_count
1100 Function: Returns the number of results we have processed
1109 return $self->{'_result_count'};
1112 sub attach_EventHandler
{
1113 my ($self,$handler) = @_;
1115 $self->SUPER::attach_EventHandler
($handler);
1117 # Optimization: caching the EventHandler since it is used a lot
1120 $self->{'_handler_cache'} = $handler;
1126 Title : _will_handle
1127 Usage : Private method. For internal use only.
1128 if( $self->_will_handle($type) ) { ... }
1129 Function: Provides an optimized way to check whether or not an element of a
1130 given type is to be handled.
1131 Returns : Reference to EventHandler object if the element type is to be handled.
1132 undef if the element type is not to be handled.
1133 Args : string containing type of element.
1141 Using the cached pointer to the EventHandler to minimize repeated
1146 Caching the will_handle status for each type that is encountered so
1147 that it only need be checked by calling
1148 handler-E<gt>will_handle($type) once.
1152 This does not lead to a major savings by itself (only 5-10%). In
1153 combination with other optimizations, or for large parse jobs, the
1154 savings good be significant.
1156 To test against the unoptimized version, remove the parentheses from
1157 around the third term in the ternary " ? : " operator and add two
1158 calls to $self-E<gt>_eventHandler().
1163 my ( $self, $type ) = @_;
1164 my $handler = $self->{'_handler_cache'};
1166 defined( $self->{'_will_handle_cache'}->{$type} )
1167 ?
$self->{'_will_handle_cache'}->{$type}
1168 : ( $self->{'_will_handle_cache'}->{$type} =
1169 $handler->will_handle($type) );
1171 return $will_handle ?
$handler : undef;