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