3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
24 Please email comments or questions to the
public Ensembl
25 developers list at <http:
27 Questions may also be sent to the Ensembl help desk at
35 Alternative allele groupings
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;
48 # Get a list of AltAlleleGroups
49 my $list = $aag_adaptor->fetch_all_(
'IS_REPRESENTATIVE');
50 $list = $aag_adaptor->fetch_all();
52 my $dbID = $aag_adaptor->store($aag);
54 $aag = $aag_adaptor->fetch_by_dbID($dbID);
55 $aag_adaptor->remove($aag);
60 groups of alleles to be retrieved by group and gene ids.
64 package Bio::EnsEMBL::DBSQL::AltAlleleGroupAdaptor;
74 use DBI qw( :sql_types );
78 Arg[1] : (optional) String - type of group
79 Restrict group fetches to just one type. Technically it selects
80 out mixed-annotation groups where a single member contains that type.
81 Description : Fetches all the alt-allele groups, creates objects to represent
82 them and returns them in a list. Specifying a group type
83 identifies all groups containing a member of
this type. It
84 does not filter out the other members
86 Multispecies support is triggered by the is_multispecies flag
96 $type = uc($type)
if ($type);
103 if ($self->db->is_multispecies()) {
104 # multispecies databases must be restricted in their treatment
105 $species_id = $self->db->species_id;
109 SELECT DISTINCT alt_allele_group_id
111 JOIN gene g ON g.gene_id = a.gene_id
112 JOIN seq_region s ON s.seq_region_id = g.seq_region_id
113 JOIN coord_system c ON c.coord_system_id = s.coord_system_id
114 JOIN alt_allele_attrib b ON a.alt_allele_id = b.alt_allele_id
115 WHERE c.species_id = ? AND b.attrib = ?
119 SELECT DISTINCT alt_allele_group_id
121 JOIN gene g ON g.gene_id = a.gene_id
122 JOIN seq_region s ON s.seq_region_id = g.seq_region_id
123 JOIN coord_system c ON c.coord_system_id = s.coord_system_id
124 WHERE c.species_id = ?
129 $get_all_sql = q(SELECT DISTINCT alt_allele_group_id
130 FROM alt_allele a, alt_allele_attrib b
131 WHERE a.alt_allele_id = b.alt_allele_id
134 $get_all_sql = q(SELECT DISTINCT alt_allele_group_id FROM alt_allele);
139 my $sth = $self->prepare($get_all_sql);
142 if ($self->db->is_multispecies()) {
143 $sth->bind_param($x,$species_id, SQL_INTEGER);
147 $sth->bind_param($x,$type, SQL_VARCHAR)
if ($type);
148 eval { $sth->execute() };
150 throw(
"Query error in AltAlleleGroupAdaptor: $@");
155 $sth->bind_col(1, \$group_id );
157 while ( $sth->fetch() ) {
158 my $aag = $self->fetch_by_dbID($group_id);
159 push @group_list, $aag;
168 Description : Creates and returns an
AltAlleleGroup for the given group
id
176 my $group_id = shift;
180 my $get_alt_allele_sql = q(
181 SELECT alt_allele_id, gene_id FROM alt_allele
182 WHERE alt_allele_group_id = ? ORDER BY alt_allele_id
184 my $sth = $self->prepare($get_alt_allele_sql);
186 $sth->bind_param(1,$group_id, SQL_INTEGER);
189 my ($alt_allele_id, $gene_id);
190 $sth->bind_columns( \($alt_allele_id,$gene_id) );
192 my $attrib_fetch = q(
193 SELECT attrib FROM alt_allele_attrib WHERE alt_allele_id = ?
195 my $attrib_sth = $self->prepare($attrib_fetch);
198 while ($sth->fetch()) {
199 # fetch alt_allele attributes
200 $attrib_sth->execute($alt_allele_id);
201 $attrib_sth->bind_col(1,\$attrib);
203 while ($attrib_sth->fetch) {
204 $attrib_list{$attrib} = 1;
206 push @members,[$gene_id, \%attrib_list];
211 if ($group_id && scalar(@members) > 0) {
214 -MEMBERS => \@members,
222 =head2 fetch_by_gene_id
224 Arg[1] : Integer Gene ID of the member to query by
225 Description : Returns an AltAlleleGroup which contains
226 the specified gene member
231 sub fetch_by_gene_id {
232 my ($self, $gene_id) = @_;
234 my $aag_list = $self->fetch_all_by_gene_id($gene_id);
236 return unless @$aag_list;
238 # return first group from list
239 my $group_id = $aag_list->[0]->
dbID;
240 return $self->fetch_by_dbID($group_id);
243 =head2 fetch_all_by_gene_id
245 Arg[1] : Integer Gene ID of the member to query by
246 Description : Returns an array of one or more AltAlleleGroups,
247 which each contain the specified gene member
252 sub fetch_all_by_gene_id {
253 my ($self, $gene_id) = @_;
256 SELECT alt_allele_group_id FROM alt_allele
257 WHERE gene_id = ? ORDER BY alt_allele_group_id
259 my $sth = $self->prepare($gene_id_sql);
260 $sth->bind_param(1,$gene_id, SQL_INTEGER);
263 my $group_ids = $sth->fetchall_arrayref();
267 if (! $@ && @$group_ids) {
268 foreach my $group (@$group_ids) {
269 push(@aag, $self->fetch_by_dbID($group->[0]));
278 Description: Used
for persisting
new groups to the database.
279 It updates the dbID of the
object handed to it to match the
281 Returntype : Integer Alt Allele Group
id
287 my $allele_group = shift;
289 assert_ref($allele_group,
'Bio::EnsEMBL::AltAlleleGroup',
'allele_group');
290 if ($allele_group->size < 2) {
291 warning(
'At least 2 genes must be provided to construct alternative alleles. Ignoring.');
295 my $helper = $self->dbc()->sql_helper();
296 my $dbID = $allele_group->dbID;
298 my $new_group_sql =
'INSERT INTO alt_allele_group (alt_allele_group_id) VALUES (?)';
299 my $existing_group_sql =
'SELECT count(*) FROM alt_allele_group WHERE alt_allele_group_id = ?';
301 my $already_exists = $helper->execute_single_result(-SQL => $existing_group_sql, -PARAMS => [[$dbID, SQL_INTEGER]]);
303 # If the ID is not already there then we need to add one
304 if($already_exists == 0) {
305 $helper->execute_update(-SQL => $new_group_sql, -CALLBACK => sub {
306 my ($sth, $dbh, $rv) = @_;
308 my $id = $dbh->last_insert_id(undef, undef,
'alt_allele_group',
'alt_allele_group_id');
315 my $sth = $self->prepare(
"INSERT INTO alt_allele (alt_allele_id, alt_allele_group_id, gene_id) VALUES (?,?,?)");
316 my $attrib_sth = $self->prepare(
"INSERT INTO alt_allele_attrib (alt_allele_id,attrib) VALUES (?,?)");
317 my $check_exists_sth = $self->prepare(
"SELECT alt_allele_id FROM alt_allele WHERE gene_id = ?");
319 foreach my $allele (@{ $allele_group->get_all_members() }) {
320 my $gene_id = $allele->[0];
321 my %flags = %{$allele->[1]};
324 # Check if gene is not already stored
325 # Return allele_id if it is
326 $check_exists_sth->bind_param(1, $gene_id, SQL_INTEGER);
327 $check_exists_sth->execute();
328 $check_exists_sth->bind_col(1, \$allele_id);
329 if ($check_exists_sth->fetch() ) {
333 $sth->bind_param(1, undef, SQL_INTEGER);
334 $sth->bind_param(2, $dbID, SQL_INTEGER);
335 $sth->bind_param(3, $gene_id, SQL_INTEGER);
336 my $altered_rows = $sth->execute();
337 if ($altered_rows > 0) {
338 $allele_id = $self->last_insert_id(); # all alleles get added to the same alt_allele_id group
340 throw(
"Creation of new alt_allele failed: $@");
344 foreach my $flag (keys %flags) {
345 $attrib_sth->bind_param(1, $allele_id);
346 $attrib_sth->bind_param(2, $flag);
347 $attrib_sth->execute();
350 if ($@) {
throw (
"Problem inserting new AltAlleleGroup into database: $@");}
353 $check_exists_sth->finish;
355 $allele_group->dbID($dbID);
362 Arg [1] : AltAlleleGroup
363 Description: Removes the existing DB record of an AltAlleleGroup and stores
365 Returntype : Integer - the
return value of the store method, viz. whether the
366 insert was successful.
371 my $allele_group = shift;
372 assert_ref($allele_group,
'Bio::EnsEMBL::AltAlleleGroup',
'allele_group');
373 throw "Cannot update an AltAlleleGroup without a dbID. AltAlleleGroups should be fetched from the DB prior to updating them" if ! $allele_group->dbID();
375 $self->remove($allele_group, $keep_group);
376 return $self->store($allele_group);
381 Arg [1] : The AltAlleleGroup to remove.
382 Arg [2] : Boolean indicates
if the entry in alt_allele_group should be retained or remove. Defaults to removing the entry
383 Example : $aaga->remove($alt_allele_group);
384 Description: This removes an AltAlleleGroup from all tables of the database.
390 my ($self, $allele_group, $keep_group) = @_;
391 assert_ref($allele_group,
'Bio::EnsEMBL::AltAlleleGroup',
'allele_group');
393 my $helper = $self->dbc()->sql_helper();
394 my $delete_attribs_sql;
395 if ($self->dbc->driver() eq
'mysql') {
396 $delete_attribs_sql = q{
398 FROM alt_allele_attrib aaa
399 JOIN alt_allele aa
using (alt_allele_id)
400 where alt_allele_group_id =?
404 $delete_attribs_sql = q{
405 DELETE FROM alt_allele_attrib WHERE alt_allele_id IN (
406 SELECT alt_allele_id FROM alt_allele WHERE alt_allele_group_id = ?
410 my $delete_alt_alleles_sql =
'DELETE FROM alt_allele where alt_allele_group_id =?';
411 my $delete_group_sql =
'DELETE from alt_allele_group where alt_allele_group_id =?';
412 my $params = [[$allele_group->dbID, SQL_INTEGER]];
414 $helper->execute_update(-SQL => $delete_attribs_sql, -PARAMS => $params);
415 $helper->execute_update(-SQL => $delete_alt_alleles_sql, -PARAMS => $params);
417 $helper->execute_update(-SQL => $delete_group_sql, -PARAMS => $params);
424 return ([
'alt_allele',
'a'], [
'alt_allele_group',
'g'], [
'alt_allele_attrib',
'b']);