2 # BioPerl module for Bio::SearchIO::waba
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Jason Stajich <jason@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::waba - SearchIO parser for Jim Kent WABA program
21 # do not use this object directly, rather through Bio::SearchIO
24 my $in = Bio::SearchIO->new(-format => 'waba',
25 -file => 'output.wab');
26 while( my $result = $in->next_result ) {
27 while( my $hit = $result->next_hit ) {
28 while( my $hsp = $result->next_hsp ) {
36 This parser will process the waba output (NOT the human readable format).
42 User feedback is an integral part of the evolution of this and other
43 Bioperl modules. Send your comments and suggestions preferably to
44 the Bioperl mailing list. Your participation is much appreciated.
46 bioperl-l@bioperl.org - General discussion
47 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
51 Please direct usage questions or support issues to the mailing list:
53 I<bioperl-l@bioperl.org>
55 rather than to the module maintainer directly. Many experienced and
56 reponsive experts will be able look at the problem and quickly
57 address it. Please include a thorough description of the problem
58 with code and data examples if at all possible.
62 Report bugs to the Bioperl bug tracking system to help us keep track
63 of the bugs and their resolution. Bug reports can be submitted via the
66 https://redmine.open-bio.org/projects/bioperl/
68 =head1 AUTHOR - Jason Stajich
70 Email jason-at-bioperl.org
74 The rest of the documentation details each of the object methods.
75 Internal methods are usually preceded with a _
80 # Let the code begin...
83 package Bio
::SearchIO
::waba
;
84 use vars
qw(%MODEMAP %MAPPING @STATES);
87 # Object preamble - inherits from Bio::Root::Root
89 use Bio::Search::Result::ResultFactory;
90 use Bio::Search::HSP::HSPFactory;
95 # mapping of NCBI Blast terms to Bioperl hash keys
96 %MODEMAP = ('WABAOutput' => 'result',
100 @STATES = qw(Hsp_qseq Hsp_hseq Hsp_stateseq);
103 'Hsp_query-from'=> 'HSP-query_start',
104 'Hsp_query-to' => 'HSP-query_end',
105 'Hsp_hit-from' => 'HSP-hit_start',
106 'Hsp_hit-to' => 'HSP-hit_end',
107 'Hsp_qseq' => 'HSP-query_seq',
108 'Hsp_hseq' => 'HSP-hit_seq',
109 'Hsp_midline' => 'HSP-homology_seq',
110 'Hsp_stateseq' => 'HSP-hmmstate_seq',
111 'Hsp_align-len' => 'HSP-hsp_length',
113 'Hit_id' => 'HIT-name',
114 'Hit_accession' => 'HIT-accession',
116 'WABAOutput_program' => 'RESULT-algorithm_name',
117 'WABAOutput_version' => 'RESULT-algorithm_version',
118 'WABAOutput_query-def'=> 'RESULT-query_name',
119 'WABAOutput_query-db' => 'RESULT-query_database',
120 'WABAOutput_db' => 'RESULT-database_name',
125 use base
qw(Bio::SearchIO);
130 Usage : my $obj = Bio::SearchIO::waba->new();
131 Function: Builds a new Bio::SearchIO::waba object
132 Returns : Bio::SearchIO::waba
133 Args : see Bio::SearchIO
138 my ($self,@args) = @_;
139 $self->SUPER::_initialize
(@args);
140 $self->_eventHandler->register_factory('result', Bio
::Search
::Result
::ResultFactory
->new(-type
=> 'Bio::Search::Result::WABAResult'));
142 $self->_eventHandler->register_factory('hsp', Bio
::Search
::HSP
::HSPFactory
->new(-type
=> 'Bio::Search::HSP::WABAHSP'));
149 Usage : my $hit = $searchio->next_result;
150 Function: Returns the next Result from a search
151 Returns : Bio::Search::Result::ResultI object
161 my ($curquery,$curhit);
163 $self->start_document();
165 while( defined ($_ = $self->_readline )) {
168 my ($qid, $qhspid,$qpercent, $junk,
169 $alnlen,$qdb,$qacc,$qstart,$qend,$qstrand,
170 $hitdb,$hacc,$hstart,$hend,
172 ( /^(\S
+)\
.(\S
+)\s
+align\s
+ # get the queryid
173 (\d
+(\
.\d
+)?
)\
%\s
+ # get the percentage
174 of\s
+(\d
+)\s
+ # get the length of the alignment
175 (\S
+)\s
+ # this is the query database
176 (\S
+):(\
-?\d
+)\
-(\
-?\d
+) # The accession:start-end for query
177 \s
+([\
-\
+]) # query strand
179 (\S
+):(\
-?\d
+)\
-(\
-?\d
+) # The accession:start-end for hit
180 \s
+([\
-\
+])\s
*$ # hit strand
183 # Curses. Jim's code is 0 based, the following is to readjust
184 if( $hstart < 0 ) { $hstart *= -1}
185 if( $hend < 0 ) { $hend *= -1}
186 if( $qstart < 0 ) { $qstart *= -1}
187 if( $qend < 0 ) { $qend *= -1}
188 $hstart++; $hend++; $qstart++; $qend++;
189 if( ! defined $alnlen ) {
190 $self->warn("Unable to parse the rest of the WABA alignment info for: '$_'");
193 $self->{'_reporttype'} = 'WABA'; # hardcoded - only
194 # one type of WABA AFAIK
195 if( defined $curquery &&
196 $curquery ne $qid ) {
197 $self->end_element({'Name' => 'Hit'});
198 $self->_pushback($_);
199 $self->end_element({'Name' => 'WABAOutput'});
200 return $self->end_document();
203 if( defined $curhit &&
205 # slight duplication here -- keep these in SYNC
206 $self->end_element({'Name' => 'Hit'});
207 $self->start_element({'Name' => 'Hit'});
208 $self->element({'Name' => 'Hit_id',
210 $self->element({'Name' => 'Hit_accession',
213 } elsif ( ! defined $curquery ) {
214 $self->start_element({'Name' => 'WABAOutput'});
215 $self->{'_result_count'}++;
216 $self->element({'Name' => 'WABAOutput_query-def',
218 $self->element({'Name' => 'WABAOutput_program',
220 $self->element({'Name' => 'WABAOutput_query-db',
222 $self->element({'Name' => 'WABAOutput_db',
225 # slight duplication here -- keep these N'SYNC ;-)
226 $self->start_element({'Name' => 'Hit'});
227 $self->element({'Name' => 'Hit_id',
229 $self->element({'Name' => 'Hit_accession',
234 # strand is inferred by start,end values
235 # in the Result Builder
236 if( $qstrand eq '-' ) {
237 ($qstart,$qend) = ($qend,$qstart);
239 if( $hstrand eq '-' ) {
240 ($hstart,$hend) = ($hend,$hstart);
243 $self->start_element({'Name' => 'Hsp'});
244 $self->element({'Name' => 'Hsp_query-from',
246 $self->element({'Name' => 'Hsp_query-to',
248 $self->element({'Name' => 'Hsp_hit-from',
250 $self->element({'Name' => 'Hsp_hit-to',
252 $self->element({'Name' => 'Hsp_align-len',
258 } elsif( ! defined $curquery ) {
259 $self->warn("skipping because no Hit begin line was recognized\n$_") if( $_ !~ /^\s+$/ );
263 $self->element({'Name' => $STATES[$state++],
265 if( $state >= scalar @STATES ) {
267 $self->end_element({'Name' => 'Hsp'});
271 if( defined $curquery ) {
272 $self->end_element({'Name' => 'Hit'});
273 $self->end_element({'Name' => 'WABAOutput'});
274 return $self->end_document();
281 Title : start_element
282 Usage : $eventgenerator->start_element
283 Function: Handles a start element event
285 Args : hashref with at least 2 keys 'Data' and 'Name'
291 my ($self,$data) = @_;
292 # we currently don't care about attributes
293 my $nm = $data->{'Name'};
294 if( my $type = $MODEMAP{$nm} ) {
296 if( $self->_eventHandler->will_handle($type) ) {
297 my $func = sprintf("start_%s",lc $type);
298 $self->_eventHandler->$func($data->{'Attributes'});
300 unshift @
{$self->{'_elements'}}, $type;
302 if($nm eq 'WABAOutput') {
303 $self->{'_values'} = {};
304 $self->{'_result'}= undef;
305 $self->{'_mode'} = '';
312 Title : start_element
313 Usage : $eventgenerator->end_element
314 Function: Handles an end element event
316 Args : hashref with at least 2 keys 'Data' and 'Name'
322 my ($self,$data) = @_;
323 my $nm = $data->{'Name'};
325 # Hsp are sort of weird, in that they end when another
326 # object begins so have to detect this in end_element for now
328 foreach ( qw(Hsp_qseq Hsp_midline Hsp_hseq) ) {
329 $self->element({'Name' => $_,
330 'Data' => $self->{'_last_hspdata'}->{$_}});
332 $self->{'_last_hspdata'} = {}
335 if( my $type = $MODEMAP{$nm} ) {
336 if( $self->_eventHandler->will_handle($type) ) {
337 my $func = sprintf("end_%s",lc $type);
338 $rc = $self->_eventHandler->$func($self->{'_reporttype'},
341 shift @
{$self->{'_elements'}};
343 } elsif( $MAPPING{$nm} ) {
344 if ( ref($MAPPING{$nm}) =~ /hash/i ) {
345 my $key = (keys %{$MAPPING{$nm}})[0];
346 $self->{'_values'}->{$key}->{$MAPPING{$nm}->{$key}} = $self->{'_last_data'};
348 $self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
351 $self->warn( "unknown nm $nm ignoring\n");
353 $self->{'_last_data'} = ''; # remove read data if we are at
355 $self->{'_result'} = $rc if( $nm eq 'WABAOutput' );
363 Usage : $eventhandler->element({'Name' => $name, 'Data' => $str});
364 Function: Convience method that calls start_element, characters, end_element
366 Args : Hash ref with the keys 'Name' and 'Data'
372 my ($self,$data) = @_;
373 $self->start_element($data);
374 $self->characters($data);
375 $self->end_element($data);
382 Usage : $eventgenerator->characters($str)
383 Function: Send a character events
391 my ($self,$data) = @_;
393 return unless ( defined $data->{'Data'} );
394 if( $data->{'Data'} =~ /^\s+$/ ) {
395 return unless $data->{'Name'} =~ /Hsp\_(midline|qseq|hseq)/;
398 if( $self->in_element('hsp') &&
399 $data->{'Name'} =~ /Hsp\_(qseq|hseq|midline)/ ) {
401 $self->{'_last_hspdata'}->{$data->{'Name'}} .= $data->{'Data'};
404 $self->{'_last_data'} = $data->{'Data'};
410 Usage : $obj->_mode($newval)
413 Returns : value of _mode
414 Args : newvalue (optional)
420 my ($self,$value) = @_;
421 if( defined $value) {
422 $self->{'_mode'} = $value;
424 return $self->{'_mode'};
427 =head2 within_element
429 Title : within_element
430 Usage : if( $eventgenerator->within_element($element) ) {}
431 Function: Test if we are within a particular element
432 This is different than 'in' because within can be tested
435 Args : string element name
441 my ($self,$name) = @_;
442 return 0 if ( ! defined $name &&
443 ! defined $self->{'_elements'} ||
444 scalar @
{$self->{'_elements'}} == 0) ;
445 foreach ( @
{$self->{'_elements'}} ) {
456 Usage : if( $eventgenerator->in_element($element) ) {}
457 Function: Test if we are in a particular element
458 This is different than 'in' because within can be tested
461 Args : string element name
467 my ($self,$name) = @_;
468 return 0 if ! defined $self->{'_elements'}->[0];
469 return ( $self->{'_elements'}->[0] eq $name)
473 =head2 start_document
475 Title : start_document
476 Usage : $eventgenerator->start_document
477 Function: Handles a start document event
486 $self->{'_lasttype'} = '';
487 $self->{'_values'} = {};
488 $self->{'_result'}= undef;
489 $self->{'_mode'} = '';
490 $self->{'_elements'} = [];
497 Usage : $eventgenerator->end_document
498 Function: Handles an end document event
499 Returns : Bio::Search::Result::ResultI object
506 my ($self,@args) = @_;
507 return $self->{'_result'};
513 Usage : my $count = $searchio->result_count
514 Function: Returns the number of results we have processed
523 return $self->{'_result_count'};
526 sub report_count
{ shift->result_count }