Bio/TreeIO/newick.pm

filter: Statementstmt 77.7% 77 / 99 Branchbran 57.1% 24 / 42 Conditioncond 55.5% 15 / 27 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 71.4% 10 / 14 Podpod 83.3% 5 / 6 totaltotal 66.6% 134 / 201 CC 34 SCAR 23.4
CC
34
Cov
69%
bootstrap_style
20.0
_node_as_string
17.1
write_tree
12.8
CRAP 10.4
« Bio/SeqIO/tinyseq/tinyseqHandler.pm ↑ Summary Bio/DB/Fasta.pm »
1 #
2 # BioPerl module for Bio::TreeIO::newick
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Jason Stajich <jason@bioperl.org>
7 #
8 # Copyright Jason Stajich
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 Bio::TreeIO::newick - parsing and writing of Newick/PHYLIP/New Hampshire format
17  
18 =head1 SYNOPSIS
19  
20 # do not use this module directly
21 use Bio::TreeIO;
22  
23 my $treeio = Bio::TreeIO->new(-format => 'newick',
24 -file => 't/data/LOAD_Ccd1.dnd');
25 my $tree = $treeio->next_tree;
26  
27 =head1 DESCRIPTION
28  
29 This module handles parsing and writing of Newick/PHYLIP/New Hampshire format.
30  
31 =head1 FEEDBACK
32  
33 =head2 Mailing Lists
34  
35 User feedback is an integral part of the evolution of this and other
36 Bioperl modules. Send your comments and suggestions preferably to the
37 Bioperl mailing list. Your participation is much appreciated.
38  
39 bioperl-l@bioperl.org - General discussion
40 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
41  
42 =head2 Support
43  
44 Please direct usage questions or support issues to the mailing list:
45  
46 I<bioperl-l@bioperl.org>
47  
48 rather than to the module maintainer directly. Many experienced and
49 reponsive experts will be able look at the problem and quickly
50 address it. Please include a thorough description of the problem
51 with code and data examples if at all possible.
52  
53 =head2 Reporting Bugs
54  
55 Report bugs to the Bioperl bug tracking system to help us keep track
56 of the bugs and their resolution. Bug reports can be submitted via the
57 web:
58  
59 https://github.com/bioperl/bioperl-live/issues
60  
61 =head1 AUTHOR - Jason Stajich
62  
63 Email jason-at-bioperl-dot-org
64  
65 =head1 APPENDIX
66  
67 The rest of the documentation details each of the object methods.
68 Internal methods are usually preceded with a _
69  
70 =cut
71  
72 # Let the code begin...
73  
74 package Bio::TreeIO::newick;
75 14 $Bio::TreeIO::newick::VERSION = '1.7.8';
76 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 14 ▶use Bio::Event::EventGeneratorI;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 14 ▶use base qw(Bio::TreeIO Bio::TreeIO::NewickParser);
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 =head2 new
83  
84 Title : new
85 Args : -print_count => boolean default is false
86 -bootstrap_style => set the bootstrap style (one of nobranchlength,
87 molphy, traditional)
88 -order_by => set the order by sort method
89  
90 See L<Bio::Node::Node::each_Descendent()>
91  
92 =cut
93  
94 151 ▶sub _initialize {
Subroutine _initialize: called CC=2 SCAR=7.0
95 151 my $self = shift;
96 151 $self->SUPER::_initialize(@_);
97 151 my ( $print_count ) = $self->_rearrange(
98 [
99 qw(PRINT_COUNT)
100 ],
101 @_
102 );
103 151 ▶ $self->print_tree_count( $print_count || 0 );
Condition50%1/2
$print_count || 0
l!l
0151
MC/DC0%0/1
$print_count || 0
$print_count
Truth table50%1/2
A
A= $print_count
Aresult
11
00
104 151 return;
105 }
106  
107 =head2 next_tree
108  
109 Title : next_tree
110 Usage : my $tree = $treeio->next_tree
111 Function: Gets the next tree in the stream
112 Returns : L<Bio::Tree::TreeI>
113 Args : none
114  
115 =cut
116  
117 120 ▶sub next_tree {
Subroutine next_tree: called CC=5 SCAR=16.9
Pod documented
118 120 my ($self) = @_;
119 120 local $/ = ";\n";
120 120 ▶ return unless $_ = $self->_readline;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ = $self->_readline 2 118 120
121  
122 118 s/[\r\n]//gs;
123 118 my $score;
124 0 ▶ my $despace = sub { my $dirty = shift; $dirty =~ s/\s+//gs; return $dirty };
Subroutine __ANON__: not called CC=1 SCAR=3.5
125 0 ▶ my $dequote = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 my $dirty = shift;
127 0 $dirty =~ s/^"?\s*(.+?)\s*"?$/$1/;
128 0 return $dirty;
129 118 };
130 0 s/([^"]*)(".+?")([^"]*)/$despace->($1) . $dequote->($2) . $despace->($3)/egsx;
131  
132 118 ▶ if (s/^\s*\[([^\]]+)\]//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^\s*\[([^\]]+)\]//) 2 116 118
133 2 my $match = $1;
134 2 $match =~ s/\s//g;
135 2 $match =~ s/lh\=//;
136 2 ▶ if ( $match =~ /([-\d\.+]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match =~ /([-\d\.+]+)/) 2 0 2
137 2 $score = $1;
138 }
139 }
140  
141 118 $self->_eventHandler->start_document;
142  
143 # Call the parse_newick method as defined in NewickParser.pm
144 118 $self->parse_newick($_);
145  
146 118 my $tree = $self->_eventHandler->end_document;
147  
148 # Add the tree score afterwards if it exists.
149 118 ▶ if (defined $tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tree) 118 0 118
150 118 $tree->score($score);
151 118 return $tree;
152 }
153 }
154  
155 # Returns the default set of parsing & writing parameters for the Newick format.
156 151 ▶sub get_default_params {
Subroutine get_default_params: called CC=1 SCAR=0.0
Pod undocumented
157 151 my $self = shift;
158  
159 return {
160 151 newline_each_node => 0,
161 order_by => '', # ???
162 bootstrap_style => 'traditional', # Can be 'traditional', 'molphy', 'nobranchlength'
163 internal_node_id => 'id', # Can be 'id' or 'bootstrap'
164
165 no_branch_lengths => 0,
166 no_bootstrap_values => 0,
167 no_internal_node_labels => 0
168 };
169 }
170  
171  
172 =head2 write_tree
173  
174 Title : write_tree
175 Usage : $treeio->write_tree($tree);
176 Function: Write a tree out to data stream in newick/phylip format
177 Returns : none
178 Args : L<Bio::Tree::TreeI> object
179  
180 =cut
181  
182 47 ▶sub write_tree {
Subroutine write_tree: called CC=8 SCAR=25.5
Pod documented
183 47 my ( $self, @trees ) = @_;
184 47 ▶ if ( $self->print_tree_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->print_tree_count) 0 47 47
185 0 $self->_print( sprintf( " %d\n", scalar @trees ) );
186 }
187  
188 47 my $params = $self->get_params;
189  
190 47 foreach my $tree (@trees) {
191 47 ▶ if ( !defined $tree
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $tree or ref($tree) =~ /ARRAY/i or not $tree->isa("Bio::Tree::TreeI")) 0 47 47
Condition33%2/6
not defined $tree or ref($tree) =~ /ARRAY/i or not $tree->isa("Bio::Tree::TreeI")
l!l&&r!l&&!r
0047
not defined $tree or ref($tree) =~ /ARRAY/i
l!l&&r!l&&!r
0047
MC/DC0%0/3
not defined $tree or ref($tree) =~ /ARRAY/i or not $tree->isa("Bio::Tree::TreeI")
not defined $tree ref($tree) =~ /ARRAY/i not $tree->isa("Bio::Tree::TreeI")
Truth table25%1/4
not(A) or B or not(C)
A= not defined $treeB= ref($tree) =~ /ARRAY/iC= not $tree->isa("Bio::Tree::TreeI")
ABCresult
1XX1
01X1
0011
0000
192 || ref($tree) =~ /ARRAY/i
193 || !$tree->isa('Bio::Tree::TreeI') )
194 {
195 0 $self->throw(
196 "Calling write_tree with non Bio::Tree::TreeI object\n");
197 }
198 47 my @data = $self->_write_tree_Helper( $tree->get_root_node, $params);
199 47 $self->_print( join( ',', @data ).";" );
200 }
201
202 47 ▶ $self->flush if $self->_flush_on_write && defined $self->_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_flush_on_write and defined $self->_fh 47 0 47
Condition33%1/3
$self->_flush_on_write and defined $self->_fh
!ll&&!rl&&r
0047
MC/DC0%0/2
$self->_flush_on_write and defined $self->_fh
$self->_flush_on_write defined $self->_fh
Truth table33%1/3
A and B
A= $self->_flush_on_writeB= defined $self->_fh
ABresult
0X0
100
111
203 47 return;
204 }
205  
206 816 ▶sub _write_tree_Helper {
Subroutine _write_tree_Helper: called CC=3 SCAR=11.0
207 816 my $self = shift;
208 816 my ( $node, $params ) = @_;
209 816 my @data;
210  
211 816 foreach my $n ( $node->each_Descendent($params->{order_by}) ) {
212 769 push @data, $self->_write_tree_Helper( $n, $params );
213 }
214  
215 816 my $label = $self->_node_as_string($node,$params);
216  
217 816 ▶ if ( scalar(@data) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @data >= 1) { } 365 451 816
218 365 $data[0] = "(" . $data[0];
219 365 $data[-1] .= ")";
220 365 $data[-1] .= $label;
221 } else {
222 451 push @data, $label;
223 }
224  
225 816 return @data;
226 }
227  
228 816 ▶sub _node_as_string {
Subroutine _node_as_string: called CC=14 SCAR=28.4
229 816 my $self = shift;
230 816 my $node = shift;
231 816 my $params = shift;
232  
233 816 my $label_stringbuffer = '';
234  
235 816 ▶ if ($params->{no_bootstrap_values} != 1 &&
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'no_bootstrap_values'} != 1 and not $node->is_Leaf and defined $node->bootstrap and $params->{'bootstrap_style'} eq 'traditional' and $params->{'internal_node_id'} eq 'bootstrap') { } 5 811 816
elsif ($params->{'no_internal_node_labels'} != 1) { } 811 0 811
Condition83%10/12
$params->{"no_bootstrap_values"} != 1 and not $node->is_Leaf and defined $node->bootstrap and $params->{"bootstrap_style"} eq "traditional" and $params->{"internal_node_id"} eq "bootstrap"
!ll&&!rl&&r
6781335
$params->{"no_bootstrap_values"} != 1 and not $node->is_Leaf and defined $node->bootstrap and $params->{"bootstrap_style"} eq "traditional"
!ll&&!rl&&r
6780138
$params->{"no_bootstrap_values"} != 1 and not $node->is_Leaf and defined $node->bootstrap
!ll&&!rl&&r
451227138
$params->{"no_bootstrap_values"} != 1 and not $node->is_Leaf
!ll&&!rl&&r
0451365
MC/DC60%3/5
$params->{"no_bootstrap_values"} != 1 and not $node->is_Leaf and defined $node->bootstrap and $params->{"bootstrap_style"} eq "traditional" and $params->{"internal_node_id"} eq "bootstrap"
$params->{"no_bootstrap_values"} != 1 not $node->is_Leaf defined $node->bootstrap $params->{"bootstrap_style"} eq "traditional" $params->{"internal_node_id"} eq "bootstrap"
Truth table66%4/6
A and not(B) and C and D and E
A= $params->{"no_bootstrap_values"} != 1B= not $node->is_LeafC= defined $node->bootstrapD= $params->{"bootstrap_style"} eq "traditional"E= $params->{"internal_node_id"} eq "bootstrap"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
236 !$node->is_Leaf &&
237 defined $node->bootstrap &&
238 $params->{bootstrap_style} eq 'traditional' &&
239 $params->{internal_node_id} eq 'bootstrap') {
240 # If we're an internal node and we're using 'traditional' bootstrap style,
241 # we output the bootstrap instead of any label.
242 5 my $bootstrap = $node->bootstrap;
243 5 ▶ $label_stringbuffer .= $bootstrap if (defined $bootstrap);
Branch: 1 branch
BranchTrueFalseTotal
if defined $bootstrap 5 0 5
244 } elsif ($params->{no_internal_node_labels} != 1) {
245 811 my $id = $node->id;
246 811 ▶ $label_stringbuffer .= $id if( defined $id );
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 494 317 811
247 }
248  
249 816 ▶ if ($params->{no_branch_lengths} != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"no_branch_lengths"} != 1) 816 0 816
250 816 my $blen = $node->branch_length;
251 816 ▶ $label_stringbuffer .= ":". $blen if (defined $blen);
Branch: 1 branch
BranchTrueFalseTotal
if defined $blen 719 97 816
252 }
253  
254 816 ▶ if ($params->{bootstrap_style} eq 'molphy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"bootstrap_style"} eq "molphy") 0 816 816
255 0 my $bootstrap = $node->bootstrap;
256 0 ▶ $label_stringbuffer .= "[$bootstrap]" if (defined $bootstrap);
Branch: 1 branch
BranchTrueFalseTotal
if defined $bootstrap 0 0 0
257 }
258  
259 816 ▶ if ($params->{newline_each_node} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"newline_each_node"} == 1) 0 816 816
260 0 $label_stringbuffer .= "\n";
261 }
262  
263 816 return $label_stringbuffer;
264 }
265  
266  
267 =head2 print_tree_count
268  
269 Title : print_tree_count
270 Usage : $obj->print_tree_count($newval)
271 Function: Get/Set flag for printing out the tree count (paml,protml way)
272 Returns : value of print_tree_count (a scalar)
273 Args : on set, new value (a scalar or undef, optional)
274  
275 =cut
276  
277 198 ▶sub print_tree_count {
Subroutine print_tree_count: called CC=3 SCAR=11.1
Pod documented
278 198 my $self = shift;
279 198 ▶ return $self->{'_print_tree_count'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 151 47 198
280 47 ▶ return $self->{'_print_tree_count'} || 0;
Condition50%1/2
$self->{'_print_tree_count'} || 0
l!l
047
MC/DC0%0/1
$self->{'_print_tree_count'} || 0
$self->{'_print_tree_count'}
Truth table50%1/2
A
A= $self->{'_print_tree_count'}
Aresult
11
00
281 }
282  
283 =head2 bootstrap_style
284  
285 Title : bootstrap_style
286 Usage : $obj->bootstrap_style($newval)
287 Function: A description of how bootstraps and branch lengths are
288 written, as the ID part of the internal node or else in []
289 in the branch length (Molphy-like; I am sure there is a
290 better name for this but am not sure where to go for some
291 sort of format documentation)
292  
293 If no branch lengths are requested then no bootstraps are usually
294 written (unless someone REALLY wants this functionality...)
295  
296 Can take on strings which contain the possible values of
297 'nobranchlength' --> don't draw any branch lengths - this
298 is helpful if you don't want to have to
299 go through and delete branch len on all nodes
300 'molphy' --> draw bootstraps (100) like
301 (A:0.11,B:0.22):0.33[100];
302 'traditional' --> draw bootstraps (100) like
303 (A:0.11,B:0.22)100:0.33;
304 Returns : value of bootstrap_style (a scalar)
305 Args : on set, new value (a scalar or undef, optional)
306  
307 =cut
308  
309 0 ▶sub bootstrap_style {
Subroutine bootstrap_style: not called CC=4 SCAR=30.0
Pod documented
310 0 my $self = shift;
311 0 my $val = shift;
312 0 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
313  
314 0 ▶ if ( $val !~ /^nobranchlength|molphy|traditional/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $val =~ /^nobranchlength|molphy|traditional/i) { } 0 0 0
315 0 $self->warn(
316 "requested an unknown bootstrap style $val, expect one of nobranchlength,molphy,traditional, not updating value.\n"
317 );
318 }
319 else {
320 0 $self->{'_bootstrap_style'} = $val;
321 }
322 }
323 0 ▶ return $self->{'_bootstrap_style'} || 'traditional';
Condition0%0/2
$self->{'_bootstrap_style'} || 'traditional'
l!l
00
MC/DC0%0/1
$self->{'_bootstrap_style'} || 'traditional'
$self->{'_bootstrap_style'}
Truth table0%0/2
A
A= $self->{'_bootstrap_style'}
Aresult
11
00
324 }
325  
326 =head2 order_by
327  
328 Title : order_by
329 Usage : $obj->order_by($newval)
330 Function: Allow node order to be specified (typically "alpha")
331 See L<Bio::Node::Node::each_Descendent()>
332 Returns : value of order_by (a scalar)
333 Args : on set, new value (a scalar or undef, optional)
334  
335 =cut
336  
337 0 ▶sub order_by {
Subroutine order_by: not called CC=2 SCAR=17.9
Pod documented
338 0 my $self = shift;
339  
340 0 ▶ return $self->{'order_by'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
341 0 return $self->{'order_by'};
342 }
343  
344 14 1;
« Bio/SeqIO/tinyseq/tinyseqHandler.pm ↑ Summary Bio/DB/Fasta.pm »