| 1 | package Bio::Graphics::Browser2::DataLoader::useq; | ||
| 2 | |||
| 3 | # $Id$ | ||
| 4 | use strict; | ||
| 5 | use base 'Bio::Graphics::Browser2::DataLoader'; | ||
| 6 | use Bio::DB::BigBed; | ||
| 7 | use Bio::DB::BigWig; | ||
| 8 | use File::Spec; | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my $class = shift; | ||
| 12 | my $self = $class->SUPER::new(@_); | ||
| 13 | $self->{default_track_name} = 'track000'; | ||
| 14 | $self->find_paths; | ||
| 15 | $self; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub default_track_name { | ||
| 19 | my $self = shift; | ||
| 20 | return $self->{default_track_name}++; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub find_paths { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | # looking for a non-executable jar file that may not be in env path | ||
| 27 | # so hard code likely paths, taking into account known Virtual Machines | ||
| 28 | my @paths = qw(/usr /usr/local /opt /opt/gbrowse /data /data/opt /Applications); | ||
| 29 | push @paths, split ':', $ENV{PATH}; | ||
| 30 | |||
| 31 | my ($USeq2UCSCBig, $bigPath, $java); | ||
| 32 | foreach my $p (@paths) { | ||
| 33 | unless ($USeq2UCSCBig) { | ||
| 34 | my $path = File::Spec->catdir($p, 'USeq*'); | ||
| 35 | foreach my $candidate (reverse glob($path)) { | ||
| 36 | # we reverse the glob results to ensure we find the latest | ||
| 37 | # version if multiple installed, eg USeq_8.4.4 before USeq_8.0.9 | ||
| 38 | my $app = File::Spec->catdir($candidate, 'Apps', 'USeq2UCSCBig'); | ||
| 39 | if (-e $app) { | ||
| 40 | $USeq2UCSCBig = $app; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | unless ($bigPath) { | ||
| 46 | # we need the bin path to both converter utilities | ||
| 47 | my $w2bw = File::Spec->catdir($p, 'wigToBigWig'); | ||
| 48 | my $b2bb = File::Spec->catdir($p, 'bedToBigBed'); | ||
| 49 | if ( (-e $w2bw && -x _ ) and | ||
| 50 | (-e $b2bb && -x _ ) ) { | ||
| 51 | $bigPath = $p; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | unless ($java) { | ||
| 56 | my $path = File::Spec->catdir($p, 'java'); | ||
| 57 | $java = $path if (-e $path && -x _ ); | ||
| 58 | } | ||
| 59 | |||
| 60 | last if $USeq2UCSCBig && $bigPath && $java; | ||
| 61 | } | ||
| 62 | |||
| 63 | die "Please install the USeq package from http://useq.sourceforge.net" | ||
| 64 | unless $USeq2UCSCBig; | ||
| 65 | die "Please install wigToBigWig and bedToBigBed in your path" | ||
| 66 | unless $bigPath; | ||
| 67 | die "Please install Java 1.6+" unless $java; | ||
| 68 | |||
| 69 | $self->{USeq2UCSCBig} = $USeq2UCSCBig; | ||
| 70 | $self->{bigPath} = $bigPath; | ||
| 71 | $self->{java} = $java; | ||
| 72 | return 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | sub load { | ||
| 77 | my $self = shift; | ||
| 78 | my ($initial_lines,$fh) = @_; | ||
| 79 | |||
| 80 | $self->flag_busy(1); | ||
| 81 | eval { | ||
| 82 | $self->open_conf; | ||
| 83 | $self->set_status('starting load'); | ||
| 84 | |||
| 85 | mkdir $self->sources_path or die $!; | ||
| 86 | $self->{useq} = File::Spec->catfile($self->sources_path,$self->track_name); | ||
| 87 | my $source_file = IO::File->new($self->{useq},'>'); | ||
| 88 | |||
| 89 | warn "sourcefile=$self->{useq}"; | ||
| 90 | |||
| 91 | $self->start_load; | ||
| 92 | |||
| 93 | $self->set_status('load data'); | ||
| 94 | my $bytes_loaded = 0; | ||
| 95 | foreach (@$initial_lines) { | ||
| 96 | $source_file->print($_); | ||
| 97 | $bytes_loaded += length $_; | ||
| 98 | } | ||
| 99 | |||
| 100 | my $buffer; | ||
| 101 | while ((my $bytes = read($fh,$buffer,8192) > 0)) { | ||
| 102 | $source_file->print($buffer); | ||
| 103 | $bytes_loaded += length $ buffer; | ||
| 104 | $self->set_status("loaded $bytes_loaded bytes") if $bytes++ % 10000; | ||
| 105 | } | ||
| 106 | $source_file->close(); | ||
| 107 | |||
| 108 | $self->finish_load; | ||
| 109 | $self->close_conf; | ||
| 110 | $self->set_processing_complete; | ||
| 111 | }; | ||
| 112 | |||
| 113 | $self->flag_busy(0); | ||
| 114 | die $@ if $@; | ||
| 115 | return $self->tracks; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub finish_load { | ||
| 119 | my $self = shift; | ||
| 120 | |||
| 121 | $self->convert_useq; | ||
| 122 | |||
| 123 | my @bw; | ||
| 124 | my @bb; | ||
| 125 | my $path = $self->sources_path . "/*"; | ||
| 126 | foreach my $f (glob($path)) { | ||
| 127 | push @bw, $f if $f =~ /\.bw$/; | ||
| 128 | push @bb, $f if $f =~ /\.bb$/; | ||
| 129 | } | ||
| 130 | |||
| 131 | if (scalar @bb == 1) { | ||
| 132 | $self->finish_bigbed_load(@bb); | ||
| 133 | } | ||
| 134 | elsif (scalar @bw == 1) { | ||
| 135 | $self->finish_bigwig_load(@bw); | ||
| 136 | } | ||
| 137 | elsif (scalar @bw == 2) { | ||
| 138 | $self->finish_stranded_bigwig_load(@bw); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | sub convert_useq { | ||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | $self->set_status('Converting with USeq2UCSCBig'); | ||
| 146 | my $java = $self->{java}; | ||
| 147 | my $app = $self->{USeq2UCSCBig}; | ||
| 148 | my $bigPath = $self->{bigPath}; | ||
| 149 | my $useq = $self->{useq}; | ||
| 150 | local $SIG{CHLD} = 'DEFAULT'; | ||
| 151 | my $fh; | ||
| 152 | open $fh, "($java -jar '$app' -d '$bigPath' -u '$useq' && echo 'success') 2>&1 |"; | ||
| 153 | my @lines = <$fh>; | ||
| 154 | close $fh; | ||
| 155 | unless ($lines[-1] =~ /success/) { | ||
| 156 | die "USEQ CONVERSION ERROR: @lines"; | ||
| 157 | } | ||
| 158 | unlink $useq; # no longer need the original file | ||
| 159 | unlink "$useq.chromLengths" if -e "$useq.chromLengths"; | ||
| 160 | unlink "$useq.wig" if -e "$useq.wig"; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub finish_bigbed_load { | ||
| 164 | my $self = shift; | ||
| 165 | my $bigbed = shift; | ||
| 166 | my @COLORS = qw(blue red orange brown mauve peach | ||
| 167 | green cyan yellow coral); | ||
| 168 | |||
| 169 | my $loadid = $self->loadid; | ||
| 170 | $self->set_status('writing configuration for bigBed'); | ||
| 171 | my $conf = $self->conf_fh; | ||
| 172 | my $dbid = $self->new_track_label; | ||
| 173 | print $conf <<END; | ||
| 174 | [$dbid:database] | ||
| 175 | db_adaptor = Bio::DB::BigBed | ||
| 176 | db_args = -bigbed '$bigbed' | ||
| 177 | |||
| 178 | END | ||
| 179 | ; | ||
| 180 | print $conf "#>>>>>>>>>> cut here <<<<<<<<\n"; | ||
| 181 | my $color = $COLORS[rand @COLORS]; | ||
| 182 | my $name = $self->track_name; | ||
| 183 | |||
| 184 | print $conf <<END | ||
| 185 | [$dbid] | ||
| 186 | database = $dbid | ||
| 187 | feature = region | ||
| 188 | glyph = segments | ||
| 189 | label density = 50 | ||
| 190 | feature_limit = 500 | ||
| 191 | bump = fast | ||
| 192 | stranded = 1 | ||
| 193 | height = 4 | ||
| 194 | bgcolor = $color | ||
| 195 | fgcolor = $color | ||
| 196 | key = $name segments | ||
| 197 | description = | ||
| 198 | |||
| 199 | [$dbid\_coverage] | ||
| 200 | database = $dbid | ||
| 201 | feature = summary | ||
| 202 | glyph = wiggle_whiskers | ||
| 203 | fgcolor = black | ||
| 204 | height = 50 | ||
| 205 | autoscale = chromosome | ||
| 206 | key = $name coverage | ||
| 207 | description = | ||
| 208 | |||
| 209 | END | ||
| 210 | ; | ||
| 211 | # We are defining two separate tracks rather than using semantic zoom | ||
| 212 | # because of the flexible nature of the bigBed format. It can be used | ||
| 213 | # as a Bam substitute where coverage is the best glyph, or it can be used | ||
| 214 | # for sparse intervals of interest where segments is the best glyph. | ||
| 215 | # Onus is on the user to select the most appropriate one. | ||
| 216 | } | ||
| 217 | |||
| 218 | sub finish_bigwig_load { | ||
| 219 | my $self = shift; | ||
| 220 | my $bigwig = shift; | ||
| 221 | |||
| 222 | my $loadid = $self->loadid; | ||
| 223 | $self->set_status('writing configuration for bigWig'); | ||
| 224 | my $conf = $self->conf_fh; | ||
| 225 | my $dbid = $self->new_track_label; | ||
| 226 | print $conf <<END; | ||
| 227 | [$dbid:database] | ||
| 228 | db_adaptor = Bio::DB::BigWig | ||
| 229 | db_args = -bigwig '$bigwig' | ||
| 230 | |||
| 231 | END | ||
| 232 | ; | ||
| 233 | print $conf "#>>>>>>>>>> cut here <<<<<<<<\n"; | ||
| 234 | my $name = $self->track_name; | ||
| 235 | |||
| 236 | print $conf <<END | ||
| 237 | [$dbid] | ||
| 238 | database = $dbid | ||
| 239 | feature = summary | ||
| 240 | glyph = wiggle_whiskers | ||
| 241 | fgcolor = black | ||
| 242 | height = 50 | ||
| 243 | autoscale = chromosome | ||
| 244 | key = $name | ||
| 245 | description = | ||
| 246 | |||
| 247 | END | ||
| 248 | ; | ||
| 249 | |||
| 250 | } | ||
| 251 | |||
| 252 | sub finish_stranded_bigwig_load { | ||
| 253 | my $self = shift; | ||
| 254 | |||
| 255 | $self->set_status('preparing db for BigWigSet'); | ||
| 256 | |||
| 257 | my ($minus, $plus); | ||
| 258 | foreach (@_) { | ||
| 259 | my (undef, undef, $file) = File::Spec->splitpath($_); | ||
| 260 | $file =~ s/\.bw$//; | ||
| 261 | $plus = $file if $file =~ /Plus$/; | ||
| 262 | $minus = $file if $file =~ /Minus$/; | ||
| 263 | } | ||
| 264 | |||
| 265 | my $name = $self->track_name; | ||
| 266 | my $path = $self->sources_path; | ||
| 267 | my $index = File::Spec->catdir($path, 'metadata.txt'); | ||
| 268 | open my $fh, ">", $index or return; | ||
| 269 | |||
| 270 | print $fh <<END | ||
| 271 | [$plus\.bw] | ||
| 272 | primary_tag = $name | ||
| 273 | display_name = $plus | ||
| 274 | strand = plus | ||
| 275 | |||
| 276 | [$minus\.bw] | ||
| 277 | primary_tag = $name | ||
| 278 | display_name = $minus | ||
| 279 | strand = minus | ||
| 280 | END | ||
| 281 | ; | ||
| 282 | close $fh; | ||
| 283 | |||
| 284 | my $loadid = $self->loadid; | ||
| 285 | my $conf = $self->conf_fh; | ||
| 286 | my $dbid = $self->new_track_label; | ||
| 287 | print $conf <<END; | ||
| 288 | [$dbid:database] | ||
| 289 | db_adaptor = Bio::DB::BigWigSet | ||
| 290 | db_args = -dir '$path' | ||
| 291 | -feature_type summary | ||
| 292 | |||
| 293 | #>>>>>>>>>> cut here <<<<<<<< | ||
| 294 | |||
| 295 | [$dbid] | ||
| 296 | database = $dbid | ||
| 297 | feature = $name | ||
| 298 | subtrack select = Strand tag_value strand | ||
| 299 | subtrack table = :Plus plus; | ||
| 300 | :Minus minus; | ||
| 301 | glyph = wiggle_xyplot | ||
| 302 | height = 50 | ||
| 303 | bgcolor = blue | ||
| 304 | fgcolor = black | ||
| 305 | autoscale = chromosome | ||
| 306 | key = $name | ||
| 307 | description = | ||
| 308 | |||
| 309 | END | ||
| 310 | ; | ||
| 311 | } | ||
| 312 | |||
| 313 | 1; | ||
| 314 | __END__ |