Phylo/Unparsers/Adjacency.pm

filter: Statementstmt 92.4% 49 / 53 Branchbran 42.8% 6 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 84.0% 63 / 75 CC 9 SCAR 12.9
CC
9
Cov
82%
_to_string
5.4
__ANON__
5.0
BEGIN
1.0
CRAP 3.6
« Phylo/NeXML/DOM/Document/Twig.pm ↑ Summary Phylo/Matrices/Datatype/Custom.pm »
1 package Bio::Phylo::Unparsers::Adjacency;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'Bio::Phylo::Unparsers::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Bio::Phylo::Forest::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Bio::Phylo::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Bio::Phylo::Util::CONSTANT ':objecttypes';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Bio::Phylo::Unparsers::Adjacency - Serializer used by Bio::Phylo::IO, no serviceable parts inside
12  
13 =head1 DESCRIPTION
14  
15 This module turns a tree structure into tabular data organized as an "adjacency
16 list", i.e. child -> parent relationships. The table at least has the
17 following columns: 'child' and 'parent'. 'length' is interpreted as branch
18 length. Columns starting with 'node:' are created for semantic annotations
19 to the focal node, columns starting with 'branch:' are created for the focal
20 branch. Records are listed in pre-order, so that references to parent
21 nodes can be resolved immediately. Consequently, the root is the first record,
22 without a parent. Example:
23  
24 ((A:1,B:2)n1:3,C:4)n2:0;
25  
26 Becomes (with an extra example annotation):
27  
28 child parent length node:dcterms:identifier
29 n2 0 35462
30 n1 n2 3 34987
31 A n1 1 73843
32 B n1 2 98743
33 C n2 4 39847
34  
35 =cut
36  
37  
38 1 ▶sub _to_string {
Subroutine _to_string: called CC=5 SCAR=16.8
39 1 my $self = shift;
40 1 my $phylo = $self->{'PHYLO'};
41 1 my $type = $phylo->_type;
42
43 # optionally, there might be predicates to serialize
44 1 my $predicates = $self->{'PREDICATES'};
45 1 my $cols;
46 1 ▶ if ( $predicates ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($predicates) 1 0 1
47 1 $cols = "\t" . join "\t", map { "node:$_" } @{ $predicates };
48 }
49
50 # create header
51 1 my $output = <<HEADER;
52 child parent length$cols
53 HEADER
54
55 # get the focal tree from the input
56 1 my $tree;
57 1 ▶ if ( $type == _TREE_ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == 3) { } 1 0 1
elsif ($type == 4) { } 0 0 0
elsif ($type == 9) { } 0 0 0
58 1 $tree = $phylo;
59 }
60 elsif ( $type == _FOREST_ ) {
61 0 $tree = $phylo->first;
62 }
63 elsif ( $type == _PROJECT_ ) {
64 0 ($tree) = @{ $phylo->get_items(_TREE_) };
65 }
66 else {
67 0 throw 'BadArgs' => "Don't know how to serialize $phylo";
68 }
69
70 # create the output
71 $tree->visit_depth_first(
72 5 ▶ '-pre' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
73 5 my $node = shift;
74 5 my $name = $node->get_internal_name;
75
76 # parent name
77 5 my $pname = '';
78 5 ▶ if ( my $parent = $node->get_parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $node->get_parent) 4 1 5
79 4 $pname = $parent->get_internal_name;
80 }
81
82 # branch length
83 5 my $bl = $node->get_branch_length;
84 5 ▶ my $length = defined $bl ? $bl : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $bl ? : 5 0 5
85
86 # other annotations
87 5 my $annotations = '';
88 5 ▶ if ( $predicates ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($predicates) 5 0 5
89 5 my @values;
90 5 for my $p ( @{ $predicates } ) {
91 5 push @values, $node->get_meta_object($p);
92 }
93 5 $annotations = "\t" . join "\t", @values;
94 }
95 5 $output .= "$name\t$pname\t$length$annotations\n";
96 }
97 1 );
98 1 return $output;
99 }
100  
101 # podinherit_insert_token
102  
103 =head1 SEE ALSO
104  
105 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
106 for any user or developer questions and discussions.
107  
108 =over
109  
110 =item L<Bio::Phylo::IO>
111  
112 The adjacency unparser is called by the L<Bio::Phylo::IO> object.
113 Look there to learn how to unparse trees.
114  
115 =item L<Bio::Phylo::Manual>
116  
117 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
118  
119 =back
120  
121 =head1 CITATION
122  
123 If you use Bio::Phylo in published research, please cite it:
124  
125 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
126 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
127 I<BMC Bioinformatics> B<12>:63.
128 L<http://dx.doi.org/10.1186/1471-2105-12-63>
129  
130 =cut
131  
132 1 1;
« Phylo/NeXML/DOM/Document/Twig.pm ↑ Summary Phylo/Matrices/Datatype/Custom.pm »