EnsEMBL/DBSQL/DensityTypeAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 150 CC 12 SCAR 33.7
CC
12
Cov
0%
store
72.0
fetch_by_dbID
12.0
fetch_all_by_logic_name
12.0
CRAP 29.0
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  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::DensityTypeAdaptor
34  
35 =head1 SYNOPSIS
36  
37 my $density_type_adaptor =
38 $registry->get_adaptor( 'Human', 'Core', 'DensityType' );
39  
40 my @density_types = @{ $density_type_adaptor->fetch_all() };
41  
42 my $dt = $density_type_adaptor->fetch_by_dbID(12);
43  
44 =head1 DESCRIPTION
45  
46 DensityTypeAdaptor - Performs database interaction for DensityType objects.
47  
48 =head1 METHODS
49  
50 =cut
51  
52 package Bio::EnsEMBL::DBSQL::DensityTypeAdaptor;
53 $Bio::EnsEMBL::DBSQL::DensityTypeAdaptor::VERSION = '114.0.0_50'; # TRIAL
54 $Bio::EnsEMBL::DBSQL::DensityTypeAdaptor::VERSION = '114.0.050';
55 use vars qw(@ISA);
56 use strict;
57  
58 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
59  
60 use Bio::EnsEMBL::DensityType;
61 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
62  
63 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
64  
65  
66  
67 =head2 new
68  
69 Arg [1] : see superclass (Bio::EnsEMBL::DBSQL::BaseAdaptor) arguments
70 Example : #use this instead of the constructor directly:
71 my $dta = $db_adaptor->get_DensityTypeAdaptor();
72 Description: Constructor. Creates a new DensityTypeAdaptor
73 Returntype : Bio::EnsEMBL::DBSQL::DensityTypeAdaptor
74 Exceptions : none
75 Caller : DBAdaptor
76 Status : Stable
77  
78 =cut
79  
80 sub new {
81 my $class = shift;
82  
83 my $self = $class->SUPER::new(@_);
84  
85 $self->{'dbID_cache'} = {};
86  
87 return $self;
88 }
89  
90  
91 # _tables
92 # Arg [1] : none
93 # Description: PROTECTED implementation of superclass abstract method.
94 # Returns the names, aliases of the tables to use for queries.
95 # Returntype : list of listrefs of strings
96 # Exceptions : none
97 # Caller : internal
98 # Status : Stable
99  
100 sub _tables {
101 return (['density_type', 'dt']);
102 }
103  
104  
105 # _columns
106 # Arg [1] : none
107 # Example : none
108 # Description: PROTECTED implementation of superclass abstract method.
109 # Returns a list of columns to use for queries.
110 # Returntype : list of strings
111 # Exceptions : none
112 # Caller : internal
113 # Status : Stable
114  
115 sub _columns {
116 return ('dt.density_type_id', 'dt.analysis_id', 'dt.block_size', 'dt.region_features', 'dt.value_type');
117 }
118  
119  
120  
121  
122 =head2 fetch_all
123  
124 Arg [1] : none
125 Example : my @density_types = @{$density_type_adaptor->fetch_all};
126 Description: Retrieves every density type in the database.
127 NOTE: In a multi-species database, this method will
128 return all the entries, not just the ones associated with
129 the current species.
130 Returntype : reference to list of Bio::EnsEMBL::DensityType objects
131 Exceptions : none
132 Caller : general, new
133 Status : Stable
134  
135 =cut
136  
137 sub fetch_all {
138 my $self = shift;
139  
140 my @out;
141  
142 my $sth = $self->prepare("SELECT density_type_id, analysis_id, block_size,".
143 " value_type, region_features " .
144 "FROM density_type");
145  
146 $sth->execute();
147  
148 my($dbID, $analysis_id, $blk_size, $vtype, $region_features );
149 $sth->bind_columns(\$dbID, \$analysis_id, \$blk_size, \$vtype, \$region_features );
150  
151 my $analysis_adaptor = $self->db->get_AnalysisAdaptor();
152  
153 while($sth->fetch()) {
154 my $analysis = $analysis_adaptor->fetch_by_dbID($analysis_id);
155  
156  
157 my $dt = Bio::EnsEMBL::DensityType->new(-ADAPTOR => $self,
158 -DBID => $dbID,
159 -ANALYSIS => $analysis,
160 -BLOCK_SIZE => $blk_size,
161 -REGION_FEATURES => $region_features,
162 -VALUE_TYPE => $vtype);
163  
164 $self->{'dbID_cache'}->{$dbID} = $dt;
165  
166 push @out, $dt;
167 }
168  
169 return \@out;
170 }
171  
172  
173  
174 =head2 fetch_by_dbID
175  
176 Arg [1] : int $dbID
177 Example : my $dt = $density_type_adaptor->fetch_by_dbID($dbID);
178 Description: Retrieves a density type object via its internal identifier
179 Returntype : Bio::EnsEMBL::DensityType
180 Exceptions : throw if dbID argument not defined
181 Caller : general
182 Status : Stable
183  
184 =cut
185  
186 sub fetch_by_dbID {
187 my $self = shift;
188 my $dbID = shift;
189  
190 if(!defined($dbID)) {
191 throw("dbID argument must be defined");
192 }
193  
194 if($self->{'dbID_cache'}->{$dbID}) {
195 return $self->{'dbID_cache'}->{$dbID};
196 }
197  
198 # go back to database and refill caches
199 $self->fetch_all();
200  
201 return $self->{'dbID_cache'}->{$dbID};
202 }
203  
204  
205 =head2 fetch_all_by_logic_name
206  
207 Arg [1] : string $logic_name
208 Example : my @dts = @{$dtype_adaptor->fetch_all('repeat_coverage')};
209 Description: Retrieves all density types with a given logic name.
210 NOTE: In a multi-species database, this method will
211 return all the entries matching the search criteria, not
212 just the ones associated with the current species.
213 Returntype : reference to list of Bio::EnsEMBL::DensityTypes
214 Exceptions : thrown if logic_name argument is not provided
215 Caller : general
216 Status : Stable
217  
218 =cut
219  
220 sub fetch_all_by_logic_name {
221 my $self = shift;
222 my $logic_name = shift;
223  
224 if(!defined($logic_name)) {
225 throw("logic_name argument is required.");
226 }
227  
228 my $analysis_adaptor = $self->db()->get_AnalysisAdaptor();
229 my $analysis = $analysis_adaptor->fetch_by_logic_name($logic_name);
230  
231 return [] if(!$analysis);
232  
233 my $sth = $self->prepare("SELECT density_type_id, block_size,".
234 " value_type, region_features " .
235 "FROM density_type " .
236 "WHERE analysis_id = ?");
237 $sth->bind_param(1,$analysis->dbID,SQL_INTEGER);
238 $sth->execute();
239  
240 my($dbID, $blk_size, $vtype, $region_features );
241 $sth->bind_columns(\$dbID, \$blk_size, \$vtype, \$region_features);
242  
243 my @out;
244  
245 while($sth->fetch()) {
246  
247 my $dt = Bio::EnsEMBL::DensityType->new(-ADAPTOR => $self,
248 -DBID => $dbID,
249 -ANALYSIS => $analysis,
250 -BLOCK_SIZE => $blk_size,
251 -REGION_FEATURES => $region_features,
252 -VALUE_TYPE => $vtype);
253  
254 $self->{'dbID_cache'}->{$dbID} = $dt;
255  
256 push @out, $dt;
257 }
258  
259 return \@out;
260 }
261  
262  
263 =head2 store
264  
265 Arg [1] : list of Bio::EnsEMBL::DensityType @dt
266 the density types to store in the database
267 Example : $density_type->store(@density_types);
268 Description: Stores a list of density type objects in the database
269 Returntype : none
270 Exceptions : thrown if @dt is not defined
271 or if any elements of @dt are not Bio::EnsEMBL::DensityType
272 Caller : general
273 Status : Stable
274  
275 =cut
276  
277 sub store {
278 my ($self,@dt) = @_;
279  
280 if( scalar(@dt) == 0 ) {
281 throw("Must call store with list of Density Types");
282 }
283  
284 my $insert_ignore = $self->insert_ignore_clause();
285 my $sth = $self->prepare
286 ("${insert_ignore} INTO density_type (analysis_id,".
287 "block_size, value_type, region_features ) ".
288 "VALUES (?, ?, ?, ?)");
289  
290 my $db = $self->db();
291 my $analysis_adaptor = $db->get_AnalysisAdaptor();
292  
293 FEATURE: foreach my $dt ( @dt ) {
294 if( !ref $dt || !$dt->isa("Bio::EnsEMBL::DensityType") ) {
295 throw("Density Type must be an Ensembl DensityType, " .
296 "not a [".ref($dt)."]");
297 }
298  
299 if($dt->is_stored($db)) {
300 next FEATURE;
301 }
302  
303 if(!defined($dt->analysis())) {
304 throw("An analysis must be attached to the density type to be stored.");
305 }
306  
307 #store the analysis if it has not been stored yet
308 if(!$dt->analysis->is_stored($db)) {
309 $analysis_adaptor->store($dt->analysis());
310 }
311
312 my $block_size = $dt->block_size();
313 $block_size |= 0;
314 my $region_features = $dt->region_features();
315 $region_features |= 0;
316  
317 $sth->bind_param(1,$dt->analysis->dbID,SQL_INTEGER);
318 $sth->bind_param(2,$block_size,SQL_INTEGER);
319 $sth->bind_param(3,$dt->value_type,SQL_VARCHAR);
320 $sth->bind_param(4,$region_features, SQL_VARCHAR);
321 my $inserted = $sth->execute();
322  
323 my $dbID;
324  
325 # $inserted can be 0E0 which is true but equal to 0
326 if(!$inserted || $inserted == 0) {
327 # insert failed, presumably because was already stored in database
328  
329 my @dts=@{$self->fetch_all_by_logic_name($dt->analysis()->logic_name())};
330 my ($stored_dt) = grep {$_->block_size() == $dt->block_size()} @dts;
331 if(!$stored_dt) {
332 throw("Could not retrieve or store DensityType from database.\n" .
333 "Incorrect db permissions or missing density_type table?\n");
334 }
335 $dbID = $stored_dt->dbID();
336 } else {
337 $dbID = $self->last_insert_id('density_type_id', undef, 'density_type');
338 }
339  
340 # next two lines are to set the density type as stored
341 $dt->dbID($dbID);
342 $dt->adaptor($self);
343  
344 $self->{'dbID_cache'}->{$dbID} = $dt;
345 }
346 }
347  
348 1;