Commit 836acd51 authored by Ian Longden's avatar Ian Longden
Browse files

Regulatory methods etc now moved to own database so no longer needed here

parent b762e22e
#
# EnsEMBL module for Bio::EnsEMBL::DBSQL::RegulatoryFactorAdaptor
#
# Copyright EMBL/EBI
#
# You may distribute this module under the same terms as perl itself
# POD documentation - main docs before the code
=head1 NAME
Bio::EnsEMBL::DBSQL::RegulatoryFactorAdaptor
=head1
$rma = $database_adaptor->get_RegulatoryFactorAdaptor();
$regulatory_factor = $rfa->fetch_by_dbID(132);
$regulatory_factor = $rfa->fetch_by_name('Factor1');
@regulatory_factors = $rfa->fetch_all_by_type("transcription_factor");
$rma->store($rm1, $rm2, $rm3);
=head1 DESCRIPTION
This is an adaptor for the retrieval and storage of RegulatoryFactor objects.
=head1 AUTHOR - Glenn Proctor
Email glenn@ebi.ac.uk
=head1 CONTACT
Post questions to the EnsEMBL developer list ensembl-dev@ebi.ac.uk
=head1 METHODS
=cut
package Bio::EnsEMBL::DBSQL::RegulatoryFactorAdaptor;
use strict;
use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::RegulatoryFactor;
use Bio::EnsEMBL::Utils::Exception qw(throw deprecate);
use vars qw(@ISA);
@ISA = ('Bio::EnsEMBL::DBSQL::BaseAdaptor');
=head2 fetch_by_dbID
Arg [1] : int $db_id
The database identifier for the RegulatoryFactor to obtain
Example : $regulatory_factor = $rma->fetch_by_dbID(4);
Description: Obtains a RegulatoryFactor object from the database via its
primary key.
Returntype : Bio::EnsEMBL::RegulatoryFactor
Exceptions : none
Caller : general, Bio::EnsEMBL::RegulatoryFactorAdaptor
Status : At Risk
: under development
=cut
sub fetch_by_dbID {
my( $self, $db_id ) = @_;
my ($rc) = @{$self->_generic_fetch("regulatory_factor_id = $db_id")};
return $rc;
}
=head2 fetch_by_name
Arg [1] : string $name
the name of the regulatory factor to obtain
Example : $rc = $rma->fetch_by_name('Factor');
Description: Obtains a regulatory factor from the database via its name
Returntype : Bio::EnsEMBL::RegulatoryFactor
Exceptions : none
Caller : general
Status : At Risk
: under development
=cut
sub fetch_by_name {
my( $self, $name ) = @_;
my ($rc) = @{$self->_generic_fetch("name = '$name'")};
return $rc;
}
=head2 fetch_all_by_type
Arg [1] : string $type
the type of regulatory factor to obtain
Example : $rm = $rma->fetch_all_by_type('promoter');
Description: Obtains all regulatory factors of a particular type
Returntype : listREF of Bio::EnsEMBL::RegulatoryFactors
Exceptions : none
Caller : general
Status : At Risk
: under development
=cut
sub fetch_all_by_type {
my( $self, $type) = @_;
return $self->_generic_fetch("type = '$type'");
}
=head2 _generic_fetch
Arg [1] : string $where_clause
Example : none
Description: PRIVATE used to create RegulatoryFactor features from an
SQL constraint
Returntype : listref of Bio::EnsEMBL::RegulatoryFactor objects
Exceptions : none
Caller : internal
Status : At Risk
: under development
=cut
sub _generic_fetch {
my ($self, $where_clause) = @_;
my ($regulatory_factor_id, $name, $type);
my $sth = $self->prepare(qq{
SELECT regulatory_factor_id, name, type
FROM regulatory_factor
WHERE }. $where_clause);
$sth->execute;
$sth->bind_columns(\$regulatory_factor_id, \$name, \$type);
my @factors;
while ($sth->fetch) {
push @factors, Bio::EnsEMBL::RegulatoryFactor->new(-DBID => $regulatory_factor_id,
-NAME => $name,
-TYPE => $type,
-ADAPTOR => $self);
}
return \@factors;
}
=head2 store
Arg [1] : list of Bio::EnsEMBL::RegulatoryFactors @factors
Example : $rma->store(@factors);
Description: stores a list of RegulatoryFactor objects in the database
Returntype : none
Exceptions : none
Caller : ?
Status : At Risk
: under development
=cut
sub store {
my( $self, @factors ) = @_;
my $sth = $self->prepare("INSERT into regulatory_factor (name, type) VALUES (?,?)");
foreach my $rm (@factors) {
my $name = $rm->name or throw("name not set");
my $type = $rm->type or throw("type not set");
$sth->bind_param(1,$name,SQL_VARCHAR);
$sth->bind_param(2,$type,SQL_VARCHAR);
$sth->execute();
my $db_id = $sth->{'mysql_insertid'}
or throw("Didn't get an insertid from the INSERT statement");
$rm->dbID($db_id);
$rm->adaptor($self);
}
}
=head2 fetch_factors_coded_for_by_gene
Arg [1] : Bio::EnsEMBL::Gene
Example : $rfa->fetch_factors_coded_for_by_gene($gene)
Description: Fetches any regulatory_factors that are coded for by a particular gene
Returntype : Listref of Bio::Ensembl::RegulatoryFactor
Exceptions :
Caller : ?
Status : At Risk
: under development
=cut
sub fetch_factors_coded_for_by_gene {
my ($self, $gene) = @_;
if (!ref($gene) || !$gene->isa('Bio::EnsEMBL::Gene')) {
throw('Expected Bio::Ensembl::Gene argument not [' . ref($gene) .'].');
}
my @factors;
my ($factor_id);
my $sth = $self->db()->dbc()->prepare("SELECT regulatory_factor_id
FROM regulatory_factor_coding
WHERE gene_id=?");
$sth->bind_param(1,$gene->dbID,SQL_INTEGER);
$sth->execute();
$sth->bind_columns(\$factor_id);
while ($sth->fetch) {
push @factors, $self->fetch_by_dbID($factor_id);
}
return \@factors;
}
=head2 fetch_factors_coded_for_by_transcript
Arg [1] : Bio::EnsEMBL::Transcript
Example : $rfa->fetch_factors_coded_for_by_transcript($transcript)
Description: Fetches any regulatory_factors that are coded for by a particular transcript
Returntype : Listref of Bio::Ensembl::RegulatoryFactor
Exceptions :
Caller : ?
Status : At Risk
: under development
=cut
sub fetch_factors_coded_for_by_transcript {
my ($self, $transcript) = @_;
if (!ref($transcript) || !$transcript->isa('Bio::EnsEMBL::Transcript')) {
throw('Expected Bio::Ensembl::Transcript argument not [' . ref($transcript) .'].');
}
my @factors;
my ($factor_id);
my $sth = $self->db()->dbc()->prepare("SELECT regulatory_factor_id
FROM regulatory_factor_coding
WHERE transcript_id=?");
$sth->bind_param(1,$transcript->dbID,SQL_INTEGER);
$sth->execute();
$sth->bind_columns(\$factor_id);
while ($sth->fetch) {
push @factors, $self->fetch_by_dbID($factor_id);
}
return \@factors;
}
1;
__END__
#
# EnsEMBL module for Bio::EnsEMBL::DBSQL::RegulatoryFeatureAdaptor
#
# Copyright EMBL/EBI
#
# You may distribute this module under the same terms as perl itself
# POD documentation - main docs before the code
=head1 NAME
Bio::EnsEMBL::DBSQL::RegulatoryFeatureAdaptor
=head1 SYNOPSIS
$rfa = $database_adaptor->get_RegulatoryFeatureAdaptor();
my $regulatory_feature = $rfa->fetch_by_dbID(1234);
=head1 DESCRIPTION
This is an adaptor for the retrieval and storage of RegulatoryFeature objects
from the database. Most of the implementation is in the superclass BaseFeatureAdaptor.
=head1 AUTHOR - Glenn Proctor
=head1 CONTACT
Post questions to the EnsEMBL developer list: ensembl-dev@ebi.ac.uk
=head1 METHODS
=cut
package Bio::EnsEMBL::DBSQL::RegulatoryFeatureAdaptor;
use strict;
use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
use Bio::EnsEMBL::RegulatoryFeature;
use Bio::EnsEMBL::Utils::Exception qw(throw warning);
use vars qw(@ISA);
@ISA = qw(Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor);
=head2 fetch_all_by_Slice
Arg [1] : Bio::EnsEMBL::Slice $slice
Arg [2] : (optional) string $logic_name
Limits RepeatFeatures obtained to those having an Analysis with
of the specified logic_name. If no logic name is specified,
regulatory features of all analysis types are retrieved.
Example : @rfeats = @{$rfa->fetch_all_by_Slice($slice, 'miRanda')};
Description: Retrieves regulatory features overlapping the area designated by
the provided slice argument. Returned features will be in
in the same coordinate system as the provided slice and will
have coordinates relative to the slice start.
Returntype : reference to a list of Bio::EnsEMBL::RegulatoryFeatures.
Exceptions : throw on bad argument
Caller : Slice::get_all_RegulatoryFeatures
Status : At Risk
: under development
=cut
sub fetch_all_by_Slice {
my $self = shift;
my $slice = shift;
my $logic_name = shift;
my $result = $self->fetch_all_by_Slice_constraint($slice,undef,$logic_name);
return $result;
}
# _tablename
#
# Arg [1] : none
# Example : none
# Description: PROTECTED Implementation of abstract superclass method to
# provide the name of the tables to query
# Returntype : string
# Exceptions : none
# Caller : internal
sub _tables {
my $self = shift;
return (['regulatory_feature', 'rf'], ['regulatory_factor', 'rm']);
}
# _columns
#
# Arg [1] : none
# Example : none
# Description: PROTECTED Implementation of abstract superclass method to
# provide the name of the columns to query
# Returntype : list of strings
# Exceptions : none
# Caller : internal
sub _columns {
my $self = shift;
return qw (rf.regulatory_feature_id
rf.name
rf.seq_region_id
rf.seq_region_start
rf.seq_region_end
rf.seq_region_strand
rf.analysis_id
rf.regulatory_factor_id
rm.name
rm.type);
}
# _default_where_clause
# Arg [1] : none
# Example : none
# Description: Overrides superclass method to provide an additional
# table joining constraint before the SQL query is performed.
# Returntype : string
# Exceptions : none
# Caller : generic_fetch
#
sub _default_where_clause {
my $self = shift;
return 'rf.regulatory_factor_id = rm.regulatory_factor_id';
}
# Description: PROTECTED implementation of abstract superclass method.
# responsible for the creation of RegulatoryFeatures from a
# hashref generated from an SQL query
sub _objs_from_sth {
my ($self, $sth, $mapper, $dest_slice) = @_;
#
# This code is ugly because an attempt has been made to remove as many
# function calls as possible for speed purposes. Thus many caches and
# a fair bit of gymnastics is used.
#
my $rca = $self->db()->get_RegulatoryFactorAdaptor();
my $sa = $self->db()->get_SliceAdaptor();
my $aa = $self->db->get_AnalysisAdaptor();
my @features;
my %rm_hash;
my %analysis_hash;
my %slice_hash;
my %sr_name_hash;
my %sr_cs_hash;
my($regulatory_feature_id, $regulatory_feature_name, $seq_region_id,
$seq_region_start, $seq_region_end, $seq_region_strand, $analysis_id,
$factor_id, $factor_name, $factor_type);
$sth->bind_columns( \$regulatory_feature_id, \$regulatory_feature_name, \$seq_region_id,
\$seq_region_start, \$seq_region_end, \$seq_region_strand, \$analysis_id,
\$factor_id, \$factor_name, \$factor_type);
my $asm_cs;
my $cmp_cs;
my $asm_cs_vers;
my $asm_cs_name;
my $cmp_cs_vers;
my $cmp_cs_name;
if($mapper) {
$asm_cs = $mapper->assembled_CoordSystem();
$cmp_cs = $mapper->component_CoordSystem();
$asm_cs_name = $asm_cs->name();
$asm_cs_vers = $asm_cs->version();
$cmp_cs_name = $cmp_cs->name();
$cmp_cs_vers = $cmp_cs->version();
}
my $dest_slice_start;
my $dest_slice_end;
my $dest_slice_strand;
my $dest_slice_length;
my $dest_slice_sr_name;
my $dest_slice_sr_id;
if($dest_slice) {
$dest_slice_start = $dest_slice->start();
$dest_slice_end = $dest_slice->end();
$dest_slice_strand = $dest_slice->strand();
$dest_slice_length = $dest_slice->length();
$dest_slice_sr_name = $dest_slice->seq_region_name();
$dest_slice_sr_id = $dest_slice->get_seq_region_id();
}
FEATURE: while($sth->fetch()) {
#create a regulatory factor object
my $rm = $rm_hash{$factor_id} ||=
Bio::EnsEMBL::RegulatoryFactor->new_fast
({'dbID' => $factor_id,
'name' => $factor_name,
'type' => $factor_type});
#get the analysis object
my $analysis = $analysis_hash{$analysis_id} ||=
$aa->fetch_by_dbID($analysis_id);
my $slice = $slice_hash{"ID:".$seq_region_id};
if(!$slice) {
$slice = $sa->fetch_by_seq_region_id($seq_region_id);
$slice_hash{"ID:".$seq_region_id} = $slice;
$sr_name_hash{$seq_region_id} = $slice->seq_region_name();
$sr_cs_hash{$seq_region_id} = $slice->coord_system();
}
my $sr_name = $sr_name_hash{$seq_region_id};
my $sr_cs = $sr_cs_hash{$seq_region_id};
#
# remap the feature coordinates to another coord system
# if a mapper was provided
#
if($mapper) {
($seq_region_id,$seq_region_start,$seq_region_end,$seq_region_strand) =
$mapper->fastmap($sr_name, $seq_region_start, $seq_region_end,
$seq_region_strand, $sr_cs);
#skip features that map to gaps or coord system boundaries
next FEATURE if(!defined($seq_region_id));
#get a slice in the coord system we just mapped to
# if($asm_cs == $sr_cs || ($cmp_cs != $sr_cs && $asm_cs->equals($sr_cs))) {
$slice = $slice_hash{"ID:".$seq_region_id} ||=
$sa->fetch_by_seq_region_id($seq_region_id);
# } else {
# $slice = $slice_hash{"NAME:$sr_name:$asm_cs_name:$asm_cs_vers"} ||=
# $sa->fetch_by_region($asm_cs_name, $sr_name, undef, undef, undef,
# $asm_cs_vers);
# }
}
#
# If a destination slice was provided convert the coords
# If the dest_slice starts at 1 and is foward strand, nothing needs doing
#
if($dest_slice) {
if($dest_slice_start != 1 || $dest_slice_strand != 1) {
if($dest_slice_strand == 1) {
$seq_region_start = $seq_region_start - $dest_slice_start + 1;
$seq_region_end = $seq_region_end - $dest_slice_start + 1;
} else {
my $tmp_seq_region_start = $seq_region_start;
$seq_region_start = $dest_slice_end - $seq_region_end + 1;
$seq_region_end = $dest_slice_end - $tmp_seq_region_start + 1;
$seq_region_strand *= -1;
}
}
#throw away features off the end of the requested slice
if($seq_region_end < 1 || $seq_region_start > $dest_slice_length ||
( $dest_slice_sr_id ne $seq_region_id )) {
next FEATURE;
}
$slice = $dest_slice;
}
#finally, create the new regulatory feature
push @features, Bio::EnsEMBL::RegulatoryFeature->new_fast
( { 'analysis' => $analysis,
'name' => $regulatory_feature_name,
'start' => $seq_region_start,
'end' => $seq_region_end,
'strand' => $seq_region_strand,
'factor' => $rm,
'adaptor' => $self,
'slice' => $slice,
'dbID' => $regulatory_feature_id } );
}
return \@features;
}
=head2 fetch_all_by_factor
Arg [1] : Bio::EnsEMBL::RegulatoryFactor
the type of regulatory factor to obtain
Example : $rm = $rma->fetch_all_by_factor($factor);
Description: Obtains all regulatory features that correspond to a
particular regulatory factor
Returntype : listREF of Bio::EnsEMBL::RegulatoryFeatures
Exceptions : none
Caller : general
Status : At Risk
: under development
=cut
sub fetch_all_by_factor {
my( $self, $factor) = @_;
return $self->generic_fetch("rf.regulatory_factor_id = " . $factor->dbID());
}
=head2 fetch_all_by_factor_name
Arg [1] : String
the name of the regulatory factor to fetch by
Example : $rm = $rma->fetch_all_by_factor_name($factor_name);
Description: Obtains all regulatory features that correspond to a
particular regulatory factor
Returntype : listREF of Bio::EnsEMBL::RegulatoryFeatures
Exceptions : none
Caller : general
Status : At Risk
: under development