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

filter: Statementstmt 0.0% 0 / 183 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 62 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 350 CC 35 SCAR 51.2
CC
35
Cov
0%
draw
380.0
draw_component
156.0
sixframe
12.0
CRAP 167.2
1 package Bio::Graphics::Glyph::cds;
2  
3 use strict;
4 use Bio::Graphics::Glyph::segments;
5 use Bio::Graphics::Util qw(frame_and_offset);
6 use Bio::Tools::CodonTable;
7 use base qw(Bio::Graphics::Glyph::segmented_keyglyph Bio::Graphics::Glyph::translation);
8  
9 sub my_description {
10 return <<END;
11 This glyph draws features that are associated with a protein coding
12 region. At high magnifications, draws a series of boxes that are
13 color-coded to indicate the frame in which the translation occurs. At
14 low magnifications, draws the amino acid sequence of the resulting
15 protein. Amino acids that are created by a splice are optionally
16 shown in a distinctive color.
17 END
18 }
19 sub my_options {
20 return {
21 frame0f => [
22 'color',
23 undef,
24 'Color for the first (+) frame. If undefined, uses the bgcolor.'],
25 frame1f => [
26 'color',
27 undef,
28 'Color for the second (+) frame. If undefined, uses the bgcolor.'],
29 frame2f => [
30 'color',
31 undef,
32 'Color for the third (+) frame. If undefined, uses the bgcolor.'],
33 frame0r => [
34 'color',
35 undef,
36 'Color for the first (-) frame. If undefined, uses the bgcolor.'],
37 frame1r => [
38 'color',
39 undef,
40 'Color for the first (-) frame. If undefined, uses the bgcolor.'],
41 frame2r => [
42 'color',
43 undef,
44 'Color for the third (-) frame. If undefined, uses the bgcolor.'],
45 gridcolor => [
46 'color',
47 'lightslategray',
48 'Color for the "staff".'],
49 translation => [
50 ['3frame','6frame'],
51 '3frame',
52 'Number of lines of reading frames to show.',
53 'For best results, specify a height of at least 30 pixels for "6frame",',
54 'and at least 15 pixels for 3frame.'],
55 sixframe => [
56 'boolean',
57 undef,
58 'Draw a six-frame staff. This option overrides -translation,',
59 'which essentially does the same thing.'],
60 require_subparts => [
61 'boolean',
62 undef,
63 "Don't try to draw reading frames unless the feature has subparts."],
64 sub_part => [
65 'string',
66 undef,
67 'For features with multiple subpart types, define which one is the CDS',
68 'part that contains phase information.'],
69 codontable => [
70 'integer',
71 1,
72 'Which codon table to use for translations, see L<Bio::Tools::CodonTable>.'],
73 phase_style => [
74 ['012','021'],
75 '012',
76 'The way the phase method is to be interpreted. See the manual page of this',
77 'glyph for an explanation.'],
78 ignore_empty_phase => [
79 'boolean',
80 undef,
81 'Only draw features that have a phase defined.'],
82 cds_only => [
83 'boolean',
84 undef,
85 'Only draw features of type "CDS".'],
86 }
87 }
88  
89 my %default_colors = qw(
90 frame0f cornflowerblue
91 frame1f blue
92 frame2f darkblue
93 frame0r magenta
94 frame1r red
95 frame2r darkred
96 );
97  
98 my %swap_phase = ( 0 => 0,
99 1 => 2,
100 2 => 1,
101 '' => 0);
102  
103 sub connector { 0 };
104 sub description {
105 my $self = shift;
106 return if $self->level;
107 return $self->SUPER::description;
108 };
109  
110 sub default_color {
111 my ($self,$key) = @_;
112 return $self->factory->translate_color($default_colors{$key});
113 }
114  
115 sub sixframe {
116 my $self = shift;
117 return $self->{sixframe} if exists $self->{sixframe};
118 my $sixframe = $self->option('sixframe');
119 $sixframe = $self->option('translation') eq '6frame' unless defined $sixframe;
120 return $self->{sixframe} = $sixframe;
121 }
122  
123 sub maxdepth { 1 };
124  
125 sub require_subparts {
126 my $self = shift;
127 my $rs = $self->option('require_subparts');
128 $rs = $self->feature->type eq 'coding' if !defined $rs; # shortcut for the "coding" aggregator
129 $rs;
130 }
131  
132 sub ignore_undef_phase {
133 shift->option('ignore_empty_phase');
134 }
135  
136 sub ignore_non_cds {
137 shift->option('cds_only');
138 }
139  
140 sub phase_style {
141 shift->option('phase_style') || '012';
142 }
143  
144 # figure out (in advance) the color of each component
145 sub draw {
146 my $self = shift;
147 my ($gd,$left,$top) = @_;
148  
149 $self->panel->startGroup($gd);
150  
151 my @parts = $self->parts;
152 @parts = $self if !@parts && $self->level == 0 && !$self->require_subparts;
153  
154 my $fits = $self->protein_fits;
155 my $strand = $self->feature->strand || 1;
156  
157 # draw the staff (musically speaking)
158 if ($self->level == 0) {
159 my ($x1,$y1,$x2,$y2) = $self->bounds($left,$top);
160 my $line_count = $self->sixframe ? 6 : 3;
161 my $height = ($y2-$y1)/$line_count;
162 my $grid = $self->gridcolor;
163 for (0..$line_count-1) {
164 my $offset = $y1+$height*$_+1;
165 $gd->line($x1,$offset,$x2,$offset,$grid);
166 # with three-frame translation, the position of the arrows changes depending on
167 # the strand of the feature. With six-frame translation, we draw the first three
168 # staff lines with an arrow to the right, and the second three to the left
169 my $forward = ($line_count == 6) ? ($_ < 3) : ($strand > 0);
170 if ($forward) {
171 $gd->line($x2,$offset,$x2-2,$offset-2,$grid);
172 $gd->line($x2,$offset,$x2-2,$offset+2,$grid);
173 } else {
174 $gd->line($x1,$offset,$x1+2,$offset-2,$grid);
175 $gd->line($x1,$offset,$x1+2,$offset+2,$grid);
176 }
177 }
178 }
179  
180 $self->{cds_part2color} ||= {};
181 my $fill = $self->bgcolor;
182  
183 # figure out the colors of each part
184 # sort minus strand features backward
185 @parts = map { $_->[0] }
186 sort { $b->[1] <=> $a->[1] }
187 map { [$_, $_->left ] } @parts if $strand < 0;
188  
189 my $codon_table = $self->option('codontable');
190 $codon_table = 1 unless defined $codon_table;
191 my $translate_table = Bio::Tools::CodonTable->new(-id=>$codon_table);
192  
193 my $ignore_undef_phase = $self->ignore_undef_phase;
194 my $ignore_non_cds = $self->ignore_non_cds;
195 my $broken_phase = $self->phase_style eq '021';
196  
197 for (my $i=0; $i < @parts; $i++) {
198 my $part = $parts[$i];
199 my $feature = $part->feature;
200  
201 my $type =
202 $feature->can('method') ? $feature->method :
203 $feature->can('type') ? $feature->type :
204 '';
205  
206 next if ($self->option('sub_part') && $type ne $self->option('sub_part'));
207  
208 next if $ignore_non_cds && lc($type) ne 'cds';
209  
210 my $pos = $feature->strand >= 0 ? $feature->start : $feature->end;
211 my $phase = $feature->can('phase') ? $feature->phase # bioperl uses "frame" but this is incorrect usage
212 :$feature->can('frame') ? $feature->frame
213 :undef;
214 next if $ignore_undef_phase && !defined($phase);
215 $phase ||= 0;
216 $phase = $swap_phase{$phase} if $broken_phase;
217 my $strand = $feature->strand;
218 my ($frame,$offset) = frame_and_offset($pos,
219 $strand,
220 $phase);
221 my $suffix = $strand < 0 ? 'r' : 'f';
222 my $key = "frame$frame$suffix";
223 $self->{cds_frame2color}{$key} ||= $self->color($key) || $self->default_color($key) || $fill;
224 $part->{cds_partcolor} = $self->{cds_frame2color}{$key};
225 $part->{cds_frame} = $frame;
226 $part->{cds_offset} = $offset;
227  
228 if ($self->do_cds_translation && (my $seq = $feature->seq)) {
229 BLOCK: {
230 $seq = $self->get_seq($seq);
231
232 # do in silico splicing in order to find the codon that
233 # arises from the splice
234 my $protein = $seq->translate(undef,undef,$phase,$codon_table)->seq;
235 $part->{cds_translation} = $protein;
236
237 length $protein >= $feature->length/3 and last BLOCK;
238 ($feature->length - $phase) % 3 == 0 and last BLOCK;
239
240 my $next_part = $parts[$i+1]
241 or do {
242 $part->{cds_splice_residue} = '?';
243 last BLOCK; };
244
245 my $next_feature = $next_part->feature or last BLOCK;
246 my $next_phase = eval {$next_feature->phase} or last BLOCK;
247 my $splice_codon = '';
248 my $left_of_splice = substr($self->get_seq($feature->seq), -$next_phase, $next_phase);
249 my $right_of_splice = substr($self->get_seq($next_feature->seq),0 , 3-$next_phase);
250 $splice_codon = $left_of_splice . $right_of_splice;
251 length $splice_codon == 3 or last BLOCK;
252 my $amino_acid = $translate_table->translate($splice_codon);
253 $part->{cds_splice_residue} = $amino_acid;
254 }
255 }
256 }
257  
258 $self->Bio::Graphics::Glyph::generic::draw($gd,$left,$top);
259  
260 $self->panel->endGroup($gd);
261 }
262  
263 sub do_cds_translation { return shift->protein_fits }
264  
265 # draw the notes on the staff
266 sub draw_component {
267 my $self = shift;
268 my $gd = shift;
269 my ($x1,$y1,$x2,$y2) = $self->bounds(@_);
270  
271 my $color = $self->{cds_partcolor} or return;
272 my $feature = $self->feature;
273 my $frame = $self->{cds_frame};
274 my $linecount = $self->sixframe ? 6 : 3;
275  
276 unless ($self->protein_fits && $self->{cds_translation}) {
277 my $height = ($y2-$y1)/$linecount;
278 my $offset = $y1 + $height*$frame;
279 $offset += ($y2-$y1)/2 if $self->sixframe && $self->strand < 0;
280 # ugh. This works, but I don't know why
281 $offset = $y1 + (($y2-$y1) - ($offset-$y1))-$height if $self->{flip};
282 $gd->filledRectangle($x1,$offset,$x2,$offset+2,$color);
283 return;
284 }
285  
286 # we get here if there's room to draw the primary sequence
287 my $font = $self->mono_font;
288 my $pixels_per_residue = $self->pixels_per_residue;
289 my $strand = $feature->strand;
290 my $y = $y1-1;
291 my $fontwidth = $font->width;
292  
293 $strand *= -1 if $self->{flip};
294 $y += ($y2-$y1)/2 if $self->sixframe && $strand < 0;
295  
296 # have to remap feature start and end into pixel coords in order to:
297 # 1) correctly align the amino acids with the nucleotide seq
298 # 2) correct for the phase offset
299 my $start = $self->map_no_trunc($feature->start + $self->{cds_offset});
300 my $stop = $self->map_no_trunc($feature->end + $self->{cds_offset});
301  
302 ($start,$stop) = ($stop,$start) if $stop < $start; # why does this keep happening?
303  
304 my @residues = split '',$self->{cds_translation};
305  
306 push @residues,$self->{cds_splice_residue} if $self->{cds_splice_residue};
307 for (my $i=0;$i<@residues;$i++) {
308 my $x = $strand > 0 ? $start + $i * $pixels_per_residue
309 : $stop - $i * $pixels_per_residue;
310 next unless ($x >= $x1 && $x <= $x2);
311 $x -= $fontwidth + 1 if $self->{flip}; # align right when flipped
312 $gd->char($font,$x+1,$y,$residues[$i],$color);
313 }
314 }
315  
316 sub make_key_feature {
317 my $self = shift;
318 my @gatc = qw(g a t c);
319 my $offset = $self->panel->offset;
320 my $scale = 1/$self->scale; # base pairs/pixel
321 my $start = $offset;
322 my $stop = $offset + 100 * $scale;
323 my $seq = join('',map{$gatc[rand 4]} (1..1500));
324 my $feature =
325 Bio::Graphics::Feature->new(-start=> $start,
326 -end => $stop,
327 -seq => $seq,
328 -name => $self->option('key'),
329 -strand=> +1,
330 );
331 $feature->add_segment(Bio::Graphics::Feature->new(
332 -start=> $start,
333 -end => $start + ($stop - $start)/2,
334 -seq => $seq,
335 -name => $self->option('key'),
336 -strand=> +1,
337 ),
338 Bio::Graphics::Feature->new(
339 -start=> $start + ($stop - $start)/2+1,
340 -end => $stop,
341 -seq => $seq,
342 -name => $self->option('key'),
343 -phase=> 1,
344 -strand=> +1,
345 ));
346 $feature;
347 }
348  
349 # never allow our components to bump
350 sub bump {
351 my $self = shift;
352 return $self->SUPER::bump(@_) if $self->all_callbacks;
353 return 0;
354 }
355  
356 1;
357  
358 __END__