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

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 154 CC 15 SCAR 39.7
CC
15
Cov
0%
draw
110.0
get_bg_color
42.0
bump
2.0
CRAP 52.8
1 package Bio::Graphics::Glyph::spectrogram;
2  
3 use strict;
4 use Bio::Graphics::Glyph::generic;
5 use GD::Simple;
6 use GD;
7 use List::Util qw/sum max/;
8  
9 use Data::Dumper;
10  
11 use vars '@ISA';
12 @ISA = 'Bio::Graphics::Glyph::generic';
13  
14 # saturation value is fixed at the maximum
15 use constant SAT => 255;
16 use constant GDS => GD::Simple->new(1,1);
17  
18 # each spectrogram feature will be a standalone
19 # (unaggregated) feature.
20 sub draw {
21 my $self = shift;
22 my $gd = shift;
23 my ( $x1, $y1, $x2, $y2 ) = $self->bounds(@_);
24 my $v_offset = $y1;
25 my $last_y = 0;
26 my $last_label;
27 my $height = $self->option('height');
28 my $win = $self->option('win');
29 my $feat = $self->feature;
30  
31 # API change?
32 my $att;
33 if (ref $feat->attributes) {
34 $att = $feat->attributes;
35 }
36 else {
37 $att = {$feat->attributes};
38 }
39  
40 my $max = $att->{max};
41 my $black = $gd->colorResolve(0,0,0);
42  
43 my %seen;
44  
45 my $rows = @{$att->{g}};
46 my $step = int $height/$rows;
47 my $lbl_type = shift @{$att->{labels}} if defined $att->{labels};
48  
49 for my $g (@{$att->{g}}) {
50 my $a = shift @{$att->{a}};
51 my $t = shift @{$att->{t}};
52 my $c = shift @{$att->{c}};
53 my $lbl = shift @{$att->{labels}} if defined $att->{labels};
54  
55 my ($hue,$bri) = get_bg_color($g,$a,$t,$c);
56 $hue = int(($hue/360) * 255);
57 $hue += 255 if $hue < 0;
58 $hue -= 255 if $hue > 255;
59 $bri = int(($bri/$max) * 255);
60 my @rgb = GDS->HSVtoRGB($hue,SAT,$bri);
61 my $bgcolor = $gd->colorResolve(@rgb);
62 $self->filled_box($gd, $x1, $y1, $x2, $y1+$step, $bgcolor, $bgcolor);
63  
64 if ($lbl && $y1 > $last_y+10) {
65  
66 my $label = sprintf '%4s', int $lbl;
67  
68 # print labels for the number closest to an integer
69 # we use the previous value to catch the transition
70 if ($last_label ne $label) {
71 $gd->string(gdSmallFont, $x1-25, $last_y-5, $last_label, $black);
72 }
73 # this will hopefully catch the label at the bottom of the stack
74 elsif (!$att->{labels}->[0]) {
75 $gd->string(gdSmallFont, $x1-25, $y1-5, $label, $black);
76 }
77
78 $last_y = $y1 + 15;
79 $last_label = $label;
80 }
81
82 if ($lbl) {
83 $gd->stringUp(gdSmallFont, $x1-27, $y2-5, $lbl_type, $black);
84 }
85  
86 $y1 += $step;
87 }
88 }
89  
90  
91 # HSV color space:
92 # Hue (0-360 degrees)
93 # Saturation (0-100)
94 # Brightness (0-100)
95  
96 sub get_bg_color {
97 my ($g,$a,$t,$c) = @_;
98 my $total = sum(@_) || return (0,0);
99 my $max = max (@_);
100 my $angle;
101  
102 # Assign the angular coordinate for the
103 # dominant base (>50% of signal)
104 if ($max == $g && $max >= $total/2) {
105 $angle = 60; # yellow
106 }
107 elsif ($max == $a && $max >= $total/2) {
108 $angle = 240; # blue
109 }
110 elsif ($max == $t && $max >= $total/2) {
111 $angle = 0; # red
112 }
113 elsif ($max == $c && $max >= $total/2) {
114 $angle = 120; # green
115 }
116  
117 # or else take the weighted average coordinate
118 # This is not perfect, as the coordinates are not
119 # equidistant, but most spots will fall into
120 # the above category
121 else {
122 my $acg = 60*$g + 120*$c + 240*$a;
123 my $t_angle = $acg/($g + $c + $a) > 180 ? 360 : 1;
124 $angle = ($t_angle*$t + $acg)/$total;
125 }
126  
127 $angle += 0.5;
128  
129 return (int($angle), $total);
130 }
131  
132 # make sure bumping is off to get an aligned spectrogram
133 sub bump { 0 }
134  
135 1;
136  
137 =head1 NAME
138  
139 Bio::Graphics::Glyph::spectrogram - The "spectrogram" glyph
140  
141 =head1 SYNOPSIS
142  
143 See L<Bio::Graphics::Panel>, L<Bio::Graphics::Glyph>
144 and L<Bio::Graphics::Browser::Plugin::Spectrogram>
145  
146 =head1 DESCRIPTION
147  
148 This glyph is designed to draw DNA spectrograms for the
149 Spectrogram plugin. It is not meant to be used as a
150 standalone glyph and has few public options. Most of the
151 glyph's behavior is controlled via the spectrogram plugin.
152  
153 The glyph expects unaggregated 1D spectrogram features, each of which
154 is a vertical column, with one row for each integer frequency.
155 The number of frequencies is controlled by the window size and/or
156 the Spectrogram plugin. The width of the feature corresponds to
157 the size of the overlap between adjacent windows.
158 The values for each frequency are in four channels,
159 one for each base. The color of each row ("spot")
160 represents the dominant base(s) and the intensity represents
161 the magnitude of the signal at that frequency
162  
163 The entire 2D spectrogram is a series of
164 unaggregated, unbumped 1D spectrogram features.
165
166 The spectrogram glyph assigns colors using the HSV color space,
167 where an angular coordinate for hue is assigned to each base
168 (G yellow [60]; A blue [240]; C green [120]; T red[0/360]).
169 The saturation value is fixed at the maximum of 100 and the brightness
170 value is scaled according to the magnitude for each frequency,
171 ranging from black to the pure hue.
172  
173 The hue is determined in one of two ways:
174  
175 If the signal for one base is dominant (> 50% of total for the four
176 channels) the angular coordinate for that base is used.
177 The brightness is calculated using the total signal from
178 all four channels.
179  
180 If no base has a dominant signal, the weighted average angular
181 coordinate is calculated using the relative contribution
182 from each channel. The brightness is calculated from
183 the total signal from all four channels.
184  
185 The y-axis labels require at least 40 pixels of left-padding.
186 They will be truncated if less than 40 of padding is specified
187 in the configuration file.
188  
189  
190 =head2 OPTIONS
191  
192 The following standard options are accepted:
193  
194 Option Description Default
195 ------ ----------- -------
196  
197 -height Height of glyph calculated
198  
199 -bump Whether to bump features off
200
201 The following glyph-specific options are also used:
202
203 -win window size used to calculate calculated
204 the spectrogram values
205  
206  
207 =head1 BUGS
208  
209 Please report them.
210  
211 =head1 AUTHOR
212  
213 Sheldon McKay E<lt>mckays@cshl.orgE<gt>.
214  
215 Copyright (c) 2006 Cold Spring Harbor Laboratory
216  
217 This package and its accompanying libraries is free software; you can
218 redistribute it and/or modify it under the terms of the GPL (either
219 version 1, or at your option, any later version) or the Artistic
220 License 2.0. Refer to LICENSE for the full license text. In addition,
221 please see DISCLAIMER.txt for disclaimers of warranty.
222  
223 =cut