my $self = shift;
my $allele_group = shift;
assert_ref($allele_group, 'Bio::EnsEMBL::AltAlleleGroup', 'allele_group');
if ($allele_group->size < 2) {
warning('At least 2 genes must be provided to construct alternative alleles. Ignoring.');
return;
}
my $helper = $self->dbc()->sql_helper();
my $dbID = $allele_group->dbID;
my $new_group_sql = 'INSERT INTO alt_allele_group (alt_allele_group_id) VALUES (?)';
my $existing_group_sql = 'SELECT count(*) FROM alt_allele_group WHERE alt_allele_group_id = ?';
my $already_exists = $helper->execute_single_result(-SQL => $existing_group_sql, -PARAMS => [[$dbID, SQL_INTEGER]]);
# If the ID is not already there then we need to add one
if($already_exists == 0) {
$helper->execute_update(-SQL => $new_group_sql, -CALLBACK => sub {
my ($sth, $dbh, $rv) = @_;
if($rv) {
my $id = $dbh->last_insert_id(undef, undef, 'alt_allele_group', 'alt_allele_group_id');
$dbID = $id;
}
return;
});
}
my $sth = $self->prepare("INSERT INTO alt_allele (alt_allele_id, alt_allele_group_id, gene_id) VALUES (?,?,?)");
my $attrib_sth = $self->prepare("INSERT INTO alt_allele_attrib (alt_allele_id,attrib) VALUES (?,?)");
my $check_exists_sth = $self->prepare("SELECT alt_allele_id FROM alt_allele WHERE gene_id = ?");
foreach my $allele (@{ $allele_group->get_all_members() }) {
my $gene_id = $allele->[0];
my %flags = %{$allele->[1]};
my $allele_id;
# Check if gene is not already stored
# Return allele_id if it is
$check_exists_sth->bind_param(1, $gene_id, SQL_INTEGER);
$check_exists_sth->execute();
$check_exists_sth->bind_col(1, \$allele_id);
if ($check_exists_sth->fetch() ) {
return $allele_id;
}
$sth->bind_param(1, undef, SQL_INTEGER);
$sth->bind_param(2, $dbID, SQL_INTEGER);
$sth->bind_param(3, $gene_id, SQL_INTEGER);
my $altered_rows = $sth->execute();
if ($altered_rows > 0) {
$allele_id = $self->last_insert_id(); # all alleles get added to the same alt_allele_id group
} else {
throw("Creation of new alt_allele failed: $@");
}
foreach my $flag (keys %flags) {
$attrib_sth->bind_param(1, $allele_id);
$attrib_sth->bind_param(2, $flag);
$attrib_sth->execute();
}
}
if ($@) {throw ("Problem inserting new AltAlleleGroup into database: $@");}
$sth->finish;
$attrib_sth->finish;
$check_exists_sth->finish;
$allele_group->dbID($dbID);
return $dbID;
}