FAST/Bio/SearchIO/XML/BlastHandler.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 112 CC 14 SCAR 40.7
CC
14
Cov
0%
end_element
110.0
start_element
12.0
characters
6.0
CRAP 58.3
1 #
2 # BioPerl module for FAST::Bio::SearchIO::XML::BlastHandler
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich, Chris Fields
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::XML::BlastHandler - XML Handler for NCBI Blast XML parsing.
17  
18 =head1 SYNOPSIS
19  
20 # This is not to be used directly.
21  
22 =head1 DESCRIPTION
23  
24 This is the XML handler for BLAST XML parsing. Currently it passes elements off
25 to the event handler, which is ultimately responsible for FAST::Bio::Search object
26 generation.
27  
28 This was recently split off from the original code for FAST::Bio::SearchIO::blastxml
29 primarily for maintenance purposes.
30  
31 =head1 DEPENDENCIES
32  
33 In addition to parts of the FAST::Bio:: hierarchy, this module uses:
34  
35 XML::SAX::Base
36  
37 which comes with the XML::SAX distribution.
38  
39 =head1 FEEDBACK
40  
41 =head2 Mailing Lists
42  
43 User feedback is an integral part of the evolution of this and other
44 Bioperl modules. Send your comments and suggestions preferably to
45 the Bioperl mailing list. Your participation is much appreciated.
46  
47 bioperl-l@bioperl.org - General discussion
48 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
49  
50 =head2 Support
51  
52 Please direct usage questions or support issues to the mailing list:
53  
54 I<bioperl-l@bioperl.org>
55  
56 rather than to the module maintainer directly. Many experienced and
57 reponsive experts will be able look at the problem and quickly
58 address it. Please include a thorough description of the problem
59 with code and data examples if at all possible.
60  
61 =head2 Reporting Bugs
62  
63 Report bugs to the Bioperl bug tracking system to help us keep track
64 of the bugs and their resolution. Bug reports can be submitted via the
65 web:
66  
67 https://redmine.open-bio.org/projects/bioperl/
68  
69 =head1 AUTHOR - Jason Stajich, Chris Fields
70  
71 Email jason-at-bioperl.org
72 Email cjfields-at-uiuc dot edu
73  
74 =head1 APPENDIX
75  
76 The rest of the documentation details each of the object methods.
77 Internal methods are usually preceded with a _
78  
79 =cut
80  
81 # Let the code begin...
82 package FAST::Bio::SearchIO::XML::BlastHandler;
83 use base qw(FAST::Bio::Root::Root XML::SAX::Base);
84  
85 my %MODEMAP = (
86 'Iteration' => 'result',
87 'Hit' => 'hit',
88 'Hsp' => 'hsp'
89 );
90  
91 # major post 2.2.12 BLAST XML changes
92 # 1) moved XML Handler to it's own class
93 # 2) reconfigure blastxml to deal with old and new BLAST XML output
94  
95 my %MAPPING = (
96 # Result-specific fields
97 'BlastOutput_program' => 'RESULT-algorithm_name',
98 'BlastOutput_version' => 'RESULT-algorithm_version',
99 'BlastOutput_db' => 'RESULT-database_name',
100 'BlastOutput_reference' => 'RESULT-program_reference',
101 'BlastOutput_query-def' => 'RESULT-query_description',
102 'BlastOutput_query-len' => 'RESULT-query_length',
103 'BlastOutput_query-ID' => 'runid',
104 'Parameters_matrix' => { 'RESULT-parameters' => 'matrix'},
105 'Parameters_expect' => { 'RESULT-parameters' => 'expect'},
106 'Parameters_include' => { 'RESULT-parameters' => 'include'},
107 'Parameters_sc-match' => { 'RESULT-parameters' => 'match'},
108 'Parameters_sc-mismatch' => { 'RESULT-parameters' => 'mismatch'},
109 'Parameters_gap-open' => { 'RESULT-parameters' => 'gapopen'},
110 'Parameters_gap-extend' => { 'RESULT-parameters' => 'gapext'},
111 'Parameters_filter' => {'RESULT-parameters' => 'filter'},
112 'Statistics_db-num' => 'RESULT-database_entries',
113 'Statistics_db-len' => 'RESULT-database_letters',
114 'Statistics_hsp-len' => { 'RESULT-statistics' => 'hsplength'},
115 'Statistics_eff-space' => { 'RESULT-statistics' => 'effectivespace'},
116 'Statistics_kappa' => { 'RESULT-statistics' => 'kappa' },
117 'Statistics_lambda' => { 'RESULT-statistics' => 'lambda' },
118 'Statistics_entropy' => { 'RESULT-statistics' => 'entropy'},
119
120 # HSP specific fields
121 'Hsp_bit-score' => 'HSP-bits',
122 'Hsp_score' => 'HSP-score',
123 'Hsp_evalue' => 'HSP-evalue',
124 'Hsp_query-from' => 'HSP-query_start',
125 'Hsp_query-to' => 'HSP-query_end',
126 'Hsp_hit-from' => 'HSP-hit_start',
127 'Hsp_hit-to' => 'HSP-hit_end',
128 'Hsp_positive' => 'HSP-conserved',
129 'Hsp_identity' => 'HSP-identical',
130 'Hsp_gaps' => 'HSP-gaps',
131 'Hsp_hitgaps' => 'HSP-hit_gaps',
132 'Hsp_querygaps' => 'HSP-query_gaps',
133 'Hsp_qseq' => 'HSP-query_seq',
134 'Hsp_hseq' => 'HSP-hit_seq',
135 'Hsp_midline' => 'HSP-homology_seq',
136 'Hsp_align-len' => 'HSP-hsp_length',
137 'Hsp_query-frame'=> 'HSP-query_frame',
138 'Hsp_hit-frame' => 'HSP-hit_frame',
139  
140 # Hit specific fields
141 'Hit_id' => 'HIT-name',
142 'Hit_len' => 'HIT-length',
143 'Hit_accession' => 'HIT-accession',
144 'Hit_def' => 'HIT-description',
145 'Hit_num' => 'HIT-order',
146 'Iteration_iter-num' => 'HIT-iteration',
147 'Iteration_stat' => 'HIT-iteration_statistic',
148
149 # if these tags are present, they will overwrite the
150 # above with more current data (i.e. multiquery hits)
151 'Iteration_query-def' => 'RESULT-query_description',
152 'Iteration_query-len' => 'RESULT-query_length',
153 'Iteration_query-ID' => 'runid',
154 );
155  
156 # these XML tags are ignored for now
157 my %IGNOREDTAGS = (
158 'Hsp_num' => 1,#'HSP-order',
159 'Hsp_pattern-from' => 1,#'patternend',
160 'Hsp_pattern-to' => 1,#'patternstart',
161 'Hsp_density' => 1,#'hspdensity',
162 'Iteration_message' => 1,
163 'Hit_hsps' => 1,
164 'BlastOutput_param' => 1,
165 'Iteration_hits' => 1,
166 'Statistics' => 1,
167 'Parameters' => 1,
168 'BlastOutput' => 1,
169 'BlastOutput_iterations' => 1,
170 );
171  
172 =head2 SAX methods
173  
174 =cut
175  
176 =head2 start_document
177  
178 Title : start_document
179 Usage : $parser->start_document;
180 Function: SAX method to indicate starting to parse a new document
181 Returns : none
182 Args : none
183  
184 =cut
185  
186 sub start_document{
187 my ($self) = @_;
188 $self->{'_lasttype'} = '';
189 $self->{'_values'} = {};
190 $self->{'_result'}= [];
191 }
192  
193 =head2 end_document
194  
195 Title : end_document
196 Usage : $parser->end_document;
197 Function: SAX method to indicate finishing parsing a new document
198 Returns : FAST::Bio::Search::Result::ResultI object
199 Args : none
200  
201 =cut
202  
203 sub end_document{
204 my ($self,@args) = @_;
205
206 # reset data carried throughout parse
207 $self->{'_resultdata'} = undef;
208
209 # pass back ref to results queue; caller must reset handler results queue
210 return $self->{'_result'};
211 }
212  
213 =head2 start_element
214  
215 Title : start_element
216 Usage : $parser->start_element($data)
217 Function: SAX method to indicate starting a new element
218 Returns : none
219 Args : hash ref for data
220  
221 =cut
222  
223 sub start_element{
224 my ($self,$data) = @_;
225 # we currently don't care about attributes
226 my $nm = $data->{'Name'};
227  
228 if( my $type = $MODEMAP{$nm} ) {
229 if( $self->eventHandler->will_handle($type) ) {
230 my $func = sprintf("start_%s",lc $type);
231 $self->eventHandler->$func($data->{'Attributes'});
232 }
233 }
234 }
235  
236 =head2 end_element
237  
238 Title : end_element
239 Usage : $parser->end_element($data)
240 Function: Signals finishing an element
241 Returns : FAST::Bio::Search object dpending on what type of element
242 Args : hash ref for data
243  
244 =cut
245  
246 sub end_element{
247 my ($self,$data) = @_;
248  
249 my $nm = $data->{'Name'};
250 my $rc;
251 if($nm eq 'BlastOutput_program' &&
252 $self->{'_last_data'} =~ /(t?blast[npx])/i ) {
253 $self->{'_type'} = uc $1;
254 }
255 if ($nm eq 'Iteration') {
256 map {
257 $self->{'_values'}->{$_} = $self->{'_resultdata'}->{$_};
258 } keys %{ $self->{'_resultdata'} };
259 }
260 if( my $type = $MODEMAP{$nm} ) {
261 if( $self->eventHandler->will_handle($type) ) {
262 my $func = sprintf("end_%s",lc $type);
263 $rc = $self->eventHandler->$func($self->{'_type'},
264 $self->{'_values'});
265 }
266 }
267 elsif( exists $MAPPING{$nm} ) {
268 if ( ref($MAPPING{$nm}) =~ /hash/i ) {
269 my $key = (keys %{$MAPPING{$nm}})[0];
270 $self->{'_values'}->{$key}->{$MAPPING{$nm}->{$key}} = $self->{'_last_data'};
271 } else {
272 $self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
273 }
274 }
275 elsif( exists $IGNOREDTAGS{$nm} ){
276 # ignores these elements for now
277 }
278 else {
279 $self->debug("ignoring unrecognized element type $nm\n");
280 }
281 $self->{'_last_data'} = ''; # remove read data if we are at
282 # end of an element
283
284 # add to ResultI array
285 $self->{'_result'} = $rc if( $nm eq 'Iteration' );
286 # reset values for each Result round
287 if ($nm eq 'Iteration') {
288 $self->{'_values'} = {};
289 }
290 }
291  
292 =head2 characters
293  
294 Title : characters
295 Usage : $parser->characters($data)
296 Function: Signals new characters to be processed
297 Returns : characters read
298 Args : hash ref with the key 'Data'
299  
300  
301 =cut
302  
303 sub characters{
304 my ($self,$data) = @_;
305 return unless ( defined $data->{'Data'} && $data->{'Data'} !~ /^\s+$/ );
306 $self->{'_last_data'} .= $data->{'Data'};
307 }
308  
309 sub eventHandler {
310 my $self = shift;
311 return $self->{'_handler'} = shift if @_;
312 return $self->{'_handler'};
313 }
314  
315 1;