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