Phylo/Unparsers/Phyloxml.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 156 CC 15 SCAR 31.3
CC
15
Cov
0%
_meta_to_xml
56.0
_to_string
30.0
_node_to_xml
12.0
CRAP 22.9
1 package Bio::Phylo::Unparsers::Phyloxml;
2 use strict;
3 use warnings;
4 use base 'Bio::Phylo::Unparsers::Abstract';
5 use Bio::Phylo::Util::Exceptions 'throw';
6 use Bio::Phylo::Util::CONSTANT qw':objecttypes looks_like_object :namespaces';
7 use Bio::Phylo::Util::Dependency 'XML::Twig';
8 my $phyloxml_ns = _NS_PHYLOXML_;
9 my $phyloxml_header = <<'HEADER';
10 <?xml version="1.0" encoding="UTF-8"?>
11 <phyloxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
12 xsi:schemaLocation="http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd"
13 xmlns="http://www.phyloxml.org">
14 HEADER
15 my %has_attribute = (
16 'confidence' => 'type',
17 'id' => 'provider',
18 );
19  
20 =head1 NAME
21  
22 Bio::Phylo::Unparsers::Phyloxml - Serializer used by Bio::Phylo::IO, no serviceable parts inside
23  
24 =head1 DESCRIPTION
25  
26 This module turns a L<Bio::Phylo::Forest> object into a PhyloXML file. It is called by the L<Bio::Phylo::IO> facade, don't call it
27 directly.
28  
29 =begin comment
30  
31 Type : Wrapper
32 Title : _to_string
33 Usage : my $mrp_string = $mrp->_to_string;
34 Function: Stringifies a matrix object into
35 an MRP nexus formatted table.
36 Alias :
37 Returns : SCALAR
38 Args : Bio::Phylo::Matrices::Matrix;
39  
40 =end comment
41  
42 =cut
43  
44 sub _to_string {
45 my $self = shift;
46 my $proj = $self->{'PHYLO'};
47 $self->_logger->debug("serializing object $proj");
48 my @trees;
49 if ( looks_like_object $proj, _PROJECT_ ) {
50 $self->_logger->debug("object is a project");
51 for my $forest ( @{ $proj->get_forests } ) {
52 push @trees, @{ $forest->get_entities };
53 }
54 }
55 elsif ( looks_like_object $proj, _FOREST_ ) {
56 $self->_logger->debug("object is a forest");
57 push @trees, @{ $proj->get_entities };
58 }
59 elsif ( looks_like_object $proj, _TREE_ ) {
60 $self->_logger->debug("object is a tree");
61 push @trees, $proj;
62 }
63 my $xml = $phyloxml_header;
64 $xml .= $self->_tree_to_xml($_) for @trees;
65 $xml .= '</phyloxml>';
66  
67 # pretty printing
68 my $twig = XML::Twig->new( 'pretty_print' => 'indented' );
69 eval { $twig->parse($xml) };
70 if ($@) {
71 throw 'API' => "Couldn't build xml: " . $@;
72 }
73 else {
74 return $twig->sprint;
75 }
76 }
77  
78 sub _name_to_xml {
79 my ( $self, $obj ) = @_;
80 if ( my $name = $obj->get_name ) {
81 return sprintf( '<name>%s</name>', $name );
82 }
83 return '';
84 }
85  
86 sub _tree_to_xml {
87 my ( $self, $tree ) = @_;
88 my $rooted = $tree->is_rooted ? 'true' : 'false';
89 my $xml = sprintf( '<phylogeny rooted="%s">', $rooted );
90 $xml .= $self->_name_to_xml($tree);
91 $xml .= $self->_node_to_xml( $tree->get_root );
92 return $xml .= '</phylogeny>';
93 }
94  
95 sub _node_to_xml {
96 my ( $self, $node ) = @_;
97 my $xml = '<clade>' . $self->_name_to_xml($node);
98  
99 # branch length
100 my $length = $node->get_branch_length;
101 if ( defined $length ) {
102 $xml .= '<branch_length>' . $length . '</branch_length>';
103 }
104  
105 # annotations
106 $xml .= $self->_meta_to_xml($_) for @{ $node->get_meta };
107  
108 # taxon links
109 if ( my $taxon = $node->get_taxon ) {
110 $xml .= $self->_taxon_to_xml($taxon);
111 }
112  
113 # traverse nodes
114 $xml .= $self->_node_to_xml($_) for @{ $node->get_children };
115 return $xml .= '</clade>';
116 }
117  
118 sub _meta_to_xml {
119 my ( $self, $meta ) = @_;
120 my $fq_predicate = $meta->get_predicate;
121 my $xml;
122 if ( $fq_predicate =~ /^(.+?):(.+)$/ ) {
123 my ( $pre, $predicate ) = ( $1, $2 );
124 my $namespace = $meta->get_namespaces($pre);
125 if ( $namespace eq $phyloxml_ns ) {
126 my $obj = $meta->get_object;
127 $xml = "<${predicate}>";
128  
129 # object is a single, nested annotation
130 if ( UNIVERSAL::can( $obj, '_type' ) && $obj->_type == _META_ ) {
131 if ( my $att = $has_attribute{$predicate} ) {
132 my $inner_predicate = $obj->get_predicate;
133 my $obj = $obj->get_object;
134 $inner_predicate =~ s/^.+://;
135 $xml = "<${predicate} ${att}=\"${inner_predicate}\">${obj}";
136 $self->_logger->debug($xml);
137 }
138 else {
139 $xml .= $self->_meta_to_xml($obj);
140 }
141 }
142  
143 # object is an array of annotations
144 elsif ( UNIVERSAL::isa( $obj, 'ARRAY' ) ) {
145 for my $inner ( @{$obj} ) {
146 if ( UNIVERSAL::can( $inner, 'sprint' ) ) {
147 $xml .= $inner->sprint;
148 }
149 else {
150 $xml .= $self->_meta_to_xml($inner);
151 }
152 }
153 }
154 else {
155 $self->_logger->debug("meta object is $obj");
156 $xml .= $obj;
157 }
158 $xml .= "</${predicate}>";
159 }
160 }
161 return $xml;
162 }
163  
164 sub _taxon_to_xml {
165 my ( $self, $taxon ) = @_;
166 my $xml = '<taxonomy>';
167 $xml .= $self->_meta_to_xml($_) for @{ $taxon->get_meta };
168 return $xml .= '</taxonomy>';
169 }
170  
171 sub _datum_to_xml {
172 my ( $self, $datum ) = @_;
173 return '';
174 }
175  
176 # podinherit_insert_token
177  
178 =head1 SEE ALSO
179  
180 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
181 for any user or developer questions and discussions.
182  
183 =over
184  
185 =item L<Bio::Phylo::IO>
186  
187 The newick unparser is called by the L<Bio::Phylo::IO> object.
188 Look there to learn how to create mrp matrices.
189  
190 =item L<Bio::Phylo::Manual>
191  
192 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
193  
194 =item L<http://www.phyloxml.org>
195  
196 To learn more about the PhyloXML standard, visit L<http://www.phyloxml.org>
197  
198 =back
199  
200 =head1 CITATION
201  
202 If you use Bio::Phylo in published research, please cite it:
203  
204 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
205 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
206 I<BMC Bioinformatics> B<12>:63.
207 L<http://dx.doi.org/10.1186/1471-2105-12-63>
208  
209 =cut
210  
211 1;