File Coverage

blib/lib/Bio/Phylo/Unparsers/Adjacency.pm
Criterion Covered Total %
statement 48 52 92.3
branch 6 14 42.8
condition n/a
subroutine 8 8 100.0
pod n/a
total 62 74 83.7


line stmt bran cond sub pod time code
1             package Bio::Phylo::Unparsers::Adjacency;
2 1     1   7 use strict;
  1         2  
  1         28  
3 1     1   4 use warnings;
  1         2  
  1         27  
4 1     1   4 use base 'Bio::Phylo::Unparsers::Abstract';
  1         2  
  1         105  
5 1     1   7 use Bio::Phylo::Forest::Tree;
  1         1  
  1         8  
6 1     1   5 use Bio::Phylo::Util::Exceptions 'throw';
  1         1  
  1         52  
7 1     1   6 use Bio::Phylo::Util::CONSTANT ':objecttypes';
  1         2  
  1         529  
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             sub _to_string {
39 1     1   3 my $self = shift;
40 1         6 my $phylo = $self->{'PHYLO'};
41 1         5 my $type = $phylo->_type;
42            
43             # optionally, there might be predicates to serialize
44 1         1 my $predicates = $self->{'PREDICATES'};
45 1         2 my $cols;
46 1 50       4 if ( $predicates ) {
47 1         1 $cols = "\t" . join "\t", map { "node:$_" } @{ $predicates };
  1         5  
  1         2  
48             }
49            
50             # create header
51 1         3 my $output = <<HEADER;
52             child parent length$cols
53             HEADER
54            
55             # get the focal tree from the input
56 1         1 my $tree;
57 1 50       4 if ( $type == _TREE_ ) {
    0          
    0          
58 1         1 $tree = $phylo;
59             }
60             elsif ( $type == _FOREST_ ) {
61 0         0 $tree = $phylo->first;
62             }
63             elsif ( $type == _PROJECT_ ) {
64 0         0 ($tree) = @{ $phylo->get_items(_TREE_) };
  0         0  
65             }
66             else {
67 0         0 throw 'BadArgs' => "Don't know how to serialize $phylo";
68             }
69            
70             # create the output
71             $tree->visit_depth_first(
72             '-pre' => sub {
73 5     5   5 my $node = shift;
74 5         21 my $name = $node->get_internal_name;
75            
76             # parent name
77 5         8 my $pname = '';
78 5 100       10 if ( my $parent = $node->get_parent ) {
79 4         9 $pname = $parent->get_internal_name;
80             }
81            
82             # branch length
83 5         13 my $bl = $node->get_branch_length;
84 5 50       12 my $length = defined $bl ? $bl : '';
85            
86             # other annotations
87 5         8 my $annotations = '';
88 5 50       11 if ( $predicates ) {
89 5         8 my @values;
90 5         6 for my $p ( @{ $predicates } ) {
  5         9  
91 5         13 push @values, $node->get_meta_object($p);
92             }
93 5         13 $annotations = "\t" . join "\t", @values;
94             }
95 5         17 $output .= "$name\t$pname\t$length$annotations\n";
96             }
97 1         13 );
98 1         8 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;