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

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 122 CC 13 SCAR 27.4
CC
13
Cov
0%
draw
42.0
subseq
12.0
fgcolor
12.0
CRAP 15.5
1 package Bio::Graphics::Glyph::graded_segments;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph::segments Bio::Graphics::Glyph::minmax Bio::Graphics::Glyph::merge_parts);
5  
6 sub my_description {
7 return
8 'This glyph is used for drawing features that consist of discontinuous segments.',
9 'The color intensity of each subfeature proportional to its score tag.'
10 }
11  
12 sub my_options {
13 return {
14 max_score => [
15 'integer',
16 undef,
17 "Maximum value of the feature's \"score\" attribute."],
18 min_score => [
19 'integer',
20 undef,
21 "Minimum value of the feature's \"score\" attribute."],
22 vary_fg => [
23 'boolean',
24 undef,
25 "Vary the foreground color as well as the background."],
26 merge_parts => [
27 'boolean',
28 undef,
29 "At low magnifications, smooth small gaps to improve the visual display.",
30 "See this glyph's manual page for the gory details."],
31 max_gap => [
32 'integer',
33 undef,
34 'Do not merge across gaps that exceed this threshold.'],
35 }
36 }
37  
38 # override draw method to calculate the min and max values for the components
39 sub draw {
40 my $self = shift;
41  
42 # bail out if this isn't the right kind of feature
43 # handle both das-style and Bio::SeqFeatureI style,
44 # which use different names for subparts.
45 my @parts = $self->parts;
46 @parts = $self if !@parts && $self->level == 0;
47 return $self->SUPER::draw(@_) unless @parts;
48  
49 my ($min_score,$max_score) = $self->minmax(\@parts);
50  
51 return $self->SUPER::draw(@_)
52 unless defined($max_score) && defined($min_score)
53 && $min_score < $max_score;
54  
55 my $span = $max_score - $min_score;
56  
57 # allocate colors
58 my $fill = $self->bgcolor;
59 my ($red,$green,$blue) = $self->panel->rgb($fill);
60  
61 @parts = $self->merge_parts(@parts) if $self->option('merge_parts');
62  
63 foreach my $part (@parts) {
64 my $s = eval { $part->feature->score };
65 unless (defined $s) {
66 $part->{partcolor} = $fill;
67 next;
68 }
69 my ($r,$g,$b) = $self->calculate_color($s,[$red,$green,$blue],$min_score,$span);
70 my $idx = $self->panel->translate_color($r,$g,$b);
71 $part->{partcolor} = $idx;
72 }
73 $self->SUPER::draw(@_);
74 }
75  
76 sub calculate_color {
77 my $self = shift;
78 my ($s,$rgb,$min_score,$span) = @_;
79 return map { 255 - (255-$_) * min(max( ($s-$min_score)/$span, 0), 1) } @$rgb;
80 }
81  
82 sub min { $_[0] < $_[1] ? $_[0] : $_[1] }
83 sub max { $_[0] > $_[1] ? $_[0] : $_[1] }
84  
85 sub subseq {
86 my $class = shift;
87 my $feature = shift;
88 return $feature->segments if $feature->can('segments');
89 return $feature->sub_SeqFeature if $feature->can('sub_SeqFeature');
90 return;
91 }
92  
93 # synthesize a key glyph
94 sub keyglyph {
95 my $self = shift;
96  
97 my $scale = 1/$self->scale; # base pairs/pixel
98  
99 # two segments, at pixels 0->50, 60->80
100 my $offset = $self->panel->offset;
101  
102 my $feature =
103 Bio::Graphics::Feature->new(
104 -segments=>[ [ 0*$scale +$offset,20*$scale+$offset],
105 [ 30*$scale +$offset,50*$scale+$offset],
106 [60*$scale+$offset, 80*$scale+$offset]
107 ],
108 -name => $self->option('key'),
109 -strand => '+1');
110 ($feature->segments)[0]->score(10);
111 ($feature->segments)[1]->score(50);
112 ($feature->segments)[2]->score(100);
113 my $factory = $self->factory->clone;
114 $factory->set_option(label => 1);
115 $factory->set_option(bump => 0);
116 $factory->set_option(connector => 'solid');
117 return $factory->make_glyph($feature);
118 }
119  
120 # component draws a shaded box
121 sub bgcolor {
122 my $self = shift;
123 return defined $self->{partcolor} ? $self->{partcolor} : $self->SUPER::bgcolor;
124 }
125 sub fgcolor {
126 my $self = shift;
127 return $self->SUPER::fgcolor unless $self->option('vary_fg');
128 return defined $self->{partcolor} ? $self->{partcolor} : $self->SUPER::fgcolor;
129 }
130  
131 1;
132  
133 =head1 NAME
134  
135 Bio::Graphics::Glyph::graded_segments - The "graded_segments" glyph
136  
137 =head1 SYNOPSIS
138  
139 See L<Bio::Graphics::Panel> and L<Bio::Graphics::Glyph>.
140  
141 =head1 DESCRIPTION
142  
143 This is identical to the "alignment" glyph, and is used for
144 drawing features that consist of discontinuous segments. The
145 color intensity of each segment is proportionate to the score.
146  
147 =head2 OPTIONS
148  
149 The following options are standard among all Glyphs. See
150 L<Bio::Graphics::Glyph> for a full explanation.
151  
152 Option Description Default
153 ------ ----------- -------
154  
155 -fgcolor Foreground color black
156  
157 -outlinecolor Synonym for -fgcolor
158  
159 -bgcolor Background color turquoise
160  
161 -fillcolor Synonym for -bgcolor
162  
163 -linewidth Line width 1
164  
165 -height Height of glyph 10
166  
167 -font Glyph font gdSmallFont
168  
169 -connector Connector type 0 (false)
170  
171 -connector_color
172 Connector color black
173  
174 -label Whether to draw a label 0 (false)
175  
176 -description Whether to draw a description 0 (false)
177  
178 -hilite Highlight color undef (no color)
179  
180 In addition, the alignment glyph recognizes the following
181 glyph-specific options:
182  
183 Option Description Default
184 ------ ----------- -------
185  
186 -max_score Maximum value of the Calculated
187 feature's "score" attribute
188  
189 -min_score Minimum value of the Calculated
190 feature's "score" attribute
191  
192 -vary_fg Vary the foreground color as 0 (false)
193 well as the background
194  
195 -merge_parts 0 (false)
196 Whether to simplify the
197 alignment at low magnification
198  
199 -max_gap Do not merge across gaps Calculated
200 that exceed this threshold
201  
202  
203 If max_score and min_score are not specified, then the glyph will
204 calculate the local maximum and minimum scores at run time. Since
205 many scoring functions are exponential you may wish to take the log of
206 your scores before passing them to this glyph.
207  
208  
209 =head2 Simplifying the display of alignment features for large segments
210  
211 The "merge_parts" option is used for semantic zooming.
212 Specifically, if features are small and dense, they
213 will not be displayed very well for large segments and the
214 color-coding will be lost. If merge-parts is set to a
215 true value, adjacent alignment parts will be merged until a gap
216 exceeding a calculated or user-specified value is encountered.
217 Unless specified, the maximum gap allowed for merging adjacent features is
218 calculated as (L/10000)*(L/500), where L = the length of the sequence
219 displayed in the browser. The exponentially increasing gap threshold
220 allows more aggressive merging of alignment features as the size of
221 the displayed sequence grows larger.
222  
223 The score of the merged feature is calculated as a weighted average.
224 For example, consider two adjacent HSPs that are each 400 bp in
225 length and have scores of 60% and 70%. If the merge_parts option
226 is set to a true value, the two HSPs would be merged in the display to
227 a single 800 bp alignment block with an average score of 65%.
228  
229 The merge_parts option is turned off by default.
230  
231  
232 =head1 BUGS
233  
234 Please report them.
235  
236 =head1 SEE ALSO
237  
238 L<Bio::Graphics::Panel>,
239 L<Bio::Graphics::Glyph>,
240 L<Bio::Graphics::Glyph::arrow>,
241 L<Bio::Graphics::Glyph::cds>,
242 L<Bio::Graphics::Glyph::crossbox>,
243 L<Bio::Graphics::Glyph::diamond>,
244 L<Bio::Graphics::Glyph::dna>,
245 L<Bio::Graphics::Glyph::dot>,
246 L<Bio::Graphics::Glyph::ellipse>,
247 L<Bio::Graphics::Glyph::extending_arrow>,
248 L<Bio::Graphics::Glyph::generic>,
249 L<Bio::Graphics::Glyph::graded_segments>,
250 L<Bio::Graphics::Glyph::heterogeneous_segments>,
251 L<Bio::Graphics::Glyph::line>,
252 L<Bio::Graphics::Glyph::pinsertion>,
253 L<Bio::Graphics::Glyph::primers>,
254 L<Bio::Graphics::Glyph::rndrect>,
255 L<Bio::Graphics::Glyph::segments>,
256 L<Bio::Graphics::Glyph::ruler_arrow>,
257 L<Bio::Graphics::Glyph::toomany>,
258 L<Bio::Graphics::Glyph::transcript>,
259 L<Bio::Graphics::Glyph::transcript2>,
260 L<Bio::Graphics::Glyph::translation>,
261 L<Bio::Graphics::Glyph::triangle>,
262 L<Bio::DB::GFF>,
263 L<Bio::SeqI>,
264 L<Bio::SeqFeatureI>,
265 L<Bio::Das>,
266 L<GD>
267  
268 =head1 AUTHOR
269  
270 Lincoln Stein E<lt>lstein@cshl.orgE<gt>
271  
272 Copyright (c) 2001 Cold Spring Harbor Laboratory
273  
274 This library is free software; you can redistribute it and/or modify
275 it under the same terms as Perl itself. See DISCLAIMER.txt for
276 disclaimers of warranty.
277  
278 =cut