Phylo/Treedrawer/Processing.pm

filter: Statementstmt 65.3% 100 / 153 Branchbran 33.3% 14 / 42 Conditioncond 10.0% 1 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 77.7% 14 / 18 Podpod n/a totaltotal 56.3% 129 / 229 CC 29 SCAR 24.3
CC
29
Cov
56%
_draw_arc
42.0
_draw_triangle
20.0
_draw_line
12.0
CRAP 11.4
« Phylo/Treedrawer/Png.pm ↑ Summary Phylo/PhyloWS/Service.pm »
1 package Bio::Phylo::Treedrawer::Processing;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'Bio::Phylo::Treedrawer::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Bio::Phylo::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Bio::Phylo::Util::Exceptions 'throw';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Bio::Phylo::Util::CONSTANT '_PI_';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Bio::Phylo::Treedrawer::Processing - Graphics format writer used by treedrawer,
12 no serviceable parts inside
13  
14 =head1 DESCRIPTION
15  
16 This module creates a Processing graphic from a Bio::Phylo::Forest::DrawTree
17 object. It is called by the L<Bio::Phylo::Treedrawer> object, so look there to
18 learn how to create tree drawings.
19  
20 =cut
21  
22 1 my $logger = Bio::Phylo::Util::Logger->new;
23 1 my $black = 0;
24 1 my $white = 255;
25 1 my %colors;
26 1 my $PI = _PI_;
27  
28 1 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
29 1 my $class = shift;
30 1 my %args = @_;
31 1 my $commands;
32 1 my $self = $class->SUPER::_new( %args, '-api' => \$commands );
33 1 return bless $self, $class;
34 }
35  
36 1 ▶sub _draw_pies {
Subroutine _draw_pies: called CC=1 SCAR=0.0
37 1 my $self = shift;
38 1 my $api = $self->_api;
39 $self->_tree->visit_level_order(
40 7 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
41 7 my $node = shift;
42 7 ▶ if ( not $node->get_collapsed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $node->get_collapsed) 7 0 7
43 7 my $cx = sprintf( "%.3f", $node->get_x );
44 7 my $cy = sprintf( "%.3f", $node->get_y );
45 7 my $r;
46 7 ▶ if ( $node->is_internal ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->is_internal) { } 3 4 7
47 3 $r =
48 sprintf( "%.3f", $self->_drawer->get_node_radius($node) );
49 }
50 else {
51 4 $r =
52 sprintf( "%.3f", $self->_drawer->get_tip_radius($node) );
53 }
54 7 ▶ if ( my $pievalues = $node->get_generic('pie') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pievalues = $node->get_generic("pie")) 7 0 7
55 7 my @keys = keys %{$pievalues};
56 7 my $start = 0;
57 7 my $total;
58 7 $total += $pievalues->{$_} for @keys;
59 7 for my $i ( 0 .. $#keys ) {
60 14 ▶ next if not $pievalues->{ $keys[$i] };
Branch: 1 branch
BranchTrueFalseTotal
unless $$pievalues{$keys[$i]} 0 14 14
61 my $slice =
62 14 $pievalues->{ $keys[$i] } / $total * 2 * $PI;
63 14 my $color = $colors{ $keys[$i] };
64 14 ▶ if ( not $color ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($color) 4 10 14
65 4 $colors{ $keys[$i] } = $color =
66 int( ( $i / $#keys ) * 256 );
67 }
68 14 my $stop = $start + $slice;
69 14 $$api .=
70 " drawArc($cx,$cy,$r,0,1,$color,$start,$stop);\n";
71 14 $start += $slice;
72 }
73 }
74 }
75 }
76 1 );
77 }
78  
79 1 ▶sub _draw_legend {
Subroutine _draw_legend: called CC=4 SCAR=13.9
80 1 my $self = shift;
81 1 ▶ if (%colors) {
Branch: 1 branch
BranchTrueFalseTotal
if (%colors) 1 0 1
82 1 my $api = $self->_api;
83 1 my $tree = $self->_tree;
84 1 my $draw = $self->_drawer;
85 1 my @keys = keys %colors;
86 1 my $increment =
87 ( $tree->get_tallest_tip->get_x - $tree->get_root->get_x ) /
88 scalar @keys;
89 1 my $x = sprintf( "%.3f", $tree->get_root->get_x + 5 );
90 1 foreach my $key (@keys) {
91 2 my $y = sprintf( "%.3f", $draw->get_height - 90 );
92 2 my $width = sprintf( "%.3f", $increment - 10 );
93 2 my $height = sprintf( "%.3f", 10.0 );
94 2 my $color = int $colors{$key};
95 2 $$api .= " drawRectangle($x,$y,$width,$height,$color);\n";
96 2 ▶ $self->_draw_text(
Condition50%1/2
$key || ' '
l!l
20
MC/DC0%0/1
$key || ' '
$key
Truth table50%1/2
A
A= $key
Aresult
11
00
97 '-x' => int($x),
98 '-y' => int( $draw->get_height - 60 ),
99 '-text' => $key || ' ',
100 );
101 2 $x += $increment;
102 }
103 $self->_draw_text(
104 1 '-x' => int(
105 $tree->get_tallest_tip->get_x + $draw->get_text_horiz_offset
106 ),
107 '-y' => int( $draw->get_height - 80 ),
108 '-text' => 'Node value legend',
109 );
110 }
111 }
112  
113 1 ▶sub _finish {
Subroutine _finish: called CC=1 SCAR=0.0
114 1 my $self = shift;
115 1 my $commands = $self->_api;
116 1 my $tmpl = do { local $/; <DATA> };
117 1 return sprintf( $tmpl,
118 __PACKAGE__, my $time = localtime(),
119 $self->_drawer->get_width, $self->_drawer->get_height,
120 $white, $$commands );
121 }
122  
123 7 ▶sub _draw_text {
Subroutine _draw_text: called CC=2 SCAR=7.0
124 7 my $self = shift;
125 7 my %args = @_;
126 7 my ( $x, $y, $text, $url, $stroke ) = @args{qw(-x -y -text -url -color)};
127 7 ▶ $stroke = $black if not defined $stroke;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stroke 7 0 7
128 7 my $api = $self->_api;
129 7 $$api .= " drawText(\"$text\",$x,$y,$stroke);\n";
130 }
131  
132 0 ▶sub _draw_line {
Subroutine _draw_line: not called CC=3 SCAR=24.8
133 0 my $self = shift;
134 0 my %args = @_;
135 0 my @keys = qw(-x1 -y1 -x2 -y2 -width -color);
136 0 my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys};
137 0 ▶ $color = $black if not defined $color;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color 0 0 0
138 0 ▶ $width = 1 if not defined $width;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width 0 0 0
139 0 my $api = $self->_api;
140 0 $$api .= sprintf(" drawLine(%u,%u,%u,%u,%u,%u);\n",$x1,$y1,$x2,$y2,$color,$width);
141 }
142  
143 0 ▶sub _draw_curve {
Subroutine _draw_curve: not called CC=3 SCAR=24.8
144 0 my $self = shift;
145 0 my $api = $self->_api;
146 0 my %args = @_;
147 0 my @keys = qw(-x1 -y1 -x2 -y2 -width -color);
148 0 my ( $x1, $y1, $x3, $y3, $width, $color ) = @args{@keys};
149 0 $x1 = sprintf( "%.3f", $x1 );
150 0 $x3 = sprintf( "%.3f", $x3 );
151 0 $y1 = sprintf( "%.3f", $y1 );
152 0 $y3 = sprintf( "%.3f", $y3 );
153 0 ▶ $color = $black if not defined $color;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color 0 0 0
154 0 ▶ $width = 1 if not defined $width;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width 0 0 0
155 0 $$api .= " drawCurve($x1,$y1,$x3,$y3,$color,$width);\n";
156 }
157  
158 0 ▶sub _draw_arc {
Subroutine _draw_arc: not called CC=6 SCAR=37.4
159 0 my $self = shift;
160 0 my $api = $self->_api;
161 0 my %args = @_;
162 0 my @keys = qw(-x1 -y1 -x2 -y2 -radius -width -color);
163 0 my ( $x1, $y1, $x2, $y2, $radius, $lineWidth, $lineColor ) = @args{@keys};
164 0 ▶ $lineColor = $black if not defined $lineColor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lineColor 0 0 0
165 0 ▶ $lineWidth = 1 if not defined $lineWidth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lineWidth 0 0 0
166 0 ▶ $radius = 0 if not defined $radius;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $radius 0 0 0
167 0 $radius *= 2;
168 0 my $fillColor = $white;
169
170 # get center of arc
171 0 my $drawer = $self->_drawer;
172 0 my $cx = $drawer->get_width / 2;
173 0 my $cy = $drawer->get_height / 2;
174  
175 # compute start and end
176 0 my ( $r1, $start ) = $drawer->cartesian_to_polar( $x1 - $cx, $y1 - $cy );
177 0 my ( $r2, $stop ) = $drawer->cartesian_to_polar( $x2 - $cx, $y2 - $cy );
178 0 ▶ $start += 360 if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 0 0 0
179 0 ▶ $stop += 360 if $stop < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $stop < 0 0 0 0
180 0 $start = ( $start / 360 ) * 2 * $PI;
181 0 $stop = ( $stop / 360 ) * 2 * $PI;
182 0 $start = sprintf( "%.3f", $start );
183 0 $stop = sprintf( "%.3f", $stop );
184
185 0 $$api .= " drawArc($cx,$cy,$radius,$lineColor,$lineWidth,$fillColor,$start,$stop);\n";
186 }
187  
188 6 ▶sub _draw_multi {
Subroutine _draw_multi: called CC=3 SCAR=11.1
189 6 my $self = shift;
190 6 my $api = $self->_api;
191 6 my %args = @_;
192 6 my @keys = qw(-x1 -y1 -x2 -y2 -width -color);
193 6 my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys};
194 6 ▶ $color = $black if not defined $color;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color 6 0 6
195 6 ▶ $width = 1 if not defined $width;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width 0 6 6
196 6 $$api .= sprintf( " drawMulti(%u,%u,%u,%u,%u,%u);\n",
197 $x1, $y1, $x2, $y2, $color, $width );
198 }
199  
200 0 ▶sub _draw_triangle {
Subroutine _draw_triangle: not called CC=4 SCAR=30.0
201 0 my $self = shift;
202 0 my $api = $self->_api;
203 0 my %args = @_;
204 0 my @coord = qw(-x1 -y1 -x2 -y2 -x3 -y3);
205 0 my ( $x1, $y1, $x2, $y2, $x3, $y3 ) = @args{@coord};
206 0 my @optional = qw(-fill -stroke -width -url -api);
207 0 ▶ my $fill = $args{'-fill'} || $white;
Condition0%0/3
$args{'-fill'} || $white
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'-fill'} || $white
$args{'-fill'} $white
Truth table0%0/3
A || B
A= $args{'-fill'}B= $white
ABresult
1X1
011
000
208 0 ▶ my $stroke = $args{'-stroke'} || $black;
Condition0%0/3
$args{'-stroke'} || $black
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'-stroke'} || $black
$args{'-stroke'} $black
Truth table0%0/3
A || B
A= $args{'-stroke'}B= $black
ABresult
1X1
011
000
209 0 ▶ my $width = $args{'-width'} || 1;
Condition0%0/2
$args{'-width'} || 1
l!l
00
MC/DC0%0/1
$args{'-width'} || 1
$args{'-width'}
Truth table0%0/2
A
A= $args{'-width'}
Aresult
11
00
210 0 my $url = $args{'-url'};
211 0 $$api .=
212 " drawTriangle($x1,$y1,$x2,$y2,$x3,$y3,$stroke,$width,$fill);\n";
213 }
214  
215 3 ▶sub _draw_circle {
Subroutine _draw_circle: called CC=4 SCAR=14.2
216 3 my $self = shift;
217 3 my $api = $self->_api;
218 3 my %args = @_;
219 my ( $x, $y, $radius, $width, $stroke, $fill, $url ) =
220 3 @args{qw(-x -y -radius -width -stroke -fill -url)};
221 3 ▶ $stroke = $black if not defined $stroke;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stroke 3 0 3
222 3 ▶ $width = 1 if not defined $width;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width 3 0 3
223 3 ▶ $fill = $white if not defined $fill;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fill 3 0 3
224 3 $$api .= sprintf( " drawCircle(%u,%u,%u,%u,%u,%u,\"%s\");\n",
225 $x, $y, $radius, $stroke, $width, $fill, $url );
226 }
227  
228 =head1 SEE ALSO
229  
230 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
231 for any user or developer questions and discussions.
232  
233 =over
234  
235 =item L<http://processing.org>
236  
237 This treedrawer produces a tree description in Processing language syntax. Visit
238 the website to learn more about how to deploy such graphics.
239  
240 =item L<Bio::Phylo::Treedrawer>
241  
242 The processing treedrawer is called by the L<Bio::Phylo::Treedrawer> object.
243 Look there to learn how to create tree drawings.
244  
245 =item L<Bio::Phylo::Manual>
246  
247 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
248  
249 =back
250  
251 =head1 CITATION
252  
253 If you use Bio::Phylo in published research, please cite it:
254  
255 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
256 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
257 I<BMC Bioinformatics> B<12>:63.
258 L<http://dx.doi.org/10.1186/1471-2105-12-63>
259  
260 =cut
261  
262 1 1;
263 __DATA__
« Phylo/Treedrawer/Png.pm ↑ Summary Phylo/PhyloWS/Service.pm »