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