| 1 | package Bio::Graphics::Glyph::wiggle_box; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base qw(Bio::Graphics::Glyph::box Bio::Graphics::Glyph::smoothing); | ||
| 5 | use File::Spec; | ||
| 6 | |||
| 7 | sub draw { | ||
| 8 | my $self = shift; | ||
| 9 | my ($gd,$left,$top,$partno,$total_parts) = @_; | ||
| 10 | my $feature = $self->feature; | ||
| 11 | |||
| 12 | my $drawnit; | ||
| 13 | |||
| 14 | my ($wigfile) = eval{$feature->get_tag_values('wigfile')}; | ||
| 15 | if ($wigfile) { | ||
| 16 | $self->draw_wigfile($feature,$self->rel2abs($wigfile),@_); | ||
| 17 | $drawnit++; | ||
| 18 | } | ||
| 19 | |||
| 20 | my ($wigdata) = eval{$feature->get_tag_values('wigdata')}; | ||
| 21 | if ($wigdata) { | ||
| 22 | $self->draw_wigdata($feature,$wigdata,@_); | ||
| 23 | $drawnit++; | ||
| 24 | } | ||
| 25 | |||
| 26 | my ($coverage) = eval{$feature->get_tag_values('coverage')}; | ||
| 27 | if ($coverage) { | ||
| 28 | $self->draw_coverage($feature,$coverage,@_); | ||
| 29 | $drawnit++; | ||
| 30 | } | ||
| 31 | # support for BigWig/BigBed | ||
| 32 | if ($feature->can('statistical_summary')) { | ||
| 33 | my $stats = $feature->statistical_summary($self->width); | ||
| 34 | my @vals = map {$_->{validCount} ? $_->{sumData}/$_->{validCount}:0} @$stats; | ||
| 35 | $self->draw_coverage($feature,\@vals,@_); | ||
| 36 | $drawnit++; | ||
| 37 | } | ||
| 38 | |||
| 39 | if ($drawnit) { | ||
| 40 | $self->draw_label(@_) if $self->option('label'); | ||
| 41 | $self->draw_description(@_) if $self->option('description'); | ||
| 42 | return; | ||
| 43 | } | ||
| 44 | |||
| 45 | return $self->SUPER::draw(@_); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub wig { | ||
| 49 | my $self = shift; | ||
| 50 | my $d = $self->{wig}; | ||
| 51 | $self->{wig} = shift if @_; | ||
| 52 | $d; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub draw_wigdata { | ||
| 56 | my $self = shift; | ||
| 57 | my $feature = shift; | ||
| 58 | my $data = shift; | ||
| 59 | |||
| 60 | eval "require MIME::Base64" | ||
| 61 | unless MIME::Base64->can('decode_base64'); | ||
| 62 | my $unencoded_data = MIME::Base64::decode_base64($data); | ||
| 63 | |||
| 64 | my $wig = eval { Bio::Graphics::Wiggle->new() }; | ||
| 65 | unless ($wig) { | ||
| 66 | warn $@; | ||
| 67 | return $self->SUPER::draw(@_); | ||
| 68 | } | ||
| 69 | |||
| 70 | $wig->import_from_wif($unencoded_data); | ||
| 71 | |||
| 72 | $self->wig($wig); | ||
| 73 | $self->_draw_wigfile($feature,$wig,@_); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub draw_wigfile { | ||
| 77 | my $self = shift; | ||
| 78 | my $feature = shift; | ||
| 79 | my $wigfile = shift; | ||
| 80 | |||
| 81 | eval "require Bio::Graphics::Wiggle" unless Bio::Graphics::Wiggle->can('new'); | ||
| 82 | my $wig = Bio::Graphics::Wiggle->new($wigfile) or die; | ||
| 83 | $self->wig($wig); | ||
| 84 | |||
| 85 | $self->_draw_wigfile($feature,$wig,@_); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _draw_wigfile { | ||
| 89 | my $self = shift; | ||
| 90 | my $feature = shift; | ||
| 91 | my $wig = shift; | ||
| 92 | my ($gd,$left,$top) = @_; | ||
| 93 | |||
| 94 | my $start = $self->smooth_start; | ||
| 95 | my $end = $self->smooth_end; | ||
| 96 | |||
| 97 | my ($x1,$y1,$x2,$y2) = $self->bounds($left,$top); | ||
| 98 | $self->draw_segment($gd, | ||
| 99 | $start,$end, | ||
| 100 | $wig,$start,$end, | ||
| 101 | 1,1, | ||
| 102 | $x1,$y1,$x2,$y2); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub draw_coverage { | ||
| 106 | my $self = shift; | ||
| 107 | my $feature = shift; | ||
| 108 | my $array = shift; | ||
| 109 | |||
| 110 | $array = [split ',',$array] unless ref $array; | ||
| 111 | my ($gd,$left,$top) = @_; | ||
| 112 | |||
| 113 | my ($start,$end) = $self->effective_bounds($feature); | ||
| 114 | my $length = $end - $start + 1; | ||
| 115 | my $bases_per_bin = ($end-$start)/@$array; | ||
| 116 | my @parts; | ||
| 117 | my $samples = $length < $self->panel->width ? $length | ||
| 118 | : $self->panel->width; | ||
| 119 | my $samples_per_base = $samples/$length; | ||
| 120 | |||
| 121 | for (my $i=0;$i<$samples;$i++) { | ||
| 122 | my $offset = $i/$samples_per_base; | ||
| 123 | my $v = $array->[$offset/$bases_per_bin]; | ||
| 124 | push @parts,$v; | ||
| 125 | } | ||
| 126 | my ($x1,$y1,$x2,$y2) = $self->bounds($left,$top); | ||
| 127 | $self->draw_segment($gd, | ||
| 128 | $start,$end, | ||
| 129 | \@parts, | ||
| 130 | $start,$end, | ||
| 131 | 1,1, | ||
| 132 | $x1,$y1,$x2,$y2); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub effective_bounds { # copied from wiggle_xyplot -- ouch! | ||
| 136 | my $self = shift; | ||
| 137 | my $feature = shift; | ||
| 138 | my $panel_start = $self->panel->start; | ||
| 139 | my $panel_end = $self->panel->end; | ||
| 140 | my $start = $feature->start>$panel_start | ||
| 141 | ? $feature->start | ||
| 142 | : $panel_start; | ||
| 143 | my $end = $feature->end<$panel_end | ||
| 144 | ? $feature->end | ||
| 145 | : $panel_end; | ||
| 146 | return ($start,$end); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub draw_segment { | ||
| 150 | my $self = shift; | ||
| 151 | my ($gd, | ||
| 152 | $start,$end, | ||
| 153 | $seg_data, | ||
| 154 | $seg_start,$seg_end, | ||
| 155 | $step,$span, | ||
| 156 | $x1,$y1,$x2,$y2) = @_; | ||
| 157 | |||
| 158 | # clip, because wig files do no clipping | ||
| 159 | $seg_start = $start if $seg_start < $start; | ||
| 160 | $seg_end = $end if $seg_end > $end; | ||
| 161 | |||
| 162 | # figure out where we're going to start | ||
| 163 | my $scale = $self->scale; # pixels per base pair | ||
| 164 | my $pixels_per_span = $scale * $span + 1; | ||
| 165 | my $pixels_per_step = 1; | ||
| 166 | my $length = $end-$start+1; | ||
| 167 | |||
| 168 | # if the feature starts before the data starts, then we need to draw | ||
| 169 | # a line indicating missing data (this only happens if something went | ||
| 170 | # wrong upstream) | ||
| 171 | if ($seg_start > $start) { | ||
| 172 | my $terminus = $self->map_pt($seg_start); | ||
| 173 | $start = $seg_start; | ||
| 174 | $x1 = $terminus; | ||
| 175 | } | ||
| 176 | # if the data ends before the feature ends, then we need to draw | ||
| 177 | # a line indicating missing data (this only happens if something went | ||
| 178 | # wrong upstream) | ||
| 179 | if ($seg_end < $end) { | ||
| 180 | my $terminus = $self->map_pt($seg_end); | ||
| 181 | $end = $seg_end; | ||
| 182 | $x2 = $terminus; | ||
| 183 | } | ||
| 184 | |||
| 185 | return unless $start < $end; | ||
| 186 | |||
| 187 | # get data values across the area | ||
| 188 | my $samples = $length < $self->panel->width ? $length : $self->panel->width; | ||
| 189 | my $data = ref $seg_data eq 'ARRAY' ? $seg_data | ||
| 190 | : $seg_data->values($start,$end,$samples); | ||
| 191 | |||
| 192 | # scale the glyph if the data end before the panel does | ||
| 193 | my $data_width = $end - $start; | ||
| 194 | my $data_width_ratio; | ||
| 195 | if ($data_width < $self->panel->length) { | ||
| 196 | $data_width_ratio = $data_width/$self->panel->length; | ||
| 197 | } | ||
| 198 | else { | ||
| 199 | $data_width_ratio = 1; | ||
| 200 | } | ||
| 201 | |||
| 202 | return unless $data && ref $data && @$data > 0 && grep {$_} @$data; | ||
| 203 | |||
| 204 | # allocate colors | ||
| 205 | my $bg_idx = $self->panel->translate_color($self->panel->rgb($self->bgcolor)); | ||
| 206 | my $fg_idx = $self->panel->translate_color($self->panel->rgb($self->fgcolor)) || $bg_idx; | ||
| 207 | |||
| 208 | $pixels_per_step = $scale * $step; | ||
| 209 | $pixels_per_step = 1 if $pixels_per_step < 1; | ||
| 210 | my $datapoints_per_base = @$data/$length; | ||
| 211 | my $pixels_per_datapoint = $self->panel->width/@$data * $data_width_ratio; | ||
| 212 | |||
| 213 | my $xstart; | ||
| 214 | for (my $i = 0; $i <= @$data ; $i++) { | ||
| 215 | $xstart ||= $x1 + $pixels_per_datapoint * $i if $data->[$i]; | ||
| 216 | # trigger to draw the previous box is empty space of the end of the stack | ||
| 217 | if (!$data->[$i] || ($i+1 == @$data)) { | ||
| 218 | $xstart || next; | ||
| 219 | my $xend = $x1 + $pixels_per_datapoint * $i; | ||
| 220 | $self->filled_box($gd,$xstart,$y1,$xend,$y2,$bg_idx,$fg_idx); | ||
| 221 | undef $xstart; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | |||
| 228 | sub rel2abs { | ||
| 229 | my $self = shift; | ||
| 230 | my $wig = shift; | ||
| 231 | my $path = $self->option('basedir'); | ||
| 232 | return File::Spec->rel2abs($wig,$path); | ||
| 233 | } | ||
| 234 | |||
| 235 | 1; | ||
| 236 | |||
| 237 | __END__ |