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

filter: Statementstmt 0.0% 0 / 235 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 44 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 395 CC 42 SCAR 43.2
CC
42
Cov
0%
calculate_gradient
210.0
calculate_color
90.0
HSVtoRGB
56.0
CRAP 75.3
1 package Bio::Graphics::Glyph::heat_map;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph::generic);
5 use Bio::Graphics::Glyph::minmax;
6  
7 # A glyph to draw a heat map for scored features along a continuous color
8 # gradient calculated in HSV color space
9  
10 sub my_description {
11 return <<END;
12 This glyph draws "scored" features using a continuous
13 color gradient is the HSV color space. The color of
14 each segment is proportional to the score. Either monochrome
15 gradients (for example the default white->red), or gradients progressing
16 through the colors of the rainbow (magenta->blue->green->yelloe->red)
17 can be created.
18  
19 For example:
20 # a white->red heat map
21 start_color = white
22 end_color = red
23  
24 # a rainbow
25 start_color = magenta
26 end_color = red
27  
28 # green->yellow->red
29 start_color = green
30 end_color = red
31 END
32 }
33  
34 sub my_options {
35 {
36 start_color => [
37 'color',
38 'white',
39 'Beginning of the color gradient, expressed as a named color or',
40 'RGB hex string.'],
41 end_color => [
42 'color',
43 'red',
44 'End of the color gradient.'],
45 brightness => [
46 'integer',
47 undef,
48 'Color brilliance: an integer between 0 and 100. This will override',
49 'the value calculated from the name color.'],
50 saturation => [
51 'integer',
52 undef,
53 'Color saturation: an integer between 0 and 100. This will override',
54 'the value calculated from the named color.'],
55 pure_hue => [
56 'boolean',
57 undef,
58 'Use the pure hue (brightness and saturation both at 100)',
59 'for the named color.'],
60 min_score => [
61 'integer',
62 undef,
63 "Minimum value of the feature's \"score\" attribute."],
64 max_score => [
65 'integer',
66 undef,
67 "Maximum value of the feature's \"score\" attribute."],
68 vary_fg => [
69 'boolean',
70 1,
71 'Vary both the foreground and background colors.'],
72 };
73 }
74  
75 # set up getter/setter methods
76 BEGIN {
77 no strict 'refs';
78  
79 my @subs = qw/ h_start s_start v_start h_range s_range v_range
80 min_score max_score low_rgb low_hsv high_rgb score_range/;
81  
82 for my $sub ( @subs ) {
83 *{$sub} = sub {
84 my ($self, $v) = @_;
85 my $k = "_$sub";
86  
87 if (defined $v) {
88 $self->{$k} = $v;
89 }
90  
91 return $self->{$k};
92 }
93 }
94 }
95  
96 sub draw {
97 my $self = shift;
98  
99 my @parts = $self->parts;
100 @parts = $self if !@parts && $self->level == 0;
101 return $self->SUPER::draw(@_) unless @parts;
102  
103 $self->calculate_gradient(\@parts);
104 my $low_rgb = $self->low_rgb;
105  
106 for my $part (@parts) {
107 my $score = $part->feature->score;
108  
109 # use start color if no score or no score gradient
110 unless (defined $score && $self->score_range ) {
111 $part->{partcolor} = $self->color_index(@$low_rgb);
112 }
113 else {
114 my @rgb = $self->calculate_color($score);
115 $part->{partcolor} = $self->color_index(@rgb);
116 }
117
118 }
119  
120 return $self->SUPER::draw(@_);
121 }
122  
123 # We want an exact match, so allocate the color
124 # if required
125 sub color_index {
126 my ($self, @rgb) = @_;
127 my $gd = $self->panel->gd;
128 return $gd->colorResolve(@rgb);
129 }
130  
131 # Override minmax method to get user supplied
132 # values. This will be helpful for single or
133 # unaggregated features.
134 sub minmax {
135 my ($self, $parts) = @_;
136 my $min = $self->option('min_score');
137 my $max = $self->option('max_score');
138 return ($min,$max) if $min && $max && $min < $max;
139 return (0,$max) if $max && !$min; # minscore may be zero
140 return (0,100) unless $parts;
141 return $self->SUPER::minmax($parts);
142 }
143  
144 # convert named color or hex string to RGB value, then HSV
145 sub color2hsv {
146 my ($self,$color) = @_;
147 my $color_idx = $self->panel->translate_color($color);
148 my @rgb = $self->panel->rgb($color_idx);
149 return [$self->RGBtoHSV(@rgb)];
150 }
151  
152 sub calculate_gradient {
153 my ($self, $parts) = @_;
154 my $start_color = lc $self->option('start_color') || 'white';
155 my $stop_color = lc $self->option('end_color') || 'red';
156 my $hsv_start = $self->color2hsv($start_color);
157 my $hsv_stop = $self->color2hsv($stop_color);
158  
159 my ($h_start,$s_start,$v_start) = @$hsv_start;
160 my ($h_stop,$s_stop,$v_stop ) = @$hsv_stop;
161  
162 my $s_range = $s_stop - $s_start;
163 my $v_range = $v_stop - $v_start;
164  
165 my $h_range;
166 # special case: if start hue = end hue, we want to go round
167 # the whole wheel once. Otherwise round the wheel clockwise
168 # or counterclockwise depending on start and end coordinate
169 if ($h_start != $h_stop) {
170 my $direction = abs($h_stop - $h_start)/($h_stop - $h_start);
171 my ($sstart,$sstop) = sort {$a <=> $b} ($h_start,$h_stop);
172 $direction *= -1 if $sstop - $sstart > 256/2; #reverse the direction if we cross 0
173 $h_range = ($sstop - $sstart) <= 256/2 ? ($sstop - $sstart)*$direction : (256 - $sstop + $sstart)*$direction;
174 }
175 else {
176 $h_range = 256;
177 }
178  
179 # override brightness and saturation if required
180 if (my $bri = $self->option('brightness')) {
181 $bri = int($bri*255/100 + 0.5);
182 $v_start = $v_stop = $bri;
183 $v_range = 0;
184 }
185 if (my $sat = $self->option('saturation')) {
186 $sat = int($sat*255/100 + 0.5);
187 $s_start = $s_stop = $sat;
188 $s_range = 0;
189 }
190 if ($self->option('pure_hue')) {
191 $hsv_start = [$h_start,255,255];
192 $hsv_stop = [$h_stop,255,255];
193 $v_start = $v_stop = 255;
194 $s_start = $s_stop = 255;
195 $v_range = $s_range = 0;
196 }
197  
198 # darkness or monochrome gradient?
199 if ( !_isa_color($start_color) || !_isa_color($stop_color) ) {
200 # hue (H) is fixed
201 $h_range = 0;
202  
203 # gradient S V
204 # white -> color 0->255 255
205 # color -> white 255->0 255
206 # white -> black 0 255->0
207 # black -> white 0 0->255
208 # black -> color 0->255 0->255
209 # color -> black 255->0 255->0
210 if ( $start_color eq 'white' && _isa_color($stop_color) ) {
211 $s_range = 255;
212 $s_start = 0;
213 $v_range = 0;
214 $v_start = 255;
215 $h_start = $h_stop;
216 }
217 elsif ( _isa_color($start_color) && $stop_color eq 'white' ) {
218 $s_range = -255;
219 $s_start = 255;
220 $v_range = 0;
221 $v_start = 255;
222 }
223 elsif ( $start_color eq 'white' ) { # end black
224 $s_range = 0;
225 $s_start = 0;
226 $v_range = -255;
227 $v_start = 255;
228 }
229 elsif ( $stop_color eq 'white' ) { # start black
230 $s_range = 0;
231 $s_start = 0;
232 $v_range = 255;
233 $v_start = 0;
234 }
235 elsif ( _isa_color($start_color) ) { # end black
236 $s_range = 255;
237 $s_start = 0;
238 $v_range = 255;
239 $v_start = 0;
240 }
241 elsif ( _isa_color($stop_color) ) { # start black
242 $s_range = -255;
243 $s_start = 255;
244 $v_range = -255;
245 $v_start = 255;
246 }
247
248 }
249  
250 # store gradient info
251 $self->h_range($h_range);
252 $self->h_start($h_start);
253 $self->s_start($s_start);
254 $self->v_start($v_start);
255 $self->s_range($s_range);
256 $self->v_range($v_range);
257  
258 # store score info
259 my ($min,$max) = $self->minmax($parts);
260 $self->score_range($max - $min);
261 $self->min_score($min);
262 $self->max_score($max);
263
264 # store color extremes
265 my @low_rgb = $self->HSVtoRGB(@$hsv_start);
266 my @high_rgb = $self->HSVtoRGB(@$hsv_stop);
267 $self->low_hsv($hsv_start);
268 $self->high_rgb(\@high_rgb);
269 $self->low_rgb(\@low_rgb);
270 return 1;
271 }
272  
273 sub _isa_color {
274 my $color = shift;
275 return $color =~ /white|black|FFFFFF|000000/i ? 0 : 1;
276 }
277  
278 sub calculate_color {
279 my ($self,$score,$min,$max,$range) = @_;
280 $score ||= 0;
281  
282 # relative score
283 $min = $self->min_score || 0 unless defined $min;
284 $max = $self->max_score unless defined $max;
285 $range = $self->score_range || 1 unless defined $range;
286 # reset off-scale scores
287 $score = $min if $score < $min && $min;
288 $score = $max if $score > $max && $max;
289 my $score_diff = ($score - $min)/$range;
290  
291 # Hue
292 my $hue = $self->h_start;
293 my $h_diff = $score_diff * $self->h_range;
294 $hue += $h_diff;
295 $hue = $hue < 255 ? int($hue+0.5) : 255;
296  
297 # Saturation
298 my $sat = $self->s_start;
299 $sat += $score_diff * $self->s_range;
300 $sat = $sat < 255 ? int($sat+0.5) : 255;
301  
302 # Brightness
303 my $bri = $self->v_start;
304 $bri += $score_diff * $self->v_range;
305 $bri = $bri < 255 ? int($bri + 0.5) : 255;
306 return $self->HSVtoRGB($hue,$sat,$bri);
307 }
308  
309 # synthesize a key glyph
310 sub keyglyph {
311 my $self = shift;
312 my $scale = 1/$self->scale; # base pairs/pixel
313 my $offset = $self->panel->offset;
314 my ($min,$max) = $self->minmax;
315 my $range = $max - $min;
316 my ($segments, $low);
317  
318 for my $start (0..9) {
319 $start *= 10;
320 push @$segments, [ $start*$scale + $offset, ($start + 10)*$scale + $offset ];
321 }
322  
323 my $feature = Bio::Graphics::Feature->new( -segments => $segments,
324 -name => $self->option('key'),
325 -strand => '+1' );
326  
327 for (0..9) {
328 my $score += ($range/10) * $_;
329 ($feature->segments)[$_]->score($score);
330 }
331  
332 my $factory = $self->factory->clone;
333 $factory->set_option(label => 1);
334 $factory->set_option(bump => 0);
335 $factory->set_option(min_score => 0);
336 $factory->set_option(max_score => 100);
337 return $factory->make_glyph(0,$feature);
338 }
339  
340 sub bgcolor {
341 my $self = shift;
342 return defined $self->{partcolor} ? $self->{partcolor} : $self->SUPER::bgcolor;
343 }
344  
345 sub fgcolor {
346 my $self = shift;
347 return $self->option('vary_fg') ? $self->bgcolor : $self->SUPER::fgcolor;;
348 }
349  
350 sub RGBtoHSV {
351 my ($self, $r, $g ,$bl) = @_;
352 my ($min,undef,$max) = sort {$a<=>$b} ($r,$g,$bl);
353  
354 my $range = $max - $min or return (0,0,$r);
355 my $v = $max;
356 my $s = 255 * ($max - $min)/$max;
357 my $h;
358
359 if ($max == $r) {
360 $h = 60 * ($g-$bl)/$range;
361 }
362 elsif ($max == $g) {
363 $h = 60 * ($bl-$r)/$range + 120;
364 }
365 else {
366 $h = 60 * ($r-$g)/$range + 240;
367 }
368  
369 $h = int($h*255/360 + 0.5);
370 $h += 255 if $h < 0;
371 $h -= 255 if $h > 255;
372  
373 return ($h, $s, $v);
374 }
375  
376 # method courtesy of Lincoln Stein
377 sub HSVtoRGB {
378 my $self = shift;
379 @_ == 3 or die "Usage: GD::Simple->HSVtoRGB(\$hue,\$saturation,\$value)";
380  
381 my ($h,$s,$v)=@_;
382 my ($r,$g,$b,$i,$f,$p,$q,$t);
383  
384 if( $s == 0 ) {
385 ## achromatic (grey)
386 return ($v,$v,$v);
387 }
388 $h %= 255;
389 $s /= 255; ## scale saturation from 0.0-1.0
390 $h /= 255; ## scale hue from 0 to 1.0
391 $h *= 360; ## and now scale it to 0 to 360
392  
393 $h /= 60; ## sector 0 to 5
394 $i = $h % 6;
395 $f = $h - $i; ## factorial part of h
396 $p = $v * ( 1 - $s );
397 $q = $v * ( 1 - $s * $f );
398 $t = $v * ( 1 - $s * ( 1 - $f ) );
399  
400 if($i<1) {
401 $r = $v;
402 $g = $t;
403 $b = $p;
404 } elsif($i<2){
405 $r = $q;
406 $g = $v;
407 $b = $p;
408 } elsif($i<3){
409 $r = $p;
410 $g = $v;
411 $b = $t;
412 } elsif($i<4){
413 $r = $p;
414 $g = $q;
415 $b = $v;
416 } elsif($i<5){
417 $r = $t;
418 $g = $p;
419 $b = $v;
420 } else {
421 $r = $v;
422 $g = $p;
423 $b = $q;
424 }
425 return (int($r+0.5),int($g+0.5),int($b+0.5));
426 }
427  
428  
429 1;
430  
431 =head1 NAME
432  
433 Bio::Graphics::Glyph::heat_map - The "heat_map" glyph
434  
435 =head1 SYNOPSIS
436  
437 See L<Bio::Graphics::Panel> and L<Bio::Graphics::Glyph>.
438  
439 =head1 DESCRIPTION
440  
441 This glyph draws "scored" features using a continuous
442 color gradient is the HSV color space. The color of
443 each segment is proportionate to the score.
444  
445 =head1 OPTIONS
446  
447 =head2 Global glyph options:
448  
449 See L<Bio::Graphics::Glyph::generic>
450  
451 =head2 Glyph-specific options:
452  
453 The color_map glyph recognizes the following
454 glyph-specific options:
455  
456 Option Description Default
457 ------ ----------- -------
458  
459 -start_color Beginning of the color white
460 gradient, expressed as a
461 named color or RGB hex
462 string
463
464 -end_color End of the color gradient red
465  
466 -brightness Color brilliance: 0-100 Calculated
467 This will override the
468 value from the named
469 color
470  
471 -saturation Color saturation: 0-100 Calculated
472 This will override the
473 value from the named
474 color
475  
476 -pure_hue Use the pure hue (bright- 0 (false)
477 ness and saturation both
478 at 100) for the named color
479
480 -max_score Maximum value of the Calculated
481 feature's "score" attribute
482  
483 -min_score Minimum value of the Calculated
484 feature's "score" attribute
485  
486 -vary_fg Vary the foreground color 1 (true)
487 with the background color
488  
489  
490  
491 If max_score and min_score are not specified, the glyph will
492 calculate the local maximum and minimum scores at run time. If single
493 features, unaggregated features, or multiple aggregates are being drawn,
494 this will result in an inconsistent color scale. It is recommended
495 that global maximum and minimum scores be specified in the track
496 configuration. Since many scoring functions are exponential,
497 you may wish to take the log of your scores before passing
498 them to this glyph.
499  
500 =head2 Color Gradients
501  
502 The color gradient is calculating by
503 progressing along the rainbow spectrum from red to violet,
504 also incrementing brightness and saturation, all proportate
505 to the score value. To vary the hue only, "pure" hues can
506 be used. Pure hues have brightness and saturation values of
507 100. Some examples, in order, are red, yellow, lime,
508 aqua/cyan, blue and magenta. The gradient can progress in
509 reverse orientation with the respect to the visible light
510 spectrum if a lower-order color is used as the start and a higher
511 order color used as the end (for example lime->red).
512  
513 Using the "pure_hue" option results in a brighter, more vibrant
514 color spectrum, Choosing darker start and end colors, such as
515 green or maroon, will result in a darker spectrum.
516 A single color spectrum can be created by using black
517 or white as the start or end color.
518  
519 A grayscale spectrum will result if black and white
520 are used as start and end colors. One example of an
521 effective visual heat map is to progress from
522 white->red.
523  
524 For the start_color and end_color options, 140 named webcolors
525 and their corresponsing RGB hex codes (listed below) are supported.
526  
527 steelblue #4682B4
528 royalblue #041690
529 cornflowerblue #6495ED
530 lightsteelblue #B0C4DE
531 mediumslateblue #7B68EE
532 slateblue #6A5ACD
533 darkslateblue #483D8B
534 midnightblue #191970
535 navy #000080
536 darkblue #00008B
537 mediumblue #0000CD
538 blue #0000FF
539 dodgerblue #1E90FF
540 deepskyblue #00BFFF
541 lightskyblue #87CEFA
542 skyblue #87CEEB
543 lightblue #ADD8E6
544 powderblue #B0E0E6
545 azure #F0FFFF
546 lightcyan #E0FFFF
547 paleturquoise #AFEEEE
548 mediumturquoise #48D1CC
549 lightseagreen #20B2AA
550 darkcyan #008B8B
551 teal #008080
552 cadetblue #5F9EA0
553 darkturquoise #00CED1
554 aqua #00FFFF
555 cyan #00FFFF
556 turquoise #40E0D0
557 aquamarine #7FFFD4
558 mediumaquamarine #66CDAA
559 darkseagreen #8FBC8F
560 mediumseagreen #3CB371
561 seagreen #2E8B57
562 darkgreen #006400
563 green #008000
564 forestgreen #228B22
565 limegreen #32CD32
566 lime #00FF00
567 chartreuse #7FFF00
568 lawngreen #7CFC00
569 greenyellow #ADFF2F
570 yellowgreen #9ACD32
571 palegreen #98FB98
572 lightgreen #90EE90
573 springgreen #00FF7F
574 mediumspringgreen #00FA9A
575 darkolivegreen #556B2F
576 olivedrab #6B8E23
577 olive #808000
578 darkkhaki #BDB76B
579 darkgoldenrod #B8860B
580 goldenrod #DAA520
581 gold #FFD700
582 yellow #FFFF00
583 khaki #F0E68C
584 palegoldenrod #EEE8AA
585 blanchedalmond #FFEBCD
586 moccasin #FFE4B5
587 wheat #F5DEB3
588 navajowhite #FFDEAD
589 burlywood #DEB887
590 tan #D2B48C
591 rosybrown #BC8F8F
592 sienna #A0522D
593 saddlebrown #8B4513
594 chocolate #D2691E
595 peru #CD853F
596 sandybrown #F4A460
597 darkred #8B0000
598 maroon #800000
599 brown #A52A2A
600 firebrick #B22222
601 indianred #CD5C5C
602 lightcoral #F08080
603 salmon #FA8072
604 darksalmon #E9967A
605 lightsalmon #FFA07A
606 coral #FF7F50
607 tomato #FF6347
608 darkorange #FF8C00
609 orange #FFA500
610 orangered #FF4500
611 crimson #DC143C
612 red #FF0000
613 deeppink #FF1493
614 fuchsia #FF00FF
615 magenta #FF00FF
616 hotpink #FF69B4
617 lightpink #FFB6C1
618 pink #FFC0CB
619 palevioletred #DB7093
620 mediumvioletred #C71585
621 purple #800080
622 darkmagenta #8B008B
623 mediumpurple #9370DB
624 blueviolet #8A2BE2
625 indigo #4B0082
626 darkviolet #9400D3
627 darkorchid #9932CC
628 mediumorchid #BA55D3
629 orchid #DA70D6
630 violet #EE82EE
631 plum #DDA0DD
632 thistle #D8BFD8
633 lavender #E6E6FA
634 ghostwhite #F8F8FF
635 aliceblue #F0F8FF
636 mintcream #F5FFFA
637 honeydew #F0FFF0
638 lightgoldenrodyellow #FAFAD2
639 lemonchiffon #FFFACD
640 cornsilk #FFF8DC
641 lightyellow #FFFFE0
642 ivory #FFFFF0
643 floralwhite #FFFAF0
644 linen #FAF0E6
645 oldlace #FDF5E6
646 antiquewhite #FAEBD7
647 bisque #FFE4C4
648 peachpuff #FFDAB9
649 papayawhip #FFEFD5
650 beige #F5F5DC
651 seashell #FFF5EE
652 lavenderblush #FFF0F5
653 mistyrose #FFE4E1
654 snow #FFFAFA
655 white #FFFFFF
656 whitesmoke #F5F5F5
657 gainsboro #DCDCDC
658 lightgrey #D3D3D3
659 silver #C0C0C0
660 darkgray #A9A9A9
661 gray #808080
662 lightslategray #778899
663 slategray #708090
664 dimgray #696969
665 darkslategray #2F4F4F
666 black #000000
667  
668  
669 =head1 BUGS
670  
671 Please report them.
672  
673 =head1 SEE ALSO
674  
675 L<Bio::Graphics::Panel>,
676 L<Bio::Graphics::Glyph>,
677 L<Bio::Graphics::Glyph::graded_segments>,
678 L<Bio::Graphics::Glyph::heterogeneous_segments>,
679 L<Bio::Graphics::Feature>,
680 L<Bio::DB::GFF>
681 L<GD>
682  
683 =head1 AUTHOR
684  
685 Sheldon McKay E<lt>mckays@cshl.eduE<gt>
686  
687 Copyright (c) 2006 Cold Spring Harbor Laboratory
688  
689 This package and its accompanying libraries is free software; you can
690 redistribute it and/or modify it under the terms of the GPL (either
691 version 1, or at your option, any later version) or the Artistic
692 License 2.0. Refer to LICENSE for the full license text. In addition,
693 please see DISCLAIMER.txt for disclaimers of warranty.
694  
695 =cut