1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
package Bio::Graphics::Browser2::DataLoader::useq;
# $Id$
use strict;
use base 'Bio::Graphics::Browser2::DataLoader';
use Bio::DB::BigBed;
use Bio::DB::BigWig;
use File::Spec;
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{default_track_name} = 'track000';
$self->find_paths;
$self;
}
sub default_track_name {
my $self = shift;
return $self->{default_track_name}++;
}
sub find_paths {
my $self = shift;
# looking for a non-executable jar file that may not be in env path
# so hard code likely paths, taking into account known Virtual Machines
my @paths = qw(/usr /usr/local /opt /opt/gbrowse /data /data/opt /Applications);
push @paths, split ':', $ENV{PATH};
my ($USeq2UCSCBig, $bigPath, $java);
foreach my $p (@paths) {
unless ($USeq2UCSCBig) {
my $path = File::Spec->catdir($p, 'USeq*');
foreach my $candidate (reverse glob($path)) {
# we reverse the glob results to ensure we find the latest
# version if multiple installed, eg USeq_8.4.4 before USeq_8.0.9
my $app = File::Spec->catdir($candidate, 'Apps', 'USeq2UCSCBig');
if (-e $app) {
$USeq2UCSCBig = $app;
}
}
}
unless ($bigPath) {
# we need the bin path to both converter utilities
my $w2bw = File::Spec->catdir($p, 'wigToBigWig');
my $b2bb = File::Spec->catdir($p, 'bedToBigBed');
if ( (-e $w2bw && -x _ ) and
(-e $b2bb && -x _ ) ) {
$bigPath = $p;
}
}
unless ($java) {
my $path = File::Spec->catdir($p, 'java');
$java = $path if (-e $path && -x _ );
}
last if $USeq2UCSCBig && $bigPath && $java;
}
die "Please install the USeq package from http://useq.sourceforge.net"
unless $USeq2UCSCBig;
die "Please install wigToBigWig and bedToBigBed in your path"
unless $bigPath;
die "Please install Java 1.6+" unless $java;
$self->{USeq2UCSCBig} = $USeq2UCSCBig;
$self->{bigPath} = $bigPath;
$self->{java} = $java;
return 1;
}
sub load {
my $self = shift;
my ($initial_lines,$fh) = @_;
$self->flag_busy(1);
eval {
$self->open_conf;
$self->set_status('starting load');
mkdir $self->sources_path or die $!;
$self->{useq} = File::Spec->catfile($self->sources_path,$self->track_name);
my $source_file = IO::File->new($self->{useq},'>');
warn "sourcefile=$self->{useq}";
$self->start_load;
$self->set_status('load data');
my $bytes_loaded = 0;
foreach (@$initial_lines) {
$source_file->print($_);
$bytes_loaded += length $_;
}
my $buffer;
while ((my $bytes = read($fh,$buffer,8192) > 0)) {
$source_file->print($buffer);
$bytes_loaded += length $ buffer;
$self->set_status("loaded $bytes_loaded bytes") if $bytes++ % 10000;
}
$source_file->close();
$self->finish_load;
$self->close_conf;
$self->set_processing_complete;
};
$self->flag_busy(0);
die $@ if $@;
return $self->tracks;
}
sub finish_load {
my $self = shift;
$self->convert_useq;
my @bw;
my @bb;
my $path = $self->sources_path . "/*";
foreach my $f (glob($path)) {
push @bw, $f if $f =~ /\.bw$/;
push @bb, $f if $f =~ /\.bb$/;
}
if (scalar @bb == 1) {
$self->finish_bigbed_load(@bb);
}
elsif (scalar @bw == 1) {
$self->finish_bigwig_load(@bw);
}
elsif (scalar @bw == 2) {
$self->finish_stranded_bigwig_load(@bw);
}
}
sub convert_useq {
my $self = shift;
$self->set_status('Converting with USeq2UCSCBig');
my $java = $self->{java};
my $app = $self->{USeq2UCSCBig};
my $bigPath = $self->{bigPath};
my $useq = $self->{useq};
local $SIG{CHLD} = 'DEFAULT';
my $fh;
open $fh, "($java -jar '$app' -d '$bigPath' -u '$useq' && echo 'success') 2>&1 |";
my @lines = <$fh>;
close $fh;
unless ($lines[-1] =~ /success/) {
die "USEQ CONVERSION ERROR: @lines";
}
unlink $useq; # no longer need the original file
unlink "$useq.chromLengths" if -e "$useq.chromLengths";
unlink "$useq.wig" if -e "$useq.wig";
}
sub finish_bigbed_load {
my $self = shift;
my $bigbed = shift;
my @COLORS = qw(blue red orange brown mauve peach
green cyan yellow coral);
my $loadid = $self->loadid;
$self->set_status('writing configuration for bigBed');
my $conf = $self->conf_fh;
my $dbid = $self->new_track_label;
print $conf <<END;
[$dbid:database]
db_adaptor = Bio::DB::BigBed
db_args = -bigbed '$bigbed'
END
;
print $conf "#>>>>>>>>>> cut here <<<<<<<<\n";
my $color = $COLORS[rand @COLORS];
my $name = $self->track_name;
print $conf <<END
[$dbid]
database = $dbid
feature = region
glyph = segments
label density = 50
feature_limit = 500
bump = fast
stranded = 1
height = 4
bgcolor = $color
fgcolor = $color
key = $name segments
description =
[$dbid\_coverage]
database = $dbid
feature = summary
glyph = wiggle_whiskers
fgcolor = black
height = 50
autoscale = chromosome
key = $name coverage
description =
END
;
# We are defining two separate tracks rather than using semantic zoom
# because of the flexible nature of the bigBed format. It can be used
# as a Bam substitute where coverage is the best glyph, or it can be used
# for sparse intervals of interest where segments is the best glyph.
# Onus is on the user to select the most appropriate one.
}
sub finish_bigwig_load {
my $self = shift;
my $bigwig = shift;
my $loadid = $self->loadid;
$self->set_status('writing configuration for bigWig');
my $conf = $self->conf_fh;
my $dbid = $self->new_track_label;
print $conf <<END;
[$dbid:database]
db_adaptor = Bio::DB::BigWig
db_args = -bigwig '$bigwig'
END
;
print $conf "#>>>>>>>>>> cut here <<<<<<<<\n";
my $name = $self->track_name;
print $conf <<END
[$dbid]
database = $dbid
feature = summary
glyph = wiggle_whiskers
fgcolor = black
height = 50
autoscale = chromosome
key = $name
description =
END
;
}
sub finish_stranded_bigwig_load {
my $self = shift;
$self->set_status('preparing db for BigWigSet');
my ($minus, $plus);
foreach (@_) {
my (undef, undef, $file) = File::Spec->splitpath($_);
$file =~ s/\.bw$//;
$plus = $file if $file =~ /Plus$/;
$minus = $file if $file =~ /Minus$/;
}
my $name = $self->track_name;
my $path = $self->sources_path;
my $index = File::Spec->catdir($path, 'metadata.txt');
open my $fh, ">", $index or return;
print $fh <<END
[$plus\.bw]
primary_tag = $name
display_name = $plus
strand = plus
[$minus\.bw]
primary_tag = $name
display_name = $minus
strand = minus
END
;
close $fh;
my $loadid = $self->loadid;
my $conf = $self->conf_fh;
my $dbid = $self->new_track_label;
print $conf <<END;
[$dbid:database]
db_adaptor = Bio::DB::BigWigSet
db_args = -dir '$path'
-feature_type summary
#>>>>>>>>>> cut here <<<<<<<<
[$dbid]
database = $dbid
feature = $name
subtrack select = Strand tag_value strand
subtrack table = :Plus plus;
:Minus minus;
glyph = wiggle_xyplot
height = 50
bgcolor = blue
fgcolor = black
autoscale = chromosome
key = $name
description =
END
;
}
1;
__END__
=head1 NAME
Bio::Graphics::Browser2::DataLoader::useq
=head1 DESCRIPTION
A data loader for the USeq archive, recognized by the file extension ".useq".
See L<http://useq.sourceforge.net/useqArchiveFormat.html>
for information regarding the file format. Briefly, this format can store either
genomic intervals with or without text and/or scores, or quantitative scores
along a chromosome (point data).
There is currently no native BioPerl adaptor for the USeq archive. Upon upload, the
file is converted to either a UCSC BigBed or BigWig format, depending upon the
file contents. Stranded point data may be converted into two BigWig files, each for
the Plus and Minus strand. Configuration files are generated as appropriate for
the converted files.
=head1 SETUP
To process the USeq archive, the USeq package (L<http://useq.sourceforge.net> must
be installed in a globally accessible path. This location is searched upon
initiation. Common paths to search include "/usr", "/usr/local", "/opt",
"/opt/gbrowse", "/data", "/data/opt", and "/Applications", in that order.
The USeq App "USeq2UCSCBig" (a jar file) is used to convert the USeq archive. This
app requires three binary executables: "java" and the two UCSC utilities "bedToBigBed"
and "wigToBigWig". These are searched for in the environment $PATH variable.
The UCSC utilities are available at L<http://hgdownload.cse.ucsc.edu/admin/exe/>.
The USeq Apps requires Java 1.6+.
Failure to find the paths for all three will result in failure to process the .useq
file.
=head1 AUTHOR
Timothy J. Parnell, PhD
Dept of Oncological Sciences
Huntsman Cancer Institute
University of Utah
Salt Lake City, UT, 84112
This package is free software; you can redistribute it and/or modify
it under the terms of the GPL (either version 1, or at your option,
any later version) or the Artistic License 2.0.
|