blib/lib/Bio/Graphics/Browser2/RemoteSet.pm untested

filter: Statementstmt 0.0% 0 / 218 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 43 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 398 CC 45 SCAR 38.4
CC
45
Cov
0%
mirror
132.0
get_das_segment
90.0
feature_file
72.0
CRAP 46.5
1 package Bio::Graphics::Browser2::RemoteSet;
2 # API for handling a set of remote annotation sources
3  
4 use strict;
5 use base 'Bio::Graphics::Browser2::ExternalData';
6  
7 use Bio::Graphics::Browser2::Util 'shellwords';
8 use IO::File;
9 use File::Path 'mkpath';
10 use CGI 'cookie','param','unescape';
11 use Digest::MD5 'md5_hex';
12 use File::Spec;
13  
14 use constant URL_FETCH_TIMEOUT => 20; # seconds max!
15 use constant URL_FETCH_MAX_SIZE => 50_000_000; # don't accept any files larger than 50 Meg
16  
17 use constant DEBUG=>0;
18  
19 sub new {
20 my $package = shift;
21 my $config = shift;
22 my $state = shift;
23 my $lang = shift;
24 my $uploadid = shift;
25 my $self = bless {
26 config => $config,
27 language => $lang,
28 state => $state,
29 uploadid => $uploadid,
30 sources => {},
31 },ref $package || $package;
32 $self;
33 }
34  
35 sub uploadid { shift->{uploadid} }
36  
37 sub add_files_from_state {
38 my $self = shift;
39 my $state = $self->state;
40 my $config = $self->config;
41  
42 for my $track (@{$state->{tracks}||[]}) {
43  
44 if ($track =~ /^(http|ftp|das):/) {
45 warn "adding remote track $track" if DEBUG;
46 $self->add_source($track,$track);
47 next;
48 }
49  
50 my $remote_url = $config->setting($track=>'remote feature') or next;
51 $remote_url = &$remote_url()
52 if ref $remote_url && ref $remote_url eq 'CODE';
53 warn "adding remote_url = $remote_url" if DEBUG;
54 $self->add_source($track,$remote_url);
55 }
56 }
57  
58 sub language { shift->{language} }
59 sub state { shift->{state} }
60 sub config { shift->{config} }
61 sub sources { keys %{shift->{sources}} }
62 sub source2url { shift->{sources}{shift()} }
63  
64 sub add_source {
65 my $self = shift;
66 my ($label,$source) = @_;
67 $self->{sources}{$label}=$source;
68 }
69  
70 sub delete_source {
71 my $self = shift;
72 my $label = shift;
73 delete $self->{sources}{$label};
74 }
75  
76 sub set_sources {
77 my $self = shift;
78 my $sources = shift;
79  
80 my $state = $self->state;
81 for (@$sources) {
82 next if $_ eq '';
83 $self->add_source($_,$_);
84 $state->{features}{$_}{visible}++ unless exists $state->{features}{$_};
85 }
86  
87 # remove unused tracks
88 my $adjusted;
89 for my $track (keys %{$state->{features}}) {
90 next unless $track =~ /^(http|ftp|das):/;
91 next if $self->{sources}{$track};
92 delete $state->{features}{$track};
93 $adjusted++;
94 }
95 }
96  
97 sub feature_file {
98 my $self = shift;
99 my ($label,$segment,$rel2abs,$slow_mapper,$overview,$region) = @_;
100  
101 my $config = $self->config;
102 my $state = $self->state;
103  
104 warn "feature_file(): fetching $label" if DEBUG;
105  
106 # DAS handling
107 my $url = $self->source2url($label);
108 my $feature_file;
109  
110 if ($url =~ m!^(http://.+/das)/([^?]+)(?:\?(.+))?$!) { # DAS source!
111 warn "getting DAS segment for $url" if DEBUG;
112 $feature_file = $self->get_das_segment($1,$2,$3,$segment);
113 }
114 else {
115 warn "getting featurefile for $url" if DEBUG;
116 $feature_file = $self->get_remote_upload($url,$rel2abs,$slow_mapper,$segment,$label,$overview,$region);
117 }
118 return unless $feature_file;
119  
120 # Tell the feature file what its name is, so that it can be formatted
121 # nicely in the user interface.
122 my $name = $feature_file->setting('name') if $feature_file->can('setting');
123 $feature_file->name($name||$url) if $feature_file->can('name');
124  
125 return $feature_file;
126 }
127  
128 sub transform_url {
129 my $self = shift;
130 my ($url,$segment,$overview_segment,$region_segment) = @_;
131  
132 my ($seqid,$start,$end) = ref $segment
133 ? ($segment->seq_id,$segment->start,$segment->end)
134 : $segment =~ /^([^:]+):(.+)(?:\.\.|,)(.+)$/;
135  
136 # do certain substitutions on the URL
137 # for DAS
138 $url =~ s!(http:.+/das/\w+)(?:\?(.+))?$!$1/features?segment=$seqid:$start..$end;$2!;
139  
140 # for gbgff and the like
141 $url =~ s/\$segment/$seqid:$start..$end/g;
142 $url =~ s/\$overview/"$seqid:".$overview_segment->start.'..'.$overview_segment->end/ge if $overview_segment;
143 $url =~ s/\$region/"$seqid:".$region_segment->start.'..'.$region_segment->end/ge if $region_segment;
144 $url =~ s/\$ref/$seqid/g;
145 $url =~ s/\$start/$start/e;
146 $url =~ s/\$end/$end/e;
147
148 return $url;
149 }
150  
151 sub get_remote_upload {
152 my $self = shift;
153 my ($url,$rel2abs,$slow_mapper,$segment,$label,$overview,$region) = @_;
154 my $config = $self->config;
155  
156 # do certain substitutions on the URL
157 $url = $self->transform_url($url,$segment,$overview,$region);
158  
159 my (undef,$filename) = $self->name_file($url,0);
160 my $response = $self->mirror($url,$filename);
161 if ($response->is_error) {
162 # error($self->language->tr('Fetch_failed',$url,$response->message));
163 warn "$url: ",$response->message;
164 return;
165 }
166 my $fh = IO::File->new("<$filename") or return;
167 my $in_overview = $rel2abs ne $slow_mapper && $self->probe_for_overview_sections($fh);
168 my $feature_file =
169 Bio::Graphics::FeatureFile->new(-file => $fh,
170 # this is broken
171 # -map_coords => $in_overview ? $slow_mapper : $rel2abs,
172 -smart_features => 1,
173 -safe_world =>
174 $self->config->global_setting('allow remote callbacks')||0,
175 );
176 warn "get_remote_feature_data(): got $feature_file" if DEBUG;
177
178 # let proximal configuration override remote
179 if ($config->setting($label => 'remote feature')) {
180  
181 # local track name may not correspond to the
182 # FeatureFile types
183 my ($types) = $url =~ /type=([^;&]+)/;
184 my @types = split(/\+|\s+/, $types);
185  
186 for my $option ($config->config->_setting($label)) {
187 my $val = $config->setting($label => $option);
188 for my $type (@types) {
189 $feature_file->set($type,$option,$val);
190 }
191 }
192 }
193  
194 return $feature_file;
195 }
196  
197 # We override name_file() so as to place all remote data
198 # into one "remote" directory. This avoids replicating data.
199 sub name_file {
200 my $self = shift;
201 my $filename = shift;
202 my $strip = shift;
203  
204 my $state = $self->state;
205 my $config = $self->config;
206 my $hex = md5_hex($filename);
207 my ($a,$b) = $hex =~ /^(.{2})(.{2})/;
208 my $id = File::Spec->catfile('shared_remote_tracks',$a,$b);
209  
210 my ($url,$path) = $self->file2path($config,$id,$filename,$strip);
211 warn "name_file() returns => ($url,$path)" if DEBUG;
212 return ($url,$path);
213 }
214  
215 sub get_das_segment {
216 my $self = shift;
217 my ($src,$dsn,$cgi_args,
218 $segment) = @_;
219 my $config = $self->config;
220  
221 unless (eval "require Bio::Das; 1;") {
222 Bio::Graphics::Browser2::Util::error($self->language->tr('NO_DAS'));
223 return;
224 }
225  
226 my @aggregators = shellwords($config->setting('aggregators') ||'');
227 my (@types,@categories);
228  
229 if ($cgi_args) {
230 my @a = split /[;&]/,$cgi_args;
231 foreach (@a) {
232 my ($arg,$val) = split '=';
233 push @types,unescape($val) if $arg eq 'type';
234 push @categories,unescape($val) if $arg eq 'category';
235 }
236 }
237 my @args = (-source => $src,
238 -dsn => $dsn,
239 -aggregators=> \@aggregators);
240 push @args,(-types => \@types) if @types;
241 push @args,(-categories => \@categories) if @categories;
242 my $das = Bio::Das->new(@args);
243  
244 return unless $das;
245  
246 # set up proxy
247 my $http_proxy = $self->http_proxy;
248 $das->proxy($http_proxy) if $http_proxy && $http_proxy ne 'none';
249  
250 my $seg = $das->segment($segment->abs_ref,
251 $segment->abs_start,
252 $segment->abs_end);
253 return $seg;
254 }
255  
256 sub mirror {
257 my $self = shift;
258 my ($url,$filename) = @_;
259  
260 my $config = $self->config;
261  
262 # Uploaded feature handling
263 unless (LWP::UserAgent->can('new')) {
264 unless (eval "require LWP") {
265 Bio::Graphics::Browser2::Util::error($self->language->tr('NO_LWP'));
266 return;
267 }
268 }
269 my $ua = LWP::UserAgent->new(agent => "Generic-Genome-Browser/$main::VERSION",
270 timeout => URL_FETCH_TIMEOUT,
271 max_size => URL_FETCH_MAX_SIZE,
272 );
273 my $http_proxy = $self->http_proxy;
274 my $ftp_proxy = $self->ftp_proxy;
275  
276 $ua->proxy(http => $http_proxy) if $http_proxy && $http_proxy ne 'none';
277 $ua->proxy(ftp => $http_proxy) if $ftp_proxy && $ftp_proxy ne 'none';
278  
279 my $request = HTTP::Request->new(GET => $url);
280 if (-e $filename) {
281 my($mtime) = (stat($filename))[9];
282 if($mtime) {
283 $request->header('If-Modified-Since' =>
284 HTTP::Date::time2str($mtime));
285 }
286 }
287  
288 my ($volume,$dirs,$file) = File::Spec->splitpath($filename);
289 $file = "$file-$$";
290 my $tmpfile = File::Spec->catfile($volume,$dirs,$file);
291  
292 mkpath($dirs) unless -e $dirs && -d _;
293 my $response = $ua->request($request,$tmpfile);
294  
295 if ($response->is_success) { # we got a new file, so need to process it
296 my $fh = IO::File->new($tmpfile);
297  
298 # handle possible un-gzipping
299 my $dummy_name = $url;
300 $dummy_name .= ".gz" if $response->header('Content-Type') =~ /gzip/;
301  
302 my $infh = $self->maybe_unzip($dummy_name,$fh) || $fh;
303 $infh or die "Couldn't open $tmpfile: $!";
304 my $outfh = IO::File->new(">$filename") or die "Couldn't open $filename: $!";
305 $self->process_uploaded_file($infh,$outfh);
306 if (my $lm = $response->last_modified) {
307 utime($lm,$lm,$filename);
308 }
309 }
310 unlink $tmpfile; # either way, this file is no longer needed
311 return $response;
312 }
313  
314 sub annotate {
315 my $self = shift;
316 my $segment = shift;
317 my $feature_files = shift || {};
318 my $fast_mapper = shift; # fast mapper filters out features that are outside cur segment
319 my $slow_mapper = shift; # slow mapper doesn't
320 my $max_segment = shift; # ignored
321 my $whole_segment = shift;
322 my $region_segment = shift;
323 my $state = $self->state;
324  
325 for my $url ($self->sources) {
326 next unless $state->{features}{$url}{visible};
327
328 # check to see whether URL includes the magic $segment and/or $ref
329 # parameters. If so, then it is safe to use the coordinate remapper
330 # which remaps all coordinates. Otherwise, this is probably just a GFF
331 # file and we need to filter out features that are outside the range of
332 # the current segment.
333 my $mapper = $url =~ m!\$(segment|ref|overview|region)!
334 ? $fast_mapper
335 : $slow_mapper;
336 my $feature_file = eval {$self->feature_file($url,$segment,
337 $mapper,$slow_mapper,
338 $whole_segment,$region_segment)
339 };
340 $self->error($url=>$@) if $@;
341 $feature_files->{$url} = $feature_file;
342 }
343 }
344  
345 sub error {
346 my $self = shift;
347 my $url = shift;
348 $self->{error}{$url} = shift if @_;
349 return $self->{error}{$url};
350 }
351  
352 1;
353  
354 __END__