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

filter: Statementstmt 0.0% 0 / 277 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 57 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 500 CC 62 SCAR 48.7
CC
62
Cov
0%
draw_signal
342.0
draw_peaks
210.0
calculate_gradient
132.0
CRAP 130.4
1 package Bio::Graphics::Glyph::vista_plot;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph::wiggle_xyplot
5 Bio::Graphics::Glyph::wiggle_density
6 Bio::Graphics::Glyph::wiggle_whiskers
7 Bio::Graphics::Glyph::heat_map
8 Bio::Graphics::Glyph::smoothing);
9  
10  
11 our $VERSION = '1.0';
12  
13 sub my_options {
14 {
15 start_color => [
16 'color',
17 'white',
18 'Beginning of the color gradient, expressed as a named color or',
19 'RGB hex string.'],
20 end_color => [
21 'color',
22 'red',
23 'End of the color gradient.'],
24 min_peak => [
25 'integer',
26 1,
27 "Minimum value of the peak feature's \"score\" attribute."],
28 max_peak => [
29 'integer',
30 255,
31 "Maximum value of the peak feature's \"score\" attribute."],
32 min_score => [
33 'integer',
34 undef,
35 "Minimum value of the signal graph feature's \"score\" attribute."],
36 max_score => [
37 'integer',
38 undef,
39 "Maximum value of the signal graph feature's \"score\" attribute."],
40 peakwidth => [
41 'integer',
42 3,
43 "Line width determine the thickness of the line representing a peak."],
44 glyph_subtype => [
45 ['peaks+signal','peaks','signal','density'],
46 'vista',
47 "What to show, peaks or signal, both (vista plot) or density graph."],
48 graph_type => [
49 ['whiskers','histogram','boxes','line','points','linepoints'],
50 'boxes',
51 "Type of signal graph to show."],
52 alpha => [
53 'integer',
54 100,
55 "Alpha transparency of peak regions",
56 ],
57 };
58 }
59  
60 sub my_description {
61 return <<END;
62 This glyph draws peak calls (features with discreet boundaries,
63 i.e. putative transcription sites, over signal graph (wiggle_xyplot)
64 requires a special load gff file that uses attributes 'wigfile' and 'peak_type'
65 BigWig support is available also, see POD documentation for more details
66  
67 Example:
68  
69 2L chip_seq vista 5407 23011573 . . . Name=Chip-Seq Experiment 1;wigfile=SomeWigFile.wigdb;peak_type=transcript_region:exp1
70  
71 END
72 }
73  
74  
75 BEGIN {
76 no strict 'refs';
77  
78 my @subs = qw/ h_start s_start v_start h_range s_range v_range
79 min_peak_score max_peak_score low_rgb low_hsv high_rgb peak_score_range/;
80  
81 for my $sub ( @subs ) {
82 *{$sub} = sub {
83 my ($self, $v) = @_;
84 my $k = "_$sub";
85  
86 if (defined $v) {
87 $self->{$k} = $v;
88 }
89  
90 return $self->{$k};
91 }
92 }
93 }
94  
95 sub peakwidth {
96 shift->option('peakwidth') || 3;
97 }
98  
99 sub alpha_c {
100 my $self = shift;
101 return $self->option('alpha') || 100;
102 }
103  
104 # Need to override wiggle_xyplot padding function to enable adequate height control in density mode
105 sub pad_top {
106 my $self = shift;
107 return 0 if $self->glyph_subtype eq 'density';
108 my $pad = $self->Bio::Graphics::Glyph::generic::pad_top(@_);
109 if ($pad < ($self->font('gdTinyFont')->height)) {
110 $pad = $self->font('gdTinyFont')->height; # extra room for the scale
111 }
112 $pad;
113 }
114  
115 sub bigwig_summary {
116 my $self = shift;
117 my $d = $self->{bigwig_summary};
118 $self->{bigwig_summary} = shift if @_;
119 $d;
120 }
121  
122 # Need to override this too b/c we need unconventional mean and stdev calculation
123 sub global_mean_and_variance {
124 my $self = shift;
125 if (my $wig = $self->wig) {
126 my @result = eval{($wig->mean,$wig->stdev)};
127 return @result if @result;
128 }
129 if (my $sum = $self->bigwig_summary){
130 use Bio::DB::BigWig qw(binMean binStdev);
131 my $stats = $sum->statistical_summary(1);
132 return eval{(binMean($stats->[0]),binStdev($stats->[0]))};
133 }
134 return;
135 }
136  
137 sub glyph_subtype {
138 my $self = shift;
139 my $only_show = $self->option('only_show') || $self->option('glyph_subtype') || 'vista';
140 $only_show = 'vista' if $only_show eq 'both' || $only_show eq 'peaks+signal';
141 return $only_show;
142 }
143  
144 sub graph_type {
145 my $self = shift;
146 return $self->option('graph_type') || $self->options->{graph_type}[1];
147 }
148  
149 # we override the draw method so that it dynamically creates the parts needed
150 # from the wig file rather than trying to fetch them from the database
151 sub draw {
152 my $self = shift;
153 my($gd,$dx,$dy) = @_;
154  
155 my $only_show = $self->glyph_subtype;
156 my $feature = $self->feature;
157
158 # Draw dual graph if we have both types of attributes, BigWig and wiggle format supported
159 my %features = (wig => (eval{$feature->get_tag_values('wigfile')})[0],
160 peak => (eval{$feature->get_tag_values('peak_type')})[0],
161 fasta=> (eval{$feature->get_tag_values('fasta')})[0]);
162 $self->panel->startGroup($gd);
163  
164 $self->draw_signal($only_show,\%features,@_) if $only_show =~ /signal|density|vista/;
165 $self->draw_peaks(\%features,@_) if $features{peak} && $only_show =~ /peaks|vista|both/;
166 $self->Bio::Graphics::Glyph::xyplot::draw_label(@_) if $self->option('label');
167 $self->draw_description(@_) if $self->option('description');
168 $self->panel->endGroup($gd);
169 }
170  
171 # this should be refactored from wiggle_xyplot and wiggle_density
172 sub draw_signal {
173 my $self = shift;
174 my $signal_type = shift;
175 my $paths = shift;
176  
177 my $feature = $self->feature;
178  
179 # Signal Graph drawing:
180 if ($paths->{wig} && $paths->{wig}=~/\.wi\w{1,3}$/) {
181 eval "require Bio::Graphics::Wiggle" unless Bio::Graphics::Wiggle->can('new');
182 my $wig = eval { Bio::Graphics::Wiggle->new($paths->{wig}) };
183 $self->wig($wig);
184 $self->_draw_wigfile($feature,$wig,@_);
185 } elsif ($paths->{wig} && $paths->{wig}=~/\.bw$/i) {
186 eval "use Bio::DB::BigWig 'binMean'" unless Bio::DB::BigWig->can('new');
187 my @args = (-bigwig => "$paths->{wig}");
188 if ($paths->{fasta}) {
189 eval "use Bio::DB::Sam" unless Bio::DB::Sam::Fai->can('open');
190 my $fasta_accessor = Bio::DB::Sam::Fai->can('open') ? Bio::DB::Sam::Fai->open("$paths->{fasta}")
191 : Bio::DB::Fasta->new("$paths->{fasta}");
192 push @args,(-fasta => $fasta_accessor);
193 }
194 my $bigwig = Bio::DB::BigWig->new(@args);
195 $self->wig($bigwig);
196 my ($summary) = $bigwig->features(-seq_id => $feature->segment->ref,
197 -start => $self->panel->start,
198 -end => $self->panel->end,
199 -type => 'summary');
200  
201 local $self->{feature} = $summary;
202 if ($signal_type ne 'density' and $self->graph_type eq 'whiskers') {
203 $self->Bio::Graphics::Glyph::wiggle_whiskers::draw(@_);
204 } else {
205 my $stats = $summary->statistical_summary($self->width);
206 my $interval_method = $self->option('interval_method') || 'mean';
207 my @vals;
208 if ($interval_method eq 'mean') {
209 @vals = map {$_->{validCount} ? Bio::DB::BigWig::binMean($_) : undef} @$stats;
210 }
211 elsif ($interval_method eq 'sum') {
212 @vals = map {$_->{validCount} ? $_->{sumData} : undef} @$stats;
213 }
214 elsif ($interval_method eq 'min') {
215 @vals = map {$_->{validCount} ? $_->{minVal} : undef} @$stats;
216 }
217 elsif ($interval_method eq 'max') {
218 @vals = map {$_->{validCount} ? $_->{maxVal} : undef} @$stats;
219 }
220 else {
221 warn "unrecognized interval method $interval_method!";
222 }
223 $self->bigwig_summary($summary);
224 if ($signal_type eq 'density') {
225 $self->Bio::Graphics::Glyph::wiggle_density::_draw_coverage($summary,\@vals,@_);
226 } else {
227 $self->Bio::Graphics::Glyph::wiggle_data::_draw_coverage($summary,\@vals,@_);
228 }
229 }
230 }
231 }
232  
233 sub draw_peaks {
234 my $self = shift;
235 my $paths = shift;
236 my($gd,$dx,$dy) = @_;
237 my($left,$top,$right,$bottom) = $self->calculate_boundaries($dx,$dy);
238  
239 # Peak drawing:
240 my $alpha_c = $self->alpha_c;
241 my $feature = $self->feature;
242
243 my $p_type = $paths->{peak};
244 my @peaks = $self->peaks();
245 my $x_scale = $self->scale;
246 my $panel_start = $self->panel->start;
247 my $f_start = $feature->start > $panel_start
248 ? $feature->start
249 : $panel_start;
250 my $lw = $self->peakwidth;
251 my($max_s,$min_s) = ($self->option('max_peak'),$self->option('min_peak'));
252 $max_s = 255 if !defined $max_s;
253 $min_s = 1 if !defined $min_s;
254 my $grad_ok = 0;
255 if (defined $max_s && defined $min_s) {
256 $grad_ok = $self->calculate_gradient($min_s,$max_s);
257 }
258  
259 my $flip = $self->{flip};
260  
261 $self->{peak_cache} = []; # remember coordinates of the peaks
262  
263 foreach my $peak (@peaks) {
264 my $x1 = $left + ($peak->{start} - $f_start) * $x_scale;
265 my $x2 = $left + ($peak->{stop} - $f_start) * $x_scale;
266 if ($x2 >= $left and $x1 <= $right) {
267 my $y1 = $top;
268 my $y2 = $bottom;
269 $x1 = $left if $x1 < $left;
270 $x2 = $right if $x2 > $right;
271 $alpha_c = $alpha_c <=127 ? $alpha_c : 0; # Reset to zero if illegal value is passed
272 my $score = $peak->{score};
273 if ($score eq "."){$score = 255;} # Set score to 255 if peak is unscored
274 my $color;
275 if ($grad_ok && defined $score && $score!=255) {
276 my @rgb = $self->Bio::Graphics::Glyph::heat_map::calculate_color($score,
277 $self->min_peak_score,
278 $self->max_peak_score,
279 $self->peak_score_range);
280 $color = $self->color_index(@rgb);
281 }else{
282 $color = $self->fgcolor;
283 }
284  
285 my $bgcolor = $self->bgcolor;
286
287 if ($alpha_c > 0){
288 $gd->alphaBlending(1);
289 $bgcolor = $self->add_alpha($gd,$bgcolor,$alpha_c);
290 }
291  
292 if ($flip) {
293 $x1 = $right - ($x1-$left);
294 $x2 = $right - ($x2-$left);
295 ($x1,$x2) = ($x2,$x1);
296 }
297
298 my @rect = (int($x1+0.5),int($y1+0.5),int($x2+0.5),int($y2+0.5));
299 $self->filled_box($gd,@rect,$bgcolor,$bgcolor,0.5) if abs($y2-$y1) > 0;
300 $gd->setThickness($lw);
301 $gd->line(int($x1+0.5),int($y1+0.5),int($x2+0.5),int($y1+0.5),$color);
302 $gd->setThickness(1);
303 push @{$self->{peak_cache}},[$peak,@rect];
304 }
305 }
306 }
307  
308 # Adding alpha channel to a color:
309 sub add_alpha {
310 my($self,$im,$color,$alpha) = @_;
311 my($r,$g,$b) = $im->rgb($color);
312 return $im->colorAllocateAlpha($r,$g,$b,$alpha);
313 }
314  
315 # Slightly modified function from heat_map.pm
316 sub calculate_gradient {
317 my($self, $min, $max) = @_;
318 my $start_color = lc $self->option('start_color') || 'white';
319 my $stop_color = lc $self->option('end_color') || 'red';
320 my $hsv_start = $self->color2hsv($start_color);
321 my $hsv_stop = $self->color2hsv($stop_color);
322  
323 my ($h_start,$s_start,$v_start) = @$hsv_start;
324 my ($h_stop,$s_stop,$v_stop ) = @$hsv_stop;
325  
326 my $s_range = abs($s_stop - $s_start);
327 my $v_range = abs($v_stop - $v_start);
328  
329 my $h_range;
330 # special case: if start hue = end hue, we want to go round
331 # the whole wheel once. Otherwise round the wheel clockwise
332 # or counterclockwise depending on start and end coordinate
333 if ($h_start != $h_stop) {
334 my $direction = abs($h_stop - $h_start)/($h_stop - $h_start);
335 my ($sstart,$sstop) = sort {$a <=> $b} ($h_start,$h_stop);
336 $direction *= -1 if $sstop - $sstart > 256/2; #reverse the direction if we cross 0
337 $h_range = ($sstop - $sstart) <= 256/2 ? ($sstop - $sstart)*$direction : (256 - $sstop + $sstart)*$direction;
338 }
339 else {
340 $h_range = 256;
341 }
342 # darkness or monochrome gradient?
343 if ( !_isa_color($start_color) || !_isa_color($stop_color) ) {
344 # hue (H) is fixed
345 $h_range = 0;
346  
347 # gradient S V
348 # white -> color 0->255 255
349 # color -> white 255->0 255
350 # white -> black 0 255->0
351 # black -> white 0 0->255
352 # black -> color 0->255 0->255
353 # color -> black 255->0 255->0
354 if ( $start_color eq 'white' && _isa_color($stop_color) ) {
355 $s_range = 255;
356 $s_start = 0;
357 $v_range = 0;
358 $v_start = 255;
359 $h_start = $h_stop;
360 }
361 elsif ( _isa_color($start_color) && $stop_color eq 'white' ) {
362 $s_range = -255;
363 $s_start = 255;
364 $v_range = 0;
365 $v_start = 255;
366 }
367 elsif ( $start_color eq 'white' ) { # end black
368 $s_range = 0;
369 $s_start = 0;
370 $v_range = -255;
371 $v_start = 255;
372 }
373 elsif ( $stop_color eq 'white' ) { # start black
374 $s_range = 0;
375 $s_start = 0;
376 $v_range = 255;
377 $v_start = 0;
378 }
379 elsif ( _isa_color($start_color) ) { # end black
380 $s_range = 255;
381 $s_start = 0;
382 $v_range = 255;
383 $v_start = 0;
384 }
385 elsif ( _isa_color($stop_color) ) { # start black
386 $s_range = -255;
387 $s_start = 255;
388 $v_range = -255;
389 $v_start = 255;
390 }
391  
392 }
393  
394 # store gradient info
395 $self->h_range($h_range);
396 $self->h_start($h_start);
397 $self->s_start($s_start);
398 $self->v_start($v_start);
399 $self->s_range($s_range);
400 $self->v_range($v_range);
401  
402 # store score info
403 $self->peak_score_range($max - $min);
404 $self->min_peak_score($min);
405 $self->max_peak_score($max);
406  
407 # store color extremes
408 my @low_rgb = $self->HSVtoRGB(@$hsv_start);
409 my @high_rgb = $self->HSVtoRGB(@$hsv_stop);
410 $self->low_hsv($hsv_start);
411 $self->high_rgb(\@high_rgb);
412 $self->low_rgb(\@low_rgb);
413 return 1;
414 }
415  
416  
417 sub _isa_color {
418 my $color = shift;
419 return $color =~ /white|black|FFFFFF|000000/i ? 0 : 1;
420 }
421  
422 sub level { -1 }
423  
424 # Need to override this so we have a nice image map for overlayed peaks
425 sub boxes {
426 my $self = shift;
427 my($left,$top,$parent) = @_;
428  
429 return if $self->glyph_subtype eq 'density'; # No boxes for density plot
430 my @boxes = $self->SUPER::boxes(@_);
431
432 if (my $rects = $self->{peak_cache}) {
433 push @boxes,[@$_,$parent] foreach @$rects;
434 }
435 return wantarray ? @boxes : \@boxes;
436 }
437  
438  
439 # Modified and fused functions from wiggle_density.pm and wiggle_xyplot.pm
440 sub _draw_wigfile {
441 my $self = shift;
442 my $feature = shift;
443 my $wig = shift;
444  
445 $wig->smoothing($self->get_smoothing);
446 $wig->window($self->smooth_window);
447  
448 my ($gd,$left,$top) = @_;
449 my ($start,$end) = $self->effective_bounds($feature);
450  
451 if ($self->glyph_subtype eq 'density') {
452 my ($x1,$y1,$x2,$y2) = $self->bounds($left,$top);
453 $self->draw_segment($gd,
454 $start,$end,
455 $wig,$start,$end,
456 1,1,
457 $x1,$y1,$x2,$y2);
458 $self->Bio::Graphics::Glyph::xyplot::draw_label(@_) if $self->option('label');
459 $self->draw_description(@_) if $self->option('description');
460 } else {
461 my ($start,$end) = $self->effective_bounds($feature);
462 $self->wig($wig);
463 my $parts = $self->create_parts_for_dense_feature($wig,$start,$end);
464 $self->draw_plot($parts,@_);
465 }
466 }
467  
468 sub peaks {
469 my $self = shift;
470 return @{$self->{_peaks}} if $self->{_peaks};
471  
472 my $feature = $self->feature;
473 my $db = $feature->object_store;
474 my ($p_type) = eval{$feature->get_tag_values('peak_type')};
475  
476 unless ($db && $p_type) {
477 $self->{_peaks} = [];
478 return;
479 }
480  
481 my @peaks = $db->features(-seq_id => $feature->segment->ref,
482 -start => $self->panel->start,
483 -end => $self->panel->end,
484 -type => $p_type);
485  
486 $self->{_peaks} = \@peaks;
487 return @{$self->{_peaks}};
488 }
489  
490 1;
491  
492 =head1 NAME
493  
494 Bio::Graphics::Glyph::vista_plot - The "vista_plot" glyph
495  
496 =head1 SYNOPSIS
497  
498 See
499 L<Bio::Graphics::Glyph>, L<Bio::Graphics::Glyph::wiggle_xyplot> and L<Bio::Graphics::Glyph::heat_map>.
500  
501 =head1 DESCRIPTION
502  
503 This glyph draws peak calls (features with discreet boundaries,
504 i.e. putative transcription sites, over signal graph (wiggle_xyplot)
505 requires a special load gff file that uses attributes 'wigfile' and 'peak_type'
506  
507 B<Example:>
508  
509 2L chip_seq vista 5407 23011573 . . . Name=ChipSeq Exp 1;wigfile=SomeWigFile.wigdb;peak_type=binding_site:exp1
510  
511 The glyph will draw the wiggle file first, than overlay the peaks (if there are any)
512 over signal graph. Elsewhere in the GFF3 file, there should be one or more features
513 of type "binding_site:exp1", e.g.:
514  
515 2L exp1 binding_site 91934 92005 . . .
516  
517 Options like 'balloon hover' and 'link' are available to customize
518 interaction with peaks in detail view.
519  
520 B<BigWig support:>
521  
522 Supported bigwig format also requires another attribute to be supplied
523 in load gff file (fasta) which specifies sequence index file for the
524 organism in use. The data file should have the 'bw' extension - it is
525 used to detect the BigWig format by vista_plot
526  
527 3L chip_seq vista 1 24543530 . . . Name=ChipSeq Exp 2;wigfile=SomeBigWigFile.bw;peak_type=binding_site:exp2;fasta=YourOrganism.fasta
528  
529 Note that all attributes should be present in load gff, as the code currently does not handle situation when
530 only some of the attributes are in gff. To omit peak or signal drawing use "" (i.e. peak_type="")
531 In both cases, the stanza code will look the same (only essential parameters shown):
532  
533 [VISTA_PLOT]
534 feature = vista:chip_seq
535 glyph = vista_plot
536 label = 1
537 smoothing = mean
538 smoothing_window = 10
539 bump density = 250
540 autoscale = local
541 variance_band = 1
542 max_peak = 255
543 min_peak = 1
544 peakwidth = 3
545 start_color = lightgray
546 end_color = black
547 pos_color = blue
548 neg_color = orange
549 bgcolor = orange
550 alpha = 80
551 fgcolor = black
552 database = database_with_load_gff_data
553 box_subparts = 1
554 bicolor_pivot = min
555 key = VISTA plot
556  
557 =head1 OPTIONS
558  
559 Options are the same as for wiggle_xyplot and heat_map
560  
561 B<Additional parameters:>
562  
563 B<alpha>
564 set transparency for peak area.
565  
566 B<glyph_subtype>
567 Display only 'peaks', 'signal', 'density' or 'peaks+signal'.
568 Aliases for 'peaks+signal' include "both" and "vista".
569  
570 B<Recommended global settings:>
571  
572 for proper peak drawing transparency should be enabled
573 by setting
574 B<truecolors=1>
575 in I<GBrowse.conf> file
576  
577 =head1 BUGS
578  
579 Please report them.
580  
581 =head1 SEE ALSO
582  
583 L<Bio::Graphics::Panel>
584 L<Bio::Graphics::Glyph>
585 L<Bio::Graphics::Glyph::wiggle_xyplot>
586 L<Bio::Graphics::Glyph::heat_map>
587 L<GD>
588  
589 =head1 AUTHOR
590  
591 Peter Ruzanov pruzanov@oicr.on.ca
592  
593 Copyright (c) 2010 Ontario Institute for Cancer Research
594  
595 This package and its accompanying libraries is free software; you can
596 redistribute it and/or modify it under the terms of the GPL (either
597 version 1, or at your option, any later version) or the Artistic
598 License 2.0. Refer to LICENSE for the full license text. In addition,
599 please see DISCLAIMER.txt for disclaimers of warranty.
600  
601 =cut
602