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

filter: Statementstmt 0.0% 0 / 431 Branchbran 0.0% 0 / 228 Conditioncond 0.0% 0 / 129 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 56 Podpod 0.0% 0 / 51 totaltotal 0.0% 0 / 895 CC 115 SCAR 41.5
CC
115
Cov
0%
calculate_cds
240.0
draw_sequence
182.0
draw_translation
132.0
CRAP 63.4
1 package Bio::Graphics::Glyph::generic;
2  
3 use strict;
4 use Bio::Graphics::Util qw(frame_and_offset);
5 use base qw(Bio::Graphics::Glyph);
6 use Memoize 'memoize';
7 #memoize('pad_left');
8 #memoize('pad_right');
9  
10 my %complement = (g=>'c',a=>'t',t=>'a',c=>'g',
11 G=>'C',A=>'T',T=>'A',C=>'G');
12  
13 # new options are 'label' -- short label to print over glyph
14 # 'description' -- long label to print under glyph
15 # label and description can be flags or coderefs.
16 # If a flag, label will be taken from seqname, if it exists or primary_tag().
17 # description will be taken from source_tag().
18 sub my_description {
19 return <<END;
20 This glyph draws genomic features as rectangles. If the feature contains subfeatures,
21 each subfeature is rendered as a separate rectangle. The subfeatures can be connected
22 by lines of various sorts using the "connector" option. Features can be named with
23 a label at the top, and annotated with a descriptive string at the bottom.
24 END
25 }
26  
27 sub my_options {
28 {
29 label => [
30 'string',
31 undef,
32 'Whether to label the feature. A value of 1 will label the feature with',
33 'the value returned by its display_name() method. Any other true value',
34 'will label the feature with the provided value. Undef will suppress labeling',
35 'entirely.'],
36 label_position => [
37 [qw(top left alignment_left)],
38 'top',
39 'Where to place the feature label.',
40 '"top" will place the label above the glyph aligned with its left side.',
41 '"left" will place the label to the left of the glyph, vertically centered with it.',
42 '"alignment_left" will place the label to the left of the glyph in the panel pad-left positon.',
43 'The last option is used internally for drawing DNA alignments which span the screen.'
44 ],
45 part_labels => [
46 'boolean',
47 undef,
48 'If false, do not label subparts of the feature. If equal to a value of 1, subparts',
49 'are labeled with their display_name(). Any other true value, will be used as the subpart label.',
50 'A false value suppresses the printing of subpart labels.'],
51 arrowhead => [
52 [qw(regular filled)],
53 'regular',
54 'Set the style of arrowhead used when drawing a stranded feature.',
55 '"regular" will generate a thin arrowhead that protrudes from the feature.',
56 '"filled" will taper the feature itself to turn it into an arrowhead.'],
57 description => [
58 'string',
59 undef,
60 'Whether to place a description underneath the feature. ',
61 'A value of 1 will describe the feature using the values returned',
62 'by its source_tag() method. Any other true value',
63 'will label the feature with the provided value. Undef will suppress labeling',
64 'entirely.'],
65 draw_translation => [
66 'boolean',
67 undef,
68 'Draw the protein translation of the feature (assumes that the feature is attached to a DNA sequence.'
69 ],
70 draw_dna => [
71 'boolean',
72 undef,
73 'If true, draw the dna residues when magnification level',
74 'allows (assumes that the feature is attached to a DNA sequence.'],
75 pad_top => [
76 'integer',
77 0,
78 'Additional whitespace (in pixels) to add to the top of this glyph.'],
79 pad_bottom => [
80 'integer',
81 0,
82 'Additional whitespace (in pixels) to add to the bottom of this glyph.'],
83 pad_right=> [
84 'integer',
85 0,
86 'Additional whitespace (in pixels) to add to the right of this glyph.'],
87 pad_left=> [
88 'integer',
89 0,
90 'Additional whitespace (in pixels) to add to the left of this glyph.'],
91 labelcolor => [
92 'color',
93 'black',
94 'The color to use for drawing label text in this glyph (also known as fontcolor).'],
95 fontcolor => [
96 'color',
97 'black',
98 'The color to use for drawing label text in this glyph (also known as labelcolor).'],
99 font2color => [
100 'color',
101 'black',
102 'The color to use for drawing description text in this glyph (also known as descriptioncolor.'],
103 descriptioncolor => [
104 'color',
105 'black',
106 'The color to use for drawing description text in this glyph (also known as font2color.'],
107 basecolor => [
108 'color',
109 'black',
110 'The color to use for drawing DNA/protein residues at the base level',
111 ],
112 font => [
113 'font',
114 'gdSmallFont',
115 'Font for glyph label and description.'],
116 connector=>[
117 [qw(hat solid quill dashed crossed undef)],
118 undef,
119 'Type of line to use for connecting discontinuous pieces of the feature.',
120 'Leave this undef to draw no connector at all.'],
121 connector_color => [
122 'color',
123 'black',
124 'Color to use for lines connecting discontinuous pieces of the feature.'],
125 record_label_positions => [
126 'integer',
127 undef,
128 'If true, remember the coordinates of the glyph label and return it',
129 'by calling $panel->key_boxes. If -1, then remember coordinates, but',
130 "don't actually draw the label",
131 ]
132 }
133  
134 }
135  
136 sub connector {
137 return shift->option('connector',@_);
138 }
139 sub connector_color {
140 my $self = shift;
141 $self->color('connector_color') || $self->fgcolor;
142 }
143 sub mono_font {
144 return GD->gdSmallFont;
145 }
146  
147 sub font {
148 my $self = shift;
149 return $self->getfont('font','gdSmallFont');
150 }
151  
152 sub fontcolor {
153 my $self = shift;
154 my $fontcolor = $self->color('labelcolor') || $self->color('fontcolor');
155 return defined $fontcolor ? $fontcolor : $self->fgcolor;
156 }
157 sub font2color {
158 my $self = shift;
159 my $font2color = $self->color('descriptioncolor') || $self->color('font2color');
160 return defined $font2color ? $font2color : $self->fgcolor;
161 }
162 sub basecolor {
163 my $self = shift;
164 my $basecolor = $self->color('basecolor');
165 return defined $basecolor ? $basecolor : $self->fgcolor;
166 }
167  
168 sub labelcolor {shift->fontcolor}
169 sub descriptioncolor {shift->font2color}
170  
171 sub record_label_positions { shift->option('record_label_positions') }
172  
173 sub height {
174 my $self = shift;
175 my $h = $self->SUPER::height;
176 return $h unless
177 $self->option('draw_translation') && $self->protein_fits
178 or
179 $self->option('draw_dna') && $self->dna_fits;
180 my $fh = $self->font_height + 2;
181 return $h > $fh ? $h : $fh;
182 }
183  
184 sub pad_top {
185 my $self = shift;
186 my $top = $self->option('pad_top');
187 return $top if defined $top;
188 my $pad = $self->SUPER::pad_top;
189 $pad += $self->labelheight if $self->label && $self->label_position eq 'top';
190 $pad;
191 }
192 sub pad_bottom {
193 my $self = shift;
194 my $bottom = $self->option('pad_bottom');
195 return $bottom if defined $bottom;
196 my $pad = $self->SUPER::pad_bottom;
197 $pad += $self->labelheight+6 if $self->description;
198 $pad += $self->labelheight+6 if $self->part_labels && $self->label_position eq 'top';
199 $pad;
200 }
201 sub pad_right {
202 my $self = shift;
203 my $pad = $self->SUPER::pad_right;
204 return $pad unless $self->label;
205 my $label_width = $self->label_position eq 'top' ? $self->labelwidth : 0;
206 my $description_width = $self->descriptionwidth;
207 my $max = $label_width > $description_width ? $label_width : $description_width;
208 my $right = $max - $self->width;
209 return $pad > $right ? $pad : $right;
210 }
211 sub pad_left {
212 my $self = shift;
213 my $pad = $self->SUPER::pad_left;
214 return $pad unless $self->label_position eq 'left' && $self->label;
215 $pad += $self->labelwidth + 3;
216 $pad;
217 }
218 sub labelfont {
219 my $self = shift;
220 my $font = $self->getfont('label_font',$self->font);
221 $font = GD->gdTinyFont if $self->string_height($font) > $self->height && $self->label_position eq 'left';
222 return $font;
223 }
224 sub descfont {
225 my $self = shift;
226 return $self->getfont('desc_font',$self->font);
227 }
228 sub labelwidth {
229 my $self = shift;
230 return $self->{labelwidth} ||= $self->string_width($self->label||'',$self->labelfont);
231 }
232 sub descriptionwidth {
233 my $self = shift;
234 return $self->{descriptionwidth} ||= $self->string_width($self->description||'',$self->descfont);
235 }
236 sub labelheight {
237 my $self = shift;
238 return $self->{labelheight} ||= $self->string_height($self->labelfont);
239 }
240 sub label_position {
241 my $self = shift;
242 return $self->{labelposition} ||= $self->option('label_position') || 'top';
243 }
244 sub label {
245 my $self = shift;
246  
247 return if $self->{overbumped}; # set by the bumper when we have hit bump limit
248 return unless $self->subpart_callbacks; # returns true if this is level 0 or if subpart callbacks allowed
249 return $self->_label if $self->{level} >= 0;
250  
251 return exists $self->{label} ? $self->{label}
252 : ($self->{label} = $self->_label);
253 }
254 sub description {
255 my $self = shift;
256 return if $self->{overbumped}; # set by the bumper when we have hit bump limit
257 return unless $self->subpart_callbacks; # returns true if this is level 0 or if subpart callbacks allowed
258 return $self->_description if $self->{level} > 0;
259 return exists $self->{description} ? $self->{description}
260 : ($self->{description} = $self->_description);
261 }
262  
263 sub part_labels {
264 my $self = shift;
265 my @parts = $self->parts;
266 return ($self->{level} == 0) && @parts && @parts>1 && $self->option('part_labels');
267 }
268  
269 sub part_label_merge {
270 shift->option('part_label_merge');
271 }
272  
273 sub maxdepth {
274 my $self = shift;
275 my $maxdepth = $self->option('maxdepth');
276 return $maxdepth if defined $maxdepth;
277 return 1;
278 }
279  
280 sub _label {
281 my $self = shift;
282  
283 # allow caller to specify the label
284 my $label = $self->option('label');
285  
286 return unless defined $label;
287 return "1" if $label eq '1 '; # 1 with a space
288 return $label unless $label eq '1';
289  
290 # figure it out ourselves
291 my $f = $self->feature;
292  
293 if ($f->can('display_name') && (my $name = $f->display_name)) {
294 return $name;
295 }
296  
297 if ($f->can('attributes') && (my @aliases = $f->attributes('Alias'))) {
298 return $aliases[0];
299 }
300 return $f->info if $f->can('info'); # deprecated API
301 return $f->seq_id if $f->can('seq_id');
302 return eval{$f->primary_tag};
303 }
304 sub _description {
305 my $self = shift;
306  
307 # allow caller to specify the long label
308 my $label = $self->option('description');
309 return unless defined $label;
310 return "1" if $label eq '1 ';
311 return $label unless $label eq '1';
312  
313 return $self->{_description} if exists $self->{_description};
314 return $self->{_description} = $self->get_description($self->feature);
315 }
316  
317 sub get_description {
318 my $self = shift;
319 my $feature = shift;
320  
321 local $^W = 0;
322  
323 # common places where we can get descriptions
324 return join '; ',$feature->notes if $feature->can('notes');
325 return $feature->desc if $feature->can('desc');
326  
327 if ($feature->can('has_tag')) {
328 return join '; ',$feature->get_tag_values('note') if $feature->has_tag('note');
329 return join '; ',$feature->get_tag_values('Note') if $feature->has_tag('Note');
330 return join '; ',$feature->get_tag_values('description') if $feature->has_tag('description');
331 }
332  
333 my $tag = $feature->source_tag;
334 return if $tag eq '';
335 $tag;
336 }
337  
338 sub draw {
339 my $self = shift;
340 my ($gd,$left,$top,$partno,$total_parts) = @_;
341  
342 local($self->{partno},$self->{total_parts});
343 @{$self}{qw(partno total_parts)} = ($partno,$total_parts);
344  
345 $self->calculate_cds() if $self->option('draw_translation') && $self->protein_fits;
346  
347 $self->panel->startGroup($gd);
348 $self->SUPER::draw(@_);
349 $self->draw_label(@_) if $self->option('label');
350 $self->draw_description(@_) if $self->option('description');
351 $self->draw_part_labels(@_) if $self->option('label') && $self->option('part_labels');
352 $self->panel->endGroup($gd);
353 }
354  
355 sub draw_component {
356 my $self = shift;
357 $self->SUPER::draw_component(@_);
358 $self->draw_translation(@_) if $self->{cds_translation}; # created earlier by calculate_cds()
359 $self->draw_sequence(@_) if $self->option('draw_dna') && $self->dna_fits;
360 }
361  
362 # mostly stolen from cds.pm -- draw the protein translation
363 sub draw_translation {
364 my $self = shift;
365 my $gd = shift;
366 my ($x1,$y1,$x2,$y2) = $self->bounds(@_);
367  
368 my $feature = $self->feature;
369 my $strand = $feature->strand;
370  
371 my $font = $self->mono_font;
372 my $pixels_per_residue = $self->scale * 3;
373  
374 my $y = $y1 + ($self->height - $font->height)/2;
375 my $fontwidth = $font->width;
376 my $color = $self->basecolor;
377  
378 $strand *= -1 if $self->{flip};
379  
380 # have to remap feature start and end into pixel coords in order to:
381 # 1) correctly align the amino acids with the nucleotide seq
382 # 2) correct for the phase offset
383 my $start = $self->map_no_trunc($feature->start + $self->{cds_offset});
384 my $stop = $self->map_no_trunc($feature->end + $self->{cds_offset});
385  
386 ($start,$stop) = ($stop,$start) if $stop < $start; # why does this keep happening?
387 my $x_fudge = $self->{flip} ? -1 : 2;
388 my $right = $self->panel->right;
389 my $left = $self->panel->left;
390  
391 my @residues = split '',$self->{cds_translation};
392 # warn "residues = @residues, start=$start, stop=$stop, strand=$strand, x1=$x1, x2=$x2, cds_offset = $self->{cds_offset}";
393  
394 push @residues,$self->{cds_splice_residue_tail}
395 if $self->{cds_splice_residue_tail};
396  
397 for (my $i=0;$i<@residues;$i++) {
398 my $x = $strand > 0 ? $start + $i * $pixels_per_residue
399 : $stop - $i * $pixels_per_residue;
400 $x -= $fontwidth + 1 if $self->{flip}; # align right when flipped
401 my $pos = $x+$x_fudge;
402 $gd->char($font,$pos,$y,$residues[$i],$color)
403 if $pos >= $x1 && $pos <= $x2;
404 }
405  
406 if ($self->{cds_splice_residue_head}) {
407 $gd->char($font,$x1+2,$y,$self->{cds_splice_residue_head},$color)
408 if $strand > 0 && $start >= $left;
409 $gd->char($font,$x2-$fontwidth-2,$y,$self->{cds_splice_residue_head},$color)
410 if $strand < 0 && $stop <= $right;
411 }
412 }
413  
414 sub draw_sequence {
415 my $self = shift;
416 my $gd = shift;
417 my ($x1,$y1,$x2,$y2) = $self->bounds(@_);
418  
419 my $feature = $self->feature;
420 my $strand = $feature->strand;
421  
422 my $font = $self->mono_font;
423 my $pixels_per_base = $self->scale;
424  
425 my $y = $y1 + ($self->height - $font->height)/2 - 1;
426 my $fontwidth = $font->width;
427 my $color = $self->basecolor;
428  
429 $strand *= -1 if $self->{flip};
430  
431 # have to remap feature start and end into pixel coords in order to:
432 my $start = $self->map_no_trunc($feature->start);
433 my $stop = $self->map_no_trunc($feature->end);
434  
435 ($start,$stop) = ($stop,$start) if $stop < $start; # why does this keep happening?
436 my $x_fudge = $self->{flip} ? 1 : 2;
437 my $right = $self->panel->right;
438 my $left = $self->panel->left;
439  
440 my $seq = $self->get_dna($self->feature);
441  
442 my $canonical = $self->option('canonical_strand');
443  
444 my @bases = split '',$seq;
445 for (my $i=0;$i<@bases;$i++) {
446 my $x = $strand >= 0 ? $start + $i * $pixels_per_base
447 : $stop - $i * $pixels_per_base;
448 next unless ($x >= $x1 && $x <= $x2);
449 $x -= $fontwidth + 1 if $self->{flip}; # align right when flipped
450 if ($strand >= 0) {
451 last if $x + $fontwidth > $right;
452 } else {
453 next if $x >= $right;
454 last if $x < $left;
455 }
456 my $base = $self->{flip} ? $complement{$bases[$i]} : $bases[$i];
457 $base = $complement{$base} if $canonical && $strand < 0;
458 $gd->char($font,$x+$x_fudge,$y,$base,$color);
459 }
460 }
461  
462 sub min { $_[0] <= $_[1] ? $_[0] : $_[1] }
463 sub max { $_[0] >= $_[1] ? $_[0] : $_[1] }
464  
465 sub draw_label {
466 my $self = shift;
467 my ($gd,$left,$top,$partno,$total_parts) = @_;
468  
469 my $label = $self->label or return;
470 local $self->{default_opacity} = 1;
471  
472 my $x = $self->left + $left; # valid for both "top" and "left" because the left-hand side is defined by pad_left
473 my $font = $self->labelfont;
474 if ($self->label_position eq 'top') {
475 $x += $self->pad_left; # offset to beginning of the drawn part of the feature
476 $x = $self->panel->left + 1 if $x <= $self->panel->left;
477 $self->render_label($gd,
478 $font,
479 $x,
480 $self->top + $top - 1,
481 $label);
482 } elsif ($self->label_position eq 'left') {
483 # my $y = $top + $self->{top} + ($self->height - $self->string_height($font))/2;
484 my $y = $top + $self->{top} - 1;
485 $self->render_label($gd,
486 $font,
487 $x,
488 $y,
489 $label);
490 # used for alignments, doesn't account for padding, viewer discretion is advised...
491 } elsif ($self->label_position eq 'alignment_left') {
492 my $y = $self->{top} + ($self->height - $font->height)/2 + $top;
493 $self->render_label($gd,
494 $font,
495 1,
496 $y,
497 $label);
498 }
499 }
500  
501 sub render_label {
502 my $self = shift;
503 my ($gd,$font,$x,$y,$label,$is_legend) = @_;
504 my $rlp = $self->record_label_positions;
505 unless ($rlp || $is_legend)
506 {
507 $gd->string($font,$x,$y,$label,$self->labelcolor);
508 }
509 $self->panel->add_key_box($self,$label,$x,$y)
510 if $rlp
511 }
512  
513 sub draw_description {
514 my $self = shift;
515 my ($gd,$dx,$dy,$partno,$total_parts) = @_;
516 my $label = $self->description or return;
517  
518 local $self->{default_opacity} = 1;
519 my ($left,$top,$right,$bottom) = $self->bounds($dx,$dy);
520 $bottom += $self->pad_bottom;
521 $bottom -= $self->labelheight;
522 $bottom -= $self->labelheight if $self->part_labels && $self->label_position eq 'top';
523  
524 $gd->string($self->descfont,
525 $left,
526 $bottom-3,
527 $label,
528 $self->descriptioncolor);
529 }
530  
531 sub draw_part_labels {
532 my $self = shift;
533 my ($gd,$left,$top,$partno,$total_parts) = @_;
534 return unless $self->{level} == 0;
535 my @p = $self->parts or return;
536  
537 local $self->{default_opacity} = 1;
538  
539 @p > 1 or return;
540 @p = reverse @p if $self->flip;
541  
542 my $font = $self->font;
543 my $width = $font->width;
544 my $color = $self->labelcolor;
545  
546 my $y = $top + $self->bottom - $self->pad_bottom;
547 my $merge_em = $self->part_label_merge;
548  
549 my @parts;
550 my $previous;
551  
552 if ($merge_em) {
553 my $current_contig = [];
554  
555 for my $part (@p) {
556 if (!$previous || $part->feature->start - $previous->feature->end <= 1) {
557 push @$current_contig,$part;
558 } else {
559 push @parts,$current_contig;
560 $current_contig = [$part];
561 }
562 $previous = $part;
563 }
564 push @parts,$current_contig;
565 }
566  
567 else {
568 @parts = map {[$_]} @p;
569 }
570  
571 my $last_x; # avoid overlapping labels
572 for (my $i=0; $i<@parts; $i++) {
573 my $x1 = $parts[$i][0]->left;
574 my $x2 = $parts[$i][-1]->right;
575  
576 my $string = $self->part_label($i,scalar @parts);
577 my $x = $left + $x1 + ($x2 - $x1 - $width*length($string))/2;
578 my $w = $width * length($string);
579 next if defined $last_x && $self->flip ? $x + $w > $last_x : $x < $last_x;
580 $gd->string($font,
581 $x,$y,
582 $string,
583 $color);
584 $last_x = $x + ($self->flip ? 0 : $w);
585 }
586 }
587  
588 sub part_label {
589 my $self = shift;
590 my ($part,$total) = @_;
591  
592 local $self->{partno} = $self->feature->strand < 0 ? $total - $part -1 : $part;
593 my $label = $self->option('part_labels');
594 return unless defined $label;
595 return "1" if $label eq '1 ';
596 return $label unless $label eq '1';
597 return $self->{partno}+1;
598 }
599  
600 sub dna_fits {
601 my $self = shift;
602  
603 my $pixels_per_base = $self->scale;
604 my $font = $self->mono_font;
605 my $font_width = $font->width;
606
607 return $pixels_per_base >= $font_width;
608 }
609  
610 sub protein_fits {
611 my $self = shift;
612 my $font = $self->mono_font;
613  
614 my $font_width = $font->width;
615 my $pixels_per_residue = $self->scale * 3;
616  
617 return $pixels_per_residue >= $font_width;
618 }
619  
620 sub arrowhead {
621 my $self = shift;
622 my $image = shift;
623 my ($x,$y,$height,$orientation) = @_;
624  
625 my $fg = $self->set_pen;
626 my $style = $self->option('arrowstyle') || 'regular';
627  
628 if ($style eq 'filled') {
629 my $poly_pkg = $self->polygon_package;
630 my $poly = $poly_pkg->new();
631 if ($orientation >= 0) {
632 $poly->addPt($x-$height,$y-$height);
633 $poly->addPt($x,$y);
634 $poly->addPt($x-$height,$y+$height,$y);
635 } else {
636 $poly->addPt($x+$height,$y-$height);
637 $poly->addPt($x,$y);
638 $poly->addPt($x+$height,$y+$height,$y);
639 }
640 $image->filledPolygon($poly,$fg);
641 }
642 else {
643 if ($orientation >= 0) {
644 $image->line($x,$y,$x-$height,$y-$height,$fg);
645 $image->line($x,$y,$x-$height,$y+$height,$fg);
646 } else {
647 $image->line($x,$y,$x+$height,$y-$height,$fg);
648 $image->line($x,$y,$x+$height,$y+$height,$fg);
649 }
650 }
651 }
652  
653 sub arrow {
654 my $self = shift;
655 my $image = shift;
656 my ($x1,$x2,$y) = @_;
657  
658 my $fg = $self->set_pen;
659 my $height = $self->height/4;
660 $height = 3 if $height < 3;
661  
662 $image->line($x1,$y,$x2,$y,$fg);
663 $self->arrowhead($image,$x2,$y,$height,+1) if $x1 < $x2;
664 $self->arrowhead($image,$x2,$y,$height,-1) if $x2 < $x1;
665 }
666  
667 sub reversec {
668 my $self = shift;
669 my $dna = shift;
670 $dna =~ tr/gatcGATC/ctagCTAG/;
671 $dna = reverse $dna;
672 return $dna;
673 }
674  
675 # This gets invoked if the user has requested that the protein translation
676 # gets drawn using the draw_translation option and protein_fits() returns
677 # true. It is a rather specialized function and possibly belongs somewhere else,
678 # but putting it here makes it possible for any feature to display its protein
679 # translation.
680 sub calculate_cds {
681 my $self = shift;
682  
683 return if exists $self->{cds_translation};
684  
685 my $f = $self->feature;
686  
687 my @subfeats = $self->find_subfeats_with_phase($f) or return;
688 my @parts = $self->feature_has_subparts ? $self->parts : $self;
689  
690 my @parts_with_phase = grep {defined eval {$_->feature->phase}} @parts;
691 my %parts = map {$_->feature->start => $_} @parts_with_phase;
692  
693 my $codon_table = $self->option('codontable');
694 $codon_table = 1 unless defined $codon_table;
695 require Bio::Tools::CodonTable unless Bio::Tools::CodonTable->can('new');
696 my $translate_table = Bio::Tools::CodonTable->new(-id=>$codon_table);
697  
698 my $strand = $f->strand;
699 $strand *= -1 if $self->{flip};
700  
701 my $panel_start = $self->panel->start;
702 my $panel_end = $self->panel->end;
703  
704 for (my $i=0; $i < @subfeats; $i++) {
705 my $feature = $subfeats[$i];
706 my $prior = $subfeats[$i-1] if $i>0;
707 my $next = $subfeats[$i+1] if $i<$#subfeats;
708 ($prior,$next) = ($next,$prior) if $f->strand < 0;
709  
710 my $part = $parts{$feature->start} or next;
711  
712 my $pos = $feature->strand >= 0 ? $feature->start : $feature->end;
713 my $phase = eval {$feature->phase};
714 next unless defined $phase;
715  
716 my $seq = $self->get_seq($feature);
717 next unless defined $seq;
718  
719 my ($frame,$offset) = frame_and_offset($pos,
720 $feature->strand,
721 $phase);
722 $part->{cds_frame} = $frame;
723 $part->{cds_offset} = $offset;
724  
725 # do in silico splicing in order to find the codon that
726 # arises from the splice
727 my $protein = $seq->translate(undef,undef,$phase,$codon_table)->seq;
728 $part->{cds_translation} = $protein;
729  
730 # warn "protein = $protein";
731  
732 if ($phase == 2 && $prior) {
733 # get 1 bp from end of previous
734 my $dna = $self->get_dna($feature);
735 my $prior_dna = $self->get_dna($prior);
736 my $spliced_codon = substr($prior_dna,-1,1);
737 $spliced_codon .= substr($dna,0,2);
738 $part->{cds_splice_residue_head} = $translate_table->translate($spliced_codon);
739 # warn "codon = $spliced_codon, splice_residue_head = $part->{cds_splice_residue_head}";
740 }
741  
742 if ($next && $next->phase == 1) {
743 my $dna = $self->get_dna($feature);
744 my $next_dna = $self->get_dna($next) if $next;
745 my $spliced_codon = substr($dna,-2,2);
746 $spliced_codon .= substr($next_dna,0,1);
747 $part->{cds_splice_residue_tail} = $translate_table->translate($spliced_codon);
748 # warn "codon = $spliced_codon, splice_residue_tail = $part->{cds_splice_residue_tail}";
749 }
750 }
751 return;
752 }
753  
754 sub find_subfeats_with_phase {
755 my $self = shift;
756 my $feat = shift;
757 return $feat if $feat->can('phase') && defined $feat->phase;
758 return grep {$_->can('phase') && defined $_->phase} $feat->get_SeqFeatures;
759 }
760  
761 # hack around changed feature API
762 sub get_seq {
763 my $self = shift;
764 my $feature = shift;
765 my $dna = $self->get_dna($feature);
766 return Bio::PrimarySeq->new(-seq=>$dna);
767 }
768  
769 sub get_dna {
770 my $self = shift;
771 # could be a PrimarySeq, or some kind of feature
772 my $thing = shift or return;
773 my $key = join ':',
774 map { eval{ $thing->$_->() } || '' }
775 qw( seq_id start end strand );
776  
777 my $panel = $self->panel;
778 if (exists $panel->{_seqcache}{$key}) {
779 return $panel->{_seqcache}{$key};
780 } else {
781 my $obj = $thing->seq;
782 $obj = $obj->seq if ref $obj;
783 return $panel->{_seqcache}{$key} = $obj;
784 }
785 }
786  
787 1;
788  
789 =head1 NAME
790  
791 Bio::Graphics::Glyph::generic - The "generic" glyph
792  
793 =head1 SYNOPSIS
794  
795 See L<Bio::Graphics::Panel> and L<Bio::Graphics::Glyph>.
796  
797 =head1 DESCRIPTION
798  
799 This is identical to the "box" glyph except that it will draw the
800 subparts of features that contain subfeatures. The subparts are not
801 connected -- use the "segments" glyph for that. "Generic" is the
802 default glyph used when not otherwise specified.
803  
804 =head2 METHODS
805  
806 This module overrides the maxdepth() method to return 0 unless the
807 -maxdepth option is provided explicitly. This means that any module
808 that inherits from generic will need to override maxdepth() again in
809 order to draw subfeatures. In general, those implementing
810 multi-segmented feature glyphs should inherit from
811 Bio::Graphics::Glyph::segments, which allows for one level of descent.
812  
813 In addition, the following new methods are implemented:
814  
815 =over 4
816  
817 =item labelfont(), descfont(), labelwidth(), descriptionwidth()
818  
819 Return the font, width for the label or description.
820  
821 =item label()
822  
823 Return the glyph label text (printed above the glyph).
824  
825 =item description()
826  
827 Return the glyph description text (printed below the glyph).
828  
829 =item draw_translation()
830  
831 Draw the protein translation of the feature (assumes that the feature is attached to a DNA sequence).
832  
833 =item draw_sequence()
834  
835 Draw the sequence of the feature (either DNA or protein).
836  
837 =back
838  
839 =head2 OPTIONS
840  
841 The following options are standard among all Glyphs. See
842 L<Bio::Graphics::Glyph> for a full explanation.
843  
844 Option Description Default
845 ------ ----------- -------
846  
847 -fgcolor Foreground color black
848  
849 -bgcolor Background color turquoise
850  
851 -fillcolor Synonym for -bgcolor
852  
853 -linewidth Line width 1
854  
855 -height Height of glyph 10
856  
857 -font Default font gdSmallFont
858  
859 -label_font Font used for label gdSmallFont
860  
861 -desc_font Font used for description gdSmallFont
862  
863 -connector Connector type 0 (false)
864  
865 -connector_color
866 Connector color black
867  
868 -pad_top Top padding 0
869  
870 -pad_bottom Bottom padding 0
871  
872 -label Whether to draw a label 0 (false)
873  
874 -label_position Where to draw the label "top" (default) or "left"
875  
876 -description Whether to draw a description 0 (false)
877  
878 -strand_arrow Whether to indicate 0 (false)
879 strandedness
880  
881 -hilite Highlight color undef (no color)
882  
883 -draw_dna If true, draw the dna residues 0 (false)
884 when magnification level
885 allows.
886  
887 -canonical_strand If true, draw the dna residues 0 (false)
888 as they appear on the plus strand
889 even if the feature is on the minus
890 strand.
891  
892 -pad_top and -pad_bottom allow you to insert some blank space between
893 the glyph's boundary and its contents. This is useful if you are
894 changing the glyph's height dynamically based on its feature's score.
895  
896 =head1 BUGS
897  
898 Please report them.
899  
900 =head1 SEE ALSO
901  
902 L<Bio::Graphics::Panel>,
903 L<Bio::Graphics::Glyph>,
904 L<Bio::Graphics::Glyph::arrow>,
905 L<Bio::Graphics::Glyph::cds>,
906 L<Bio::Graphics::Glyph::crossbox>,
907 L<Bio::Graphics::Glyph::diamond>,
908 L<Bio::Graphics::Glyph::dna>,
909 L<Bio::Graphics::Glyph::dot>,
910 L<Bio::Graphics::Glyph::ellipse>,
911 L<Bio::Graphics::Glyph::extending_arrow>,
912 L<Bio::Graphics::Glyph::generic>,
913 L<Bio::Graphics::Glyph::graded_segments>,
914 L<Bio::Graphics::Glyph::heterogeneous_segments>,
915 L<Bio::Graphics::Glyph::line>,
916 L<Bio::Graphics::Glyph::pinsertion>,
917 L<Bio::Graphics::Glyph::primers>,
918 L<Bio::Graphics::Glyph::rndrect>,
919 L<Bio::Graphics::Glyph::segments>,
920 L<Bio::Graphics::Glyph::ruler_arrow>,
921 L<Bio::Graphics::Glyph::toomany>,
922 L<Bio::Graphics::Glyph::transcript>,
923 L<Bio::Graphics::Glyph::transcript2>,
924 L<Bio::Graphics::Glyph::translation>,
925 L<Bio::Graphics::Glyph::triangle>,
926 L<Bio::Graphics::Glyph::xyplot>,
927 L<Bio::DB::GFF>,
928 L<Bio::SeqI>,
929 L<Bio::SeqFeatureI>,
930 L<Bio::Das>,
931 L<GD>
932  
933 =head1 AUTHOR
934  
935 Allen Day E<lt>day@cshl.orgE<gt>,
936 Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>
937  
938 Copyright (c) 2001 Cold Spring Harbor Laboratory
939 Copyright (c) 2010 Ontario Institute for Cancer Research
940  
941 This library is free software; you can redistribute it and/or modify
942 it under the same terms as Perl itself. See DISCLAIMER.txt for
943 disclaimers of warranty.
944  
945 =cut