Phylo/Parsers/Tolweb.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 130 CC 9 SCAR 28.6
CC
9
Cov
0%
_process_metadata
42.0
_handle_node
12.0
_parse
6.0
CRAP 17.4
1 package Bio::Phylo::Parsers::Tolweb;
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::CONSTANT qw'looks_like_instance :namespaces';
7 use Bio::Phylo::Util::Dependency 'XML::Twig';
8  
9 =head1 NAME
10  
11 Bio::Phylo::Parsers::Tolweb - Parser used by Bio::Phylo::IO, no serviceable parts inside
12  
13 =head1 DESCRIPTION
14  
15 This module parses Tree of Life data. It is called by the L<Bio::Phylo::IO> facade,
16 don't call it directly. In addition to parsing from files, handles or strings (which
17 are specified by the -file, -handle and -string arguments) this parser can also parse
18 xml directly from a url (-url => $tolweb_output), provided you have L<LWP> installed.
19  
20 =cut
21  
22 # podinherit_insert_token
23  
24 =head1 SEE ALSO
25  
26 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
27 for any user or developer questions and discussions.
28  
29 =over
30  
31 =item L<Bio::Phylo::IO>
32  
33 The ToL web parser is called by the L<Bio::Phylo::IO> object.
34 Look there to learn how to parse Tree of Life data (or any other data Bio::Phylo supports).
35  
36 =item L<Bio::Phylo::Manual>
37  
38 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
39  
40 =item L<http://tolweb.org>
41  
42 For more information about the Tree of Life xml format, visit
43 L<http://tolweb.org/tree/home.pages/downloadtree.html>
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  
57  
58 =cut
59  
60 # this is the constructor that gets called by Bio::Phylo::IO,
61 # here we create the object instance that will process the file/string
62 sub _init {
63 my $self = shift;
64 $self->_logger->debug("initializing $self");
65  
66 # this is the actual parser object, which needs to hold a reference
67 # to the XML::Twig object and to the tree
68 $self->{'_tree'} = undef;
69 $self->{'_node_of'} = {};
70 $self->{'_parent_of'} = {};
71  
72 # here we put the two together, i.e. create the actual XML::Twig object
73 # with its handlers, and create a reference to it in the parser object
74 $self->{'_twig'} = XML::Twig->new(
75 'TwigHandlers' => { 'NODE' => sub { &_handle_node( $self, @_ ) }, } );
76 return $self;
77 }
78  
79 sub _parse {
80 my $self = shift;
81 $self->_init;
82 $self->_logger->debug("going to parse xml");
83 $self->{'_tree'} = $self->_factory->create_tree(
84 '-namespaces' => {
85 'tol' => _NS_TOL_,
86 'dc' => _NS_DC_,
87 }
88 );
89 $self->{'_twig'}->parse( $self->_string );
90 $self->_logger->debug("done parsing xml");
91  
92 # now we build the tree structure
93 for my $node_id ( keys %{ $self->{'_node_of'} } ) {
94 if ( defined( my $parent_id = $self->{'_parent_of'}->{$node_id} ) ) {
95 my $child = $self->{'_node_of'}->{$node_id};
96 my $parent = $self->{'_node_of'}->{$parent_id};
97 $child->set_parent($parent);
98 }
99 }
100  
101 # we're done, now insert the tree in a forest
102 return $self->_factory->create_forest->insert($self->{'_tree'});
103 }
104  
105 sub _handle_node {
106 my ( $self, $twig, $node_elt ) = @_;
107 my $fac = $self->_factory;
108 my $node_obj = $fac->create_node;
109 $self->{'_tree'}->insert($node_obj);
110  
111 # these hashes are populated so that we can build the topology
112 # once we've processed all NODE elements
113 my $id = $node_elt->att('ID');
114 $node_obj->set_guid($id);
115 $node_obj->add_meta(
116 $fac->create_meta( '-triple' => { 'dc:identifier' => $id } )
117 );
118 $self->{'_node_of'}->{$id} = $node_obj;
119 if ( my $parent = $node_elt->parent->parent ) {
120 $self->{'_parent_of'}->{$id} = $parent->att('ID');
121 }
122
123 # process node level metadata
124 $self->_process_metadata( $node_elt, $node_obj );
125
126 # process other names
127 my ($othernames_elt) = $node_elt->children('OTHERNAMES');
128 if ( $othernames_elt ) {
129 for my $othername_elt ( $othernames_elt->children ) {
130 my ($name_elt) = $othername_elt->children('NAME');
131 my $text = $name_elt->text;
132 my $meta_obj = $fac->create_meta( '-triple' => { 'tol:othername' => $text } );
133 $node_obj->add_meta( $meta_obj );
134 $self->_process_metadata( $othername_elt, $meta_obj );
135 }
136 }
137
138 $node_elt->delete;
139 }
140  
141 sub _process_metadata {
142 my ( $self, $elt, $obj ) = @_;
143 my $elt_tag = $elt->tag;
144 my $fac = $self->_factory;
145  
146 # we don't process child NODE elements here, we construct the topology
147 # afterwards. This so that we can process the element structure in chunks.
148 for my $child ( $elt->children ) {
149 my ( $child_tag, $text ) = ( $child->tag, $child->text );
150 if ( $text && $child_tag eq 'NAME' ) {
151 $obj->set_name($text) if $elt_tag eq 'NODE';
152 }
153 elsif ( $text && $child_tag eq 'DESCRIPTION' ) {
154 $obj->add_meta( $fac->create_meta( '-triple' => { 'dc:description' => $text } ) );
155 $obj->set_desc( $text );
156 }
157 elsif ( $text && $child_tag ne 'NODES' && $child_tag ne 'OTHERNAMES' ) {
158 $obj->add_meta( $fac->create_meta( '-triple' => { 'tol:' . lc( $child_tag ) => $text } ) );
159 }
160 }
161
162 for my $att_name ( $elt->att_names ) {
163 if ( defined $elt->att($att_name) ) {
164 $obj->add_meta( $fac->create_meta( '-triple' => { 'tol:' . lc($att_name) => $elt->att($att_name) } ) );
165 }
166 }
167
168 }
169  
170 1;