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

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 228 CC 32 SCAR 34.5
CC
32
Cov
0%
_subfeat
110.0
draw
30.0
pad_right
30.0
CRAP 31.4
1 package Bio::Graphics::Glyph::rainbow_gene;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph::gene Bio::Graphics::Glyph::heat_map);
5  
6 sub my_descripton {
7 return <<END;
8 This glyph is based on the gene glyph, expect that it inherits
9 methods from the heat_map glyph so that scored exons of transcript
10 features can have variable background colors. This is used to make
11 a score-based heat-map using the HSV color space. Either monochrome
12 gradients (for example the default white->red), or gradients progressing
13 through the colors of the rainbow (magenta->blue->green->yelloe->red)
14 can be created.
15  
16 For example:
17 # a white->red heat map
18 start_color = white
19 stop_color = red
20  
21 # a rainbow
22 start_color = magenta
23 stop_color = red
24  
25 # green->yellow->red
26 start_color = green
27 stop_color = red
28  
29 This glyph is a subclass of the gene and heat_maps glyphs, and
30 recognizes the same options.
31 END
32 }
33  
34 sub my_options {
35 {
36 transcript_only => [
37 'boolean',
38 undef,
39 'If true, thes score of the transcript or mRNA feature will be '.
40 'will be used to calculate the background color. Otherwise the '.
41 'score of exon sub-features are used'
42 ],
43 }
44 }
45  
46 sub draw {
47 my $self = shift;
48  
49 my @parts = $self->parts;
50 @parts = $self if !@parts && $self->level == 0;
51 return $self->SUPER::draw(@_) unless @parts;
52  
53 my $top_score = $self->feature->score if $self->option('transcript_only');
54  
55 $self->calculate_gradient(\@parts);
56 my $low_rgb = $self->low_rgb;
57  
58 for my $part (@parts) {
59 my $score = $top_score || $part->feature->score;
60  
61 unless (defined $score && $self->score_range ) {
62 $part->{partcolor} = $self->color_index(@$low_rgb);
63 }
64 else {
65 my @rgb = $self->calculate_color($score);
66 $part->{partcolor} = $self->color_index(@rgb);
67 }
68  
69 }
70  
71 return $self->SUPER::draw(@_);
72 }
73  
74  
75 sub extra_arrow_length {
76 my $self = shift;
77 return 0 unless $self->{level} == 1;
78 local $self->{level} = 0; # fake out superclass
79 return $self->SUPER::extra_arrow_length;
80 }
81  
82 sub pad_left {
83 my $self = shift;
84 my $type = $self->feature->primary_tag;
85 return 0 unless $type =~ /gene|mRNA/;
86 $self->SUPER::pad_left;
87 }
88  
89 sub pad_right {
90 my $self = shift;
91 return 0 unless $self->{level} < 2; # don't invoke this expensive call on exons
92 my $strand = $self->feature->strand;
93 $strand *= -1 if $self->{flip};
94 my $pad = $self->SUPER::pad_right;
95 return $pad unless defined($strand) && $strand > 0;
96 my $al = $self->arrow_length;
97 return $al > $pad ? $al : $pad;
98 }
99  
100 sub pad_bottom {
101 my $self = shift;
102 return 0 unless $self->{level} < 2; # don't invoke this expensive call on exons
103 return $self->SUPER::pad_bottom;
104 }
105  
106 sub pad_top {
107 my $self = shift;
108 return 0 unless $self->{level} < 2; # don't invoke this expensive call on exons
109 return $self->SUPER::pad_top;
110 }
111  
112 sub bump {
113 my $self = shift;
114 return 1 # top level bumps, other levels don't unless specified in config
115 if $self->{level} == 0
116 && lc $self->feature->primary_tag eq 'gene';
117 return $self->SUPER::bump;
118 }
119  
120 sub label {
121 my $self = shift;
122 return unless $self->{level} < 2;
123 if ($self->label_transcripts && $self->{feature}->primary_tag =~ /RNA|pseudogene/i) {
124 return $self->_label;
125 } else {
126 return $self->SUPER::label;
127 }
128 }
129  
130 sub label_position {
131 my $self = shift;
132 return 'top' if $self->{level} == 0;
133 return 'left';
134 }
135  
136 sub label_transcripts {
137 my $self = shift;
138 return $self->{label_transcripts} if exists $self->{label_transcripts};
139 return $self->{label_transcripts} = $self->_label_transcripts;
140 }
141  
142 sub _label_transcripts {
143 my $self = shift;
144 return $self->option('label_transcripts');
145 }
146  
147 sub draw_connectors {
148 my $self = shift;
149 if ($self->feature->primary_tag eq 'gene') {
150 my @parts = $self->parts;
151 return if @parts && $parts[0] =~ /rna|transcript|pseudogene/i;
152 }
153 $self->SUPER::draw_connectors(@_);
154 }
155  
156 sub maxdepth {
157 my $self = shift;
158 my $md = $self->Bio::Graphics::Glyph::maxdepth;
159 return $md if defined $md;
160 return 2;
161 }
162  
163  
164 sub _subfeat {
165 my $class = shift;
166 my $feature = shift;
167  
168 if ($feature->primary_tag =~ /^gene/i) {
169 my @transcripts;
170 for my $t (qw/mRNA tRNA snRNA snoRNA miRNA ncRNA pseudogene/) {
171 push @transcripts, $feature->get_SeqFeatures($t);
172 }
173 return @transcripts if @transcripts;
174 return $feature->get_SeqFeatures; # no transcripts?! return whatever's there
175 }
176 elsif ($feature->primary_tag =~ /^CDS/i) {
177 my @parts = $feature->get_SeqFeatures();
178 return ($feature) if $class->{level} == 0 and !@parts;
179 return @parts;
180 }
181  
182 my @subparts;
183 if ($class->option('sub_part')) {
184 @subparts = $feature->get_SeqFeatures($class->option('sub_part'));
185 }
186 elsif ($feature->primary_tag =~ /^mRNA/i) {
187 @subparts = $feature->get_SeqFeatures(qw(CDS five_prime_UTR three_prime_UTR UTR));
188 }
189 else {
190 @subparts = $feature->get_SeqFeatures('exon');
191 }
192
193 # The CDS and UTRs may be represented as a single feature with subparts or as several features
194 # that have different IDs. We handle both cases transparently.
195 my @result;
196 foreach (@subparts) {
197 if ($_->primary_tag =~ /CDS|UTR/i) {
198 my @cds_seg = $_->get_SeqFeatures;
199 if (@cds_seg > 0) { push @result,@cds_seg } else { push @result,$_ }
200 } else {
201 push @result,$_;
202 }
203 }
204 # fall back to drawing a solid box if no subparts and level 0
205 return ($feature) if $class->{level} == 0 && !@result;
206  
207 return @result;
208 }
209  
210 sub bgcolor {
211 my $self = shift;
212 return defined $self->{partcolor} ? $self->{partcolor} : $self->SUPER::bgcolor;
213 }
214  
215 sub fgcolor {
216 my $self = shift;
217 return $self->option('vary_fg') ? $self->bgcolor : $self->SUPER::fgcolor;
218 }
219  
220 1;
221  
222 __END__