File Coverage

blib/lib/Bio/Phylo/Unparsers/Adjacency.pm
Criterion Covered Total %
statement 45 49 91.8
branch 6 14 42.8
condition n/a
subroutine 7 7 100.0
pod n/a
total 58 70 82.8


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