FAST/Bio/SearchIO/psl.pm untested

filter: Statementstmt 0.0% 0 / 251 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 36 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 367 SCAR 64.0
CC
24
Cov
0%
next_result
56.0
end_element
42.0
characters
30.0
CRAP 600.0
1 #
2 # BioPerl module for FAST::Bio::SearchIO::psl
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich <jason-at-bioperl-dot-org>
7 #
8 # Copyright Jason Stajich
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 FAST::Bio::SearchIO::psl - A parser for PSL output (UCSC)
17  
18 =head1 SYNOPSIS
19  
20 use FAST::Bio::SearchIO;
21 my $parser = FAST::Bio::SearchIO->new(-file => 'file.psl',
22 -format => 'psl');
23 while( my $result = $parser->next_result ) {
24 }
25  
26 =head1 DESCRIPTION
27  
28 This is a SearchIO driver for PSL format.
29 PSL format is documented here:
30 http://genome.ucsc.edu/goldenPath/help/customTrack.html#PSL
31  
32 By default it assumes PSL output came from BLAT you can override that
33 by specifying -program_name =E<gt> 'BLASTZ' when initializing the
34 SearchIO object.
35  
36 =head1 FEEDBACK
37  
38 =head2 Mailing Lists
39  
40 User feedback is an integral part of the evolution of this and other
41 Bioperl modules. Send your comments and suggestions preferably to
42 the Bioperl mailing list. Your participation is much appreciated.
43  
44 bioperl-l@bioperl.org - General discussion
45 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
46  
47 =head2 Support
48  
49 Please direct usage questions or support issues to the mailing list:
50  
51 I<bioperl-l@bioperl.org>
52  
53 rather than to the module maintainer directly. Many experienced and
54 reponsive experts will be able look at the problem and quickly
55 address it. Please include a thorough description of the problem
56 with code and data examples if at all possible.
57  
58 =head2 Reporting Bugs
59  
60 Report bugs to the Bioperl bug tracking system to help us keep track
61 of the bugs and their resolution. Bug reports can be submitted via the
62 web:
63  
64 https://redmine.open-bio.org/projects/bioperl/
65  
66 =head1 AUTHOR - Jason Stajich
67  
68 Email jason-at-bioperl-dot-org
69  
70 =head1 APPENDIX
71  
72 The rest of the documentation details each of the object methods.
73 Internal methods are usually preceded with a _
74  
75 =cut
76  
77 # Let the code begin...
78  
79 package FAST::Bio::SearchIO::psl;
80 use vars qw(%MAPPING %MODEMAP $DEFAULT_WRITER_CLASS $DefaultProgramName);
81  
82 use strict;
83 use FAST::Bio::Search::HSP::HSPFactory;
84 use FAST::Bio::Search::Hit::HitFactory;
85 use FAST::Bio::Search::Result::ResultFactory;
86  
87 $DefaultProgramName = 'BLAT';
88 $DEFAULT_WRITER_CLASS = 'FAST::Bio::SearchIO::Writer::HitTableWriter';
89  
90 # mapping of terms to Bioperl hash keys
91 %MODEMAP = (
92 'PSLOutput' => 'result',
93 'Result' => 'result',
94 'Hit' => 'hit',
95 'Hsp' => 'hsp'
96 );
97  
98 %MAPPING = (
99 'Hsp_bit-score' => 'HSP-bits',
100 'Hsp_score' => 'HSP-score',
101 'Hsp_evalue' => 'HSP-evalue',
102 'Hsp_query-from' => 'HSP-query_start',
103 'Hsp_query-to' => 'HSP-query_end',
104 'Hsp_hit-from' => 'HSP-hit_start',
105 'Hsp_hit-to' => 'HSP-hit_end',
106 'Hsp_positive' => 'HSP-conserved',
107 'Hsp_identity' => 'HSP-identical',
108 'Hsp_mismatches' => 'HSP-mismatches',
109 'Hsp_qgapblocks' => 'HSP-query_gapblocks',
110 'Hsp_hgapblocks' => 'HSP-hit_gapblocks',
111 'Hsp_gaps' => 'HSP-hsp_gaps',
112 'Hsp_hitgaps' => 'HSP-hit_gaps',
113 'Hsp_querygaps' => 'HSP-query_gaps',
114 'Hsp_align-len' => 'HSP-hsp_length',
115 'Hsp_query-frame' => 'HSP-query_frame',
116 'Hsp_hit-frame' => 'HSP-hit_frame',
117  
118 'Hit_id' => 'HIT-name',
119 'Hit_len' => 'HIT-length',
120 'Hit_accession' => 'HIT-accession',
121 'Hit_def' => 'HIT-description',
122 'Hit_signif' => 'HIT-significance',
123 'Hit_score' => 'HIT-score',
124 'Hit_bits' => 'HIT-bits',
125  
126 'PSLOutput_program' => 'RESULT-algorithm_name',
127 'PSLOutput_version' => 'RESULT-algorithm_version',
128 'PSLOutput_query-def' => 'RESULT-query_name',
129 'PSLOutput_query-len' => 'RESULT-query_length',
130 'PSLOutput_query-acc' => 'RESULT-query_accession',
131 'PSLOutput_querydesc' => 'RESULT-query_description',
132 'PSLOutput_db' => 'RESULT-database_name',
133 'PSLOutput_db-len' => 'RESULT-database_entries',
134 'PSLOutput_db-let' => 'RESULT-database_letters',
135 );
136  
137 use base qw(FAST::Bio::SearchIO);
138  
139 =head2 new
140  
141 Title : new
142 Usage : my $obj = FAST::Bio::SearchIO::psl->new();
143 Function: Builds a new FAST::Bio::SearchIO::psl object
144 Returns : an instance of FAST::Bio::SearchIO::psl
145 Args :
146  
147  
148 =cut
149  
150 sub _initialize {
151 my ( $self, @args ) = @_;
152 $self->SUPER::_initialize(@args);
153 my ($pname) = $self->_rearrange( [qw(PROGRAM_NAME)], @args );
154 $self->program_name( $pname || $DefaultProgramName );
155 $self->_eventHandler->register_factory(
156 'result',
157 FAST::Bio::Search::Result::ResultFactory->new(
158 -type => 'FAST::Bio::Search::Result::GenericResult'
159 )
160 );
161  
162 $self->_eventHandler->register_factory(
163 'hit',
164 FAST::Bio::Search::Hit::HitFactory->new(
165 -type => 'FAST::Bio::Search::Hit::GenericHit'
166 )
167 );
168 $self->_eventHandler->register_factory(
169 'hsp',
170 FAST::Bio::Search::HSP::HSPFactory->new(
171 -type => 'FAST::Bio::Search::HSP::PSLHSP'
172 )
173 );
174 }
175  
176 =head2 next_result
177  
178 Title : next_result
179 Usage : my $result = $parser->next_result
180 Function: Parse the next result from the data stream
181 Returns : L<FAST::Bio::Search::Result::ResultI> or undef if no more results
182 Args : none
183  
184 =cut
185  
186 sub next_result {
187 my ($self) = @_;
188 my ( $lastquery, $lasthit );
189 local $/ = "\n";
190 local $_;
191  
192 # skip over any header lines
193 while( defined($_ = $self->_readline) and ! /^\d+\s+\d+\s+/ ) {}
194 $self->_pushback($_);
195  
196 # now start the main parsing loop
197 while ( defined( $_ = $self->_readline ) ) {
198 my (
199 $matches, $mismatches, $rep_matches, $n_count,
200 $q_num_insert, $q_base_insert, $t_num_insert, $t_base_insert,
201 $strand, $q_name, $q_length, $q_start,
202 $q_end, $t_name, $t_length, $t_start,
203 $t_end, $block_count, $block_sizes, $q_starts,
204 $t_starts
205 ) = split;
206  
207 $q_length > 0 or $self->throw("parse error, invalid query length '$q_length'");
208 my $score = sprintf( "%.2f", 100 * ( $matches + $mismatches + $rep_matches ) / $q_length );
209  
210 # this is overall percent identity...
211 my $match_total = $matches + $mismatches + $rep_matches;
212 $match_total > 0
213 or $self->throw("parse error, matches + mismatches + rep_matches must be > 0!");
214 my $percent_id = sprintf("%.2f", 100 * ( $matches + $rep_matches ) / $match_total );
215  
216 # Remember Jim's code is 0 based
217 if ( defined $lastquery
218 && $lastquery ne $q_name )
219 {
220 $self->end_element( { 'Name' => 'Hit' } );
221 $self->end_element( { 'Name' => 'PSLOutput' } );
222 $self->_pushback($_);
223 return $self->end_document;
224 }
225 elsif ( !defined $lastquery ) {
226 $self->{'_result_count'}++;
227 $self->start_element( { 'Name' => 'PSLOutput' } );
228 $self->element(
229 {
230 'Name' => 'PSLOutput_program',
231 'Data' => $self->program_name
232 }
233 );
234 $self->element(
235 {
236 'Name' => 'PSLOutput_query-def',
237 'Data' => $q_name
238 }
239 );
240 $self->element(
241 {
242 'Name' => 'PSLOutput_query-len',
243 'Data' => $q_length
244 }
245 );
246 $self->start_element( { 'Name' => 'Hit' } );
247 $self->element(
248 {
249 'Name' => 'Hit_id',
250 'Data' => $t_name
251 }
252 );
253 $self->element(
254 {
255 'Name' => 'Hit_len',
256 'Data' => $t_length
257 }
258 );
259 $self->element(
260 {
261 'Name' => 'Hit_score',
262 'Data' => $score
263 }
264 );
265 }
266 elsif ( $lasthit ne $t_name ) {
267 $self->end_element( { 'Name' => 'Hit' } );
268 $self->start_element( { 'Name' => 'Hit' } );
269 $self->element(
270 {
271 'Name' => 'Hit_id',
272 'Data' => $t_name
273 }
274 );
275 $self->element(
276 {
277 'Name' => 'Hit_len',
278 'Data' => $t_length
279 }
280 );
281 $self->element(
282 {
283 'Name' => 'Hit_score',
284 'Data' => $score
285 }
286 );
287 }
288  
289 my $identical = $matches + $rep_matches;
290 $self->start_element( { 'Name' => 'Hsp' } );
291 $self->element(
292 {
293 'Name' => 'Hsp_score',
294 'Data' => $score
295 }
296 );
297 $self->element(
298 {
299 'Name' => 'Hsp_identity',
300 'Data' => $identical
301 }
302 );
303 $self->element(
304 {
305 'Name' => 'Hsp_positive',
306 'Data' => $identical
307 }
308 );
309 $self->element(
310 {
311 'Name' => 'Hsp_mismatches',
312 'Data' => $mismatches
313 }
314 );
315 $self->element(
316 {
317 'Name' => 'Hsp_gaps',
318 'Data' => $q_base_insert + $t_base_insert
319 }
320 );
321  
322 # query gaps are the number of target inserts and vice-versa
323 $self->element(
324 {
325 'Name' => 'Hsp_querygaps',
326 'Data' => $t_base_insert
327 }
328 );
329 $self->element(
330 {
331 'Name' => 'Hsp_hitgaps',
332 'Data' => $q_base_insert
333 }
334 );
335 if ( $strand eq '+' ) {
336 $self->element(
337 {
338 'Name' => 'Hsp_query-from',
339 'Data' => $q_start + 1
340 }
341 );
342 $self->element(
343 {
344 'Name' => 'Hsp_query-to',
345 'Data' => $q_end
346 }
347 );
348 }
349 else {
350 $self->element(
351 {
352 'Name' => 'Hsp_query-to',
353 'Data' => $q_start + 1
354 }
355 );
356 $self->element(
357 {
358 'Name' => 'Hsp_query-from',
359 'Data' => $q_end
360 }
361 );
362 }
363 my $hsplen =
364 $q_base_insert +
365 $t_base_insert +
366 abs( $t_end - $t_start ) +
367 abs( $q_end - $q_start );
368 $self->element(
369 {
370 'Name' => 'Hsp_hit-from',
371 'Data' => $t_start + 1
372 }
373 );
374 $self->element(
375 {
376 'Name' => 'Hsp_hit-to',
377 'Data' => $t_end
378 }
379 );
380 $self->element(
381 {
382 'Name' => 'Hsp_align-len',
383 'Data' => $hsplen
384 }
385 );
386  
387 # cleanup trailing commas in some output
388 $block_sizes =~ s/\,$//;
389 $q_starts =~ s/\,$//;
390 $t_starts =~ s/\,$//;
391 my @blocksizes = split( /,/, $block_sizes ); # block sizes
392 my @qstarts = split( /,/, $q_starts ); # starting position of each block
393 # in query
394 my @tstarts = split( /,/, $t_starts ); # starting position of each block
395 # in target
396 my ( @qgapblocks, @hgapblocks );
397  
398 for ( my $i = 0 ; $i < $block_count ; $i++ ) {
399 if ( $strand eq '+' ) {
400 push @qgapblocks, [ $qstarts[$i] + 1, $blocksizes[$i] ];
401 }
402 else {
403 push @qgapblocks, [ $q_length - $qstarts[$i], $blocksizes[$i] ];
404 }
405 push @hgapblocks, [ $tstarts[$i] + 1, $blocksizes[$i] ];
406 }
407 $self->element(
408 {
409 'Name' => 'Hsp_qgapblocks',
410 'Data' => \@qgapblocks
411 }
412 );
413 $self->element(
414 {
415 'Name' => 'Hsp_hgapblocks',
416 'Data' => \@hgapblocks
417 }
418 );
419 $self->end_element( { 'Name' => 'Hsp' } );
420 $lastquery = $q_name;
421 $lasthit = $t_name;
422 }
423 if ( defined $lasthit || defined $lastquery ) {
424 $self->end_element( { 'Name' => 'Hit' } );
425 $self->end_element( { 'Name' => 'Result' } );
426 return $self->end_document;
427 }
428 }
429  
430 =head2 start_element
431  
432 Title : start_element
433 Usage : $eventgenerator->start_element
434 Function: Handles a start element event
435 Returns : none
436 Args : hashref with at least 2 keys 'Data' and 'Name'
437  
438  
439 =cut
440  
441 sub start_element {
442 my ( $self, $data ) = @_;
443  
444 # we currently don't care about attributes
445 my $nm = $data->{'Name'};
446 if ( my $type = $MODEMAP{$nm} ) {
447 $self->_mode($type);
448 if ( $self->_eventHandler->will_handle($type) ) {
449 my $func = 'start_'.lc $type;
450 $self->_eventHandler->$func( $data->{'Attributes'} );
451 }
452 unshift @{ $self->{'_elements'} }, $type;
453 }
454 if ( $nm eq 'PSLOutput' ) {
455 $self->{'_values'} = {};
456 $self->{'_result'} = undef;
457 $self->{'_mode'} = '';
458 }
459  
460 }
461  
462 =head2 end_element
463  
464 Title : end_element
465 Usage : $eventgenerator->end_element
466 Function: Handles an end element event
467 Returns : return value from the associated end_$type event handler
468 Args : hashref with at least 2 keys 'Data' and 'Name'
469  
470  
471 =cut
472  
473 sub end_element {
474 my ( $self, $data ) = @_;
475 my $nm = $data->{'Name'};
476 my $rc;
477  
478 # Hsp are sort of weird, in that they end when another
479 # object begins so have to detect this in end_element for now
480  
481 if ( my $type = $MODEMAP{$nm} ) {
482 if ( $self->_eventHandler->will_handle($type) ) {
483 my $func = 'end_'.lc $type;
484 $rc = $self->_eventHandler->$func( $self->{'_reporttype'},
485 $self->{'_values'} );
486 }
487 shift @{ $self->{'_elements'} };
488  
489 }
490 elsif ( $MAPPING{$nm} ) {
491 if ( ref( $MAPPING{$nm} ) =~ /hash/i ) {
492 my $key = ( keys %{ $MAPPING{$nm} } )[0];
493 $self->{'_values'}->{$key}->{ $MAPPING{$nm}->{$key} } =
494 $self->{'_last_data'};
495 }
496 else {
497 $self->{'_values'}->{ $MAPPING{$nm} } = $self->{'_last_data'};
498 }
499 }
500 else {
501 $self->warn(
502 __PACKAGE__ . "::end_element: unknown nm '$nm', ignoring\n" );
503 }
504 $self->{'_last_data'} = ''; # remove read data if we are at
505 # end of an element
506 $self->{'_result'} = $rc
507 if ( defined $nm
508 && defined $MODEMAP{$nm}
509 && $MODEMAP{$nm} eq 'result' );
510 return $rc;
511  
512 }
513  
514 =head2 element
515  
516 Title : element
517 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
518 Function: Convience method that calls start_element, characters, end_element
519 Returns : none
520 Args : Hash ref with the keys 'Name' and 'Data'
521  
522  
523 =cut
524  
525 sub element {
526 my ( $self, $data ) = @_;
527 $self->start_element($data);
528 $self->characters($data);
529 $self->end_element($data);
530 }
531  
532 =head2 characters
533  
534 Title : characters
535 Usage : $eventgenerator->characters($str)
536 Function: Send a character events
537 Returns : none
538 Args : string
539  
540  
541 =cut
542  
543 sub characters {
544 my ( $self, $data ) = @_;
545  
546 return unless ( defined $data->{'Data'} );
547 if ( $data->{'Data'} =~ /^\s+$/ ) {
548 return unless $data->{'Name'} =~ /Hsp\_(midline|qseq|hseq)/;
549 }
550  
551 if ( $self->in_element('hsp')
552 && $data->{'Name'} =~ /Hsp\_(qseq|hseq|midline)/ )
553 {
554  
555 $self->{'_last_hspdata'}->{ $data->{'Name'} } .= $data->{'Data'};
556 }
557  
558 $self->{'_last_data'} = $data->{'Data'};
559 }
560  
561 =head2 _mode
562  
563 Title : _mode
564 Usage : $obj->_mode($newval)
565 Function:
566 Example :
567 Returns : value of _mode
568 Args : newvalue (optional)
569  
570  
571 =cut
572  
573 sub _mode {
574 my ( $self, $value ) = @_;
575 if ( defined $value ) {
576 $self->{'_mode'} = $value;
577 }
578 return $self->{'_mode'};
579 }
580  
581 =head2 within_element
582  
583 Title : within_element
584 Usage : if( $eventgenerator->within_element($element) ) {}
585 Function: Test if we are within a particular element
586 This is different than 'in' because within can be tested
587 for a whole block.
588 Returns : boolean
589 Args : string element name
590  
591  
592 =cut
593  
594 sub within_element {
595 my ( $self, $name ) = @_;
596 return 0
597 if (!defined $name && !defined $self->{'_elements'}
598 || scalar @{ $self->{'_elements'} } == 0 );
599 foreach ( @{ $self->{'_elements'} } ) {
600 if ( $_ eq $name ) {
601 return 1;
602 }
603 }
604 return 0;
605 }
606  
607 =head2 in_element
608  
609 Title : in_element
610 Usage : if( $eventgenerator->in_element($element) ) {}
611 Function: Test if we are in a particular element
612 This is different than 'in' because within can be tested
613 for a whole block.
614 Returns : boolean
615 Args : string element name
616  
617  
618 =cut
619  
620 sub in_element {
621 my ( $self, $name ) = @_;
622 return 0 if !defined $self->{'_elements'}->[0];
623 return ( $self->{'_elements'}->[0] eq $name );
624 }
625  
626 =head2 start_document
627  
628 Title : start_document
629 Usage : $eventgenerator->start_document
630 Function: Handles a start document event
631 Returns : none
632 Args : none
633  
634  
635 =cut
636  
637 sub start_document {
638 my ($self) = @_;
639 $self->{'_lasttype'} = '';
640 $self->{'_values'} = {};
641 $self->{'_result'} = undef;
642 $self->{'_mode'} = '';
643 $self->{'_elements'} = [];
644 }
645  
646 =head2 end_document
647  
648 Title : end_document
649 Usage : $eventgenerator->end_document
650 Function: Handles an end document event
651 Returns : FAST::Bio::Search::Result::ResultI object
652 Args : none
653  
654  
655 =cut
656  
657 sub end_document {
658 my ( $self, @args ) = @_;
659 return $self->{'_result'};
660 }
661  
662 =head2 result_count
663  
664 Title : result_count
665 Usage : my $count = $searchio->result_count
666 Function: Returns the number of results we have processed
667 Returns : integer
668 Args : none
669  
670  
671 =cut
672  
673 sub result_count {
674 my $self = shift;
675 return $self->{'_result_count'};
676 }
677  
678 sub report_count { shift->result_count }
679  
680 =head2 program_name
681  
682 Title : program_name
683 Usage : $obj->program_name($newval)
684 Function: Get/Set the program name
685 Returns : value of program_name (a scalar)
686 Args : on set, new value (a scalar or undef, optional)
687  
688  
689 =cut
690  
691 sub program_name {
692 my $self = shift;
693  
694 $self->{'program_name'} = shift if @_;
695 return $self->{'program_name'} || $DefaultProgramName;
696 }
697  
698 1;