Phylo/Parsers/Phyloxml.pm untested

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