| 1 | package Bio::Graphics::Glyph::wiggle_density; | ||
| 2 | use strict; | ||
| 3 | use base qw(Bio::Graphics::Glyph::wiggle_data | ||
| 4 | Bio::Graphics::Glyph::box | ||
| 5 | Bio::Graphics::Glyph::smoothing | ||
| 6 | Bio::Graphics::Glyph::xyplot | ||
| 7 | ); | ||
| 8 | |||
| 9 | sub my_description { | ||
| 10 | return <<END; | ||
| 11 | This glyph draws quantitative data as a heatmap. Higher-intensity parts of the feature | ||
| 12 | will be drawn with more saturation. | ||
| 13 | |||
| 14 | For this glyph to work, the feature must define one of the following tags: | ||
| 15 | |||
| 16 | wigfile -- a path to a Bio::Graphics::Wiggle file | ||
| 17 | |||
| 18 | wigdata -- Wiggle data in the Bio::Graphics::Wiggle "wif" format, as created | ||
| 19 | by \$wig->export_to_wif(). | ||
| 20 | |||
| 21 | coverage-- a simple comma-delimited string containing the quantitative values, | ||
| 22 | assumed to be one value per pixel. | ||
| 23 | |||
| 24 | END | ||
| 25 | } | ||
| 26 | |||
| 27 | sub my_options { | ||
| 28 | { | ||
| 29 | basedir => [ | ||
| 30 | 'string', | ||
| 31 | undef, | ||
| 32 | 'If a relative path is used for "wigfile", then this option provides', | ||
| 33 | 'the base directory on which to resolve the path.' | ||
| 34 | ], | ||
| 35 | z_score_bounds => [ | ||
| 36 | 'integer', | ||
| 37 | 4, | ||
| 38 | 'When using z_score autoscaling, this option controls how many standard deviations', | ||
| 39 | 'above and below the mean to show.' | ||
| 40 | ], | ||
| 41 | autoscale => [ | ||
| 42 | ['local','chromosome','global','z_score','clipped_global'], | ||
| 43 | 'clipped_global', | ||
| 44 | 'If set to "global" , then the minimum and maximum values of the XY plot', | ||
| 45 | 'will be taken from the wiggle file as a whole. If set to "chromosome", then', | ||
| 46 | 'scaling will be to minimum and maximum on the current chromosome.', | ||
| 47 | '"clipped_global" is similar to "global", but clips the top and bottom values', | ||
| 48 | 'to the multiples of standard deviations indicated by "z_score_bounds"', | ||
| 49 | 'If set to "z_score", then the whole plot will be rescaled to z-scores in which', | ||
| 50 | 'the "0" value corresponds to the mean across the genome, and the units correspond', | ||
| 51 | 'to standard deviations above and below the mean. The number of SDs to show are', | ||
| 52 | 'controlled by the "z_score_bound" option.', | ||
| 53 | 'Otherwise, the plot will be', | ||
| 54 | 'scaled to the minimum and maximum values of the region currently on display.', | ||
| 55 | 'min_score and max_score override autoscaling if one or both are defined' | ||
| 56 | ], | ||
| 57 | graph_type => [ | ||
| 58 | undef, | ||
| 59 | undef, | ||
| 60 | 'Unused option', | ||
| 61 | ], | ||
| 62 | }; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub pad_top { | ||
| 66 | my $self = shift; | ||
| 67 | my $overlap = $self->bump eq 'overlap'; | ||
| 68 | return $overlap ?$self->Bio::Graphics::Glyph::xyplot::pad_top(@_) | ||
| 69 | :$self->SUPER::pad_top(@_); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub draw { | ||
| 73 | my $self = shift; | ||
| 74 | my ($gd,$dx,$dy) = @_; | ||
| 75 | |||
| 76 | my $retval = $self->SUPER::draw(@_); | ||
| 77 | if ($retval) { | ||
| 78 | $self->draw_label(@_) if $self->option('label'); | ||
| 79 | $self->draw_description(@_) if $self->option('description'); | ||
| 80 | $self->panel->endGroup($gd); | ||
| 81 | return $retval; | ||
| 82 | } else { | ||
| 83 | # This draws a filled box for null value data | ||
| 84 | #return $self->Bio::Graphics::Glyph::box::draw(@_); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | sub draw_coverage { | ||
| 89 | my $self = shift; | ||
| 90 | my $feature = shift; | ||
| 91 | my $array = shift; | ||
| 92 | |||
| 93 | |||
| 94 | if (! $array || ref($array) ne 'ARRAY'){ | ||
| 95 | unshift(@_,$array); | ||
| 96 | my @arr = (eval{$feature->get_tag_values('coverage')}); | ||
| 97 | $array = $arr[0]; | ||
| 98 | } else { | ||
| 99 | $array = [split ',',$array] unless ref $array; | ||
| 100 | } | ||
| 101 | return unless @$array; | ||
| 102 | |||
| 103 | my ($gd,$left,$top) = @_; | ||
| 104 | |||
| 105 | my ($start,$end) = $self->effective_bounds($feature); | ||
| 106 | my $length = $end - $start + 1; | ||
| 107 | my $bases_per_bin = ($end-$start)/@$array; | ||
| 108 | my @parts; | ||
| 109 | my $samples = $length < $self->panel->width ? $length | ||
| 110 | : $self->panel->width; | ||
| 111 | my $samples_per_base = $samples/$length; | ||
| 112 | |||
| 113 | for (my $i=0;$i<$samples;$i++) { | ||
| 114 | my $offset = $i/$samples_per_base; | ||
| 115 | my $v = $array->[$offset/$bases_per_bin]; | ||
| 116 | push @parts,$v; | ||
| 117 | } | ||
| 118 | |||
| 119 | my ($x1,$y1,$x2,$y2) = $self->bounds($left,$top); | ||
| 120 | $self->draw_segment($gd, | ||
| 121 | $start,$end, | ||
| 122 | \@parts, | ||
| 123 | $start,$end, | ||
| 124 | 1,1, | ||
| 125 | $x1,$y1,$x2,$y2); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub draw_segment { | ||
| 129 | my $self = shift; | ||
| 130 | my ($gd, | ||
| 131 | $start,$end, | ||
| 132 | $seg_data, | ||
| 133 | $seg_start,$seg_end, | ||
| 134 | $step,$span, | ||
| 135 | $x1,$y1,$x2,$y2) = @_; | ||
| 136 | |||
| 137 | # clip, because wig files do no clipping | ||
| 138 | $seg_start = $start if $seg_start < $start; | ||
| 139 | $seg_end = $end if $seg_end > $end; | ||
| 140 | |||
| 141 | # figure out where we're going to start | ||
| 142 | my $scale = $self->scale; # pixels per base pair | ||
| 143 | my $pixels_per_span = $scale * $span + 1; | ||
| 144 | my $pixels_per_step = 1; | ||
| 145 | my $length = $end-$start+1; | ||
| 146 | |||
| 147 | # if the feature starts before the data starts, then we need to draw | ||
| 148 | # a line indicating missing data (this only happens if something went | ||
| 149 | # wrong upstream) | ||
| 150 | if ($seg_start > $start) { | ||
| 151 | my $terminus = $self->map_pt($seg_start); | ||
| 152 | $start = $seg_start; | ||
| 153 | $x1 = $terminus; | ||
| 154 | } | ||
| 155 | # if the data ends before the feature ends, then we need to draw | ||
| 156 | # a line indicating missing data (this only happens if something went | ||
| 157 | # wrong upstream) | ||
| 158 | if ($seg_end < $end) { | ||
| 159 | my $terminus = $self->map_pt($seg_end); | ||
| 160 | $end = $seg_end; | ||
| 161 | $x2 = $terminus; | ||
| 162 | } | ||
| 163 | |||
| 164 | return unless $start < $end; | ||
| 165 | |||
| 166 | # get data values across the area | ||
| 167 | my $samples = $length < $self->panel->width ? $length | ||
| 168 | : $self->panel->width; | ||
| 169 | my $data = ref $seg_data eq 'ARRAY' ? $seg_data | ||
| 170 | : $seg_data->values($start,$end,$samples); | ||
| 171 | |||
| 172 | |||
| 173 | # scale the glyph if the data end before the panel does | ||
| 174 | my $data_width = $end - $start; | ||
| 175 | my $data_width_ratio; | ||
| 176 | if ($data_width < $self->panel->length) { | ||
| 177 | $data_width_ratio = $data_width/$self->panel->length; | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | $data_width_ratio = 1; | ||
| 181 | } | ||
| 182 | return unless $data && ref $data && @$data > 0; | ||
| 183 | |||
| 184 | my $min_value = $self->min_score; | ||
| 185 | my $max_value = $self->max_score; | ||
| 186 | |||
| 187 | my ($min,$max,$mean,$stdev) = $self->minmax($data); | ||
| 188 | unless (defined $min_value && defined $max_value) { | ||
| 189 | $min_value ||= $min; | ||
| 190 | $max_value ||= $max; | ||
| 191 | } | ||
| 192 | |||
| 193 | my $rescale = $self->option('autoscale') eq 'z_score'; | ||
| 194 | my ($scaled_min,$scaled_max); | ||
| 195 | if ($rescale) { | ||
| 196 | my $bound = $self->z_score_bound; | ||
| 197 | $scaled_min = -$bound; | ||
| 198 | $scaled_max = +$bound; | ||
| 199 | } else { | ||
| 200 | ($scaled_min,$scaled_max) = ($min_value,$max_value); | ||
| 201 | } | ||
| 202 | |||
| 203 | my $t = 0; for (@$data) {$t+=$_} | ||
| 204 | |||
| 205 | # allocate colors | ||
| 206 | # There are two ways to do this. One is a scale from min to max. The other is a | ||
| 207 | # bipartite scale using one color range from zero to min, and another color range | ||
| 208 | # from 0 to max. The latter behavior is triggered when the config file contains | ||
| 209 | # entries for "pos_color" and "neg_color" and the data ranges from < 0 to > 0. | ||
| 210 | |||
| 211 | my $poscolor = $self->pos_color || $self->fgcolor; | ||
| 212 | my $negcolor = $self->neg_color || $self->bgcolor; | ||
| 213 | |||
| 214 | my $data_midpoint = $self->midpoint; | ||
| 215 | $data_midpoint = 0 if $rescale; | ||
| 216 | my $bicolor = $poscolor != $negcolor | ||
| 217 | && $scaled_min < $data_midpoint | ||
| 218 | && $scaled_max > $data_midpoint; | ||
| 219 | |||
| 220 | my ($rgb_pos,$rgb_neg,$rgb); | ||
| 221 | if ($bicolor) { | ||
| 222 | $rgb_pos = [$self->panel->rgb($poscolor)]; | ||
| 223 | $rgb_neg = [$self->panel->rgb($negcolor)]; | ||
| 224 | } else { | ||
| 225 | $rgb = $scaled_max > $scaled_min ? ([$self->panel->rgb($poscolor)] || [$self->panel->rgb($self->bgcolor)]) | ||
| 226 | : ([$self->panel->rgb($negcolor)] || [$self->panel->rgb($self->bgcolor)]); | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | my %color_cache; | ||
| 231 | |||
| 232 | @$data = reverse @$data if $self->flip; | ||
| 233 | |||
| 234 | if (@$data <= $self->panel->width) { # data fits in width, so just draw it | ||
| 235 | |||
| 236 | $pixels_per_step = $scale * $step; | ||
| 237 | $pixels_per_step = 1 if $pixels_per_step < 1; | ||
| 238 | my $datapoints_per_base = @$data/$length; | ||
| 239 | my $pixels_per_datapoint = $self->panel->width/@$data * $data_width_ratio; | ||
| 240 | |||
| 241 | my %temps; | ||
| 242 | map{$temps{$_}++} (@$data); | ||
| 243 | my %colorss = (); | ||
| 244 | for (my $i = 0; $i <= @$data ; $i++) { | ||
| 245 | my $x = $x1 + $pixels_per_datapoint * $i; | ||
| 246 | my $data_point = $data->[$i]; | ||
| 247 | defined $data_point || next; | ||
| 248 | $data_point = ($data_point-$mean)/$stdev if $rescale; | ||
| 249 | $data_point = $scaled_min if $scaled_min > $data_point; | ||
| 250 | $data_point = $scaled_max if $scaled_max < $data_point; | ||
| 251 | my ($r,$g,$b) = $bicolor | ||
| 252 | ? $data_point > $data_midpoint ? $self->calculate_color($data_point,$rgb_pos, | ||
| 253 | $data_midpoint,$scaled_max) | ||
| 254 | : $self->calculate_color($data_point,$rgb_neg, | ||
| 255 | $data_midpoint,$scaled_min) | ||
| 256 | : $self->calculate_color($data_point,$rgb, | ||
| 257 | $scaled_min,$scaled_max); | ||
| 258 | |||
| 259 | my $idx = $color_cache{$r,$g,$b} ||= $self->panel->translate_color($r,$g,$b); | ||
| 260 | $colorss{$idx} = $data_point; | ||
| 261 | $self->filled_box($gd,$x,$y1,$x+$pixels_per_datapoint,$y2,$idx,$idx); | ||
| 262 | } | ||
| 263 | (keys %colorss); # Alleviate a silent crash somewhere in GD that causes density graph get drawn as a solid-colored box | ||
| 264 | } else { # use Sheldon's code to subsample data | ||
| 265 | $pixels_per_step = $scale * $step; | ||
| 266 | my $pixels = 0; | ||
| 267 | |||
| 268 | # only draw boxes 2 pixels wide, so take the mean value | ||
| 269 | # for n data points that span a 2 pixel interval | ||
| 270 | my $binsize = 2/$pixels_per_step; | ||
| 271 | my $pixelstep = $pixels_per_step; | ||
| 272 | $pixels_per_step *= $binsize; | ||
| 273 | $pixels_per_step *= $data_width_ratio; | ||
| 274 | $pixels_per_span = 2; | ||
| 275 | |||
| 276 | my $scores = 0; | ||
| 277 | my $defined; | ||
| 278 | |||
| 279 | for (my $i = $start; $i < $end ; $i += $step) { | ||
| 280 | # draw the box if we have accumulated >= 2 pixel's worth of data. | ||
| 281 | if ($pixels >= 2) { | ||
| 282 | my $data_point = $defined ? $scores/$defined : 0; | ||
| 283 | $scores = 0; | ||
| 284 | $defined = 0; | ||
| 285 | |||
| 286 | $data_point = $scaled_min if $scaled_min > $data_point; | ||
| 287 | $data_point = $scaled_max if $scaled_max < $data_point; | ||
| 288 | my ($r,$g,$b) = $bicolor | ||
| 289 | ? $data_point > $data_midpoint ? $self->calculate_color($data_point,$rgb_pos, | ||
| 290 | $data_midpoint,$scaled_max) | ||
| 291 | : $self->calculate_color($data_point,$rgb_neg, | ||
| 292 | $data_midpoint,$scaled_min) | ||
| 293 | : $self->calculate_color($data_point,$rgb, | ||
| 294 | $scaled_min,$max_value); | ||
| 295 | my $idx = $color_cache{$r,$g,$b} ||= $self->panel->translate_color($r,$g,$b); | ||
| 296 | $self->filled_box($gd,$x1,$y1,$x1+$pixels_per_span,$y2,$idx,$idx); | ||
| 297 | $x1 += $pixels; | ||
| 298 | $pixels = 0; | ||
| 299 | } | ||
| 300 | |||
| 301 | my $val = shift @$data; | ||
| 302 | # don't include undef scores in the mean calculation | ||
| 303 | # $scores is the numerator; $defined is the denominator | ||
| 304 | $scores += $val if defined $val; | ||
| 305 | $defined++ if defined $val; | ||
| 306 | |||
| 307 | # keep incrementing until we exceed 2 pixels | ||
| 308 | # the step is a fraction of a pixel, not an integer | ||
| 309 | $pixels += $pixelstep; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | sub draw_plot { | ||
| 315 | my $self = shift; | ||
| 316 | my $parts = shift; | ||
| 317 | my ($gd,$dx,$dy) = @_; | ||
| 318 | |||
| 319 | my $x_scale = $self->scale; | ||
| 320 | my $panel_start = $self->panel->start; | ||
| 321 | my $feature = $self->feature; | ||
| 322 | my $f_start = $feature->start > $panel_start | ||
| 323 | ? $feature->start | ||
| 324 | : $panel_start; | ||
| 325 | |||
| 326 | my ($left,$top,$right,$bottom) = $self->calculate_boundaries($dx,$dy); | ||
| 327 | |||
| 328 | # There is a minmax inherited from xyplot as well as wiggle_data, and I don't want to | ||
| 329 | # rely on Perl's multiple inheritance DFS to find the right one. | ||
| 330 | my ($min_score,$max_score,$mean,$stdev) = $self->minmax($parts); | ||
| 331 | my $rescale = $self->option('autoscale') eq 'z_score'; | ||
| 332 | |||
| 333 | my ($scaled_min,$scaled_max); | ||
| 334 | if ($rescale) { | ||
| 335 | $scaled_min = int(($min_score-$mean)/$stdev + 0.5); | ||
| 336 | $scaled_max = int(($max_score-$mean)/$stdev + 0.5); | ||
| 337 | my $bound = $self->z_score_bound; | ||
| 338 | $scaled_max = $bound if $scaled_max > $bound; | ||
| 339 | $scaled_min = -$bound if $scaled_min < -$bound; | ||
| 340 | } else { | ||
| 341 | ($scaled_min,$scaled_max) = ($min_score,$max_score); | ||
| 342 | } | ||
| 343 | |||
| 344 | my $pivot = $self->bicolor_pivot; | ||
| 345 | my $positive = $self->pos_color; | ||
| 346 | my $negative = $self->neg_color; | ||
| 347 | my $midpoint = $self->midpoint; | ||
| 348 | my ($rgb_pos,$rgb_neg,$rgb); | ||
| 349 | if ($pivot) { | ||
| 350 | $rgb_pos = [$self->panel->rgb($positive)]; | ||
| 351 | $rgb_neg = [$self->panel->rgb($negative)]; | ||
| 352 | } else { | ||
| 353 | $rgb = $scaled_max > $scaled_min ? ([$self->panel->rgb($positive)] || [$self->panel->rgb($self->bgcolor)]) | ||
| 354 | : ([$self->panel->rgb($negative)] || [$self->panel->rgb($self->bgcolor)]); | ||
| 355 | } | ||
| 356 | |||
| 357 | my %color_cache; | ||
| 358 | my $flip = $self->{flip}; | ||
| 359 | |||
| 360 | $self->panel->startGroup($gd); | ||
| 361 | foreach (@$parts) { | ||
| 362 | my ($start,$end,$score) = @$_; | ||
| 363 | next unless defined $score; # undefined (absent) score transparent | ||
| 364 | $score = ($score-$mean)/$stdev if $rescale; | ||
| 365 | $score = $scaled_min if $scaled_min > $score; | ||
| 366 | $score = $scaled_max if $scaled_max < $score; | ||
| 367 | |||
| 368 | my $x1 = $left + ($start - $f_start) * $x_scale; | ||
| 369 | my $x2 = $left + ($end - $f_start) * $x_scale; | ||
| 370 | if ($flip) { | ||
| 371 | $x1 = $right - ($x1-$left); | ||
| 372 | $x2 = $right - ($x2-$left); | ||
| 373 | ($x1,$x2) = ($x2,$x1); | ||
| 374 | } | ||
| 375 | |||
| 376 | my ($r,$g,$b) = $pivot | ||
| 377 | ? ($score > $midpoint ? $self->calculate_color($score,$rgb_pos, | ||
| 378 | $midpoint,$scaled_max) | ||
| 379 | : $self->calculate_color($score,$rgb_neg, | ||
| 380 | $midpoint,$scaled_min) | ||
| 381 | ) | ||
| 382 | : $self->calculate_color($score,$rgb, | ||
| 383 | $scaled_min,$scaled_max); | ||
| 384 | my $idx = $color_cache{$r,$g,$b} ||= $self->panel->translate_color($r,$g,$b); | ||
| 385 | $self->filled_box($gd,$x1,$top,$x2,$bottom,$idx,$idx); | ||
| 386 | } | ||
| 387 | return 1; | ||
| 388 | } | ||
| 389 | |||
| 390 | sub _draw_coverage { | ||
| 391 | my $self = shift; | ||
| 392 | my $feature = shift; | ||
| 393 | my $array = shift; | ||
| 394 | |||
| 395 | $array = [split ',',$array] unless ref $array; | ||
| 396 | return unless @$array; | ||
| 397 | |||
| 398 | my ($start,$end) = $self->effective_bounds($feature); | ||
| 399 | my $bases_per_bin = ($end-$start)/@$array; | ||
| 400 | my $pixels_per_base = $self->scale; | ||
| 401 | my @parts; | ||
| 402 | for (my $pixel=0;$pixel<$self->width;$pixel++) { | ||
| 403 | my $offset = $pixel/$pixels_per_base; | ||
| 404 | my $s = $start + $offset; | ||
| 405 | my $e = $s+1; # fill in gaps | ||
| 406 | my $v = $array->[$offset/$bases_per_bin]; | ||
| 407 | #$v = 0 unless defined $v; # don't want undefined values | ||
| 408 | push @parts,[$s,$s,$v]; | ||
| 409 | } | ||
| 410 | $self->Bio::Graphics::Glyph::wiggle_density::draw_plot(\@parts,@_); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub calculate_color { | ||
| 414 | my $self = shift; | ||
| 415 | my ($s,$rgb,$min_score,$max_score) = @_; | ||
| 416 | $s ||= $min_score; | ||
| 417 | |||
| 418 | return (255,255,255) unless $max_score - $min_score; # avoid div by zero | ||
| 419 | |||
| 420 | my $relative_score = ($s-$min_score)/($max_score-$min_score); | ||
| 421 | $relative_score = 0 if $relative_score < 0; | ||
| 422 | $relative_score = 1 if $relative_score > 1; | ||
| 423 | return map { int(255 - (255-$_) * $relative_score) } @$rgb; | ||
| 424 | } | ||
| 425 | |||
| 426 | sub min { $_[0] < $_[1] ? $_[0] : $_[1] } | ||
| 427 | sub max { $_[0] > $_[1] ? $_[0] : $_[1] } | ||
| 428 | |||
| 429 | sub record_label_positions { | ||
| 430 | my $self = shift; | ||
| 431 | my $rlp = $self->option('record_label_positions'); | ||
| 432 | return $rlp if defined $rlp; | ||
| 433 | return 1; | ||
| 434 | } | ||
| 435 | |||
| 436 | sub draw_label { | ||
| 437 | shift->Bio::Graphics::Glyph::xyplot::draw_label(@_); | ||
| 438 | } | ||
| 439 | |||
| 440 | 1; | ||
| 441 | |||
| 442 | __END__ |