blib/lib/Bio/Graphics/Glyph/dumbbell.pm untested

filter: Statementstmt 0.0% 0 / 173 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 229 CC 8 SCAR 21.5
CC
8
Cov
0%
draw_component
30.0
draw_end_shape
6.0
draw_end_bubble
6.0
CRAP 8.6
1 package Bio::Graphics::Glyph::dumbbell;
2 # DAS-compatible package to use for drawing a line of repeating shapes
3  
4 use strict;
5 use base qw(Bio::Graphics::Glyph::generic);
6  
7 use Math::Trig;
8  
9 sub my_description {
10 return <<END;
11 This glyph draws a "dumbbell" with the same shapes on both ends.
12 END
13 }
14 sub my_options {
15 {
16 shape_size => [
17 'integer',
18 10,
19 'The size of the shape on both ends.'],
20 end_shape => [
21 ['square','diamond','tree','clover',
22 'star','bubble','arrow','wave'],
23 'square',
24 'The type of shape to place at the ends of the dumbbell.'],
25 bubble_text => [
26 'string',
27 undef,
28 'The text to show in the bubble if the bubble option is',
29 'chosen (the -shape_size option is ignored in this case).'],
30 antiparallel => [
31 'boolean',
32 undef,
33 'Whether the right arrow is reversed.'],
34 arc => [
35 'boolean',
36 undef,
37 'If true, the end shapes will be connected by a curved',
38 'arc rather than a straight line.'],
39 }
40 }
41  
42 sub default_shape_size
43 {
44 return 10;
45 }
46  
47 sub default_shape
48 {
49 return 'square';
50 }
51  
52 sub draw_end_shape
53 {
54 my ($self, @args) = @_;
55 my $shape = $self->option('end_shape') || $self->default_shape();
56 my $method = "draw_end_$shape";
57 if ($self->can($method))
58 {
59 return $self->$method(@args);
60 }
61 else
62 {
63 return $self->draw_end_square(@args);
64 }
65 }
66  
67 sub draw_end_square
68 {
69 my ($self, $gd, $x1, $y1, $shape_size, $fg) = @_;
70  
71 my $x2 = $x1 + $shape_size;
72 my $y2 = $y1 + $shape_size;
73  
74 my $poly_pkg = $self->polygon_package;
75
76 my $polygon = $poly_pkg->new();
77 $polygon->addPt($x1,$y1);
78 $polygon->addPt($x2,$y1);
79 $polygon->addPt($x2,$y2);
80 $polygon->addPt($x1, $y2);
81  
82 $gd->filledPolygon($polygon,$fg);
83
84 return ($x1, $x2);
85 }
86  
87 sub draw_end_diamond
88 {
89 my ($self, $gd, $x1, $y1, $shape_size, $fg) = @_;
90  
91 my $x2 = $x1 + $shape_size;
92 my $y2 = $y1 + $shape_size;
93  
94 my $poly_pkg = $self->polygon_package;
95  
96 my $midX = ($x1+$x2)/2;
97 my $midY = ($y1+$y2)/2;
98
99 my $polygon = $poly_pkg->new();
100 $polygon->addPt($x1,$midY);
101 $polygon->addPt($midX,$y1);
102 $polygon->addPt($x2,$midY);
103 $polygon->addPt($midX,$y2);
104  
105 $gd->filledPolygon($polygon,$fg);
106
107 return ($x1, $x2);
108 }
109  
110 sub translated_polygon
111 {
112 my ($self, $midX, $midY, $scale_factor, @coords) = @_;
113  
114 my $poly_pkg = $self->polygon_package;
115  
116 my $polygon = $poly_pkg->new();
117 for (my $i=0; $i<(scalar @coords) / 2; $i++)
118 {
119 $polygon->addPt($coords[2*$i], $coords[2*$i+1]);
120 }
121
122 $polygon->scale($scale_factor, $scale_factor);
123 $polygon->offset($midX, $midY);
124
125 return $polygon;
126 }
127  
128 sub draw_end_star
129 {
130 my ($self, $gd, $x1, $y1, $shape_size, $fg) = @_;
131
132 #my @coords = (95, -31, -58, 81, 0, -100, 58, 81, -95, -31);
133 my @coords1 = (31, 42, 31, -42, -49, -30, -38, 0, -49, 30);
134 my @coords2 = (100, 0, -81, 59, 31, -95, 31, 95, -81, -58);
135  
136 my $star_size = 190;
137
138 my $scale_factor = $shape_size / $star_size;
139
140 my ($midX, $midY) = ($x1+$shape_size/2, $y1+$shape_size/2);
141
142 $gd->filledPolygon($self->translated_polygon($midX, $midY, $scale_factor, @coords1), $fg);
143 $gd->filledPolygon($self->translated_polygon($midX, $midY, $scale_factor, @coords2), $fg);
144  
145 return ($midX, $midX);
146 }
147
148 sub draw_end_tree
149 {
150 my ($self, $gd, $x1, $y1, $shape_size, $fg) = @_;
151  
152 my $x2 = $x1 + $shape_size;
153 my $y2 = $y1 + $shape_size;
154
155 my $trunk_width = $shape_size/4;
156  
157 my $midX = ($x1+$x2)/2;
158 my $midY = ($y1+$y2)/2;
159  
160 my $poly_pkg = $self->polygon_package;
161
162 my $polygon = $poly_pkg->new();
163 $polygon->addPt($midX-$trunk_width/2,$midY);
164 $polygon->addPt($midX+$trunk_width/2,$midY);
165 $polygon->addPt($midX+$trunk_width/2,$y2);
166 $polygon->addPt($midX-$trunk_width/2,$y2);
167  
168 $gd->filledPolygon($polygon, $fg);
169
170 $self->filled_oval($gd, $x1, $y1, $x2, $y1+2*$shape_size/3, $fg, $fg);
171
172 return ($midX, $midX);
173 }
174  
175 sub draw_end_clover
176 {
177 my ($self, $gd, $x1, $y1, $shape_size, $fg) = @_;
178  
179 my $x2 = $x1 + $shape_size;
180 my $y2 = $y1 + $shape_size;
181
182 my $trunk_width = $shape_size/4;
183  
184 my $midX = ($x1+$x2)/2;
185  
186 my $poly_pkg = $self->polygon_package;
187
188 my $polygon = $poly_pkg->new();
189 $polygon->addPt($midX-$trunk_width/2,$y1+0.4*$shape_size);
190 $polygon->addPt($midX+$trunk_width/2,$y1+0.4*$shape_size);
191 $polygon->addPt($midX+$trunk_width/2,$y2);
192 $polygon->addPt($midX-$trunk_width/2,$y2);
193  
194 $gd->filledPolygon($polygon, $fg);
195
196 my $radius = $shape_size / 4.3;
197
198 $self->filled_oval($gd, $midX-$radius, $y1, $midX+$radius, $y1+2*$radius, $fg, $fg);
199 $self->filled_oval($gd, $x1, $y1+1.3*$radius, $x1+2*$radius, $y1+3.3*$radius, $fg, $fg);
200 $self->filled_oval($gd, $x2-2*$radius, $y1+1.3*$radius, $x2, $y1+3.3*$radius, $fg, $fg);
201
202 return ($midX, $midX);
203 }
204  
205 sub draw_end_bubble
206 {
207 my ($self, $gd, $x1, $y1, $shape_size, $fg) = @_;
208
209 my $x2 = $x1 + $shape_size;
210 my $y2 = $y1 + $shape_size;
211  
212 my $midX = ($x1+$x2)/2;
213 my $midY = ($y1+$y2)/2;
214
215 my $bubble_text = defined $self->option('bubble_text') ? $self->option('bubble_text') : "Text";
216  
217 my $font = $self->option('labelfont') || $self->font;
218 my $bubble_text_length = $self->string_width($bubble_text,$font);
219 my $bubble_text_x = $midX - $bubble_text_length/2;
220 my $bubble_text_y = $midY - $self->font_height($font)/2;
221
222 $gd->string($font, $bubble_text_x, $bubble_text_y, $bubble_text, $self->fontcolor);
223
224 my $oval_width = $bubble_text_length * 1.414;
225 my $oval_height = $self->font_height($font) * 1.414;
226  
227 $self->oval($gd, $midX-$oval_width/2, $midY-$oval_height/2, $midX+$oval_width/2, $midY+$oval_height/2);
228  
229 return ($midX-$oval_width/2, $midX+$oval_width/2);
230 }
231  
232 sub draw_end_arrow
233 {
234 my ($self, $gd, $x1, $y1, $shape_size, $fg, $antiparallel) = @_;
235
236 my $x2 = $x1 + $shape_size;
237 my $y2 = $y1 + $shape_size;
238
239 my $angle = deg2rad(30);
240 my $dx = 2*$shape_size*cos($angle)/5;
241 my $dy = 2*$shape_size*sin($angle)/5;
242 my $midX = $x2-$dx;
243 my $midY = ($y1+$y2)/2;
244  
245 $gd->line($x1, $midY, $x2, $midY, $fg);
246 if ($antiparallel)
247 {
248 $gd->line($x1, $midY, $x1+$dx, $midY-$dy, $fg);
249 $gd->line($x1, $midY, $x1+$dx, $midY+$dy, $fg);
250 }
251 else
252 {
253 $gd->line($x2, $midY, $x2-$dx, $midY-$dy, $fg);
254 $gd->line($x2, $midY, $x2-$dx, $midY+$dy, $fg);
255 }
256 return ($x1, $x2);
257 }
258  
259 sub draw_end_wave
260 {
261 my ($self, $gd, $x1, $y1, $shape_size, $fg) = @_;
262
263 my $x2 = $x1 + $shape_size;
264
265 #Make the heigh constant
266 my $y2 = $y1 + $shape_size/2;
267 $y1 = $y2-10;
268
269 my $step = $shape_size/6;
270 $gd->line($x1, $y2, $x1+$step, $y1, $fg);
271 $gd->line($x1+$step, $y1, $x1+2*$step, $y2, $fg);
272 $gd->line($x1+2*$step, $y2, $x1+3*$step, $y1, $fg);
273 $gd->line($x1+3*$step, $y1, $x1+4*$step, $y2, $fg);
274 $gd->line($x1+4*$step, $y2, $x1+5*$step, $y1, $fg);
275 $gd->line($x1+5*$step, $y1, $x1+6*$step, $y2, $fg);
276 return ($x1, $x2);
277 }
278  
279 sub draw_component {
280 my $self = shift;
281 my $gd = shift;
282 my $fg = $self->fgcolor;
283
284 my $shape_size = defined $self->option('shape_size') ? $self->option('shape_size') : $self->default_shape_size;
285  
286 # find the center and vertices
287 my ($x1,$y1,$x2,$y2) = $self->calculate_boundaries(@_);
288
289 if ($x2-$x1 < $shape_size)
290 {
291 return $self->SUPER::draw_component($gd, @_);
292 }
293
294 my $midX = ($x2-$x1) / 2 + $x1;
295 my $midY = ($y2-$y1) / 2 + $y1;
296 my $startY = $midY - $shape_size/2;
297
298 my $antiparallel = $self->option('antiparallel');
299
300 #We need to store the bounds of the shapes drawn because the connecting line will have
301 #different length depending on them.
302 my ($leftX1, $leftX2) = $self->draw_end_shape($gd, $x1, $startY, $shape_size, $fg);
303 my ($rightX1, $rightX2) = $self->draw_end_shape($gd, $x2-$shape_size, $startY, $shape_size, $fg, $antiparallel);
304  
305 if ($self->option('arc') == 1)
306 {
307 #Draw an arc of an ellipse relative to the midpoint between shapes
308 #whose center is at (0, -q) and which intersects with the X axis at (p,0) and (-p, 0).
309 my $p = ($rightX1 - $leftX2) / 2;
310 my $q = $shape_size/2;
311
312 my $c = 2 * $p / sqrt(3);
313 my $d = 2 * $q;
314 my $b = $q - $d;
315 my $angle = atan2(sqrt(3), 1);
316 my $deg = rad2deg($angle);
317 $gd->arc($leftX2+$p,$midY+$q,2*$c,2*$d,270-$deg,270+$deg,$self->factory->translate_color('black'));
318 }
319 else
320 {
321 $gd->line($leftX2,$midY,$rightX1,$midY,$fg);
322 }
323
324 if (my $caption = $self->option('caption'))
325 {
326 my $font = $self->option('labelfont') || $self->font;
327 my $midX = ($x2-$x1-2*$shape_size)/2+$x1+$shape_size;
328 my $startCaption = $midX - $self->string_width($caption,$font)/2;
329 $gd->string($font, $startCaption, $midY-$self->font_height($font), $caption, $self->fontcolor);
330 }
331 }
332  
333  
334 1;
335  
336 __END__