Archive Ensembl HomeArchive Ensembl Home
DensityTypeAdaptor.pm
Go to the documentation of this file.
00001 =head1 LICENSE
00002 
00003   Copyright (c) 1999-2012 The European Bioinformatics Institute and
00004   Genome Research Limited.  All rights reserved.
00005 
00006   This software is distributed under a modified Apache license.
00007   For license details, please see
00008 
00009     http://www.ensembl.org/info/about/code_licence.html
00010 
00011 =head1 CONTACT
00012 
00013   Please email comments or questions to the public Ensembl
00014   developers list at <dev@ensembl.org>.
00015 
00016   Questions may also be sent to the Ensembl help desk at
00017   <helpdesk@ensembl.org>.
00018 
00019 =cut
00020 
00021 =head1 NAME
00022 
00023 Bio::EnsEMBL::DBSQL::DensityTypeAdaptor
00024 
00025 =head1 SYNOPSIS
00026 
00027   my $density_type_adaptor =
00028     $registry->get_adaptor( 'Human', 'Core', 'DensityType' );
00029 
00030   my @density_types = @{ $density_type_adaptor->fetch_all() };
00031 
00032   my $dt = $density_type_adaptor->fetch_by_dbID(12);
00033 
00034 =head1 DESCRIPTION
00035 
00036 DensityTypeAdaptor - Performs database interaction for DensityType objects.
00037 
00038 =head1 METHODS
00039 
00040 =cut
00041 
00042 package Bio::EnsEMBL::DBSQL::DensityTypeAdaptor;
00043 use vars qw(@ISA);
00044 use strict;
00045 
00046 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
00047 
00048 use Bio::EnsEMBL::DensityType;
00049 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
00050 
00051 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
00052 
00053 
00054 
00055 =head2 new
00056 
00057   Arg [1]    : see superclass (Bio::EnsEMBL::DBSQL::BaseAdaptor) arguments
00058   Example    : #use this instead of the constructor directly:
00059                my $dta = $db_adaptor->get_DensityTypeAdaptor();
00060   Description: Constructor. Creates a new DensityTypeAdaptor
00061   Returntype : Bio::EnsEMBL::DBSQL::DensityTypeAdaptor
00062   Exceptions : none
00063   Caller     : DBAdaptor
00064   Status     : Stable
00065 
00066 =cut
00067 
00068 sub new {
00069   my $class = shift;
00070 
00071   my $self = $class->SUPER::new(@_);
00072 
00073   $self->{'dbID_cache'} = {};
00074 
00075   return $self;
00076 }
00077 
00078 
00079 
00080 =head2 fetch_all
00081 
00082   Arg [1]    : none
00083   Example    : my @density_types = @{$density_type_adaptor->fetch_all};
00084   Description: Retrieves every density type in the database.
00085                NOTE:  In a multi-species database, this method will
00086                return all the entries, not just the ones associated with
00087                the current species.
00088   Returntype : reference to list of Bio::EnsEMBL::DensityType objects
00089   Exceptions : none
00090   Caller     : general, new
00091   Status     : Stable
00092 
00093 =cut
00094 
00095 sub fetch_all {
00096   my $self = shift;
00097 
00098   my @out;
00099 
00100   my $sth = $self->prepare("SELECT density_type_id, analysis_id, block_size,".
00101                            "       value_type, region_features " .
00102                            "FROM density_type");
00103 
00104   $sth->execute();
00105 
00106   my($dbID, $analysis_id, $blk_size, $vtype, $region_features );
00107   $sth->bind_columns(\$dbID, \$analysis_id, \$blk_size, \$vtype, \$region_features );
00108 
00109   my $analysis_adaptor = $self->db->get_AnalysisAdaptor();
00110 
00111   while($sth->fetch()) {
00112     my $analysis = $analysis_adaptor->fetch_by_dbID($analysis_id);
00113 
00114 
00115     my $dt = Bio::EnsEMBL::DensityType->new(-ADAPTOR => $self,
00116                                             -DBID    => $dbID,
00117                                             -ANALYSIS => $analysis,
00118                                             -BLOCK_SIZE => $blk_size,
00119                         -REGION_FEATURES => $region_features,
00120                                             -VALUE_TYPE => $vtype);
00121 
00122     $self->{'dbID_cache'}->{$dbID} = $dt;
00123 
00124     push @out, $dt;
00125   }
00126 
00127   return \@out;
00128 }
00129 
00130 
00131 
00132 =head2 fetch_by_dbID
00133 
00134   Arg [1]    : int $dbID
00135   Example    : my $dt = $density_type_adaptor->fetch_by_dbID($dbID);
00136   Description: Retrieves a density type object via its internal identifier
00137   Returntype : Bio::EnsEMBL::DensityType
00138   Exceptions : throw if dbID argument not defined
00139   Caller     : general
00140   Status     : Stable
00141 
00142 =cut
00143 
00144 sub fetch_by_dbID {
00145   my $self = shift;
00146   my $dbID = shift;
00147 
00148   if(!defined($dbID)) {
00149     throw("dbID argument must be defined");
00150   }
00151 
00152   if($self->{'dbID_cache'}->{$dbID}) {
00153     return $self->{'dbID_cache'}->{$dbID};
00154   }
00155 
00156   # go back to database and refill caches
00157   $self->fetch_all();
00158 
00159   return $self->{'dbID_cache'}->{$dbID};
00160 }
00161 
00162 
00163 =head2 fetch_all_by_logic_name
00164 
00165   Arg [1]    : string $logic_name
00166   Example    : my @dts = @{$dtype_adaptor->fetch_all('repeat_coverage')};
00167   Description: Retrieves all density types with a given logic name.
00168                NOTE:  In a multi-species database, this method will
00169                return all the entries matching the search criteria, not
00170                just the ones associated with the current species.
00171   Returntype : reference to list of Bio::EnsEMBL::DensityTypes
00172   Exceptions : thrown if logic_name argument is not provided
00173   Caller     : general
00174   Status     : Stable
00175 
00176 =cut
00177 
00178 sub fetch_all_by_logic_name {
00179   my $self = shift;
00180   my $logic_name = shift;
00181 
00182   if(!defined($logic_name)) {
00183     throw("logic_name argument is required.");
00184   }
00185 
00186   my $analysis_adaptor = $self->db()->get_AnalysisAdaptor();
00187   my $analysis = $analysis_adaptor->fetch_by_logic_name($logic_name);
00188 
00189   return [] if(!$analysis);
00190 
00191   my $sth = $self->prepare("SELECT density_type_id, block_size,".
00192                            "       value_type, region_features " .
00193                            "FROM density_type " .
00194                            "WHERE analysis_id = ?");
00195   $sth->bind_param(1,$analysis->dbID,SQL_INTEGER);
00196   $sth->execute();
00197 
00198   my($dbID, $blk_size, $vtype, $region_features );
00199   $sth->bind_columns(\$dbID, \$blk_size, \$vtype, \$region_features);
00200 
00201   my @out;
00202 
00203   while($sth->fetch()) {
00204 
00205     my $dt = Bio::EnsEMBL::DensityType->new(-ADAPTOR => $self,
00206                                             -DBID    => $dbID,
00207                                             -ANALYSIS => $analysis,
00208                                             -BLOCK_SIZE => $blk_size,
00209                         -REGION_FEATURES => $region_features,
00210                                             -VALUE_TYPE => $vtype);
00211 
00212     $self->{'dbID_cache'}->{$dbID} = $dt;
00213 
00214     push @out, $dt;
00215   }
00216 
00217   return \@out;
00218 }
00219 
00220 
00221 =head2 store
00222 
00223   Arg [1]    : list of Bio::EnsEMBL::DensityType @dt
00224                the density types to store in the database
00225   Example    : $density_type->store(@density_types);
00226   Description: Stores a list of density type objects in the database
00227   Returntype : none
00228   Exceptions : thrown if @dt is not defined
00229                or if any elements of @dt are not Bio::EnsEMBL::DensityType 
00230   Caller     : general
00231   Status     : Stable
00232 
00233 =cut
00234 
00235 sub store {
00236   my ($self,@dt) = @_;
00237 
00238   if( scalar(@dt) == 0 ) {
00239     throw("Must call store with list of Density Types");
00240   }
00241 
00242   my $sth = $self->prepare
00243     ("INSERT IGNORE INTO density_type (analysis_id,".
00244                                   "block_size, value_type, region_features ) ". 
00245     "VALUES (?, ?, ?, ?)");
00246 
00247   my $db = $self->db();
00248   my $analysis_adaptor = $db->get_AnalysisAdaptor();
00249 
00250  FEATURE: foreach my $dt ( @dt ) {
00251     if( !ref $dt || !$dt->isa("Bio::EnsEMBL::DensityType") ) {
00252       throw("Density Type must be an Ensembl DensityType, " .
00253             "not a [".ref($dt)."]");
00254     }
00255 
00256     if($dt->is_stored($db)) {
00257       next FEATURE;
00258     }
00259 
00260     if(!defined($dt->analysis())) {
00261       throw("An analysis must be attached to the density type to be stored.");
00262     }
00263 
00264     #store the analysis if it has not been stored yet
00265     if(!$dt->analysis->is_stored($db)) {
00266       $analysis_adaptor->store($dt->analysis());
00267     }
00268     
00269     my $block_size = $dt->block_size();
00270     $block_size |= 0;
00271     my $region_features = $dt->region_features();
00272     $region_features |= 0;
00273 
00274     $sth->bind_param(1,$dt->analysis->dbID,SQL_INTEGER);
00275     $sth->bind_param(2,$block_size,SQL_INTEGER);
00276     $sth->bind_param(3,$dt->value_type,SQL_VARCHAR);
00277     $sth->bind_param(4,$region_features, SQL_VARCHAR);
00278     my $inserted = $sth->execute();
00279 
00280     my $dbID;
00281 
00282     # $inserted can be 0E0 which is true but equal to 0
00283     if(!$inserted || $inserted == 0) {
00284       # insert failed, presumably because was already stored in database
00285 
00286       my @dts=@{$self->fetch_all_by_logic_name($dt->analysis()->logic_name())};
00287       my ($stored_dt) = grep {$_->block_size() == $dt->block_size()} @dts;
00288       if(!$stored_dt) {
00289         throw("Could not retrieve or store DensityType from database.\n" .
00290               "Incorrect db permissions or missing density_type table?\n");
00291       }
00292       $dbID = $stored_dt->dbID();
00293     } else {
00294       $dbID = $sth->{'mysql_insertid'};
00295     }
00296 
00297     # next two lines are to set the density type as stored
00298     $dt->dbID($dbID);
00299     $dt->adaptor($self);
00300 
00301     $self->{'dbID_cache'}->{$dbID} = $dt;
00302   }
00303 }
00304 
00305 1;