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

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 108 SCAR 53.5
CC
14
Cov
0%
end_element
110.0
start_element
12.0
characters
6.0
CRAP 210.0
1 #
2 # BioPerl module for FAST::Bio::SearchIO::XML::PsiBlastHandler
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 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::XML::PsiBlastHandler - XML Handler for NCBI Blast PSIBLAST 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 PSIBLAST XML parsing. Currently it passes
25 elements off to the event handler, which is ultimately responsible for
26 FAST::Bio::Search object 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::PsiBlastHandler;
83 use base qw(FAST::Bio::Root::Root XML::SAX::Base);
84  
85 my %MODEMAP = (
86 'BlastOutput' => 'result',
87 'Iteration' => 'iteration',
88 'Hit' => 'hit',
89 'Hsp' => 'hsp'
90 );
91  
92 # MAPPING is distinct from BlastHandler, can't really mix the two...
93  
94 my %MAPPING = (
95 # Result-specific fields
96 'BlastOutput_program' => 'RESULT-algorithm_name',
97 'BlastOutput_version' => 'RESULT-algorithm_version',
98 'BlastOutput_db' => 'RESULT-database_name',
99 'BlastOutput_reference' => 'RESULT-program_reference',
100 'BlastOutput_query-def' => 'RESULT-query_description',
101 'BlastOutput_query-len' => 'RESULT-query_length',
102 'BlastOutput_query-ID' => 'runid',
103 'Parameters_matrix' => { 'RESULT-parameters' => 'matrix'},
104 'Parameters_expect' => { 'RESULT-parameters' => 'expect'},
105 'Parameters_include' => { 'RESULT-parameters' => 'include'},
106 'Parameters_sc-match' => { 'RESULT-parameters' => 'match'},
107 'Parameters_sc-mismatch' => { 'RESULT-parameters' => 'mismatch'},
108 'Parameters_gap-open' => { 'RESULT-parameters' => 'gapopen'},
109 'Parameters_gap-extend' => { 'RESULT-parameters' => 'gapext'},
110 'Parameters_filter' => {'RESULT-parameters' => 'filter'},
111 'Statistics_db-num' => 'RESULT-database_entries',
112 'Statistics_db-len' => 'RESULT-database_letters',
113 'Statistics_hsp-len' => { 'RESULT-statistics' => 'hsplength'},
114 'Statistics_eff-space' => { 'RESULT-statistics' => 'effectivespace'},
115 'Statistics_kappa' => { 'RESULT-statistics' => 'kappa' },
116 'Statistics_lambda' => { 'RESULT-statistics' => 'lambda' },
117 'Statistics_entropy' => { 'RESULT-statistics' => 'entropy'},
118  
119 # Iteration-specific parameters
120 'Iteration_iter-num' => 'ITERATION-number',
121 'Iteration_converged' => 'ITERATION-converged',
122  
123 # HSP specific fields
124 'Hsp_bit-score' => 'HSP-bits',
125 'Hsp_score' => 'HSP-score',
126 'Hsp_evalue' => 'HSP-evalue',
127 'Hsp_query-from' => 'HSP-query_start',
128 'Hsp_query-to' => 'HSP-query_end',
129 'Hsp_hit-from' => 'HSP-hit_start',
130 'Hsp_hit-to' => 'HSP-hit_end',
131 'Hsp_positive' => 'HSP-conserved',
132 'Hsp_identity' => 'HSP-identical',
133 'Hsp_gaps' => 'HSP-gaps',
134 'Hsp_hitgaps' => 'HSP-hit_gaps',
135 'Hsp_querygaps' => 'HSP-query_gaps',
136 'Hsp_qseq' => 'HSP-query_seq',
137 'Hsp_hseq' => 'HSP-hit_seq',
138 'Hsp_midline' => 'HSP-homology_seq',
139 'Hsp_align-len' => 'HSP-hsp_length',
140 'Hsp_query-frame'=> 'HSP-query_frame',
141 'Hsp_hit-frame' => 'HSP-hit_frame',
142  
143 # Hit specific fields
144 'Hit_id' => 'HIT-name',
145 'Hit_len' => 'HIT-length',
146 'Hit_accession' => 'HIT-accession',
147 'Hit_def' => 'HIT-description',
148 'Hit_num' => 'HIT-order',
149 'Iteration_iter-num' => 'HIT-iteration',
150 'Iteration_stat' => 'HIT-iteration_statistic',
151 );
152  
153 # these XML tags are ignored for now
154 my %IGNOREDTAGS = (
155 'Hsp_num' => 1,#'HSP-order',
156 'Hsp_pattern-from' => 1,#'patternend',
157 'Hsp_pattern-to' => 1,#'patternstart',
158 'Hsp_density' => 1,#'hspdensity',
159 'Iteration_message' => 1,
160 'Hit_hsps' => 1,
161 'BlastOutput_param' => 1,
162 'Iteration_hits' => 1,
163 'Statistics' => 1,
164 'Parameters' => 1,
165 'BlastOutput' => 1,
166 'BlastOutput_iterations' => 1,
167 );
168  
169 =head2 SAX methods
170  
171 =cut
172  
173 =head2 start_document
174  
175 Title : start_document
176 Usage : $parser->start_document;
177 Function: SAX method to indicate starting to parse a new document
178 Returns : none
179 Args : none
180  
181 =cut
182  
183 sub start_document{
184 my ($self) = @_;
185 $self->{'_lasttype'} = '';
186 $self->{'_values'} = {};
187 $self->{'_result'}= [];
188 }
189  
190 =head2 end_document
191  
192 Title : end_document
193 Usage : $parser->end_document;
194 Function: SAX method to indicate finishing parsing a new document
195 Returns : FAST::Bio::Search::Result::ResultI object
196 Args : none
197  
198 =cut
199  
200 sub end_document{
201 my ($self,@args) = @_;
202
203 # reset data carried throughout parse
204 $self->{'_resultdata'} = undef;
205
206 # pass back ref to results queue; caller must reset handler results queue
207 return $self->{'_result'};
208 }
209  
210 =head2 start_element
211  
212 Title : start_element
213 Usage : $parser->start_element($data)
214 Function: SAX method to indicate starting a new element
215 Returns : none
216 Args : hash ref for data
217  
218 =cut
219  
220 sub start_element{
221 my ($self,$data) = @_;
222 # we currently don't care about attributes
223 my $nm = $data->{'Name'};
224  
225 if( my $type = $MODEMAP{$nm} ) {
226 if( $self->eventHandler->will_handle($type) ) {
227 my $func = sprintf("start_%s",lc $type);
228 $self->eventHandler->$func($data->{'Attributes'});
229 }
230 }
231 }
232  
233 =head2 end_element
234  
235 Title : end_element
236 Usage : $parser->end_element($data)
237 Function: Signals finishing an element
238 Returns : FAST::Bio::Search object dpending on what type of element
239 Args : hash ref for data
240  
241 =cut
242  
243 sub end_element{
244 my ($self,$data) = @_;
245  
246 my $nm = $data->{'Name'};
247 my $rc;
248 if($nm eq 'BlastOutput_program' &&
249 $self->{'_last_data'} =~ /(t?blast[npx])/i ) {
250 $self->{'_type'} = uc $1;
251 }
252 if ($nm eq 'Iteration') {
253 map {
254 $self->{'_values'}->{$_} = $self->{'_resultdata'}->{$_};
255 } keys %{ $self->{'_resultdata'} };
256 }
257 if( my $type = $MODEMAP{$nm} ) {
258 if( $self->eventHandler->will_handle($type) ) {
259 my $func = sprintf("end_%s",lc $type);
260 $rc = $self->eventHandler->$func($self->{'_type'},
261 $self->{'_values'});
262 }
263 }
264 elsif( exists $MAPPING{$nm} ) {
265 if ( ref($MAPPING{$nm}) =~ /hash/i ) {
266 my $key = (keys %{$MAPPING{$nm}})[0];
267 $self->{'_values'}->{$key}->{$MAPPING{$nm}->{$key}} = $self->{'_last_data'};
268 } else {
269 $self->{'_values'}->{$MAPPING{$nm}} = $self->{'_last_data'};
270 }
271 }
272 elsif( exists $IGNOREDTAGS{$nm} ){
273 # ignores these elements for now
274 }
275 else {
276 $self->debug("ignoring unrecognized element type $nm\n");
277 }
278 $self->{'_last_data'} = ''; # remove read data if we are at
279 # end of an element
280
281 # add to ResultI array
282 $self->{'_result'} = $rc if( $nm eq 'BlastOutput' );
283 # reset values for each Result round
284 if ($nm eq 'BlastOutput') {
285 $self->{'_values'} = {};
286 }
287 }
288  
289 =head2 characters
290  
291 Title : characters
292 Usage : $parser->characters($data)
293 Function: Signals new characters to be processed
294 Returns : characters read
295 Args : hash ref with the key 'Data'
296  
297  
298 =cut
299  
300 sub characters{
301 my ($self,$data) = @_;
302 return unless ( defined $data->{'Data'} && $data->{'Data'} !~ /^\s+$/ );
303 $self->{'_last_data'} .= $data->{'Data'};
304 }
305  
306 sub eventHandler {
307 my $self = shift;
308 return $self->{'_handler'} = shift if @_;
309 return $self->{'_handler'};
310 }
311  
312 1;