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

filter: Statementstmt 0.0% 0 / 188 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 47 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 325 CC 35 SCAR 50.9
CC
35
Cov
0%
draw_gc_content
342.0
draw_dna
110.0
draw_component
30.0
CRAP 162.6
1 package Bio::Graphics::Glyph::dna;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph::generic);
5  
6 my %complement = (g=>'c',a=>'t',t=>'a',c=>'g',n=>'n',
7 G=>'C',A=>'T',T=>'A',C=>'G',N=>'N');
8  
9 sub my_description {
10 return <<END;
11 This glyph draws DNA sequences. At high magnifications, this glyph
12 will draw the actual base pairs of the sequence (both strands). At
13 low magnifications, the glyph will plot the GC content. By default,
14 the GC calculation will use non-overlapping bins, but this can be
15 changed by specifying the gc_window option, in which case, a
16 sliding window calculation will be used.
17  
18 For this glyph to work, the attached feature must return
19 a Bio::PrimarySeq DNA in response to the seq() method.
20 END
21 }
22  
23 sub my_options {
24 return {
25 do_gc => [
26 'boolean',
27 1,
28 'Whether to draw the GC graph at low magnifications.'],
29 gc_window=> [
30 'integer',
31 undef,
32 'Size of the sliding window to use in the GC content',
33 'calculation. If this is not defined, non-overlapping',
34 'bins will be used. If this is set to "auto", then the',
35 'glyph will choose a window equal to 1% of the interval.'],
36 gc_bins => [
37 'integer',
38 100,
39 'Fixed number of intervals to sample across the track.'],
40 axis_color => [
41 'color',
42 'black',
43 'Color of the vertical axes in the GC content graph.'],
44 strand => [
45 [qw(forward reverse both auto)],
46 'auto',
47 'Show both forward and reverse strand, one of "forward", "reverse",',
48 '"both" or "auto". In "auto" mode, +1 strand features will',
49 'show the plus strand, -1 strand features will',
50 'show the reverse complement and strandless features will show both.'],
51 };
52 }
53  
54 # turn off description
55 sub description { 0 }
56  
57 # turn off label
58 # sub label { 1 }
59  
60 sub pad_top {
61 my $self = shift;
62 my $font = $self->mono_font;
63 my $pt = $self->SUPER::pad_top;
64 return $self->dna_fits
65 ? $pt + $font->height+5
66 : 16;
67 }
68  
69 sub height {
70 my $self = shift;
71 my $font = $self->mono_font;
72 return $self->dna_fits ? 2*$font->height
73 : $self->do_gc ? $self->SUPER::height
74 : 0;
75 }
76  
77 sub do_gc {
78 my $self = shift;
79 my $do_gc = $self->option('do_gc');
80 return if defined($do_gc) && !$do_gc;
81 return 1;
82 }
83  
84 sub draw_component {
85 my $self = shift;
86 my $gd = shift;
87 my ($x1,$y1,$x2,$y2) = $self->bounds(@_);
88  
89 my $dna = eval { $self->feature->seq };
90 $dna = $dna->seq if ref($dna) and $dna->can('seq'); # to catch Bio::PrimarySeqI objects
91 $dna or return;
92  
93 # workaround for my misreading of interface -- LS
94 $dna = $dna->seq if ref($dna) && $dna->can('seq');
95  
96 if ($self->dna_fits) {
97 $self->draw_dna($gd,$dna,$x1,$y1,$x2,$y2);
98 } elsif ($self->do_gc) {
99 $self->draw_gc_content($gd,$dna,$x1,$y1,$x2,$y2);
100 }
101 }
102  
103 sub draw_dna {
104 my $self = shift;
105  
106 my ($gd,$dna,$x1,$y1,$x2,$y2) = @_;
107 my $pixels_per_base = $self->scale;
108 my $feature = $self->feature;
109  
110 my $strand = $feature->strand || 1;
111 $strand *= -1 if $self->{flip};
112  
113 my @bases = split '',$strand >= 0 ? $dna : $self->reversec($dna);
114  
115 my $color = $self->fgcolor;
116 my $font = $self->mono_font;
117 my $lineheight = $font->height;
118 $y1 -= $lineheight/2 - 3;
119 my $strands = $self->option('strand') || 'auto';
120  
121 my ($forward,$reverse);
122 if ($strands eq 'auto') {
123 $forward = $feature->strand >= 0;
124 $reverse = $feature->strand <= 0;
125 } elsif ($strands eq 'both') {
126 $forward = $reverse = 1;
127 } elsif ($strands eq 'reverse') {
128 $reverse = 1;
129 } else {
130 $forward = 1;
131 }
132 # minus strand features align right, not left
133 $x1 += $pixels_per_base - $font->width - 1 if $strand < 0;
134 for (my $i=0;$i<@bases;$i++) {
135 my $x = $x1 + $i * $pixels_per_base;
136 $gd->char($font,$x+2,$y1,$bases[$i],$color) if $forward;
137 $gd->char($font,$x+2,$y1+($forward ? $lineheight:0),
138 $complement{$bases[$i]}||$bases[$i],$color) if $reverse;
139 }
140  
141 }
142  
143 sub draw_gc_content {
144 my $self = shift;
145 my $gd = shift;
146 my $dna = shift;
147 my ($x1,$y1,$x2,$y2) = @_;
148  
149 $dna = $self->reversec($dna) if $self->{flip};
150 my $font = $self->mono_font;
151  
152 # get the options that tell us how to draw the GC content
153  
154 my $bin_size = length($dna) / ($self->option('gc_bins') || 100);
155 $bin_size = 10 if $bin_size < 10;
156 my $gc_window = $self->option('gc_window');
157 # if ($gc_window && $gc_window eq 'auto' or $gc_window <= length($dna)) {
158 if ($gc_window) {
159 if ($gc_window eq 'auto') {
160 $gc_window = length($dna)/100;
161 } elsif ($gc_window > length($dna)) {
162 $gc_window = length($dna);
163 }
164 }
165  
166 # Calculate the GC content...
167  
168 my (@bins, @datapoints, $i);
169 my $gc = 0;
170 my $maxgc = -1000;
171 my $mingc = +1000;
172  
173 if ($gc_window) { # ...using a sliding window...
174  
175 my @dna = split '', $dna;
176 for ($i = 0; $i < @dna; $i++) {
177 if ($dna[$i] eq 'G' or $dna[$i] eq 'C' or $dna[$i] eq 'g' or $dna[$i] eq 'c') {
178 $dna[$i] = 1;
179 } else {
180 $dna[$i] = 0;
181 }
182 }
183  
184 for ($i = 0; $i < $gc_window; $i++) {$gc += $dna[$i]}
185  
186 push @datapoints, $gc;
187 $datapoints[$#datapoints] > $maxgc and $maxgc = $datapoints[$#datapoints];
188 $datapoints[$#datapoints] < $mingc and $mingc = $datapoints[$#datapoints];
189 for ($i = 0; $i < @dna - $gc_window; $i++) {
190 $gc -= $dna[$i];
191 $gc += $dna[$i + $gc_window];
192 push @datapoints, $gc;
193 $datapoints[$#datapoints] > $maxgc and $maxgc = $datapoints[$#datapoints];
194 $datapoints[$#datapoints] < $mingc and $mingc = $datapoints[$#datapoints];
195 }
196  
197 my $scale = $maxgc - $mingc;
198 $scale = 1 unless $scale;
199 for ($i = 0; $i < @datapoints; $i++) {
200 $datapoints[$i] = ($datapoints[$i] - $mingc) / $scale;
201 }
202 $maxgc = int($maxgc * 100 / $gc_window);
203 $mingc = int($mingc * 100 / $gc_window);
204  
205 } else { # ...or a fixed number of bins.
206  
207 for (my $i = 0; $i < length($dna) - $bin_size; $i+= $bin_size) {
208 my $subseq = substr($dna,$i,$bin_size);
209 my $gc = $subseq =~ tr/gcGC/gcGC/;
210 my $content = $gc/$bin_size;
211 $maxgc = $content if ($content > $maxgc);
212 $mingc = $content if ($content < $mingc);
213 push @bins,$content;
214 }
215  
216 my $scale = $maxgc - $mingc;
217 foreach (my $i; $i < @bins; $i++)
218 {
219 $bins[$i] = $scale != 0 ? ($bins[$i] - $mingc) / $scale : 0;
220 }
221 $maxgc = int($maxgc * 100);
222 $mingc = int($mingc * 100);
223  
224 }
225  
226 # Calculate values that will be used in the layout
227  
228 push @bins,0.5 unless @bins; # avoid div by zero
229 my $bin_width = ($x2-$x1)/@bins;
230 my $bin_height = $y2-$y1;
231 my $fgcolor = $self->fgcolor;
232 my $bgcolor = $self->factory->translate_color(
233 $self->option('grid color') || $self->panel->gridmajorcolor
234 );
235 my $axiscolor = $self->color('axis_color') || $fgcolor;
236  
237 # Draw the axes
238 my $fontwidth = $font->width;
239 $gd->line($x1, $y1, $x1, $y2, $axiscolor);
240 $gd->line($x2-2,$y1, $x2-2,$y2, $axiscolor);
241 $gd->line($x1, $y1, $x1+3,$y1, $axiscolor);
242 $gd->line($x1, $y2, $x1+3,$y2, $axiscolor);
243 $gd->line($x1, ($y2+$y1)/2,$x1+3,($y2+$y1)/2,$axiscolor);
244 $gd->line($x2-4,$y1, $x2-1, $y1, $axiscolor);
245 $gd->line($x2-4,$y2, $x2-1, $y2, $axiscolor);
246 $gd->line($x2-4,($y2+$y1)/2,$x2-1,($y2+$y1)/2,$axiscolor);
247 $gd->line($x1+5,$y2, $x2-5,$y2, $bgcolor);
248 $gd->line($x1+5,($y2+$y1)/2,$x2-5,($y2+$y1)/2,$bgcolor);
249 $gd->line($x1+5,$y1, $x2-5,$y1, $bgcolor);
250 $gd->string($self->font,$x1-$self->string_width('% gc',$self->font),$y1,'% gc',$axiscolor)
251 if $bin_height > $self->font_height($font)*2;
252  
253 # If we are using a sliding window, the GC graph will be scaled to use the full
254 # height of the glyph, so label the right vertical axis to show the scaling that# is in effect
255  
256 $gd->string($self->font,$x2+3,$y1,"${maxgc}%",$axiscolor)
257 if $bin_height > $self->font_height*2.5;
258 $gd->string($self->font,$x2+3,$y2-$self->font_height,"${mingc}%",$axiscolor)
259 if $bin_height > $self->font_height*2.5;
260  
261 # Draw the GC content graph itself
262  
263 if ($gc_window) {
264  
265 my $graphwidth = $x2 - $x1;
266  
267 # the $points_to_draw variable here can be used in various ways to adjust the
268 # sampling of the graphic output. It could be converted to a glphy
269 # option that can be set by user. Here we are just using $graphwidth
270  
271 # my $points_to_draw = 1800;
272 my $points_to_draw = $graphwidth;
273  
274 # if $points_to_draw is taken to mean the total number of points to show along the graph then
275 my $inc = int (length($dna) / $points_to_draw) + 1;
276 # it might be useful to set this to the x pixel resolution of your
277 # screen or the "default width" option in the gbrowse config file or current pixel width
278  
279 # if $points_to_draw is taken to mean the number of points per $gc_window length then
280 # my $inc = int ($gc_window / $points_to_draw) + 1;
281  
282 my $gc_window_width = $gc_window * $self->panel->scale; # pixels for the gc_window length
283 my $scale = ($graphwidth - $gc_window_width) / @datapoints;
284 for ($i = $inc; $i < @datapoints; $i += $inc) {
285 my $x = $i + $gc_window / 2; # the base at the center of the gc_window
286 my $xlo = $x1 + ($x - $inc) * $scale; # pixel coordinate for previous point
287 my $xhi = $x1 + $x * $scale;
288 $gd->line($xlo, $y2 - ($bin_height * $datapoints[$i-$inc]),
289 $xhi, $y2 - ($bin_height * $datapoints[$i]),
290 $fgcolor);
291 }
292 }
293 else
294 {
295 for (my $i = 0; $i < @bins; $i++)
296 {
297 my $bin_start = $x1+$i*$bin_width;
298 my $bin_stop = $bin_start + $bin_width;
299 my $y = $y2 - ($bin_height*$bins[$i]);
300 $gd->line($bin_start,$y,
301 $bin_stop,$y,
302 $fgcolor);
303 $gd->line($bin_stop,$y,
304 $bin_stop,$y2 - ($bin_height*$bins[$i+1]),
305 $fgcolor)
306 if $i < @bins-1;
307 }
308 }
309 }
310  
311 sub make_key_feature {
312 my $self = shift;
313 my @gatc = qw(g a t c);
314 my $offset = $self->panel->offset;
315 my $scale = 1/$self->scale; # base pairs/pixel
316  
317 my $start = $offset+1;
318 my $stop = $offset+100*$scale;
319 my $feature =
320 Bio::Graphics::Feature->new(-start=> $start,
321 -stop => $stop,
322 -seq => join('',map{$gatc[rand 4]} (1..500)),
323 -name => $self->option('key'),
324 -strand => '+1',
325 );
326 $feature;
327 }
328  
329 1;
330  
331 __END__