lib/Bio/MUST/Core/Tree.pm

filter: Statementstmt 55.7% 188 / 337 Branchbran 31.8% 21 / 66 Conditioncond 48.3% 15 / 31 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 71.4% 35 / 49 Podpod 100% 19 / 19 totaltotal 54.3% 282 / 519 CC 59 SCAR 31.3
CC
59
Cov
52%
store_itol_datasets
132.0
__ANON__
56.0
match_branch_lengths
30.0
CRAP 22.9
« lib/Bio/MUST/Core/SeqMask/Weights.pm ↑ Summary lib/Bio/MUST/Core/Taxonomy/Labeler.pm »
1 package Bio::MUST::Core::Tree;
2 # ABSTRACT: Thin wrapper around Bio::Phylo trees
3 # CONTRIBUTOR: Valerian LUPO <valerian.lupo@uliege.be>
4 18 $Bio::MUST::Core::Tree::VERSION = '0.252040';
5 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # use MooseX::SemiAffordanceAccessor;
7 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 18 ▶use Smart::Comments '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use List::AllUtils qw(uniq max_by);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 18 ▶use Bio::Phylo::Util::Logger ':levels';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use Bio::Phylo::IO qw(parse);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # silence warnings about removing orphan nodes
24 18 ▶use Bio::Phylo::Forest::TreeRole 'verbose' => ERROR;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 18 ▶use Bio::MUST::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 18 ▶use Bio::MUST::Core::Constants qw(:files);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 18 ▶use Bio::MUST::Core::Utils qw(:filenames);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 18 ▶use aliased 'Bio::MUST::Core::SeqId';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 18 ▶use aliased 'Bio::MUST::Core::IdList';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 # TODO: check if no circularity issue between Tree and Tree::Splits load?
32 18 ▶use aliased 'Bio::MUST::Core::Tree::Splits';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 18 with 'Bio::MUST::Core::Roles::Commentable',
34 'Bio::MUST::Core::Roles::Listable';
35  
36  
37 18 has 'tree' => (
38 is => 'ro',
39 isa => 'Maybe[Bio::Phylo::Forest::Tree]',
40 default => undef,
41 writer => '_set_tree',
42 );
43  
44  
45  
46 17 ▶sub newick_str { ## no critic (RequireArgUnpacking)
Subroutine newick_str: called CC=1 SCAR=0.0
Pod documented
47 17 return _clean_newick_str( shift->tree->to_newick(@_) ); # currying
48 }
49  
50 17 ▶sub _clean_newick_str {
Subroutine _clean_newick_str: called CC=1 SCAR=0.0
51 17 my $newick_str = shift;
52  
53 # remove quotes...
54 # ...and trailing zero-length branch length (RAxML) if any
55 17 $newick_str =~ tr{'"}{}d;
56 17 $newick_str =~ s{:0\.0+;}{;}xmsg;
57  
58 17 return $newick_str;
59 }
60  
61 # color for uncolored taxon (see Taxonomy::ColorScheme)
62 18 const my $BLACK => '#000000';
63  
64 # Note: we don't store SeqId objects in the tree but dynamically build them
65 # to benefit from SeqId methods (e.g., auto-removal of first '_'). This is
66 # the most flexible approach without costing too much in CPU-time.
67  
68  
69 11 ▶sub all_seq_ids {
Subroutine all_seq_ids: called CC=1 SCAR=0.0
Pod documented
70 11 my $self = shift;
71  
72 # old code:
73 # my @tips = @{ $self->tree->get_terminals };
74 # my @full_ids = map { $_->get_name } @tips;
75 # return map { SeqId->new(full_id => $_) } @full_ids;
76  
77 # Note1: we use a slower visitor method to ensure that the id array
78 # is sorted as when displayed by TreeDrawer methods
79 # Note2: this order is consistent with FigTree display as well, but not
80 # with Seaview (and njplot) renderings
81  
82 11 my @seq_ids;
83  
84 $self->tree->visit_depth_first(
85 # collect tip names and convert them to SeqIds
86 1526 ▶ -pre => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
87 1526 my $node = shift;
88 1526 ▶ if ($node->is_terminal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->is_terminal) 772 754 1526
89 772 push @seq_ids, SeqId->new( full_id => $node->get_name );
90 }
91 1526 return;
92 },
93 11 );
94  
95 11 return @seq_ids;
96 }
97  
98  
99 # NODE-LABEL EDITING METHODS
100  
101  
102 0 ▶sub shorten_ids { ## no critic (RequireArgUnpacking)
Subroutine shorten_ids: not called CC=1 SCAR=6.9
Pod documented
103 0 return shift->_change_ids_(1, @_);
104 }
105  
106  
107 10 ▶sub restore_ids { ## no critic (RequireArgUnpacking)
Subroutine restore_ids: called CC=1 SCAR=0.0
Pod documented
108 10 return shift->_change_ids_(0, @_);
109 }
110  
111  
112 10 ▶sub _change_ids_ {
Subroutine _change_ids_: called CC=4 SCAR=14.0
113 10 my $self = shift;
114 10 my $abbr = shift;
115 10 my $id_mapper = shift;
116  
117 # update only terminal nodes
118 10 for my $tip ( @{ $self->tree->get_terminals } ) {
119 360 my $seq_id = SeqId->new( full_id => $tip->get_name );
120 360 ▶ my $new_id = $abbr ? $id_mapper->abbr_id_for( $seq_id->full_id )
Branch: 1 branch
BranchTrueFalseTotal
$abbr ? : 0 360 360
121 : $id_mapper->long_id_for( $seq_id->full_id );
122 360 ▶ $tip->set_name($new_id) if $new_id;
Branch: 1 branch
BranchTrueFalseTotal
if $new_id 360 0 360
123 } # Note: leave id alone if not found
124  
125 10 return;
126 }
127  
128  
129 0 ▶sub switch_attributes_and_labels_for_terminals { ## no critic (RequireArgUnpacking)
Subroutine switch_attributes_and_labels_for_terminals: not called CC=1 SCAR=6.9
Pod documented
130 0 return shift->_switch_attributes_and_labels_(0, @_);
131 }
132  
133  
134 0 ▶sub switch_attributes_and_labels_for_internals { ## no critic (RequireArgUnpacking)
Subroutine switch_attributes_and_labels_for_internals: not called CC=1 SCAR=6.9
Pod documented
135 0 return shift->_switch_attributes_and_labels_(1, @_);
136 }
137  
138  
139 0 ▶sub switch_attributes_and_labels_for_entities { ## no critic (RequireArgUnpacking)
Subroutine switch_attributes_and_labels_for_entities: not called CC=1 SCAR=6.9
Pod documented
140 0 return shift->_switch_attributes_and_labels_(2, @_);
141 }
142  
143  
144 0 ▶sub _switch_attributes_and_labels_ {
Subroutine _switch_attributes_and_labels_: not called CC=4 SCAR=30.0
145 0 my $self = shift;
146 0 my $mode = shift;
147 0 my $key = shift;
148  
149 # TODO: investigate options of Bio::Phylo::Unparsers::Newick
150  
151 # update either terminal or internal nodes
152 0 my $tree = $self->tree;
153 my @nodes = @{
154 0 ▶ $mode == 2 ? $tree->get_entities :
Branch: 2 branches
BranchTrueFalseTotal
$mode == 2 ? : 0 0 0
$mode == 1 ? : 0 0 0
155 $mode == 1 ? $tree->get_internals :
156 $tree->get_terminals
157 };
158  
159 # Note: old labels are backuped in specified attributes and vice-versa
160 # TODO: allow appending acc for terminal nodes?
161 0 for my $node (@nodes) {
162 0 my $label = $node->get_name;
163 0 my $attribute = $node->get_generic($key);
164 0 $node->set_generic($key => $label);
165 0 $node->set_name($attribute);
166 }
167  
168 0 return;
169 }
170  
171  
172 0 ▶sub switch_branch_lengths_and_labels_for_entities {
Subroutine switch_branch_lengths_and_labels_for_entities: not called CC=3 SCAR=24.8
Pod documented
173 0 my $self = shift;
174 0 my $length = shift;
175  
176 # use branch lengths as labels
177 0 my $tree = $self->tree;
178 0 for my $node ( @{ $tree->get_internals } ) {
179 0 $node->set_name($node->get_branch_length);
180 }
181  
182 # delete branch lengths
183 0 for my $node ( @{ $tree->get_entities } ) {
184 0 $node->set_branch_length($length); # default is undef
185 }
186  
187 0 return;
188 }
189  
190  
191 0 ▶sub collapse_subtrees {
Subroutine collapse_subtrees: not called CC=2 SCAR=17.9
Pod documented
192 0 my $self = shift;
193 0 ▶ my $key = shift // 'taxon_collapse';
Condition0%0/2
shift() // "taxon_collapse"
l!l
00
MC/DC0%0/1
shift() // "taxon_collapse"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
194  
195 # compute maximal path length (from root)
196 0 my $tree_max_path = $self->tree->get_root->calc_max_path_to_tips;
197  
198 # "balanced"-order tree traversal
199 0 my $collapsed; # will be defined when within a collapsed subtree
200 $self->tree->visit_depth_first(
201  
202 # collapse subtrees with identical attributes
203 0 ▶ -pre_daughter => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
204 0 my $node = shift;
205 0 ▶ return if $node->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $node->is_terminal 0 0 0
206  
207 # reset collapsing for robustness
208 0 $node->set_generic('!collapse' => undef);
209  
210 # do not further collapse children of a collapsed subtree
211 # to facilitate interactive uncollapsing (e.g., in FigTree)
212 0 ▶ return if $collapsed;
Branch: 1 branch
BranchTrueFalseTotal
if $collapsed 0 0 0
213  
214 # collect children attributes
215 0 my @attrs;
216 0 ▶ for (my $i = 0; my $child = $node->get_child($i); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $node->get_child($i)) 0 0 0
217 0 push @attrs, $child->get_generic($key);
218 }
219  
220 # collapse subtree if all attributes are defined and identical
221 # TODO: check consistency with group naming
222 0 ▶ return if List::AllUtils::any { not defined $_ } @attrs;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &List::AllUtils::any(sub { not defined $_; } , @attrs) 0 0 0
223 0 ▶ return if uniq(@attrs) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if &uniq(@attrs) > 1 0 0 0
224  
225 # skip black color when collapsing at color
226 0 my $color = shift @attrs;
227 0 ▶ return if $color eq $BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if $color eq $BLACK 0 0 0
228  
229 # compute and set FigTree's "node height" for collapsed clade
230 # Note: the tallest tip will be 0
231 0 my $sub_max_path = $node->calc_max_path_to_tips
232 + $node->calc_path_to_root;
233 0 my $node_height = $tree_max_path - $sub_max_path;
234 0 $node->set_generic('!collapse' => qq|{"collapsed",$node_height}|);
235  
236 # set "within a collapsed subtree" status
237 0 $collapsed = $node->get_id;
238  
239 0 return;
240 },
241  
242 0 ▶ -post_daughter => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
243 0 my $node = shift;
244 0 ▶ return if $node->is_terminal;
Branch: 1 branch
BranchTrueFalseTotal
if $node->is_terminal 0 0 0
245  
246 # unset "within a collapsed subtree" status (when leaving subtree)
247 0 ▶ $collapsed = undef
Branch: 1 branch
BranchTrueFalseTotal
if defined $collapsed and $collapsed eq $node->get_id 0 0 0
Condition0%0/3
defined $collapsed and $collapsed eq $node->get_id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $collapsed and $collapsed eq $node->get_id
defined $collapsed $collapsed eq $node->get_id
Truth table0%0/3
A and B
A= defined $collapsedB= $collapsed eq $node->get_id
ABresult
0X0
100
111
248 if defined $collapsed && $collapsed eq $node->get_id;
249  
250 0 return;
251 },
252 0 );
253  
254 0 return;
255 }
256  
257  
258  
259 1 ▶sub get_node_that_maximizes {
Subroutine get_node_that_maximizes: called CC=2 SCAR=7.0
Pod documented
260 1 my $self = shift;
261 1 ▶ my $method = shift // 'get_branch_length';
Condition50%1/2
shift() // "get_branch_length"
l!l
01
MC/DC0%0/1
shift() // "get_branch_length"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
262  
263 # return node for which method yields the highest value
264 1 ▶ my $node = max_by { $_->$method // 0 } @{ $self->tree->get_entities };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$_->$method // 0
l!l
1451
MC/DC100%1/1
$_->$method // 0
$_->$method
Truth table100%2/2
A
A= $_->$method
Aresult
11
00
265 # Note: scalar context to get only one node!
266  
267 1 return $node;
268 }
269  
270  
271 12 ▶sub root_tree { ## no critic (RequireArgUnpacking)
Subroutine root_tree: called CC=9 SCAR=22.0
Pod documented
272 12 my $self = shift;
273 12 my $node = shift; # can be a filter rather than a specific node
274  
275 12 my $tree = $self->tree;
276 12 my $splits;
277  
278 12 ▶ if ( $node->can('score') # splits are always needed when passed a filter
Branch: 1 branch
BranchTrueFalseTotal
if ($node->can("score") or &List::AllUtils::any(sub { $_->get_name; } , @{$tree->get_internals;})) 12 0 12
Condition66%2/3
$node->can("score") or &List::AllUtils::any(sub { $_->get_name; } , @{$tree->get_internals;})
l!l&&r!l&&!r
2100
MC/DC0%0/2
$node->can("score") or &List::AllUtils::any(sub { $_->get_name; } , @{$tree->get_internals;})
$node->can("score") &List::AllUtils::any(sub { $_->get_name; } , @{$tree->get_internals;})
Truth table66%2/3
A or B
A= $node->can("score")B= &List::AllUtils::any(sub { $_->get_name; } , @{$tree->get_internals;})
ABresult
1X1
011
000
279 10 ▶ || List::AllUtils::any { $_->get_name } @{ $tree->get_internals } ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
280 #### Saving splits metadata before rooting...
281 12 $splits = Splits->new_from_tree($tree);
282 #### s: $splits->_bp_for
283 }
284  
285 # when passed a filter use it as outgroup to determine root node
286 12 ▶ if ( $node->can('score') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->can("score")) 2 10 12
287 2 $node = $splits->get_node_for_split( $self,
288 $splits->get_split_that_maximizes($node) );
289 }
290  
291 # actually root tree using Bio::Phylo method
292 12 $node->set_root_below(@_); # currying...
293  
294 # remove old root (but only if the tree was already rooted)
295 12 $tree->remove_orphans;
296  
297 # restore node metadata if needed
298 12 ▶ if ($splits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($splits) 12 0 12
299 #### Restoring splits metadata after rooting...
300 12 for my $node ( @{ $tree->get_internals } ) {
301 668 my $bp_key = $splits->node2key($node);
302 #### $bp_key
303 # Note: when rooting on a single OTU there exists an internal node
304 # for which the key looks like a key for the trivial split leading
305 # to the root. It generally must be clear-up (see just below).
306 668 ▶ my $bp_val = !$bp_key ? q{} : $splits->bp_for($bp_key)
Branch: 1 branch
BranchTrueFalseTotal
!$bp_key ? : 12 656 668
Condition100%5/5
($splits->bp_for($bp_key) // $splits->comp_bp_for($bp_key)) // ""
l!l
6506
$splits->bp_for($bp_key) // $splits->comp_bp_for($bp_key)
l!l&&r!l&&!r
619316
MC/DC100%2/2
($splits->bp_for($bp_key) // $splits->comp_bp_for($bp_key)) // ""
$splits->bp_for($bp_key) $splits->comp_bp_for($bp_key)
Truth table100%3/3
A // B
A= $splits->bp_for($bp_key)B= $splits->comp_bp_for($bp_key)
ABresult
1X1
011
000
307 // $splits->comp_bp_for($bp_key) // q{};
308 #### $bp_val
309 # Note: a key can be undef (e.g., root) or corresponds to a trivial
310 # split for which no value was stored. In both cases, the original
311 # value must be explicitly cleared-up using q{}.
312 668 $node->set_name($bp_val);
313 }
314 }
315  
316 12 return $self;
317 }
318  
319  
320 # TREE-MATCHING METHODS
321  
322  
323 # TODO1: need for a taxon pruning sub as it seems that the -keep option
324 # TODO1: of Bio::Phylo Newick parser does not work completely
325 # TODO2: need for a rerooting sub that completely works!
326  
327 0 ▶sub match_branch_lengths {
Subroutine match_branch_lengths: not called CC=5 SCAR=34.0
Pod documented
328 0 my $self = shift;
329 0 my $other = shift; # second tree
330  
331 0 my $tree1 = $self->tree;
332 0 my $tree2 = $other->tree;
333 0 tie my %blens_for, 'Tie::IxHash';
334  
335 0 for my $tree ($tree1, $tree2) {
336 0 for my $node ( @{ $tree->get_entities } ) {
337  
338 # compute clade key and store corresponding branch length
339 my $clade_key
340 = join '::',
341 0 sort { $a cmp $b }
342 0 map { $_->get_internal_name } @{ $node->get_terminals }
343 ;
344 0 my $branch_length = $node->get_branch_length;
345 0 ▶ push @{ $blens_for{$clade_key} },
Branch: 1 branch
BranchTrueFalseTotal
if defined $branch_length 0 0 0
346 $branch_length if defined $branch_length;
347 }
348 }
349  
350 # ensure that bipartitions matching proceeded as expected
351 carp '[BMC] Warning: cannot match all bipartitions; returning useless hash!'
352 0 ▶ unless List::AllUtils::all {
Subroutine __ANON__: not called CC=1 SCAR=6.9
353 0 @{ $blens_for{$_} } == 2
354 0 ▶ } keys %blens_for;
Branch: 1 branch
BranchTrueFalseTotal
unless &List::AllUtils::all(sub { @{$blens_for{$_};} == 2; } , keys %blens_for) 0 0 0
355  
356 0 return \%blens_for;
357 }
358  
359  
360 # I/O METHODS
361  
362  
363 19 ▶sub load {
Subroutine load: called CC=4 SCAR=13.9
Pod documented
364 19 my $class = shift;
365 19 my $infile = shift;
366  
367 19 open my $in, '<', $infile;
368  
369 19 my $tree = $class->new();
370 19 my $newick_str;
371  
372 LINE:
373 19 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 25 19 44
374 25 chomp $line;
375  
376 # skip empty lines and process comment lines
377 25 ▶ next LINE if $line =~ $EMPTY_LINE
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$Bio::MUST::Core::Tree::EMPTY_LINE/ or $tree->is_comment($line) 6 19 25
Condition66%2/3
$line =~ /$Bio::MUST::Core::Tree::EMPTY_LINE/ or $tree->is_comment($line)
l!l&&r!l&&!r
0619
MC/DC50%1/2
$line =~ /$Bio::MUST::Core::Tree::EMPTY_LINE/ or $tree->is_comment($line)
$line =~ /$Bio::MUST::Core::Tree::EMPTY_LINE/ $tree->is_comment($line)
Truth table66%2/3
A or B
A= $line =~ /$Bio::MUST::Core::Tree::EMPTY_LINE/B= $tree->is_comment($line)
ABresult
1X1
011
000
378 || $tree->is_comment($line);
379  
380 19 $newick_str .= $line;
381 }
382  
383 19 my $forest = parse(-format => 'newick', -string => $newick_str);
384 19 $tree->_set_tree($forest->first);
385  
386 19 return $tree;
387 }
388  
389  
390 # Note: it seems that to_newick automatically replace spaces by '_' in node
391 # labels (ids), which is a quite reasonable behavior.
392  
393 # TODO: define better API for outputting branch lengths/support values
394 # TODO: use constants for to_newick parameters
395  
396  
397  
398 13 ▶sub store {
Subroutine store: called CC=3 SCAR=11.1
Pod documented
399 13 my $self = shift;
400 13 my $outfile = shift;
401 13 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition50%1/2
shift() // {}
l!l
130
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
402  
403 # TODO: consider allowing this hash for all store methods?
404  
405 13 ▶ $args->{-nodelabels} //= 1; # default to nodelabels on
Condition50%1/2
$args->{"-nodelabels"} //= 1
l!l
013
MC/DC0%0/1
$args->{"-nodelabels"} //= 1
$args->{"-nodelabels"}
Truth table50%1/2
A
A= $args->{"-nodelabels"}
Aresult
11
00
406  
407 13 open my $out, '>', $outfile;
408 13 say {$out} $self->newick_str( %{$args} );
409  
410 13 close $out;
411  
412 13 return;
413 }
414  
415  
416 0 ▶sub store_itol_datasets {
Subroutine store_itol_datasets: not called CC=11 SCAR=48.8
Pod documented
417 0 my $self = shift;
418 0 my $outfile = shift;
419 0 ▶ my $key = shift // 'taxon';
Condition0%0/2
shift() // "taxon"
l!l
00
MC/DC0%0/1
shift() // "taxon"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
420  
421 # name dataset files
422 0 my $outbase = change_suffix($outfile, '.txt');
423 0 my $color_file = insert_suffix($outbase, '-color');
424 0 my $clade_file = insert_suffix($outbase, '-clade');
425 0 my $range_file = insert_suffix($outbase, '-range');
426 0 my $label_file = insert_suffix($outbase, '-label');
427 0 my $colps_file = insert_suffix($outbase, '-collapse');
428  
429 # open and setup dataset files
430 0 open my $color_out, '>', $color_file;
431 0 say {$color_out} join "\n", 'TREE_COLORS', 'SEPARATOR COMMA', 'DATA';
432 0 open my $clade_out, '>', $clade_file;
433 0 say {$clade_out} join "\n", 'TREE_COLORS', 'SEPARATOR COMMA', 'DATA';
434 0 open my $range_out, '>', $range_file;
435 0 say {$range_out} join "\n", 'TREE_COLORS', 'SEPARATOR COMMA', 'DATA';
436 0 open my $label_out, '>', $label_file;
437 0 say {$label_out} join "\n", 'LABELS', 'SEPARATOR COMMA', 'DATA';
438 0 open my $colps_out, '>', $colps_file;
439 0 say {$colps_out} join "\n", 'COLLAPSE', 'DATA';
440  
441 # setup format
442 0 my $type = 'normal',
443 my $size = 1;
444  
445 NODE:
446 0 for my $node ( @{ $self->tree->get_entities } ){
447 0 ▶ my $color = $node->get_generic('!color') // $BLACK;
Condition0%0/3
$node->get_generic("!color") // $BLACK
l!l&&r!l&&!r
000
MC/DC0%0/2
$node->get_generic("!color") // $BLACK
$node->get_generic("!color") $BLACK
Truth table0%0/3
A // B
A= $node->get_generic("!color")B= $BLACK
ABresult
1X1
011
000
448 0 my $label = $node->get_generic($key);
449 0 my $collapse = $node->get_generic('!collapse');
450  
451 0 ▶ if ($node->is_terminal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->is_terminal) 0 0 0
452 0 ▶ next NODE if $color eq $BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if $color eq $BLACK 0 0 0
453  
454 0 my $id = SeqId->new( full_id => $node->get_name )->foreign_id;
455 0 say {$color_out} join q{,}, $id, 'label', $color, $type, $size;
456 0 say {$clade_out} join q{,}, $id, 'clade', $color, $type, $size;
457 0 say {$range_out} join q{,}, $id, 'range', $color, $type, $size;
458  
459 0 next NODE;
460 }
461  
462 0 my @descendants;
463 0 ▶ for (my $i = 0; my $child = $node->get_child($i); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $node->get_child($i)) 0 0 0
464 0 push @descendants, @{ $child->get_terminals };
465 }
466  
467 # Note: Simply calling get_terminals on node returns descendants
468 # in an order that leads to mis-selecting the first and last ones.
469 # Instead we store the descendants from node's main two (or more)
470 # children sequentially. This seems to fix the issue.
471  
472 # determine first and last descendants to build node id
473 my $id = join '|', map {
474 0 ▶ SeqId->new( full_id => $_->get_name )->foreign_id
Branch: 1 branch
BranchTrueFalseTotal
@descendants > 1 ? : 0 0 0
475 } @descendants[ @descendants > 1 ? (0,-1) : (0) ];
476 # Note: should always > 1 but one never knows...
477  
478 0 ▶ say {$label_out} join q{,}, $id, $label if $label;
Branch: 1 branch
BranchTrueFalseTotal
if $label 0 0 0
479 0 ▶ say {$colps_out} $id if $collapse;
Branch: 1 branch
BranchTrueFalseTotal
if $collapse 0 0 0
480  
481 0 ▶ next NODE if $color eq $BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if $color eq $BLACK 0 0 0
482  
483 0 say {$color_out} join q{,}, $id, 'label', $color, $type, $size;
484 0 say {$clade_out} join q{,}, $id, 'clade', $color, $type, $size;
485 0 say {$range_out} join q{,}, $id, 'range', $color, $type, $size;
486 }
487  
488 0 close $color_out;
489 0 close $clade_out;
490 0 close $range_out;
491 0 close $label_out;
492 0 close $colps_out;
493  
494 0 return;
495 }
496  
497  
498 0 ▶sub store_figtree {
Subroutine store_figtree: not called CC=3 SCAR=24.8
Pod documented
499 0 my $self = shift;
500 0 my $outfile = shift;
501  
502 # transfer taxon names for internals only
503 # this is needed to avoid double naming of tips (std label + taxon)
504 0 for my $node ( @{ $self->tree->get_internals } ) {
505 0 my $taxon = $node->get_generic('taxon');
506 0 ▶ $node->set_generic('!name' => qq|"$taxon"|) if $taxon;
Branch: 1 branch
BranchTrueFalseTotal
if $taxon 0 0 0
507 } # Note: get_generic does not return undef, hence: if $taxon
508  
509 # build mesquite-enabled Newick string
510 # TODO: consider using newick_str instead?
511 0 my $newick_str = $self->tree->to_newick(
512 -nodelabels => 1,
513 # -blformat => '%.10f',
514 -nhxkeys => [ '!name', '!color', '!collapse' ],
515 -nhxstyle => 'mesquite',
516 );
517  
518 # ... and adapt it for FigTree
519 0 $newick_str =~ s{\[%}{[&}xmsg;
520  
521 # ... then restore zero-valued bootstrap values
522 # since 'false' internal names are converted to 'NodeNNN' strings
523 0 $newick_str =~ s{\b Node\d+ \b}{0}xmsg;
524 # TODO: consider doing that also in the standard store?
525  
526 0 open my $out, '>', $outfile;
527  
528 # output minimal NEXUS tree file
529 0 print {$out} <<"EOF";
530 #NEXUS
531  
532 begin trees;
533 tree tree_1 = [&R] $newick_str
534 end;
535 EOF
536  
537 0 close $out;
538  
539 0 return;
540 }
541  
542  
543 3 ▶sub store_arb {
Subroutine store_arb: called CC=3 SCAR=11.0
Pod documented
544 3 my $self = shift;
545 3 my $outfile = shift;
546 3 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition50%1/2
shift() // {}
l!l
30
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
547  
548 3 my $alifile = $args->{alifile};
549  
550 # optionally link to Ali (without path)
551 3 ▶ if ($alifile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alifile) 1 2 3
552 1 my ($basename, $dir, $ext) = fileparse($alifile, qr{\.[^.]*}xms);
553 1 $self->insert_comment("$basename$ext");
554 }
555  
556 # output ARB tree file
557 3 open my $out, '>', $outfile;
558 3 print {$out} $self->header;
559 3 say {$out} $self->newick_str( -nodelabels => 0 );
560  
561 3 close $out;
562  
563 3 return;
564 }
565  
566  
567 5 ▶sub store_grp {
Subroutine store_grp: called CC=4 SCAR=13.9
Pod documented
568 5 my $self = shift;
569 5 my $outfile = shift;
570  
571 # extract tip ids, non-root nodes and support values
572 5 my @tip_ids = map { $_->foreign_id } $self->all_seq_ids;
573 5 my @nodes = grep { not $_->is_root } @{ $self->tree->get_internals };
574 5 my @bp_vals = map { $_->get_name } @nodes;
575  
576 # determine support value type (BP or PP)
577 5 ▶ my $pp = List::AllUtils::all { $_ >= 0.0 && $_ <= 1.0 } @bp_vals;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_ >= 0 and $_ <= 1 8 0 8
578  
579 5 open my $out, '>', $outfile;
580  
581 5 for my $node (@nodes) {
582  
583 # build bipartition string
584 my %in_bip = map {
585 755 SeqId->new( full_id => $_->get_name )->foreign_id => 1
586 81 } @{ $node->get_terminals };
587 81 ▶ my $bip = join q{}, map { $in_bip{$_} ? '*' : '.' } @tip_ids;
Branch: 1 branch
BranchTrueFalseTotal
$in_bip{$_} ? : 755 4549 5304
588  
589 # fetch (and possibly fix) support value for bipartition
590 81 my $support = shift @bp_vals;
591 81 ▶ $support = int( $support * 100.0 ) if $pp;
Branch: 1 branch
BranchTrueFalseTotal
if $pp 5 76 81
592  
593 # write bipartition line
594 81 say {$out} "$bip $support";
595 }
596  
597 5 close $out;
598  
599 5 return;
600 }
601  
602  
603 1 ▶sub store_tpl {
Subroutine store_tpl: called CC=3 SCAR=11.0
Pod documented
604 1 my $self = shift;
605 1 my $outfile = shift;
606  
607 # backup and discard branch lengths
608 # Note: I have to do that since I cannot clone the tree (Bio::Phylo bug?)
609 1 my @branch_lengths;
610 1 for my $node ( @{ $self->tree->get_entities } ) {
611 914 push @branch_lengths, $node->get_branch_length;
612 914 $node->set_branch_length(undef);
613 }
614  
615 1 open my $out, '>', $outfile;
616  
617 # output topology
618 1 say {$out} '1'; # TODO: improve this for multiple topologies
619 1 say {$out} $self->newick_str( -nodelabels => 0 );
620  
621 # restore branch lengths
622 1 for my $node ( @{ $self->tree->get_entities } ) {
623 914 $node->set_branch_length( shift @branch_lengths );
624 }
625  
626 1 close $out;
627  
628 1 return;
629 }
630  
631 18 __PACKAGE__->meta->make_immutable;
632 18 1;
633  
634 __END__
« lib/Bio/MUST/Core/SeqMask/Weights.pm ↑ Summary lib/Bio/MUST/Core/Taxonomy/Labeler.pm »