Phylo/Unparsers/Newick.pm

filter: Statementstmt 39.1% 29 / 74 Branchbran 10.7% 3 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 34.8% 38 / 109 CC 21 SCAR 41.9
CC
21
Cov
31%
__to_string
90.0
_to_string
74.6
BEGIN
1.0
CRAP 66.1
« Phylo/Treedrawer/Abstract.pm ↑ Summary Phylo/Forest.pm »
1 package Bio::Phylo::Unparsers::Newick;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use base 'Bio::Phylo::Unparsers::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Bio::Phylo::Forest::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Bio::Phylo::Util::CONSTANT ':objecttypes';
Subroutine BEGIN: called CC=1 SCAR=0.0
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 28 ▶sub _to_string {
Subroutine _to_string: called CC=13 SCAR=43.1
65 28 my $self = shift;
66 28 my $tree = $self->{'PHYLO'};
67 28 my $type = $tree->_type;
68 28 ▶ if ( $type == _TREE_ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == 3) { } 28 0 28
elsif ($type == 4) { } 0 0 0
elsif ($type == 9) { } 0 0 0
69 28 my $root = $tree->get_root;
70 28 my %args;
71 28 for
72 my $key (qw(TRANSLATE TIPNAMES NHXKEYS NODELABELS BLFORMAT NHXSTYLE))
73 {
74 168 ▶ if ( my $val = $self->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $self->{$key}) 19 149 168
75 19 my $arg = '-' . lc($key);
76 19 $args{$arg} = $val;
77 }
78 }
79 28 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 } ) {
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 ▶ if ( my $val = $self->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $self->{$key}) 0 0 0
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 } ) {
103 0 for my $tree ( @{ $forest->get_entities } ) {
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 ▶ if ( my $val = $self->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $self->{$key}) 0 0 0
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 7 my $string = q{};
138  
139 #no warnings 'uninitialized';
140 0 ▶ sub __to_string {
Subroutine __to_string: not called CC=9 SCAR=45.0
141 0 my ( $self, $tree, $n ) = @_;
142 0 ▶ if ( !$n->get_parent ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $n->get_parent) { } 0 0 0
elsif (not $n->get_previous_sister) { } 0 0 0
143 0 ▶ if ( defined $n->get_branch_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n->get_branch_length) { } 0 0 0
144 0 $string = $n->get_name . ':' . $n->get_branch_length . ';';
145 }
146 else {
147 0 ▶ $string = defined $n->get_name ? $n->get_name . ';' : ';';
Branch: 1 branch
BranchTrueFalseTotal
defined $n->get_name ? : 0 0 0
148 }
149 }
150 elsif ( !$n->get_previous_sister ) {
151 0 ▶ if ( defined $n->get_branch_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n->get_branch_length) { } 0 0 0
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 ▶ if ( defined $n->get_branch_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n->get_branch_length) { } 0 0 0
158 0 $string =
159 $n->get_name . ':' . $n->get_branch_length . ',' . $string;
160 }
161 0 else { $string = $n->get_name . ',' . $string; }
162 }
163 0 ▶ if ( $n->get_first_daughter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->get_first_daughter) 0 0 0
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 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->get_next_sister) 0 0 0
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 7 1;
« Phylo/Treedrawer/Abstract.pm ↑ Summary Phylo/Forest.pm »