Phylo/Unparsers/Phyloxml.pm untested

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