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

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 140 CC 10 SCAR 27.9
CC
10
Cov
0%
draw
42.0
example_scores
12.0
in_range
6.0
CRAP 16.3
1 package Bio::Graphics::Glyph::merged_alignment;
2  
3 # this glyph acts like graded_segments but the bgcolor of each segment is
4 # more configurable. Supply a list of colors and corresponding
5 # bins. Each bin is a range of x-y scores, where score n is > x and <= y
6 # e.g.
7 # [ALIGNMENT]
8 # feature = alignment
9 # bins = 0-50 50-70 70-90 90-100
10 # bincolors = white powderblue cornflowerblue blue
11  
12 # for sematic zooming (at lower magnification), to
13 # reduce visual complexity of alignment
14 # [ALIGNMENT:20000] # segment length >= 20000
15 # feature = alignment
16 # merge_parts = 1
17 # max_gap = 500 # do not merge across gaps > 500 bp
18  
19  
20 use strict;
21 use base qw(Bio::Graphics::Glyph::graded_segments);
22  
23 use constant COLORS => "lightgrey powderblue cornflowerblue blue";
24  
25 sub my_description {
26 return <<END;
27 Like graded_segments, this glyph draws each subpart of a feature with a different color
28 dependingon its score attribute. It accepts a series of colors in the option 'bincolors',
29 and then assigns each color to a bin based on the score of the feature\'s subparts.
30  
31 This glyph also supports
32 semantic zooming to optimize glyph drawing for larger sequence
33 displays.
34 END
35 }
36 sub my_options {
37 {
38 max_score => [
39 'integer',
40 undef,
41 "Maximum value of the feature's \"score\" attribute.",
42 "If undef, the value will be calculated automatically."],
43 min_score => [
44 'integer',
45 undef,
46 "Minimum value of the feature's \"score\" attribute.",
47 "If undef, the value will be calculated automatically."],
48 bincolors => [
49 'string',
50 'lightgrey powderblue cornflowerblue blue',
51 'A space-delimited string of colors to be assigned to score bins.'],
52 bins => [
53 'integer',
54 undef,
55 'Size of the score bins. If undefined, the range of scores will be divided',
56 'into the number of bins indicated by the size of the list of bincolors.'],
57 merge_parts => [
58 'boolean',
59 undef,
60 'Whether to merge small subfeatures to simplify the display at low magnifications.'],
61 }
62 }
63  
64 # override draw method
65 sub draw {
66 my $self = shift;
67  
68 # bail out if this isn't the right kind of feature
69 # handle both das-style and Bio::SeqFeatureI style,
70 # which use different names for subparts.
71 my @parts = $self->parts;
72 @parts = $self if !@parts && $self->level == 0;
73 return $self->SUPER::draw(@_) unless @parts;
74  
75 my $cols = $self->option('bincolors') || COLORS;
76 my @cols = split /\s+/, $cols;
77 my $bins = $self->option('bins');
78 my @bins = $bins ? split /\s+/, $bins : $self->get_bins(\@parts, @cols);
79 my %color;
80 @color{@bins} = @cols;
81  
82 @parts = $self->merge_parts(@parts) if $self->option('merge_parts');
83  
84 # figure out the colors
85 for my $part (@parts) {
86 my ($bin) = grep { $part->in_range($_) } @bins;
87 my $idx = $bin
88 ? $self->panel->translate_color($color{$bin})
89 : $self->panel->translate_color('white');
90 $part->{partcolor} = $idx;
91 }
92
93 $self->{parts} = \@parts;
94  
95 $self->Bio::Graphics::Glyph::merge_parts::draw(@_);
96 }
97  
98 sub in_range {
99 my $self = shift;
100 my $range = shift;
101 my ($low,$high) = split '-', $range;
102 my $s = $self->score || shift;
103 return 1 if $s >= $low && $s <= $high;
104 return 0;
105 }
106  
107 # overide background method to paint glyph white as
108 # a last resort
109 sub bgcolor {
110 my $self = shift;
111 return $self->{partcolor} || 'white';
112 }
113  
114 # used if bins are not defined in the configuration
115 # makes equal sized bins corresponding to the number of
116 # colors specified
117 sub get_bins {
118 my $self = shift;
119 my $parts = shift;
120 my $cols = @_;
121 my ($min,$max) = $self->minmax($parts);
122 my $range = $max - $min;
123 return ($max) if $range == 0;
124 my $increment = $range/$cols+1;
125
126 my ($score,@bins) = $min;
127 until ($score > $max) {
128 my $range = "$score-";
129 $score += $increment;
130 $range .= $score-1;
131 push @bins, $range;
132 }
133 return @bins;
134 }
135  
136  
137 # synthesize a key glyph
138 sub keyglyph {
139 my $self = shift;
140  
141 my $scale = 1/$self->scale; # base pairs/pixel
142  
143 # two segments, at pixels 0->50, 60->80
144 my $offset = $self->panel->offset;
145
146 my $feature =
147 Bio::Graphics::Feature->new(
148 -segments=>[ [ 0*$scale +$offset,25*$scale+$offset],
149 [ 25*$scale +$offset,50*$scale+$offset],
150 [ 50*$scale+$offset, 75*$scale+$offset]
151 ],
152 -name => $self->option('key'),
153 -strand => '+1');
154  
155 my @scores = $self->example_scores;
156 my @segments = $feature->segments;
157 for ($feature->segments) {
158 $_->score(shift @scores);
159 }
160  
161 my $factory = $self->factory->clone;
162 $factory->set_option(label => 1);
163 $factory->set_option(bump => 0);
164 $factory->set_option(connector => 'solid');
165 my $glyph = $factory->make_glyph(0,$feature);
166 }
167  
168 sub example_scores {
169 my $self = shift;
170 my $bins = $self->option('bins');
171  
172 if ($bins) {
173 my @bins = split /\s+/, $bins;
174 $bins[0] =~ s/(\S+)\-\S+/$1/;
175 $bins[-1] =~ s/\S+\-(\S+)/$1/;
176 my $mid = $bins[0] + ($bins[-1] - $bins[0])/2;
177
178 return ($bins[0], $mid, $bins[-1]);
179 }
180 if ($self->option('min_score') || $self->option('max_score')) {
181 my ($min,$max) = $self->minmax;
182 my $mid = $min + ($max - $min)/2;
183 return($min,$mid,$max);
184 }
185
186 return (0,50,100);
187 }
188  
189 1;
190  
191 =pod
192  
193 =head1 NAME
194  
195 Bio::Graphics::Glyph::merged_alignment - The "merged_alignment" glyph
196  
197 =head1 SYNOPSIS
198  
199 See L<Bio::Graphics::Panel> and L<Bio::Graphics::Glyph>.
200  
201 =head1 DESCRIPTION
202  
203 This glyph acts like graded_segments but the bgcolor of segments
204 (sub-feature) is controlled by binned scores. It also supports
205 semantic zooming to optimize glyph drawing for larger sequence
206 displays.
207  
208 =head2 OPTIONS
209  
210 The following options are standard among all Glyphs. See
211 L<Bio::Graphics::Glyph> for a full explanation.
212  
213 Option Description Default
214 ------ ----------- -------
215  
216 -fgcolor Foreground color black
217  
218 -outlinecolor Synonym for -fgcolor
219  
220 -bgcolor Background color turquoise
221  
222 -fillcolor Synonym for -bgcolor
223  
224 -linewidth Line width 1
225  
226 -height Height of glyph 10
227  
228 -font Glyph font gdSmallFont
229  
230 -connector Connector type 0 (false)
231  
232 -connector_color
233 Connector color black
234  
235 -label Whether to draw a label 0 (false)
236  
237 -description Whether to draw a description 0 (false)
238  
239 -hilite Highlight color undef (no color)
240  
241  
242 In addition, the merged-alignment glyph recognizes the following
243 glyph-specific options:
244  
245 Option Description Default
246 ------ ----------- -------
247  
248 -max_score Maximum value of the Calculated
249 feature's "score" attribute
250  
251 -min_score Minimum value of the Calculated
252 feature's "score" attribute
253  
254 -bincolors Colors assigned to bins lightgrey powderblue cornflowerblue blue
255 (in order)
256  
257 -bins Bins to which scores are Calculated
258 assigned
259  
260 -merge_parts 0 (false)
261 Whether to simplify the
262 alignment at low magnification
263  
264 -max_gap Do not merge across gaps Calculated
265 that exceed this threshold
266  
267  
268 If max_score and min_score are not specified, then the glyph will
269 calculate the local maximum and minimum scores at run time.
270  
271 If the bins are not specified, they will be calculated
272 based on the number of colors assigned and the local
273 (or user-specified) minimum and maximum scores.
274 Calculated bins are equal in size.
275  
276 User-specified bins are expressed as ranges,
277  
278 bins = 0-50 50-70 70-90 90-100
279  
280 where each range means greater than the lower number and
281 less than or equal to the higher number.
282  
283  
284 =head2 Simplifying the display of alignment features for large segments
285  
286 The "merge_parts" option is used for semantic zooming.
287 Specifically, if features are small and dense, they
288 will not be displayed very well for large segments and the
289 color-coding will be lost. If merge-parts is set to a
290 true value, adjacent alignment parts will be merged until a gap
291 exceeding a calculated or user-specified value is encountered.
292 Unless specified, the maximum gap allowed for merging adjacent features is
293 calculated as (L/10000)*(L/500), where L = the length of the sequence
294 displayed in the browser. The exponentially increasing gap threshold
295 allows more aggressive merging of alignment features as the size of
296 the displayed sequence grows larger.
297  
298 The score of the merged feature is calculated as a weighted average.
299 For example, consider two adjacent HSPs that are each 400 bp in
300 length and have scores of 60% and 70%. If the merge_parts option
301 is set to a true value, the two HSPs would be merged in the display to
302 a single 800 bp alignment block with an average score of 65%.
303  
304 The merge_parts option is turned off by default.
305  
306 =head2 SAMPLE CONFIGURATION
307  
308 Sample gbrowse configuration stanzas for an alignment feature
309 using this glyph. The scores are assumed to be expressed
310 as percent identity (0-100).
311  
312 # base configuration
313 [BLASTZ]
314 feature = blastz_alignment
315 glyph = merged_alignment
316 bincolors = #A0A0A0 powderblue cornflowerblue blue
317 bins = 60-70 70-80 80-90 90-100
318 category = Sequence Similarity Tracks
319 height = 6
320 bump = 1
321 label = 1
322 fgcolor = black
323 key = BLASTZ
324  
325 Semantic zooming with defined maximum gap between
326 merged features for different zoom levels
327  
328 # if the displayed segment is >= 20000 in length,
329 # use the merge_parts option to simplify the alignment
330 # display
331 [BLASTZ:20000]
332 feature = blastz_alignment
333 merge_parts = 1
334 max_gap = 50 # do not merge across gaps > 50 bp
335  
336 # if the displayed segment is >= 50000 in length
337 [BLASTZ:50000]
338 feature = blastz_alignment
339 merge_parts = 1
340 max_gap = 500 # do not merge across gaps > 500 bp
341  
342 --OR--
343  
344 Semantic zooming with dynamically calculated maximum
345 gap
346  
347 # if the displayed segment is >= 20000 in length,
348 [BLASTZ:20000]
349 feature = blastz_alignment
350 merge_parts = 1
351  
352 =head1 BUGS
353  
354 Please report them.
355  
356 =head1 SEE ALSO
357  
358 L<Bio::Graphics::Panel>,
359 L<Bio::Graphics::Glyph>
360 L<Bio::DB::GFF>,
361 L<Bio::SeqI>,
362 L<Bio::SeqFeatureI>,
363 L<Bio::Das>,
364 L<GD>
365  
366 =head1 AUTHOR
367  
368 Sheldon McKay E<lt>mckays@cshl.eduE<gt>
369  
370 Copyright (c) 2005 Cold Spring Harbor Laboratory
371  
372 This library is free software; you can redistribute it and/or modify
373 it under the same terms as Perl itself. See DISCLAIMER.txt for
374 disclaimers of warranty.
375  
376 =cut