« blib/script/gbrowse_grow_cloud_vol.pl
↑ Summary
blib/lib/Bio/Graphics/Browser2/DataLoader/wiggle.pm »
| 1 | package Bio::Graphics::Browser2::DataLoader::wig2bigwig; | ||
| 2 | |||
| 3 | # $Id$ | ||
| 4 | use strict; | ||
| 5 | use base 'Bio::Graphics::Browser2::DataLoader'; | ||
| 6 | use Bio::DB::BigWig; | ||
| 7 | use Bio::Graphics::Browser2::Util 'shellwords'; | ||
| 8 | use File::Basename 'basename','dirname'; | ||
| 9 | |||
| 10 | # we will sort out the sections corresponding to each track line | ||
| 11 | sub load_line { | ||
| 12 | my $self = shift; | ||
| 13 | my $line = shift; | ||
| 14 | |||
| 15 | my $prefix = $self->strip_prefix; | ||
| 16 | if ($prefix) { | ||
| 17 | $line =~ s/^$prefix//; | ||
| 18 | $line =~ s/chrom=$prefix/chrom=/; | ||
| 19 | } | ||
| 20 | |||
| 21 | if ($line =~ /^track/) { # starting or ending a section | ||
| 22 | $self->finish_track_declaration; | ||
| 23 | $self->start_track_declaration($line); | ||
| 24 | } elsif ($line =~ /\S/ && $line !~ /^#/ && !$self->{current_track}) { | ||
| 25 | warn "no track declaration; faking it"; | ||
| 26 | $self->start_default_track_declaration(); | ||
| 27 | $self->write_line($line); | ||
| 28 | } | ||
| 29 | else { | ||
| 30 | $self->write_line($line); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | sub start_track_declaration { | ||
| 36 | my $self = shift; | ||
| 37 | my $line = shift; | ||
| 38 | my (undef,@pairs) = shellwords($line); | ||
| 39 | my %options = map{split '='} @pairs; | ||
| 40 | $self->{current_track}{options} = \%options; | ||
| 41 | $self->{current_track}{name} ||= $self->default_track_name; | ||
| 42 | |||
| 43 | my $dest = File::Spec->catfile($self->data_path,$self->{current_track}{name}).'.wig'; | ||
| 44 | $self->{current_track}{wigfile} = $dest; | ||
| 45 | |||
| 46 | open my $fh,'>',$dest or die "Can't open $dest for writing: $!"; | ||
| 47 | $self->{current_track}{fh} = $fh; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub start_default_track_declaration { | ||
| 51 | my $self = shift; | ||
| 52 | $self->start_track_declaration('track type=wiggle_0'); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub finish_track_declaration { | ||
| 56 | my $self = shift; | ||
| 57 | return unless $self->{current_track}; | ||
| 58 | if ($self->{current_track}{fh}) { | ||
| 59 | close $self->{current_track}{fh} | ||
| 60 | or die "Error closing wig file $self->{current_track}{wigfile}:$!"; | ||
| 61 | } | ||
| 62 | $self->wig2bigwig($self->{current_track}{wigfile}); | ||
| 63 | push @{$self->{tracks}},$self->{current_track}; | ||
| 64 | delete $self->{current_track}; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub write_line { | ||
| 68 | my $self = shift; | ||
| 69 | my $line = shift; | ||
| 70 | my $fh = $self->{current_track}{fh} or return; | ||
| 71 | print $fh $line or die "Couldn't write to $self->{current_track}{wigfile}: $!"; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub wig2bigwig { | ||
| 75 | my $self = shift; | ||
| 76 | |||
| 77 | my $src = $self->{current_track}{wigfile} or return; | ||
| 78 | (my $dest=$src) =~ s/\.wig$/\.bw/; | ||
| 79 | my $cs = $self->chrom_sizes; | ||
| 80 | |||
| 81 | $self->{current_track}{bigwig} = $dest; | ||
| 82 | |||
| 83 | $self->set_status('creating bigwig file'); | ||
| 84 | # the cause for this complicated code is that we first look for the | ||
| 85 | # external wigToBigWig and run that in a fashion that allows us to | ||
| 86 | # recover its stderr. Otherwise we fork (safely) and call our internal | ||
| 87 | # library routine from Bio::DB::BigFile. This can exit out in an | ||
| 88 | # uncatchable way so we have to capture stderr as well. | ||
| 89 | { | ||
| 90 | local $SIG{CHLD} = 'DEFAULT'; | ||
| 91 | my $fh; | ||
| 92 | if (my $wigtobigwig = $self->wigtobigwig_path) { | ||
| 93 | open $fh,"($wigtobigwig -clip '$src' '$cs' '$dest' && echo success) 2>&1 |"; | ||
| 94 | } else { | ||
| 95 | eval "use IO::Pipe" unless IO::Pipe->can('new'); | ||
| 96 | $fh = IO::Pipe->new; | ||
| 97 | my $child = Bio::Graphics::Browser2::Render->fork(); | ||
| 98 | die "Couldn't fork" unless defined $child; | ||
| 99 | if (!$child) { # child process | ||
| 100 | $fh->writer(); | ||
| 101 | my $fileno = fileno($fh); | ||
| 102 | open STDERR,">&$fileno"; | ||
| 103 | open STDOUT,">&$fileno"; | ||
| 104 | Bio::DB::BigFile->createBigWig($src,$cs,$dest, | ||
| 105 | {clipDontDie=>1}); | ||
| 106 | print STDERR "success"; | ||
| 107 | exit 0; | ||
| 108 | } else { | ||
| 109 | $fh->reader; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | my @lines = <$fh>; | ||
| 113 | close $fh; | ||
| 114 | unless ($lines[-1] =~ /success/) { | ||
| 115 | die "PROCESSING ERROR: @lines"; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | unlink $self->{current_track}{wigfile}; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub finish_load { | ||
| 123 | my $self = shift; | ||
| 124 | |||
| 125 | $self->finish_track_declaration; | ||
| 126 | die "no tracks defined!" unless $self->{tracks}; | ||
| 127 | |||
| 128 | my $loadid = $self->loadid; | ||
| 129 | |||
| 130 | $self->set_status('creating configuration'); | ||
| 131 | my $conf = $self->conf_fh; | ||
| 132 | my @tracks = @{$self->{tracks}}; | ||
| 133 | for my $track (@tracks) { | ||
| 134 | my $dbid = $self->new_track_label; | ||
| 135 | my $bigwig = $track->{bigwig} or die "no bigwig for $track->{wigfile}"; | ||
| 136 | $track->{dbid} = $dbid; | ||
| 137 | print $conf <<END; | ||
| 138 | [$dbid:database] | ||
| 139 | db_adaptor = Bio::DB::BigWig | ||
| 140 | db_args = -bigwig '$bigwig' | ||
| 141 | |||
| 142 | END | ||
| 143 | ; | ||
| 144 | } | ||
| 145 | print $conf "#>>>>>>>>>> cut here <<<<<<<<\n"; | ||
| 146 | |||
| 147 | my $trackno = 1; | ||
| 148 | for my $track (@tracks) { | ||
| 149 | my $dbid = $track->{dbid}; | ||
| 150 | my $options = $track->{options}; | ||
| 151 | my $description = $options->{description}; | ||
| 152 | $options->{name} ||= sprintf("%s.%03d",$self->track_name,$trackno++); | ||
| 153 | $options->{visibility} ||= 'full'; | ||
| 154 | $options->{maxHeightPixels} ||= $options->{visibility} eq 'full' ? 50 : 20; | ||
| 155 | $options->{autoScale} ||= 'off'; | ||
| 156 | my @options; | ||
| 157 | push @options,"database = $dbid"; | ||
| 158 | push @options,"feature = summary"; | ||
| 159 | push @options,"key = $options->{name}"; | ||
| 160 | push @options,'glyph = '. ($options->{visibility} eq 'pack' | ||
| 161 | ? 'wiggle_density' | ||
| 162 | : 'wiggle_whiskers' ); | ||
| 163 | push @options,'autoscale = '.($options->{autoScale} eq 'on' | ||
| 164 | ? 'local' | ||
| 165 | : 'chromosome'); | ||
| 166 | if (exists $options->{viewLimits} && | ||
| 167 | (my ($low,$hi) = split ':',$options->{viewLimits})) { | ||
| 168 | push @options,"min_score = $low"; | ||
| 169 | push @options,"max_score = $hi"; | ||
| 170 | } | ||
| 171 | |||
| 172 | if (exists $options->{maxHeightPixels} && | ||
| 173 | (my ($max,$default,$min) = split ':',$options->{maxHeightPixels})) { | ||
| 174 | $default ||= $max; | ||
| 175 | push @options,"height = $default"; | ||
| 176 | } | ||
| 177 | push @options,"description = $description"; | ||
| 178 | my $config_lines = join "\n",@options; | ||
| 179 | print $conf <<END; | ||
| 180 | [$dbid] | ||
| 181 | $config_lines | ||
| 182 | |||
| 183 | END | ||
| 184 | } | ||
| 185 | |||
| 186 | } | ||
| 187 | |||
| 188 | sub wigtobigwig_path { | ||
| 189 | my $self = shift; | ||
| 190 | return $self->{_wig2bigwig_path} if exists $self->{_wig2bigwig_path}; | ||
| 191 | return $self->{_wig2bigwig_path} ||= $self->search_for_binary('wigToBigWig'); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub search_for_binary { | ||
| 195 | my $self = shift; | ||
| 196 | my $target = shift; | ||
| 197 | my @path = split ':',$ENV{PATH}; | ||
| 198 | for my $d (@path) { | ||
| 199 | my $tgt = File::Spec->catfile($d,$target); | ||
| 200 | return $tgt if -e $tgt && -x _; | ||
| 201 | } | ||
| 202 | return; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub new { | ||
| 206 | my $class = shift; | ||
| 207 | my $self = $class->SUPER::new(@_); | ||
| 208 | $self->{default_track_name} = 'track000'; | ||
| 209 | $self; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub default_track_name { | ||
| 213 | my $self = shift; | ||
| 214 | return $self->{default_track_name}++; | ||
| 215 | } | ||
| 216 | |||
| 217 | 1; |