EnsEMBL/DBSQL/DataFileAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 147 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 8 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 234 CC 24 SCAR 32.8
CC
24
Cov
0%
DataFile_to_adaptor
90.0
get_base_path
20.0
generic_fetch
20.0
CRAP 26.6
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::DBSQL::DataFileAdaptor;
21 $Bio::EnsEMBL::DBSQL::DataFileAdaptor::VERSION = '114.0.0_50'; # TRIAL
22 $Bio::EnsEMBL::DBSQL::DataFileAdaptor::VERSION = '114.0.050';
23 =pod
24  
25  
26 =head1 CONTACT
27  
28 Please email comments or questions to the public Ensembl
29 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
30  
31 Questions may also be sent to the Ensembl help desk at
32 <http://www.ensembl.org/Help/Contact>.
33  
34 =head1 NAME
35  
36 Bio::EnsEMBL::DBSQL::DataFileAdaptor
37  
38 =head1 SYNOPSIS
39  
40 my $dfa = $dba->get_DataFileAdaptor();
41 my $file = $dfa->fetch_by_dbID(1);
42 my $files = $dfa->fetch_all();
43
44 my $logic_name_files = $dfa->fetch_all_by_logic_name('bam_alignments');
45  
46 =head1 DESCRIPTION
47  
48 Provides a database wrapper to store the locations of files and to pull these
49 records back out. DataFile objects can only provide basic information but they
50 can return an intended external database adaptor which can be used to
51 parse the information. This system assumes nothing about the file just that
52 your parser can access it.
53  
54 Files are supported over any protocol your parser supports and locations can be
55 made absolute, built on the fly or versioned.
56  
57 =head1 METHODS
58  
59 =cut
60  
61 use strict;
62 use warnings;
63  
64 use base qw/Bio::EnsEMBL::DBSQL::BaseAdaptor/;
65  
66 use Bio::EnsEMBL::DataFile;
67 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
68 use Bio::EnsEMBL::Utils::Exception qw/throw/;
69 use Bio::EnsEMBL::Utils::Scalar qw/:assert/;
70  
71 my $GLOBAL_BASE_PATH;
72  
73 =head2 global_base_path
74  
75 Arg[1] : String; base path
76 Example : Bio::EnsEMBL::DBSQL::DataFileAdaptor->global_base_path('/base/path');
77 Description : Stores a global value to be used when building data file paths
78 Returntype : String
79 Exceptions : None
80  
81 =cut
82  
83 sub global_base_path {
84 my ($class, $base_path) = @_;
85 return $GLOBAL_BASE_PATH unless $base_path;
86 $GLOBAL_BASE_PATH = $base_path;
87 return $GLOBAL_BASE_PATH;
88 }
89  
90 =head2 get_base_path
91  
92 Arg[1] : String; (optional) base path
93 Example : $dfa->get_base_path();
94 Description : If given the path it will return that path; if not it consults
95 $self->global_base_path() for a value. As a last resort
96 it will look at the meta table for an entry keyed by
97 B<data_file.base_path>
98 Returntype : String
99 Exceptions : Thrown if nothing is found after consulting all three locations
100  
101 =cut
102  
103 sub get_base_path {
104 my ($self, $path) = @_;
105 return $path if defined $path;
106 my $global_base_path = $self->global_base_path();
107 return $global_base_path if defined $global_base_path;
108 my $meta_base_path = $self->db()->get_MetaContainer()->single_value_by_key('data_file.base_path', 1);
109 return $meta_base_path if defined $meta_base_path;
110 throw "No base path discovered. Either provide a path, set a global using global_base_path() or specify 'data_file.base_path' in meta";
111 }
112  
113 =head2 DataFile_to_extensions
114  
115 Arg[1] : Bio::EnsEMBL::DataFile
116 Example : my $exts = $dfa->DataFile_to_extensions($bam_df);
117 Description : Returns all expected extensions for the given DataFile type. The
118 first returned is the default extension
119 Returntype : ArrayRef
120 Exceptions : Raised if the given file type is not understood
121  
122 =cut
123  
124 sub DataFile_to_extensions {
125 my ($self, $df) = @_;
126 my $type = $df->file_type();
127 my $extensions = {
128 BAM => ['bam', 'bam.bai'],
129 BAMCOV => ['bam', 'bam.bai', 'bam.bw'], # BAM coverage files
130 BIGBED => ['bb'],
131 BIGWIG => ['bw'],
132 VCF => ['vcf.gz', 'vcf.gz.tbi'],
133 }->{$type};
134 throw sprintf(q{No extensions found for the type '%s'}, $type ) if ! $extensions;
135 return $extensions;
136 }
137  
138  
139 =head2 DataFile_to_adaptor
140  
141 Arg[1] : Bio::EnsEMBL::DataFile
142 Arg[2] : (optional) base path
143 Arg[3] : (optional) file type
144 Example : my $bam = $dfa->DataFile_to_adaptor($bam_df);
145 Description : Returns an adaptor instance which will access the given DataFile.
146 Can explicitly request for an adaptor of a given file type (third
147 argument), useful with composite types, e.g. BAM coverage files
148 can be returned as BAM or BIGWIG
149 Returntype : Scalar actual return depends upon the given file type and the
150 requested type
151 Exceptions : Raised if the given file type is not understood or if the requested
152 file type is incompatible with the actual data file type.
153  
154 =cut
155  
156 sub DataFile_to_adaptor {
157 my ($self, $df, $base, $requested_type) = @_;
158 my $type = $df->file_type();
159  
160 throw sprintf("Request for a '%s' adaptor, but file is of type '%s'", $requested_type, $type)
161 if $type ne 'BAMCOV' and $type ne $requested_type;
162  
163 SWITCH:
164 {
165 return Bio::EnsEMBL::IO::Adaptor::BAMAdaptor->new($df->path($base))
166 if $type eq 'BAM';
167  
168 return Bio::EnsEMBL::IO::Adaptor::BigBedAdaptor->new($df->path($base))
169 if $type eq 'BIGBED';
170  
171 return Bio::EnsEMBL::IO::Adaptor::BigWigAdaptor->new($df->path($base))
172 if $type eq 'BIGWIG';
173  
174 return Bio::EnsEMBL::IO::Adaptor::VCFAdaptor->new($df->path($base))
175 if $type eq 'VCF';
176
177 # BAMCOV composite case
178 if ($type eq 'BAMCOV') {
179 return Bio::EnsEMBL::IO::Adaptor::BAMAdaptor->new($df->path($base))
180 if $requested_type eq 'BAM' or $requested_type eq 'BAMCOV';
181
182 return Bio::EnsEMBL::IO::Adaptor::BigWigAdaptor->new($df->get_all_paths($base)->[2])
183 if $requested_type eq 'BIGWIG';
184 }
185  
186 throw sprintf(q{No '%s' handler found for the type '%s'}, $requested_type, $type )
187 }
188  
189 }
190  
191 =head2 fetch_all_by_logic_name
192  
193 Args [1] : String $logic_name for the linked analysis
194 Example : my $dfs = $dfa->fetch_all_by_logic_name('bam_alignments');
195 Description : Returns all DataFile entries linked to the given analysis
196 logic name
197 Returntype : ArrayRef contains Bio::EnsEMBL::DataFile instances
198 Exceptions : Thrown if logic name does not exist
199  
200 =cut
201  
202 sub fetch_all_by_logic_name {
203 my ($self, $logic_name) = @_;
204 my $analysis = $self->db()->get_AnalysisAdaptor()->fetch_by_logic_name($logic_name);
205 throw "No analysis found for logic_name '${logic_name}'" if ! $analysis;
206 return $self->fetch_all_by_Analysis($analysis);
207 }
208  
209 =head2 fetch_all_by_Analysis
210  
211 Args [1] : Bio::EnsEMBL::Analysis $analysis to look up by
212 Example : my $dfs = $dfa->fetch_all_by_Analysis($analysis);
213 Description : Returns all DataFile entries linked to the given analysis
214 Returntype : ArrayRef contains Bio::EnsEMBL::DataFile instances
215 Exceptions : None
216  
217 =cut
218  
219 sub fetch_all_by_Analysis {
220 my ($self, $analysis) = @_;
221 assert_ref($analysis, 'Bio::EnsEMBL::Analysis', 'analysis');
222 $self->bind_param_generic_fetch($analysis->dbID(), SQL_INTEGER);
223 return $self->generic_fetch('df.analysis_id =?');
224 }
225  
226 =head2 fetch_all_by_CoordSystem
227  
228 Args [1] : Bio::EnsEMBL::CoordSystem $coord_system to look up by
229 Example : my $dfs = $dfa->fetch_all_by_CoordSystem($cs);
230 Description : Returns all DataFile entries linked to the given coordinate
231 system. Does B<not> support I<toplevel>
232 Returntype : ArrayRef contains Bio::EnsEMBL::DataFile instances
233 Exceptions : None
234  
235 =cut
236  
237 sub fetch_all_by_CoordSystem {
238 my ($self, $cs) = @_;
239 assert_ref($cs, 'Bio::EnsEMBL::CoordSystem', 'coord_system');
240 $self->bind_param_generic_fetch($cs->dbID(), SQL_INTEGER);
241 return $self->generic_fetch('df.coord_system_id =?');
242 }
243  
244 sub fetch_by_name_and_type {
245 my ($self, $name, $type) = @_;
246 $self->bind_param_generic_fetch($name, SQL_VARCHAR);
247 $self->bind_param_generic_fetch($type, SQL_VARCHAR);
248 my $results = $self->generic_fetch('df.name =? and df.file_type =?');
249 return $results->[0] if @{$results};
250 return;
251 }
252  
253 sub generic_fetch {
254 my ($self, $constraint) = @_;
255 $constraint ||= q{};
256
257 my $sql = <<'SQL';
258 select df.data_file_id, df.coord_system_id, df.analysis_id, df.name, df.version_lock, df.absolute, df.url, df.file_type
259 from data_file df
260 join coord_system cs using (coord_system_id)
261 where cs.species_id =?
262 SQL
263 $sql .= 'AND '.$constraint if $constraint;
264
265 my $params = $self->bind_param_generic_fetch();
266 if(defined $params) {
267 $self->{'_bind_param_generic_fetch'} = ();
268 }
269 else {
270 $params = [];
271 }
272 unshift(@{$params}, $self->db()->species_id());
273
274 my $csa = $self->db()->get_CoordSystemAdaptor();
275 my $aa = $self->db()->get_AnalysisAdaptor();
276
277 return $self->dbc()->sql_helper()->execute(-SQL => $sql, -PARAMS => $params, -CALLBACK => sub {
278 my ($row) = @_;
279 my ($data_file_id, $coord_system_id, $analysis_id, $name, $version_lock, $absolute, $url, $file_type) = @{$row};
280 my $hash = {
281 dbID => $data_file_id,
282 adaptor => $self,
283 coord_system => $csa->fetch_by_dbID($coord_system_id),
284 analysis => $aa->fetch_by_dbID($analysis_id),
285 name => $name,
286 version_lock => $version_lock,
287 absolute => $absolute,
288 file_type => $file_type,
289 };
290 $hash->{url} = $url if $url;
291 return Bio::EnsEMBL::DataFile->new_fast($hash);
292 });
293 }
294  
295 sub store {
296 my ($self, $df) = @_;
297
298 assert_ref($df, 'Bio::EnsEMBL::DataFile', 'datafile');
299
300 if ($df->is_stored($self->db())) {
301 return $df->dbID();
302 }
303
304 throw 'Analysis is not defined for this data file' if ! defined $df->analysis();
305 throw 'Coord system is not defined for this data file' if ! defined $df->coord_system();
306
307 my $sql = <<'SQL';
308 INSERT INTO data_file (coord_system_id, analysis_id, name, version_lock, absolute, url, file_type)
309 VALUES (?,?,?,?,?,?,?)
310 SQL
311 my $params = [
312 [$df->coord_system()->dbID(), SQL_INTEGER],
313 [$df->analysis()->dbID(), SQL_INTEGER],
314 [$df->name(), SQL_VARCHAR],
315 [$df->version_lock(), SQL_INTEGER],
316 [$df->absolute(), SQL_INTEGER],
317 [$df->url(), SQL_VARCHAR],
318 [$df->file_type(), SQL_VARCHAR],
319 ];
320 $self->dbc()->sql_helper()->execute_update(-SQL => $sql, -PARAMS => $params, -CALLBACK => sub {
321 my ( $sth, $dbh ) = @_;
322 $df->dbID($self->last_insert_id());
323 return;
324 });
325 $df->adaptor($self);
326
327 return $df->dbID();
328 }
329  
330 sub update {
331 my ($self, $df) = @_;
332
333 assert_ref($df, 'Bio::EnsEMBL::DataFile', 'datafile');
334
335 if (! $df->is_stored($self->db())) {
336 $self->store($df);
337 return;
338 }
339
340 my $sql = <<'SQL';
341 UPDATE data_file SET coord_system_id =?, analysis_id=?, name=?, version_lock=?, absolute=?, url=?, file_type=?
342 WHERE data_file_id =?
343 SQL
344 my $params = [
345 [$df->coord_system()->dbID(), SQL_INTEGER],
346 [$df->analysis()->dbID(), SQL_INTEGER],
347 [$df->name(), SQL_VARCHAR],
348 [$df->version_lock(), SQL_INTEGER],
349 [$df->absolute(), SQL_INTEGER],
350 [$df->url(), SQL_VARCHAR],
351 [$df->file_type(), SQL_VARCHAR],
352 [$df->dbID(), SQL_INTEGER],
353 ];
354 $self->dbc()->sql_helper()->execute_update(-SQL => $sql, -PARAMS => $params);
355 return;
356 }
357  
358 sub delete {
359 my ($self, $df) = @_;
360
361 assert_ref($df, 'Bio::EnsEMBL::DataFile', 'datafile');
362
363 if (! $df->is_stored($self->db())) {
364 throw "Cannot delete the data file if it has not already been stored in this database";
365 }
366
367 $self->dbc()->sql_helper()->execute_update(
368 -SQL => 'DELETE from data_file where data_file_id =?',
369 -PARAMS => [[$df->dbID(), SQL_INTEGER]],
370 );
371
372 return;
373 }
374  
375 sub _tables {
376 my ($self) = @_;
377 return (
378 [qw/data_file df/]
379 );
380 }
381  
382 1;