File Coverage

blib/lib/Bio/Phylo/Unparsers/Newick.pm
Criterion Covered Total %
statement 26 71 36.6
branch 3 26 11.5
condition n/a
subroutine 6 7 85.7
pod n/a
total 35 104 33.6


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