Archive Ensembl HomeArchive Ensembl Home
MiscSetAdaptor.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::MiscSetAdaptor - Provides database interaction for
00024 Bio::EnsEMBL::MiscSet objects.
00025 
00026 =head1 SYNOPSIS
00027 
00028   my $msa = $registry->get_adaptor( 'Human', 'Core', 'MiscSet' );
00029 
00030   my $misc_set = $msa->fetch_by_dbID(1234);
00031 
00032   $misc_set = $msa->fetch_by_code('clone');
00033 
00034 =head1 DESCRIPTION
00035 
00036 This class provides database interactivity for MiscSet objects.
00037 MiscSets are used to classify MiscFeatures into groups.
00038 
00039 =head1 METHODS
00040 
00041 =cut
00042 
00043 package Bio::EnsEMBL::DBSQL::MiscSetAdaptor;
00044 
00045 use strict;
00046 use warnings;
00047 
00048 use Bio::EnsEMBL::MiscSet;
00049 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
00050 
00051 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
00052 
00053 use vars qw(@ISA);
00054 
00055 @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);
00056 
00057 
00058 =head2 new
00059 
00060   Arg [...]  : Superclass args.  See Bio::EnsEMBL::DBSQL::BaseAdaptor
00061   Description: Instantiates a Bio::EnsEMBL::DBSQL::MiscSetAdaptor and
00062                caches the contents of the MiscSet table.
00063   Returntype : Bio::EnsEMBL::MiscSet
00064   Exceptions : none
00065   Caller     : MiscFeatureAdaptor
00066   Status     : Stable
00067 
00068 =cut
00069 
00070 
00071 sub new {
00072   my $class = shift;
00073 
00074   my $self = $class->SUPER::new(@_);
00075 
00076   $self->{'_id_cache'} = {};
00077   $self->{'_code_cache'} = {};
00078 
00079   # cache the entire contents of the misc set table
00080   # the table is small and it removes the need to repeatedly query the
00081   # table or join to the table
00082 
00083   $self->fetch_all();
00084 
00085   return $self;
00086 }
00087 
00088 
00089 
00090 
00091 =head2 fetch_all
00092 
00093   Arg [1]    : none
00094   Example    : foreach my $ms (@{$msa->fetch_all()}) {
00095                  print $ms->code(), ' ', $ms->name(), "\n";
00096                }
00097   Description: Retrieves every MiscSet defined in the DB.
00098                NOTE:  In a multi-species database, this method will
00099                return all the entries matching the search criteria, not
00100                just the ones associated with the current species.
00101   Returntype : listref of Bio::EnsEMBL::MiscSets
00102   Exceptions : none
00103   Caller     : general
00104   Status     : Stable
00105 
00106 =cut
00107 
00108 sub fetch_all {
00109   my $self = shift;
00110 
00111   my $sth = $self->prepare
00112     ('SELECT misc_set_id, code, name, description, max_length FROM misc_set');
00113 
00114   $sth->execute();
00115 
00116   my ($dbID, $code, $name, $desc, $max_len);
00117   $sth->bind_columns(\$dbID, \$code, \$name, \$desc, \$max_len);
00118 
00119   my @all;
00120 
00121   while($sth->fetch()) {
00122     my $ms = Bio::EnsEMBL::MiscSet->new
00123       (-DBID     => $dbID,
00124        -ADAPTOR  => $self,
00125        -CODE     => $code,
00126        -NAME     =>  $name,
00127        -DESCRIPTION => $desc,
00128        -LONGEST_FEATURE => $max_len);
00129 
00130     $self->{'_id_cache'}->{$dbID} = $ms;
00131     $self->{'_code_cache'}->{lc($code)} = $ms;
00132     push @all, $ms;
00133   }
00134 
00135   $sth->finish();
00136 
00137   return \@all;
00138 }
00139 
00140 
00141 
00142 =head2 fetch_by_dbID
00143 
00144   Arg [1]    : int $dbID
00145                The internal identifier of the misc set to retrieve
00146   Example    : my $ms = $msa->fetch_by_dbID($dbID);
00147   Description: Retrieves a misc set via its internal identifier
00148   Returntype : Bio::EnsEMBL::MiscSet
00149   Exceptions : none
00150   Caller     : general
00151   Status     : Stable
00152 
00153 =cut
00154 
00155 sub fetch_by_dbID {
00156   my $self = shift;
00157   my $dbID = shift;
00158 
00159   if(!$self->{'_id_cache'}->{$dbID}) {
00160     # on a cache miss reread the whole table and reload the cache
00161     $self->fetch_all();
00162   }
00163 
00164   return $self->{'_id_cache'}->{$dbID};
00165 }
00166 
00167 
00168 
00169 =head2 fetch_by_code
00170 
00171   Arg [1]    : string $code
00172                The unique code of the MiscSet to retrieve
00173   Example    : my $ms = $msa->fetch_by_code('clone');
00174   Description: Retrieves a MiscSet via its code
00175   Returntype : Bio::EnsEMBL::MiscSet
00176   Exceptions : none
00177   Caller     : general
00178   Status     : Stable
00179 
00180 =cut
00181 
00182 sub fetch_by_code {
00183   my $self = shift;
00184   my $code = shift;
00185 
00186   if(!$self->{'_code_cache'}->{lc($code)}) {
00187     # on cache miss, reread whole table and reload cache
00188     $self->fetch_all();
00189   }
00190 
00191   return $self->{'_code_cache'}->{lc($code)};
00192 }
00193 
00194 
00195 
00196 =head2 store
00197 
00198   Arg [1]    : list of MiscSets @mist_sets
00199   Example    : $misc_set_adaptor->store(@misc_sets);
00200   Description: Stores a list of MiscSets in the database, and sets the
00201                dbID and adaptor attributes of the stored sets.
00202   Returntype : none
00203   Exceptions : throw on incorrect arguments
00204                warning if a feature is already stored in this database
00205   Caller     : MiscFeatureAdaptor::store
00206   Status     : Stable
00207 
00208 =cut
00209 
00210 sub store {
00211   my $self = shift;
00212   my @misc_sets = @_;
00213 
00214   # we use 'insert ignore' so that inserts can occur safely on the farm
00215   # otherwise 2 processes could try to insert at the same time and one
00216   # would fail
00217 
00218   my $sth = $self->prepare
00219     ("INSERT IGNORE INTO misc_set " .
00220      "SET code = ?, " .
00221      "    name = ?, " .
00222      "    description = ?, " .
00223      "    max_length  = ?");
00224 
00225   my $db = $self->db();
00226 
00227  SET:
00228   foreach my $ms (@misc_sets) {
00229     if(!ref($ms) || !$ms->isa('Bio::EnsEMBL::MiscSet')) {
00230       throw("List of MiscSet arguments expected.");
00231     }
00232 
00233     if($ms->is_stored($db)) {
00234       warning("MiscSet [".$ms->dbID."] is already stored in this database.");
00235       next SET;
00236     }
00237 
00238     $sth->bind_param(1,$ms->code,SQL_VARCHAR);
00239     $sth->bind_param(2,$ms->name,SQL_VARCHAR);
00240     $sth->bind_param(3,$ms->description,SQL_LONGVARCHAR);
00241     $sth->bind_param(4,$ms->longest_feature,SQL_INTEGER);
00242 
00243     my $num_inserted = $sth->execute();
00244 
00245     my $dbID;
00246 
00247     if($num_inserted == 0) {
00248       # insert failed because set with this code already exists
00249       my $sth2 = $self->prepare("SELECT misc_set_id from misc_set " .
00250                                 "WHERE code = ?");
00251       $sth2->bind_param(1,$ms->code,SQL_VARCHAR);
00252       $sth2->execute();
00253 
00254       if($sth2->rows() != 1) {
00255         throw("Could not retrieve or store MiscSet, code=[".$ms->code."]\n".
00256               "Wrong database user/permissions?");
00257       }
00258 
00259       ($dbID) = $sth2->fetchrow_array();
00260     } else {
00261       $dbID = $sth->{'mysql_insertid'};
00262     }
00263 
00264     $ms->dbID($dbID);
00265     $ms->adaptor($self);
00266 
00267     # update the internal caches
00268     $self->{'_id_cache'}->{$dbID} = $ms;
00269     $self->{'_code_cache'}->{lc($ms->code())} = $ms;
00270   }
00271 
00272   return;
00273 }
00274 
00275 =head2 update
00276 
00277   Arg [1]    : Bio::EnsEMBL::MiscSet $miscset
00278   Example    : $adaptor->update($miscset)
00279   Description: Updates this misc_set in the database
00280   Returntype : int 1 if update is performed, undef if it is not
00281   Exceptions : throw if arg is not an misc_set object
00282   Caller     : ?
00283   Status     : Stable
00284 
00285 =cut
00286 
00287 sub update {
00288   my $self = shift;
00289   my $m    = shift;
00290 
00291   if (!ref($m) || !$m->isa('Bio::EnsEMBL::MiscSet')) {
00292     throw("Expected Bio::EnsEMBL::MiscSet argument.");
00293   }
00294 
00295   if(!$m->is_stored($self->db())) {
00296     return undef;
00297   }
00298 
00299   my $sth = $self->prepare("UPDATE misc_set ".
00300                "SET code =?, name =?, description = ?, max_length = ? ".
00301                "WHERE misc_set_id = ?");
00302 
00303   $sth->bind_param(1,$m->code,SQL_VARCHAR);
00304   $sth->bind_param(2,$m->name,SQL_VARCHAR);
00305   $sth->bind_param(3,$m->description,SQL_VARCHAR);
00306   $sth->bind_param(4,$m->longest_feature,SQL_INTEGER);
00307   $sth->bind_param(5,$m->dbID,SQL_INTEGER);
00308 
00309   $sth->execute();
00310   $sth->finish();
00311 
00312  # update the internal caches
00313   $self->{'_id_cache'}->{$m->dbID} = $m;
00314   $self->{'_code_cache'}->{lc($m->code())} = $m;
00315 }
00316 
00317 1;