decorated_transcript.pm

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