Phylo/Parsers/Phyloxml.pm untested

filter: Statementstmt 0.0% 0 / 142 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 202 CC 13 SCAR 19.5
CC
13
Cov
0%
_obj_from_elt
20.0
_process_branch_length
12.0
_fetch_taxon_and_taxa
12.0
CRAP 7.1
1 package Bio::Phylo::Parsers::Phyloxml;
2 use strict;
3 use warnings;
4 use base 'Bio::Phylo::Parsers::Abstract';
5 use Bio::Phylo::Util::Exceptions 'throw';
6 use Bio::Phylo::Util::Dependency 'XML::Twig';
7 use Bio::Phylo::Util::CONSTANT qw'looks_like_instance';
8 use Bio::Phylo::NeXML::Writable;
9 use Bio::Phylo::Factory;
10  
11 =head1 NAME
12  
13 Bio::Phylo::Parsers::Phyloxml - Parser used by Bio::Phylo::IO, no serviceable parts inside
14  
15 =head1 DESCRIPTION
16  
17 This module parses phyloxml data. It is called by the L<Bio::Phylo::IO> facade,
18 don't call it directly. In addition to parsing from files, handles or strings (which
19 are specified by the -file, -handle and -string arguments) this parser can also parse
20 xml directly from a url (-url => $phylows_output), provided you have L<LWP> installed.
21  
22 =cut
23  
24 # podinherit_insert_token
25  
26 =head1 SEE ALSO
27  
28 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
29 for any user or developer questions and discussions.
30  
31 =over
32  
33 =item L<Bio::Phylo::IO>
34  
35 The phyloxml parser is called by the L<Bio::Phylo::IO> object.
36 Look there to learn how to parse phyloxml (or any other data Bio::Phylo supports).
37  
38 =item L<Bio::Phylo::Manual>
39  
40 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
41  
42 =item L<http://www.phyloxml.org>
43  
44 For more information about the phyloxml data standard, visit L<http://www.phyloxml.org>
45  
46 =back
47  
48 =head1 CITATION
49  
50 If you use Bio::Phylo in published research, please cite it:
51  
52 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
53 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
54 I<BMC Bioinformatics> B<12>:63.
55 L<http://dx.doi.org/10.1186/1471-2105-12-63>
56  
57 =cut
58  
59 # The factory object, to instantiate Bio::Phylo objects
60 #my $factory = Bio::Phylo::Factory->new;
61 # For semantic annotations
62 Bio::Phylo::NeXML::Writable->set_namespaces(
63 'px' => 'http://www.phyloxml.org/1.10/terms#' );
64  
65 # I factored the logging methods in Bio::Phylo (debug, info,
66 # warning, error, fatal) out of the inheritance tree and put
67 # them in a separate logging object.
68 # my $logger = Bio::Phylo::Util::Logger->new;
69 # helper method to add parser reading position to log messages
70 sub _pos {
71 my $self = shift;
72 my $t = $self->{'_twig'};
73 join ':', ( $t->current_line, $t->current_column, $t->current_byte );
74 }
75  
76 # nice 'n' generic: we provide an element and a class,
77 # from the class we instantiate a new object, we set
78 # the element id in the generic slot of the object.
79 # If the element has a label, use that as name,
80 # otherwise use id. Additional constructor args can
81 # be specified using named arguments, e.g. -type => 'dna'
82 sub _obj_from_elt {
83 my ( $self, $elt, $class, %args ) = @_;
84  
85 # factory object handles instantiation (and class loading)
86 # see Bio::Phylo::Factory
87 my $method = "create_$class";
88 my $obj = $self->_factory->$method(%args);
89  
90 # description
91 if ( my ($desc_elt) = $elt->children('description') ) {
92 $obj->set_desc( $desc_elt->text );
93 }
94  
95 # id_source
96 if ( my $id_source = $elt->att('id_source') ) {
97 $obj->add_meta(
98 $self->_factory->create_meta(
99 '-triple' => { 'px:id_source' => $id_source }
100 )
101 );
102 }
103  
104 # name
105 my $tag = $elt->tag;
106 my ($name_elt) = ( $elt->children('name') );
107 if ( defined $name_elt ) {
108 my $id = $name_elt->text;
109 $obj->set_name($id);
110 $self->_logger->debug( $self->_pos . " processed <$tag id=\"$id\"/>" );
111 }
112 else {
113 $self->_logger->debug( $self->_pos . " processed <$tag/>" );
114 }
115 return $obj;
116 }
117  
118 # here we create the object instance that will process the file/string
119 sub _init {
120 my $self = shift;
121  
122 # this is the actual parser object, which needs to hold a reference
123 # to the XML::Twig object, to a hash of processed blocks (for fast lookup by id)
124 # and an array of ids (to preserve processing order)
125 $self->{'_taxon_in_taxa'} = {};
126 $self->{'_proj'} = $self->_factory->create_project;
127 $self->{'_blocks'} = [ $self->{'_proj'} ];
128  
129 # here we put the two together, i.e. create the actual XML::Twig object
130 # with its handlers, and create a reference to it in the parser object
131 $self->{'_twig'} = XML::Twig->new(
132  
133 # These handlers are called when the subtree is fully loaded, which
134 # means we can traverse it
135 'TwigHandlers' =>
136 { 'phylogeny' => sub { &_handle_phylogeny( @_, $self ) }, },
137  
138 # These handlers are called when the element opens, that is the
139 # subtree hasn't been loaded yet - but the attributes have been,
140 # so we can read in the namespaces here.
141 'StartTagHandlers' => {
142 '_all_' => sub {
143 my ( $twig, $elt ) = @_;
144 for my $att_name ( $elt->att_names ) {
145 if ( $att_name =~ /^xmlns:(.+)$/ ) {
146 my $prefix = $1;
147 my $ns = $elt->att($att_name);
148 Bio::Phylo::NeXML::Writable->set_namespaces(
149 $prefix => $ns );
150 }
151 }
152 }
153 },
154 );
155 return $self;
156 }
157  
158 sub _handle_phylogeny {
159 my ( $twig, $phylogeny_elt, $self ) = @_;
160 my $forest;
161 my $tree = _obj_from_elt( $self, $phylogeny_elt, 'tree' );
162 unless ( $forest = $self->_project->get_forests->[0] ) {
163 $forest = $self->_factory->create_forest;
164 $self->_project->insert($forest);
165 }
166 $forest->insert($tree);
167 $tree->set_as_unrooted( $phylogeny_elt->att('rooted') ne 'true' );
168 for ( $phylogeny_elt->children('clade') ) {
169 $self->_process_clade( $twig, $_, $tree );
170 }
171 }
172  
173 sub _process_clade {
174 my ( $self, $twig, $clade_elt, $tree, $parent ) = @_;
175 my $node = _obj_from_elt( $self, $clade_elt, 'node' );
176 $node->set_parent($parent) if $parent;
177 $tree->insert($node);
178  
179 # branch length
180 $self->_process_branch_length( $clade_elt, $node );
181  
182 # support values, e.g. bootstrap, posterior
183 $self->_handle_confidence( $_, $node )
184 for $clade_elt->children('confidence');
185  
186 # taxonomy, e.g. identifiers, GUIDs, ranks, names
187 $self->_process_taxonomy( $_, $node ) for $clade_elt->children('taxonomy');
188  
189 # events, e.g. duplications, speciations
190 $self->_process_events( $_, $node ) for $clade_elt->children('events');
191 for ( $clade_elt->children('clade') ) {
192 $self->_process_clade( $twig, $_, $tree, $node );
193 }
194 }
195  
196 sub _process_sequence {
197 my ( $self, $seq_elt, $node ) = @_;
198 my ( $taxon, $taxa ) = $self->_fetch_taxon_and_taxa($node);
199 my $matrix;
200 unless ( $matrix = $self->get_matrices->[0] ) {
201 $matrix = $self->_factory->create_matrix( '-taxa' => $taxa );
202 $self->_project->insert($matrix);
203 }
204 my $datum = $self->_obj_from_elt( $seq_elt, 'datum', '-taxon' => $taxon );
205 $matrix->insert($datum);
206 }
207  
208 sub _process_branch_length {
209 my ( $self, $clade_elt, $node ) = @_;
210 if ( my ($bl_elt) = ( $clade_elt->children('branch_length') ) ) {
211 $node->set_branch_length( $bl_elt->text );
212 }
213 if ( my $length = $clade_elt->att('branch_length') ) {
214 $node->set_branch_length($length);
215 }
216 }
217  
218 sub _fetch_taxon_and_taxa {
219 my ( $self, $node ) = @_;
220  
221 # fetch or instantiate taxon object
222 my ( $taxon, $taxa );
223 unless ( $taxon = $node->get_taxon ) {
224 unless ( $taxa = $self->_project->get_taxa->[0] ) {
225 $self->_project->insert( $taxa = $self->_factory->create_taxa );
226 $self->_project->get_forests->[0]->set_taxa($taxa);
227 }
228 $taxon = $self->_factory->create_taxon;
229 $taxa->insert($taxon);
230 $node->set_taxon($taxon);
231 }
232 return $taxon, $taxa;
233 }
234  
235 sub _process_taxonomy {
236 my ( $self, $taxonomy_elt, $node ) = @_;
237  
238 # fetch or instantiate taxon object
239 my ($taxon) = $self->_fetch_taxon_and_taxa($node);
240  
241 # handle taxonomy annotations
242 $self->_process_taxonomy_annotations( $_, $taxon )
243 for $taxonomy_elt->children;
244 }
245  
246 sub _process_taxonomy_annotations {
247 my ( $self, $elt, $taxon ) = @_;
248 my ( $text, $tag ) = ( $elt->text, $elt->tag );
249 if ( my $provider = $elt->att('provider') ) {
250 $taxon->add_meta(
251 $self->_factory->create_meta(
252 '-triple' => {
253 "px:${tag}" => $self->_factory->create_meta(
254 '-triple' => { "px:${provider}" => $elt->text }
255 )
256 }
257 )
258 );
259 }
260 else {
261 $taxon->add_meta(
262 $self->_factory->create_meta(
263 '-triple' => { "px:${tag}" => $text }
264 )
265 );
266 }
267 }
268  
269 sub _handle_confidence {
270 my ( $self, $confidence_elt, $node ) = @_;
271 $node->add_meta(
272 $self->_factory->create_meta(
273 '-triple' => {
274 'px:confidence' => $self->_factory->create_meta(
275 '-triple' => {
276 'px:'
277 . $confidence_elt->att('type') =>
278 $confidence_elt->text
279 }
280 )
281 }
282 )
283 );
284 }
285  
286 sub _process_events {
287 my ( $self, $events_elt, $node ) = @_;
288 my @events;
289 for ( $events_elt->children ) {
290 push @events,
291 $self->_factory->create_meta(
292 '-triple' => { 'px:' . $_->tag => $_->text } );
293 }
294 $node->add_meta(
295 $self->_factory->create_meta(
296 '-triple' => { 'px:events' => \@events }
297 )
298 );
299 }
300  
301 # this method will be called by Bio::Phylo::IO, indirectly, through
302 # _from_handle if the parse function is called with the -file => $filename
303 # argument, or through _from_string if called with the -string => $string
304 # argument
305 sub _parse {
306 my $self = shift;
307 $self->_logger->debug("going to parse xml");
308 $self->_init;
309 my %opt = @_;
310  
311 # XML::Twig doesn't care if we parse from a handle or a string
312 $self->{'_twig'}->parse( $self->_string );
313  
314 # we're done, now order the blocks
315 my $ordered_blocks = $self->{'_blocks'};
316  
317 # prepare the requested return...
318 my $temp_project = shift( @{$ordered_blocks} );
319 return @{ $temp_project->get_taxa },
320 @{ $temp_project->get_forests },
321 @{ $temp_project->get_matrices };
322 }
323 sub DESTROY { 1 }
324 1;