Phylo/Treedrawer.pm

filter: Statementstmt 62.6% 198 / 316 Branchbran 44.1% 38 / 86 Conditioncond 30.9% 17 / 55 MC/DCmcdc 8.8% 3 / 34 Subroutinesub 77.0% 47 / 61 Podpod 92.6% 38 / 41 totaltotal 57.5% 341 / 593 CC 68 SCAR 21.3
CC
68
Cov
55%
set_scale_options
30.9
_compute_unrooted_coordinates
30.0
__ANON__
30.0
CRAP 8.4
« Phylo/Util/Math.pm ↑ Summary Phylo.pm »
1 package Bio::Phylo::Treedrawer;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Bio::Phylo::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Bio::Phylo::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Bio::Phylo::Util::CONSTANT qw'_TREE_ /looks_like/ _PI_';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 my @fields = qw(
9 WIDTH
10 BRANCH_WIDTH
11 HEIGHT
12 MODE
13 SHAPE
14 PADDING
15 NODE_RADIUS
16 TIP_RADIUS
17 TEXT_HORIZ_OFFSET
18 TEXT_VERT_OFFSET
19 TEXT_WIDTH
20 TREE
21 _SCALEX
22 _SCALEY
23 SCALE
24 FORMAT
25 COLLAPSED_CLADE_WIDTH
26 CLADE_LABEL_WIDTH
27 PIE_COLORS;
28 );
29  
30 3 my $PI = _PI_;
31 3 my $tips = 0.000_000_000_000_01;
32 3 my $logger = Bio::Phylo::Util::Logger->new;
33  
34 =head1 NAME
35  
36 Bio::Phylo::Treedrawer - Visualizer of tree shapes
37  
38 =head1 SYNOPSIS
39  
40 use Bio::Phylo::IO 'parse';
41 use Bio::Phylo::Treedrawer;
42  
43 my $string = '((A:1,B:2)n1:3,C:4)n2:0;';
44 my $tree = parse( -format => 'newick', -string => $string )->first;
45  
46 my $treedrawer = Bio::Phylo::Treedrawer->new(
47 -width => 800,
48 -height => 600,
49 -shape => 'CURVY', # curvogram
50 -mode => 'PHYLO', # phylogram
51 -format => 'SVG'
52 );
53  
54 $treedrawer->set_scale_options(
55 -width => '100%',
56 -major => '10%', # major cross hatch interval
57 -minor => '2%', # minor cross hatch interval
58 -label => 'MYA',
59 );
60  
61 $treedrawer->set_tree($tree);
62 print $treedrawer->draw;
63  
64 =head1 DESCRIPTION
65  
66 This module prepares a tree object for drawing (calculating coordinates for
67 nodes) and calls the appropriate format-specific drawer.
68  
69 =head1 METHODS
70  
71 =head2 CONSTRUCTOR
72  
73 =over
74  
75 =item new()
76  
77 Treedrawer constructor.
78  
79 Type : Constructor
80 Title : new
81 Usage : my $treedrawer = Bio::Phylo::Treedrawer->new(
82 %args
83 );
84 Function: Initializes a Bio::Phylo::Treedrawer object.
85 Alias :
86 Returns : A Bio::Phylo::Treedrawer object.
87 Args : none.
88  
89 =cut
90  
91 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
92 3 my $class = shift;
93 3 my $self = {
94 'WIDTH' => 500,
95 'HEIGHT' => 500,
96 'MODE' => 'PHYLO',
97 'SHAPE' => 'CURVY',
98 'PADDING' => 50,
99 'NODE_RADIUS' => 0,
100 'TIP_RADIUS' => 0,
101 'TEXT_HORIZ_OFFSET' => 6,
102 'TEXT_VERT_OFFSET' => 4,
103 'TEXT_WIDTH' => 150,
104 'TREE' => undef,
105 '_SCALEX' => 1,
106 '_SCALEY' => 1,
107 'FORMAT' => 'Svg',
108 'SCALE' => undef,
109 'BRANCH_WIDTH' => 1,
110 'COLLAPSED_CLADE_WIDTH' => 6,
111 'CLADE_LABEL_WIDTH' => 36,
112 'PIE_COLORS' => {},
113 };
114 3 bless $self, $class;
115 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
116 2 my %opts = looks_like_hash @_;
117 2 for my $key ( keys %opts ) {
118 11 my $mutator = lc $key;
119 11 $mutator =~ s/^-/set_/;
120 11 $self->$mutator( $opts{$key} );
121 }
122 }
123 3 return $self;
124 }
125  
126 2 ▶sub _cascading_setter {
Subroutine _cascading_setter: called CC=3 SCAR=12.3
127 2 my ( $self, $value ) = @_;
128 2 my ( $package, $filename, $line, $subroutine ) = caller(1);
129 2 $subroutine =~ s/.*://;
130 2 $logger->debug($subroutine);
131 2 ▶ if ( my $tree = $self->get_tree ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tree = $self->get_tree) 0 2 2
132 0 ▶ if ( $tree->can($subroutine) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->can($subroutine)) 0 0 0
133 0 $tree->$subroutine($value);
134 }
135 }
136 2 $subroutine =~ s/^set_//;
137 2 $self->{ uc $subroutine } = $value;
138 2 return $self;
139 }
140  
141 51 ▶sub _cascading_getter {
Subroutine _cascading_getter: called CC=3 SCAR=11.1
142 51 my ( $self, $invocant ) = @_;
143 51 my ( $package, $filename, $line, $subroutine ) = caller(1);
144 51 $subroutine =~ s/.*://;
145 51 $logger->debug($subroutine);
146 51 ▶ if ( $invocant ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invocant) 31 20 51
147
148 # The general idea is that there are certain properties that can potentially be
149 # set globally (i.e. in this package) or at the level of the object it applies
150 # to. For example, maybe we want to set the node radius globally here, or maybe
151 # we want to set it on the node. The idea, here, is then that we might first
152 # check to see if the values are set on $invocant, and if not, return the global
153 # value. The way this used to be done was by calling ->can(), however, because of
154 # the way in which method calls are handled by the Draw*Role classes, we can't
155 # do that.
156 #if ( $invocant->can($subroutine) ) {
157 31 my $value = $invocant->$subroutine();
158 31 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 31 31
159 0 return $value;
160 }
161 #}
162 }
163 51 $subroutine =~ s/^get_//;
164 51 return $self->{ uc $subroutine };
165 }
166  
167 =back
168  
169 =head2 MUTATORS
170  
171 =over
172  
173 =item set_format()
174  
175 Sets image format.
176  
177 Type : Mutator
178 Title : set_format
179 Usage : $treedrawer->set_format('Svg');
180 Function: Sets the drawer submodule.
181 Returns :
182 Args : Name of an image format
183  
184 =cut
185  
186 3 ▶sub set_format {
Subroutine set_format: called CC=2 SCAR=7.2
Pod documented
187 3 my ( $self, $format ) = @_;
188 3 $format = ucfirst( lc($format) );
189 3 ▶ if ( looks_like_class __PACKAGE__ . '::' . $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_class('Bio::Phylo::Treedrawer::' . $format)) { } 3 0 3
190 3 $self->{'FORMAT'} = $format;
191 3 return $self;
192 }
193 else {
194 0 throw 'BadFormat' => "'$format' is not a valid image format";
195 }
196 }
197  
198 =item set_width()
199  
200 Sets image width.
201  
202 Type : Mutator
203 Title : set_width
204 Usage : $treedrawer->set_width(1000);
205 Function: sets the width of the drawer canvas.
206 Returns :
207 Args : Integer width in pixels.
208  
209 =cut
210  
211 3 ▶sub set_width {
Subroutine set_width: called CC=3 SCAR=12.7
Pod documented
212 3 my ( $self, $width ) = @_;
213 3 ▶ if ( looks_like_number $width && $width > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($width) and $width > 0) { } 3 0 3
Condition33%1/3
&looks_like_number($width) and $width > 0
!ll&&!rl&&r
003
MC/DC0%0/2
&looks_like_number($width) and $width > 0
&looks_like_number($width) $width > 0
Truth table33%1/3
A and B
A= &looks_like_number($width)B= $width > 0
ABresult
0X0
100
111
214 3 $self->{'WIDTH'} = $width;
215 }
216 else {
217 0 throw 'BadNumber' => "'$width' is not a valid image width";
218 }
219 3 return $self;
220 }
221  
222 =item set_height()
223  
224 Sets image height.
225  
226 Type : Mutator
227 Title : set_height
228 Usage : $treedrawer->set_height(1000);
229 Function: sets the height of the canvas.
230 Returns :
231 Args : Integer height in pixels.
232  
233 =cut
234  
235 3 ▶sub set_height {
Subroutine set_height: called CC=3 SCAR=12.7
Pod documented
236 3 my ( $self, $height ) = @_;
237 3 ▶ if ( looks_like_number $height && $height > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($height) and $height > 0) { } 3 0 3
Condition33%1/3
&looks_like_number($height) and $height > 0
!ll&&!rl&&r
003
MC/DC0%0/2
&looks_like_number($height) and $height > 0
&looks_like_number($height) $height > 0
Truth table33%1/3
A and B
A= &looks_like_number($height)B= $height > 0
ABresult
0X0
100
111
238 3 $self->{'HEIGHT'} = $height;
239 }
240 else {
241 0 throw 'BadNumber' => "'$height' is not a valid image height";
242 }
243 3 return $self;
244 }
245  
246 =item set_mode()
247  
248 Sets tree drawing mode.
249  
250 Type : Mutator
251 Title : set_mode
252 Usage : $treedrawer->set_mode('clado');
253 Function: Sets the tree mode, i.e. cladogram
254 or phylogram.
255 Returns : Invocant.
256 Args : String, [clado|phylo]
257  
258 =cut
259  
260 3 ▶sub set_mode {
Subroutine set_mode: called CC=2 SCAR=7.4
Pod documented
261 3 my ( $self, $mode ) = @_;
262 3 ▶ if ( $mode =~ m/^(?:clado|phylo)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ /^(?:clado|phylo)$/i) { } 3 0 3
263 3 $self->{'MODE'} = uc $mode;
264 }
265 else {
266 0 throw 'BadFormat' => "'$mode' is not a valid drawing mode";
267 }
268 3 return $self;
269 }
270  
271 =item set_shape()
272  
273 Sets tree drawing shape.
274  
275 Type : Mutator
276 Title : set_shape
277 Usage : $treedrawer->set_shape('rect');
278 Function: Sets the tree shape, i.e.
279 rectangular, diagonal, curvy or radial.
280 Returns : Invocant.
281 Args : String, [rect|diag|curvy|radial]
282  
283 =cut
284  
285 3 ▶sub set_shape {
Subroutine set_shape: called CC=2 SCAR=7.4
Pod documented
286 3 my ( $self, $shape ) = @_;
287 3 ▶ if ( $shape =~ m/^(?:rect|diag|curvy|radial|unrooted)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shape =~ /^(?:rect|diag|curvy|radial|unrooted)/i) { } 3 0 3
288 3 $self->{'SHAPE'} = uc $shape;
289 }
290 else {
291 0 throw 'BadFormat' => "'$shape' is not a valid drawing shape";
292 }
293 3 return $self;
294 }
295  
296 =item set_padding()
297  
298 Sets image padding.
299  
300 Type : Mutator
301 Title : set_padding
302 Usage : $treedrawer->set_padding(100);
303 Function: Sets the canvas padding.
304 Returns :
305 Args : Integer value in pixels.
306  
307 =cut
308  
309 1 ▶sub set_padding {
Subroutine set_padding: called CC=3 SCAR=12.7
Pod documented
310 1 my ( $self, $padding ) = @_;
311 1 ▶ if ( looks_like_number $padding && $padding > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($padding) and $padding > 0) { } 1 0 1
Condition33%1/3
&looks_like_number($padding) and $padding > 0
!ll&&!rl&&r
001
MC/DC0%0/2
&looks_like_number($padding) and $padding > 0
&looks_like_number($padding) $padding > 0
Truth table33%1/3
A and B
A= &looks_like_number($padding)B= $padding > 0
ABresult
0X0
100
111
312 1 $self->{'PADDING'} = $padding;
313 }
314 else {
315 0 throw 'BadNumber' => "'$padding' is not a valid padding value";
316 }
317 1 return $self;
318 }
319  
320 =item set_text_horiz_offset()
321  
322 Sets text horizontal offset.
323  
324 Type : Mutator
325 Title : set_text_horiz_offset
326 Usage : $treedrawer->set_text_horiz_offset(5);
327 Function: Sets the distance between
328 tips and text, in pixels.
329 Returns :
330 Args : Integer value in pixels.
331  
332 =cut
333  
334 1 ▶sub set_text_horiz_offset {
Subroutine set_text_horiz_offset: called CC=2 SCAR=7.4
Pod documented
335 1 my ( $self, $offset ) = @_;
336 1 ▶ if ( looks_like_number $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($offset)) { } 1 0 1
337 1 $self->{'TEXT_HORIZ_OFFSET'} = $offset;
338 }
339 else {
340 0 throw 'BadNumber' =>
341 "'$offset' is not a valid text horizontal offset value";
342 }
343 1 return $self;
344 }
345  
346 =item set_text_vert_offset()
347  
348 Sets text vertical offset.
349  
350 Type : Mutator
351 Title : set_text_vert_offset
352 Usage : $treedrawer->set_text_vert_offset(3);
353 Function: Sets the text baseline
354 relative to the tips, in pixels.
355 Returns :
356 Args : Integer value in pixels.
357  
358 =cut
359  
360 1 ▶sub set_text_vert_offset {
Subroutine set_text_vert_offset: called CC=2 SCAR=7.4
Pod documented
361 1 my ( $self, $offset ) = @_;
362 1 ▶ if ( looks_like_number $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($offset)) { } 1 0 1
363 1 $self->{'TEXT_VERT_OFFSET'} = $offset;
364 }
365 else {
366 0 throw 'BadNumber' =>
367 "'$offset' is not a valid text vertical offset value";
368 }
369 1 return $self;
370 }
371  
372 =item set_text_width()
373  
374 Sets text width.
375  
376 Type : Mutator
377 Title : set_text_width
378 Usage : $treedrawer->set_text_width(150);
379 Function: Sets the canvas width for
380 terminal taxon names.
381 Returns :
382 Args : Integer value in pixels.
383  
384 =cut
385  
386 1 ▶sub set_text_width {
Subroutine set_text_width: called CC=3 SCAR=12.7
Pod documented
387 1 my ( $self, $width ) = @_;
388 1 ▶ if ( looks_like_number $width && $width > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($width) and $width > 0) { } 1 0 1
Condition33%1/3
&looks_like_number($width) and $width > 0
!ll&&!rl&&r
001
MC/DC0%0/2
&looks_like_number($width) and $width > 0
&looks_like_number($width) $width > 0
Truth table33%1/3
A and B
A= &looks_like_number($width)B= $width > 0
ABresult
0X0
100
111
389 1 $self->{'TEXT_WIDTH'} = $width;
390 }
391 else {
392 0 throw 'BadNumber' => "'$width' is not a valid text width value";
393 }
394 1 return $self;
395 }
396  
397 =item set_tree()
398  
399 Sets tree to draw.
400  
401 Type : Mutator
402 Title : set_tree
403 Usage : $treedrawer->set_tree($tree);
404 Function: Sets the Bio::Phylo::Forest::Tree
405 object to unparse.
406 Returns :
407 Args : A Bio::Phylo::Forest::Tree object.
408  
409 =cut
410  
411 3 ▶sub set_tree {
Subroutine set_tree: called CC=3 SCAR=12.0
Pod documented
412 3 my ( $self, $tree ) = @_;
413 3 ▶ if ( looks_like_object $tree, _TREE_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_object($tree, 3)) 3 0 3
414 3 $self->{'TREE'} = $tree->negative_to_zero;
415 3 my $root = $tree->get_root;
416 3 ▶ if ( my $length = $root->get_branch_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $length = $root->get_branch_length) 0 3 3
417 0 $logger->warn("Collapsing root branch length of $length");
418 0 $root->set_branch_length(0);
419 }
420 }
421 3 return $self;
422 }
423  
424 =item set_scale_options()
425  
426 Sets time scale options.
427  
428 Type : Mutator
429 Title : set_scale_options
430 Usage : $treedrawer->set_scale_options(
431 -width => 400,
432 -major => '10%', # major cross hatch interval
433 -minor => '2%', # minor cross hatch interval
434 -blocks => '10%', # alternating blocks in light-gray
435 -label => 'MYA',
436 -reverse => 1, # tips are 0
437 -tmpl => '%d', # sprintf template for major cross hatch numbers
438 -font => {
439 -face => 'Verdana',
440 -size => 11,
441 }
442 );
443 Function: Sets the options for time (distance) scale
444 Returns :
445 Args :
446 -width => 400,
447 -major => '10%', # major cross hatch interval
448 -minor => '2%', # minor cross hatch interval
449 -blocks => '10%', # alternating blocks in light-gray
450 -label => 'MYA',
451 -reverse => 1, # tips are 0
452 -tmpl => '%d', # sprintf template for major cross hatch numbers
453 -font => {
454 -face => 'Verdana',
455 -size => 11,
456 }
457  
458 =cut
459  
460 2 ▶sub set_scale_options {
Subroutine set_scale_options: called CC=13 SCAR=34.3
Pod documented
461 2 my $self = shift;
462 2 ▶ if ( ( @_ && !scalar @_ % 2 ) || ( scalar @_ == 1 && ref $_[0] eq 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not scalar @_ % 2 or scalar @_ == 1 and ref $_[0] eq 'HASH') { } 2 0 2
Condition22%2/9
@_ and not scalar @_ % 2 or scalar @_ == 1 and ref $_[0] eq "HASH"
l!l&&r!l&&!r
200
@_ and not scalar @_ % 2
!ll&&!rl&&r
002
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/4
@_ and not scalar @_ % 2 or scalar @_ == 1 and ref $_[0] eq "HASH"
@_ not scalar @_ % 2 scalar @_ == 1 ref $_[0] eq "HASH"
Truth table14%1/7
A and not(B) or C and D
A= @_B= not scalar @_ % 2C= scalar @_ == 1D= ref $_[0] eq "HASH"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
463 2 my %o; # %options
464 2 ▶ if ( scalar @_ == 1 && ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] eq 'HASH') { } 0 2 2
Condition33%1/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
200
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
465 0 %o = %{ $_[0] };
466 }
467 else {
468 2 %o = looks_like_hash @_;
469 }
470
471 # copy verbatim
472 2 $self->{'SCALE'}->{'-label'} = $o{'-label'};
473 2 $self->{'SCALE'}->{'-units'} = $o{'-units'};
474 2 $self->{'SCALE'}->{'-reverse'} = $o{'-reverse'};
475 2 $self->{'SCALE'}->{'-font'} = $o{'-font'};
476 2 $self->{'SCALE'}->{'-tmpl'} = $o{'-tmpl'};
477 2 $self->{'SCALE'}->{'-blocks'} = $o{'-blocks'};
478
479 # set scale width, either pixels or relative to tree
480 2 ▶ if ( looks_like_number $o{'-width'} or $o{'-width'} =~ m/^\d+%$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($o{'-width'}) or $o{'-width'} =~ /^\d+%$/) { } 2 0 2
Condition33%1/3
&looks_like_number($o{"-width"}) or $o{"-width"} =~ /^\d+%$/
l!l&&r!l&&!r
020
MC/DC0%0/2
&looks_like_number($o{"-width"}) or $o{"-width"} =~ /^\d+%$/
&looks_like_number($o{"-width"}) $o{"-width"} =~ /^\d+%$/
Truth table33%1/3
A or B
A= &looks_like_number($o{"-width"})B= $o{"-width"} =~ /^\d+%$/
ABresult
1X1
011
000
481 2 $self->{'SCALE'}->{'-width'} = $o{'-width'};
482 }
483 else {
484 0 throw 'BadArgs' => "\"$o{'-width'}\" is invalid for '-width'";
485 }
486
487 # set major tick mark distances
488 2 ▶ if ( looks_like_number $o{'-major'} or $o{'-major'} =~ m/^\d+%$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($o{'-major'}) or $o{'-major'} =~ /^\d+%$/) { } 2 0 2
Condition33%1/3
&looks_like_number($o{"-major"}) or $o{"-major"} =~ /^\d+%$/
l!l&&r!l&&!r
020
MC/DC0%0/2
&looks_like_number($o{"-major"}) or $o{"-major"} =~ /^\d+%$/
&looks_like_number($o{"-major"}) $o{"-major"} =~ /^\d+%$/
Truth table33%1/3
A or B
A= &looks_like_number($o{"-major"})B= $o{"-major"} =~ /^\d+%$/
ABresult
1X1
011
000
489 2 $self->{'SCALE'}->{'-major'} = $o{'-major'};
490 }
491 else {
492 0 throw 'BadArgs' => "\"$o{'-major'}\" is invalid for '-major'";
493 }
494
495 # set minor tick mark distances
496 2 ▶ if ( looks_like_number $o{'-minor'} or $o{'-minor'} =~ m/^\d+%$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($o{'-minor'}) or $o{'-minor'} =~ /^\d+%$/) { } 2 0 2
Condition33%1/3
&looks_like_number($o{"-minor"}) or $o{"-minor"} =~ /^\d+%$/
l!l&&r!l&&!r
020
MC/DC0%0/2
&looks_like_number($o{"-minor"}) or $o{"-minor"} =~ /^\d+%$/
&looks_like_number($o{"-minor"}) $o{"-minor"} =~ /^\d+%$/
Truth table33%1/3
A or B
A= &looks_like_number($o{"-minor"})B= $o{"-minor"} =~ /^\d+%$/
ABresult
1X1
011
000
497 2 $self->{'SCALE'}->{'-minor'} = $o{'-minor'};
498 }
499 else {
500 0 throw 'BadArgs' => "\"$o{'-minor'}\" is invalid for '-minor'";
501 }
502 }
503 else {
504 0 throw 'OddHash' => 'Odd number of elements in hash assignment';
505 }
506 2 return $self;
507 }
508  
509 =item set_pie_colors
510  
511 Sets a hash reference whose keys are (unique) names for the different segments in a
512 likelihood pie chart, and whose values are color codes.
513  
514 Type : Mutator
515 Title : set_pie_colors
516 Usage : $treedrawer->set_pie_colors({ 'p1' => 'red', 'p2' => 'blue' });
517 Function: sets likelihood pie colors
518 Returns :
519 Args : HASH
520  
521 =cut
522  
523 0 ▶sub set_pie_colors {
Subroutine set_pie_colors: not called CC=2 SCAR=17.9
Pod documented
524 0 my ( $self, $hash ) = @_;
525 0 ▶ if ( ref($hash) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash eq 'HASH') { } 0 0 0
526 0 $self->{'PIE_COLORS'} = $hash;
527 }
528 else {
529 0 throw 'BadArgs' => "Not a hash reference!";
530 }
531 0 return $self;
532 }
533  
534 =back
535  
536 =head2 CASCADING MUTATORS
537  
538 =over
539  
540 =item set_branch_width()
541  
542 Sets branch width.
543  
544 Type : Mutator
545 Title : set_branch_width
546 Usage : $treedrawer->set_branch_width(1);
547 Function: sets the width of branch lines
548 Returns :
549 Args : Integer width in pixels.
550  
551 =cut
552  
553 0 ▶sub set_branch_width {
Subroutine set_branch_width: not called CC=3 SCAR=24.8
Pod documented
554 0 my ( $self, $width ) = @_;
555 0 ▶ if ( looks_like_number $width && $width > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($width) and $width > 0) { } 0 0 0
Condition0%0/3
&looks_like_number($width) and $width > 0
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_number($width) and $width > 0
&looks_like_number($width) $width > 0
Truth table0%0/3
A and B
A= &looks_like_number($width)B= $width > 0
ABresult
0X0
100
111
556 0 $self->_cascading_setter($width);
557 }
558 else {
559 0 throw 'BadNumber' => "'$width' is not a valid branch width";
560 }
561 0 return $self;
562 }
563  
564 =item set_node_radius()
565  
566 Sets node radius.
567  
568 Type : Mutator
569 Title : set_node_radius
570 Usage : $treedrawer->set_node_radius(20);
571 Function: Sets the node radius in pixels.
572 Returns :
573 Args : Integer value in pixels.
574  
575 =cut
576  
577 2 ▶sub set_node_radius {
Subroutine set_node_radius: called CC=3 SCAR=12.7
Pod documented
578 2 my ( $self, $radius ) = @_;
579 2 ▶ if ( looks_like_number $radius && $radius >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($radius) and $radius >= 0) { } 2 0 2
Condition33%1/3
&looks_like_number($radius) and $radius >= 0
!ll&&!rl&&r
002
MC/DC0%0/2
&looks_like_number($radius) and $radius >= 0
&looks_like_number($radius) $radius >= 0
Truth table33%1/3
A and B
A= &looks_like_number($radius)B= $radius >= 0
ABresult
0X0
100
111
580 2 $self->_cascading_setter($radius);
581 }
582 else {
583 0 throw 'BadNumber' => "'$radius' is not a valid node radius value";
584 }
585 2 return $self;
586 }
587  
588 =item set_collapsed_clade_width()
589  
590 Sets collapsed clade width.
591  
592 Type : Mutator
593 Title : set_collapsed_clade_width
594 Usage : $treedrawer->set_collapsed_clade_width(6);
595 Function: sets the width of collapsed clade triangles relative to uncollapsed tips
596 Returns :
597 Args : Positive number
598  
599 =cut
600  
601 0 ▶sub set_collapsed_clade_width {
Subroutine set_collapsed_clade_width: not called CC=3 SCAR=24.8
Pod documented
602 0 my ( $self, $width ) = @_;
603 0 ▶ if ( looks_like_number $width && $width > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($width) and $width > 0) { } 0 0 0
Condition0%0/3
&looks_like_number($width) and $width > 0
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_number($width) and $width > 0
&looks_like_number($width) $width > 0
Truth table0%0/3
A and B
A= &looks_like_number($width)B= $width > 0
ABresult
0X0
100
111
604 0 $self->_cascading_setter($width);
605 }
606 else {
607 0 throw 'BadNumber' => "'$width' is not a valid image width";
608 }
609 0 return $self;
610 }
611  
612 =item set_clade_label_width
613  
614 Sets clade label width, i.e. the spacing between nested clade annotations
615  
616 Type : Mutator
617 Title : set_clade_label_width
618 Usage : $treedrawer->set_clade_label_width(6);
619 Function: sets the spacing between nested clade annotations
620 Returns :
621 Args : Positive number
622  
623 =cut
624  
625 0 ▶sub set_clade_label_width {
Subroutine set_clade_label_width: not called CC=3 SCAR=24.8
Pod documented
626 0 my ( $self, $width ) = @_;
627 0 ▶ if ( looks_like_number $width && $width >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($width) and $width >= 0) { } 0 0 0
Condition0%0/3
&looks_like_number($width) and $width >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_number($width) and $width >= 0
&looks_like_number($width) $width >= 0
Truth table0%0/3
A and B
A= &looks_like_number($width)B= $width >= 0
ABresult
0X0
100
111
628 0 $self->{'CLADE_LABEL_WIDTH'} = $width;
629 }
630 else {
631 0 throw 'BadNumber' => "'$width' is not a valid clade label width value";
632 }
633 0 return $self;
634 }
635  
636 =item set_tip_radius()
637  
638 Sets tip radius.
639  
640 Type : Mutator
641 Title : set_tip_radius
642 Usage : $treedrawer->set_tip_radius(20);
643 Function: Sets the tip radius in pixels.
644 Returns :
645 Args : Integer value in pixels.
646  
647 =cut
648  
649 0 ▶sub set_tip_radius {
Subroutine set_tip_radius: not called CC=3 SCAR=24.8
Pod documented
650 0 my ( $self, $radius ) = @_;
651 0 ▶ if ( looks_like_number $radius && $radius >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($radius) and $radius >= 0) { } 0 0 0
Condition0%0/3
&looks_like_number($radius) and $radius >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_number($radius) and $radius >= 0
&looks_like_number($radius) $radius >= 0
Truth table0%0/3
A and B
A= &looks_like_number($radius)B= $radius >= 0
ABresult
0X0
100
111
652 0 $self->_cascading_setter($radius);
653 }
654 else {
655 0 throw 'BadNumber' => "'$radius' is not a valid tip radius value";
656 }
657 0 return $self;
658 }
659  
660 =back
661  
662 =head2 ACCESSORS
663  
664 =over
665  
666 =item get_format()
667  
668 Gets image format.
669  
670 Type : Accessor
671 Title : get_format
672 Usage : my $format = $treedrawer->get_format;
673 Function: Gets the image format.
674 Returns :
675 Args : None.
676  
677 =cut
678  
679 3 ▶sub get_format { shift->{'FORMAT'} }
Subroutine get_format: called CC=1 SCAR=0.0
Pod documented
680  
681 =item get_format_mime()
682  
683 Gets image format as MIME type.
684  
685 Type : Accessor
686 Title : get_format_mime
687 Usage : print "Content-type: ", $treedrawer->get_format_mime, "\n\n";
688 Function: Gets the image format as MIME type.
689 Returns :
690 Args : None.
691  
692 =cut
693  
694 0 ▶sub get_format_mime {
Subroutine get_format_mime: not called CC=1 SCAR=6.9
Pod documented
695 0 my $self = shift;
696 0 my %mapping = (
697 'canvas' => 'text/html',
698 'gif' => 'image/gif',
699 'jpeg' => 'image/jpeg',
700 'pdf' => 'application/pdf',
701 'png' => 'image/png',
702 'processing' => 'text/plain',
703 'svg' => 'image/svg+xml',
704 'swf' => 'application/x-shockwave-flash',
705 );
706 0 return $mapping{ lc $self->get_format };
707 }
708  
709 =item get_width()
710  
711 Gets image width.
712  
713 Type : Accessor
714 Title : get_width
715 Usage : my $width = $treedrawer->get_width;
716 Function: Gets the width of the drawer canvas.
717 Returns :
718 Args : None.
719  
720 =cut
721  
722 6 ▶sub get_width { shift->{'WIDTH'} }
Subroutine get_width: called CC=1 SCAR=0.0
Pod documented
723  
724 =item get_height()
725  
726 Gets image height.
727  
728 Type : Accessor
729 Title : get_height
730 Usage : my $height = $treedrawer->get_height;
731 Function: Gets the height of the canvas.
732 Returns :
733 Args : None.
734  
735 =cut
736  
737 13 ▶sub get_height { shift->{'HEIGHT'} }
Subroutine get_height: called CC=1 SCAR=0.0
Pod documented
738  
739 =item get_mode()
740  
741 Gets tree drawing mode.
742  
743 Type : Accessor
744 Title : get_mode
745 Usage : my $mode = $treedrawer->get_mode('clado');
746 Function: Gets the tree mode, i.e. cladogram or phylogram.
747 Returns :
748 Args : None.
749  
750 =cut
751  
752 3 ▶sub get_mode { shift->{'MODE'} }
Subroutine get_mode: called CC=1 SCAR=0.0
Pod documented
753  
754 =item get_shape()
755  
756 Gets tree drawing shape.
757  
758 Type : Accessor
759 Title : get_shape
760 Usage : my $shape = $treedrawer->get_shape;
761 Function: Gets the tree shape, i.e. rectangular,
762 diagonal, curvy or radial.
763 Returns :
764 Args : None.
765  
766 =cut
767  
768 17 ▶sub get_shape { shift->{'SHAPE'} }
Subroutine get_shape: called CC=1 SCAR=0.0
Pod documented
769  
770 =item get_padding()
771  
772 Gets image padding.
773  
774 Type : Accessor
775 Title : get_padding
776 Usage : my $padding = $treedrawer->get_padding;
777 Function: Gets the canvas padding.
778 Returns :
779 Args : None.
780  
781 =cut
782  
783 3 ▶sub get_padding { shift->{'PADDING'} }
Subroutine get_padding: called CC=1 SCAR=0.0
Pod documented
784  
785 =item get_text_horiz_offset()
786  
787 Gets text horizontal offset.
788  
789 Type : Accessor
790 Title : get_text_horiz_offset
791 Usage : my $text_horiz_offset =
792 $treedrawer->get_text_horiz_offset;
793 Function: Gets the distance between
794 tips and text, in pixels.
795 Returns : SCALAR
796 Args : None.
797  
798 =cut
799  
800 17 ▶sub get_text_horiz_offset { shift->{'TEXT_HORIZ_OFFSET'} }
Subroutine get_text_horiz_offset: called CC=1 SCAR=0.0
Pod documented
801  
802 =item get_text_vert_offset()
803  
804 Gets text vertical offset.
805  
806 Type : Accessor
807 Title : get_text_vert_offset
808 Usage : my $text_vert_offset =
809 $treedrawer->get_text_vert_offset;
810 Function: Gets the text baseline relative
811 to the tips, in pixels.
812 Returns :
813 Args : None.
814  
815 =cut
816  
817 14 ▶sub get_text_vert_offset { shift->{'TEXT_VERT_OFFSET'} }
Subroutine get_text_vert_offset: called CC=1 SCAR=0.0
Pod documented
818  
819 =item get_text_width()
820  
821 Gets text width.
822  
823 Type : Accessor
824 Title : get_text_width
825 Usage : my $textwidth =
826 $treedrawer->get_text_width;
827 Function: Returns the canvas width
828 for terminal taxon names.
829 Returns :
830 Args : None.
831  
832 =cut
833  
834 3 ▶sub get_text_width { shift->{'TEXT_WIDTH'} }
Subroutine get_text_width: called CC=1 SCAR=0.0
Pod documented
835  
836 =item get_tree()
837  
838 Gets tree to draw.
839  
840 Type : Accessor
841 Title : get_tree
842 Usage : my $tree = $treedrawer->get_tree;
843 Function: Returns the Bio::Phylo::Forest::Tree
844 object to unparse.
845 Returns : A Bio::Phylo::Forest::Tree object.
846 Args : None.
847  
848 =cut
849  
850 31 ▶sub get_tree { shift->{'TREE'} }
Subroutine get_tree: called CC=1 SCAR=0.0
Pod documented
851  
852 =item get_scale_options()
853  
854 Gets time scale option.
855  
856 Type : Accessor
857 Title : get_scale_options
858 Usage : my %options = %{
859 $treedrawer->get_scale_options
860 };
861 Function: Returns the time/distance
862 scale options.
863 Returns : A hash ref.
864 Args : None.
865  
866 =cut
867  
868 3 ▶sub get_scale_options { shift->{'SCALE'} }
Subroutine get_scale_options: called CC=1 SCAR=0.0
Pod documented
869  
870 =item get_pie_colors
871  
872 Gets a hash reference whose keys are (unique) names for the different segments in a
873 likelihood pie chart, and whose values are color codes.
874  
875 Type : Accessor
876 Title : get_pie_colors
877 Usage : my %h = %{ $treedrawer->get_pie_colors() };
878 Function: gets likelihood pie colors
879 Returns :
880 Args : None
881  
882 =cut
883  
884 2 ▶sub get_pie_colors { shift->{'PIE_COLORS'} }
Subroutine get_pie_colors: called CC=1 SCAR=0.0
Pod documented
885  
886 =item get_clade_label_width
887  
888 Gets clade label width, i.e. the spacing between nested clade annotations
889  
890 Type : Mutator
891 Title : get_clade_label_width
892 Usage : my $width = $treedrawer->get_clade_label_width();
893 Function: gets the spacing between nested clade annotations
894 Returns :
895 Args : None
896  
897 =cut
898  
899 0 ▶sub get_clade_label_width { shift->{'CLADE_LABEL_WIDTH'} }
Subroutine get_clade_label_width: not called CC=1 SCAR=6.9
Pod documented
900  
901 =back
902  
903 =head2 CASCADING ACCESSORS
904  
905 =over
906  
907 =item get_branch_width()
908  
909 Gets branch width.
910  
911 Type : Accessor
912 Title : get_branch_width
913 Usage : my $w = $treedrawer->get_branch_width();
914 Function: gets the width of branch lines
915 Returns :
916 Args : Integer width in pixels.
917  
918 =cut
919  
920 14 ▶sub get_branch_width {
Subroutine get_branch_width: called CC=1 SCAR=0.0
Pod documented
921 14 my $self = shift;
922 14 return $self->_cascading_getter(@_);
923 }
924  
925 =item get_collapsed_clade_width()
926  
927 Gets collapsed clade width.
928  
929 Type : Mutator
930 Title : get_collapsed_clade_width
931 Usage : $w = $treedrawer->get_collapsed_clade_width();
932 Function: gets the width of collapsed clade triangles relative to uncollapsed tips
933 Returns : Positive number
934 Args : None
935  
936 =cut
937  
938 3 ▶sub get_collapsed_clade_width {
Subroutine get_collapsed_clade_width: called CC=1 SCAR=0.0
Pod documented
939 3 my $self = shift;
940 3 return $self->_cascading_getter(@_);
941 }
942  
943 =item get_node_radius()
944  
945 Gets node radius.
946  
947 Type : Accessor
948 Title : get_node_radius
949 Usage : my $node_radius = $treedrawer->get_node_radius;
950 Function: Gets the node radius in pixels.
951 Returns : SCALAR
952 Args : None.
953  
954 =cut
955  
956 14 ▶sub get_node_radius {
Subroutine get_node_radius: called CC=1 SCAR=0.0
Pod documented
957 14 my $self = shift;
958 14 return $self->_cascading_getter(@_);
959 }
960  
961 =item get_tip_radius()
962  
963 Gets tip radius.
964  
965 Type : Accessor
966 Title : get_tip_radius
967 Usage : my $tip_radius = $treedrawer->get_tip_radius;
968 Function: Gets the tip radius in pixels.
969 Returns : SCALAR
970 Args : None.
971  
972 =cut
973  
974 20 ▶sub get_tip_radius {
Subroutine get_tip_radius: called CC=1 SCAR=0.0
Pod documented
975 20 my $self = shift;
976 20 return $self->_cascading_getter(@_);
977 }
978  
979 =begin comment
980  
981 Type : Internal method.
982 Title : _set_scalex
983 Usage : $treedrawer->_set_scalex($scalex);
984 Function:
985 Returns :
986 Args :
987  
988 =end comment
989  
990 =cut
991  
992 3 ▶sub _set_scalex {
Subroutine _set_scalex: called CC=2 SCAR=7.4
993 3 my $self = shift;
994 3 ▶ if ( looks_like_number $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($_[0])) { } 3 0 3
995 3 $self->{'_SCALEX'} = $_[0];
996 }
997 else {
998 0 throw 'BadNumber' => "\"$_[0]\" is not a valid number value";
999 }
1000 3 return $self;
1001 }
1002 0 ▶sub _get_scalex { shift->{'_SCALEX'} }
Subroutine _get_scalex: not called CC=1 SCAR=6.9
1003  
1004 =begin comment
1005  
1006 Type : Internal method.
1007 Title : _set_scaley
1008 Usage : $treedrawer->_set_scaley($scaley);
1009 Function:
1010 Returns :
1011 Args :
1012  
1013 =end comment
1014  
1015 =cut
1016  
1017 3 ▶sub _set_scaley {
Subroutine _set_scaley: called CC=2 SCAR=7.4
1018 3 my $self = shift;
1019 3 ▶ if ( looks_like_number $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($_[0])) { } 3 0 3
1020 3 $self->{'_SCALEY'} = $_[0];
1021 }
1022 else {
1023 0 throw 'BadNumber' => "\"$_[0]\" is not a valid integer value";
1024 }
1025 3 return $self;
1026 }
1027 0 ▶sub _get_scaley { shift->{'_SCALEY'} }
Subroutine _get_scaley: not called CC=1 SCAR=6.9
1028  
1029 =back
1030  
1031 =head2 TREE DRAWING
1032  
1033 =over
1034  
1035 =item draw()
1036  
1037 Creates tree drawing.
1038  
1039 Type : Unparsers
1040 Title : draw
1041 Usage : my $drawing = $treedrawer->draw;
1042 Function: Unparses a Bio::Phylo::Forest::Tree
1043 object into a drawing.
1044 Returns : SCALAR
1045 Args :
1046  
1047 =cut
1048  
1049 3 ▶sub draw {
Subroutine draw: called CC=2 SCAR=7.1
Pod documented
1050 3 my $self = shift;
1051 3 ▶ if ( !$self->get_tree ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_tree) 0 3 3
1052 0 throw 'BadArgs' => "Can't draw an undefined tree";
1053 }
1054 3 my $root = $self->get_tree->get_root;
1055  
1056 # Reset the stored data in the tree
1057 3 $self->_reset_internal($root);
1058 3 $logger->debug("going to compute coordinates");
1059 3 $self->compute_coordinates;
1060 3 $logger->debug("going to render figure");
1061 3 return $self->render;
1062 }
1063  
1064 3 ▶sub compute_coordinates {
Subroutine compute_coordinates: called CC=2 SCAR=7.4
Pod undocumented
1065 3 my $self = shift;
1066 3 ▶ if ( $self->get_shape =~ m/(?:radial|unrooted)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_shape =~ /(?:radial|unrooted)/i) { } 0 3 3
1067 0 $self->_compute_unrooted_coordinates;
1068 }
1069 else {
1070 3 $self->_compute_rooted_coordinates;
1071 }
1072 3 return $self;
1073 }
1074  
1075 0 ▶sub polar_to_cartesian {
Subroutine polar_to_cartesian: not called CC=1 SCAR=6.9
Pod undocumented
1076 0 my ( $self, $radius, $angleInDegrees ) = @_;
1077 0 my $angleInRadians = $angleInDegrees * $PI / 180.0;
1078 0 my $x = $radius * cos($angleInRadians);
1079 0 my $y = $radius * sin($angleInRadians);
1080 0 return $x, $y;
1081 }
1082  
1083 0 ▶sub cartesian_to_polar {
Subroutine cartesian_to_polar: not called CC=1 SCAR=6.9
Pod undocumented
1084 0 my ( $self, $x, $y ) = @_;
1085 0 my $angleInDegrees = atan2( $y, $x ) / $PI * 180;
1086 0 my $radius = sqrt( $y ** 2 + $x ** 2 );
1087 0 return $radius, $angleInDegrees;
1088 }
1089  
1090 0 ▶sub _compute_unrooted_coordinates {
Subroutine _compute_unrooted_coordinates: not called CC=5 SCAR=34.0
1091 0 my $self = shift;
1092 0 my $tre = $self->get_tree;
1093 0 ▶ my $phy = $self->get_mode =~ /^p/i ? $tre->is_cladogram ? 0 : 1 : 0; # phylogram?
Branch: 2 branches
BranchTrueFalseTotal
$self->get_mode =~ /^p/i ? : 0 0 0
$tre->is_cladogram ? : 0 0 0
1094
1095 # compute unscaled rotation, depth and tip count
1096 0 my ( %unscaled_rotation, %depth );
1097 0 my ( $total_tips, $total_depth ) = ( 0, 0 );
1098
1099 $tre->visit_depth_first(
1100 # process tips first
1101 0 ▶ '-no_daughter' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1102 0 my $node = shift;
1103 0 my $id = $node->get_id;
1104 0 ( $unscaled_rotation{$id}, $depth{$id} ) = ( $total_tips, 0 );
1105 0 $total_tips++;
1106 },
1107
1108 # then process internals
1109 0 ▶ '-post_daughter' => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
1110 0 my $node = shift;
1111 0 my $id = $node->get_id;
1112
1113 # get deepest child and average rotation
1114 0 my @child = @{ $node->get_children };
1115 0 my ( $unscaled_rotation, $depth ) = ( 0, 0 );
1116 0 for my $c ( @child ) {
1117 0 my $cid = $c->get_id;
1118 0 my $c_depth = $depth{$cid};
1119 0 $unscaled_rotation += $unscaled_rotation{$cid};
1120 0 ▶ $depth = $c_depth if $c_depth > $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $c_depth > $depth 0 0 0
1121 }
1122
1123 # increment depth
1124 0 ▶ if ( $phy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phy) { } 0 0 0
1125 0 my @mapped = map { $_->get_branch_length } @child;
1126 0 my ($tallest) = sort { $b <=> $a } @mapped;
1127 0 $depth += $tallest;
1128 }
1129 else {
1130 0 $depth++;
1131 }
1132
1133 # update rotation
1134 0 $unscaled_rotation /= scalar(@child);
1135
1136 # check to see if current depth is overal deepest
1137 0 ▶ $total_depth = $depth if $depth > $total_depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > $total_depth 0 0 0
1138
1139 # store results
1140 0 ( $unscaled_rotation{$id}, $depth{$id} ) =
1141 ( $unscaled_rotation, $depth );
1142 },
1143 0 );
1144  
1145 # root, exactly centered on the canvas
1146 0 my $center_x = $self->get_width / 2;
1147 0 my $center_y = $self->get_height / 2;
1148
1149 0 my $horiz_offset = $self->get_text_horiz_offset;
1150 0 my $text_width = $self->get_text_width;
1151 0 my $padding = $self->get_padding;
1152 0 my $range = $center_x - ( $horiz_offset + $text_width + $padding );
1153
1154 # cladogram branch length
1155 0 $self->_set_scalex( $range / $total_depth );
1156 0 $self->_set_scaley( $range / $total_depth );
1157
1158 0 for my $n ( @{ $tre->get_entities } ) {
1159 0 ▶ if ( $n->is_root ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->is_root) { } 0 0 0
1160 0 $n->set_x( $center_x );
1161 0 $n->set_y( $center_y );
1162 }
1163 else {
1164 0 my $id = $n->get_id;
1165 0 my ( $unscaled_rotation, $depth ) = ( $unscaled_rotation{$id}, $depth{$id} );
1166 0 my $radius = $self->_get_scalex * ( $depth - $total_depth ) * -1;
1167 0 my $rotation = $unscaled_rotation / $total_tips * 360;
1168 0 my ( $x, $y ) = $self->polar_to_cartesian( $radius, $rotation );
1169 0 $n->set_x( $x + $center_x );
1170 0 $n->set_y( $y + $center_y );
1171 0 $n->set_rotation( $rotation );
1172 0 $n->set_generic( 'radius' => $radius );
1173 }
1174 }
1175 }
1176  
1177 3 ▶sub _compute_rooted_coordinates {
Subroutine _compute_rooted_coordinates: called CC=2 SCAR=6.9
1178 3 my $td = shift;
1179 3 my $tree = $td->get_tree;
1180 3 ▶ my $phylo = $td->get_mode =~ /^p/i ? 1 : 0; # phylogram or cladogram
Branch: 1 branch
BranchTrueFalseTotal
$td->get_mode =~ /^p/i ? : 2 1 3
1181 3 my $padding = $td->get_padding;
1182 3 my $width = $td->get_width - ( $td->get_text_width + ( $padding * 2 ) );
1183 3 my $height = $td->get_height - ( $padding * 2 );
1184 3 my $cladew = $td->get_collapsed_clade_width;
1185 3 my ( $tip_counter, $tallest_tip ) = ( 0, 0 );
1186 $tree->visit_depth_first(
1187
1188 # preprocess each node
1189 17 ▶ '-pre' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
1190 17 my $node = shift;
1191 17 ▶ if ( my $parent = $node->get_parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $node->get_parent) { } 14 3 17
1192 14 ▶ my $parent_x = $parent->get_x || 0;
Condition100%2/2
$parent->get_x || 0
l!l
86
MC/DC100%1/1
$parent->get_x || 0
$parent->get_x
Truth table100%2/2
A
A= $parent->get_x
Aresult
11
00
1193 14 ▶ my $x = $phylo ? $node->get_branch_length || 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$phylo ? : 10 4 14
Condition50%1/2
$node->get_branch_length or 0
l!l
100
MC/DC0%0/1
$node->get_branch_length or 0
$node->get_branch_length
Truth table50%1/2
A
A= $node->get_branch_length
Aresult
11
00
1194 14 $node->set_x( $x + $parent_x );
1195 }
1196 else {
1197 3 $node->set_x(0); # root
1198 }
1199 },
1200
1201 # process this only on tips
1202 10 ▶ '-no_daughter' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1203 10 my $node = shift;
1204 10 $node->set_y( $tip_counter++ );
1205 10 my $x = $node->get_x;
1206 10 ▶ $tallest_tip = $x if $x > $tallest_tip;
Branch: 1 branch
BranchTrueFalseTotal
if $x > $tallest_tip 4 6 10
1207 },
1208
1209 # process this only on internal nodes
1210 7 ▶ '-post_daughter' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1211 7 my $node = shift;
1212 7 my ( $child_count, $child_y ) = ( 0, 0 );
1213 7 for my $child ( @{ $node->get_children } ) {
1214 14 $child_count++;
1215 14 $child_y += $child->get_y;
1216 }
1217 7 $node->set_y( $child_y / $child_count );
1218 },
1219 3 );
1220 $tree->visit(
1221 17 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
1222 17 my $node = shift;
1223 17 ▶ if ( not $tallest_tip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tallest_tip) 0 17 17
1224 0 throw 'BadArgs' => "This tree has no branch lengths, can't draw a phylogram";
1225 }
1226 17 $node->set_x( $padding + $node->get_x * ( $width / $tallest_tip ) );
1227 17 $node->set_y( $padding + $node->get_y * ( $height / $tip_counter ) );
1228 17 ▶ if ( !$phylo && $node->is_terminal ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $phylo and $node->is_terminal) 3 14 17
Condition100%3/3
not $phylo and $node->is_terminal
!ll&&!rl&&r
1223
MC/DC100%2/2
not $phylo and $node->is_terminal
not $phylo $node->is_terminal
Truth table100%3/3
not(A) and B
A= not $phyloB= $node->is_terminal
ABresult
0X0
100
111
1229 3 $node->set_x( $padding + $tallest_tip * ( $width / $tallest_tip ) );
1230 }
1231 }
1232 3 );
1233 3 $td->_set_scaley( $height / $tip_counter );
1234 3 $td->_set_scalex( $width / $tallest_tip );
1235 }
1236  
1237 =item render()
1238  
1239 Renders tree based on pre-computed node coordinates. You would typically use
1240 this method if you have passed a Bio::Phylo::Forest::DrawTree on which you
1241 have already calculated the node coordinates separately.
1242  
1243 Type : Unparsers
1244 Title : render
1245 Usage : my $drawing = $treedrawer->render;
1246 Function: Unparses a Bio::Phylo::Forest::DrawTree
1247 object into a drawing.
1248 Returns : SCALAR
1249 Args :
1250  
1251 =cut
1252  
1253 3 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod documented
1254 3 my $self = shift;
1255 3 my $library =
1256 looks_like_class __PACKAGE__ . '::' . ucfirst( lc( $self->get_format ) );
1257 3 my $drawer = $library->_new(
1258 '-tree' => $self->get_tree,
1259 '-drawer' => $self
1260 );
1261 3 return $drawer->_draw;
1262 }
1263  
1264 =begin comment
1265  
1266 Type : Internal method.
1267 Title : _reset_internal
1268 Usage : $treedrawer->_reset_internal;
1269 Function: resets the set_generic values stored by Treedrawer, this must be
1270 called at the start of each draw command or weird results are obtained!
1271 Returns : nothing
1272 Args : treedrawer, node being processed
1273  
1274 =end comment
1275  
1276 =cut
1277  
1278 17 ▶sub _reset_internal {
Subroutine _reset_internal: called CC=2 SCAR=6.9
1279 17 my ( $self, $node ) = @_;
1280 17 my $tree = $self->get_tree;
1281 17 $node->set_x(undef);
1282 17 $node->set_y(undef);
1283 17 my $children = $node->get_children;
1284 17 for my $child (@$children) {
1285 14 _reset_internal( $self, $child );
1286 }
1287 }
1288  
1289 =back
1290  
1291 =head1 SEE ALSO
1292  
1293 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
1294 for any user or developer questions and discussions.
1295  
1296 =over
1297  
1298 =item L<Bio::Phylo>
1299  
1300 The L<Bio::Phylo::Treedrawer> object inherits from the L<Bio::Phylo> object.
1301 Look there for more methods applicable to the treedrawer object.
1302  
1303 =item L<Bio::Phylo::Manual>
1304  
1305 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
1306  
1307 =back
1308  
1309 =head1 CITATION
1310  
1311 If you use Bio::Phylo in published research, please cite it:
1312  
1313 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
1314 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
1315 I<BMC Bioinformatics> B<12>:63.
1316 L<http://dx.doi.org/10.1186/1471-2105-12-63>
1317  
1318 =cut
1319  
1320 3 1;
« Phylo/Util/Math.pm ↑ Summary Phylo.pm »