Bio/TreeIO/phyloxml.pm

filter: Statementstmt 91.0% 539 / 592 Branchbran 73.1% 155 / 212 Conditioncond 50.0% 40 / 80 MC/DCmcdc 18.6% 8 / 43 Subroutinesub 97.6% 41 / 42 Podpod 96.1% 25 / 26 totaltotal 81.2% 808 / 995 CC 157 SCAR 24.2
CC
157
Cov
83%
end_element_default
36.9
node_to_string
30.0
element_default
12.6
CRAP 11.2
« Bio/Species.pm ↑ Summary Bio/OntologyIO/dagflat.pm »
1 # $Id: phyloxml.pm 11507 2007-06-23 01:37:45Z jason $
2 #
3 # BioPerl module for Bio::TreeIO::phyloxml
4 #
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 #
7 # Cared for by Mira Han <mirhan@indiana.edu>
8 #
9 # Copyright Mira Han
10 #
11 # You may distribute this module under the same terms as perl itself
12  
13 # POD documentation - main docs before the code
14  
15 =head1 NAME
16  
17 Bio::TreeIO::phyloxml - TreeIO implementation for parsing PhyloXML format.
18  
19 =head1 SYNOPSIS
20  
21 # do not use this module directly
22 use Bio::TreeIO;
23 my $treeio = Bio::TreeIO->new(-format => 'phyloxml',
24 -file => 'tree.dnd');
25 my $tree = $treeio->next_tree;
26  
27 =head1 DESCRIPTION
28  
29 This module handles parsing and writing of phyloXML format.
30  
31 =head1 FEEDBACK
32  
33 =head2 Mailing Lists
34  
35 User feedback is an integral part of the evolution of this and other
36 Bioperl modules. Send your comments and suggestions preferably to the
37 Bioperl mailing list. Your participation is much appreciated.
38  
39 bioperl-l@bioperl.org - General discussion
40 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
41  
42 =head2 Support
43  
44 Please direct usage questions or support issues to the mailing list:
45  
46 I<bioperl-l@bioperl.org>
47  
48 rather than to the module maintainer directly. Many experienced and
49 reponsive experts will be able look at the problem and quickly
50 address it. Please include a thorough description of the problem
51 with code and data examples if at all possible.
52  
53 =head2 Reporting Bugs
54  
55 Report bugs to the Bioperl bug tracking system to help us keep track
56 of the bugs and their resolution. Bug reports can be submitted viax the
57 web:
58  
59 https://github.com/bioperl/bioperl-live/issues
60  
61 =head1 AUTHOR - Mira Han
62  
63 Email mirhan@indiana.edu
64  
65 =head1 APPENDIX
66  
67 The rest of the documentation details each of the object methods.
68 Internal methods are usually preceded with a _
69  
70 =cut
71  
72  
73 # Let the code begin...
74  
75  
76 package Bio::TreeIO::phyloxml;
77 2 $Bio::TreeIO::phyloxml::VERSION = '1.7.8';
78 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 # Object preamble - inherits from Bio::Root::Root
81  
82 2 ▶use Bio::Tree::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use Bio::Tree::AnnotatableNode;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶use Bio::Annotation::SimpleValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 ▶use Bio::Annotation::Relation;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use base qw(Bio::TreeIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90  
91 sub _initialize
92 15 ▶{
Subroutine _initialize: called CC=7 SCAR=20.2
93 15 my($self, %args) = @_;
94 15 ▶ $args{-treetype} ||= 'Bio::Tree::Tree';
Condition50%1/2
$args{"-treetype"} ||= "Bio::Tree::Tree"
l!l
015
MC/DC0%0/1
$args{"-treetype"} ||= "Bio::Tree::Tree"
$args{"-treetype"}
Truth table50%1/2
A
A= $args{"-treetype"}
Aresult
11
00
95 15 ▶ $args{-nodetype} ||= 'Bio::Tree::AnnotatableNode';
Condition50%1/2
$args{"-nodetype"} ||= "Bio::Tree::AnnotatableNode"
l!l
015
MC/DC0%0/1
$args{"-nodetype"} ||= "Bio::Tree::AnnotatableNode"
$args{"-nodetype"}
Truth table50%1/2
A
A= $args{"-nodetype"}
Aresult
11
00
96 15 $self->SUPER::_initialize(%args);
97  
98 # phyloxml TreeIO does not use SAX,
99 # therefore no need to attach EventHandler
100 # instead we will define a reader that is a pull-parser of libXML
101 15 ▶ if ($self->mode eq 'r') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->mode eq 'r') { } 1 14 15
elsif ($self->mode eq 'w') { } 14 0 14
102 1 ▶ if ($self->_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_fh) 1 0 1
103 1 $self->{'_reader'} = XML::LibXML::Reader->new(
104 IO => $self->_fh,
105 no_blanks => 1
106 );
107 }
108 1 ▶ if (!$self->{'_reader'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_reader"}) 0 1 1
109 0 $self->throw("XML::LibXML::Reader not initialized");
110 }
111 }
112 elsif ($self->mode eq 'w') {
113 # print default lines
114 14 $self->_print('<?xml version="1.0" encoding="UTF-8"?>',"\n");
115 14 $self->_print('<phyloxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.phyloxml.org" xsi:schemaLocation="http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd">');
116 }
117  
118 15 $self->treetype($args{-treetype});
119 15 $self->nodetype($args{-nodetype});
120 15 $self->{'_lastitem'} = {}; # holds open items and the attribute hash
121 15 $self->_init_func();
122 }
123  
124 sub _init_func
125 15 ▶{
Subroutine _init_func: called CC=1 SCAR=0.0
126 15 my ($self) = @_;
127 15 my %start_elements = (
128 'phylogeny' => \&element_phylogeny,
129 'clade' => \&element_clade,
130 'sequence_relation' => \&element_relation,
131 'clade_relation' => \&element_relation,
132 );
133 15 $self->{'_start_elements'} = \%start_elements;
134 15 my %end_elements = (
135 'phylogeny' => \&end_element_phylogeny,
136 'clade' => \&end_element_clade,
137 'sequence_relation' => \&end_element_relation,
138 'clade_relation' => \&end_element_relation,
139 );
140 15 $self->{'_end_elements'} = \%end_elements;
141 }
142  
143 15 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=14.5
144 15 my $self = shift;
145 15 ▶ if ($self->mode eq 'w') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "w") 14 1 15
146 14 $self->_print('</phyloxml>');
147 14 ▶ $self->flush if $self->_flush_on_write && defined $self->_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_flush_on_write and defined $self->_fh 14 0 14
Condition33%1/3
$self->_flush_on_write and defined $self->_fh
!ll&&!rl&&r
0014
MC/DC0%0/2
$self->_flush_on_write and defined $self->_fh
$self->_flush_on_write defined $self->_fh
Truth table33%1/3
A and B
A= $self->_flush_on_writeB= defined $self->_fh
ABresult
0X0
100
111
148 }
149 15 $self->SUPER::DESTROY;
150 }
151  
152 =head2 next_tree
153  
154 Title : next_tree
155 Usage : my $tree = $treeio->next_tree
156 Function: Gets the next tree in the stream
157 Returns : Bio::Tree::TreeI
158 Args : none
159  
160 =cut
161  
162 sub next_tree
163 13 ▶{
Subroutine next_tree: called CC=4 SCAR=13.9
Pod documented
164 13 my ($self) = @_;
165 13 my $reader = $self->{'_reader'};
166 13 my $tree;
167 13 ▶ while ($reader->read)
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->read) 710 0 710
168 {
169 710 ▶ if ($reader->nodeType == XML_READER_TYPE_END_ELEMENT)
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->nodeType == 15) 273 437 710
170 {
171 273 ▶ if ($reader->name eq 'phylogeny')
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->name eq "phylogeny") 13 260 273
172 {
173 13 $tree = $self->end_element_phylogeny();
174 13 last;
175 }
176 }
177 697 $self->processXMLNode;
178 }
179 13 return $tree;
180 }
181  
182 =head2 add_attribute
183  
184 Title : add_phyloXML_annotation
185 Usage : my $node = $treeio->add_phyloXML_annotation(-obj=>$node, -attr=>"id_source = \"A\"")
186 Function: add attributes to an object
187 Returns : the node that we added annotations to
188 Args : -obj => object that will have the Annotation. (Bio::Tree::AnnotatableNode)
189 -attr => string in the form "A = B", where A is the attribute name and B is the attribute value
190  
191 =cut
192  
193 sub add_attribute
194 2 ▶{
Subroutine add_attribute: called CC=10 SCAR=23.6
Pod documented
195 2 my ($self, @args) = @_;
196 2 my ($obj, $attr) = $self->_rearrange([qw(OBJ ATTR)], @args);
197  
198 2 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 2 0 2
199 2 $attr = '<dummy '.$attr.'/>';
200 }
201
202 2 my $oldreader = $self->{'_reader'}; # save reader
203 2 $self->{'_reader'} = XML::LibXML::Reader->new(
204 string => $attr,
205 no_blanks => 1
206 );
207 2 my $reader = $self->{'_reader'};
208 2 $self->{'_currentannotation'} = []; # holds annotationcollection
209 2 $self->{'_currenttext'} = '';
210 #$self->{'_id_link'} = {};
211  
212 # pretend we saw a <clade> element
213 2 $self->{'_lastitem'}->{'dummy'}++;
214 2 push @{$self->{'_lastitem'}->{'current'}}, { 'dummy'=>{}}; # current holds current element and empty hash for its attributes
215  
216 # push object to annotate
217 2 push @{$self->{'_currentitems'}}, $obj;
218  
219 # read attributes of element
220 2 ▶ while ($reader->read)
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->read) 2 2 4
221 {
222 #$self->processXMLNode;
223 2 $self->processAttribute($self->current_attr);
224 }
225  
226 # if there is id_source add sequence to _id_link
227 2 ▶ if (exists $self->current_attr->{'id_source'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->current_attr->{"id_source"}) 2 0 2
228 2 my $idsrc = $self->current_attr->{'id_source'};
229 2 $self->{'_id_link'}->{$idsrc} = $obj;
230 }
231  
232 # check idref
233 2 my $idref = '';
234 2 ▶ if (exists $self->current_attr->{'id_ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->current_attr->{"id_ref"}) 0 2 2
235 0 $idref = $self->current_attr->{'id_ref'};
236 }
237  
238 2 my $srcbyidref = '';
239 2 $srcbyidref = $self->{'_id_link'}->{$idref};
240  
241 # exception when id_ref is defined but id_src is not, or vice versa.
242 2 ▶ if ($idref xor $srcbyidref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idref xor $srcbyidref) 0 2 2
Condition25%1/4
$idref xor $srcbyidref
l&&rl&&!r!l&&r!l&&!r
0002
MC/DC0%0/2
$idref xor $srcbyidref
$idref $srcbyidref
Truth table25%1/4
A xor B
A= $idrefB= $srcbyidref
ABresult
110
101
011
000
243 0 $self->throw("id_ref and id_src incompatible: $idref, $srcbyidref");
244 }
245  
246 # if attribute exists then add Annotation::Collection with tag '_attr'
247 2 my $newac = $obj->annotation;
248 2 ▶ if ( scalar keys %{$self->current_attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->current_attr;}) 2 0 2
249 2 my $newattr = Bio::Annotation::Collection->new();
250 2 foreach my $tag (keys %{$self->current_attr}) {
251 my $sv = Bio::Annotation::SimpleValue->new(
252 2 -value => $self->current_attr->{$tag}
253 );
254 2 $newattr->add_Annotation($tag, $sv);
255 }
256 2 $newac->add_Annotation('_attr', $newattr);
257 }
258  
259 # pop from temporary list
260 2 pop @{$self->{'_currentitems'}};
261 2 ▶ $self->{'_lastitem'}->{ $reader->name }-- if $reader->name;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->name 0 2 2
262 2 pop @{$self->{'_lastitem'}->{'current'}};
263  
264 2 $self->{'_reader'} = $oldreader; # restore reader
265 2 return $obj;
266  
267 }
268  
269 =head2 add_phyloXML_annotation
270  
271 Title : add_phyloXML_annotation
272 Usage : my $node = $treeio->add_phyloXML_annotation(-obj=>$node, -xml=>$xmlstring)
273 my $tree = $treeio->add_phyloXML_annotation('-obj'=>$tree, '-xml'=>'<sequence_relation id_ref_0="A" id_ref_1="B" type="orthology"/>')
274  
275 Function: add annotations to a node in the phyloXML format string
276 Returns : the node that we added annotations to
277 Args : -obj => object that will have the Annotation. (Bio::Tree::AnnotatableNode)
278 -xml => string in phyloXML format that describes the annotation for the node
279  
280 =cut
281  
282 sub add_phyloXML_annotation
283 2 ▶{
Subroutine add_phyloXML_annotation: called CC=3 SCAR=11.0
Pod documented
284 2 my ($self, @args) = @_;
285 2 my ($obj, $xml_string) = $self->_rearrange([qw(OBJ XML)], @args);
286
287 2 $xml_string = '<phyloxml>'.$xml_string.'</phyloxml>';
288 2 $self->debug( $xml_string );
289  
290 2 my $oldreader = $self->{'_reader'}; # save reader
291 2 $self->{'_reader'} = XML::LibXML::Reader->new(
292 string => $xml_string,
293 no_blanks => 1
294 );
295 2 my $reader = $self->{'_reader'};
296 #$self->{'_currentannotation'} = []; # holds annotationcollection
297 #$self->{'_currenttext'} = '';
298 #$self->{'_id_link'} = {};
299  
300 # pretend we saw a <clade> element
301 2 $self->{'_lastitem'}->{'clade'}++;
302 2 push @{$self->{'_lastitem'}->{'current'}}, { 'clade'=>{}}; # current holds current element and empty hash for its attributes
303 # our object to annotate (nodeI)
304 # push into temporary list
305 2 push @{$self->{'_currentitems'}}, $obj;
306  
307 2 $reader->read; #read away the first element 'phyloxml'
308 2 ▶ while ($reader->read)
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->read) 14 2 16
309 {
310 14 $self->processXMLNode;
311 }
312  
313 # pop from temporary list
314 2 pop @{$self->{'_currentitems'}};
315 2 ▶ $self->{'_lastitem'}->{ $reader->name }-- if $reader->name;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->name 0 2 2
316 2 pop @{$self->{'_lastitem'}->{'current'}};
317
318 2 $self->{'_reader'} = $oldreader; # restore reader
319 2 return $obj;
320 }
321  
322  
323 =head2 write_tree
324  
325 Title : write_tree
326 Usage : $treeio->write_tree($tree);
327 Function: Write a tree out to data stream in phyloxml format
328 Returns : none
329 Args : Bio::Tree::TreeI object
330  
331 =cut
332  
333 sub write_tree
334 14 ▶{
Subroutine write_tree: called CC=11 SCAR=24.1
Pod documented
335 14 my ($self, @trees) = @_;
336 14 foreach my $tree (@trees) {
337 14 my $root = $tree->get_root_node;
338 14 $self->_print("<phylogeny");
339 14 my @tags = $tree->get_all_tags();
340 14 my $attr_str = '';
341 14 foreach my $tag (@tags) {
342 29 my @values = $tree->get_tag_values($tag);
343 29 foreach (@values) {
344 29 $attr_str .= " ".$tag."=\"".$_."\"";
345 }
346 }
347 # check if rooted
348 14 my ($b_rooted) = $tree->get_tag_values('rooted');
349 14 ▶ if ($b_rooted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b_rooted) { } 13 1 14
350 13 $attr_str .= " rooted=\"true\"";
351 }
352 else {
353 1 ▶ if($tree->is_binary($tree->get_root_node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->is_binary($tree->get_root_node)) { } 0 1 1
354 0 $attr_str .= " rooted=\"true\"";
355 }
356 else {
357 1 $attr_str .= " rooted=\"false\"";
358 }
359 }
360 14 $self->_print($attr_str);
361 14 $self->_print(">");
362 14 ▶ if ($root->isa('Bio::Tree::AnnotatableNode')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root->isa('Bio::Tree::AnnotatableNode')) { } 13 1 14
363 13 $self->_print($self->_write_tree_Helper_annotatableNode($root));
364 }
365 else {
366 1 $self->_print($self->_write_tree_Helper_generic($root));
367 }
368  
369 # print clade relations
370 14 ▶ while (my $str = pop (@{$self->{'_tree_attr'}->{'clade_relation'}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $str = pop @{$$self{"_tree_attr"}{"clade_relation"};}) 2 14 16
371 2 $self->_print($str);
372 }
373 # print sequence relations
374 14 ▶ while (my $str = pop (@{$self->{'_tree_attr'}->{'sequence_relation'}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $str = pop @{$$self{"_tree_attr"}{"sequence_relation"};}) 8 14 22
375 8 $self->_print($str);
376 }
377 14 $self->_print("</phylogeny>");
378 }
379 14 ▶ $self->flush if $self->_flush_on_write && defined $self->_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_flush_on_write and defined $self->_fh 14 0 14
Condition33%1/3
$self->_flush_on_write and defined $self->_fh
!ll&&!rl&&r
0014
MC/DC0%0/2
$self->_flush_on_write and defined $self->_fh
$self->_flush_on_write defined $self->_fh
Truth table33%1/3
A and B
A= $self->_flush_on_writeB= defined $self->_fh
ABresult
0X0
100
111
380 14 return;
381 }
382  
383 =head2 _write_tree_Helper_annotatableNode
384  
385 Title : _write_tree_Helper_annotatableNode
386 Usage : internal method used by write_tree, not to be used directly
387 Function: recursive helper function of write_tree for the annotatableNodes.
388 translates annotations into xml elements.
389 Returns : string describing the node
390 Args : Bio::Node::AnnotatableNode object, string
391  
392 =cut
393  
394 sub _write_tree_Helper_annotatableNode
395 66 ▶{
Subroutine _write_tree_Helper_annotatableNode: called CC=8 SCAR=20.8
396 66 my ($self, $node, $str) = @_; # this self is a Bio::Tree::phyloxml
397
398 66 my $ac = $node->annotation;
399  
400 # if clade_relation exists
401 66 my @relations = $ac->get_Annotations('clade_relation');
402 66 foreach (@relations) {
403 2 my $clade_rel = $self->_relation_to_string($node, $_, '');
404 # set as tree attr
405 2 push (@{$self->{'_tree_attr'}->{'clade_relation'}}, $clade_rel);
406 }
407  
408 # start <clade>
409 66 $str .= '<clade';
410 66 my ($attr) = $ac->get_Annotations('_attr'); # check id_source
411 66 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 66 0 66
412 66 my ($id_source) = $attr->get_Annotations('id_source');
413 66 ▶ if ($id_source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id_source) 7 59 66
414 7 $str .= " id_source=\"".$id_source->value."\"";
415 }
416 }
417 66 $str .= ">";
418  
419 # print all descendent nodes
420 66 foreach my $child ( $node->each_Descendent() ) {
421 53 $str = $self->_write_tree_Helper_annotatableNode($child, $str);
422 }
423  
424 # print all annotations
425 66 $str = print_annotation( $node, $str, $ac );
426  
427 # print all sequences
428 66 ▶ if ($node->has_sequence) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->has_sequence) 9 57 66
429 9 foreach my $seq (@{$node->sequence}) {
430 # if sequence_relation exists
431 9 my @relations = $seq->annotation->get_Annotations('sequence_relation');
432 9 foreach (@relations) {
433 8 my $sequence_rel = $self->_relation_to_string($seq, $_, '');
434 # set as tree attr
435 8 push (@{$self->{'_tree_attr'}->{'sequence_relation'}}, $sequence_rel);
436 }
437 9 $str = print_seq_annotation( $node, $str, $seq );
438 }
439 }
440  
441 66 $str .= "</clade>";
442  
443 66 return $str;
444 }
445  
446 =head2 _write_tree_Helper_generic
447  
448 Title : _write_tree_Helper_generic
449 Usage : internal method used by write_tree, not to be used directly
450 Function: recursive helper function of write_tree for generic NodesI.
451 all tags are translated into property elements.
452 Returns : string describing the node
453 Args : Bio::Node::NodeI object, string
454  
455 =cut
456  
457 sub _write_tree_Helper_generic
458 12 ▶{
Subroutine _write_tree_Helper_generic: called CC=7 SCAR=19.5
459 12 my ($self, $node, $str) = @_; # this self is a Bio::Tree::phyloxml
460
461 # start <clade>
462 12 $str .= '<clade>';
463  
464 # print all descendent nodes
465 12 foreach my $child ( $node->each_Descendent() ) {
466 11 $str = $self->_write_tree_Helper_generic($child, $str);
467 }
468  
469 # print all tags
470 12 my @tags = $node->get_all_tags();
471 12 foreach my $tag (@tags) {
472 26 my @values = $node->get_tag_values($tag);
473 26 foreach my $val (@values) {
474 26 $str .= "<property datatype=\"xsd:string\" ref=\"tag:$tag\" applies_to=\"clade\">";
475 26 $str .=$val;
476 26 $str .= "</property>";
477 }
478 }
479  
480 # print NodeI features
481 12 ▶ if ($node->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->id) 8 4 12
482 8 $str .= "<name>";
483 8 $str .= $node->id;
484 8 $str .= "</name>";
485 }
486 12 ▶ if ($node->branch_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->branch_length) 10 2 12
487 10 $str .= "<branch_length>";
488 10 $str .= $node->branch_length;
489 10 $str .= "</branch_length>";
490 }
491 12 ▶ if ($node->bootstrap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->bootstrap) 1 11 12
492 1 $str .= "<confidence type = \"bootstrap\">";
493 1 $str .= $node->bootstrap;
494 1 $str .= "</confidence>";
495 }
496  
497 12 $str .= "</clade>";
498 12 return $str;
499 }
500  
501 =head2 _relation_to_string
502  
503 Title : _relation_to_string
504 Usage : internal method used by write_tree, not to be used directly
505 Function: internal function used by write_tree to translate Annotation::Relation objects into xml elements.
506 Returns : string describing the node
507 Args : Bio::Node::AnnotatableNode (or Bio::SeqI) object that contains the Annotation::Relation,
508 the Annotation::Relation object,
509 the string
510  
511 =cut
512  
513 # It may be more appropriate to make Annotation::Relation have
514 # a to_string callback function,
515 # and have this subroutine set as the callback when we are in
516 # phyloXML context.
517 # I've put it here for now, since write_tree is the only place it is used.
518  
519 10 ▶sub _relation_to_string {
Subroutine _relation_to_string: called CC=4 SCAR=16.4
520 10 my ($self, $obj, $rel, $str) = @_;
521  
522 10 my @attr = $obj->annotation->get_Annotations('_attr'); # check id_source
523 10 ▶ if (@attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attr) 10 0 10
524 10 my @id_source = $attr[0]->get_Annotations('id_source');
525 }
526 10 my ($id_ref_0) = $obj->annotation->get_nested_Annotations(
527 '-keys' => ['id_source'],
528 '-recursive' => 1);
529 10 my ($id_ref_1) = $rel->to->annotation->get_nested_Annotations(
530 '-keys' => ['id_source'],
531 '-recursive' => 1);
532  
533 10 my $confidence = $rel->confidence();
534 10 my $confidence_type = $rel->confidence_type();
535 10 $str .= "<";
536 10 $str .= $rel->tagname;
537 10 $str .= " id_ref_0=\"".$id_ref_0->value."\"";
538 10 $str .= " id_ref_1=\"".$id_ref_1->value."\"";
539 10 $str .= " type=\"".$rel->type."\"";
540 10 ▶ if ($confidence) {
Branch: 1 branch
BranchTrueFalseTotal
if ($confidence) { } 0 10 10
541 0 $str .= " ><confidence";
542 0 ▶ if ($confidence_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($confidence_type) 0 0 0
543 0 $str .= " type=\"".$confidence_type."\"";
544 }
545 0 $str .= ">";
546 0 $str .= $confidence;
547 0 $str .= "</confidence>";
548 0 $str .= "</";
549 0 $str .= $rel->tagname;
550 0 $str .= ">";
551 }
552 else {
553 10 $str .= "/>";
554 }
555 10 return $str;
556 }
557  
558 =head2 read_annotation
559  
560 Title : read_annotation
561 Usage : $treeio->read_annotation(-obj=>$node, -path=>$path, -attr=>1);
562 Function: read text value (or attribute value) of the annotations corresponding to the element path
563 Returns : list of text values of the annotations matching the path
564 Args : -obj => object that contains the Annotation. (Bio::Tree::AnnotatableNode or Bio::SeqI)
565 -path => path of the nested elements
566 -attr => Boolean value to indicate whether to get the attribute of the element or the text value.
567 (default is 0, meaning text value is returned)
568  
569 =cut
570  
571 # It may be more appropriate to make a separate Annotation::phyloXML object
572 # and have this subroutine within that object so it can handle the
573 # reading and writing of the values and attributes.
574 # but since tagTree is a temporary stub and I didn't want to make
575 # a redundant object I've put it here for now.
576  
577 sub read_annotation
578 12 ▶{
Subroutine read_annotation: called CC=2 SCAR=6.9
Pod documented
579 12 my ($self, @args) = @_;
580 12 my ($obj, $path, $attr) = $self->_rearrange([qw(OBJ PATH ATTR)], @args);
581 12 my $ac = $obj->annotation;
582 12 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) { } 4 8 12
583 4 my @elements = split ('/', $path);
584 4 my $final = pop @elements;
585 4 push (@elements, '_attr');
586 4 push (@elements, $final);
587 4 $path = join ('/', @elements);
588 4 return $self->_read_annotation_attr_Helper( [$ac], $path);
589 }
590 else {
591 8 return $self->_read_annotation_text_Helper( [$ac], $path);
592 }
593 }
594  
595 sub _read_annotation_text_Helper
596 18 ▶{
Subroutine _read_annotation_text_Helper: called CC=7 SCAR=19.5
597 18 my ($self, $acs, $path) = @_;
598 18 my @elements = split ('/', $path);
599 18 my $key = shift @elements;
600 18 my @nextacs = ();
601 18 foreach my $ac (@$acs) {
602 18 foreach my $ann ($ac->get_Annotations($key)) {
603 18 ▶ if ($ann->isa('Bio::AnnotationCollectionI')) {push (@nextacs, $ann)}
Branch: 1 branch
BranchTrueFalseTotal
if ($ann->isa("Bio::AnnotationCollectionI")) 18 0 18
604 }
605 }
606 18 ▶ if (@elements == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elements == 0) { } 8 10 18
607 8 my @values = ();
608 8 my @texts = map {$_->get_Annotations('_text')} @nextacs;
609 8 foreach (@texts) {
610 8 ▶ $_ && push (@values, $_->value);
Branch: 1 branch
BranchTrueFalseTotal
$_ and push @values, $_->value 8 0 8
611 }
612 8 return @values;
613 }
614 else {
615 10 $path = join ('/', @elements);
616 10 return $self->_read_annotation_text_Helper( \@nextacs, $path);
617 }
618 }
619  
620 sub _read_annotation_attr_Helper
621 8 ▶{
Subroutine _read_annotation_attr_Helper: called CC=5 SCAR=16.1
622 8 my ($self, $acs, $path) = @_;
623 8 my @elements = split ('/', $path);
624 8 my $key = shift @elements;
625 8 my @nextacs = ();
626 8 foreach my $ac (@$acs) {
627 8 foreach my $ann ($ac->get_Annotations($key)) {
628 8 ▶ if ($ann->isa('Bio::AnnotationCollectionI')) {push (@nextacs, $ann)}
Branch: 1 branch
BranchTrueFalseTotal
if ($ann->isa("Bio::AnnotationCollectionI")) 8 0 8
629 }
630 }
631 8 ▶ if (@elements == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elements == 1) { } 4 4 8
632 4 my $attrname = $elements[0];
633 4 my @sv = map {$_->get_Annotations($attrname)} @nextacs;
634 4 return map {$_->value} @sv;
635 }
636 else {
637 4 $path = join ('/', @elements);
638 4 return $self->_read_annotation_attr_Helper( \@nextacs, $path);
639 }
640 }
641  
642 =head1 Methods for parsing the XML document
643  
644 =cut
645  
646 =head2 processXMLNode
647  
648 Title : processXMLNode
649 Usage : $treeio->processXMLNode
650 Function: read the XML node and process according to the node type
651 Returns : none
652 Args : none
653  
654 =cut
655  
656 sub processXMLNode
657 711 ▶{
Subroutine processXMLNode: called CC=7 SCAR=19.5
Pod documented
658 711 my ($self) = @_;
659 711 my $reader = $self->{'_reader'};
660 711 my $nodetype = $reader->nodeType;
661 711 ▶ if ( $nodetype == XML_READER_TYPE_ELEMENT)
Branch: 1 branch
BranchTrueFalseTotal
if ($nodetype == 1) 290 421 711
662 {
663 290 $self->{'_lastitem'}->{$reader->name}++;
664 290 push @{$self->{'_lastitem'}->{'current'}}, { $reader->name=>{}}; # current holds current element and empty hash for its attributes
665  
666 290 ▶ if (exists $self->{'_start_elements'}->{$reader->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_start_elements'}{$reader->name}) { } 84 206 290
667 84 my $method = $self->{'_start_elements'}->{$reader->name};
668 84 $self->$method();
669 }
670 else {
671 206 $self->element_default();
672 }
673 290 ▶ if ($reader->isEmptyElement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->isEmptyElement) 12 278 290
674 # element is complete
675 # set nodetype so it can jump and
676 # do procedures for XML_READER_TYPE_END_ELEMENT
677 12 $nodetype = XML_READER_TYPE_END_ELEMENT;
678 }
679 }
680 711 ▶ if ($nodetype == XML_READER_TYPE_TEXT)
Branch: 1 branch
BranchTrueFalseTotal
if ($nodetype == 3) 155 556 711
681 {
682 155 $self->{'_currenttext'} = $reader->value;
683 }
684 711 ▶ if ($nodetype == XML_READER_TYPE_END_ELEMENT)
Branch: 1 branch
BranchTrueFalseTotal
if ($nodetype == 15) 278 433 711
685 {
686 278 ▶ if (exists $self->{'_end_elements'}->{$reader->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_end_elements'}{$reader->name}) { } 71 207 278
687 71 my $method = $self->{'_end_elements'}->{$reader->name};
688 71 $self->$method();
689 }
690 else {
691 207 $self->end_element_default();
692 }
693 278 $self->{'_lastitem'}->{ $reader->name }--;
694 278 pop @{$self->{'_lastitem'}->{'current'}};
695 278 $self->{'_currenttext'} = '';
696 }
697 }
698  
699  
700 =head2 processAttribute
701  
702 Title : processAttribute
703 Usage : $treeio->processAttribute(\%hash_for_attribute);
704 Function: reads the attributes of the current element into a hash
705 Returns : none
706 Args : hash reference where the attributes will be stored.
707  
708 =cut
709  
710 sub processAttribute
711 292 ▶{
Subroutine processAttribute: called CC=3 SCAR=11.0
Pod documented
712 292 my ($self, $data) = @_;
713 292 my $reader = $self->{'_reader'};
714  
715 # several ways of reading attributes:
716 # read all attributes:
717 292 ▶ if ($reader-> moveToFirstAttribute) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->moveToFirstAttribute) 78 214 292
718 78 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$reader->moveToNextAttribute and do { do { $$data{$reader->name} = $reader->value } } 37 78 115
719 115 $data->{$reader->name()} = $reader->value;
720 } while ($reader-> moveToNextAttribute);
721 78 $reader-> moveToElement;
722 }
723 }
724  
725  
726 =head2 element_phylogeny
727  
728 Title : element_phylogeny
729 Usage : $treeio->element_phylogeny
730 Function: initialize the parsing of a tree
731 Returns : none
732 Args : none
733  
734 =cut
735  
736 sub element_phylogeny
737 13 ▶{
Subroutine element_phylogeny: called CC=1 SCAR=0.0
Pod documented
738 13 my ($self) = @_;
739 13 $self->{'_currentitems'} = []; # holds nodes while parsing current level
740 13 $self->{'_currentnodes'} = []; # holds nodes while constructing tree
741 13 $self->{'_currentannotation'} = []; # holds annotationcollection
742 13 $self->{'_currenttext'} = '';
743 13 $self->{'_levelcnt'} = [];
744 13 $self->{'_id_link'} = {};
745 13 $self->{'_tree_attr'} = $self->current_attr;
746 13 $self->processAttribute($self->current_attr);
747 13 return;
748 }
749  
750 =head2 end_element_phylogeny
751  
752 Title : end_element_phylogeny
753 Usage : $treeio->end_element_phylogeny
754 Function: ends the parsing of a tree building a Tree::TreeI object.
755 Returns : Tree::TreeI
756 Args : none
757  
758 =cut
759  
760 sub end_element_phylogeny
761 13 ▶{
Subroutine end_element_phylogeny: called CC=5 SCAR=18.9
Pod documented
762 13 my ($self) = @_;
763  
764 13 my $root;
765 # if there is more than one node in _currentnodes
766 # aggregate the nodes into trees basically ad-hoc.
767 13 ▶ if ( @{$self->{'_currentnodes'}} > 1)
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->{'_currentnodes'};} > 1) { } 0 13 13
elsif (@{$self->{'_currentnodes'};} == 1) { } 13 0 13
768 {
769 0 $root = $self->nodetype->new(
770 -id => '',
771 tostring => \&node_to_string,
772 );
773 0 ▶ while ( @{$self->{'_currentnodes'}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_currentnodes"};}) 0 0 0
774 0 my ($node) = ( shift @{$self->{'_currentnodes'}});
775 0 $root->add_Descendent($node);
776 }
777 }
778 # if there is only one node in _currentnodes
779 # that node is root.
780 13 elsif ( @{$self->{'_currentnodes'}} == 1)
781 {
782 13 $root = shift @{$self->{'_currentnodes'}};
783 }
784  
785 my $tree = $self->treetype->new(
786 -root => $root,
787 -id => $self->current_attr->{'name'},
788 13 %{$self->current_attr}
789 );
790 13 foreach my $tag ( keys %{$self->current_attr} ) {
791 29 $tree->add_tag_value( $tag, $self->current_attr->{$tag} );
792 }
793 13 return $tree;
794 }
795  
796 =head2 element_clade
797  
798 Title : element_clade
799 Usage : $treeio->element_clade
800 Function: initialize the parsing of a node
801 creates a new AnnotatableNode with annotations
802 Returns : none
803 Args : none
804  
805 =cut
806  
807 sub element_clade
808 66 ▶{
Subroutine element_clade: called CC=3 SCAR=11.0
Pod documented
809 66 my ($self) = @_;
810 66 my $reader = $self->{'_reader'};
811 66 my %clade_attr = (); # doesn't use current attribute in order to save memory
812 66 $self->processAttribute(\%clade_attr);
813 # create a node (Annotatable Node)
814 66 my $tnode = $self->nodetype->new(
815 -id => '',
816 tostring => \&node_to_string,
817 %clade_attr,
818 );
819 # add all attributes as annotation collection with tag '_attr'
820 66 my $ac = $tnode->annotation;
821 66 my $newattr = Bio::Annotation::Collection->new();
822 66 foreach my $tag (keys %clade_attr) {
823 my $sv = Bio::Annotation::SimpleValue->new(
824 19 -value => $clade_attr{$tag}
825 );
826 19 $newattr->add_Annotation($tag, $sv);
827 }
828 66 $ac->add_Annotation('_attr', $newattr);
829
830 # if there is id_source add clade to _id_link
831 66 ▶ if (exists $clade_attr{'id_source'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $clade_attr{"id_source"}) 7 59 66
832 7 $self->{'_id_link'}->{$clade_attr{'id_source'}} = $tnode;
833 }
834 # push into temporary list
835 66 push @{$self->{'_currentitems'}}, $tnode;
836 }
837  
838 =head2 end_element_clade
839  
840 Title : end_element_clade
841 Usage : $treeio->end_element_clade
842 Function: ends the parsing of a node
843 Returns : none
844 Args : none
845  
846 =cut
847  
848 sub end_element_clade
849 66 ▶{
Subroutine end_element_clade: called CC=5 SCAR=16.1
Pod documented
850 66 my ($self) = @_;
851 66 my $reader = $self->{'_reader'};
852  
853 66 my $curcount = scalar @{$self->{'_currentnodes'}};
854 66 my $level = $reader->depth() - 2;
855 66 ▶ my $childcnt = $self->{'_levelcnt'}->[$level+1] || 0;
Condition100%2/2
$self->{'_levelcnt'}[$level + 1] || 0
l!l
2640
MC/DC100%1/1
$self->{'_levelcnt'}[$level + 1] || 0
$self->{'_levelcnt'}[$level + 1]
Truth table100%2/2
A
A= $self->{'_levelcnt'}[$level + 1]
Aresult
11
00
856  
857 # pop from temporary list
858 66 my $tnode = pop @{$self->{'_currentitems'}};
859 66 ▶ if ( $childcnt > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($childcnt > 0) 26 40 66
860 26 ▶ if( $childcnt > $curcount)
Branch: 1 branch
BranchTrueFalseTotal
if ($childcnt > $curcount) 0 26 26
861 {
862 0 $self->throw("something wrong with event construction treelevel ".
863 "$level is recorded as having $childcnt nodes ".
864 "but current nodes at this level is $curcount\n");
865 }
866 26 my @childnodes = splice( @{$self->{'_currentnodes'}}, - $childcnt);
867 26 for ( @childnodes ) {
868 53 $tnode->add_Descendent($_);
869 }
870 26 $self->{'_levelcnt'}->[$level+1] = 0;
871 }
872 66 push @{$self->{'_currentnodes'}}, $tnode;
873 66 $self->{'_levelcnt'}->[$level]++;
874  
875 }
876  
877 =head2 element_relation
878  
879 Title : element_relation
880 Usage : $treeio->element_relation
881 Function: starts the parsing of clade relation & sequence relation
882 Returns : none
883 Args : none
884  
885 =cut
886  
887 sub element_relation
888 5 ▶{
Subroutine element_relation: called CC=5 SCAR=17.6
Pod documented
889 5 my ($self) = @_;
890 5 $self->processAttribute($self->current_attr);
891 5 my $relationtype = $self->current_attr->{'type'};
892 5 my $id_ref_0 = $self->current_attr->{'id_ref_0'};
893 5 my $id_ref_1 = $self->current_attr->{'id_ref_1'};
894
895 5 my @srcbyidref = ();
896 5 $srcbyidref[0] = $self->{'_id_link'}->{$id_ref_0};
897 5 $srcbyidref[1] = $self->{'_id_link'}->{$id_ref_1};
898
899 # exception when id_ref is defined but id_src is not, or vice versa.
900 5 ▶ if ( ($id_ref_0 xor $srcbyidref[0])||($id_ref_1 xor $srcbyidref[1]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id_ref_0 xor $srcbyidref[0] or ($id_ref_1 xor $srcbyidref[1])) 0 5 5
Condition27%3/11
$id_ref_0 xor $srcbyidref[0] or ($id_ref_1 xor $srcbyidref[1])
l!l&&r!l&&!r
005
$id_ref_0 xor $srcbyidref[0]
l&&rl&&!r!l&&r!l&&!r
5000
$id_ref_1 xor $srcbyidref[1]
l&&rl&&!r!l&&r!l&&!r
5000
MC/DC0%0/4
$id_ref_0 xor $srcbyidref[0] or ($id_ref_1 xor $srcbyidref[1])
$id_ref_0 $srcbyidref[0] $id_ref_1 $srcbyidref[1]
Truth table10%1/10
A xor B or C xor D
A= $id_ref_0B= $srcbyidref[0]C= $id_ref_1D= $srcbyidref[1]
ABCDresult
10XX1
01XX1
11101
11011
00101
00011
11110
11000
00110
00000
901 0 $self->throw("id_ref and id_src incompatible: $id_ref_0, $id_ref_1, ", $srcbyidref[0], $srcbyidref[1]);
902 }
903  
904 # set id_ref_0
905 5 my $ac0 = $srcbyidref[0]->annotation;
906 5 my $newann = Bio::Annotation::Relation->new(
907 '-type' => $relationtype,
908 '-to' => $srcbyidref[1],
909 '-tagname' => $self->current_element
910 );
911 5 $ac0->add_Annotation($self->current_element, $newann);
912 # set id_ref_1
913 5 my $ac1 = $srcbyidref[1]->annotation;
914 5 $newann = Bio::Annotation::Relation->new(
915 '-type' => $relationtype,
916 '-to' => $srcbyidref[0],
917 '-tagname' => $self->current_element
918 );
919 5 $ac1->add_Annotation($self->current_element, $newann);
920 5 push (@{$self->{'_currentannotation'}}, $newann);
921 }
922  
923 =head2 end_element_relation
924  
925 Title : end_element_relation
926 Usage : $treeio->end_element_relation
927 Function: ends the parsing of clade relation & sequence relation
928 Returns : none
929 Args : none
930  
931 =cut
932  
933 sub end_element_relation
934 5 ▶{
Subroutine end_element_relation: called CC=1 SCAR=0.0
Pod documented
935 5 my ($self) = @_;
936 5 my $ac = pop (@{$self->{'_currentannotation'}});
937 }
938  
939  
940 =head2 element_default
941  
942 Title : element_default
943 Usage : $treeio->element_default
944 Function: starts the parsing of all other elements
945 Returns : none
946 Args : none
947  
948 =cut
949  
950 sub element_default
951 206 ▶{
Subroutine element_default: called CC=12 SCAR=25.3
Pod documented
952 206 my ($self) = @_;
953 206 my $reader = $self->{'_reader'};
954 206 my $current = $self->current_element();
955 206 my $prev = $self->prev_element();
956
957 # read attributes of element
958 206 $self->processAttribute($self->current_attr);
959  
960 # check idref
961 206 my $idref = '';
962 206 ▶ if (exists $self->current_attr->{'id_ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->current_attr->{"id_ref"}) 3 203 206
963 3 $idref = $self->current_attr->{'id_ref'};
964 }
965
966 206 my $srcbyidref = '';
967 206 $srcbyidref = $self->{'_id_link'}->{$idref};
968  
969 # exception when id_ref is defined but id_src is not, or vice versa.
970 206 ▶ if ($idref xor $srcbyidref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idref xor $srcbyidref) 0 206 206
Condition50%2/4
$idref xor $srcbyidref
l&&rl&&!r!l&&r!l&&!r
300203
MC/DC0%0/2
$idref xor $srcbyidref
$idref $srcbyidref
Truth table50%2/4
A xor B
A= $idrefB= $srcbyidref
ABresult
110
101
011
000
971 0 $self->throw("id_ref and id_src incompatible: $idref, $srcbyidref");
972 }
973
974 # we are annotating a Node
975 # set _currentannotation
976 206 ▶ if ( ($srcbyidref && $srcbyidref->isa($self->nodetype)) || ((!$srcbyidref) && $prev eq 'clade') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($srcbyidref and $srcbyidref->isa($self->nodetype) or not $srcbyidref and $prev eq 'clade') { } 84 122 206
elsif ($prev eq 'clade_relation' or $prev eq 'sequence_relation') { } 0 122 122
Condition77%7/9
$srcbyidref and $srcbyidref->isa($self->nodetype) or not $srcbyidref and $prev eq "clade"
l!l&&r!l&&!r
381122
$srcbyidref and $srcbyidref->isa($self->nodetype)
!ll&&!rl&&r
20303
not $srcbyidref and $prev eq "clade"
!ll&&!rl&&r
012281
MC/DC50%2/4
$srcbyidref and $srcbyidref->isa($self->nodetype) or not $srcbyidref and $prev eq "clade"
$srcbyidref $srcbyidref->isa($self->nodetype) not $srcbyidref $prev eq "clade"
Truth table42%3/7
A and B or not(C) and D
A= $srcbyidrefB= $srcbyidref->isa($self->nodetype)C= not $srcbyidrefD= $prev eq "clade"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
977 # find node to annotate
978 84 my $tnode;
979 84 ▶ if ($srcbyidref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($srcbyidref) { } 3 81 84
980 3 $tnode = $srcbyidref;
981 }
982 else {
983 81 $tnode = $self->{'_currentitems'}->[-1];
984 }
985 84 my $ac = $tnode->annotation();
986 # add the new anncollection with the current element as key
987 84 my $newann = Bio::Annotation::Collection->new();
988 84 $ac->add_Annotation($current, $newann);
989 # push to current annotation
990 84 push (@{$self->{'_currentannotation'}}, $newann);
991 }
992 # we are within sequence_relation or clade_relation
993 ▶ elsif ($prev eq 'clade_relation' || $prev eq 'sequence_relation') {
Condition33%1/3
$prev eq "clade_relation" or $prev eq "sequence_relation"
l!l&&r!l&&!r
00122
MC/DC0%0/2
$prev eq "clade_relation" or $prev eq "sequence_relation"
$prev eq "clade_relation" $prev eq "sequence_relation"
Truth table33%1/3
A or B
A= $prev eq "clade_relation"B= $prev eq "sequence_relation"
ABresult
1X1
011
000
994 # do nothing?
995 }
996 # we are already within an annotation
997 else {
998 122 my $ac = $self->{'_currentannotation'}->[-1];
999 122 ▶ if ($ac) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ac) 105 17 122
1000 # add the new anncollection with the current element as key
1001 105 my $newann = Bio::Annotation::Collection->new();
1002 105 $ac->add_Annotation($current, $newann);
1003 105 push (@{$self->{'_currentannotation'}}, $newann);
1004 }
1005 }
1006 }
1007  
1008  
1009 =head2 end_element_default
1010  
1011 Title : end_element_default
1012 Usage : $treeio->end_element_default
1013 Function: ends the parsing of all other elements
1014 Returns : none
1015 Args : none
1016  
1017 =cut
1018  
1019 sub end_element_default
1020 207 ▶{
Subroutine end_element_default: called CC=26 SCAR=36.1
Pod documented
1021 207 my ($self) = @_;
1022 207 my $reader = $self->{'_reader'};
1023 207 my $current = $self->current_element();
1024 207 my $prev = $self->prev_element();
1025
1026 # check idsrc
1027 207 my $idsrc = $self->current_attr->{'id_source'};
1028  
1029 # check idref
1030 207 my $idref = '';
1031 207 ▶ if (exists $self->current_attr->{'id_ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->current_attr->{"id_ref"}) 3 204 207
1032 3 $idref = $self->current_attr->{'id_ref'};
1033 3 delete $self->current_attr->{'id_ref'};
1034 }
1035
1036 207 my $srcbyidref = '';
1037 207 $srcbyidref = $self->{'_id_link'}->{$idref};
1038  
1039 # exception when id_ref is defined but id_src is not, or vice versa.
1040 207 ▶ if ($idref xor $srcbyidref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idref xor $srcbyidref) 0 207 207
Condition50%2/4
$idref xor $srcbyidref
l&&rl&&!r!l&&r!l&&!r
300204
MC/DC0%0/2
$idref xor $srcbyidref
$idref $srcbyidref
Truth table50%2/4
A xor B
A= $idrefB= $srcbyidref
ABresult
110
101
011
000
1041 0 $self->throw("id_ref and id_src incompatible: $idref, $srcbyidref");
1042 }
1043
1044 # we are annotating a Tree
1045 207 ▶ if ((!$srcbyidref) && $prev eq 'phylogeny') {
Branch: 3 branches
BranchTrueFalseTotal
if (not $srcbyidref and $prev eq 'phylogeny') { } 18 189 207
elsif ($prev eq 'clade_relation' or $prev eq 'sequence_relation') { } 0 189 189
elsif ($srcbyidref and $srcbyidref->isa($self->nodetype) or not $srcbyidref and $prev eq 'clade') { } 84 105 189
Condition100%3/3
not $srcbyidref and $prev eq "phylogeny"
!ll&&!rl&&r
318618
MC/DC100%2/2
not $srcbyidref and $prev eq "phylogeny"
not $srcbyidref $prev eq "phylogeny"
Truth table100%3/3
not(A) and B
A= not $srcbyidrefB= $prev eq "phylogeny"
ABresult
0X0
100
111
1046 # annotate Tree via tree attribute
1047 18 $self->prev_attr->{$current} = $self->{'_currenttext'};
1048 }
1049 # we are within sequence_relation or clade_relation
1050 ▶ elsif ($prev eq 'clade_relation' || $prev eq 'sequence_relation') {
Condition33%1/3
$prev eq "clade_relation" or $prev eq "sequence_relation"
l!l&&r!l&&!r
00189
MC/DC0%0/2
$prev eq "clade_relation" or $prev eq "sequence_relation"
$prev eq "clade_relation" $prev eq "sequence_relation"
Truth table33%1/3
A or B
A= $prev eq "clade_relation"B= $prev eq "sequence_relation"
ABresult
1X1
011
000
1051 0 my $ann_relation = $self->{'_currentannotation'}->[-1];
1052 # we are here only with <confidence>
1053 0 ▶ if ($current eq 'confidence') {
Branch: 1 branch
BranchTrueFalseTotal
if ($current eq 'confidence') { } 0 0 0
1054 0 ▶ if (exists $self->current_attr->{'type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->current_attr->{"type"}) 0 0 0
1055 0 $ann_relation->confidence_type($self->current_attr->{'type'});
1056 }
1057 0 $ann_relation->confidence($self->{'_currenttext'});
1058 }
1059 else {
1060 0 $self->throw($current, " is not allowed within <*_relation>");
1061 }
1062 }
1063 # we are annotating a Node
1064 ▶ elsif (( $srcbyidref && $srcbyidref->isa($self->nodetype)) || ((!$srcbyidref) && $prev eq 'clade'))
Condition77%7/9
$srcbyidref and $srcbyidref->isa($self->nodetype) or not $srcbyidref and $prev eq "clade"
l!l&&r!l&&!r
381105
$srcbyidref and $srcbyidref->isa($self->nodetype)
!ll&&!rl&&r
18603
not $srcbyidref and $prev eq "clade"
!ll&&!rl&&r
010581
MC/DC50%2/4
$srcbyidref and $srcbyidref->isa($self->nodetype) or not $srcbyidref and $prev eq "clade"
$srcbyidref $srcbyidref->isa($self->nodetype) not $srcbyidref $prev eq "clade"
Truth table42%3/7
A and B or not(C) and D
A= $srcbyidrefB= $srcbyidref->isa($self->nodetype)C= not $srcbyidrefD= $prev eq "clade"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1065 {
1066 # pop from current annotation
1067 84 my $ac = pop (@{$self->{'_currentannotation'}});
1068 84 $self->annotateNode( $current, $ac);
1069  
1070 # additional setups for compatibility with NodeI
1071 84 my $tnode;
1072 84 ▶ if ($srcbyidref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($srcbyidref) { } 3 81 84
1073 3 $tnode = $srcbyidref;
1074 }
1075 else {
1076 81 $tnode = $self->{'_currentitems'}->[-1];
1077 }
1078 84 ▶ if ($current eq 'name') {
Branch: 5 branches
BranchTrueFalseTotal
if ($current eq 'name') { } 37 47 84
elsif ($current eq 'branch_length') { } 4 43 47
elsif ($current eq 'confidence') { } 1 42 43
elsif ($current eq 'sequence') { } 9 33 42
elsif ($idsrc and $current eq 'taxonomy') { } 0 66 66
1079 37 $tnode->id($self->{'_currenttext'});
1080 }
1081 elsif ($current eq 'branch_length') {
1082 4 $tnode->branch_length($self->{'_currenttext'});
1083 }
1084 elsif ($current eq 'confidence') {
1085 1 ▶ if ((exists $self->current_attr->{'type'}) && ($self->current_attr->{'type'} eq 'bootstrap')) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->current_attr->{"type"} and $self->current_attr->{"type"} eq "bootstrap") 1 0 1
Condition33%1/3
exists $self->current_attr->{"type"} and $self->current_attr->{"type"} eq "bootstrap"
!ll&&!rl&&r
001
MC/DC0%0/2
exists $self->current_attr->{"type"} and $self->current_attr->{"type"} eq "bootstrap"
exists $self->current_attr->{"type"} $self->current_attr->{"type"} eq "bootstrap"
Truth table33%1/3
A and B
A= exists $self->current_attr->{"type"}B= $self->current_attr->{"type"} eq "bootstrap"
ABresult
0X0
100
111
1086 1 $tnode->bootstrap($self->{'_currenttext'}); # this needs to change (adds 'B' annotation)
1087 }
1088 }
1089 elsif ($current eq 'sequence') {
1090 # if annotation is <sequence>
1091 # transform the Bio::Annotation object into a Bio::Seq object
1092 9 my $str = '';
1093 # retrieve the sequence
1094 9 ▶ if (my ($molseq) = $ac->get_Annotations('mol_seq')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($molseq) = $ac->get_Annotations("mol_seq")) 3 6 9
1095 3 my ($strac) = $molseq->get_Annotations('_text');
1096 3 $str = $strac->value();
1097 }
1098 # create Seq object with sequence
1099 9 my $newseq = Bio::Seq->new( -seq => $str,
1100 -annotation=>$ac,
1101 -nowarnonempty=>1);
1102 9 $tnode->sequence($newseq);
1103 9 $ac->remove_Annotations('mol_seq');
1104 9 $tnode->annotation->remove_Annotations($current);
1105 # if there is id_source add sequence to _id_link
1106 9 ▶ if ($idsrc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idsrc) 3 6 9
1107 3 $self->{'_id_link'}->{$idsrc} = $newseq;
1108 }
1109 }
1110 ▶ elsif ($idsrc && $current eq 'taxonomy') {
Condition33%1/3
$idsrc and $current eq "taxonomy"
!ll&&!rl&&r
3300
MC/DC0%0/2
$idsrc and $current eq "taxonomy"
$idsrc $current eq "taxonomy"
Truth table33%1/3
A and B
A= $idsrcB= $current eq "taxonomy"
ABresult
0X0
100
111
1111 # if there is id_source add sequence to _id_link
1112 0 $self->{'_id_link'}->{$idsrc} = $ac;
1113 }
1114 }
1115 # we are within a default Annotation
1116 else {
1117 105 my $ac = pop (@{$self->{'_currentannotation'}});
1118 105 ▶ if ($ac) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ac) 105 0 105
1119 105 $self->annotateNode( $current, $ac);
1120 }
1121 }
1122 }
1123  
1124  
1125 =head2 annotateNode
1126  
1127 Title : annotateNode
1128 Usage : $treeio->annotateNode($element, $ac)
1129 Function: adds text value and attributes to the AnnotationCollection
1130 that has element name as key. If there are nested elements,
1131 optional AnnotationCollections are added recursively,
1132 with the nested element name as key.
1133 The structure of each AnnotationCollection is
1134 'element' => AnnotationCollection {
1135 '_text' => SimpleValue (text value)
1136 '_attr' => AnnotationCollection {
1137 attribute1 => SimpleValue (attribute value 1)
1138 attribute2 => SimpleValue (attribute value 2)
1139 ...
1140 }
1141 ['nested element' => AnnotationCollection ]
1142 }
1143 Returns : none
1144 Args : none
1145  
1146 =cut
1147  
1148 sub annotateNode
1149 189 ▶{
Subroutine annotateNode: called CC=4 SCAR=13.9
Pod documented
1150 189 my ($self, $element, $newac) = @_;
1151 # if attribute exists then add Annotation::Collection with tag '_attr'
1152 189 ▶ if ( scalar keys %{$self->current_attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->current_attr;}) 42 147 189
1153 42 my $newattr = Bio::Annotation::Collection->new();
1154 42 foreach my $tag (keys %{$self->current_attr}) {
1155 my $sv = Bio::Annotation::SimpleValue->new(
1156 60 -value => $self->current_attr->{$tag}
1157 );
1158 60 $newattr->add_Annotation($tag, $sv);
1159 }
1160 42 $newac->add_Annotation('_attr', $newattr);
1161 }
1162 # if text exists add text as SimpleValue with tag '_text'
1163 189 ▶ if ( $self->{'_currenttext'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_currenttext"}) 139 50 189
1164 139 my $newvalue = Bio::Annotation::SimpleValue->new( -value => $self->{'_currenttext'} );
1165 139 $newac->add_Annotation('_text', $newvalue);
1166 }
1167 }
1168  
1169  
1170 =head1 Methods for exploring the document
1171  
1172 =cut
1173  
1174 =head2 current_attr
1175  
1176 Title : current_attr
1177 Usage : $attr_hash = $treeio->current_attr;
1178 Function: returns the attribute hash for current item
1179 Returns : reference of the attribute hash
1180 Args : none
1181  
1182 =cut
1183  
1184 1256 ▶sub current_attr {
Subroutine current_attr: called CC=4 SCAR=15.0
Pod documented
1185 1256 my ($self) = @_;
1186  
1187 return 0 if ! defined $self->{'_lastitem'} ||
1188 1256 ▶ ! defined $self->{'_lastitem'}->{'current'}->[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-1] 0 1256 1256
Condition33%1/3
defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-1]
!ll&&!rl&&r
001256
MC/DC0%0/2
defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-1]
defined $self->{"_lastitem"} defined $self->{"_lastitem"}{"current"}[-1]
Truth table33%1/3
A and B
A= defined $self->{"_lastitem"}B= defined $self->{"_lastitem"}{"current"}[-1]
ABresult
0X0
100
111
1189 1256 my @keys = keys %{$self->{'_lastitem'}->{'current'}->[-1]};
1190 1256 ▶ (@keys == 1) || die "there should be only one key for each hash";
Branch: 1 branch
BranchTrueFalseTotal
@keys == 1 or die "there should be only one key for each hash" 1256 0 1256
1191 1256 return $self->{'_lastitem'}->{'current'}->[-1]->{$keys[0]};
1192 }
1193  
1194 =head2 prev_attr
1195  
1196 Title : prev_attr
1197 Usage : $hash_ref = $treeio->prev_attr
1198 Function: returns the attribute hash for previous item
1199 Returns : reference of the attribute hash
1200 Args : none
1201  
1202 =cut
1203  
1204 18 ▶sub prev_attr {
Subroutine prev_attr: called CC=4 SCAR=15.0
Pod documented
1205 18 my ($self) = @_;
1206  
1207 return 0 if ! defined $self->{'_lastitem'} ||
1208 18 ▶ ! defined $self->{'_lastitem'}->{'current'}->[-2];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-2] 0 18 18
Condition33%1/3
defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-2]
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-2]
defined $self->{"_lastitem"} defined $self->{"_lastitem"}{"current"}[-2]
Truth table33%1/3
A and B
A= defined $self->{"_lastitem"}B= defined $self->{"_lastitem"}{"current"}[-2]
ABresult
0X0
100
111
1209 18 my @keys = keys %{$self->{'_lastitem'}->{'current'}->[-2]};
1210 18 ▶ (@keys == 1) || die "there should be only one key for each hash";
Branch: 1 branch
BranchTrueFalseTotal
@keys == 1 or die "there should be only one key for each hash" 18 0 18
1211 18 return $self->{'_lastitem'}->{'current'}->[-2]->{$keys[0]};
1212 }
1213  
1214 =head2 current_element
1215  
1216 Title : current_element
1217 Usage : $element = $treeio->current_element
1218 Function: returns the name of the current element
1219 Returns : string (element name)
1220 Args : none
1221  
1222 =cut
1223  
1224 433 ▶sub current_element {
Subroutine current_element: called CC=4 SCAR=15.0
Pod documented
1225 433 my ($self) = @_;
1226  
1227 return 0 if ! defined $self->{'_lastitem'} ||
1228 433 ▶ ! defined $self->{'_lastitem'}->{'current'}->[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-1] 0 433 433
Condition33%1/3
defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-1]
!ll&&!rl&&r
00433
MC/DC0%0/2
defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-1]
defined $self->{"_lastitem"} defined $self->{"_lastitem"}{"current"}[-1]
Truth table33%1/3
A and B
A= defined $self->{"_lastitem"}B= defined $self->{"_lastitem"}{"current"}[-1]
ABresult
0X0
100
111
1229 433 my @keys = keys %{$self->{'_lastitem'}->{'current'}->[-1]};
1230 433 ▶ (@keys == 1) || die "there should be only one key for each hash";
Branch: 1 branch
BranchTrueFalseTotal
@keys == 1 or die "there should be only one key for each hash" 433 0 433
1231 433 return $keys[0];
1232 }
1233  
1234 =head2 prev_element
1235  
1236 Title : prev_element
1237 Usage : $element = $treeio->current_element
1238 Function: returns the name of the previous element
1239 Returns : string (element name)
1240 Args : none
1241  
1242 =cut
1243  
1244 413 ▶sub prev_element {
Subroutine prev_element: called CC=4 SCAR=14.0
Pod documented
1245 413 my ($self) = @_;
1246  
1247 return 0 if ! defined $self->{'_lastitem'} ||
1248 413 ▶ ! defined $self->{'_lastitem'}->{'current'}->[-2];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-2] 1 412 413
Condition66%2/3
defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-2]
!ll&&!rl&&r
01412
MC/DC50%1/2
defined $self->{"_lastitem"} and defined $self->{"_lastitem"}{"current"}[-2]
defined $self->{"_lastitem"} defined $self->{"_lastitem"}{"current"}[-2]
Truth table66%2/3
A and B
A= defined $self->{"_lastitem"}B= defined $self->{"_lastitem"}{"current"}[-2]
ABresult
0X0
100
111
1249 412 my @keys = keys %{$self->{'_lastitem'}->{'current'}->[-2]};
1250 412 ▶ (@keys == 1) || die "there should be only one key for each hash";
Branch: 1 branch
BranchTrueFalseTotal
@keys == 1 or die "there should be only one key for each hash" 412 0 412
1251 412 return $keys[0];
1252 }
1253  
1254  
1255 =head2 treetype
1256  
1257 Title : treetype
1258 Usage : $obj->treetype($newval)
1259 Function: returns the tree type (default is Bio::Tree::Tree)
1260 Returns : value of treetype
1261 Args : newvalue (optional)
1262  
1263  
1264 =cut
1265  
1266 28 ▶sub treetype{
Subroutine treetype: called CC=2 SCAR=6.9
Pod documented
1267 28 my ($self,$value) = @_;
1268 28 ▶ if( defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 15 13 28
1269 15 $self->{'treetype'} = $value;
1270 }
1271 28 return $self->{'treetype'};
1272 }
1273  
1274 =head2 nodetype
1275  
1276 Title : nodetype
1277 Usage : $obj->nodetype($newval)
1278 Function: returns the node type (default is Bio::Node::AnnotatableNode)
1279 Returns : value of nodetype
1280 Args : newvalue (optional)
1281  
1282 =cut
1283  
1284 87 ▶sub nodetype{
Subroutine nodetype: called CC=2 SCAR=6.9
Pod documented
1285 87 my ($self,$value) = @_;
1286 87 ▶ if( defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 15 72 87
1287 15 $self->{'nodetype'} = $value;
1288 }
1289 87 return $self->{'nodetype'};
1290 }
1291  
1292  
1293 =head1 Methods for implementing to_string callback for AnnotatableNode
1294  
1295 =cut
1296  
1297 =head2 node_to_string
1298  
1299 Title : node_to_string
1300 Usage : $annotatablenode->to_string_callback(\&node_to_string)
1301 Function: set as callback in AnnotatableNode, prints the node information in string
1302 Returns : string of node information
1303 Args : none
1304  
1305 =cut
1306  
1307 # this function is similar to _write_tree_Helper_annotatableNode,
1308 # but it is not recursive
1309 sub node_to_string
1310 0 ▶{
Subroutine node_to_string: not called CC=5 SCAR=34.0
Pod documented
1311 0 my ($self) = @_; # this self is a Bio::Tree::AnnotatableNode
1312 # not a Bio::TreeIO::phyloxml
1313 0 my $str = '';
1314 0 my $ac = $self->annotation;
1315  
1316 # start <clade>
1317 0 $str .= '<clade';
1318 0 my @attr = $ac->get_Annotations('_attr'); # check id_source
1319 0 ▶ if (@attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attr) 0 0 0
1320 0 my @id_source = $attr[0]->get_Annotations('id_source');
1321 0 ▶ if (@id_source) {
Branch: 1 branch
BranchTrueFalseTotal
if (@id_source) 0 0 0
1322 0 $str .= " id_source=\"".$id_source[0]->value."\"";
1323 }
1324 }
1325 0 $str .= '>';
1326  
1327 # print all annotations
1328 0 $str = print_annotation( $self, $str, $ac );
1329 # print all sequences
1330 0 ▶ if ($self->has_sequence) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_sequence) 0 0 0
1331 0 foreach my $seq (@{$self->sequence}) {
1332 0 $str = print_seq_annotation( $self, $str, $seq );
1333 }
1334 }
1335
1336 0 $str .= '</clade>';
1337 0 return $str;
1338 }
1339  
1340 =head2 print_annotation
1341  
1342 Title : print_annotation
1343 Usage : $str = $annotatablenode->print_annotation($str, $annotationcollection)
1344 Function: prints the annotationCollection in a phyloXML format.
1345 Returns : string of annotation information
1346 Args : string to which the Annotation should be concatenated to,
1347 annotationCollection that holds the Annotations
1348  
1349 =cut
1350  
1351 # Again, it may be more appropriate to make a separate Annotation::phyloXML object
1352 # and have this subroutine within that object so it can handle the
1353 # reading and writing of the values and attributes.
1354 # especially since this function is used both by
1355 # Bio::TreeIO::phyloxml (through write_tree) and
1356 # Bio::Node::AnnotatableNode (through node_to_string).
1357 # but since tagTree is a temporary stub and I didn't want to make
1358 # a redundant object I've put it here for now.
1359  
1360 sub print_annotation
1361 243 ▶{
Subroutine print_annotation: called CC=7 SCAR=19.5
Pod documented
1362 243 my ($self, $str, $ac) = @_;
1363
1364 243 my @all_anns = $ac->get_Annotations();
1365 243 foreach my $ann (@all_anns) {
1366 393 my $key = $ann->tagname;
1367 393 ▶ if ($key eq '_attr') { next; } # attributes are already printed in the previous level
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "_attr") 105 288 393
1368 288 ▶ if ($ann->isa('Bio::Annotation::SimpleValue'))
Branch: 2 branches
BranchTrueFalseTotal
if ($ann->isa('Bio::Annotation::SimpleValue')) { } 137 151 288
elsif ($ann->isa('Bio::Annotation::Collection')) { } 149 2 151
1369 {
1370 137 ▶ if ($key eq '_text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '_text') { } 136 1 137
1371 136 $str .= $ann->value;
1372 }
1373 else {
1374 1 $str .= "<$key>";
1375 1 $str .= $ann->value;
1376 1 $str .= "</$key>";
1377 }
1378 }
1379 elsif ($ann->isa('Bio::Annotation::Collection'))
1380 {
1381 149 my @attrs = $ann->get_Annotations('_attr');
1382 149 ▶ if (@attrs) { # if there is a attribute collection
Branch: 1 branch
BranchTrueFalseTotal
if (@attrs) { } 26 123 149
1383 26 $str .= "<$key";
1384 26 $str = print_attr($self, $str, $attrs[0]);
1385 26 $str .= ">";
1386 }
1387 else {
1388 123 $str .= "<$key>";
1389 }
1390 149 $str = print_annotation($self, $str, $ann);
1391 149 $str .= "</$key>";
1392 }
1393 }
1394 243 return $str;
1395 }
1396  
1397 =head2 print_attr
1398  
1399 Title : print_attr
1400 Usage : $str = $annotatablenode->print_attr($str, $annotationcollection)
1401 Function: prints the annotationCollection in a phyloXML format.
1402 Returns : string of attributes
1403 Args : string to which the Annotation should be concatenated to,
1404 AnnotationCollection that holds the attributes
1405  
1406 =cut
1407  
1408 # Again, it may be more appropriate to make a separate Annotation::phyloXML object
1409 # and have this subroutine within that object so it can handle the
1410 # reading and writing of the values and attributes.
1411 # especially since this function is used both by
1412 # Bio::TreeIO::phyloxml and Bio::Node::AnnotatableNode
1413 # (through print_annotation).
1414 # but since tagTree is a temporary stub and I didn't want to make
1415 # a redundant object I've put it here for now.
1416  
1417 sub print_attr
1418 39 ▶{
Subroutine print_attr: called CC=3 SCAR=11.1
Pod documented
1419 39 my ($self, $str, $ac) = @_;
1420 39 my @all_attrs = $ac->get_Annotations();
1421 39 foreach my $attr (@all_attrs) {
1422 57 ▶ if (!$attr->isa('Bio::Annotation::SimpleValue')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attr->isa("Bio::Annotation::SimpleValue")) 0 57 57
1423 0 $self->throw("attribute should be a SimpleValue");
1424 }
1425 57 $str .= ' ';
1426 57 $str .= $attr->tagname;
1427 57 $str .= '=';
1428 57 $str .= '"'.$attr->value.'"';
1429 }
1430 39 return $str;
1431 }
1432  
1433 =head2 print_sequence_annotation
1434  
1435 Title : print_sequence_annotation
1436 Usage : $str = $node->print_seq_annotation( $str, $seq );
1437 Function: prints the Bio::Seq object associated with the node
1438 in a phyloXML format.
1439 Returns : string that describes the sequence
1440 Args : string to which the Annotation should be concatenated to,
1441 Seq object to print in phyloXML
1442  
1443 =cut
1444  
1445 # Again, it may be more appropriate to make a separate Annotation::phyloXML object
1446 # and have this subroutine within that object so it can handle the
1447 # reading and writing of the values and attributes.
1448 # especially since this function is used both by
1449 # Bio::TreeIO::phyloxml (through write_tree) and
1450 # Bio::Node::AnnotatableNode (through node_to_string).
1451 # but since tagTree is a temporary stub and I didn't want to make
1452 # a redundant object I've put it here for now.
1453  
1454  
1455 sub print_seq_annotation
1456 9 ▶{
Subroutine print_seq_annotation: called CC=10 SCAR=23.6
Pod undocumented
1457 9 my ($self, $str, $seq) = @_;
1458
1459 9 $str .= "<sequence";
1460 9 my ($attr) = $seq->annotation->get_Annotations('_attr'); # check id_source
1461 9 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 5 4 9
1462 5 my ($id_source) = $attr->get_Annotations('id_source');
1463 5 ▶ if ($id_source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id_source) 5 0 5
1464 5 $str .= " id_source=\"".$id_source->value."\"";
1465 }
1466 }
1467 9 $str .= ">";
1468  
1469 9 my @all_anns = $seq->annotation->get_Annotations();
1470 9 foreach my $ann (@all_anns) {
1471 41 my $key = $ann->tagname;
1472 41 ▶ if ($key eq '_attr') { next; } # attributes are already printed in the previous level
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "_attr") 5 36 41
1473 36 ▶ if ($ann->isa('Bio::Annotation::SimpleValue'))
Branch: 2 branches
BranchTrueFalseTotal
if ($ann->isa('Bio::Annotation::SimpleValue')) { } 0 36 36
elsif ($ann->isa('Bio::Annotation::Collection')) { } 28 8 36
1474 {
1475 0 ▶ if ($key eq '_text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '_text') { } 0 0 0
1476 0 $str .= $ann->value;
1477 }
1478 else {
1479 0 $str .= "<$key>";
1480 0 $str .= $ann->value;
1481 0 $str .= "</$key>";
1482 }
1483 }
1484 elsif ($ann->isa('Bio::Annotation::Collection'))
1485 {
1486 28 my @attrs = $ann->get_Annotations('_attr');
1487 28 ▶ if (@attrs) { # if there is a attribute collection
Branch: 1 branch
BranchTrueFalseTotal
if (@attrs) { } 13 15 28
1488 13 $str .= "<$key";
1489 13 $str = print_attr($self, $str, $attrs[0]);
1490 13 $str .= ">";
1491 }
1492 else {
1493 15 $str .= "<$key>";
1494 }
1495 28 $str = print_annotation($self, $str, $ann);
1496 28 $str .= "</$key>";
1497 }
1498 }
1499 # print mol_seq
1500 9 ▶ if ($seq->seq()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq->seq) 3 6 9
1501 3 $str .= "<mol_seq>";
1502 3 $str .= $seq->seq();
1503 3 $str .= "</mol_seq>";
1504 }
1505  
1506 9 $str .= "</sequence>";
1507 9 return $str;
1508 }
1509  
1510 2 1;
« Bio/Species.pm ↑ Summary Bio/OntologyIO/dagflat.pm »