FAST/Bio/SearchIO/infernal.pm untested

filter: Statementstmt 0.0% 0 / 469 Branchbran 0.0% 0 / 230 Conditioncond 0.0% 0 / 101 MC/DCmcdc 0.0% 0 / 66 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 919 CC 116 SCAR 62.2
CC
116
Cov
0%
_parse_latest
1056.0
_parse_pre
812.0
_parse_old
600.0
CRAP 504.0
1 #
2 # BioPerl module for FAST::Bio::SearchIO::infernal
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Chris Fields <cjfields-at-uiuc-dot-edu>
7 #
8 # Copyright Chris Fields
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 =head1 NAME
15  
16 FAST::Bio::SearchIO::infernal - SearchIO-based Infernal parser
17  
18 =head1 SYNOPSIS
19  
20 my $parser = FAST::Bio::SearchIO->new(-format => 'infernal',
21 -file => 'purine.inf');
22 while( my $result = $parser->next_result ) {
23 # general result info, such as model used, Infernal version
24 while( my $hit = $result->next_hit ) {
25 while( my $hsp = $hit->next_hsp ) {
26 # ...
27 }
28 }
29 }
30  
31 =head1 DESCRIPTION
32  
33 This is a SearchIO-based parser for Infernal output from the cmsearch program.
34 It currently parses cmsearch output for Infernal versions 0.7-1.0; older
35 versions may work but will not be supported.
36  
37 As the first stable version has been released (and output has stabilized) it is
38 highly recommended that users upgrade to using the latest Infernal release.
39 Support for the older pre-v.1 developer releases will be dropped for future core
40 1.6 releases.
41  
42 =head1 FEEDBACK
43  
44 =head2 Mailing Lists
45  
46 User feedback is an integral part of the evolution of this and other
47 Bioperl modules. Send your comments and suggestions preferably to
48 the Bioperl mailing list. Your participation is much appreciated.
49  
50 bioperl-l@bioperl.org - General discussion
51 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
52  
53 =head2 Support
54  
55 Please direct usage questions or support issues to the mailing list:
56  
57 I<bioperl-l@bioperl.org>
58  
59 rather than to the module maintainer directly. Many experienced and
60 reponsive experts will be able look at the problem and quickly
61 address it. Please include a thorough description of the problem
62 with code and data examples if at all possible.
63  
64 =head2 Reporting Bugs
65  
66 Report bugs to the Bioperl bug tracking system to help us keep track
67 of the bugs and their resolution. Bug reports can be submitted via the
68 web:
69  
70 https://redmine.open-bio.org/projects/bioperl/
71  
72 =head1 AUTHOR - Chris Fields
73  
74 Email cjfields-at-uiuc-dot-edu
75  
76 =head1 CONTRIBUTORS
77  
78 Jeffrey Barrick, Michigan State University
79  
80 =head1 APPENDIX
81  
82 The rest of the documentation details each of the object methods.
83 Internal methods are usually preceded with a _
84  
85 =cut
86  
87 # Let the code begin...
88  
89 package FAST::Bio::SearchIO::infernal;
90 use strict;
91  
92 use Data::Dumper;
93 use base qw(FAST::Bio::SearchIO);
94  
95 our %MODEMAP = (
96 'Result' => 'result',
97 'Hit' => 'hit',
98 'Hsp' => 'hsp'
99 );
100  
101 our %MAPPING = (
102 'Hsp_bit-score' => 'HSP-bits',
103 'Hsp_score' => 'HSP-score',
104 'Hsp_evalue' => 'HSP-evalue', # evalues only in v0.81, optional
105 'Hsp_pvalue' => 'HSP-pvalue', # pvalues only in v0.81, optional
106 'Hsp_query-from' => 'HSP-query_start',
107 'Hsp_query-to' => 'HSP-query_end',
108 'Hsp_hit-from' => 'HSP-hit_start',
109 'Hsp_hit-to' => 'HSP-hit_end',
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_structure' => 'HSP-meta',
117 'Hsp_align-len' => 'HSP-hsp_length',
118 'Hsp_stranded' => 'HSP-stranded',
119
120 'Hit_id' => 'HIT-name',
121 'Hit_len' => 'HIT-length',
122 'Hit_gi' => 'HIT-ncbi_gi',
123 'Hit_accession' => 'HIT-accession',
124 'Hit_def' => 'HIT-description',
125 'Hit_signif' => 'HIT-significance', # evalues only in v0.81, optional
126 'Hit_p' => 'HIT-p', # pvalues in 1.0, optional
127 'Hit_score' => 'HIT-score', # best HSP bit score
128 'Hit_bits' => 'HIT-bits', # best HSP bit score
129
130 'Infernal_program' => 'RESULT-algorithm_name', # get/set
131 'Infernal_version' => 'RESULT-algorithm_version', # get/set
132 'Infernal_query-def'=> 'RESULT-query_name', # get/set
133 'Infernal_query-len'=> 'RESULT-query_length',
134 'Infernal_query-acc'=> 'RESULT-query_accession', # get/set
135 'Infernal_querydesc'=> 'RESULT-query_description', # get/set
136 'Infernal_db' => 'RESULT-database_name', # get/set
137 'Infernal_db-len' => 'RESULT-database_entries', # none yet
138 'Infernal_db-let' => 'RESULT-database_letters', # none yet
139 );
140  
141 my $MINSCORE = 0;
142 my $DEFAULT_ALGORITHM = 'cmsearch';
143 my $DEFAULT_VERSION = '1.0';
144  
145 my @VALID_SYMBOLS = qw(5-prime 3-prime single-strand unknown gap);
146 my %STRUCTURE_SYMBOLS = (
147 '5-prime' => '<',
148 '3-prime' => '>',
149 'single-strand' => ':',
150 'unknown' => '?',
151 'gap' => '.'
152 );
153  
154 =head2 new
155  
156 Title : new
157 Usage : my $obj = FAST::Bio::SearchIO::infernal->new();
158 Function: Builds a new FAST::Bio::SearchIO::infernal object
159 Returns : FAST::Bio::SearchIO::infernal
160 Args : -fh/-file => cmsearch (infernal) filename
161 -format => 'infernal'
162 -model => query model (Rfam ID) (default undef)
163 -database => database name (default undef)
164 -query_acc => query accession, eg. Rfam accession RF####
165 -query_desc => query description, eg. Rfam description
166 -hsp_minscore => minimum HSP score cutoff
167 -convert_meta => boolean, set to convert meta string to simple WUSS format
168 -symbols => hash ref of structure symbols to use
169 (default symbols in %STRUCTURE_SYMBOLS hash)
170  
171 =cut
172  
173 sub _initialize {
174 my ( $self, @args ) = @_;
175 $self->SUPER::_initialize(@args);
176 my ($model, $database, $convert, $symbols, $cutoff,
177 $desc, $accession, $algorithm, $version) =
178 $self->_rearrange([qw(MODEL
179 DATABASE
180 CONVERT_META
181 SYMBOLS
182 HSP_MINSCORE
183 QUERY_DESC
184 QUERY_ACC
185 ALGORITHM
186 VERSION)],@args);
187 my $handler = $self->_eventHandler;
188 $handler->register_factory(
189 'result',
190 FAST::Bio::Factory::ObjectFactory->new(
191 -type => 'FAST::Bio::Search::Result::GenericResult',
192 -interface => 'FAST::Bio::Search::Result::ResultI',
193 -verbose => $self->verbose
194 )
195 );
196  
197 $handler->register_factory(
198 'hit',
199 FAST::Bio::Factory::ObjectFactory->new(
200 -type => 'FAST::Bio::Search::Hit::ModelHit',
201 -interface => 'FAST::Bio::Search::Hit::HitI',
202 -verbose => $self->verbose
203 )
204 );
205  
206 $handler->register_factory(
207 'hsp',
208 FAST::Bio::Factory::ObjectFactory->new(
209 -type => 'FAST::Bio::Search::HSP::ModelHSP',
210 -interface => 'FAST::Bio::Search::HSP::HSPI',
211 -verbose => $self->verbose
212 )
213 );
214
215 defined $model && $self->model($model);
216 defined $database && $self->database($database);
217 defined $accession && $self->query_accession($accession);
218 defined $convert && $self->convert_meta($convert);
219 defined $desc && $self->query_description($desc);
220
221 $version ||= $DEFAULT_VERSION;
222 $self->version($version);
223 $symbols ||= \%STRUCTURE_SYMBOLS;
224 $self->structure_symbols($symbols);
225 $cutoff ||= $MINSCORE;
226 $self->hsp_minscore($cutoff);
227 $algorithm ||= $DEFAULT_ALGORITHM;
228 $self->algorithm($algorithm);
229 }
230  
231 =head2 next_result
232  
233 Title : next_result
234 Usage : my $hit = $searchio->next_result;
235 Function: Returns the next Result from a search
236 Returns : FAST::Bio::Search::Result::ResultI object
237 Args : none
238  
239 =cut
240  
241 sub next_result {
242 my ($self) = @_;
243 unless (exists $self->{'_handlerset'}) {
244 my $line;
245 while ($line = $self->_readline) {
246 # advance to first line
247 next if $line =~ m{^\s*$};
248 # newer output starts with model name
249 if ($line =~ m{^\#\s+cmsearch\s}) {
250 $self->{'_handlerset'} = 'latest';
251 } elsif ($line =~ m{^CM\s\d+:}) {
252 $self->{'_handlerset'} = 'pre-1.0';
253 } else {
254 $self->{'_handlerset'} ='old';
255 }
256 last;
257 }
258 $self->_pushback($line);
259 #if ($self->{'_handlerset'} ne '1.0') {
260 # $self->deprecated(
261 # -message => "Parsing of Infernal pre-1.0 release is deprecated;\n".
262 # "upgrading to Infernal 1.0 or above is highly recommended",
263 # -version => 1.007);
264 #}
265 }
266 return ($self->{'_handlerset'} eq 'latest') ? $self->_parse_latest :
267 ($self->{'_handlerset'} eq 'pre-1.0') ? $self->_parse_pre :
268 $self->_parse_old;
269 }
270  
271 =head2 start_element
272  
273 Title : start_element
274 Usage : $eventgenerator->start_element
275 Function: Handles a start element event
276 Returns : none
277 Args : hashref with at least 2 keys 'Data' and 'Name'
278  
279  
280 =cut
281  
282 sub start_element {
283 my ( $self, $data ) = @_;
284  
285 # we currently don't care about attributes
286 my $nm = $data->{'Name'};
287 my $type = $MODEMAP{$nm};
288 if ($type) {
289 if ( $self->_eventHandler->will_handle($type) ) {
290 my $func = sprintf( "start_%s", lc $type );
291 $self->_eventHandler->$func( $data->{'Attributes'} );
292 }
293 unshift @{ $self->{'_elements'} }, $type;
294 }
295 if ( defined $type
296 && $type eq 'result' )
297 {
298 $self->{'_values'} = {};
299 $self->{'_result'} = undef;
300 }
301 }
302  
303 =head2 end_element
304  
305 Title : start_element
306 Usage : $eventgenerator->end_element
307 Function: Handles an end element event
308 Returns : none
309 Args : hashref with at least 2 keys, 'Data' and 'Name'
310  
311 =cut
312  
313 sub end_element {
314 my ( $self, $data ) = @_;
315 my $nm = $data->{'Name'};
316 my $type = $MODEMAP{$nm};
317 my $rc;
318  
319 if ($type) {
320 if ( $self->_eventHandler->will_handle($type) ) {
321 my $func = sprintf( "end_%s", lc $type );
322 $rc = $self->_eventHandler->$func( $self->{'_reporttype'},
323 $self->{'_values'} );
324 }
325 my $lastelem = shift @{ $self->{'_elements'} };
326 }
327 elsif ( $MAPPING{$nm} ) {
328 if ( ref( $MAPPING{$nm} ) =~ /hash/i ) {
329 my $key = ( keys %{ $MAPPING{$nm} } )[0];
330 $self->{'_values'}->{$key}->{ $MAPPING{$nm}->{$key} } =
331 $self->{'_last_data'};
332 }
333 else {
334 $self->{'_values'}->{ $MAPPING{$nm} } = $self->{'_last_data'};
335 }
336 }
337 else {
338 $self->debug("unknown nm $nm, ignoring\n");
339 }
340 $self->{'_last_data'} = ''; # remove read data if we are at
341 # end of an element
342 $self->{'_result'} = $rc if ( defined $type && $type eq 'result' );
343 return $rc;
344 }
345  
346 =head2 element
347  
348 Title : element
349 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
350 Function: Convenience method that calls start_element, characters, end_element
351 Returns : none
352 Args : Hash ref with the keys 'Name' and 'Data'
353  
354 =cut
355  
356 sub element {
357 my ( $self, $data ) = @_;
358 # simple data calls (%MAPPING) do not need start_element
359 $self->characters($data);
360 $self->end_element($data);
361 }
362  
363 =head2 element_hash
364  
365 Title : element
366 Usage : $eventhandler->element_hash({'Hsp_hit-from' => $start,
367 'Hsp_hit-to' => $end,
368 'Hsp_score' => $lastscore});
369 Function: Convenience method that takes multiple simple data elements and
370 maps to appropriate parameters
371 Returns : none
372 Args : Hash ref with the mapped key (in %MAPPING) and value
373  
374 =cut
375  
376 sub element_hash {
377 my ($self, $data) = @_;
378 $self->throw("Must provide data hash ref") if !$data || !ref($data);
379 for my $nm (sort keys %{$data}) {
380 next if $data->{$nm} && $data->{$nm} =~ m{^\s*$}o;
381 if ( $MAPPING{$nm} ) {
382 if ( ref( $MAPPING{$nm} ) =~ /hash/i ) {
383 my $key = ( keys %{ $MAPPING{$nm} } )[0];
384 $self->{'_values'}->{$key}->{ $MAPPING{$nm}->{$key} } =
385 $data->{$nm};
386 }
387 else {
388 $self->{'_values'}->{ $MAPPING{$nm} } = $data->{$nm};
389 }
390 }
391 }
392 }
393  
394 =head2 characters
395  
396 Title : characters
397 Usage : $eventgenerator->characters($str)
398 Function: Send a character events
399 Returns : none
400 Args : string
401  
402  
403 =cut
404  
405 sub characters {
406 my ( $self, $data ) = @_;
407 return unless ( defined $data->{'Data'} && $data->{'Data'} !~ /^\s+$/o );
408 $self->{'_last_data'} = $data->{'Data'};
409 }
410  
411 =head2 within_element
412  
413 Title : within_element
414 Usage : if( $eventgenerator->within_element($element) ) {}
415 Function: Test if we are within a particular element
416 This is different than 'in' because within can be tested
417 for a whole block.
418 Returns : boolean
419 Args : string element name
420  
421 =cut
422  
423 sub within_element {
424 my ( $self, $name ) = @_;
425 return 0
426 if ( !defined $name
427 || !defined $self->{'_elements'}
428 || scalar @{ $self->{'_elements'} } == 0 );
429 foreach ( @{ $self->{'_elements'} } ) {
430 return 1 if ( $_ eq $name );
431 }
432 return 0;
433 }
434  
435 =head2 in_element
436  
437 Title : in_element
438 Usage : if( $eventgenerator->in_element($element) ) {}
439 Function: Test if we are in a particular element
440 This is different than 'within' because 'in' only
441 tests its immediate parent.
442 Returns : boolean
443 Args : string element name
444  
445 =cut
446  
447 sub in_element {
448 my ( $self, $name ) = @_;
449 return 0 if !defined $self->{'_elements'}->[0];
450 return ( $self->{'_elements'}->[0] eq $name );
451 }
452  
453 =head2 start_document
454  
455 Title : start_document
456 Usage : $eventgenerator->start_document
457 Function: Handle a start document event
458 Returns : none
459 Args : none
460  
461 =cut
462  
463 sub start_document {
464 my ($self) = @_;
465 $self->{'_lasttype'} = '';
466 $self->{'_values'} = {};
467 $self->{'_result'} = undef;
468 $self->{'_elements'} = [];
469 }
470  
471 =head2 end_document
472  
473 Title : end_document
474 Usage : $eventgenerator->end_document
475 Function: Handles an end document event
476 Returns : FAST::Bio::Search::Result::ResultI object
477 Args : none
478  
479 =cut
480  
481 sub end_document {
482 my ($self) = @_;
483 return $self->{'_result'};
484 }
485  
486 =head2 result_count
487  
488 Title : result_count
489 Usage : my $count = $searchio->result_count
490 Function: Returns the number of results we have processed
491 Returns : integer
492 Args : none
493  
494 =cut
495  
496 sub result_count {
497 my $self = shift;
498 return $self->{'_result_count'};
499 }
500  
501 =head2 model
502  
503 Title : model
504 Usage : my $model = $parser->model();
505 Function: Get/Set model; Infernal currently does not output
506 the model name (Rfam ID)
507 Returns : String (name of model)
508 Args : [optional] String (name of model)
509  
510 =cut
511  
512 sub model {
513 my $self = shift;
514 return $self->{'_model'} = shift if @_;
515 return $self->{'_model'};
516 }
517  
518 =head2 database
519  
520 Title : database
521 Usage : my $database = $parser->database();
522 Function: Get/Set database; Infernal currently does not output
523 the database name
524 Returns : String (database name)
525 Args : [optional] String (database name)
526  
527 =cut
528  
529 sub database {
530 my $self = shift;
531 return $self->{'_database'} = shift if @_;
532 return $self->{'_database'};
533 }
534  
535 =head2 algorithm
536  
537 Title : algorithm
538 Usage : my $algorithm = $parser->algorithm();
539 Function: Get/Set algorithm; current versions of Infernal do not output
540 the algorithm name
541 Returns : String (algorithm name)
542 Args : [optional] String (algorithm name)
543  
544 =cut
545  
546 sub algorithm {
547 my $self = shift;
548 return $self->{'_algorithm'} = shift if @_;
549 return $self->{'_algorithm'};
550 }
551  
552 =head2 query_accession
553  
554 Title : query_accession
555 Usage : my $acc = $parser->query_accession();
556 Function: Get/Set query (model) accession; Infernal currently does not output
557 the accession number (Rfam accession #)
558 Returns : String (accession)
559 Args : [optional] String (accession)
560  
561 =cut
562  
563 sub query_accession {
564 my $self = shift;
565 return $self->{'_query_accession'} = shift if @_;
566 return $self->{'_query_accession'};
567 }
568  
569 =head2 query_description
570  
571 Title : query_description
572 Usage : my $acc = $parser->query_description();
573 Function: Get/Set query (model) description; Infernal currently does not output
574 the Rfam description
575 Returns : String (description)
576 Args : [optional] String (description)
577  
578 =cut
579  
580 sub query_description {
581 my $self = shift;
582 return $self->{'_query_description'} = shift if @_;
583 return $self->{'_query_description'};
584 }
585  
586 =head2 hsp_minscore
587  
588 Title : hsp_minscore
589 Usage : my $cutoff = $parser->hsp_minscore();
590 Function: Get/Set min bit score cutoff (for generating Hits/HSPs)
591 Returns : score (number)
592 Args : [optional] score (number)
593  
594 =cut
595  
596 sub hsp_minscore {
597 my $self = shift;
598 return $self->{'_hsp_minscore'} = shift if @_;
599 return $self->{'_hsp_minscore'};
600 }
601  
602 =head2 convert_meta
603  
604 Title : convert_meta
605 Usage : $parser->convert_meta(1);
606 Function: Get/Set boolean flag for converting Infernal WUSS format
607 to a simple bracketed format (simple WUSS by default)
608 Returns : boolean flag (TRUE or FALSE)
609 Args : [optional] boolean (eval's to TRUE or FALSE)
610  
611 =cut
612  
613 sub convert_meta {
614 my $self = shift;
615 return $self->{'_convert_meta'} = shift if @_;
616 return $self->{'_convert_meta'};
617 }
618  
619 =head2 version
620  
621 Title : version
622 Usage : $parser->version();
623 Function: Set the Infernal cmsearch version
624 Returns : version
625 Args : [optional] version
626  
627 =cut
628  
629 sub version {
630 my $self = shift;
631 return $self->{'_version'} = shift if @_;
632 return $self->{'_version'};
633 }
634  
635 =head2 structure_symbols
636  
637 Title : structure_symbols
638 Usage : my $hashref = $parser->structure_symbols();
639 Function: Get/Set RNA structure symbols
640 Returns : Hash ref of delimiters (5' stem, 3' stem, single-strand, etc)
641 : default = < (5-prime)
642 > (3-prime)
643 : (single-strand)
644 ? (unknown)
645 . (gap)
646 Args : Hash ref of substitute delimiters, using above keys.
647  
648 =cut
649  
650 sub structure_symbols {
651 my ($self, $delim) = @_;
652 if ($delim) {
653 if (ref($delim) =~ m{HASH}) {
654 my %data = %{ $delim };
655 for my $d (@VALID_SYMBOLS) {
656 if ( exists $data{$d} ) {
657 $self->{'_delimiter'}->{$d} = $data{$d};
658 }
659 }
660 } else {
661 $self->throw("Args to helix_delimiters() should be in a hash reference");
662 }
663 }
664 return $self->{'_delimiter'};
665 }
666  
667 =head2 simple_meta
668  
669 Title : simple_meta
670 Usage : my $string = $parser->simple_meta($str);
671 Function: converts more complex WUSS meta format into simple bracket format
672 using symbols defined in structure_symbols()
673 Returns : converted string
674 Args : [required] string to convert
675 Note : This is a very simple conversion method to get simple bracketed
676 format from Infernal data. If the convert_meta() flag is set,
677 this is the method used to convert the strings.
678  
679 =cut
680  
681 sub simple_meta {
682 my ($self, $str) = @_;
683 $self->throw("No string arg sent!") if !$str;
684 my $structs = $self->structure_symbols();
685 my ($ls, $rs, $ss, $unk, $gap) = ($structs->{'5-prime'}, $structs->{'3-prime'},
686 $structs->{'single-strand'}, $structs->{'unknown'},
687 $structs->{'gap'});
688 $str =~ s{[\(\<\[\{]}{$ls}g;
689 $str =~ s{[\)\>\]\}]}{$rs}g;
690 $str =~ s{[:,_-]}{$ss}g;
691 $str =~ s{\.}{$gap}g;
692 # unknown not handled yet
693 return $str;
694 }
695  
696 ## private methods
697  
698 # this is a hack which guesses the format and sets the handler for parsing in
699 # an instance; it'll be taken out when infernal 1.0 is released
700  
701 sub _parse_latest {
702 my ($self) = @_;
703 my $seentop = 0;
704 local $/ = "\n";
705 my ($accession, $description) = ($self->query_accession, $self->query_description);
706 my ($maxscore, $mineval, $minpval);
707 $self->start_document();
708 my ($lasthit, $lastscore, $lasteval, $lastpval, $laststart, $lastend);
709 PARSER:
710 while (my $line = $self->_readline) {
711 next if $line =~ m{^\s+$};
712 # stats aren't parsed yet...
713 if ($line =~ m{^\#\s+cmsearch}xms) {
714 $seentop = 1;
715 $self->start_element({'Name' => 'Result'});
716 $self->element_hash({
717 'Infernal_program' => 'CMSEARCH'
718 });
719 }
720 elsif ($line =~ m{^\#\sINFERNAL\s+(\d+\.\d+)}xms) {
721 $self->element_hash({
722 'Infernal_version' => $1,
723 });
724 }
725 elsif ($line =~ m{^\#\scommand:.*?\s(\S+)$}xms) {
726 $self->element_hash({
727 'Infernal_db' => $1,
728 });
729 }
730 elsif ($line =~ m{^\#\s+dbsize\(Mb\):\s+(\d+\.\d+)}xms) {
731 # store absolute DB length
732 $self->element_hash({
733 'Infernal_db-let' => $1 * 1e6
734 });
735 }
736 elsif ($line =~ m{^CM(?:\s(\d+))?:\s*(\S+)}xms) {
737 # not sure, but it's possible single reports may contain multiple
738 # models; if so, they should be rolled over into a new ResultI
739 #print STDERR "ACC: $accession\nDESC: $description\n";
740 $self->element_hash({
741 'Infernal_query-def' => $2, # present in output now
742 'Infernal_query-acc' => $accession,
743 'Infernal_querydesc' => $description
744 });
745 }
746 elsif ($line =~ m{^>\s*(\S+)} ){
747 #$self->debug("Start Hit: Found hit:$1\n");
748 if ($self->in_element('hit')) {
749 $self->element_hash({'Hit_score' => $maxscore,
750 'Hit_bits' => $maxscore});
751 ($maxscore, $minpval, $mineval) = undef;
752 $self->end_element({'Name' => 'Hit'});
753 }
754 $lasthit = $1;
755 }
756 elsif ($line =~ m{
757 ^\sQuery\s=\s\d+\s-\s\d+,\s # Query start/end
758 Target\s=\s(\d+)\s-\s(\d+) # Target start/end
759 }xmso) {
760 # Query (model) start/end always the same, determined from
761 # the HSP length
762 ($laststart, $lastend) = ($1, $2);
763 #$self->debug("Found hit coords:$laststart - $lastend\n");
764 } elsif ($line =~ m{
765 ^\sScore\s=\s([\d\.]+),\s # Score = Bitscore (for now)
766 (?:E\s=\s([\d\.e-]+),\s # E-val optional
767 P\s=\s([\d\.e-]+),\s)? # P-val optional
768 GC\s= # GC not captured
769 }xmso
770 ) {
771 ($lastscore, $lasteval, $lastpval) = ($1, $2, $3);
772 #$self->debug(sprintf("Found hit data:Score:%s,Eval:%s,Pval:%s\n",$lastscore, $lasteval||'', $lastpval||''));
773 $maxscore ||= $lastscore;
774 if ($lasteval && $lastpval) {
775 $mineval ||= $lasteval;
776 $minpval ||= $lastpval;
777 $mineval = ($mineval > $lasteval) ? $lasteval :
778 $mineval;
779 $minpval = ($minpval > $lastpval) ? $lastpval :
780 $minpval;
781 }
782 $maxscore = ($maxscore < $lastscore) ? $lastscore :
783 $maxscore;
784 if (!$self->within_element('hit')) {
785 my ($gi, $acc, $ver) = $self->_get_seq_identifiers($lasthit);
786 $self->start_element({'Name' => 'Hit'});
787 $self->element_hash({
788 'Hit_id' => $lasthit,
789 'Hit_accession' => $ver ? "$acc.$ver" :
790 $acc ? $acc : $lasthit,
791 'Hit_gi' => $gi
792 });
793 }
794 if (!$self->in_element('hsp')) {
795 $self->start_element({'Name' => 'Hsp'});
796 }
797
798 # hsp is similar to older output
799 } elsif ($line =~ m{^(\s+)[<>\{\}\(\)\[\]:_,-\.]+}xms) { # start of HSP
800 $self->_pushback($line); # set up for loop
801 #$self->debug("Start HSP\n");
802 # what is length of the gap to the structure data?
803 my $offset = length($1);
804 my ($ct, $strln) = 0;
805 my $hsp;
806 HSP:
807 my %hsp_key = ('0' => 'meta',
808 '1' => 'query',
809 '2' => 'midline',
810 '3' => 'hit');
811 HSP:
812 while (defined ($line = $self->_readline)) {
813 chomp $line;
814 next if (!$line); # toss empty lines
815 # next if $line =~ m{^\s*$}; # toss empty lines
816 # it is possible to have homology lines consisting
817 # entirely of spaces if the subject has a large
818 # insertion where nothing matches the model
819
820 # exit loop if at end of file or upon next hit/HSP
821 if ($line =~ m{^\s{0,2}\S+}) {
822 $self->_pushback($line);
823 last HSP;
824 }
825 # iterate to keep track of each line (4 lines per hsp block)
826 my $iterator = $ct % 4;
827 # strlen set only with structure lines (proper length)
828 $strln = length($line) if $iterator == 0;
829 # only grab the data needed (hit start and stop in hit line above)
830
831 my $data = substr($line, $offset, $strln-$offset);
832 $hsp->{ $hsp_key{$iterator} } .= $data;
833
834 $ct++;
835 }
836
837 # query start, end are from the actual query length (entire hit is
838 # mapped to CM data, so all CM data is represented)
839 # works for now...
840 if ($self->in_element('hsp')) {
841 # In some cases with HSPs unaligned residues are present in
842 # the hit or query (Ex: '*[ 8]*' is 8 unaligned residues).
843 # This info needs to be passed on unmodifed to the HSP class
844 # and handled there as it is subjectively changed based on
845 # use.
846 my $strlen = 0;
847
848 # catch any insertions and add them into the actual length
849 while ($hsp->{'query'} =~ m{\*\[\s*(\d+)\s*\]\*}g) {
850 $strlen += $1;
851 }
852 # add on the actual residues
853 $strlen += $hsp->{'query'} =~ tr{A-Za-z}{A-Za-z};
854
855 my $metastr = ($self->convert_meta) ? ($self->simple_meta($hsp->{'meta'})) :
856 $hsp->{'meta'};
857 $self->element_hash(
858 {'Hsp_stranded' => 'HIT',
859 'Hsp_qseq' => $hsp->{'query'},
860 'Hsp_hseq' => $hsp->{'hit'},
861 'Hsp_midline' => $hsp->{'midline'},
862 'Hsp_structure' => $metastr,
863 'Hsp_query-from' => 1,
864 'Infernal_query-len' => $strlen,
865 'Hsp_query-to' => $strlen,
866 'Hsp_hit-from' => $laststart,
867 'Hsp_hit-to' => $lastend,
868 'Hsp_score' => $lastscore,
869 'Hsp_bit-score' => $lastscore,
870 });
871 $self->element_hash(
872 {'Hsp_evalue' => $lasteval,
873 'Hsp_pvalue' => $lastpval,
874 }) if ($lasteval && $lastpval);
875 $self->end_element({'Name' => 'Hsp'});
876 }
877 # result now ends with // and 'Fin'
878 } elsif ($line =~ m{^//}xms ) {
879 if ($self->within_element('result') && $seentop) {
880 if ($self->in_element('hit')) {
881 $self->element_hash({'Hit_score' => $maxscore,
882 'Hit_bits' => $maxscore});
883 # don't know where to put minpval yet
884 $self->element_hash({'Hit_signif' => $mineval}) if $mineval;
885 $self->element_hash({'Hit_p' => $minpval}) if $minpval;
886 $self->end_element({'Name' => 'Hit'});
887 }
888 last PARSER;
889 }
890 }
891 }
892 $self->within_element('hit') && $self->end_element( { 'Name' => 'Hit' } );
893 $self->end_element( { 'Name' => 'Result' } ) if $seentop;
894 return $self->end_document();
895 }
896  
897 # cmsearch 0.81 (pre-1.0)
898 sub _parse_pre {
899 my ($self) = @_;
900 my $seentop = 0;
901 local $/ = "\n";
902 my ($accession, $db, $algorithm, $description, $version) =
903 ($self->query_accession, $self->database, $self->algorithm,
904 $self->query_description, '0.81');
905 my ($maxscore, $mineval, $minpval);
906 $self->start_document();
907 my ($lasthit, $lastscore, $lasteval, $lastpval, $laststart, $lastend);
908 PARSER:
909 while (my $line = $self->_readline) {
910 next if $line =~ m{^\s+$};
911 # stats aren't parsed yet...
912 if ($line =~ m{CM\s\d+:\s*(\S+)}xms) {
913 #$self->debug("Start Result: Found model:$1\n");
914 if (!$self->within_element('result')) {
915 $seentop = 1;
916 $self->start_element({'Name' => 'Result'});
917 $self->element_hash({
918 'Infernal_program' => $algorithm,
919 'Infernal_query-def' => $1, # present in output now
920 'Infernal_query-acc' => $accession,
921 'Infernal_querydesc' => $description,
922 'Infernal_db' => $db
923 });
924 }
925 } elsif ($line =~ m{^>\s*(\S+)} ){
926 #$self->debug("Start Hit: Found hit:$1\n");
927 if ($self->in_element('hit')) {
928 $self->element_hash({'Hit_score' => $maxscore,
929 'Hit_bits' => $maxscore});
930 ($maxscore, $minpval, $mineval) = undef;
931 $self->end_element({'Name' => 'Hit'});
932 }
933 $lasthit = $1;
934 }
935 elsif ($line =~ m{
936 ^\sQuery\s=\s\d+\s-\s\d+,\s # Query start/end
937 Target\s=\s(\d+)\s-\s(\d+) # Target start/end
938 }xmso) {
939 # Query (model) start/end always the same, determined from
940 # the HSP length
941 ($laststart, $lastend) = ($1, $2);
942 #$self->debug("Found hit coords:$laststart - $lastend\n");
943 } elsif ($line =~ m{
944 ^\sScore\s=\s([\d\.]+),\s # Score = Bitscore (for now)
945 (?:E\s=\s([\d\.e-]+),\s # E-val optional
946 P\s=\s([\d\.e-]+),\s)? # P-val optional
947 GC\s= # GC not captured
948 }xmso
949 ) {
950 ($lastscore, $lasteval, $lastpval) = ($1, $2, $3);
951 #$self->debug(sprintf("Found hit data:Score:%s,Eval:%s,Pval:%s\n",$lastscore, $lasteval||'', $lastpval||''));
952 $maxscore ||= $lastscore;
953 if ($lasteval && $lastpval) {
954 $mineval ||= $lasteval;
955 $minpval ||= $lastpval;
956 $mineval = ($mineval > $lasteval) ? $lasteval :
957 $mineval;
958 $minpval = ($minpval > $lastpval) ? $lastpval :
959 $minpval;
960 }
961 $maxscore = ($maxscore < $lastscore) ? $lastscore :
962 $maxscore;
963 if (!$self->within_element('hit')) {
964 my ($gi, $acc, $ver) = $self->_get_seq_identifiers($lasthit);
965 $self->start_element({'Name' => 'Hit'});
966 $self->element_hash({
967 'Hit_id' => $lasthit,
968 'Hit_accession' => $ver ? "$acc.$ver" :
969 $acc ? $acc : $lasthit,
970 'Hit_gi' => $gi
971 });
972 }
973 if (!$self->in_element('hsp')) {
974 $self->start_element({'Name' => 'Hsp'});
975 }
976
977 # hsp is similar to older output
978 } elsif ($line =~ m{^(\s+)[<>\{\}\(\)\[\]:_,-\.]+}xms) { # start of HSP
979 $self->_pushback($line); # set up for loop
980 #$self->debug("Start HSP\n");
981 # what is length of the gap to the structure data?
982 my $offset = length($1);
983 my ($ct, $strln) = 0;
984 my $hsp;
985 HSP:
986 my %hsp_key = ('0' => 'meta',
987 '1' => 'query',
988 '2' => 'midline',
989 '3' => 'hit');
990 HSP:
991 while (defined ($line = $self->_readline)) {
992 chomp $line;
993 next if (!$line); # toss empty lines
994 # next if $line =~ m{^\s*$}; # toss empty lines
995 # it is possible to have homology lines consisting
996 # entirely of spaces if the subject has a large
997 # insertion where nothing matches the model
998
999 # exit loop if at end of file or upon next hit/HSP
1000 if ($line =~ m{^\s{0,2}\S+}) {
1001 $self->_pushback($line);
1002 last HSP;
1003 }
1004 # iterate to keep track of each line (4 lines per hsp block)
1005 my $iterator = $ct%4;
1006 # strlen set only with structure lines (proper length)
1007 $strln = length($line) if $iterator == 0;
1008 # only grab the data needed (hit start and stop in hit line above)
1009
1010 my $data = substr($line, $offset, $strln-$offset);
1011 $hsp->{ $hsp_key{$iterator} } .= $data;
1012
1013 $ct++;
1014 }
1015
1016 # query start, end are from the actual query length (entire hit is
1017 # mapped to CM data, so all CM data is represented)
1018 # works for now...
1019 if ($self->in_element('hsp')) {
1020 my $strlen = $hsp->{'query'} =~ tr{A-Za-z}{A-Za-z};
1021
1022 my $metastr;
1023 $metastr = ($self->convert_meta) ? ($self->simple_meta($hsp->{'meta'})) :
1024 ($hsp->{'meta'});
1025 $self->element_hash(
1026 {'Hsp_stranded' => 'HIT',
1027 'Hsp_qseq' => $hsp->{'query'},
1028 'Hsp_hseq' => $hsp->{'hit'},
1029 'Hsp_midline' => $hsp->{'midline'},
1030 'Hsp_structure' => $metastr,
1031 'Hsp_query-from' => 1,
1032 'Infernal_query-len' => $strlen,
1033 'Hsp_query-to' => $strlen,
1034 'Hsp_hit-from' => $laststart,
1035 'Hsp_hit-to' => $lastend,
1036 'Hsp_score' => $lastscore,
1037 'Hsp_bit-score' => $lastscore,
1038 });
1039 $self->element_hash(
1040 {'Hsp_evalue' => $lasteval,
1041 'Hsp_pvalue' => $lastpval,
1042 }) if ($lasteval && $lastpval);
1043 $self->end_element({'Name' => 'Hsp'});
1044 }
1045 # result now ends with // and 'Fin'
1046 } elsif ($line =~ m{^//}xms ) {
1047 if ($self->within_element('result') && $seentop) {
1048 $self->element(
1049 {'Name' => 'Infernal_version',
1050 'Data' => $version}
1051 );
1052 if ($self->in_element('hit')) {
1053 $self->element_hash({'Hit_score' => $maxscore,
1054 'Hit_bits' => $maxscore});
1055 # don't know where to put minpval yet
1056 $self->element_hash({'Hit_signif' => $mineval}) if $mineval;
1057 $self->end_element({'Name' => 'Hit'});
1058 }
1059 last PARSER;
1060 }
1061 }
1062 }
1063 $self->within_element('hit') && $self->end_element( { 'Name' => 'Hit' } );
1064 $self->end_element( { 'Name' => 'Result' } ) if $seentop;
1065 return $self->end_document();
1066 }
1067  
1068 # cmsearch 0.72 and below; will likely be dropped when Infernal 1.0 is released
1069 sub _parse_old {
1070 my ($self) = @_;
1071 my $seentop = 0;
1072 local $/ = "\n";
1073 my ($accession, $db, $algorithm, $model, $description, $version) =
1074 ($self->query_accession, $self->database, $self->algorithm,
1075 $self->model, $self->query_description, $self->version);
1076 my $maxscore;
1077 my $cutoff = $self->hsp_minscore;
1078 $self->start_document();
1079 local ($_);
1080 my $line;
1081 my ($lasthit, $lastscore, $laststart, $lastend);
1082 my $hitline;
1083 PARSER:
1084 while ( defined( $line = $self->_readline ) ) {
1085 next if $line =~ m{^\s+$};
1086 # bypass this for now...
1087 next if $line =~ m{^HMM\shit};
1088 # pre-0.81
1089 if ($line =~ m{^sequence:\s+(\S+)} ){
1090 if (!$self->within_element('result')) {
1091 $seentop = 1;
1092 $self->start_element({'Name' => 'Result'});
1093 $self->element_hash({
1094 'Infernal_program' => $algorithm,
1095 'Infernal_query-def' => $model,
1096 'Infernal_query-acc' => $accession,
1097 'Infernal_querydesc' => $description,
1098 'Infernal_db' => $db
1099 });
1100 }
1101 if ($self->in_element('hit')) {
1102 $self->element_hash({'Hit_score' => $maxscore,
1103 'Hit_bits' => $maxscore});
1104 $maxscore = undef;
1105 $self->end_element({'Name' => 'Hit'});
1106 }
1107 $lasthit = $1;
1108 } elsif ($line =~ m{^hit\s+\d+\s+:\s+(\d+)\s+(\d+)\s+(\d+\.\d+)\s+bits}xms) {
1109 ($laststart, $lastend, $lastscore) = ($1, $2, $3);
1110 $maxscore = $lastscore unless $maxscore;
1111 if ($lastscore > $cutoff) {
1112 if (!$self->within_element('hit')) {
1113 my ($gi, $acc, $ver) = $self->_get_seq_identifiers($lasthit);
1114 $self->start_element({'Name' => 'Hit'});
1115 $self->element_hash({
1116 'Hit_id' => $lasthit,
1117 'Hit_accession' => $ver ? "$acc.$ver" :
1118 $acc ? $acc : $lasthit,
1119 'Hit_gi' => $gi
1120 });
1121 }
1122 # necessary as infernal 0.71 has repeated hit line
1123 if (!$self->in_element('hsp')) {
1124 $self->start_element({'Name' => 'Hsp'});
1125 }
1126 $maxscore = ($maxscore < $lastscore) ? $lastscore :
1127 $maxscore;
1128 }
1129 } elsif ($line =~ m{^(\s+)[<>\{\}\(\)\[\]:_,-\.]+}xms) { # start of HSP
1130 $self->_pushback($line); # set up for loop
1131 # what is length of the gap to the structure data?
1132 my $offset = length($1);
1133 my ($ct, $strln) = 0;
1134 my $hsp;
1135 HSP:
1136 my %hsp_key = ('0' => 'meta',
1137 '1' => 'query',
1138 '2' => 'midline',
1139 '3' => 'hit');
1140 HSP:
1141 while ($line = $self->_readline) {
1142 next if $line =~ m{^\s*$}; # toss empty lines
1143 chomp $line;
1144 # exit loop if at end of file or upon next hit/HSP
1145 if (!defined($line) || $line =~ m{^\S+}) {
1146 $self->_pushback($line);
1147 last HSP;
1148 }
1149 # iterate to keep track of each line (4 lines per hsp block)
1150 my $iterator = $ct%4;
1151 # strlen set only with structure lines (proper length)
1152 $strln = length($line) if $iterator == 0;
1153 # only grab the data needed (hit start and stop in hit line above)
1154  
1155 my $data = substr($line, $offset, $strln-$offset);
1156 $hsp->{ $hsp_key{$iterator} } .= $data;
1157 $ct++;
1158 }
1159 # query start, end are from the actual query length (entire hit is
1160 # mapped to CM data, so all CM data is represented)
1161 # works for now...
1162 if ($self->in_element('hsp')) {
1163 my $strlen = $hsp->{'query'} =~ tr{A-Za-z}{A-Za-z};
1164
1165 my $metastr;
1166 # Ugh...these should be passed in a hash
1167 $metastr = ($self->convert_meta) ? ($self->simple_meta($hsp->{'meta'})) :
1168 ($hsp->{'meta'});
1169 $self->element_hash(
1170 {'Hsp_stranded' => 'HIT',
1171 'Hsp_qseq' => $hsp->{'query'},
1172 'Hsp_hseq' => $hsp->{'hit'},
1173 'Hsp_midline' => $hsp->{'midline'},
1174 'Hsp_structure' => $metastr,
1175 'Hsp_query-from' => 1,
1176 'Infernal_query-len' => $strlen,
1177 'Hsp_query-to' => $strlen,
1178 'Hsp_hit-from' => $laststart,
1179 'Hsp_hit-to' => $lastend,
1180 'Hsp_score' => $lastscore,
1181 'Hsp_bit-score' => $lastscore
1182 });
1183 $self->end_element({'Name' => 'Hsp'});
1184 }
1185 } elsif ($line =~ m{^memory}xms || $line =~ m{^CYK\smemory}xms ) {
1186 if ($self->within_element('result') && $seentop) {
1187 $self->element(
1188 {'Name' => 'Infernal_version',
1189 'Data' => $version}
1190 );
1191 if ($self->in_element('hit')) {
1192 $self->element_hash({'Hit_score' => $maxscore,
1193 'Hit_bits' => $maxscore});
1194 $self->end_element({'Name' => 'Hit'});
1195 }
1196 last PARSER;
1197 }
1198 }
1199 }
1200 $self->within_element('hit') && $self->end_element( { 'Name' => 'Hit' } );
1201 $self->end_element( { 'Name' => 'Result' } ) if $seentop;
1202 return $self->end_document();
1203 }
1204  
1205 1;