ensembl-hive  2.8.1
MiscSetAdaptor.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 
33 Bio::EnsEMBL::DBSQL::MiscSetAdaptor - Provides database interaction for
34 Bio::EnsEMBL::MiscSet objects.
35 
36 =head1 SYNOPSIS
37 
38  my $msa = $registry->get_adaptor( 'Human', 'Core', 'MiscSet' );
39 
40  my $misc_set = $msa->fetch_by_dbID(1234);
41 
42  $misc_set = $msa->fetch_by_code('clone');
43 
44 =head1 DESCRIPTION
45 
46 This class provides database interactivity for MiscSet objects.
47 MiscSets are used to classify MiscFeatures into groups.
48 
49 =head1 METHODS
50 
51 =cut
52 
53 package Bio::EnsEMBL::DBSQL::MiscSetAdaptor;
54 
55 use strict;
56 use warnings;
57 
60 
61 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
62 
63 use vars qw(@ISA);
64 
66 
67 
68 =head2 new
69 
70  Arg [...] : Superclass args. See Bio::EnsEMBL::DBSQL::BaseAdaptor
71  Description: Instantiates a Bio::EnsEMBL::DBSQL::MiscSetAdaptor and
72  caches the contents of the MiscSet table.
73  Returntype : Bio::EnsEMBL::MiscSet
74  Exceptions : none
75  Caller : MiscFeatureAdaptor
76  Status : Stable
77 
78 =cut
79 
80 
81 sub new {
82  my $class = shift;
83 
84  my $self = $class->SUPER::new(@_);
85 
86  $self->{'_id_cache'} = {};
87  $self->{'_code_cache'} = {};
88 
89  # cache the entire contents of the misc set table
90  # the table is small and it removes the need to repeatedly query the
91  # table or join to the table
92 
93  $self->fetch_all();
94 
95  return $self;
96 }
97 
98 
99 
100 
101 =head2 fetch_all
102 
103  Arg [1] : none
104  Example : foreach my $ms (@{$msa->fetch_all()}) {
105  print $ms->code(), ' ', $ms->name(), "\n";
106  }
107  Description: Retrieves every MiscSet defined in the DB.
108  NOTE: In a multi-species database, this method will
109  return all the entries matching the search criteria, not
110  just the ones associated with the current species.
111  Returntype : listref of Bio::EnsEMBL::MiscSets
112  Exceptions : none
113  Caller : general
114  Status : Stable
115 
116 =cut
117 
118 sub fetch_all {
119  my $self = shift;
120 
121  my $sth = $self->prepare
122  ('SELECT misc_set_id, code, name, description, max_length FROM misc_set');
123 
124  $sth->execute();
125 
126  my ($dbID, $code, $name, $desc, $max_len);
127  $sth->bind_columns(\$dbID, \$code, \$name, \$desc, \$max_len);
128 
129  my @all;
130 
131  while($sth->fetch()) {
132  my $ms = Bio::EnsEMBL::MiscSet->new
133  (-DBID => $dbID,
134  -ADAPTOR => $self,
135  -CODE => $code,
136  -NAME => $name,
137  -DESCRIPTION => $desc,
138  -LONGEST_FEATURE => $max_len);
139 
140  $self->{'_id_cache'}->{$dbID} = $ms;
141  $self->{'_code_cache'}->{lc($code)} = $ms;
142  push @all, $ms;
143  }
144 
145  $sth->finish();
146 
147  return \@all;
148 }
149 
150 
151 
152 =head2 fetch_by_dbID
153 
154  Arg [1] : int $dbID
155  The internal identifier of the misc set to retrieve
156  Example : my $ms = $msa->fetch_by_dbID($dbID);
157  Description: Retrieves a misc set via its internal identifier
158  Returntype : Bio::EnsEMBL::MiscSet
159  Exceptions : none
160  Caller : general
161  Status : Stable
162 
163 =cut
164 
165 sub fetch_by_dbID {
166  my $self = shift;
167  my $dbID = shift;
168 
169  if(!$self->{'_id_cache'}->{$dbID}) {
170  # on a cache miss reread the whole table and reload the cache
171  $self->fetch_all();
172  }
173 
174  return $self->{'_id_cache'}->{$dbID};
175 }
176 
177 
178 
179 =head2 fetch_by_code
180 
181  Arg [1] : string $code
182  The unique code of the MiscSet to retrieve
183  Example : my $ms = $msa->fetch_by_code('clone');
184  Description: Retrieves a MiscSet via its code
185  Returntype : Bio::EnsEMBL::MiscSet
186  Exceptions : none
187  Caller : general
188  Status : Stable
189 
190 =cut
191 
192 sub fetch_by_code {
193  my $self = shift;
194  my $code = shift;
195 
196  if(!$self->{'_code_cache'}->{lc($code)}) {
197  # on cache miss, reread whole table and reload cache
198  $self->fetch_all();
199  }
200 
201  return $self->{'_code_cache'}->{lc($code)};
202 }
203 
204 
205 
206 =head2 store
207 
208  Arg [1] : list of MiscSets @mist_sets
209  Example : $misc_set_adaptor->store(@misc_sets);
210  Description: Stores a list of MiscSets in the database, and sets the
211  dbID and adaptor attributes of the stored sets.
212  Returntype : none
213  Exceptions : throw on incorrect arguments
214  warning if a feature is already stored in this database
215  Caller : MiscFeatureAdaptor::store
216  Status : Stable
217 
218 =cut
219 
220 sub store {
221  my $self = shift;
222  my @misc_sets = @_;
223 
224  # we use 'insert ignore' so that inserts can occur safely on the farm
225  # otherwise 2 processes could try to insert at the same time and one
226  # would fail
227 
228  my $insert_ignore = $self->insert_ignore_clause();
229  my $sth = $self->prepare(
230  qq{${insert_ignore} INTO misc_set (
231  code,
232  name,
233  description,
234  max_length
235  ) VALUES (?, ?, ?, ?)
236  });
237 
238  my $db = $self->db();
239 
240  SET:
241  foreach my $ms (@misc_sets) {
242  if(!ref($ms) || !$ms->isa('Bio::EnsEMBL::MiscSet')) {
243  throw("List of MiscSet arguments expected.");
244  }
245 
246  if($ms->is_stored($db)) {
247  warning("MiscSet [".$ms->dbID."] is already stored in this database.");
248  next SET;
249  }
250 
251  $sth->bind_param(1,$ms->code,SQL_VARCHAR);
252  $sth->bind_param(2,$ms->name,SQL_VARCHAR);
253  $sth->bind_param(3,$ms->description,SQL_LONGVARCHAR);
254  $sth->bind_param(4,$ms->longest_feature,SQL_INTEGER);
255 
256  my $num_inserted = $sth->execute();
257 
258  my $dbID;
259 
260  if($num_inserted == 0) {
261  # insert failed because set with this code already exists
262  my $sth2 = $self->prepare("SELECT misc_set_id from misc_set " .
263  "WHERE code = ?");
264  $sth2->bind_param(1,$ms->code,SQL_VARCHAR);
265  $sth2->execute();
266 
267  ($dbID) = $sth2->fetchrow_array();
268 
269  if($sth2->rows() != 1) {
270  throw("Could not retrieve or store MiscSet, code=[".$ms->code."]\n".
271  "Wrong database user/permissions?");
272  }
273  } else {
274  $dbID = $self->last_insert_id('misc_set_id', undef, 'misc_set');
275  }
276 
277  $ms->dbID($dbID);
278  $ms->adaptor($self);
279 
280  # update the internal caches
281  $self->{'_id_cache'}->{$dbID} = $ms;
282  $self->{'_code_cache'}->{lc($ms->code())} = $ms;
283  }
284 
285  return;
286 }
287 
288 =head2 update
289 
290  Arg [1] : Bio::EnsEMBL::MiscSet $miscset
291  Example : $adaptor->update($miscset)
292  Description: Updates this misc_set in the database
293  Returntype : int 1 if update is performed, undef if it is not
294  Exceptions : throw if arg is not an misc_set object
295  Caller : ?
296  Status : Stable
297 
298 =cut
299 
300 sub update {
301  my $self = shift;
302  my $m = shift;
303 
304  if (!ref($m) || !$m->isa('Bio::EnsEMBL::MiscSet')) {
305  throw("Expected Bio::EnsEMBL::MiscSet argument.");
306  }
307 
308  if(!$m->is_stored($self->db())) {
309  return undef;
310  }
311 
312  my $sth = $self->prepare("UPDATE misc_set ".
313  "SET code =?, name =?, description = ?, max_length = ? ".
314  "WHERE misc_set_id = ?");
315 
316  $sth->bind_param(1,$m->code,SQL_VARCHAR);
317  $sth->bind_param(2,$m->name,SQL_VARCHAR);
318  $sth->bind_param(3,$m->description,SQL_VARCHAR);
319  $sth->bind_param(4,$m->longest_feature,SQL_INTEGER);
320  $sth->bind_param(5,$m->dbID,SQL_INTEGER);
321 
322  $sth->execute();
323  $sth->finish();
324 
325  # update the internal caches
326  $self->{'_id_cache'}->{$m->dbID} = $m;
327  $self->{'_code_cache'}->{lc($m->code())} = $m;
328 }
329 
330 1;
Bio::EnsEMBL::DBSQL::MiscFeatureAdaptor
Definition: MiscFeatureAdaptor.pm:40
Bio::EnsEMBL::Storable::dbID
public Int dbID()
Bio::EnsEMBL::MiscSet
Definition: MiscSet.pm:31
Bio::EnsEMBL::DBSQL::BaseAdaptor
Definition: BaseAdaptor.pm:71
Bio::EnsEMBL::MiscSet::new
public Bio::EnsEMBL::MiscSet new()
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68
Bio::EnsEMBL::DBSQL::MiscSetAdaptor
Definition: MiscSetAdaptor.pm:23
Bio::EnsEMBL::DBSQL::BaseAdaptor::fetch_all
public fetch_all()