Phylo/Parsers/Tolweb.pm untested

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