Bio-EnsEMBL

 view release on metacpan or  search on metacpan

lib/Bio/EnsEMBL/DBSQL/AltAlleleGroupAdaptor.pm  view on Meta::CPAN

                $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;
}

=head2 update

  Arg [1]    : AltAlleleGroup 
  Description: Removes the existing DB record of an AltAlleleGroup and stores 
               the altered version.
  Returntype : Integer - the return value of the store method, viz. whether the
               insert was successful.
=cut

sub update {
    my $self = shift;
    my $allele_group = shift;
    assert_ref($allele_group, 'Bio::EnsEMBL::AltAlleleGroup', 'allele_group');
    throw "Cannot update an AltAlleleGroup without a dbID. AltAlleleGroups should be fetched from the DB prior to updating them" if ! $allele_group->dbID();
    my $keep_group = 1;
    $self->remove($allele_group, $keep_group);
    return $self->store($allele_group);
}

=head2 remove

  Arg [1]    : The AltAlleleGroup to remove.
  Arg [2]    : Boolean indicates if the entry in alt_allele_group should be retained or remove. Defaults to removing the entry
  Example    : $aaga->remove($alt_allele_group);
  Description: This removes an AltAlleleGroup from all tables of the database. 
  Exceptions : None
  
=cut

sub remove {
    my ($self, $allele_group, $keep_group) = @_;
    assert_ref($allele_group, 'Bio::EnsEMBL::AltAlleleGroup', 'allele_group');

    my $helper = $self->dbc()->sql_helper();
    my $delete_attribs_sql;
    if ($self->dbc->driver() eq 'mysql') {
      $delete_attribs_sql = q{
        DELETE aaa 
        FROM alt_allele_attrib aaa 
        JOIN alt_allele aa using (alt_allele_id) 
        where alt_allele_group_id =?
      };
    }
    else {
      $delete_attribs_sql = q{
        DELETE FROM alt_allele_attrib WHERE alt_allele_id IN (
            SELECT alt_allele_id FROM alt_allele WHERE alt_allele_group_id = ?
        )
      };
    }
    my $delete_alt_alleles_sql = 'DELETE FROM alt_allele where alt_allele_group_id =?';
    my $delete_group_sql = 'DELETE from alt_allele_group where alt_allele_group_id =?';
    my $params = [[$allele_group->dbID, SQL_INTEGER]];

    $helper->execute_update(-SQL => $delete_attribs_sql, -PARAMS => $params);
    $helper->execute_update(-SQL => $delete_alt_alleles_sql, -PARAMS => $params);
    if(! $keep_group) {
        $helper->execute_update(-SQL => $delete_group_sql, -PARAMS => $params);
    }

    return;
}

sub _tables {
    return (['alt_allele', 'a'], ['alt_allele_group', 'g'], ['alt_allele_attrib', 'b']);
}

1;



( run in 0.536 second using v1.01-cache-2.11-cpan-140bd7fdf52 )