2 # BioPerl module for Bio::SearchIO::fasta
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Jason Stajich <jason-at-bioperl.org>
8 # Copyright Jason Stajich
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
16 Bio::SearchIO::fasta - A SearchIO parser for FASTA results
20 # Do not use this object directly, use it through the SearchIO system
22 my $searchio = Bio::SearchIO->new(-format => 'fasta',
23 -file => 'report.FASTA');
24 while( my $result = $searchio->next_result ) {
25 # ... do what you would normally doi with Bio::SearchIO.
30 This object contains the event based parsing code for FASTA format
31 reports. It creates L<Bio::Search::HSP::FastaHSP> objects instead of
32 L<Bio::Search::HSP::GenericHSP> for the HSP objects.
34 This module will parse -m 9 -d 0 output as well as default m 1 output
35 from FASTA as well as SSEARCH.
37 Also see the SearchIO HOWTO:
38 L<http://bioperl.org/howtos/SearchIO_HOWTO.html>.
44 User feedback is an integral part of the evolution of this and other
45 Bioperl modules. Send your comments and suggestions preferably to
46 the Bioperl mailing list. Your participation is much appreciated.
48 bioperl-l@bioperl.org - General discussion
49 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
53 Please direct usage questions or support issues to the mailing list:
55 I<bioperl-l@bioperl.org>
57 rather than to the module maintainer directly. Many experienced and
58 reponsive experts will be able look at the problem and quickly
59 address it. Please include a thorough description of the problem
60 with code and data examples if at all possible.
64 Report bugs to the Bioperl bug tracking system to help us keep track
65 of the bugs and their resolution. Bug reports can be submitted via the
68 https://github.com/bioperl/bioperl-live/issues
70 =head1 AUTHOR - Jason Stajich, Aaron Mackey, William Pearson
72 Email jason-at-bioperl.org
76 The rest of the documentation details each of the object methods.
77 Internal methods are usually preceded with a _
81 # Let the code begin...
83 package Bio
::SearchIO
::fasta
;
84 use vars
qw(%MODEMAP %MAPPING $IDLENGTH);
87 # Object preamble - inherits from Bio::Root::RootI
89 use Bio::Factory::ObjectFactory;
93 # Set IDLENGTH to a new value if you have
94 # compile FASTA with a different ID length
95 # (actually newest FASTA allows the setting of this
96 # via -C parameter, default is 6)
99 # mapping of NCBI Blast terms to Bioperl hash keys
101 'FastaOutput' => 'result',
106 # This should really be done more intelligently, like with
110 'Hsp_bit-score' => 'HSP-bits',
111 'Hsp_score' => 'HSP-score',
112 'Hsp_sw-score' => 'HSP-swscore',
113 'Hsp_evalue' => 'HSP-evalue',
114 'Hsp_evalue2' => 'HSP-evalue2',
115 'Hsp_query-from' => 'HSP-query_start',
116 'Hsp_query-to' => 'HSP-query_end',
117 'Hsp_hit-from' => 'HSP-hit_start',
118 'Hsp_hit-to' => 'HSP-hit_end',
119 'Hsp_positive' => 'HSP-conserved',
120 'Hsp_identity' => 'HSP-identical',
121 'Hsp_gaps' => 'HSP-hsp_gaps',
122 'Hsp_hitgaps' => 'HSP-hit_gaps',
123 'Hsp_querygaps' => 'HSP-query_gaps',
124 'Hsp_qseq' => 'HSP-query_seq',
125 'Hsp_hseq' => 'HSP-hit_seq',
126 'Hsp_midline' => 'HSP-homology_seq',
127 'Hsp_align-len' => 'HSP-hsp_length',
128 'Hsp_query-frame' => 'HSP-query_frame',
129 'Hsp_hit-frame' => 'HSP-hit_frame',
131 'Hit_id' => 'HIT-name',
132 'Hit_len' => 'HIT-length',
133 'Hit_accession' => 'HIT-accession',
134 'Hit_def' => 'HIT-description',
135 'Hit_signif' => 'HIT-significance',
136 'Hit_score' => 'HIT-score',
138 'FastaOutput_program' => 'RESULT-algorithm_name',
139 'FastaOutput_version' => 'RESULT-algorithm_version',
140 'FastaOutput_query-def' => 'RESULT-query_name',
141 'FastaOutput_querydesc' => 'RESULT-query_description',
142 'FastaOutput_query-len' => 'RESULT-query_length',
143 'FastaOutput_db' => 'RESULT-database_name',
144 'FastaOutput_db-len' => 'RESULT-database_entries',
145 'FastaOutput_db-let' => 'RESULT-database_letters',
147 'Parameters_matrix' => { 'RESULT-parameters' => 'matrix' },
148 'Parameters_expect' => { 'RESULT-parameters' => 'expect' },
149 'Parameters_include' => { 'RESULT-parameters' => 'include' },
150 'Parameters_sc-match' => { 'RESULT-parameters' => 'match' },
151 'Parameters_sc-mismatch' => { 'RESULT-parameters' => 'mismatch' },
152 'Parameters_gap-open' => { 'RESULT-parameters' => 'gapopen' },
153 'Parameters_gap-ext' => { 'RESULT-parameters' => 'gapext' },
154 'Parameters_word-size' => { 'RESULT-parameters' => 'wordsize' },
155 'Parameters_ktup' => { 'RESULT-parameters' => 'ktup' },
156 'Parameters_filter' => { 'RESULT-parameters' => 'filter' },
157 'Statistics_db-num' => { 'RESULT-statistics' => 'dbentries' },
158 'Statistics_db-len' => { 'RESULT-statistics' => 'dbletters' },
159 'Statistics_hsp-len' => { 'RESULT-statistics' => 'hsplength' },
160 'Statistics_eff-space' => { 'RESULT-statistics' => 'effectivespace' },
161 'Statistics_kappa' => { 'RESULT-statistics' => 'kappa' },
162 'Statistics_lambda' => { 'RESULT-statistics' => 'lambda' },
163 'Statistics_entropy' => { 'RESULT-statistics' => 'entropy' },
167 use base qw(Bio::SearchIO);
172 Usage : my $obj = Bio::SearchIO::fasta->new();
173 Function: Builds a new Bio::SearchIO::fasta object
174 Returns : Bio::SearchIO::fasta
175 Args : -idlength - set ID length to something other
176 than the default (6), this is only
177 necessary if you have compiled FASTA
178 with a new default id length to display
179 in the HSP alignment blocks
184 my ( $self, @args ) = @_;
185 $self->SUPER::_initialize
(@args);
187 my ($idlength) = $self->_rearrange( [qw(IDLENGTH)], @args );
188 $self->idlength( $idlength || $IDLENGTH );
189 $self->_eventHandler->register_factory(
191 Bio
::Factory
::ObjectFactory
->new(
192 -type
=> 'Bio::Search::HSP::FastaHSP',
193 -interface
=> 'Bio::Search::HSP::HSPI'
202 Usage : my $hit = $searchio->next_result;
203 Function: Returns the next Result from a search
204 Returns : Bio::Search::Result::ResultI object
218 $self->start_document();
220 while ( defined( $_ = $self->_readline ) ) {
221 next if ( !$self->in_element('hsp')
222 && /^\s+$/ ); # skip empty lines
224 m/(\S+)\s+searches\s+a\s+(protein\s+or\s+DNA\s+)?sequence/oxi
225 || /(\S+)\s+compares\s+a/
226 || /(\S+)\s+performs\s+a/
227 || /(\S+)\s+produces\s/
228 || /(\S+)\s+finds\s+/ # for lalign, but does not work because no "The best scores are:"
229 || ( m/^\#\s+/ # has a command log line
230 && ( $_ = $self->_readline )
231 && /(\S+)\s+searches\s+a\s+(protein\s+or\s+DNA\s+)?sequence/oxi
232 || /(\S+)\s+compares\s+a/
233 || /(\S+)\s+performs\s+a/
234 || /(\S+)\s+produces\s/
235 || /(\S+)\s+finds\s+/ # for lalign, but does not work because no "The best scores are:"
240 $self->_pushback($_);
241 $self->end_element( { 'Name' => 'FastaOutput' } );
242 return $self->end_document();
244 $self->{'_reporttype'} = $1;
245 $self->start_element( { 'Name' => 'FastaOutput' } );
246 $self->{'_result_count'}++;
248 #$self->debug( "reporttype is " . $self->{'_reporttype'} . "\n" );
251 'Name' => 'FastaOutput_program',
252 'Data' => $self->{'_reporttype'}
256 # version 35 version string on same line
258 ($version) = (/version\s+(\S+)/);
260 # earlier versions, it's on the next line
262 $_ = $self->_readline();
263 ($version) = (/version\s+(\S+)/);
265 $version = '' unless defined $version;
266 $self->{'_version'} = $version;
269 'Name' => 'FastaOutput_version',
274 my ( $last, $leadin, $type, $querylen, $querytype, $querydef );
276 while ( defined( $_ = $self->_readline() ) ) {
279 (?
:\s
+>) | # fa33 lead-in
280 (?
:\s
*\d
+\s
*>>>) # fa34 mlib lead-in
286 ( $leadin, $querydef ) = ( $1, $2 );
287 if ( $leadin =~ m/>>>/ ) {
289 /^(.*?)\s+(?:\-\s+)?(\d+)\s+(aa|nt).*$/o )
291 ( $querydef, $querylen, $querytype ) =
297 if ( $last =~ /(\S+)[:,]\s*(\d+)\s+(aa|nt)/ ) {
298 ( $querylen, $querytype ) = ( $2, $3 );
304 elsif (m/^\s*vs\s+\S+/o) {
305 if ( $last =~ /(\S+)[,:]\s+(\d+)\s+(aa|nt)/o ) {
306 ( $querydef, $querylen, $querytype ) = ( $1, $2, $3 );
312 if ( $self->{'_reporttype'}
313 && $self->{'_reporttype'} eq 'FASTA' )
315 if ( $querytype eq 'nt' ) {
316 $self->{'_reporttype'} = 'FASTN';
318 elsif ( $querytype eq 'aa' ) {
319 $self->{'_reporttype'} = 'FASTP';
322 my ( $name, $descr ) = $querydef =~ m/^(\S+)\s*(.*?)\s*$/o;
325 'Name' => 'FastaOutput_query-def',
331 'Name' => 'FastaOutput_querydesc',
338 'Name' => 'FastaOutput_query-len',
344 $self->warn("unable to find and set query length");
347 $last =~ /^\s*vs\s+(\S+)/
348 || ( $last =~ /^searching\s+(\S+)\s+library/ )
349 || ( $last =~ /^Library:\s+(\S+)\s+/ )
352 && ( /^\s*vs\s+(\S+)/
353 || /^Library:\s+(\S+)\s+/ )
355 || ( defined( $_ = $self->_readline() )
356 && ( /^\s*vs\s+(\S+)/ || /^Library:\s+(\S+)/ ) )
361 'Name' => 'FastaOutput_db',
366 elsif (m/^\s+opt(?:\s+E\(\))?$/o) {
368 # histogram ... read over it more rapidly than the larger outer loop:
369 while ( defined( $_ = $self->_readline ) ) {
374 elsif (/(\d+)\s+residues\s+in\s+(\d+)\s+(?:library\s+)?sequences/) {
377 'Name' => 'FastaOutput_db-let',
383 'Name' => 'FastaOutput_db-len',
389 'Name' => 'Statistics_db-len',
395 'Name' => 'Statistics_db-num',
400 elsif (/Lambda=\s*(\S+)/) {
403 'Name' => 'Statistics_lambda',
408 elsif (/K=\s*(\S+)/) {
411 'Name' => 'Statistics_kappa',
416 elsif (/^\s*(Smith-Waterman)/) {
418 $self->{'_reporttype'} = $1;
420 m/\[\s*(\S+)\s+matrix \([^\)]+\)(xS)?\],/;
424 'Name' => 'Parameters_matrix',
430 'Name' => 'Parameters_filter',
431 'Data' => defined $2 ?
1 : 0,
434 if (/\s*gap\-penalty:\s*(\-?\d+)\/(\
-?\d
+)/) {
437 'Name' => 'Parameters_gap-open',
444 'Name' => 'Parameters_gap-ext',
449 elsif (/\s*open\/ext
:\s
*(\
-?\d
+)\
/(\-?\d+)/) {
452 'Name' => 'Parameters_gap-open',
459 'Name' => 'Parameters_gap-ext',
467 'Name' => 'FastaOutput_program',
468 'Data' => $self->{'_reporttype'}
472 elsif (/The best( related| unrelated)? scores are:/) {
476 if ( $_ =~ m/^E\((\d+)\)$/o )
479 { 'Name' => 'Statistics_eff-space', 'Data' => $1 } );
483 # canonicalize changed column headers
486 } elsif ($_ eq "gapq") {
488 } elsif ($_ eq "E2()") {
494 } @labels[ $rel ?
5 : 4 .. $#labels ];
496 while ( defined( $_ = $self->_readline() )
501 if ( $line[-1] =~ m/\=/o && $labels[-1] ne 'aln_code' ) {
502 # unlabelled alignment hit;
503 push @labels, "aln_code";
506 if ($line[0] eq "+-") {
508 # parse HSP, add to last parsed Hit
513 @hspData{@labels} = splice( @line, @line - @labels );
514 $hspData{lframe
} = $hit_signifs[-1]->{lframe
};
516 push @
{$hit_signifs[-1]->{HSPs
}}, \
%hspData;
520 elsif ($line[0] eq '>>><<<') {
524 my (%data, %hspData);
525 @data{@labels} = @hspData{@labels} = splice( @line, @line - @labels );
526 if ( $line[-1] =~ m/\[([1-6rf])\]/o ) {
528 $hspData{lframe
} = $data{lframe
} = (
530 ?
( $fr <= 3 ?
"+$fr" : "-@{[$fr-3]}" )
531 : ( $fr eq 'f' ?
'+1' : '-1' )
536 $hspData{lframe
} = $data{lframe
} = '0';
539 if ( $line[-1] =~ m/^\(?(\d+)\)$/ ) {
542 if ( $line[-1] =~ m/^\($/ ) {
550 # rebuild the first part of the line, preserving spaces:
551 ($_) = m/^(\S+(?:\s+\S+){$#line})/;
553 my ( $id, $desc ) = split( /\s+/, $_, 2 );
554 my @pieces = split( /\|/, $id );
555 my $acc = pop @pieces;
558 @data{qw(id desc acc)} = ( $id, $desc, $acc );
559 $data{HSPs
} = [ \
%hspData ];
561 push @hit_signifs, \
%data;
565 /^\s*([T]?FAST[XYAF]).+,\s*(\S+)\s*matrix[^\]]+?(xS)?\]\s*ktup:\s*(\d+)/
571 'Name' => 'Parameters_matrix',
577 'Name' => 'Parameters_filter',
578 'Data' => defined $3 ?
1 : 0,
583 'Name' => 'Parameters_ktup',
587 $self->{'_reporttype'} = $1
588 if ( $self->{'_reporttype'} !~ /FAST[PN]/i );
591 # get gap-pen line for FASTA33, which is not on the matrix line
593 # FASTA (3.36 June 2000) function [optimized, BL50 matrix (15:-5)] ktup: 2
594 # join: 36, opt: 24, gap-pen: -12/ -2, width: 16
596 $_ = $self->_readline();
597 if (/(?:gap\-pen|open\/ext
):\s
+([\
-\
+]?\d
+)\s
*\
/\s*([\-\+]?\d+)/) {
600 'Name' => 'Parameters_gap-open',
606 'Name' => 'Parameters_gap-ext',
614 'Name' => 'FastaOutput_program',
615 'Data' => $self->{'_reporttype'}
620 elsif (/^Algorithm:\s+(\S+)\s+.*\s*\(([^)]+)\)\s+(\S+)/) {
621 $self->{'_reporttype'} = $1
622 if ( $self->{'_reporttype'} !~ /FAST[PN]/i );
624 elsif ( /^Parameters:/ ) { # FASTA 35.04/FASTA 36
625 m/Parameters:\s+(\S+)\s+matrix\s+\([^\)]+\)(xS)?,?\s/;
628 'Name' => 'Parameters_matrix',
634 'Name' => 'Parameters_filter',
635 'Data' => defined $2 ?
$2 : 0,
638 if (/ktup:\s(\d+)/) {
641 'Name' => 'Parameters_ktup',
646 $_ = $self->_readline();
650 if (/(?:gap\-pen|open\/ext
):\s
+([\
-\
+]?\d
+)\s
*\
/\s*([\-\+]?\d+)/) {
653 'Name' => 'Parameters_gap-open',
659 'Name' => 'Parameters_gap-ext',
666 'Name' => 'FastaOutput_program',
667 'Data' => $self->{'_reporttype'}
677 'Name' => 'Parameters_ktup',
682 elsif (/^(>--)$/ || /^>>(?!>)(.+?)\s+(?:\((\d+)\s*(aa|nt)\))?$/) {
683 if ( $self->in_element('hsp') ) {
684 $self->end_element( { 'Name' => 'Hsp' } );
691 my ($hit_id, $len, $alphabet) = ($1, $2, $3);
692 if (!$len || !$alphabet) {
694 while (defined($_ = $self->_readline)) {
695 if (/(.*?)\s+\((\d+)\s*(aa|nt)\)/) {
696 ($len, $alphabet) = ($2, $3);
697 $hit_id .= $1 ?
" ".$1 : '';
700 if (/^>>(?!>)/) { # too far, throw
701 $self->throw("Couldn't find length, bailing");
705 if ( $self->in_element('hit') ) {
706 $self->end_element( { 'Name' => 'Hit' } );
707 shift @hit_signifs if @hit_signifs;
710 $self->start_element( { 'Name' => 'Hit' } );
717 my ( $id, $desc ) = split( /\s+/, $hit_id, 2 );
725 #$self->debug("Hit ID is $id\n");
726 my @pieces = split( /\|/, $id );
727 my $acc = pop @pieces;
731 'Name' => 'Hit_accession',
743 # push @{$hit_signifs[0]->{HSPs}}, $current_hsp;
747 $_ = $self->_readline();
748 my ( $score, $bits, $e, $e2 ) = /Z
-score
: \s
* (\S
+) \s
*
749 (?
: bits
: \s
* (\S
+) \s
+ )?
750 (?
: E
|expect
) \s
* \
((?
:\d
+)?\
) :? \s
*(\S
+)
751 (?
: \s
* E2 \s
* \
(\
) :? \s
*(\S
+) )?
753 $bits = $score unless defined $bits;
757 if ($firstHSP && !$m9HSP) {
758 $v = shift @
{$hit_signifs[0]->{HSPs
}}
759 if (@hit_signifs && @
{$hit_signifs[0]->{HSPs
}});
767 @
{$v}{qw(evalue evalue2 bits z-sc)} = ( $e, $e2, $bits, $score );
773 'Name' => 'Hit_signif',
774 'Data' => $v ?
$v->{evalue
} : $e
779 'Name' => 'Hit_score',
780 'Data' => $v ?
$v->{bits
} : $bits
785 $self->start_element( { 'Name' => 'Hsp' } );
789 'Name' => 'Hsp_score',
790 'Data' => $v ?
$v->{'z-sc'} : $score
795 'Name' => 'Hsp_evalue',
796 'Data' => $v ?
$v->{evalue
} : $e
801 'Name' => 'Hsp_evalue2',
802 'Data' => $v && exists($v->{evalue2
}) ?
$v->{evalue2
} : $e2
804 ) if (($v && exists($v->{evalue2
})) || defined $e2);
808 'Name' => 'Hsp_bit-score',
809 'Data' => $v ?
$v->{bits
} : $bits
812 $_ = $self->_readline();
814 if (s/global\/.* score:\s*(\d+)\;?//) {
817 'Name' => 'Hsp_sw-score',
822 if (s/Smith-Waterman score:\s*(\d+)\;?//) {
825 'Name' => 'Hsp_sw-score',
831 / (\d
*\
.?\d
+)\
% \s
* identity
832 (?
:\s
* \
(\s
*(\S
+)\
% \s
* (?
:ungapped
|similar
) \
) )?
833 \s
* in \s
* (\d
+) \s
+ (?
:aa
|nt
) \s
+ overlap \s
*
834 \
( (\d
+) \
- (\d
+) : (\d
+) \
- (\d
+) \
)
838 my ( $identper, $gapper, $len, $querystart, $queryend,
840 = ( $1, $2, $3, $4, $5, $6, $7 );
841 my $ident = sprintf( "%.0f", ( $identper / 100 ) * $len );
842 my $positive = sprintf( "%.0f", ( $gapper / 100 ) * $len );
846 'Name' => 'Hsp_identity',
852 'Name' => 'Hsp_positive',
858 'Name' => 'Hsp_align-len',
865 'Name' => 'Hsp_query-from',
866 'Data' => $querystart
871 'Name' => 'Hsp_query-to',
877 'Name' => 'Hsp_hit-from',
883 'Name' => 'Hsp_hit-to',
892 { 'Name' => 'Hsp_querygaps', 'Data' => $v->{qgaps
} } )
893 if exists $v->{qgaps
};
895 { 'Name' => 'Hsp_hitgaps', 'Data' => $v->{lgaps
} } )
896 if exists $v->{lgaps
};
898 if ( $self->{'_reporttype'} =~ m/^FAST[NXY]$/o ) {
899 if ( 8 == scalar grep { exists $v->{$_} }
900 qw(an0 ax0 pn0 px0 an1 ax1 pn1 px1) )
902 if ( $v->{ax0
} < $v->{an0
} ) {
905 'Name' => 'Hsp_query-frame',
907 "-@{[(($v->{px0} - $v->{ax0}) % 3) + 1]}"
914 'Name' => 'Hsp_query-frame',
916 "+@{[(($v->{an0} - $v->{pn0}) % 3) + 1]}"
920 if ( $v->{ax1
} < $v->{an1
} ) {
923 'Name' => 'Hsp_hit-frame',
925 "-@{[(($v->{px1} - $v->{ax1}) % 3) + 1]}"
932 'Name' => 'Hsp_hit-frame',
934 "+@{[(($v->{an1} - $v->{pn1}) % 3) + 1]}"
942 'Name' => 'Hsp_query-frame',
943 'Data' => $v->{lframe
}
947 { 'Name' => 'Hsp_hit-frame', 'Data' => 0 } );
952 { 'Name' => 'Hsp_query-frame', 'Data' => 0 } );
954 { 'Name' => 'Hsp_hit-frame', 'Data' => $v->{lframe
} } );
959 $self->warn("unable to parse FASTA score line: $_");
962 elsif (/\d+\s*residues\s*in\s*\d+\s*query\s*sequences/) {
963 if ( $self->in_element('hsp') ) {
964 $self->end_element( { 'Name' => 'Hsp' } );
966 if ( $self->in_element('hit') ) {
967 $self->end_element( { 'Name' => 'Hit' } );
968 shift @hit_signifs if @hit_signifs;
972 # $_ = $self->_readline();
973 # my ( $liblen,$libsize) = /(\d+)\s+residues\s*in(\d+)\s*library/;
974 # fast forward to the end of the file as there is
975 # nothing else left to do with this file and want to be sure and
977 while ( defined( $_ = $self->_readline() ) ) {
978 last if (/^Function used was/);
980 /(\S
+)\s
+searches\s
+a\s
+(protein\s
+or\s
+DNA\s
+)?
981 sequence
/oxi || /(\S
+)\s
+compares\s
+a
/oi
984 $self->_pushback($_);
988 $self->_processHits(@hit_signifs) if @hit_signifs;
990 $self->end_element( { 'Name' => 'FastaOutput' } );
991 return $self->end_document();
993 elsif (/^\s*\d+\s*>>>/) {
994 if ( $self->within_element('FastaOutput') ) {
995 if ( $self->in_element('hsp') ) {
996 $self->end_element( { 'Name' => 'Hsp' } );
998 if ( $self->in_element('hit') ) {
999 $self->end_element( { 'Name' => 'Hit' } );
1000 shift @hit_signifs if @hit_signifs;
1004 $self->_processHits(@hit_signifs) if (@hit_signifs);
1006 $self->end_element( { 'Name' => 'FastaOutput' } );
1007 $self->_pushback($_);
1008 return $self->end_document();
1011 $self->start_element( { 'Name' => 'FastaOutput' } );
1012 $self->{'_result_count'}++;
1016 'Name' => 'FastaOutput_program',
1017 'Data' => $self->{'_reporttype'}
1022 'Name' => 'FastaOutput_version',
1023 'Data' => $self->{'_version'}
1027 my ( $type, $querylen, $querytype, $querydef );
1029 if (/^\s*\d+\s*>>>(.*)/) {
1031 if ( $querydef =~ /^(.*?)\s+(?:\-\s+)?(\d+)\s+(aa|nt).*$/o )
1033 ( $querydef, $querylen, $querytype ) = ( $1, $2, $3 );
1037 if ( $self->{'_reporttype'}
1038 && $self->{'_reporttype'} eq 'FASTA' )
1040 if ( $querytype eq 'nt' ) {
1041 $self->{'_reporttype'} = 'FASTN';
1043 elsif ( $querytype eq 'aa' ) {
1044 $self->{'_reporttype'} = 'FASTP';
1047 my ( $name, $descr ) =
1048 ( $querydef =~ m/^(\S+)(?:\s+(.*))?\s*$/o );
1051 'Name' => 'FastaOutput_query-def',
1057 'Name' => 'FastaOutput_querydesc',
1064 'Name' => 'FastaOutput_query-len',
1070 $self->warn("unable to find and set query length");
1072 if ( defined( $_ = $self->_readline() )
1073 && ( /^\s*vs\s+(\S+)/ || /^Library:\s+(\S+)/ ) )
1077 'Name' => 'FastaOutput_db',
1085 elsif ( $self->in_element('hsp') ) {
1086 my @data = ( [], [], [] );
1088 my $len = $self->idlength + 1;
1090 while ( defined($_) ) {
1092 #$self->debug("$count $_\n");
1093 if (/residues in \d+\s+query\s+sequences/o) {
1094 $self->_pushback($_);
1097 elsif (/^>>>(\*\*\*|\/\
/\/|<<<)/o
) {
1098 $self->end_element( { Name
=> "Hsp" } );
1102 $self->_pushback($_);
1105 elsif (/^\s*\d+\s*>>>/o) {
1106 $self->_pushback($_);
1109 if ( $count == 0 ) {
1111 $self->_pushback($_);
1114 elsif ( /^\s+\d+/ || /^\s+$/ ) {
1116 # do nothing, this is really a 0 line
1118 elsif ( length($_) == 0 ) {
1122 $self->_pushback($_);
1126 elsif ( $count == 1 || $count == 3 ) {
1128 $len = CORE
::length($1) if $len < CORE
::length($1);
1129 s/\s+$//; # trim trailing spaces,we don't want them
1130 push @
{ $data[ $count - 1 ] }, substr( $_, $len );
1132 elsif (/^\s+(\d+)/) {
1134 $self->_pushback($_);
1136 elsif ( /^\s+$/ || length($_) == 0 ) {
1139 # going to skip these
1141 elsif ( /\s+\S+fasta3\d\s+/) {
1142 # this is something that looks like a path but contains
1143 # the fasta3x executable string, such as:
1144 # /usr/local/fasta3/bin/fasta35 -n -U -Q -H -A -E 2.0 -C 19 -m 0 -m 9i test.fa ../other_mirs.fa -O test.fasta35
1149 "Unrecognized alignment line ($count) '$_'");
1152 elsif ( $count == 2 ) {
1154 $self->warn("$_\n") if $self->verbose > 0;
1156 # we are on a Subject part of the alignment
1157 # but we THOUGHT we were on the Query
1158 # move that last line to the proper place
1159 push @
{ $data[2] }, pop @
{ $data[0] };
1164 # toss the first IDLENGTH characters of the line
1165 if ( length($_) >= $len ) {
1166 push @
{ $data[ $count - 1 ] }, substr( $_, $len );
1168 elsif (/^$/) { # fix issue #255
1173 last if ( $count++ >= 5 );
1174 $_ = $self->_readline();
1176 if ( @
{ $data[0] } || @
{ $data[2] } ) {
1179 'Name' => 'Hsp_qseq',
1180 'Data' => join( '', @
{ $data[0] } )
1185 'Name' => 'Hsp_midline',
1186 'Data' => join( '', @
{ $data[1] } )
1191 'Name' => 'Hsp_hseq',
1192 'Data' => join( '', @
{ $data[2] } )
1200 #$self->warn("unrecognized FASTA Family report file!");
1205 if ( $self->in_element('result') ) {
1206 if ( $self->in_element('hsp') ) {
1207 $self->end_element( { 'Name' => 'Hsp' } );
1209 if ( $self->in_element('hit') ) {
1210 $self->end_element( { 'Name' => 'Hit' } );
1211 shift @hit_signifs if @hit_signifs;
1213 $self->end_element( { 'Name' => 'FastaOutput' } );
1215 return $self->end_document();
1218 =head2 start_element
1220 Title : start_element
1221 Usage : $eventgenerator->start_element
1222 Function: Handles a start element event
1224 Args : hashref with at least 2 keys 'Data' and 'Name'
1230 my ( $self, $data ) = @_;
1232 # we currently don't care about attributes
1233 my $nm = $data->{'Name'};
1234 if ( my $type = $MODEMAP{$nm} ) {
1235 $self->_mode($type);
1236 if ( my $handler = $self->_will_handle($type) ) {
1237 my $func = sprintf( "start_%s", lc $type );
1238 $handler->$func( $data->{'Attributes'} );
1240 unshift @
{ $self->{'_elements'} }, $type;
1242 if ( $nm eq 'FastaOutput' ) {
1243 $self->{'_values'} = {};
1244 $self->{'_result'} = undef;
1245 $self->{'_mode'} = '';
1252 Title : start_element
1253 Usage : $eventgenerator->end_element
1254 Function: Handles an end element event
1256 Args : hashref with at least 2 keys 'Data' and 'Name'
1262 my ( $self, $data ) = @_;
1263 my $nm = $data->{'Name'};
1266 # Hsp are sort of weird, in that they end when another
1267 # object begins so have to detect this in end_element for now
1268 if ( $nm eq 'Hsp' ) {
1269 foreach (qw(Hsp_qseq Hsp_midline Hsp_hseq)) {
1273 'Data' => $self->{'_last_hspdata'}->{$_}
1277 $self->{'_last_hspdata'} = {};
1280 if ( my $type = $MODEMAP{$nm} ) {
1281 if ( my $handler = $self->_will_handle($type) ) {
1282 my $func = sprintf( "end_%s", lc $type );
1283 $rc = $handler->$func( $self->{'_reporttype'}, $self->{'_values'} );
1285 shift @
{ $self->{'_elements'} };
1288 elsif ( $MAPPING{$nm} ) {
1289 if ( ref( $MAPPING{$nm} ) =~ /hash/i ) {
1290 my $key = ( keys %{ $MAPPING{$nm} } )[0];
1291 $self->{'_values'}->{$key}->{ $MAPPING{$nm}->{$key} } =
1292 $self->{'_last_data'};
1295 $self->{'_values'}->{ $MAPPING{$nm} } = $self->{'_last_data'};
1299 $self->warn("unknown nm $nm, ignoring\n");
1301 $self->{'_last_data'} = ''; # remove read data if we are at
1303 $self->{'_result'} = $rc if ( $nm eq 'FastaOutput' );
1311 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
1312 Function: Convience method that calls start_element, characters, end_element
1314 Args : Hash ref with the keys 'Name' and 'Data'
1320 my ( $self, $data ) = @_;
1321 $self->start_element($data);
1322 $self->characters($data);
1323 $self->end_element($data);
1329 Usage : $eventgenerator->characters($str)
1330 Function: Send a character events
1338 my ( $self, $data ) = @_;
1340 return unless ( defined $data->{'Data'} );
1341 if ( $data->{'Data'} =~ /^\s+$/ ) {
1342 return unless $data->{'Name'} =~ /Hsp\_(midline|qseq|hseq)/;
1345 if ( $self->in_element('hsp')
1346 && $data->{'Name'} =~ /Hsp\_(qseq|hseq|midline)/ )
1349 $self->{'_last_hspdata'}->{ $data->{'Name'} } .= $data->{'Data'};
1352 $self->{'_last_data'} = $data->{'Data'};
1358 Usage : $obj->_mode($newval)
1361 Returns : value of _mode
1362 Args : newvalue (optional)
1368 my ( $self, $value ) = @_;
1369 if ( defined $value ) {
1370 $self->{'_mode'} = $value;
1372 return $self->{'_mode'};
1375 =head2 within_element
1377 Title : within_element
1378 Usage : if( $eventgenerator->within_element($element) ) {}
1379 Function: Test if we are within a particular element
1380 This is different than 'in' because within can be tested
1383 Args : string element name
1388 sub within_element
{
1389 my ( $self, $name ) = @_;
1391 if (!defined $name && !defined $self->{'_elements'}
1392 || scalar @
{ $self->{'_elements'} } == 0 );
1393 foreach ( @
{ $self->{'_elements'} } ) {
1394 if ( $_ eq $name || $_ eq $MODEMAP{$name} ) {
1404 Usage : if( $eventgenerator->in_element($element) ) {}
1405 Function: Test if we are in a particular element
1406 This is different than 'in' because within can be tested
1409 Args : string element name
1415 my ( $self, $name ) = @_;
1416 return 0 if !defined $self->{'_elements'}->[0];
1418 $self->{'_elements'}->[0] eq $name
1419 || ( exists $MODEMAP{$name}
1420 && $self->{'_elements'}->[0] eq $MODEMAP{$name} )
1424 =head2 start_document
1426 Title : start_document
1427 Usage : $eventgenerator->start_document
1428 Function: Handles a start document event
1435 sub start_document
{
1437 $self->{'_lasttype'} = '';
1438 $self->{'_values'} = {};
1439 $self->{'_result'} = undef;
1440 $self->{'_mode'} = '';
1441 $self->{'_elements'} = [];
1446 Title : end_document
1447 Usage : $eventgenerator->end_document
1448 Function: Handles an end document event
1449 Returns : Bio::Search::Result::ResultI object
1456 my ( $self, @args ) = @_;
1457 return $self->{'_result'};
1463 Usage : $obj->idlength($newval)
1464 Function: Internal storage of the length of the ID desc
1465 in the HSP alignment blocks. Defaults to
1466 $IDLENGTH class variable value
1467 Returns : value of idlength
1468 Args : newvalue (optional)
1474 my ( $self, $value ) = @_;
1475 if ( defined $value ) {
1476 $self->{'_idlength'} = $value;
1478 return $self->{'_idlength'} || $IDLENGTH;
1483 Title : result_count
1484 Usage : my $count = $searchio->result_count
1485 Function: Returns the number of results we have processed
1493 return $self->{'_result_count'};
1496 sub attach_EventHandler
{
1497 my ( $self, $handler ) = @_;
1499 $self->SUPER::attach_EventHandler
($handler);
1501 # Optimization: caching the EventHandler since it is used a lot
1504 $self->{'_handler_cache'} = $handler;
1510 Title : _will_handle
1511 Usage : Private method. For internal use only.
1512 if( $self->_will_handle($type) ) { ... }
1513 Function: Provides an optimized way to check whether or not an element of a
1514 given type is to be handled.
1515 Returns : Reference to EventHandler object if the element type is to be handled.
1516 undef if the element type is not to be handled.
1517 Args : string containing type of element.
1525 Using the cached pointer to the EventHandler to minimize repeated
1530 Caching the will_handle status for each type that is encountered so
1531 that it only need be checked by calling
1532 handler-E<gt>will_handle($type) once.
1536 This does not lead to a major savings by itself (only 5-10%). In
1537 combination with other optimizations, or for large parse jobs, the
1538 savings good be significant.
1540 To test against the unoptimized version, remove the parentheses from
1541 around the third term in the ternary " ? : " operator and add two
1542 calls to $self-E<gt>_eventHandler().
1547 my ( $self, $type ) = @_;
1548 my $handler = $self->{'_handler_cache'};
1550 defined( $self->{'_will_handle_cache'}->{$type} )
1551 ?
$self->{'_will_handle_cache'}->{$type}
1552 : ( $self->{'_will_handle_cache'}->{$type} =
1553 $handler->will_handle($type) );
1555 return $will_handle ?
$handler : undef;
1560 Title : _processHits
1561 Usage : Private method. For internal use only.
1562 Function: Process/report any hits/hsps we saw in the top table, not in alignments.
1564 Args : array of hits to process.
1570 my ($self, @hit_signifs) = @_;
1572 # process remaining best hits
1573 for my $hit (@hit_signifs) {
1575 # Hsp_score Hsp_evalue Hsp_bit-score
1576 # Hsp_sw-score Hsp_gaps Hsp_identity Hsp_positive
1577 # Hsp_align-len Hsp_query-from Hsp_query-to
1578 # Hsp_hit-from Hsp_hit-to Hsp_qseq Hsp_midline
1580 $self->start_element( { 'Name' => 'Hit' } );
1583 'Name' => 'Hit_len',
1584 'Data' => $hit->{hit_len
}
1586 ) if exists $hit->{hit_len
};
1590 'Data' => $hit->{id
}
1592 ) if exists $hit->{id
};
1595 'Name' => 'Hit_accession',
1596 'Data' => $hit->{acc
}
1598 ) if exists $hit->{acc
};
1601 'Name' => 'Hit_def',
1602 'Data' => $hit->{desc
}
1604 ) if exists $hit->{desc
};
1607 'Name' => 'Hit_signif',
1608 'Data' => $hit->{evalue
}
1610 ) if exists $hit->{evalue
};
1613 'Name' => 'Hit_score',
1614 'Data' => $hit->{bits
}
1616 ) if exists $hit->{bits
};
1618 for my $hsp (@
{$hit->{HSPs
}}) {
1620 $self->start_element( { 'Name' => 'Hsp' } );
1621 $self->element({'Name' => 'Hsp_score', 'Data' => $hsp->{'z-sc'}})
1622 if exists $hsp->{'z-sc'};
1623 $self->element({'Name' => 'Hsp_evalue', 'Data' => $hsp->{evalue
} } )
1624 if exists $hsp->{evalue
};
1625 $self->element({'Name' => 'Hsp_evalue2', 'Data' => $hsp->{evalue2
} } )
1626 if exists $hsp->{evalue2
};
1628 $self->element({'Name' => 'Hsp_bit-score', 'Data' => $hsp->{bits
} } )
1629 if exists $hsp->{bits
};
1630 $self->element({'Name' => 'Hsp_sw-score', 'Data' => $hsp->{'n-w'} } )
1631 if exists $hsp->{'n-w'};
1632 $self->element({'Name' => 'Hsp_sw-score', 'Data' => $hsp->{sw
} } )
1633 if exists $hsp->{sw
};
1634 $self->element({'Name' => 'Hsp_gaps', 'Data' => $hsp->{'%_gid'} } )
1635 if exists $hsp->{'%_gid'};
1637 'Name' => 'Hsp_identity',
1639 sprintf( "%.0f", $hsp->{'%_id'} * $hsp->{alen
} )
1640 }) if ( exists $hsp->{'%_id'} && exists $hsp->{alen
} );
1642 if ( exists $hsp->{'%_gid'} ) {
1645 'Name' => 'Hsp_positive',
1647 sprintf( "%.0f", $hsp->{'%_gid'} * $hsp->{alen
} )
1649 ) if exists $hsp->{'%_gid'} && exists $hsp->{alen
};
1653 'Name' => 'Hsp_positive',
1655 sprintf( "%.0f", $hsp->{'%_id'} * $hsp->{alen
} )
1657 ) if ( exists $hsp->{'%_id'} && exists $hsp->{alen
} );
1662 'Name' => 'Hsp_align-len', 'Data' => $hsp->{alen
} } )
1663 if exists $hsp->{alen
};
1666 'Name' => 'Hsp_query-from', 'Data' => $hsp->{an0
} } )
1667 if exists $hsp->{an0
};
1670 'Name' => 'Hsp_query-to', 'Data' => $hsp->{ax0
} } )
1671 if exists $hsp->{ax0
};
1674 'Name' => 'Hsp_hit-from', 'Data' => $hsp->{an1
} } )
1675 if exists $hsp->{an1
};
1678 'Name' => 'Hsp_hit-to', 'Data' => $hsp->{ax1
} } )
1679 if exists $hsp->{ax1
};
1683 'Name' => 'Hsp_querygaps', 'Data' => $hsp->{qgaps
} } )
1684 if exists $hsp->{qgaps
};
1687 'Name' => 'Hsp_hitgaps', 'Data' => $hsp->{lgaps
} } )
1688 if exists $hsp->{lgaps
};
1690 if ( $self->{'_reporttype'} =~ m/^FAST[NXY]$/o ) {
1691 if ( 8 == scalar grep { exists $hsp->{$_} }
1692 qw(an0 ax0 pn0 px0 an1 ax1 pn1 px1) ) {
1693 if ( $hsp->{ax0
} < $hsp->{an0
} ) {
1696 'Name' => 'Hsp_query-frame',
1698 "-@{[(($hsp->{px0} - $hsp->{ax0}) % 3) + 1]}"
1704 'Name' => 'Hsp_query-frame',
1706 "+@{[(($hsp->{an0} - $hsp->{pn0}) % 3) + 1]}"
1710 if ( $hsp->{ax1
} < $hsp->{an1
} ) {
1713 'Name' => 'Hsp_hit-frame',
1715 "-@{[(($hsp->{px1} - $hsp->{ax1}) % 3) + 1]}"
1721 'Name' => 'Hsp_hit-frame',
1723 "+@{[(($hsp->{an1} - $hsp->{pn1}) % 3) + 1]}"
1730 'Name' => 'Hsp_query-frame',
1731 'Data' => $hsp->{lframe
}
1736 'Name' => 'Hsp_hit-frame', 'Data' => 0 } );
1741 'Name' => 'Hsp_query-frame', 'Data' => 0 } );
1744 'Name' => 'Hsp_hit-frame',
1745 'Data' => $hsp->{lframe
}
1750 $self->end_element( { 'Name' => 'Hsp' } );
1753 $self->end_element( { 'Name' => 'Hit' } );