EnsEMBL/DBSQL/AltAlleleGroupAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 179 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 246 CC 20 SCAR 34.5
CC
20
Cov
0%
fetch_all
72.0
store
56.0
remove
12.0
CRAP 31.4
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  
22 =head1 CONTACT
23  
24 Please email comments or questions to the public Ensembl
25 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
26  
27 Questions may also be sent to the Ensembl help desk at
28 <http://www.ensembl.org/Help/Contact>.
29  
30 =cut
31  
32 =head1 NAME
33  
34 Bio::EnsEMBL::DBSQL::AltAlleleGroupAdaptor - Adaptor for the manipulation of
35 Alternative allele groupings
36  
37 =head1 SYNOPSIS
38  
39 use Bio::EnsEMBL::AltAlleleGroup;
40 use Bio::EnsEMBL::DBSQL::AltAlleleGroupAdaptor;
41
42 my $aag_adaptor = Bio::EnsEMBL::Registry->get_DBAdaptor("Human","core","AltAlleleGroup");
43
44 # For a known Gene, find the reference alternative allele
45 my $aag = $aag_adaptor->fetch_by_gene_id($gene->dbID);
46 my $reference_gene = $aag->get_ref_Gene;
47
48 # Get a list of AltAlleleGroups
49 my $list = $aag_adaptor->fetch_all_('IS_REPRESENTATIVE');
50 $list = $aag_adaptor->fetch_all();
51
52 my $dbID = $aag_adaptor->store($aag);
53
54 $aag = $aag_adaptor->fetch_by_dbID($dbID);
55 $aag_adaptor->remove($aag);
56  
57 =head1 DESCRIPTION
58  
59 The AltAlleleGroupAdaptor provides CRUD for AltAlleleGroup objects. It allows
60 groups of alleles to be retrieved by group and gene ids.
61  
62 =cut
63  
64 package Bio::EnsEMBL::DBSQL::AltAlleleGroupAdaptor;
65 $Bio::EnsEMBL::DBSQL::AltAlleleGroupAdaptor::VERSION = '114.0.0_50'; # TRIAL
66 $Bio::EnsEMBL::DBSQL::AltAlleleGroupAdaptor::VERSION = '114.0.050';
67 use strict;
68 use warnings;
69  
70 use base qw/Bio::EnsEMBL::DBSQL::BaseAdaptor/;
71  
72 use Bio::EnsEMBL::AltAlleleGroup;
73 use Bio::EnsEMBL::Utils::Exception qw/throw warning/;
74 use Bio::EnsEMBL::Utils::Scalar qw/assert_ref/;
75 use DBI qw( :sql_types );
76  
77 =head2 fetch_all
78  
79 Arg[1] : (optional) String - type of group
80 Restrict group fetches to just one type. Technically it selects
81 out mixed-annotation groups where a single member contains that type.
82 Description : Fetches all the alt-allele groups, creates objects to represent
83 them and returns them in a list. Specifying a group type
84 identifies all groups containing a member of this type. It
85 does not filter out the other members
86
87 Multispecies support is triggered by the is_multispecies flag
88 and species_id of the DBAdaptor.
89 Returntype : ArrayRef of Bio::EnsEMBL::AltAlleleGroup
90  
91 =cut
92  
93 sub fetch_all {
94 my $self = shift;
95 my $type = shift;
96  
97 $type = uc($type) if ($type);
98
99 my @group_list = ();
100 my @members;
101
102 my $species_id;
103 my $get_all_sql;
104 if ($self->db->is_multispecies()) {
105 # multispecies databases must be restricted in their treatment
106 $species_id = $self->db->species_id;
107
108 if ($type) {
109 $get_all_sql = q(
110 SELECT DISTINCT alt_allele_group_id
111 FROM alt_allele a
112 JOIN gene g ON g.gene_id = a.gene_id
113 JOIN seq_region s ON s.seq_region_id = g.seq_region_id
114 JOIN coord_system c ON c.coord_system_id = s.coord_system_id
115 JOIN alt_allele_attrib b ON a.alt_allele_id = b.alt_allele_id
116 WHERE c.species_id = ? AND b.attrib = ?
117 );
118 } else {
119 $get_all_sql = q(
120 SELECT DISTINCT alt_allele_group_id
121 FROM alt_allele a
122 JOIN gene g ON g.gene_id = a.gene_id
123 JOIN seq_region s ON s.seq_region_id = g.seq_region_id
124 JOIN coord_system c ON c.coord_system_id = s.coord_system_id
125 WHERE c.species_id = ?
126 );
127 }
128 } else {
129 if ($type) {
130 $get_all_sql = q(SELECT DISTINCT alt_allele_group_id
131 FROM alt_allele a, alt_allele_attrib b
132 WHERE a.alt_allele_id = b.alt_allele_id
133 AND b.attrib = ?);
134 } else {
135 $get_all_sql = q(SELECT DISTINCT alt_allele_group_id FROM alt_allele);
136 }
137
138 }
139
140 my $sth = $self->prepare($get_all_sql);
141
142 my $x = 1;
143 if ($self->db->is_multispecies()) {
144 $sth->bind_param($x,$species_id, SQL_INTEGER);
145 $x++;
146 }
147
148 $sth->bind_param($x,$type, SQL_VARCHAR) if ($type);
149 eval { $sth->execute() };
150 if ($@) {
151 throw("Query error in AltAlleleGroupAdaptor: $@");
152 }
153
154
155 my $group_id;
156 $sth->bind_col(1, \$group_id );
157
158 while ( $sth->fetch() ) {
159 my $aag = $self->fetch_by_dbID($group_id);
160 push @group_list, $aag;
161 }
162 $sth->finish;
163 return \@group_list;
164 }
165  
166 =head2 fetch_by_dbID
167  
168 Arg[1] : AltAlleleGroup dbID.
169 Description : Creates and returns an AltAlleleGroup for the given group id
170
171 Returntype : Bio::EnsEMBL::AltAlleleGroup
172  
173 =cut
174  
175 sub fetch_by_dbID {
176 my $self = shift;
177 my $group_id = shift;
178
179 my @members;
180
181 my $get_alt_allele_sql = q(
182 SELECT alt_allele_id, gene_id FROM alt_allele
183 WHERE alt_allele_group_id = ? ORDER BY alt_allele_id
184 );
185 my $sth = $self->prepare($get_alt_allele_sql);
186
187 $sth->bind_param(1,$group_id, SQL_INTEGER);
188
189 $sth->execute();
190 my ($alt_allele_id, $gene_id);
191 $sth->bind_columns( \($alt_allele_id,$gene_id) );
192
193 my $attrib_fetch = q(
194 SELECT attrib FROM alt_allele_attrib WHERE alt_allele_id = ?
195 );
196 my $attrib_sth = $self->prepare($attrib_fetch);
197 my $attrib;
198
199 while ($sth->fetch()) {
200 # fetch alt_allele attributes
201 $attrib_sth->execute($alt_allele_id);
202 $attrib_sth->bind_col(1,\$attrib);
203 my %attrib_list;
204 while ($attrib_sth->fetch) {
205 $attrib_list{$attrib} = 1;
206 }
207 push @members,[$gene_id, \%attrib_list];
208 }
209 $attrib_sth->finish;
210 $sth->finish;
211
212 if ($group_id && scalar(@members) > 0) {
213 my $aag = Bio::EnsEMBL::AltAlleleGroup->new(
214 -dbID => $group_id,
215 -MEMBERS => \@members,
216 -ADAPTOR => $self,
217 );
218 return $aag;
219 }
220 return;
221 }
222  
223 =head2 fetch_by_gene_id
224  
225 Arg[1] : Integer Gene ID of the member to query by
226 Description : Returns an AltAlleleGroup which contains
227 the specified gene member
228 Returntype : Bio::EnsEMBL::AltAlleleGroup
229  
230 =cut
231  
232 sub fetch_by_gene_id {
233 my ($self, $gene_id) = @_;
234  
235 my $aag_list = $self->fetch_all_by_gene_id($gene_id);
236  
237 return unless @$aag_list;
238  
239 # return first group from list
240 my $group_id = $aag_list->[0]->dbID;
241 return $self->fetch_by_dbID($group_id);
242 }
243  
244 =head2 fetch_all_by_gene_id
245  
246 Arg[1] : Integer Gene ID of the member to query by
247 Description : Returns an array of one or more AltAlleleGroups,
248 which each contain the specified gene member
249 Returntype : Array of Bio::EnsEMBL::AltAlleleGroup objects
250  
251 =cut
252  
253 sub fetch_all_by_gene_id {
254 my ($self, $gene_id) = @_;
255  
256 my $gene_id_sql = q(
257 SELECT alt_allele_group_id FROM alt_allele
258 WHERE gene_id = ? ORDER BY alt_allele_group_id
259 );
260 my $sth = $self->prepare($gene_id_sql);
261 $sth->bind_param(1,$gene_id, SQL_INTEGER);
262 $sth->execute;
263  
264 my $group_ids = $sth->fetchall_arrayref();
265 $sth->finish;
266  
267 my @aag;
268 if (! $@ && @$group_ids) {
269 foreach my $group (@$group_ids) {
270 push(@aag, $self->fetch_by_dbID($group->[0]));
271 }
272 }
273 return \@aag;
274 }
275  
276 =head2 store
277  
278 Arg[1] : Bio::EnsEMBL::AltAlleleGroup
279 Description: Used for persisting new groups to the database.
280 It updates the dbID of the object handed to it to match the
281 database.
282 Returntype : Integer Alt Allele Group id
283  
284 =cut
285  
286 sub store {
287 my $self = shift;
288 my $allele_group = shift;
289  
290 assert_ref($allele_group, 'Bio::EnsEMBL::AltAlleleGroup', 'allele_group');
291 if ($allele_group->size < 2) {
292 warning('At least 2 genes must be provided to construct alternative alleles. Ignoring.');
293 return;
294 }
295
296 my $helper = $self->dbc()->sql_helper();
297 my $dbID = $allele_group->dbID;
298
299 my $new_group_sql = 'INSERT INTO alt_allele_group (alt_allele_group_id) VALUES (?)';
300 my $existing_group_sql = 'SELECT count(*) FROM alt_allele_group WHERE alt_allele_group_id = ?';
301  
302 my $already_exists = $helper->execute_single_result(-SQL => $existing_group_sql, -PARAMS => [[$dbID, SQL_INTEGER]]);
303
304 # If the ID is not already there then we need to add one
305 if($already_exists == 0) {
306 $helper->execute_update(-SQL => $new_group_sql, -CALLBACK => sub {
307 my ($sth, $dbh, $rv) = @_;
308 if($rv) {
309 my $id = $dbh->last_insert_id(undef, undef, 'alt_allele_group', 'alt_allele_group_id');
310 $dbID = $id;
311 }
312 return;
313 });
314 }
315
316 my $sth = $self->prepare("INSERT INTO alt_allele (alt_allele_id, alt_allele_group_id, gene_id) VALUES (?,?,?)");
317 my $attrib_sth = $self->prepare("INSERT INTO alt_allele_attrib (alt_allele_id,attrib) VALUES (?,?)");
318 my $check_exists_sth = $self->prepare("SELECT alt_allele_id FROM alt_allele WHERE gene_id = ?");
319  
320 foreach my $allele (@{ $allele_group->get_all_members() }) {
321 my $gene_id = $allele->[0];
322 my %flags = %{$allele->[1]};
323 my $allele_id;
324  
325 # Check if gene is not already stored
326 # Return allele_id if it is
327 $check_exists_sth->bind_param(1, $gene_id, SQL_INTEGER);
328 $check_exists_sth->execute();
329 $check_exists_sth->bind_col(1, \$allele_id);
330 if ($check_exists_sth->fetch() ) {
331 return $allele_id;
332 }
333
334 $sth->bind_param(1, undef, SQL_INTEGER);
335 $sth->bind_param(2, $dbID, SQL_INTEGER);
336 $sth->bind_param(3, $gene_id, SQL_INTEGER);
337 my $altered_rows = $sth->execute();
338 if ($altered_rows > 0) {
339 $allele_id = $self->last_insert_id(); # all alleles get added to the same alt_allele_id group
340 } else {
341 throw("Creation of new alt_allele failed: $@");
342 }
343
344
345 foreach my $flag (keys %flags) {
346 $attrib_sth->bind_param(1, $allele_id);
347 $attrib_sth->bind_param(2, $flag);
348 $attrib_sth->execute();
349 }
350 }
351 if ($@) {throw ("Problem inserting new AltAlleleGroup into database: $@");}
352 $sth->finish;
353 $attrib_sth->finish;
354 $check_exists_sth->finish;
355
356 $allele_group->dbID($dbID);
357
358 return $dbID;
359 }
360  
361 =head2 update
362  
363 Arg [1] : AltAlleleGroup
364 Description: Removes the existing DB record of an AltAlleleGroup and stores
365 the altered version.
366 Returntype : Integer - the return value of the store method, viz. whether the
367 insert was successful.
368 =cut
369  
370 sub update {
371 my $self = shift;
372 my $allele_group = shift;
373 assert_ref($allele_group, 'Bio::EnsEMBL::AltAlleleGroup', 'allele_group');
374 throw "Cannot update an AltAlleleGroup without a dbID. AltAlleleGroups should be fetched from the DB prior to updating them" if ! $allele_group->dbID();
375 my $keep_group = 1;
376 $self->remove($allele_group, $keep_group);
377 return $self->store($allele_group);
378 }
379  
380 =head2 remove
381  
382 Arg [1] : The AltAlleleGroup to remove.
383 Arg [2] : Boolean indicates if the entry in alt_allele_group should be retained or remove. Defaults to removing the entry
384 Example : $aaga->remove($alt_allele_group);
385 Description: This removes an AltAlleleGroup from all tables of the database.
386 Exceptions : None
387
388 =cut
389  
390 sub remove {
391 my ($self, $allele_group, $keep_group) = @_;
392 assert_ref($allele_group, 'Bio::EnsEMBL::AltAlleleGroup', 'allele_group');
393  
394 my $helper = $self->dbc()->sql_helper();
395 my $delete_attribs_sql;
396 if ($self->dbc->driver() eq 'mysql') {
397 $delete_attribs_sql = q{
398 DELETE aaa
399 FROM alt_allele_attrib aaa
400 JOIN alt_allele aa using (alt_allele_id)
401 where alt_allele_group_id =?
402 };
403 }
404 else {
405 $delete_attribs_sql = q{
406 DELETE FROM alt_allele_attrib WHERE alt_allele_id IN (
407 SELECT alt_allele_id FROM alt_allele WHERE alt_allele_group_id = ?
408 )
409 };
410 }
411 my $delete_alt_alleles_sql = 'DELETE FROM alt_allele where alt_allele_group_id =?';
412 my $delete_group_sql = 'DELETE from alt_allele_group where alt_allele_group_id =?';
413 my $params = [[$allele_group->dbID, SQL_INTEGER]];
414  
415 $helper->execute_update(-SQL => $delete_attribs_sql, -PARAMS => $params);
416 $helper->execute_update(-SQL => $delete_alt_alleles_sql, -PARAMS => $params);
417 if(! $keep_group) {
418 $helper->execute_update(-SQL => $delete_group_sql, -PARAMS => $params);
419 }
420  
421 return;
422 }
423  
424 sub _tables {
425 return (['alt_allele', 'a'], ['alt_allele_group', 'g'], ['alt_allele_attrib', 'b']);
426 }
427  
428 1;