File Coverage

blib/lib/Bio/Phylo/Unparsers/Newick.pm
Criterion Covered Total %
statement 23 68 33.8
branch 3 26 11.5
condition n/a
subroutine 5 6 83.3
pod n/a
total 31 100 31.0


line stmt bran cond sub pod time code
1             package Bio::Phylo::Unparsers::Newick;
2 7     7   44 use strict;
  7         12  
  7         238  
3 7     7   34 use base 'Bio::Phylo::Unparsers::Abstract';
  7         13  
  7         1737  
4 7     7   41 use Bio::Phylo::Forest::Tree;
  7         14  
  7         125  
5 7     7   31 use Bio::Phylo::Util::CONSTANT ':objecttypes';
  7         13  
  7         4671  
6              
7             =head1 NAME
8              
9             Bio::Phylo::Unparsers::Newick - Serializer used by Bio::Phylo::IO, no serviceable parts inside
10              
11             =head1 DESCRIPTION
12              
13             This module turns a tree object into a newick formatted (parenthetical) tree
14             description. It is called by the L<Bio::Phylo::IO> facade, don't call it
15             directly. You can pass the following additional arguments to the unparse
16             call:
17            
18             # by default, names for tips are derived from $node->get_name, if
19             # 'internal' is specified, uses $node->get_internal_name, if 'taxon'
20             # uses $node->get_taxon->get_name, if 'taxon_internal' uses
21             # $node->get_taxon->get_internal_name, if $key, uses $node->get_generic($key)
22             -tipnames => one of (internal|taxon|taxon_internal|$key)
23            
24             # for things like a translate table in nexus, or to specify truncated
25             # 10-character names, you can pass a translate mapping as a hashref.
26             # to generate the translated names, the strings obtained following the
27             # -tipnames rules are used.
28             -translate => { Homo_sapiens => 1, Pan_paniscus => 2 }
29            
30             # array ref used to specify keys, which are embedded as key/value pairs (where
31             # the value is obtained from $node->get_generic($key)) in comments,
32             # formatted depending on '-nhxstyle', which could be 'nhx' (default), i.e.
33             # [&&NHX:$key1=$value1:$key2=$value2] or 'mesquite', i.e.
34             # [% $key1 = $value1, $key2 = $value2 ]
35             -nhxkeys => [ $key1, $key2 ]
36            
37             # if set, appends labels to internal nodes (names obtained from the same
38             # source as specified by '-tipnames')
39             -nodelabels => 1
40            
41             # specifies a formatting style / dialect
42             -nhxstyle => one of (mesquite|nhx)
43            
44             # specifies a branch length sprintf number formatting template, default is %f
45             -blformat => '%e'
46              
47              
48             =begin comment
49              
50             Type : Wrapper
51             Title : _to_string($tree)
52             Usage : $newick->_to_string($tree);
53             Function: Prepares for the recursion to unparse the tree object into a
54             newick string.
55             Alias :
56             Returns : SCALAR
57             Args : Bio::Phylo::Forest::Tree
58              
59             =end comment
60              
61             =cut
62              
63             sub _to_string {
64 28     28   59 my $self = shift;
65 28         110 my $tree = $self->{'PHYLO'};
66 28         96 my $type = $tree->_type;
67 28 50       70 if ( $type == _TREE_ ) {
    0          
    0          
68 28         112 my $root = $tree->get_root;
69 28         59 my %args;
70 28         70 for
71             my $key (qw(TRANSLATE TIPNAMES NHXKEYS NODELABELS BLFORMAT NHXSTYLE))
72             {
73 168 100       353 if ( my $val = $self->{$key} ) {
74 19         63 my $arg = '-' . lc($key);
75 19         50 $args{$arg} = $val;
76             }
77             }
78 28         179 return $root->to_newick(%args);
79             }
80             elsif ( $type == _FOREST_ ) {
81 0           my $forest = $tree;
82 0           my $newick = "";
83 0           for my $tree ( @{ $forest->get_entities } ) {
  0            
84 0           my $root = $tree->get_root;
85 0           my %args;
86 0           for my $key (
87             qw(TRANSLATE TIPNAMES NHXKEYS NODELABELS BLFORMAT NHXSTYLE))
88             {
89 0 0         if ( my $val = $self->{$key} ) {
90 0           my $arg = '-' . lc($key);
91 0           $args{$arg} = $val;
92             }
93             }
94 0           $newick .= $root->to_newick(%args) . "\n";
95             }
96 0           return $newick;
97             }
98             elsif ( $type == _PROJECT_ ) {
99 0           my $project = $tree;
100 0           my $newick = "";
101 0           for my $forest ( @{ $project->get_forests } ) {
  0            
102 0           for my $tree ( @{ $forest->get_entities } ) {
  0            
103 0           my $root = $tree->get_root;
104 0           my %args;
105 0           for my $key (
106             qw(TRANSLATE TIPNAMES NHXKEYS NODELABELS BLFORMAT NHXSTYLE))
107             {
108 0 0         if ( my $val = $self->{$key} ) {
109 0           my $arg = '-' . lc($key);
110 0           $args{$arg} = $val;
111             }
112             }
113 0           $newick .= $root->to_newick(%args) . "\n";
114             }
115             }
116 0           return $newick;
117             }
118             }
119              
120             =begin comment
121              
122             Type : Unparser
123             Title : __to_string
124             Usage : $newick->__to_string($tree, $node);
125             Function: Unparses the tree object into a newick string.
126             Alias :
127             Returns : SCALAR
128             Args : A Bio::Phylo::Forest::Tree object. Optional: A Bio::Phylo::Forest::Node
129             object, the starting point for recursion.
130              
131             =end comment
132              
133             =cut
134              
135             {
136             my $string = q{};
137              
138             #no warnings 'uninitialized';
139             sub __to_string {
140 0     0     my ( $self, $tree, $n ) = @_;
141 0 0         if ( !$n->get_parent ) {
    0          
142 0 0         if ( defined $n->get_branch_length ) {
143 0           $string = $n->get_name . ':' . $n->get_branch_length . ';';
144             }
145             else {
146 0 0         $string = defined $n->get_name ? $n->get_name . ';' : ';';
147             }
148             }
149             elsif ( !$n->get_previous_sister ) {
150 0 0         if ( defined $n->get_branch_length ) {
151 0           $string = $n->get_name . ':' . $n->get_branch_length . $string;
152             }
153 0           else { $string = $n->get_name . $string; }
154             }
155             else {
156 0 0         if ( defined $n->get_branch_length ) {
157 0           $string =
158             $n->get_name . ':' . $n->get_branch_length . ',' . $string;
159             }
160 0           else { $string = $n->get_name . ',' . $string; }
161             }
162 0 0         if ( $n->get_first_daughter ) {
163 0           $n = $n->get_first_daughter;
164 0           $string = ')' . $string;
165 0           $self->__to_string( $tree, $n );
166 0           while ( $n->get_next_sister ) {
167 0           $n = $n->get_next_sister;
168 0           $self->__to_string( $tree, $n );
169             }
170 0           $string = '(' . $string;
171             }
172             }
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 unparse newick strings.
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             =back
194              
195             =head1 CITATION
196              
197             If you use Bio::Phylo in published research, please cite it:
198              
199             B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
200             and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
201             I<BMC Bioinformatics> B<12>:63.
202             L<http://dx.doi.org/10.1186/1471-2105-12-63>
203              
204             =cut
205              
206             1;