ensembl-hive  2.7.0
DataFile.pm
Go to the documentation of this file.
1 =head1 LICENSE
2 
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 =cut
19 
20 package Bio::EnsEMBL::DataFile;
21 
22 use strict;
23 use warnings;
24 
25 use base qw/Bio::EnsEMBL::Storable/;
26 
28 use Bio::EnsEMBL::Utils::Argument qw/rearrange/;
29 use Bio::EnsEMBL::Utils::Exception qw/throw warning/;
30 use Bio::EnsEMBL::Utils::Scalar qw/:assert/;
31 use Bio::EnsEMBL::Utils::URI qw/is_uri/;
32 use File::Spec;
33 
34 =head2 new
35 
37  Arg [-DBID] : Integer $dbID
38  Arg [-COORD_SYSTEM] : Bio::EnsEMBL::CoordSystem $coord_system
39  Arg [-ANALYSIS] : Bio::EnsEMBL::Analysis $analysis
40  Arg [-NAME] : String $name
41  Arg [-VERSION_LOCK] : Boolean $version_lock
42  Arg [-ABSOLUTE] : Boolean $absolute
43  Arg [-URL] : String $url
44  Arg [-FILE_TYPE] : String $file_type
45  Example : Bio::EnsEMBL::DataFile->new();
46  Description : Returns a new instance of this object
47  Returntype : Bio::EnsEMBL::DataFile
48  Exceptions : Thrown if data is not as expected
49 
50 =cut
51 
52 sub new {
53  my ($class, @args) = @_;
54  my $self = $class->SUPER::new(@args);
55  my ($coord_system, $analysis, $name, $version_lock, $absolute, $url, $file_type) =
56  rearrange([qw/coord_system analysis name version_lock absolute url file_type/], @args);
57 
58  $self->coord_system($coord_system);
59  $self->analysis($analysis);
60  $self->name($name);
61  $self->version_lock($version_lock);
62  $self->absolute($absolute);
63  $self->url($url);
64  $self->file_type($file_type);
65 
66  return $self;
67 }
68 
69 
70 =head2 get_ExternalAdaptor
71 
72  Arg[1] : Scalar; (optional) base path. Uses defaults if not given
73  Arg[2] : Scalar; (optional) file type
74  Example : my $ea = $df->get_ExternalAdaptor('/base/path');
75  Description : Delegates to the parent adaptor to retrieve the external
76  adaptor for this data type
77  Returntype : Adaptor; will be an adaptor that can read the given data file
78  Exceptions : Thrown if there is no attached adaptor.
79 
80 =cut
81 
82 sub get_ExternalAdaptor {
83  my ($self, $base_path, $requested_type) = @_;
84  my $adaptor = $self->adaptor();
85  throw "No DataFileAdaptor found in this object. Cannot request ExternalAdaptor" if ! $adaptor;
86  return $adaptor->DataFile_to_adaptor($self, $base_path, $requested_type);
87 }
88 
89 =head2 path
90 
91  Arg[1] : Scalar base of the path to use. Can be ignored if the instance
92  already represents a canonical path
93  Example : my $f = $df->path();
94  Description : Used to generate the path to the file resource. Can return a
95  path to the file or a URL but it is up to the using code to
96  know how to interprate the different returned forms.
97 
98  If the data file url is canonical then this is just returned.
99  If not then a path is generated of the form
100  B</base/path/production_name/coord_system_version/[software_version]/db_group/name.ext>
101 
102  Returntype : Scalar the absolute path/url to the given resource
103  Exceptions : Thrown if the linked Coordinate System lacks a version and the
104  current database also lacks a default version
105  Caller : public
106 
107 =cut
108 
109 
110 sub path {
111  my ($self, $base) = @_;
112  my $all_paths = $self->get_all_paths($base);
113  return $all_paths->[0];
114 }
115 
116 sub get_all_paths {
117  my ($self, $base) = @_;
118 
119  return [$self->url()] if $self->absolute();
120 
121  my @all_paths;
122 
123  $base = $self->adaptor()->get_base_path($base) if ! $base;
124 
125  my $production_name = $self->adaptor()->db()->get_MetaContainer()->get_production_name();
126  my $cs_version = $self->coord_system()->version();
127  if(! $cs_version) {
128  my ($highest_cs) = @{$self->adaptor()->db()->get_CoordSystemAdaptor()->fetch_all()};
129  $cs_version = $highest_cs->version();
130  }
131  if(!$cs_version) {
132  my $name = $self->name();
133  throw "The file '${name}' in species '${$production_name} is attached to a CoordinateSystem lacking a version and has no default assembly. Please fix";
134  }
135 
136  my @portions;
137  push(@portions, $production_name);
138  push(@portions, $cs_version);
139  push(@portions, software_version()) if $self->version_lock();
140  push(@portions, $self->adaptor()->db()->group());
141 
142  #Targets are the files to generate
143  my @targets;
144  #If URL is populated we assume we need to add this onto the end but removing the /
145  if($self->url()) {
146  my @split = split(/\//, $self->url());
147  push(@targets, [@split]);
148  }
149  else {
150  my $extensions = $self->adaptor()->DataFile_to_extensions($self);
151  foreach my $ext (@{$extensions}) {
152  my $filename = sprintf(q{%s.%s}, $self->name(), $ext);
153  push(@targets, [$filename]);
154  }
155  }
156 
157  my $is_uri = is_uri($base);
158  foreach my $t (@targets) {
159  my $path;
160  if($is_uri) {
161  $path = join(q{/}, $base, @portions, @{$t});
162  }
163  else {
164  $path = File::Spec->catfile($base, @portions, @{$t});
165  }
166  push(@all_paths, $path);
167  }
168  return \@all_paths;
169 }
170 
171 =head2 coord_system
172 
173  Arg[1] : Bio::EnsEMBL::CoordSystem Optional setter
174  Description : Mutator for the coord system field. All files are linked to one
175  Returntype : Bio::EnsEMBL::CoordSystem
176  Exceptions : Thrown if not of the expected type
177 
178 =cut
179 
180 
181 sub coord_system {
182  my ($self, $coord_system) = @_;
183  if(defined $coord_system) {
184  assert_ref($coord_system, 'Bio::EnsEMBL::CoordSystem', 'coord_system');
185  $self->{'coord_system'} = $coord_system;
186  }
187  return $self->{'coord_system'};
188 }
189 
190 =head2 analysis
191 
192  Arg[1] : Bio::EnsEMBL::Analysis Optional setter
193  Description : Mutator for the analysis field. All files are linked to one
194  Returntype : Bio::EnsEMBL::Analysis
195  Exceptions : Thrown if not of the expected type
196 
197 =cut
198 
199 sub analysis {
200  my ($self, $analysis) = @_;
201  if(defined $analysis) {
202  assert_ref($analysis, 'Bio::EnsEMBL::Analysis', 'analysis');
203  $self->{'analysis'} = $analysis;
204  }
205  return $self->{'analysis'};
206 }
207 
208 =head2 name
209 
210  Arg[1] : String Optional setter
211  Description : Mutator for the name of the file. Can be used in file location
212  generation
213  Returntype : String
214 
215 =cut
216 
217 sub name {
218  my ($self, $name) = @_;
219  if(defined $name) {
220  $self->{'name'} = $name;
221  }
222  return $self->{'name'};
223 }
224 
225 =head2 version_lock
226 
227  Arg[1] : Boolean Optional setter
228  Description : Boolean indicating if the file is linked to the version of the
229  database it was found in.
230  Returntype : Boolean
231 
232 =cut
233 
234 sub version_lock {
235  my ($self, $version_lock) = @_;
236  if(defined $version_lock) {
237  assert_boolean($version_lock, 'version_lock');
238  $self->{'version_lock'} = $version_lock;
239  }
240  return $self->{'version_lock'};
241 }
242 
243 =head2 absolute
244 
245  Arg[1] : Boolean Optional setter
246  Description : Indicates if the URL of this file is an absolute one i.e.
247  should be used verbatim or not.
248  Returntype : Boolean
249 
250 =cut
251 
252 sub absolute {
253  my ($self, $absolute) = @_;
254  if(defined $absolute) {
255  assert_boolean($absolute, 'absolute');
256  $self->{'absolute'} = $absolute;
257  }
258  return $self->{'absolute'};
259 }
260 
261 =head2 url
262 
263  Arg[1] : String Optional setter
264  Description : Location of the file. Can be optional and if set means once
265  we are in an automatic location use this value to locate
266  the file.
267  Returntype : String
268 
269 =cut
270 
271 sub url {
272  my ($self, $url) = @_;
273  $self->{'url'} = $url if defined $url;
274  return $self->{'url'};
275 }
276 
277 =head2 file_type
278 
279  Arg[1] : String Optional setter
280  Description : The type of file we are working with. Can be used to generate
281  a file name.
282  Returntype : String
283 
284 =cut
285 
286 sub file_type {
287  my ($self, $file_type) = @_;
288  $self->{'file_type'} = $file_type if defined $file_type;
289  return $self->{'file_type'};
290 }
291 
292 #=head2 files
293 #
294 # Args :
295 # Example : my $files = @{$df->files()};
296 # Description : Returns all the file names we expect to cover for a flat file
297 # Returntype : type return_description
298 # Exceptions :
299 # Caller : caller
300 # Status : status
301 #
302 #=cut
303 #
304 #
305 #sub files {
306 # my ($self) = @_;
307 #
308 #}
309 
310 1;
Bio::EnsEMBL::Utils::URI
Definition: URI.pm:28
Bio::EnsEMBL::CoordSystem
Definition: CoordSystem.pm:40
Bio::EnsEMBL::Storable
Definition: Storable.pm:23
Bio::EnsEMBL::DataFile
Definition: DataFile.pm:9
Bio::EnsEMBL::DataFile::new
public Bio::EnsEMBL::DataFile new()
Bio::EnsEMBL::Analysis
Definition: PairAlign.pm:3
Bio::EnsEMBL::Utils::Scalar
Definition: Scalar.pm:66
Bio::EnsEMBL::DBSQL::DataFileAdaptor
Definition: DataFileAdaptor.pm:30
Bio::EnsEMBL::Utils::Argument
Definition: Argument.pm:34
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68
Bio::EnsEMBL::ApiVersion
Definition: ApiVersion.pm:17