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

filter: Statementstmt 0.0% 0 / 395 Branchbran 0.0% 0 / 206 Conditioncond 0.0% 0 / 90 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 42 Podpod 0.0% 0 / 35 totaltotal 0.0% 0 / 768 CC 104 SCAR 39.4
CC
104
Cov
0%
draw_decorations
240.0
_map_decorations
110.0
_calc_add_padding
56.0
CRAP 51.6
1 package Bio::Graphics::Glyph::decorated_transcript;
2  
3 use strict;
4 use warnings;
5  
6 use Bio::Graphics::Panel;
7 use List::Util qw[min max];
8  
9 use constant DECORATION_TAG_NAME => 'protein_decorations';
10 use constant DEBUG => 0;
11  
12 my @color_names = Bio::Graphics::Panel::color_names;
13  
14 use base
15 qw(Bio::Graphics::Glyph::processed_transcript Bio::Graphics::Glyph::segments);
16  
17 sub my_descripton {
18 return <<END;
19 This glyph extends the functionality of the Bio::Graphics::Glyph::processed_transcript glyph
20 and allows to draw protein decorations (e.g., signal peptides, transmembrane domains, protein domains)
21 on top of gene models. Currently, the glyph can draw decorations in form of colored or outlined boxes
22 inside or around CDS segments. Protein decorations are specified at the 'mRNA' transcript level
23 in protein coordinates. Protein coordinates are automatically mapped to nucleotide coordinates by the glyph.
24 Decorations are allowed to span exon-exon junctions, in which case decorations are split between exons.
25 By default, the glyph automatically assigns different colors to different types of protein decorations, whereas
26 decorations of the same type are always assigned the same color.
27  
28 Protein decorations are provided either with mRNA features inside GFF files (see example below) or
29 dynamically via callback function using the B<additional_decorations> option (see glyph options).
30 The following line is an example of an mRNA feature in a GFF file that contains two protein decorations,
31 one signal peptide predicted by SignalP and one transmembrane domain predicted by TMHMM:
32  
33 chr1 my_source mRNA 74796 75599 . + . ID=rna_gene-1;protein_decorations=SignalP40:SP:1:23:0:my_comment,TMHMM:TM:187:209:0
34  
35 Each protein decoration consists of six fields separated by a colon:
36  
37 1) Type. For example used to specify decoration source (e.g. 'SignalP40')
38 2) Name. Decoration name. Used as decoration label by default (e.g. 'SP' for signal peptide)
39 3) Start. Start coordinate at the protein-level (1-based coordinate)
40 4) End. End coordinate at the protein-level
41 5) Score. Optional. Score associated with a decoration (e.g. Pfam E-value). This score can be used
42 to dynamically filter or color decorations via callbacks (see glyph options).
43 6) Description. Optional. User-defined description of decoration. The glyph ignores this description,
44 but it will be made available to callback functions for inspection. Special characters
45 like ':' or ',' that might interfere with the GFF tag parser should be avoided.
46  
47 If callback functions are used as glyph parameters (see below), the callback is called for each
48 decoration separately. That is, the callback can be called multiple times for the same CDS feature,
49 but each time with a different decoration. The currently drawn (active) decoration is made available
50 to the callback via the glyph method 'active_decoration'. The active decoration is returned in form
51 of a Bio::Graphics::Feature object, with decoration data fields mapped to corresponding feature
52 attributes in the following way:
53  
54 type --> \$glyph->active_decoration->type
55 name --> \$glyph->active_decoration->name
56 nucleotide start coordinate --> \$glyph->active_decoration->start
57 nucleotide end coordinate --> \$glyph->active_decoration->end
58 protein start coordinate --> \$glyph->active_decoration->get_tag_values('p_start')
59 protein end coordinate --> \$glyph->active_decoration->get_tag_values('p_end')
60 score --> \$glyph->active_decoration->score
61 description --> \$glyph->active_decoration->description
62  
63 In addition, the glyph passed to the callback allows access to the parent glyph and
64 parent feature if required (use \$glyph->parent or \$glyph->parent->feature).
65  
66 END
67 }
68  
69 sub my_options {
70 return {
71 decoration_visible => [
72 'boolean',
73 'false',
74 'Specifies whether decorations should be visible or not. For selective display of individual',
75 'decorations, specify a callback function and return 1 or 0 after inspecting the active',
76 'decoration of the glyph. '],
77 decoration_color => [
78 'color',
79 undef,
80 'Decoration background color. If no color is specified, colors are assigned automatically',
81 'by decoration type and name, whereas decorations of identical type and name are assigned',
82 'the same color. A special color \'transparent\' can be used here in combination with',
83 'the option \'decoration_border\' to draw decorations as outlines.'],
84 decoration_border => [
85 ['none', 'solid', 'dashed'],
86 'none',
87 'Decoration border style. By default, decorations are drawn without border (\'none\' or',
88 '0). Other valid options here include \'solid\' or \'dashed\'.'],
89 decoration_border_color => [
90 'color',
91 'black',
92 'Color of decoration boder.'],
93 decoration_label => [
94 'string',
95 undef,
96 'Decoration label. If not specified, the second data field of the decoration is used',
97 'as label. Set this option to 0 to get unlabeled decorations. If the label text',
98 'extends beyond the size of the decorated segment, the label will be clipped. Clipping',
99 'does not occur for SVG output.'],
100 decoration_label_position => [
101 ['inside', 'above', 'below'],
102 undef,
103 'Position of decoration label. Labels can be drawn \'inside\' decorations (default)',
104 'or \'above\' and \'below\' decorations.'],
105 decoration_label_color => [
106 'color',
107 'undef',
108 'Decoration label color. If not specified, this color is complementary to',
109 'decoration_color (e.g., yellow text on blue background, white on black, etc.). If the',
110 'decoration background color is transparent and no decoration label color is specified,',
111 'the foreground color of the underlying transcript glyph is used as default.'],
112 additional_decorations => [
113 'string',
114 undef,
115 'Additional decorations to those specified in the GFF file. Expected is a string',
116 'in the same format as described above for GFF files. This parameter is intended',
117 'to be used as callback function, which inspects the currently processed transcript',
118 'feature (first parameter to callback) and returns additional protein decorations',
119 'that should be drawn.'],
120 decoration_height => [
121 'integer',
122 undef,
123 'Decoration height. Unless specified otherwise, the height of the decoration is the',
124 'height of the underlying transcript glyph minus 2, such that the decoration is drawn',
125 'within transcript boundaries.'],
126 decoration_position => [
127 ['inside'],
128 'inside',
129 'Currently, decorations can only be drawn inside CDS segments.'],
130 flip_minus => [
131 'boolean',
132 0,
133 'If set to 1, features on the negative strand will be drawn flipped.',
134 'This is not particularly useful in GBrowse, but becomes handy if multiple features',
135 'should be drawn within the same panel, left-aligned, and on top of each other,',
136 'for example to allow easy gene structure comparisons.'],
137 }
138 }
139  
140 sub new {
141 my ( $class, @args ) = @_;
142 my %param = @args;
143  
144 warn "new(): " . join( ",", @args ) . "\n" if (DEBUG == 2);
145  
146 my $feature = $param{'-feature'};
147 my $factory = $param{'-factory'};
148  
149 my $flip_minus = $factory->get_option('flip_minus');
150 if ( $flip_minus and $feature->strand < 1 ) {
151 for ( my $i = 0 ; $i < @args ; $i++ ) {
152 $args[ $i + 1 ] = 1 if ( $args[$i] eq '-flip' );
153 }
154 }
155  
156 my $self = $class->Bio::Graphics::Glyph::processed_transcript::new(@args);
157  
158 $self->{'parent'} = undef;
159 $self->{'additional_decorations'} = undef;
160 $self->{'active_decoration'} = undef;
161 $self->{'add_pad_bottom'} = undef;
162  
163 # give sub-glyphs access to parent glyph's decorations
164 if ($self->decorations_visible)
165 {
166 foreach my $sub_glyph ( $self->parts ) {
167 $sub_glyph->{'parent'} = $self;
168 }
169 }
170  
171 bless( $self, $class );
172  
173 return $self;
174 }
175  
176 sub finished {
177 my $self = shift;
178  
179 warn "finished(): ".$self->feature->primary_tag." ".$self->feature."\n" if (DEBUG == 2);
180  
181 foreach my $sub_glyph ( $self->parts ) {
182 $sub_glyph->{'parent'} = undef;
183 }
184  
185 $self->Bio::Graphics::Glyph::processed_transcript::finished(@_);
186 }
187  
188  
189 sub parent {
190 my $self = shift;
191 return $self->{'parent'};
192 }
193  
194 sub decorations {
195 my $self = shift;
196 my $feature = $self->feature;
197  
198 return $self->{'parent'}->decorations(@_)
199 if ($self->{'parent'} and $feature->primary_tag ne "mRNA");
200  
201 return $feature->get_tag_values(DECORATION_TAG_NAME);
202 }
203  
204 # allows to retrieve additional decorations via callback
205 sub additional_decorations {
206 my $self = shift;
207 my $feature = $self->feature;
208  
209 return $self->{'parent'}->additional_decorations(@_)
210 if ($self->{'parent'} and $feature->primary_tag ne "mRNA");
211  
212 return $self->{'additional_decorations'}
213 if (defined $self->{'additional_decorations'});
214
215 my @additional_decorations;
216 my $additional_decorations_str = $self->option('additional_decorations');
217 if ($additional_decorations_str)
218 {
219 push(@additional_decorations, split(",", $additional_decorations_str));
220 }
221
222 $self->{'additional_decorations'} = \@additional_decorations;
223
224 return \@additional_decorations;
225 }
226  
227 sub all_decorations {
228 my $self = shift;
229  
230 my @all_decorations;
231
232 # no decorations at gene level
233 return \@all_decorations
234 if ($self->feature->primary_tag eq "gene");
235
236 # forward request to mRNA parent glyph;
237 # allows CDS child glyphs to retrieve decoration information from mRNA
238 return $self->{'parent'}->all_decorations(@_)
239 if ($self->{'parent'} and $self->feature->primary_tag ne "mRNA");
240  
241 # decoration data specified as feature tag
242 @all_decorations = $self->decorations;
243
244 # add additional decorations provided via callback
245 push(@all_decorations, @{$self->additional_decorations});
246  
247 return \@all_decorations;
248 }
249  
250 # returns stack offset of decoration (only used if decoration is drawn stacked)
251 sub stack_offset_bottom {
252 my $self = shift;
253  
254 return $self->{'parent'}->stack_offset_bottom(@_)
255 if ($self->{'parent'} and $self->feature->primary_tag ne "mRNA");
256  
257 my $decoration = shift;
258 return $self->{'stack_offset_bottom'}{$decoration}
259 }
260  
261 sub active_decoration {
262 my $self = shift;
263 return $self->{'active_decoration'};
264 }
265  
266 # get all decorations, including mapped nucleotide coordinates
267 sub mapped_decorations {
268 my $self = shift;
269 my $feature = $self->feature;
270  
271 return $self->{'parent'}->mapped_decorations(@_)
272 if ($self->{'parent'} and $feature->primary_tag ne "mRNA");
273  
274 $self->_map_decorations()
275 if (!defined $self->{'mapped_decorations'});
276
277 return $self->{'mapped_decorations'};
278 }
279  
280 # get all mapped decorations, as sorted by user call-back (if provided)
281 # by default, decorations are sorted by length, causing shorter decorations
282 # to be drawn on top of longer decorations
283 # TODO: document this new feature
284 sub sorted_decorations {
285 my $self = shift;
286  
287 # forward request to mRNA parent glyph;
288 # allows CDS child glyphs to retrieve decoration information from mRNA
289 return $self->{'parent'}->sorted_decorations(@_)
290 if ($self->{'parent'} and $self->feature->primary_tag ne "mRNA");
291  
292 # cache for faster access
293 return $self->{'sorted_decorations'}
294 if (defined $self->{'sorted_decorations'});
295
296 # get sorted decorations from callback
297 my $sorted_decorations = $self->option('sorted_decorations');
298
299 # if no callback or bad return value, sort by length by default (causes longer
300 # decorations to be drawn first)
301 if (!$sorted_decorations or ref($sorted_decorations) ne 'ARRAY')
302 {
303 my @sorted = reverse sort { $a->length <=> $b->length } (@{$self->mapped_decorations});
304 $sorted_decorations = \@sorted;
305  
306 if (DEBUG)
307 {
308 print STDERR "sorted decorations: ";
309 foreach my $sd (@$sorted_decorations) { print STDERR $sd->name."(".$sd->length.") "; }
310 print STDERR "\n";
311 }
312 }
313  
314 $self->{'sorted_decorations'} = $sorted_decorations;
315
316 return $sorted_decorations;
317 }
318  
319 sub _map_decorations {
320 my $self = shift;
321 my $feature = $self->feature;
322
323 $self->_map_coordinates();
324  
325 my @mapped_decorations;
326 foreach my $h ( @{$self->all_decorations} ) {
327 my ( $type, $name, $p_start, $p_end, $score, $desc ) = split( ":", $h );
328  
329 if (!defined $p_end)
330 {
331 warn "_map_decorations(): WARNING: invalid decoration data for feature $feature: '$h'\n";
332 next;
333 }
334  
335 my $nt_start = $self->_map_codon_start($p_start);
336 if (!$nt_start)
337 {
338 warn "DECORATION=$h\n";
339 warn "_map_decorations(): WARNING: could not map decoration start coordinate on feature $feature(".$feature->primary_tag.")\n";
340 next;
341 }
342 my $nt_end = $self->_map_codon_end($p_end);
343 if (!$nt_end)
344 {
345 warn "DECORATION=$h\n";
346 warn "_map_decorations(): WARNING: could not map decoration end coordinate on feature $feature(".$feature->primary_tag.")\n";
347 next;
348 }
349  
350 ( $nt_start, $nt_end ) = ( $nt_end, $nt_start )
351 if ( $nt_start > $nt_end );
352  
353 my $f = Bio::Graphics::Feature->new
354 (
355 -type => $type,
356 -name => $name,
357 -start => $nt_start,
358 -end => $nt_end,
359 -score => $score,
360 -desc => $desc,
361 -seq_id => $feature->seq_id,
362 -strand => $feature->strand,
363 -attributes => { # remember protein coordinates for callbacks
364 'p_start' => $p_start,
365 'p_end' => $p_end
366 }
367 );
368  
369 # my $mapped_decoration = "$h:$nt_start:$nt_end";
370 push( @mapped_decorations, $f );
371
372 # init stack offset for stacked decorations
373 if ($self->decoration_position($f) eq 'stacked_bottom')
374 {
375 if (!defined $self->{'stack_offset_bottom'}{$f})
376 {
377 $self->{'cur_stack_offset_bottom'} = 2
378 if (!defined $self->{'cur_stack_offset_bottom'});
379
380 $self->{'stack_offset_bottom'}{$f} = $self->{'cur_stack_offset_bottom'};
381 $self->{'cur_stack_offset_bottom'} += $self->decoration_height($f);
382  
383 warn "$self: stack offset ".$f->name."($f): ".$self->{'stack_offset_bottom'}{$f}."\n"
384 if (DEBUG);
385 }
386 }
387
388 warn "DECORATION=$h --> $nt_start:$nt_end\n" if (DEBUG);
389 }
390  
391 $self->{'mapped_decorations'} = \@mapped_decorations;
392 }
393  
394 sub _map_codon_start {
395 my $self = shift;
396 my $protein_coordinate = shift;
397
398 $self->throw('protein coordinate not specified: ')
399 if (!$protein_coordinate and DEBUG);
400  
401 return $self->{'p2n'}->{$protein_coordinate}->{'codon_start'};
402 }
403  
404 sub _map_codon_end {
405 my $self = shift;
406 my $protein_coordinate = shift;
407
408 $self->throw('protein coordinate not specified')
409 if (!$protein_coordinate and DEBUG);
410  
411 return $self->{'p2n'}->{$protein_coordinate}->{'codon_end'};
412 }
413  
414 # map protein to nucleotide coordinate
415 sub _map_coordinates {
416 my $self = shift;
417  
418 # sort all CDS features by coordinates
419 # NOTE: filtering for CDS features by passing feature type to get_SeqFeatures()
420 # does not work for some reason, probably when no feature store attached
421 my @cds =
422 grep { $_->primary_tag eq 'CDS' } $self->feature->get_SeqFeatures();
423 if ( $self->feature->strand > 0 ) {
424 my ( $ppos, $residue ) = ( 1, 0 );
425 my @sorted_cds = sort { $a->start <=> $b->start } (@cds);
426 foreach my $c (@sorted_cds) {
427 $self->{'p2n'}{ $ppos - 1 }{'codon_end'} = $c->start + $residue - 1
428 if ($residue);
429 for (
430 my $ntpos = $c->start + $residue ;
431 $ntpos <= $c->end ;
432 $ntpos += 3
433 )
434 {
435 $self->{'p2n'}{$ppos}{'codon_start'} = $ntpos;
436 $self->{'p2n'}{$ppos}{'codon_end'} = $ntpos + 2;
437 $ppos++;
438 $residue = $ntpos + 2 - $c->end;
439 }
440 }
441 }
442 else {
443 my ( $ppos, $residue ) = ( 1, 0 );
444 my @sorted_cds = reverse sort { $a->start <=> $b->start } (@cds);
445 foreach my $c (@sorted_cds) {
446 $self->{'p2n'}{ $ppos - 1 }{'codon_end'} = $c->end - $residue + 1
447 if ($residue);
448 for (
449 my $ntpos = $c->end - $residue ;
450 $ntpos >= $c->start ;
451 $ntpos -= 3
452 )
453 {
454 $self->{'p2n'}{$ppos}{'codon_start'} = $ntpos;
455 $self->{'p2n'}{$ppos}{'codon_end'} = $ntpos - 2;
456 $ppos++;
457 $residue = $c->start - ( $ntpos - 2 );
458 }
459 }
460 }
461 }
462  
463 sub decoration_top {
464 my $self = shift;
465 my $decoration = shift;
466
467 if (!$decoration)
468 {
469 $self->throw("decoration not specified") if (DEBUG);
470 return $self->top;
471 }
472  
473 my $decoration_height = $self->decoration_height($decoration);
474 my $decoration_position = $self->decoration_position($decoration);
475
476 if ($decoration_position eq 'stacked_bottom')
477 {
478 $self->throw("$self: stack offset unknown for decoration ".$decoration->name."($decoration)")
479 if (!defined $self->stack_offset_bottom($decoration) and DEBUG);
480
481 return $self->bottom + $self->stack_offset_bottom($decoration);
482 }
483 else
484 {
485 $self->throw("invalid decoration_position: $decoration_position")
486 if (($decoration_position ne 'inside') and DEBUG);
487
488 return int(($self->bottom-$self->pad_bottom+$self->top+$self->pad_top)/2 - $decoration_height/2 + 0.5);
489 }
490 }
491  
492 sub decoration_bottom {
493 my $self = shift;
494 my $decoration = shift;
495
496 if (!$decoration)
497 {
498 $self->throw("decoration not specified") if (DEBUG);
499 return $self->bottom;
500 }
501  
502 return $self->decoration_top($decoration) + $self->decoration_height($decoration) - 1;
503 }
504  
505 sub _calc_add_padding
506 {
507 my $self = shift;
508
509 my $height = $self->height;
510 my ($add_pad_bottom, $add_pad_top) = (0, 0);
511  
512 foreach my $decoration (@{$self->mapped_decorations})
513 {
514 my $h_height = $self->decoration_height($decoration);
515  
516 my ($label_pad_top, $label_pad_bottom) = (0, 0);
517 if ($self->decoration_label($decoration))
518 {
519 $label_pad_top = $self->labelfont->height
520 if ($self->decoration_label_position($decoration) eq "above");
521 $label_pad_bottom = $self->labelfont->height
522 if ($self->decoration_label_position($decoration) eq "below");
523 }
524
525 if (($h_height - $height)/2 + $label_pad_top > $add_pad_top)
526 {
527 $add_pad_top = ($h_height - $height)/2 + $label_pad_top;
528 }
529 if (($h_height - $height)/2 + $label_pad_bottom > $add_pad_bottom)
530 {
531 $add_pad_bottom = ($h_height - $height)/2 + $label_pad_bottom;
532 }
533 if ($self->{'stack_offset_bottom'}{$decoration} and $self->{'stack_offset_bottom'}{$decoration}+$h_height > $add_pad_bottom)
534 {
535 $add_pad_bottom = $self->{'stack_offset_bottom'}{$decoration}+$h_height;
536 }
537 }
538
539 $self->{'add_pad_bottom'} = $add_pad_bottom;
540 $self->{'add_pad_top'} = $add_pad_top;
541 }
542  
543 # add extra padding if decoration exceeds transcript boundaries and if labeled outside
544 sub pad_bottom {
545 my $self = shift;
546  
547 my $bottom = $self->option('pad_bottom');
548 return $bottom if defined $bottom;
549
550 $self->_calc_add_padding()
551 if (!defined $self->{'add_pad_bottom'});
552  
553 my $pad = $self->Bio::Graphics::Glyph::processed_transcript::pad_bottom;
554
555 return $pad if ($self->{'add_pad_bottom'} < 0);
556 return $pad + $self->{'add_pad_bottom'};
557 }
558  
559 sub pad_top {
560 my $self = shift;
561
562 my $top = $self->option('pad_top');
563 return $top if defined $top;
564  
565 $self->_calc_add_padding()
566 if (!defined $self->{'add_pad_top'});
567  
568 my $pad = $self->Bio::Graphics::Glyph::processed_transcript::pad_top;
569
570 return $pad if ($self->{'add_pad_top'} < 0);
571 return $pad + $self->{'add_pad_top'};
572 }
573  
574 sub decoration_height {
575 my $self = shift;
576 my $decoration = shift;
577  
578 if (!$decoration)
579 {
580 $self->throw("decoration not specified") if (DEBUG);
581 return $self->height;
582 }
583  
584 $self->{'active_decoration'} = $decoration; # set active decoration for callback
585 my $decoration_height = $self->option('decoration_height');
586  
587 $decoration_height = $self->height-2
588 if ( !$decoration_height );
589  
590 return $decoration_height;
591 }
592  
593 sub decoration_position {
594 my $self = shift;
595 my $decoration = shift;
596  
597 if (!$decoration)
598 {
599 $self->throw("decoration not specified") if (DEBUG);
600 return "inside";
601 }
602  
603 $self->{'active_decoration'} = $decoration; # set active decoration for callback
604 my $decoration_position = $self->option('decoration_position');
605  
606 $decoration_position = 'inside'
607 if ( !$decoration_position );
608  
609 if ($decoration_position ne 'inside' and $decoration_position ne 'stacked_bottom')
610 {
611 $self->throw('invalid decoration_position: '.$decoration_position) if (DEBUG);
612 $decoration_position = 'inside';
613 }
614
615 return $decoration_position;
616 }
617  
618 sub _hash {
619 my $hash = 0;
620 foreach ( split //, shift ) {
621 $hash = $hash * 33 + ord($_);
622 }
623 return $hash;
624 }
625  
626 sub decoration_label_color {
627 my $self = shift;
628 my $decoration = shift;
629
630 if (!$decoration)
631 {
632 $self->throw("decoration not specified") if (DEBUG);
633 return "black";
634 }
635  
636 $self->{'active_decoration'} = $decoration; # set active decoration for callback
637 my $decoration_label_color = $self->option('decoration_label_color');
638  
639 return $decoration_label_color
640 if ( defined $decoration_label_color
641 and $decoration_label_color ne 'auto'
642 and $decoration_label_color ne '' );
643
644 my $decoration_color = $self->decoration_color($decoration);
645  
646 return $self->fgcolor
647 if ((!$decoration_label_color or $decoration_label_color eq 'auto')
648 and $decoration_color eq "transparent");
649  
650 # assign color complementary to decoration color
651 my ( $red, $green, $blue ) =
652 Bio::Graphics::Panel->color_name_to_rgb($decoration_color);
653  
654 $decoration_label_color =
655 sprintf( "#%02X%02X%02X", 255 - $red, 255 - $green, 255 - $blue ); # background complement
656  
657 return $decoration_label_color;
658 }
659  
660 sub decoration_label {
661 my $self = shift;
662 my $decoration = shift;
663
664 if (!$decoration)
665 {
666 $self->throw("decoration not specified") if (DEBUG);
667 return "";
668 }
669  
670 $self->{'active_decoration'} = $decoration; # set active decoration for callback
671 my $decoration_label = $self->option('decoration_label');
672
673 return undef
674 if ( defined $decoration_label and $decoration_label eq "0" );
675  
676 return $decoration_label
677 if ( $decoration_label and $decoration_label ne "1");
678
679 # assign decoration name as default label
680 return $decoration->name;
681 }
682  
683 sub decoration_label_position {
684 my $self = shift;
685 my $decoration = shift;
686
687 if (!$decoration)
688 {
689 $self->throw("decoration not specified") if (DEBUG);
690 return "";
691 }
692  
693 $self->{'active_decoration'} = $decoration; # set active decoration for callback
694 my $decoration_label_position = $self->option('decoration_label_position');
695
696 return "inside"
697 if (!$decoration_label_position);
698
699 return $decoration_label_position;
700 }
701  
702 sub decoration_border {
703 my $self = shift;
704 my $decoration = shift;
705
706 if (!$decoration)
707 {
708 $self->throw("decoration not specified") if (DEBUG);
709 return "";
710 }
711  
712 $self->{'active_decoration'} = $decoration; # set active decoration for callback
713 return $self->option('decoration_border');
714 }
715  
716 sub decoration_color {
717 my $self = shift;
718 my $decoration = shift;
719
720 if (!$decoration)
721 {
722 $self->throw("decoration not specified") if (DEBUG);
723 return "white";
724 }
725  
726 $self->{'active_decoration'} = $decoration; # set active decoration for callback
727 my $decoration_color = $self->option('decoration_color');
728  
729 return $decoration_color
730 if ( defined $decoration_color
731 and $decoration_color ne 'auto'
732 and $decoration_color ne '' );
733  
734 # automatically assign color by hashing feature name to color index
735 my $col_idx = _hash($decoration->type.":".$decoration->name) % scalar(@color_names);
736
737 # decoration background should be different from CDS background
738 while ( $self->factory->translate_color($color_names[$col_idx]) eq $self->bgcolor )
739 {
740 $col_idx = ($col_idx + 1) % scalar(@color_names);
741 }
742  
743 return $color_names[$col_idx];
744 }
745  
746 sub decoration_border_color {
747 my $self = shift;
748 my $decoration = shift;
749
750 if (!$decoration)
751 {
752 $self->throw("decoration not specified") if (DEBUG);
753 return "black";
754 }
755  
756 $self->{'active_decoration'} = $decoration; # set active decoration for callback
757 my $decoration_border_color = $self->option('decoration_border_color');
758  
759 return "black" if (!$decoration_border_color);
760
761 return $decoration_border_color;
762 }
763  
764 sub decorations_visible {
765 my $self = shift;
766  
767 return $self->code_option('decoration_visible');
768 }
769  
770 sub decoration_visible {
771 my $self = shift;
772 my $decoration = shift;
773
774 if (!$decoration)
775 {
776 $self->throw("decoration not specified") if (DEBUG);
777 return 1;
778 }
779  
780 $self->{'active_decoration'} = $decoration; # set active decoration for callback
781 my $decoration_visible = $self->option('decoration_visible');
782  
783 return $decoration_visible
784 if ( defined $decoration_visible and $decoration_visible ne "" );
785  
786 return 1;
787 }
788  
789 #sub draw {
790 # my $self = shift;
791 #
792 # warn "draw(): level " . $self->level . " " . $self->feature . "\n"
793 # if (DEBUG);
794 #
795 # $self->Bio::Graphics::Glyph::processed_transcript::draw(@_);
796 #
797 #}
798  
799 sub draw_component {
800 my $self = shift;
801  
802 warn "draw_component(): " . ref($self) . " " . $self->feature . "\n" if (DEBUG == 2);
803  
804 # draw regular glyph first
805 if ( $self->feature->source eq 'legend' ) {
806 # hack, but processed_transcript cannot be drawn without arrow...
807 $self->Bio::Graphics::Glyph::segments::draw_component(@_);
808 }
809 else {
810 $self->Bio::Graphics::Glyph::processed_transcript::draw_component(@_);
811 }
812  
813 # draw decorations if parent information available
814 if ( $self->{'parent'} and $self->feature->primary_tag eq "CDS") {
815 return $self->draw_decorations(@_);
816 }
817 }
818  
819 sub draw_decorations {
820 my $self = shift;
821 my ( $gd, $dx, $dy ) = @_;
822  
823 warn "draw_decorations(): " . $self->feature . "\n" if (DEBUG == 2);
824  
825 my ( $left, $top, $right, $bottom ) = $self->bounds( $dx, $dy );
826  
827 warn " bounds: left:$left,top:$top,right:$right,bottom:$bottom\n"
828 if (DEBUG == 2);
829  
830 foreach my $mh (@{$self->sorted_decorations}) {
831
832 # skip invisible decorations
833 next if ( !$self->decoration_visible($mh) );
834  
835 # determine overlapping segments between protein decorations and feature components
836 my $overlap_start_nt = max( $self->feature->start, $mh->start );
837 my $overlap_end_nt = min( $self->feature->end, $mh->end );
838 if ( $overlap_start_nt <= $overlap_end_nt ) {
839  
840 # manual override; forces flip to be drawn flipped
841 $self->factory->panel->flip( $self->flip )
842 if ( $self->option('flip_minus') );
843
844 my ( $h_left, $h_right ) =
845 $self->map_no_trunc( $overlap_start_nt, $overlap_end_nt + 1 );
846 ( $h_left, $h_right ) = ( $h_right, $h_left )
847 if ( $h_left > $h_right );
848 # my ($h_top, $h_bottom) = ($dy + $self->top + $self->pad_top, $dy + $self->bottom - $self->pad_bottom);
849 my $h_top = $dy + $self->decoration_top($mh);
850 my $h_bottom = $dy + $self->decoration_bottom($mh);
851  
852 my $color = $self->decoration_color($mh);
853  
854 # don't draw over borders; not supported by SVG
855 $gd->clip( $left + 1, $h_top, $right - 1, $h_bottom )
856 if ( !$gd->isa("GD::SVG::Image") );
857  
858 if ($color ne 'transparent')
859 {
860 warn "filledRectangle: left=$h_left,top=$h_top,right=$h_right,bottom=$h_bottom\n"
861 if (DEBUG == 2);
862 $gd->filledRectangle( $h_left, $h_top, $h_right, $h_bottom,
863 $self->factory->translate_color($color) );
864 }
865  
866 if ($self->decoration_border($mh))
867 {
868 my ($b_left, $b_top, $b_right, $b_bottom) = ($h_left, $h_top, $h_right, $h_bottom);
869 my $border_color = $self->factory->translate_color($self->decoration_border_color($mh));
870  
871 warn "border rectangle: left=$b_left,top=$b_top,right=$b_right,bottom=$b_bottom\n"
872 if (DEBUG == 2);
873  
874 if ($self->decoration_border($mh) eq "dashed")
875 {
876 my $image_class = $self->panel->image_class;
877 my $gdTransparent = $image_class->gdTransparent;
878 my $gdStyled = $image_class->gdStyled;
879 $gd->setStyle($border_color,$border_color,$border_color,$gdTransparent,$gdTransparent);
880 $gd->rectangle( $b_left, $b_top, $b_right, $b_bottom, $gdStyled );
881 }
882 else
883 {
884 $gd->rectangle( $b_left, $b_top, $b_right, $b_bottom, $border_color );
885 }
886
887 }
888
889 $gd->clip( 0, 0, $gd->width, $gd->height )
890 if ( !$gd->isa("GD::SVG::Image") );
891  
892 # draw label on first overlapping component
893 my $h_label = $self->decoration_label($mh);
894 if ( $h_label
895 and (
896 ( $self->feature->strand > 0
897 and $mh->start >= $self->feature->start )
898 or ( $self->feature->strand <= 0
899 and $mh->end <= $self->feature->end )
900 )
901 )
902 {
903 $self->draw_decoration_label( $gd, $dx, $dy, $mh, $h_top,
904 $h_left, $h_bottom, $h_right, $h_label );
905 }
906 }
907 }
908 }
909  
910 sub draw_decoration_label {
911 my $self = shift;
912 my ( $gd, $dx, $dy, $mh, $h_top, $h_left, $h_bottom, $h_right, $label ) = @_;
913  
914 warn "draw_decoration_label(): " . $self->feature . "\n" if (DEBUG == 2);
915  
916 my $font = $self->labelfont;
917 my $label_top = $h_top + ($self->decoration_height($mh)-$font->height)/2;
918 my $label_pos = $self->decoration_label_position($mh);
919 if ( $label_pos and $label_pos eq "above" ) {
920 $label_top = $h_top - $font->height - 1;
921 }
922 elsif ( $label_pos and $label_pos eq "below" ) {
923 $label_top = $h_top + $self->decoration_height($mh);
924 }
925  
926 my $label_color = $self->decoration_label_color($mh);
927  
928 $gd->clip( $h_left + 1, $label_top, $h_right - 1, $label_top + $font->height )
929 if ( !$gd->isa("GD::SVG::Image") );
930 $gd->string( $font, $h_left + 2,
931 $label_top, $label, $self->factory->translate_color($label_color) );
932 $gd->clip( 0, 0, $gd->width, $gd->height )
933 if ( !$gd->isa("GD::SVG::Image") );
934 }
935  
936 1;
937  
938 __END__