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

filter: Statementstmt 0.0% 0 / 160 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 28 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 302 CC 40 SCAR 50.7
CC
40
Cov
0%
_draw_whiskers
380.0
draw
156.0
calcStdFromSums
12.0
CRAP 159.3
1 package Bio::Graphics::Glyph::wiggle_whiskers;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph::wiggle_data
5 Bio::Graphics::Glyph::wiggle_xyplot
6 );
7  
8  
9 sub my_description {
10 return <<END;
11 This glyph draws quantitative data as an xyplot in which the interval
12 0 to mean is represented in black, mean to +/- stdev in grey, and
13 stdev to max/min in light grey. This colors can be customized. The
14 glyph currently only works for features that have a
15 statistical_summary() method, of which the only currently example is
16 Bio::DB::BigWig. The statistical_summary() method returns a hash with
17 the following keys:
18  
19 validCount Number of intervals in the bin
20 maxVal Maximum value in the bin
21 minVal Minimum value in the bin
22 sumData Sum of the intervals in the bin
23 sumSquares Sum of the squares of the intervals in the bin
24 END
25 }
26  
27 sub my_options {
28 {
29 mean_color => [
30 'color',
31 'black',
32 'The color drawn from the zero value to the mean value.'
33 ],
34 mean_color_neg => [
35 'color',
36 'same as mean_color',
37 'The color drawn from the zero value to the mean value, for negative values.'
38 ],
39 stdev_color => [
40 'color',
41 'grey',
42 'The color drawn from the mean value to +stdev.'
43 ],
44 stdev_color_neg => [
45 'color',
46 'same as stdev_color',
47 'The color drawn from the mean value to -stdev.'
48 ],
49 max_color => [
50 'color',
51 'lightgrey',
52 'The color drawn from +stdev to max.'
53 ],
54 min_color => [
55 'color',
56 'same as max_color',
57 'The color drawn from -stdev to min.'
58 ],
59 graph_type => [
60 ['histogram','whiskers'],
61 'histogram',
62 'Type of graph to generate. Options are "histogram" (for a barchart),',
63 'or "whiskers" (for a whiskerplot showing mean, +/- stdev and max/min.',
64 'The deprecated "boxes" subtype is a synonym for "histogram."'
65 ],
66 }
67 }
68  
69 sub color_series {
70 my $self = shift;
71 return $self->{color_series} if exists $self->{color_series};
72 return $self->{color_series} = $self->option('color_series');
73 }
74  
75 sub overlaps {
76 my $self = shift;
77 return $self->{overlaps} if exists $self->{overlaps};
78 return $self->{overlaps} = $self->bump eq 'overlap';
79 }
80  
81 sub pad_top {
82 my $self = shift;
83 return $self->Bio::Graphics::Glyph::wiggle_xyplot::pad_top;
84 }
85  
86 sub extra_label_pad {
87 return 8
88 }
89  
90 sub graph_type {
91 shift->glyph_subtype;
92 }
93  
94 sub glyph_subtype {
95 my $self = shift;
96 return $self->option('glyph_subtype') || $self->option('graph_type') || 'histogram';
97 }
98  
99 sub mean_color {
100 my $self = shift;
101 return $self->bgcolor if $self->color_series;
102 return $self->color('mean_color') || $self->translate_color('black');
103 }
104  
105 sub mean_color_neg {
106 my $self = shift;
107 return $self->bgcolor if $self->color_series;
108 return $self->color('mean_color_neg') || $self->mean_color;
109 }
110  
111 sub stdev_color {
112 my $self = shift;
113 return $self->bgcolor if $self->color_series;
114 return $self->color('stdev_color') || $self->translate_color('grey');
115 }
116  
117 sub stdev_color_neg {
118 my $self = shift;
119 return $self->bgcolor if $self->color_series;
120 return $self->color('stdev_color_neg') || $self->stdev_color;
121 }
122 sub max_color {
123 my $self = shift;
124 return $self->bgcolor if $self->color_series;
125 return $self->color('max_color') || $self->translate_color('lightgrey');
126 }
127  
128 sub min_color {
129 my $self = shift;
130 return $self->bgcolor if $self->color_series;
131 return $self->color('min_color') || $self->max_color;
132 }
133  
134 sub draw {
135 my $self = shift;
136 my ($gd,$dx,$dy) = @_;
137 my ($left,$top,$right,$bottom) = $self->calculate_boundaries($dx,$dy);
138  
139 $self->panel->startGroup($gd);
140  
141 my $feature = $self->feature;
142 my $stats = eval {$feature->statistical_summary($self->width)};
143 if ($@ =~ /can\'t locate object method/i) {
144 warn "This glyph only works properly with features that have a statistical_summary() method, but you passed a ",ref($feature)," object";
145 return;
146 }
147 $stats ||= [];
148  
149 my ($min_score,$max_score,$mean,$stdev) = $self->minmax($stats);
150 my $rescale = $self->option('autoscale') eq 'z_score';
151  
152 my $side = $self->_determine_side();
153  
154 # if a scale is called for, then we adjust the max and min to be even
155 # multiples of a power of 10.
156 my ($scaled_min,$scaled_max);
157 if ($rescale) {
158 my $bound = $self->z_score_bound;
159 $scaled_min = int(($min_score-$mean)/$stdev + 0.5);
160 $scaled_max = int(($max_score-$mean)/$stdev + 0.5);
161 $scaled_max = $bound if $scaled_max > $bound;
162 $scaled_min = -$bound if $scaled_min < -$bound;
163 $self->{_stdev} = $stdev;
164 $self->{_mean} = $mean;
165 $self->{_zfold} = $bound;
166 }
167 elsif ($side) {
168 $scaled_min = Bio::Graphics::Glyph::xyplot::max10($min_score);
169 $scaled_max = Bio::Graphics::Glyph::xyplot::min10($max_score);
170 }
171
172 my $height = $bottom - $top;
173 my $scale = $scaled_max > $scaled_min ? $height/($scaled_max-$scaled_min)
174 : 1;
175 my $x = $left;
176 my $y = $top + $self->pad_top;
177  
178 # position of "0" on the scale
179 my $y_origin = $scaled_min <= 0 ? $bottom - (0 - $scaled_min) * $scale : $bottom;
180 $y_origin = $top if $scaled_max < 0;
181  
182 my $clip_ok = $self->option('clip');
183 $self->{_clip_ok} = $clip_ok;
184 $self->{_scale} = $scale;
185 $self->{_min_score} = $scaled_min;
186 $self->{_max_score} = $scaled_max;
187 $self->{_top} = $top;
188 $self->{_bottom} = $bottom;
189  
190 $self->panel->startGroup($gd);
191 $self->_draw_grid($gd,$scale,$scaled_min,$scaled_max,$dx,$dy,$y_origin);
192 $self->panel->endGroup($gd);
193  
194 $self->panel->startGroup($gd);
195 $self->_draw_whiskers($gd,$dx,$dy,$y_origin,$stats);
196 $self->panel->endGroup($gd);
197  
198 $self->panel->startGroup($gd);
199 $self->_draw_scale($gd,$scale,$scaled_min,$scaled_max,$dx,$dy,$y_origin);
200 $self->panel->endGroup($gd);
201  
202 $self->draw_label(@_) if $self->option('label') or $self->record_label_positions;
203 $self->draw_description(@_) if $self->option('description');
204  
205 $self->panel->endGroup($gd);
206  
207 # inhibit the scale if we are non-bumping
208 $self->configure(-scale => 'none') if $self->overlaps;
209 }
210  
211 sub _draw_whiskers {
212 my $self = shift;
213 my ($gd,$dx,$dy,$origin,$stats) = @_;
214 my $scale = $self->{_scale};
215  
216 my $mean_color = $self->mean_color;
217 my $mean_color_neg = $self->mean_color_neg;
218 my $stdev_color = $self->stdev_color;
219 my $stdev_color_neg = $self->stdev_color_neg;
220 my $max_color = $self->max_color;
221 my $min_color = $self->min_color;
222 my $clip_color = $self->clip_color;
223  
224 my $graph_type = $self->graph_type;
225  
226 my ($left,$top,$right,$bottom) = $self->calculate_boundaries($dx,$dy);
227 my $pos = $self->{flip} ? $right : $left;
228  
229 for my $bin (@$stats) {
230 next unless $bin->{validCount};
231 my $mean = $bin->{sumData}/$bin->{validCount};
232 my $stdev = $self->calcStdFromSums($bin->{sumData},
233 $bin->{sumSquares},
234 $bin->{validCount});
235 my $max = $bin->{maxVal};
236 my $min = $bin->{minVal};
237  
238 if (my $fold = $self->{_zfold}) {
239 $mean = ($mean - $self->{_mean}) / $self->{_stdev};
240 $max = ($max - $self->{_mean}) / $self->{_stdev};
241 $min = ($min - $self->{_mean}) / $self->{_stdev};
242 $stdev /= $self->{_stdev};
243 }
244  
245 my $mean_pos = $self->score2position($mean);
246 my $plus_one = $self->score2position($mean+$stdev);
247 my $minus_one = $self->score2position($mean-$stdev);
248 my $max_pos = $self->score2position($max);
249 my $min_pos = $self->score2position($min);
250  
251 my ($clip_top,$clip_bottom);
252 foreach (\$mean_pos,\$plus_one,\$minus_one,\$max_pos,\$min_pos) {
253 if (int($$_) < $top - 2) {
254 $$_ = $top;
255 $clip_top++;
256 } elsif (int($$_) > $bottom + 2) {
257 $$_ = $bottom;
258 $clip_bottom++;
259 }
260 }
261  
262 if ($graph_type =~ /histogram|boxes/) {
263 if ($mean >= 0) {
264 $gd->line($pos,$origin,$pos,$mean_pos, $mean_color);
265 $gd->line($pos,$mean_pos,$pos,$plus_one,$stdev_color) if $mean_pos != $plus_one;
266 $gd->line($pos,$plus_one,$pos,$max_pos, $max_color) if $plus_one != $max_pos;
267 } else {
268 $gd->line($pos,$origin,$pos,$mean_pos, $mean_color_neg);
269 $gd->line($pos,$mean_pos,$pos,$minus_one,$stdev_color_neg) if $mean_pos != $minus_one;
270 $gd->line($pos,$minus_one,$pos,$min_pos, $min_color) if $minus_one != $min_pos;
271 }
272 }
273 else {
274 $gd->setPixel($pos,$mean_pos,$mean_color);
275 $gd->line($pos,$mean_pos-1,$pos,$plus_one,$stdev_color) if $plus_one != $mean_pos;
276 $gd->line($pos,$plus_one-1,$pos,$max_pos,$max_color) if $max_pos != $mean_pos;
277 $gd->line($pos,$mean_pos+1,$pos,$minus_one,$stdev_color) if $minus_one != $mean_pos;
278 $gd->line($pos,$minus_one+1,$pos,$min_pos,$min_color) if $min_pos != $mean_pos;
279 }
280  
281 # this tops off clipped peaks with a distinct color, but I just don't like how it looks
282 $gd->line($pos,$top-2, $pos,$top, $clip_color) if $clip_top;
283 $gd->line($pos,$bottom,$pos,$bottom+2, $clip_color) if $clip_bottom;
284 } continue {
285 $self->{flip} ? $pos-- : $pos++;
286 }
287 }
288  
289 sub calcStdFromSums {
290 my $self = shift;
291 my ($sum,$sumSquares,$n) = @_;
292 my $var = $sumSquares - $sum*$sum/$n;
293 if ($n > 1) {
294 $var /= $n-1;
295 }
296 return 0 if $var < 0;
297 return sqrt($var);
298 }
299  
300 1;
301