| 1 | package Bio::Graphics::Glyph::wiggle_data; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base qw(Bio::Graphics::Glyph::minmax); | ||
| 5 | use File::Spec; | ||
| 6 | use Data::Dumper; | ||
| 7 | sub minmax { | ||
| 8 | my $self = shift; | ||
| 9 | my $parts = shift; | ||
| 10 | |||
| 11 | my $autoscale = $self->option('autoscale') || 'local'; | ||
| 12 | |||
| 13 | my $min_score = $self->min_score unless $autoscale eq 'z_score'; | ||
| 14 | my $max_score = $self->max_score unless $autoscale eq 'z_score'; | ||
| 15 | |||
| 16 | my $do_min = !defined $min_score; | ||
| 17 | my $do_max = !defined $max_score; | ||
| 18 | |||
| 19 | if (@$parts && $self->feature->can('statistical_summary')) { | ||
| 20 | my ($min,$max,$mean,$stdev) = eval {$self->bigwig_stats($autoscale,$self->feature)}; | ||
| 21 | $min_score = $min if $do_min; | ||
| 22 | $max_score = $max if $do_max; | ||
| 23 | return $self->sanity_check($min_score,$max_score,$mean,$stdev); | ||
| 24 | } | ||
| 25 | |||
| 26 | elsif (eval {$self->wig}) { | ||
| 27 | if (my ($min,$max,$mean,$stdev) = eval{$self->wig_stats($autoscale,$self->wig)}) { | ||
| 28 | $min_score = $min if $do_min; | ||
| 29 | $max_score = $max if $do_max; | ||
| 30 | return $self->sanity_check($min_score,$max_score,$mean,$stdev); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | if ($do_min or $do_max) { | ||
| 35 | my $first = $parts->[0]; | ||
| 36 | for my $part (@$parts) { | ||
| 37 | my $s = ref $part ? $part->[2] : $part; | ||
| 38 | next unless defined $s; | ||
| 39 | $min_score = $s if $do_min && (!defined $min_score or $s < $min_score); | ||
| 40 | $max_score = $s if $do_max && (!defined $max_score or $s > $max_score); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | return $self->sanity_check($min_score,$max_score); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub bigwig_stats { | ||
| 47 | my $self = shift; | ||
| 48 | my ($autoscale,$feature) = @_; | ||
| 49 | my $s; | ||
| 50 | if ($autoscale =~ /global/ or $autoscale eq 'z_score') { | ||
| 51 | $s = $feature->global_stats; | ||
| 52 | } elsif ($autoscale eq 'chromosome') { | ||
| 53 | $s = $feature->chr_stats; | ||
| 54 | } else { | ||
| 55 | $s = $feature->score; | ||
| 56 | } | ||
| 57 | return $self->clip($autoscale, | ||
| 58 | $s->{minVal},$s->{maxVal},Bio::DB::BigWig::binMean($s),Bio::DB::BigWig::binStdev($s)); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub wig_stats { | ||
| 62 | my $self = shift; | ||
| 63 | my ($autoscale,$wig) = @_; | ||
| 64 | |||
| 65 | if ($autoscale =~ /global|chromosome|z_score/) { | ||
| 66 | my $min_score = $wig->min; | ||
| 67 | my $max_score = $wig->max; | ||
| 68 | my $mean = $wig->mean; | ||
| 69 | my $stdev = $wig->stdev; | ||
| 70 | return $self->clip($autoscale,$min_score,$max_score,$mean,$stdev); | ||
| 71 | } else { | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub clip { | ||
| 77 | my $self = shift; | ||
| 78 | my ($autoscale,$min,$max,$mean,$stdev) = @_; | ||
| 79 | return ($min,$max,$mean,$stdev) unless $autoscale =~ /clipped/; | ||
| 80 | my $fold = $self->z_score_bound; | ||
| 81 | my $clip_max = $mean + $stdev*$fold; | ||
| 82 | my $clip_min = $mean - $stdev*$fold; | ||
| 83 | $min = $clip_min if $min < $clip_min; | ||
| 84 | $max = $clip_max if $max > $clip_max; | ||
| 85 | return ($min,$max,$mean,$stdev); | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | sub z_score_bound { | ||
| 90 | my $self = shift; | ||
| 91 | return $self->option('z_score_bound') || 4; | ||
| 92 | } | ||
| 93 | |||
| 94 | # change the scaling of the data points if z-score autoscaling requested | ||
| 95 | sub rescale { | ||
| 96 | my $self = shift; | ||
| 97 | my $points = shift; | ||
| 98 | return $points unless $self->option('autoscale') eq 'z_score'; | ||
| 99 | |||
| 100 | my ($min,$max,$mean,$stdev) = $self->minmax($points); | ||
| 101 | foreach (@$points) { | ||
| 102 | $_ = ($_ - $mean) / $stdev; | ||
| 103 | } | ||
| 104 | return $points; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub global_mean_and_variance { | ||
| 108 | my $self = shift; | ||
| 109 | if (my $wig = $self->wig) { | ||
| 110 | return ($wig->mean,$wig->stdev); | ||
| 111 | } elsif ($self->feature->can('global_mean')) { | ||
| 112 | my $f = $self->feature; | ||
| 113 | return ($f->global_mean,$f->global_stdev); | ||
| 114 | } | ||
| 115 | return; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub global_min_max { | ||
| 119 | my $self = shift; | ||
| 120 | if (my $wig = $self->wig) { | ||
| 121 | return ($wig->min,$wig->max); | ||
| 122 | } elsif (my $stats = eval {$self->feature->global_stats}) { | ||
| 123 | return ($stats->{minVal},$stats->{maxVal}); | ||
| 124 | } | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | sub series_stdev { | ||
| 128 | my $self = shift; | ||
| 129 | my ($mean,$stdev) = $self->global_mean_and_variance; | ||
| 130 | return $stdev; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub series_mean { | ||
| 134 | my $self = shift; | ||
| 135 | my ($mean) = $self->global_mean_and_variance; | ||
| 136 | return $mean; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub series_min { | ||
| 140 | my $self = shift; | ||
| 141 | return ($self->global_min_max)[0]; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub series_max { | ||
| 145 | my $self = shift; | ||
| 146 | return ($self->global_min_max)[1]; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub wig { | ||
| 150 | my $self = shift; | ||
| 151 | my $d = $self->{wig}; | ||
| 152 | $self->{wig} = shift if @_; | ||
| 153 | $d; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub datatype { | ||
| 157 | my $self = shift; | ||
| 158 | my $feature = $self->feature; | ||
| 159 | |||
| 160 | my ($tag,$value); | ||
| 161 | for my $t ('wigfile','wigdata','densefile','coverage') { | ||
| 162 | if (my ($v) = eval{$feature->get_tag_values($t)}) { | ||
| 163 | $value = $v; | ||
| 164 | $tag = $t; | ||
| 165 | last; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | if (!$value && $feature->can('statistical_summary')) { | ||
| 169 | $tag = 'statistical_summary'; | ||
| 170 | $value = eval{$feature->statistical_summary}; | ||
| 171 | } | ||
| 172 | |||
| 173 | $tag ||= 'generic'; | ||
| 174 | |||
| 175 | return wantarray ? ($tag,$value) : $tag; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub get_parts { | ||
| 179 | my $self = shift; | ||
| 180 | my $feature = $self->feature; | ||
| 181 | my ($start,$end) = $self->effective_bounds($feature); | ||
| 182 | my ($datatype,$data) = $self->datatype; | ||
| 183 | return $self->subsample($data,$start,$end) if $datatype eq 'wigdata'; | ||
| 184 | return $self->create_parts_from_wigfile($data,$start,$end) if $datatype eq 'wigfile'; | ||
| 185 | return $self->create_parts_for_dense_feature($data,$start,$end) if $datatype eq 'densefile'; | ||
| 186 | return $self->create_parts_from_coverage($data,$start,$end) if $datatype eq 'coverage'; | ||
| 187 | return $self->create_parts_from_summary($data,$start,$end) if $datatype eq 'statistical_summary'; | ||
| 188 | return []; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub effective_bounds { | ||
| 192 | my $self = shift; | ||
| 193 | my $feature = shift; | ||
| 194 | my $panel_start = $self->panel->start; | ||
| 195 | my $panel_end = $self->panel->end; | ||
| 196 | my $start = $feature->start>$panel_start | ||
| 197 | ? $feature->start | ||
| 198 | : $panel_start; | ||
| 199 | my $end = $feature->end<$panel_end | ||
| 200 | ? $feature->end | ||
| 201 | : $panel_end; | ||
| 202 | return ($start,$end); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub create_parts_for_dense_feature { | ||
| 206 | my $self = shift; | ||
| 207 | my ($dense,$start,$end) = @_; | ||
| 208 | |||
| 209 | my $span = $self->scale> 1 ? $end - $start : $self->width; | ||
| 210 | my $data = $dense->values($start,$end,$span); | ||
| 211 | my $points_per_span = ($end-$start+1)/$span; | ||
| 212 | my @parts; | ||
| 213 | |||
| 214 | for (my $i=0; $i<$span;$i++) { | ||
| 215 | my $offset = $i * $points_per_span; | ||
| 216 | my $value = shift @$data; | ||
| 217 | next unless defined $value; | ||
| 218 | push @parts,[$start + int($i * $points_per_span), | ||
| 219 | $start + int($i * $points_per_span), | ||
| 220 | $value]; | ||
| 221 | } | ||
| 222 | return \@parts; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub create_parts_from_coverage { | ||
| 226 | my $self = shift; | ||
| 227 | my ($array,$start,$end) = @_; | ||
| 228 | $array = [split ',',$array] unless ref $array; | ||
| 229 | return unless @$array; | ||
| 230 | |||
| 231 | my $bases_per_bin = ($end-$start)/@$array; | ||
| 232 | my $pixels_per_base = $self->scale; | ||
| 233 | my @parts; | ||
| 234 | for (my $pixel=0;$pixel<$self->width;$pixel++) { | ||
| 235 | my $offset = $pixel/$pixels_per_base; | ||
| 236 | my $s = $start + $offset; | ||
| 237 | my $e = $s+1; # fill in gaps | ||
| 238 | my $v = $array->[$offset/$bases_per_bin]; | ||
| 239 | push @parts,[$s,$s,$v]; | ||
| 240 | } | ||
| 241 | return \@parts; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub create_parts_from_summary { | ||
| 245 | my $self = shift; | ||
| 246 | my ($stats,$start,$end) = @_; | ||
| 247 | $stats ||= []; | ||
| 248 | my $interval_method = $self->option('interval_method') || 'mean'; | ||
| 249 | my @vals; | ||
| 250 | if ($interval_method eq 'mean') { | ||
| 251 | @vals = map {$_->{validCount} ? $_->{sumData}/$_->{validCount} : undef} @$stats; | ||
| 252 | } | ||
| 253 | elsif ($interval_method eq 'sum') { | ||
| 254 | @vals = map {$_->{validCount} ? $_->{sumData} : undef} @$stats; | ||
| 255 | } | ||
| 256 | elsif ($interval_method eq 'min') { | ||
| 257 | @vals = map {$_->{validCount} ? $_->{minVal} : undef} @$stats; | ||
| 258 | } | ||
| 259 | elsif ($interval_method eq 'max') { | ||
| 260 | @vals = map {$_->{validCount} ? $_->{maxVal} : undef} @$stats; | ||
| 261 | } | ||
| 262 | else { | ||
| 263 | warn "unrecognized interval method $interval_method!"; | ||
| 264 | } | ||
| 265 | return \@vals; | ||
| 266 | } | ||
| 267 | |||
| 268 | sub create_parts_from_wigfile { | ||
| 269 | my $self = shift; | ||
| 270 | my ($path,$start,$end) = @_; | ||
| 271 | if (ref $path && $path->isa('Bio::Graphics::Wiggle')) { | ||
| 272 | return $self->create_parts_for_dense_feature($path,$start,$end); | ||
| 273 | } | ||
| 274 | $path = $self->rel2abs($path); | ||
| 275 | if ($path =~ /\.wi\w{1,3}$/) { | ||
| 276 | eval "require Bio::Graphics::Wiggle" unless Bio::Graphics::Wiggle->can('new'); | ||
| 277 | my $wig = eval { Bio::Graphics::Wiggle->new($path)}; | ||
| 278 | return $self->create_parts_for_dense_feature($wig,$start,$end); | ||
| 279 | } elsif ($path =~ /\.bw$/i) { | ||
| 280 | eval "use Bio::DB::BigWig" unless Bio::DB::BigWig->can('new'); | ||
| 281 | my $bigwig = Bio::DB::BigWig->new(-bigwig=>$path); | ||
| 282 | my ($summary) = $bigwig->features(-seq_id => $self->feature->segment->ref, | ||
| 283 | -start => $start, | ||
| 284 | -end => $end, | ||
| 285 | -type => 'summary'); | ||
| 286 | return $self->create_parts_from_summary($summary->statistical_summary($self->width)); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | sub subsample { | ||
| 291 | my $self = shift; | ||
| 292 | my ($data,$start,$end) = @_; | ||
| 293 | my $span = $self->scale > 1 ? $end - $start | ||
| 294 | : $self->width; | ||
| 295 | my $points_per_span = ($end-$start+1)/$span; | ||
| 296 | my @parts; | ||
| 297 | for (my $i=0; $i<$span;$i++) { | ||
| 298 | my $offset = $i * $points_per_span; | ||
| 299 | my $value = $data->[$offset + $points_per_span/2]; | ||
| 300 | push @parts,[$start + int($i*$points_per_span), | ||
| 301 | $start + int($i*$points_per_span), | ||
| 302 | $value]; | ||
| 303 | } | ||
| 304 | return \@parts; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub rel2abs { | ||
| 308 | my $self = shift; | ||
| 309 | my $wig = shift; | ||
| 310 | return $wig if ref $wig; | ||
| 311 | my $path = $self->option('basedir'); | ||
| 312 | return File::Spec->rel2abs($wig,$path); | ||
| 313 | } | ||
| 314 | |||
| 315 | sub draw { | ||
| 316 | my $self = shift; | ||
| 317 | my ($gd,$dx,$dy) = @_; | ||
| 318 | |||
| 319 | my $feature = $self->feature; | ||
| 320 | my $datatype = $self->datatype; | ||
| 321 | |||
| 322 | my $retval; | ||
| 323 | $retval = $self->draw_wigfile($feature,@_) if $datatype eq 'wigfile'; | ||
| 324 | $retval = $self->draw_wigdata($feature,@_) if $datatype eq 'wigdata'; | ||
| 325 | $retval = $self->draw_densefile($feature,@_) if $datatype eq 'densefile'; | ||
| 326 | $retval = $self->draw_coverage($feature,@_) if $datatype eq 'coverage'; | ||
| 327 | $retval = $self->draw_statistical_summary($feature,@_) if $datatype eq 'statistical_summary'; | ||
| 328 | $retval = $self->SUPER::draw(@_) if $datatype eq 'generic'; | ||
| 329 | |||
| 330 | return $retval; | ||
| 331 | } | ||
| 332 | |||
| 333 | sub draw_wigfile { | ||
| 334 | my $self = shift; | ||
| 335 | my $feature = shift; | ||
| 336 | |||
| 337 | my ($wigfile) = eval{$feature->get_tag_values('wigfile')}; | ||
| 338 | $wigfile = $self->rel2abs($wigfile); | ||
| 339 | |||
| 340 | eval "require Bio::Graphics::Wiggle" unless Bio::Graphics::Wiggle->can('new'); | ||
| 341 | my $wig = ref $wigfile && $wigfile->isa('Bio::Graphics::Wiggle') | ||
| 342 | ? $wigfile | ||
| 343 | : eval { Bio::Graphics::Wiggle->new($wigfile) }; | ||
| 344 | unless ($wig) { | ||
| 345 | warn $@; | ||
| 346 | return $self->SUPER::draw(@_); | ||
| 347 | } | ||
| 348 | $self->_draw_wigfile($feature,$wigfile,@_); | ||
| 349 | } | ||
| 350 | |||
| 351 | sub draw_wigdata { | ||
| 352 | my $self = shift; | ||
| 353 | my $feature = shift; | ||
| 354 | |||
| 355 | my ($data) = eval{$feature->get_tag_values('wigdata')}; | ||
| 356 | |||
| 357 | if (ref $data eq 'ARRAY') { | ||
| 358 | my ($start,$end) = $self->effective_bounds($feature); | ||
| 359 | my $parts = $self->subsample($data,$start,$end); | ||
| 360 | $self->draw_plot($parts,@_); | ||
| 361 | } | ||
| 362 | |||
| 363 | else { | ||
| 364 | my $wig = eval { Bio::Graphics::Wiggle->new() }; | ||
| 365 | unless ($wig) { | ||
| 366 | warn $@; | ||
| 367 | return $self->SUPER::draw(@_); | ||
| 368 | } | ||
| 369 | |||
| 370 | $wig->import_from_wif64($data); | ||
| 371 | $self->_draw_wigfile($feature,$wig,@_); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | |||
| 375 | sub draw_densefile { | ||
| 376 | my $self = shift; | ||
| 377 | my $feature = shift; | ||
| 378 | |||
| 379 | my ($densefile) = eval{$feature->get_tag_values('densefile')}; | ||
| 380 | $densefile = $self->rel2abs($densefile); | ||
| 381 | |||
| 382 | my ($denseoffset) = eval{$feature->get_tag_values('denseoffset')}; | ||
| 383 | my ($densesize) = eval{$feature->get_tag_values('densesize')}; | ||
| 384 | $denseoffset ||= 0; | ||
| 385 | $densesize ||= 1; | ||
| 386 | |||
| 387 | my $smoothing = $self->get_smoothing; | ||
| 388 | my $smooth_window = $self->smooth_window; | ||
| 389 | my $start = $self->smooth_start; | ||
| 390 | my $end = $self->smooth_end; | ||
| 391 | |||
| 392 | my $fh = IO::File->new($densefile) or die "can't open $densefile: $!"; | ||
| 393 | eval "require Bio::Graphics::DenseFeature" unless Bio::Graphics::DenseFeature->can('new'); | ||
| 394 | my $dense = Bio::Graphics::DenseFeature->new(-fh=>$fh, | ||
| 395 | -fh_offset => $denseoffset, | ||
| 396 | -start => $feature->start, | ||
| 397 | -smooth => $smoothing, | ||
| 398 | -recsize => $densesize, | ||
| 399 | -window => $smooth_window, | ||
| 400 | ) or die "Can't initialize DenseFeature: $!"; | ||
| 401 | my $parts = $self->get_parts; | ||
| 402 | $self->draw_plot($parts); | ||
| 403 | } | ||
| 404 | |||
| 405 | sub draw_coverage { | ||
| 406 | my $self = shift; | ||
| 407 | my $feature = shift; | ||
| 408 | |||
| 409 | my ($array) = eval{$feature->get_tag_values('coverage')}; | ||
| 410 | $self->_draw_coverage($feature,$array,@_); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub draw_statistical_summary { | ||
| 414 | my $self = shift; | ||
| 415 | my $feature = shift; | ||
| 416 | my $stats = $feature->statistical_summary($self->width); | ||
| 417 | $stats ||= []; | ||
| 418 | my $interval_method = $self->option('interval_method') || 'mean'; | ||
| 419 | my @vals; | ||
| 420 | if ($interval_method eq 'mean') { | ||
| 421 | @vals = map {$_->{validCount} ? $_->{sumData}/$_->{validCount} : undef} @$stats; | ||
| 422 | } | ||
| 423 | elsif ($interval_method eq 'sum') { | ||
| 424 | @vals = map {$_->{validCount} ? $_->{sumData} : undef} @$stats; | ||
| 425 | } | ||
| 426 | elsif ($interval_method eq 'min') { | ||
| 427 | @vals = map {$_->{validCount} ? $_->{minVal} : undef} @$stats; | ||
| 428 | } | ||
| 429 | elsif ($interval_method eq 'max') { | ||
| 430 | @vals = map {$_->{validCount} ? $_->{maxVal} : undef} @$stats; | ||
| 431 | } | ||
| 432 | else { | ||
| 433 | warn "unrecognized interval method $interval_method!"; | ||
| 434 | } | ||
| 435 | return $self->_draw_coverage($feature,\@vals,@_); | ||
| 436 | } | ||
| 437 | |||
| 438 | sub _draw_coverage { | ||
| 439 | my $self = shift; | ||
| 440 | my $feature = shift; | ||
| 441 | my $array = shift; | ||
| 442 | |||
| 443 | $array = [split ',',$array] unless ref $array; | ||
| 444 | return unless @$array; | ||
| 445 | |||
| 446 | my ($start,$end) = $self->effective_bounds($feature); | ||
| 447 | my $bases_per_bin = ($end-$start)/@$array; | ||
| 448 | my $pixels_per_base = $self->scale; | ||
| 449 | my @parts; | ||
| 450 | for (my $pixel=0;$pixel<$self->width;$pixel++) { | ||
| 451 | my $offset = $pixel/$pixels_per_base; | ||
| 452 | my $s = $start + $offset; | ||
| 453 | my $e = $s+1; # fill in gaps | ||
| 454 | my $v = $array->[$offset/$bases_per_bin]; | ||
| 455 | next unless defined $v; # skip missing values | ||
| 456 | push @parts,[$s,$s,$v]; | ||
| 457 | } | ||
| 458 | $self->draw_plot(\@parts,@_); | ||
| 459 | } | ||
| 460 | |||
| 461 | sub _draw_wigfile { | ||
| 462 | my $self = shift; | ||
| 463 | my $feature = shift; | ||
| 464 | my $wigfile = shift; | ||
| 465 | |||
| 466 | $self->feature->remove_tag('wigfile') if $self->feature->has_tag('wigfile'); | ||
| 467 | $self->feature->add_tag_value('wigfile',$wigfile); | ||
| 468 | |||
| 469 | eval "require Bio::Graphics::Wiggle" unless Bio::Graphics::Wiggle->can('new'); | ||
| 470 | my $wig = ref $wigfile && $wigfile->isa('Bio::Graphics::Wiggle') | ||
| 471 | ? $wigfile | ||
| 472 | : eval { Bio::Graphics::Wiggle->new($wigfile) }; | ||
| 473 | |||
| 474 | $wig->smoothing($self->get_smoothing); | ||
| 475 | $wig->window($self->smooth_window); | ||
| 476 | $self->wig($wig); | ||
| 477 | my $parts = $self->get_parts; | ||
| 478 | $self->draw_plot($parts,@_); | ||
| 479 | } | ||
| 480 | |||
| 481 | 1; |