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

filter: Statementstmt 0.0% 0 / 224 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 29 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 363 CC 45 SCAR 69.0
CC
45
Cov
0%
draw_plot
1482.0
draw_coverage
20.0
make_key_feature
12.0
CRAP 990.7
1 package Bio::Graphics::Glyph::wiggle_xyplot;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph::wiggle_data
5 Bio::Graphics::Glyph::xyplot
6 Bio::Graphics::Glyph::smoothing);
7 use IO::File;
8 use File::Spec;
9  
10 sub my_description {
11 return <<END;
12 This glyph draws quantitative data as an xyplot. It is designed to be
13 used in conjunction with features in "wiggle" format as generated by
14 Bio::Graphics::Wiggle, base pair coverage data generated by the
15 Bio::DB::Sam module, or interval statistical_summaries generated by
16 the Bio::DB::BigWig module.
17  
18 For this glyph to work, the feature must define one of the following tags
19 or methods:
20  
21 wigfile -- a path to a Bio::Graphics::Wiggle file
22  
23 wigdata -- Wiggle data in the Bio::Graphics::Wiggle "wif" format, as created
24 by \$wig->export_to_wif().
25  
26 coverage-- a simple comma-delimited string containing the quantitative values,
27 assumed to be one value per pixel.
28  
29 statistical_summary() -- a method for generating statistical information
30 including validCount, maxVal, minVal, sumData, and sumSquares.
31 END
32 }
33  
34 sub my_options {
35 {
36 basedir => [
37 'string',
38 undef,
39 'If a relative path is used for "wigfile", then this option provides',
40 'the base directory on which to resolve the path.'
41 ],
42 variance_band => [
43 'boolean',
44 0,
45 'If true, draw a semi-transparent band across the image that indicates',
46 'the mean and standard deviation of the data set. Only of use when a wig',
47 'file is provided.'
48 ],
49 z_score_bounds => [
50 'integer',
51 4,
52 'When using z_score autoscaling, this option controls how many standard deviations',
53 'above and below the mean to show.'
54 ],
55 autoscale => [
56 ['local','chromosome','global','z_score','clipped_global'],
57 'clipped_global',
58 'If set to "global" , then the minimum and maximum values of the XY plot',
59 'will be taken from the wiggle file as a whole. If set to "chromosome", then',
60 'scaling will be to minimum and maximum on the current chromosome.',
61 '"clipped_global" is similar to "global", but clips the top and bottom values',
62 'to the multiples of standard deviations indicated by "z_score_bounds"',
63 'If set to "z_score", then the whole plot will be rescaled to z-scores in which',
64 'the "0" value corresponds to the mean across the genome, and the units correspond',
65 'to standard deviations above and below the mean. The number of SDs to show are',
66 'controlled by the "z_score_bound" option.',
67 'Otherwise, the plot will be',
68 'scaled to the minimum and maximum values of the region currently on display.',
69 'min_score and max_score override autoscaling if one or both are defined'
70 ],
71 interval_method => [
72 ['mean', 'sum', 'min', 'max'],
73 'mean',
74 'When working with features that offer a statistical_summary() method,',
75 'such as those from Bio::DB::BigWig, define the method for reporting',
76 'scores within each interval.',
77 ],
78 };
79 }
80  
81 # Added pad_top subroutine (pad_top of Glyph.pm, which is called when executing $self->pad_top
82 # returns 0, so we need to override it here)
83 sub pad_top { shift->Bio::Graphics::Glyph::xyplot::pad_top(@_) }
84  
85 sub pad_left {
86 my $self = shift;
87 my $pad = $self->SUPER::pad_left(@_);
88 return $pad unless $self->option('variance_band');
89 $pad += length('+1sd')/2 * $self->font('gdTinyFont')->width+3;
90 return $pad;
91 }
92  
93 sub clip_color {
94 my $self = shift;
95 return $self->translate_color('orange');
96 }
97  
98 # we override the draw method so that it dynamically creates the parts needed
99 # from the wig file rather than trying to fetch them from the database
100  
101 # sub draw() { } is now mostly in wiggle_data.pm
102 sub draw {
103 my $self = shift;
104 my ($gd,$dx,$dy) = @_;
105 my $result = $self->Bio::Graphics::Glyph::wiggle_data::draw(@_);
106  
107 # inhibit the scale if we are non-bumping
108 $self->configure(-scale => 'none') if $self->bump eq 'overlap';
109 return $result;
110 }
111  
112 sub draw_coverage {
113 my $self = shift;
114 my $feature = shift;
115 my $array = shift;
116 if (! $array || ref($array) ne 'ARRAY'){
117 unshift(@_,$array);
118 my @arr = (eval{$feature->get_tag_values('coverage')});
119 $array = $arr[0];
120 } else {
121 $array = [split ',',$array] unless ref $array;
122 }
123 return unless @$array;
124  
125 my ($start,$end) = $self->effective_bounds($feature);
126 my $bases_per_bin = ($end-$start)/@$array;
127 my $pixels_per_base = $self->scale;
128 my @parts;
129 for (my $pixel=0;$pixel<$self->width;$pixel++) {
130 my $offset = $pixel/$pixels_per_base;
131 my $s = $start + $offset;
132 my $e = $s+1; # fill in gaps
133 my $v = $array->[$offset/$bases_per_bin];
134 push @parts,[$s,$s,$v];
135 }
136 $self->draw_plot(\@parts,@_);
137 }
138  
139  
140 sub draw_plot {
141 my $self = shift;
142 my $parts = shift;
143 my ($gd,$dx,$dy) = @_;
144 my $pivot = $self->bicolor_pivot;
145  
146 $self->panel->startGroup($gd);
147  
148 my ($left,$top,$right,$bottom) = $self->calculate_boundaries($dx,$dy);
149  
150 # There is a minmax inherited from xyplot as well as wiggle_data, and I don't want to
151 # rely on Perl's multiple inheritance DFS to find the right one.
152 my ($min_score,$max_score,$mean,$stdev) = $self->minmax($parts);
153 my $rescale = $self->option('autoscale') eq 'z_score';
154 my $side = $self->_determine_side();
155  
156 my ($scaled_min,$scaled_max);
157 if ($rescale) {
158 $scaled_min = int(($min_score-$mean)/$stdev + 0.5);
159 $scaled_max = int(($max_score-$mean)/$stdev + 0.5);
160 my $bound = $self->z_score_bound;
161 $scaled_max = $bound if $scaled_max >= 0;
162 $scaled_min = -$bound if $scaled_min <= 0;
163 }
164 elsif ($side) {
165 $scaled_min = int($min_score - 0.5);
166 $scaled_max = int($max_score + 0.5);
167 } else {
168 ($scaled_min,$scaled_max) = ($min_score,$max_score);
169 }
170  
171 my $height = $bottom - $top;
172 my $y_scale = $scaled_max > $scaled_min ? $height/($scaled_max-$scaled_min)
173 : 1;
174 my $x = $left;
175 my $y = $top;
176
177 my $x_scale = $self->scale;
178 my $panel_start = $self->panel->start;
179 my $feature = $self->feature;
180 my $f_start = $feature->start > $panel_start
181 ? $feature->start
182 : $panel_start;
183  
184 $y += $self->pad_top;
185  
186 # position of "0" on the scale
187 my $y_origin = $scaled_min <= 0 && $pivot ne 'min' ? $bottom - (0 - $scaled_min) * $y_scale : $bottom;
188 $y_origin = int($y_origin+0.5);
189  
190  
191 $self->panel->startGroup($gd);
192 $self->_draw_grid($gd,$x_scale,$scaled_min,$scaled_max,$dx,$dy,$y_origin) unless $self->option('no_grid');
193 $self->panel->endGroup($gd);
194  
195  
196 return unless $scaled_max > $scaled_min;
197  
198 my $lw = $self->linewidth;
199 my $positive = $self->pos_color;
200 my $negative = $self->neg_color;
201 my $midpoint = $self->midpoint;
202 my $clip_color = $self->clip_color;
203 my $flip = $self->{flip};
204 $midpoint = ($midpoint - $mean)/$stdev if $rescale;
205  
206 my ($clip_top,$clip_bottom);
207  
208 my @points = map {
209 my ($start,$end,$score) = @$_;
210 $score = ($score-$mean)/$stdev if $rescale;
211 my $x1 = $left + ($start - $f_start) * $x_scale;
212 my $x2 = $left + ($end - $f_start) * $x_scale;
213 if ($x2 >= $left and $x1 <= $right) {
214 my $y1 = $bottom - ($score - $scaled_min) * $y_scale;
215 my $y2 = $y_origin;
216 $y1 = $top if $y1 < $top;
217 $y1 = $bottom if $y1 > $bottom;
218  
219 $x1 = $left if $x1 < $left;
220 $x2 = $right if $x2 > $right;
221  
222 $x1 = $right - ($x1-$left) if $flip;
223 $x2 = $right - ($x2-$left) if $flip;
224
225 my $color = $score > $midpoint ? $positive : $negative;
226 [int($x1+0.5),int($y1+0.5),int($x2+0.5),int($y2+0.5),$color,$lw];
227 } else {
228 ();
229 }
230 } @$parts;
231  
232 my $svg_workaround = $gd->isa('GD::SVG::Image');
233  
234 $self->panel->startGroup($gd);
235 my $type = $self->graph_type;
236 if ($type eq 'boxes' or $type eq 'histogram') {
237 for (@points) {
238 my ($x1,$y1,$x2,$y2,$color,$lw) = @$_;
239 next unless abs($y2-$y1) > 0;
240 if ($svg_workaround && $x1==$x2) {
241 $gd->line($x1,$y1,$x2,$y2,$color);
242 } else {
243 $gd->filledRectangle($x1,$y1,$x2,$y2,$color);
244 }
245 # this tops off clipped peaks with a distinct color, but I just don't like how it looks
246 # $gd->line($x1+1,$top-2, $x1-1,$top, $clip_color) if $y1 == $top;
247 # $gd->line($x1+1,$bottom, $x1-1,$bottom+2, $clip_color) if $y1 == $bottom;
248 }
249 }
250  
251 if ($type eq 'line' or $type eq 'linepoints') {
252 my $current = shift @points;
253 my $lw = $self->option('linewidth');
254 $gd->setThickness($lw) if $lw > 1;
255 for (@points) {
256 my ($x1,$y1,$x2,$y2,$color,$lw) = @$_;
257 $gd->line(@{$current}[0,1],@{$_}[0,1],$color);
258 $current = $_;
259 }
260 $gd->setThickness(1);
261 }
262  
263 if ($type eq 'points' or $type eq 'linepoints') {
264 my $symbol_name = $self->option('point_symbol') || 'point';
265 my $filled = $symbol_name =~ s/^filled_//;
266 my $symbol_ref = $self->symbols->{$symbol_name};
267 my $pr = $self->point_radius;
268 for (@points) {
269 my ($x1,$y1,$x2,$y2,$color,$lw) = @$_;
270 $symbol_ref->($gd,$x1,$y1,$pr,$color,$filled);
271 }
272 }
273  
274 if ($self->option('variance_band') &&
275 (my ($mean,$variance) = $self->global_mean_and_variance())) {
276 if ($rescale) {
277 $mean = 0;
278 $variance = 1;
279 }
280 my $y1 = $bottom - ($mean+$variance - $scaled_min) * $y_scale;
281 my $y2 = $bottom - ($mean-$variance - $scaled_min) * $y_scale;
282 my $yy1 = $bottom - ($mean+$variance*2 - $scaled_min) * $y_scale;
283 my $yy2 = $bottom - ($mean-$variance*2 - $scaled_min) * $y_scale;
284 my ($clip_top,$clip_bottom);
285 if ($y1 < $top) {
286 $y1 = $top;
287 $clip_top++;
288 }
289 if ($yy1 < $top) {
290 $yy1 = $top;
291 $clip_top++;
292 }
293 if ($y2 > $bottom) {
294 $y2 = $bottom;
295 $clip_bottom++;
296 }
297 if ($yy2 > $bottom) {
298 $yy2 = $bottom;
299 $clip_bottom++;
300 }
301 my $y = $bottom - ($mean - $scaled_min) * $y_scale;
302 my $mean_color = $self->panel->translate_color('yellow:0.80');
303 my $onesd_color = $self->panel->translate_color('grey:0.30');
304 my $twosd_color = $self->panel->translate_color('grey:0.20');
305 $gd->filledRectangle($left,$y1,$right,$y2,$onesd_color);
306 $gd->filledRectangle($left,$yy1,$right,$yy2,$twosd_color);
307 $gd->line($left,$y,$right,$y,$mean_color);
308  
309 my $side = $self->_determine_side();
310 my $fcolor=$self->panel->translate_color('grey:0.50');
311 my $font = $self->font('gdTinyFont');
312 my $x1 = $left - $self->string_width('+2sd',$font) - ($side=~/left|three/ ? 15 : 0);
313 my $x2 = $left - $self->string_width('mn',$font) - ($side=~/left|three/ ? 15 : 0);
314 $gd->string($font,$x1,$yy1-$self->string_height('+2sd',$font),'+2sd',$fcolor) unless $clip_top;
315 $gd->string($font,$x1,$yy2-$self->string_height('-2sd')/2,'-2sd',$fcolor) unless $clip_bottom;
316 $gd->string($font,$x2,$y - $self->string_height('mn',$font),'mn', $fcolor);
317 }
318 $self->panel->endGroup($gd);
319  
320 $self->panel->startGroup($gd);
321 $self->_draw_scale($gd,$x_scale,$scaled_min,$scaled_max,$dx,$dy,$y_origin);
322 $self->panel->endGroup($gd);
323  
324 $self->draw_label(@_) if $self->option('label') || $self->record_label_positions;
325 $self->draw_description(@_) if $self->option('description');
326  
327 $self->panel->endGroup($gd);
328 }
329  
330 sub make_key_feature {
331 my $self = shift;
332 my $scale = 1/$self->scale; # base pairs/pixel
333  
334 # one segments, at pixels 0->80
335 my $offset = $self->panel->offset;
336  
337 my $start = 0 * $scale + $offset;
338 my $end = 80*$scale+$offset;
339  
340 my $span = int(0.5+($end-$start)/50);
341  
342 eval "require Bio::Graphics::Wiggle; 1";
343 my $wig = Bio::Graphics::Wiggle->new(undef,
344 1,
345 {seqid=>'chr1',
346 start => int($start),
347 span => $span});
348  
349 my @values = map {
350 (sin($_/60)+sin($_/12))*100+rand(100)
351 } 0..50;
352 my $min = $values[0];
353 my $max = $values[0];
354 my $tot = 0;
355 for (@values) {$min = $_ if $min > $_;
356 $max = $_ if $max < $_;
357 $tot += $_;
358 }
359 $wig->min($min);
360 $wig->max($max);
361 $wig->stdev(120); # just make it up
362 $wig->mean($tot/@values);
363  
364 $wig->set_value(($_*$span)+$start=>$values[$_-1]) for (0..50);
365  
366 my $feature =
367 Bio::Graphics::Feature->new(-start => $start,
368 -end => $end,
369 -name => $self->make_key_name(),
370 -strand => '+1',
371 -attributes => {wigfile=>$wig},
372 );
373 return $feature;
374 }
375  
376 1;
377  
378 __END__