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

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 221 CC 20 SCAR 24.6
CC
20
Cov
0%
readline
20.0
_guess_eol
20.0
file2path
20.0
CRAP 11.7
1 package Bio::Graphics::Browser2::ExternalData;
2  
3 # base class for Bio::Graphics::Browser::RemoteSet;
4  
5 use strict;
6 use Carp 'croak';
7 use Bio::Graphics::Browser2::Util 'error';
8 use Bio::Graphics::Wiggle;
9  
10 use constant DEBUG => 0;
11  
12 sub new {
13 croak 'virtual base class';
14 }
15  
16 sub uploadid { croak "virtual method" }
17 sub config { shift->{config} }
18 sub state { shift->{state} }
19  
20 sub readline {
21 my $self = shift;
22 my $fh = shift;
23 my $line;
24 while (<$fh>) {
25 chomp;
26 next if /^\s*$/; # blank
27 next if /^\s*#/; # comment
28 s/[\r]//g; # get rid of carriage returns from Macintosh/DOS systems
29 $line .= $_;
30 return $line unless $line =~ s/\\$//;
31 }
32 return $line;
33 }
34  
35  
36 # this converts .wig files and other UCSC formats into feature files that we can handle
37 sub convert_ucsc_file {
38 my $self = shift;
39 my ($in,$out) = @_;
40  
41 eval "require Bio::Graphics::Wiggle::Loader;1"
42 unless Bio::Graphics::Wiggle::Loader->can('new');
43  
44 my $dummy_name = $self->name_file('foo');
45 $dummy_name =~ s/foo$//; # get the directory part only!
46  
47 eval {
48 my $loader = Bio::Graphics::Wiggle::Loader->new($dummy_name);
49 $loader->load($in);
50 my $featurefile = $loader->featurefile('featurefile');
51 print $out $featurefile;
52 };
53 error($@) if $@;
54 }
55  
56 sub convert_feature_file {
57 my $self = shift;
58 my ($in,$out) = @_;
59 while ($_ = <$in>) {
60 chomp;
61 print $out $_,"\n";
62 }
63 }
64  
65 sub process_uploaded_file {
66 my $self = shift;
67 my ($infh,$outfh) = @_;
68  
69 local $/ = $self->_guess_eol($infh);
70  
71 my $pos = tell($infh);
72 my $first_line = $self->readline($infh);
73 return unless defined $first_line;
74 seek($infh,$pos,0);
75 if ($first_line =~ /^(track|browser)/) {
76 $self->convert_ucsc_file($infh,$outfh);
77 } else {
78 $self->convert_feature_file($infh,$outfh);
79 }
80 }
81  
82 sub maybe_unzip {
83 my $self = shift;
84 my $filename = shift;
85 my $fh = shift;
86  
87 # If the file is in gzip format,
88 # try to intercept and decompress the file
89 if ($filename =~ /^(.+)\.gz$/) {
90 $fh ||= IO::File->new($filename);
91 $fh->binmode(1) if $fh->can('binmode');
92 require File::Temp;
93 my $fname = File::Temp->tmpnam;
94 my $unzip = IO::File->new("|gunzip -c > $fname") or die $!;
95 $unzip->binmode(1);
96 my $buffer;
97 $unzip->print($buffer) while read($fh,$buffer,1024);
98 $unzip->close;
99 $fh = IO::File->new($fname);
100 $fh->binmode(1);
101 unlink $fname;
102 return $fh;
103 }
104 return;
105 }
106  
107 sub _guess_eol {
108 my $self = shift;
109 my $fh = shift;
110 my $buffer;
111 my $pos = tell($fh);
112 read($fh,$buffer,1024);
113 seek($fh,$pos,0); # back to where we were
114 return "\015\012" if $buffer =~ /\015\012/;
115 return "\015" if $buffer =~ /\015/;
116 return "\012" if $buffer =~ /\012/;
117 }
118  
119 sub name_file {
120 my $self = shift;
121 my $filename = shift;
122 my $strip = shift;
123  
124 my $state = $self->state;
125 my $config = $self->config;
126 my $id = $self->uploadid || $state->{userid} or return;
127  
128 my ($url,$path) = $self->file2path($config,$id,$filename,$strip);
129 warn "name_file() returns => ($url,$path)" if DEBUG;
130 return ($url,$path);
131 }
132  
133 sub file2path {
134 my $self = shift;
135 my ($config,$id,$filename,$strip) = @_;
136 $strip = 1 unless defined $strip;
137  
138 # keep last non-[\\\/:] part of name
139 my ($name) = $strip ? $filename =~ /([^:\\\/]+)$/ : $filename;
140 $name =~ tr!-/!__!; # get rid of hyphens and slashes
141 my $tmpdir = $config->userdata($id);
142 my $path = File::Spec->catfile($tmpdir,$name);
143 my $url = "file:$name";
144 return wantarray ? ($url,$path) : $path;
145 }
146  
147 sub http_proxy {
148 my $self = shift;
149 my $config = $self->config;
150 my $proxy = $config->setting('proxy') || '';
151 return $config->setting('http proxy') || $proxy || '';
152  
153 }
154  
155 sub ftp_proxy {
156 my $self = shift;
157 my $config = $self->config;
158 my $proxy = $config->setting('proxy') || '';
159 return $config->setting('ftp proxy') || $proxy || '';
160 }
161  
162 # ugly hack, but needed for performance gains when looking at really big data sets
163 sub probe_for_overview_sections {
164 my $self = shift;
165 my $fh = shift;
166 my $overview;
167 my $pos = tell($fh);
168 while (<$fh>) {
169 next unless /\S/; # skip blank lines
170 last unless /[\#\[=]/; # not a configuration section
171 if (/^section\s*=.*(region|overview)/) {
172 $overview++;
173 last;
174 }
175 }
176 seek($fh,$pos,0);
177 return $overview;
178 }
179  
180  
181 1;