| 1 | package Bio::Graphics::Glyph::hybrid_plot; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base qw(Bio::Graphics::Glyph::wiggle_xyplot); | ||
| 5 | use constant DEBUG=>0; | ||
| 6 | use constant NEGCOL=>"orange"; | ||
| 7 | use constant POSCOL=>"blue"; | ||
| 8 | our $VERSION = '1.0'; | ||
| 9 | |||
| 10 | sub my_options { | ||
| 11 | { | ||
| 12 | min_score => [ | ||
| 13 | 'integer', | ||
| 14 | undef, | ||
| 15 | "Minimum value of the signal graph feature's \"score\" attribute."], | ||
| 16 | max_score => [ | ||
| 17 | 'integer', | ||
| 18 | undef, | ||
| 19 | "Maximum value of the signal graph feature's \"score\" attribute."], | ||
| 20 | flip_sign => [ | ||
| 21 | 'boolean', | ||
| 22 | 0, | ||
| 23 | "Optionally flip the signal for wigfileB (if the scores are positive but we wish to paint the signal along negative y-axis)"] | ||
| 24 | }; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub my_description { | ||
| 28 | return <<END; | ||
| 29 | This glyph draws signal graph (wiggle_xyplot) using wiggle or BigWig files | ||
| 30 | requires a special load gff file that uses attributes 'wigfileA' and 'wigfileB' | ||
| 31 | For BigWig support it is also required to have 'fasta' attribute set to point | ||
| 32 | to a fasta file for your organism of interest | ||
| 33 | |||
| 34 | Example: | ||
| 35 | |||
| 36 | 2L test hybrid 5407 23011573 . . . Name=Experiment 1;wigfileA=SomeWigFile1.wigdb;wigfileB=SomeWigFile2.wigdb | ||
| 37 | |||
| 38 | END | ||
| 39 | } | ||
| 40 | |||
| 41 | #Checking the method for individual features (RNA-Seq reads) | ||
| 42 | sub _check_uni { | ||
| 43 | return shift->option('u_method') || 'match'; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | # Override height and pad functions (needed to correctly space features with different sources): | ||
| 48 | sub height { | ||
| 49 | my $self = shift; | ||
| 50 | my $h = $self->SUPER::height; | ||
| 51 | return $self->feature->method eq $self->_check_uni ? 3 : $h; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub pad_top { | ||
| 55 | my $self = shift; | ||
| 56 | return $self->feature->method eq $self->_check_uni ? 0 : 4; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub pad_bottom { | ||
| 60 | my $self = shift; | ||
| 61 | return $self->feature->method eq $self->_check_uni ? 0 : 4; | ||
| 62 | } | ||
| 63 | |||
| 64 | # we override the draw method so that it dynamically creates the parts needed | ||
| 65 | # from the wig file rather than trying to fetch them from the database | ||
| 66 | sub draw { | ||
| 67 | |||
| 68 | my $self = shift; | ||
| 69 | my ($gd,$dx,$dy) = @_; | ||
| 70 | my ($left,$top,$right,$bottom) = $self->calculate_boundaries($dx,$dy); | ||
| 71 | my $height = $bottom - $top; | ||
| 72 | my $feature = $self->feature; | ||
| 73 | my $set_flip = $self->option('flip_sign') || 0; | ||
| 74 | |||
| 75 | #Draw individual features for reads (unlike wiggle features reads will have scores) | ||
| 76 | my $t_id = $feature->method; | ||
| 77 | if($t_id && $t_id eq $self->_check_uni){return Bio::Graphics::Glyph::generic::draw_component($self,@_);} | ||
| 78 | |||
| 79 | #Draw multiple graph if we don't have a score | ||
| 80 | my @wiggles = $self->get_wiggles($feature); | ||
| 81 | |||
| 82 | my ($fasta) = $feature->get_tag_values('fasta'); | ||
| 83 | my($scale,$y_origin,$min_score,$max_score); | ||
| 84 | |||
| 85 | $self->panel->startGroup($gd); | ||
| 86 | |||
| 87 | #Depending on what we have (wiggle or BigWig) pick the way to paint the signal graph | ||
| 88 | for(my $w = 0; $w < @wiggles; $w++){ | ||
| 89 | if ($w > 0) { | ||
| 90 | $self->configure('bgcolor', NEGCOL); | ||
| 91 | $self->configure('no_grid', 1); | ||
| 92 | } else { | ||
| 93 | $self->configure('bgcolor', POSCOL); | ||
| 94 | } | ||
| 95 | if ($wiggles[$w] =~ /\.wi\w{1,3}$/) { | ||
| 96 | $self->draw_wigfile($feature,$wiggles[$w],@_); | ||
| 97 | } elsif ($wiggles[$w] =~ /\.bw$/) { | ||
| 98 | my $flip = ($w > 0 && $set_flip) ? -1 : 1; | ||
| 99 | eval "require Bio::DB::BigWig;1" or die $@; | ||
| 100 | eval "require Bio::DB::Sam; 1" or die $@; | ||
| 101 | my @args = (-bigwig => "$wiggles[$w]"); | ||
| 102 | push @args,(-fasta => Bio::DB::Sam::Fai->open($fasta)) if $fasta; | ||
| 103 | my $wig = Bio::DB::BigWig->new(@args); | ||
| 104 | my ($summary) = $wig->features(-seq_id => $feature->segment->ref, | ||
| 105 | -start => $self->panel->start, | ||
| 106 | -end => $self->panel->end, | ||
| 107 | -type => 'summary'); | ||
| 108 | my $stats = $summary->statistical_summary($self->width); | ||
| 109 | my $interval_method = $self->option('interval_method') || 'mean'; | ||
| 110 | my @vals; | ||
| 111 | if ($interval_method eq 'mean') { | ||
| 112 | @vals = map {$_->{validCount} ? $_->{sumData}/$_->{validCount} * $flip : undef} @$stats; | ||
| 113 | } | ||
| 114 | elsif ($interval_method eq 'sum') { | ||
| 115 | @vals = map {$_->{validCount} ? $_->{sumData} * $flip : undef} @$stats; | ||
| 116 | } | ||
| 117 | elsif ($interval_method eq 'min') { | ||
| 118 | @vals = map {$_->{validCount} ? $_->{minVal} * $flip : undef} @$stats; | ||
| 119 | } | ||
| 120 | elsif ($interval_method eq 'max') { | ||
| 121 | @vals = map {$_->{validCount} ? $_->{maxVal} * $flip : undef} @$stats; | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | warn "unrecognized interval method $interval_method!"; | ||
| 125 | } | ||
| 126 | $self->_draw_coverage($summary,\@vals,@_); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | sub get_wiggles { | ||
| 132 | my $self = shift; | ||
| 133 | my $feature = shift; | ||
| 134 | my @wiggles; | ||
| 135 | foreach ('A'..'Z') { | ||
| 136 | my $filename = 'wigfile'.$_; | ||
| 137 | my ($wiggle) = $feature->get_tag_values('wigfile'.$_); | ||
| 138 | push (@wiggles, $wiggle) if $wiggle; | ||
| 139 | } | ||
| 140 | return @wiggles; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub minmax { | ||
| 144 | my $self = shift; | ||
| 145 | my $parts = shift; | ||
| 146 | |||
| 147 | my $autoscale = $self->option('autoscale') || 'local'; | ||
| 148 | my $set_flip = $self->option('flip_sign') || 0; | ||
| 149 | |||
| 150 | my $min_score = $self->min_score unless $autoscale eq 'z_score'; | ||
| 151 | my $max_score = $self->max_score unless $autoscale eq 'z_score'; | ||
| 152 | |||
| 153 | my $do_min = !defined $min_score; | ||
| 154 | my $do_max = !defined $max_score; | ||
| 155 | |||
| 156 | my @wiggles = $self->get_wiggles($self->feature); | ||
| 157 | my ($min,$max,$mean,$stdev); | ||
| 158 | my @args = (-seq_id => (eval{$self->feature->segment->ref}||''), | ||
| 159 | -start => $self->panel->start, | ||
| 160 | -end => $self->panel->end, | ||
| 161 | -type => 'summary'); | ||
| 162 | |||
| 163 | for my $w (@wiggles) { | ||
| 164 | my ($a,$b,$c,$d); | ||
| 165 | if ($w =~ /\.bw$/) { | ||
| 166 | eval "require Bio::DB::BigWig;1" or die $@; | ||
| 167 | my $wig = Bio::DB::BigWig->new(-bigwig=>$w) or next; | ||
| 168 | ($a,$b,$c,$d) = $self->bigwig_stats($autoscale,$wig->features(@args)); | ||
| 169 | } elsif ($w =~ /\.wi\w{1,3}$/) { | ||
| 170 | eval "require Bio::Graphics::Wiggle;1" or die $@; | ||
| 171 | my $wig = Bio::Graphics::Wiggle->new($w); | ||
| 172 | ($a,$b,$c,$d) = $self->wig_stats($autoscale,$wig); | ||
| 173 | } | ||
| 174 | $min = $a if !defined $min || $min > $a; | ||
| 175 | $max = $b if !defined $max || $max < $b; | ||
| 176 | $mean += $c; | ||
| 177 | $stdev += $d**2; | ||
| 178 | } | ||
| 179 | $stdev = sqrt($stdev); | ||
| 180 | $min = $max * -1 if ($set_flip); | ||
| 181 | |||
| 182 | $min_score = $min if $do_min; | ||
| 183 | $max_score = $max if $do_max; | ||
| 184 | return $self->sanity_check($min_score,$max_score,$mean,$stdev); | ||
| 185 | } | ||
| 186 | |||
| 187 | 1; | ||
| 188 | __END__ |