blib/lib/Bio/Graphics/Browser2/DataLoader/archive.pm untested

filter: Statementstmt 0.0% 0 / 179 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 280 CC 29 SCAR 45.2
CC
29
Cov
0%
finish_load
210.0
check_metadata
90.0
load
12.0
CRAP 92.2
1 package Bio::Graphics::Browser2::DataLoader::archive;
2  
3 # $Id$
4 use strict;
5 use base 'Bio::Graphics::Browser2::DataLoader';
6 use Bio::DB::BigWigSet;
7 use File::Spec;
8  
9 sub new {
10 my $class = shift;
11 my $self = $class->SUPER::new(@_);
12 $self->{default_track_name} = 'track000';
13 $self;
14 }
15  
16 sub default_track_name {
17 my $self = shift;
18 return $self->{default_track_name}++;
19 }
20  
21 sub load {
22 my $self = shift;
23 my ($initial_lines,$fh) = @_;
24
25 $self->flag_busy(1);
26 eval {
27 $self->open_conf;
28 $self->set_status('starting load');
29
30 mkdir $self->sources_path or die $!;
31 $self->{archive} = File::Spec->catfile($self->sources_path,$self->track_name);
32 my $source_file = IO::File->new($self->{archive},'>');
33  
34 warn "sourcefile=$self->{archive}";
35  
36 $self->start_load;
37  
38 $self->set_status('load data');
39 my $bytes_loaded = 0;
40 foreach (@$initial_lines) {
41 $source_file->print($_);
42 $bytes_loaded += length $_;
43 }
44  
45 my $buffer;
46 while ((my $bytes = read($fh,$buffer,8192) > 0)) {
47 $source_file->print($buffer);
48 $bytes_loaded += length $ buffer;
49 $self->set_status("loaded $bytes_loaded bytes") if $bytes++ % 10000;
50 }
51 $source_file->close();
52
53 $self->finish_load;
54 $self->check_metadata;
55 $self->write_conf;
56 $self->close_conf;
57 $self->set_processing_complete;
58 };
59  
60 $self->flag_busy(0);
61 die $@ if $@;
62 return $self->tracks;
63 }
64  
65 sub finish_load {
66 my $self = shift;
67 $self->set_status("extracting files from archive");
68
69 my $archive = $self->{archive};
70 my $bin = $archive =~ /\.tar$/i ? $self->search_for_binary('tar') :
71 $archive =~ /\.zip$/i ? $self->search_for_binary('unzip') :
72 '';
73 die "unrecognized file type!" unless $bin;
74
75 my $command = $archive =~ /\.tar$/i ? "$bin -tf" : "$bin -l";
76 my $fh;
77 open $fh, "($command $archive && echo 'success') 2>&1 |";
78 my @contents = <$fh>;
79 close $fh;
80 unless ($contents[-1] =~ /success/) {
81 die "ARCHIVE LIST ERROR: @contents";
82 }
83 pop @contents;
84
85 my @to_extract;
86 while (@contents) {
87 my $item = shift @contents;
88 next if ($item =~ /^(Archive\:| Length|\-{5,})/); # unzip headers
89 $item = (split / {2,}/, $item)[-1]; # split on two or more spaces
90 chomp $item;
91 my (undef, undef, $file) = File::Spec->splitpath($item);
92 if ($file =~ /^meta/i) {
93 push @to_extract, [$item, $file];
94 }
95 elsif ($file =~ /^\./) {
96 next; # skip .files if present
97 }
98 elsif ($file =~ /\.bw$/i) {
99 push @to_extract, [$item, $file];
100 }
101 }
102 die "no recognizable files in archive to extract" unless @to_extract;
103
104 # we are trying to be safe here and extract just the file contents
105 # without directory paths to avoid ill- or mis-intentioned stray files
106 my $path = $self->sources_path;
107 my $count;
108 $command = $archive =~ /\.tar$/i ? "$bin -x -O -f" : "$bin -p";
109 foreach (@to_extract) {
110 my $target = File::Spec->catfile($path, $_->[1]);
111 # extract file from tar archive and redirect to file in our path
112 open my $in, "$command $archive \"$_->[0]\" |" or
113 die "unable to open achive for extraction";
114 open my $out, '>', $target or die "unable to open target file";
115 while (<$in>) {print $out $_}
116 close $in;
117 close $out;
118 $count++ if -e $target && -s _ ;
119 }
120 die "not all files could be extracted\n" unless $count == scalar(@to_extract);
121 unlink $archive; # no longer need
122 }
123  
124 sub search_for_binary {
125 my $self = shift;
126 my $target = shift;
127 for my $p (split ':', $ENV{PATH}) {
128 my $tgt = File::Spec->catfile($p,$target);
129 return $tgt if -e $tgt && -x _;
130 }
131 return;
132 }
133  
134 sub check_metadata {
135 my $self = shift;
136 $self->set_status('checking metadata');
137 my $path = $self->sources_path;
138
139 my $count = Bio::DB::BigWigSet->index_dir($path);
140 die "two or more BigWig files are required" unless $count > 1;
141
142 my $bws = Bio::DB::BigWigSet->new(-dir => $path);
143 my $md = $bws->metadata;
144 my $default = $self->track_name; # default type
145 my %types; # metadata types, not required to be unique
146 my %names; # metadata display_names, required to be unique
147 my $flag; # for updated metadata
148 foreach my $i (keys %$md) {
149 if (exists $md->{$i}{'type'}) {
150 $types{ $md->{$i}{'type'} }++;
151 }
152 elsif (exists $md->{$i}{'method'} && exists $md->{$i}{'source'}) {
153 $types{ $md->{$i}{'method'} . ':' . $md->{$i}{'source'} }++;
154 }
155 elsif (exists $md->{$i}{'method'}) {
156 $types{ $md->{$i}{'method'} }++;
157 }
158 elsif (exists $md->{$i}{'primary_tag'}) {
159 $types{ $md->{$i}{'primary_tag'} }++;
160 }
161 else {
162 $types{$default}++;
163 my (undef, undef, $filename) = File::Spec->splitpath( $md->{$i}{'dbid'} );
164 $bws->set_bigwig_attributes($md->{$i}{'dbid'}, {'type'=>$default});
165 $flag++;
166 }
167
168 if (exists $md->{$i}{'display_name'}) {
169 if (exists $names{ $md->{$i}{'display_name'} }) {
170 # must make the name unique
171 my (undef, undef, $name) = File::Spec->splitpath( $md->{$i}{'dbid'} );
172 $name =~ s/\.bw$//i;
173 $bws->set_bigwig_attributes(
174 $md->{$i}{'dbid'}, {'display_name'=>$name} );
175 $names{$name}++;
176 $flag++;
177 }
178 else {
179 $names{ $md->{$i}{'display_name'} }++;
180 }
181 }
182 else {
183 my (undef, undef, $name) = File::Spec->splitpath( $md->{$i}{'dbid'} );
184 $name =~ s/\.bw$//i;
185 $bws->set_bigwig_attributes(
186 $md->{$i}{'dbid'}, {'display_name'=>$name} );
187 $names{$name}++;
188 $flag++;
189 }
190 }
191
192 $self->write_new_metadata($bws) if $flag;
193 $self->typelist(keys %types);
194 $self->namelist(keys %names);
195 }
196  
197 sub typelist {
198 my $self = shift;
199 $self->{typelist} ||= [];
200 @{ $self->{typelist} } = @_ if @_;
201 return @{ $self->{typelist} };
202 }
203  
204 sub namelist {
205 my $self = shift;
206 $self->{namelist} ||= [];
207 @{ $self->{namelist} } = @_ if @_;
208 return @{ $self->{namelist} };
209 }
210  
211 sub write_new_metadata {
212 # Bio::DB::BigWigSet does not have a method to write out updated metadata
213 # so we will do it here
214 my $self = shift;
215 my $bws = shift;
216 my $path = $self->sources_path;
217 foreach my $f (glob($path)) {
218 unlink $f if $f =~ /^meta/i;
219 }
220 my $index = File::Spec->catfile($path, 'metadata.index');
221 open my $fh, ">", $index or return;
222 my $md = $bws->metadata;
223 foreach my $i (sort {$a <=> $b} keys %$md) {
224 my (undef, undef, $file) = File::Spec->splitpath( $md->{$i}{'dbid'} );
225 my $string = "[$file]\n";
226 foreach my $k (keys %{$md->{$i}}) {
227 next if $k eq 'dbid';
228 $string .= "$k = " . $md->{$i}{$k} . "\n";
229 }
230 print $fh "$string\n";
231 }
232 close $fh;
233 }
234  
235 sub write_conf {
236 my $self = shift;
237 my $name = $self->track_name;
238 my $path = $self->sources_path;
239 my $loadid = $self->loadid;
240 my $conf = $self->conf_fh;
241 my $dbid = $self->new_track_label;
242 my @types = $self->typelist;
243 my $table;
244 foreach my $n ($self->namelist) {
245 $table .= "\n :\"$n\" \"$n\" ;";
246 }
247
248 print $conf <<END;
249 [$dbid:database]
250 db_adaptor = Bio::DB::BigWigSet
251 db_args = -dir '$path'
252 -feature_type summary
253  
254 #>>>>>>>>>> cut here <<<<<<<<
255
256 [$dbid]
257 database = $dbid
258 feature = @types
259 subtrack select = Name tag_value display_name
260 subtrack table = $table
261 glyph = wiggle_whiskers
262 # change glyph to wiggle_xyplot to use semi-transparent overlap
263 fgcolor = black
264 height = 50
265 autoscale = chromosome
266 key = $name
267 description =
268  
269 END
270 ;
271  
272 }
273  
274 1;
275  
276 __END__