DatabaseInfoAdaptor.pm
Go to the documentation of this file.
00001 
00002 =head1 LICENSE
00003 
00004 Copyright [1999-2016] EMBL-European Bioinformatics Institute
00005 
00006 Licensed under the Apache License, Version 2.0 (the "License");
00007 you may not use this file except in compliance with the License.
00008 You may obtain a copy of the License at
00009 
00010      http://www.apache.org/licenses/LICENSE-2.0
00011 
00012 Unless required by applicable law or agreed to in writing, software
00013 distributed under the License is distributed on an "AS IS" BASIS,
00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 See the License for the specific language governing permissions and
00016 limitations under the License.
00017 
00018 =cut
00019 
00020 =pod
00021 
00022 =head1 NAME
00023 
00024 Bio::EnsEMBL::MetaData::DBSQL::DatabaseInfoAdaptor
00025 
00026 =head1 SYNOPSIS
00027 
00028 # metadata_db is an instance of MetaDataDBAdaptor
00029 my $adaptor = $metadata_db->get_DatabaseInfoAdaptor();
00030 $adaptor->fetch_databases($info);
00031 
00032 =head1 DESCRIPTION
00033 
00034 Adaptor to handle DatabaseInfo objects associated with GenomeInfo or DataReleaseInfo objects.
00035 
00036 Uses delegate pattern to handle objects differently according to their subject objects.
00037 
00038 =head1 AUTHOR
00039 
00040 Dan Staines
00041 
00042 =head1 SEE ALSO
00043 
00044 Bio::EnsEMBL::MetaData::DatabaseInfo
00045 Bio::EnsEMBL::MetaData::DataReleaseInfo
00046 Bio::EnsEMBL::MetaData::GenomeInfo
00047 
00048 =cut
00049 
00050 package Bio::EnsEMBL::MetaData::DBSQL::DatabaseInfoAdaptor;
00051 
00052 use strict;
00053 use warnings;
00054 
00055 use base qw/Bio::EnsEMBL::MetaData::DBSQL::BaseInfoAdaptor/;
00056 
00057 use Carp qw(cluck croak);
00058 use Bio::EnsEMBL::Utils::Argument qw( rearrange );
00059 use Bio::EnsEMBL::MetaData::DatabaseInfo;
00060 use List::MoreUtils qw(natatime);
00061 
00062 =head1 METHODS
00063 =head2 store
00064   Arg        : Bio::EnsEMBL::MetaData::DatabaseInfo
00065   Description: Store the supplied object
00066   Returntype : none
00067   Exceptions : none
00068   Caller     : general
00069   Status     : Stable
00070 =cut
00071 
00072 sub store {
00073   my ( $self, $database ) = @_;
00074   # delegate to appropriate method for the subject
00075   my $ref = ref( $database->subject() );
00076   $ref =~ s/.*:([^:]+)$/store_$1/;
00077   $self->$ref($database);
00078   return;
00079 }
00080 
00081 =head2 update
00082   Arg        : Bio::EnsEMBL::MetaData::DatabaseInfo
00083   Description: Update the supplied object (must be previously stored)
00084   Returntype : none
00085   Exceptions : none
00086   Caller     : general
00087   Status     : Stable
00088 =cut
00089 
00090 sub update {
00091   my ( $self, $database ) = @_;
00092   # delegate to appropriate method for the subject
00093   my $ref = ref( $database->subject() );
00094   $ref =~ s/.*:([^:]+)$/update_$1/;
00095   $self->$ref($database);
00096   return;
00097 }
00098 
00099 =head2 fetch_databases
00100   Arg        : Bio::EnsEMBL::MetaData::DataReleaseInfo 
00101                or Bio::EnsEMBL::MetaData::GenomeInfo
00102   Arg        : String - Optional division
00103   Description: Find the databases associated with the supplied object
00104   Returntype : Arrayref of Bio::EnsEMBL::MetaData::DatabaseInfo 
00105   Exceptions : none
00106   Caller     : general
00107   Status     : Stable
00108 =cut
00109 sub fetch_databases {
00110   my ( $self, $subject, $division ) = @_;
00111   # delegate to appropriate method for the subject
00112   my $ref = ref($subject);
00113   $ref =~ s/.*:([^:]+)$/fetch_databases_$1/;
00114   return $self->$ref($subject);
00115 }
00116 
00117 =head1 INTERNAL METHODS
00118 =head2 store_DataReleaseInfo
00119   Description: Implementation of store for Bio::EnsEMBL::MetaData::DataReleaseInfo 
00120   Arg        : Bio::EnsEMBL::MetaData::DatabaseInfo
00121   Returntype : none
00122   Exceptions : none
00123   Caller     : general
00124   Status     : Stable
00125 =cut
00126 
00127 sub store_DataReleaseInfo {
00128   my ( $self, $data_release_database ) = @_;
00129   if ( !defined $data_release_database->dbID() ) {
00130     my ($dbID) = @{
00131       $self->dbc()->sql_helper()->execute_simple(
00132         -SQL => q/select data_release_database_id from data_release_database 
00133 where dbname=? and data_release_id=?/,
00134         -PARAMS => [ $data_release_database->dbname(),
00135                      $data_release_database->subject()->dbID() ] ) };
00136 
00137     if ( defined $dbID ) {
00138       $data_release_database->dbID($dbID);
00139       $data_release_database->adaptor($self);
00140     }
00141     if ( defined $data_release_database->dbID() ) {
00142       $self->update($data_release_database);
00143     }
00144     else {
00145       $self->dbc()->sql_helper()->execute_update(
00146         -SQL =>
00147 q/insert into data_release_database(data_release_id,type,division_id,dbname) 
00148 values (?,?,?,?)/,
00149         -PARAMS => [ $data_release_database->subject()->dbID(),
00150                      $data_release_database->type(),
00151                      $self->_get_division_id( $data_release_database->division()
00152                      ),
00153                      $data_release_database->dbname() ],
00154         -CALLBACK => sub {
00155           my ( $sth, $dbh, $rv ) = @_;
00156           $data_release_database->dbID( $dbh->{mysql_insertid} );
00157         } );
00158       $data_release_database->adaptor($self);
00159       $self->_store_cached_obj($data_release_database);
00160     }
00161   } ## end if ( !defined $data_release_database...)
00162   return;
00163 } ## end sub store_DataReleaseInfo
00164 
00165 =head2 update_DataReleaseInfo
00166   Description: Implementation of update for Bio::EnsEMBL::MetaData::DataReleaseInfo
00167   Arg        : Bio::EnsEMBL::MetaData::DatabaseInfo
00168   Returntype : none
00169   Exceptions : none
00170   Caller     : general
00171   Status     : Stabl
00172 =cut
00173 
00174 sub update_DataReleaseInfo {
00175   my ( $self, $database ) = @_;
00176   if ( !defined $database->dbID() ) {
00177     croak "Cannot update an object that has not already been stored";
00178   }
00179 
00180   $self->dbc()->sql_helper()->execute_update(
00181     -SQL =>
00182 q/update data_release_database set data_release_id=?, type=?, division_id=?, dbname=? 
00183 where data_release_database_id=?/,
00184     -PARAMS => [ $database->subject()->dbID(),
00185                  $database->type(),
00186                  $self->_get_division_id( $database->division() ),
00187                  $database->dbname() ] );
00188   return;
00189 }
00190 
00191 =head2 store_GenomeInfo
00192   Description: Implementation of store for Bio::EnsEMBL::MetaData::GenomeInfo
00193   Arg        : Bio::EnsEMBL::MetaData::DatabaseInfo
00194   Returntype : none
00195   Exceptions : none
00196   Caller     : general
00197   Status     : Stable
00198 =cut
00199 
00200 sub store_GenomeInfo {
00201   my ( $self, $genome_database ) = @_;
00202   if ( !defined $genome_database->dbID() ) {
00203     my ($dbID) = @{
00204       $self->dbc()->sql_helper()->execute_simple(
00205         -SQL => q/select genome_database_id from genome_database 
00206 where dbname=? and genome_id=?/,
00207         -PARAMS =>
00208           [ $genome_database->dbname(), $genome_database->subject()->dbID() ] )
00209     };
00210 
00211     if ( defined $dbID ) {
00212       $genome_database->dbID($dbID);
00213       $genome_database->adaptor($self);
00214     }
00215     if ( defined $genome_database->dbID() ) {
00216       $self->update($genome_database);
00217     }
00218     else {
00219       $self->dbc()->sql_helper()->execute_update(
00220         -SQL => q/insert into genome_database(genome_id,type,species_id,dbname) 
00221 values (?,?,?,?)/,
00222         -PARAMS => [ $genome_database->subject()->dbID(),
00223                      $genome_database->type(),
00224                      ( $genome_database->species_id() || 1 ),
00225                      $genome_database->dbname() ],
00226         -CALLBACK => sub {
00227           my ( $sth, $dbh, $rv ) = @_;
00228           $genome_database->dbID( $dbh->{mysql_insertid} );
00229         } );
00230       $genome_database->adaptor($self);
00231       $self->_store_cached_obj($genome_database);
00232     }
00233   } ## end if ( !defined $genome_database...)
00234   return;
00235 } ## end sub store_GenomeInfo
00236 
00237 =head2 update_GenomeInfo
00238   Description: Implementation of update for Bio::EnsEMBL::MetaData::GenomeInfo
00239   Arg        : Bio::EnsEMBL::MetaData::DatabaseInfo
00240   Returntype : none
00241   Exceptions : none
00242   Caller     : general
00243   Status     : Stabl
00244 =cut
00245 
00246 sub update_GenomeInfo {
00247   my ( $self, $database ) = @_;
00248   if ( !defined $database->dbID() ) {
00249     croak "Cannot update an object that has not already been stored";
00250   }
00251 
00252   $self->dbc()->sql_helper()->execute_update(
00253     -SQL =>
00254       q/update genome_database set genome_id=?, type=?, species_id=?, dbname=? 
00255 where genome_database_id=?/,
00256     -PARAMS => [ $database->subject()->dbID(), $database->type(),
00257                  ( $database->species_id() || 1 ), $database->dbname() ] );
00258   return;
00259 }
00260 
00261 =head2 fetch_databases_DataReleaseInfo
00262   Description: Implementation of fetch_databases for Bio::EnsEMBL::MetaData::DataReleaseInfo
00263   Arg        : Bio::EnsEMBL::MetaData::DataReleaseInfo 
00264   Description: Find the databases associated with the supplied object
00265   Returntype : Arrayref of Bio::EnsEMBL::MetaData::DatabaseInfo 
00266   Exceptions : none
00267   Caller     : general
00268   Status     : Stable
00269 =cut
00270 
00271 sub fetch_databases_DataReleaseInfo {
00272   my ( $self, $release, $division ) = @_;
00273   my $sql = q/select dbname,type,d.name from data_release_database 
00274 join division d using (division_id) where data_release_id=?/;
00275   my $params = [ $release->dbID() ];
00276   if ( defined $division ) {
00277     $sql .= ' and (d.name=? or d.short_name=?)';
00278     $params = [ @$params, $division, $division ];
00279   }
00280 
00281   my $databases = [];
00282 
00283   $self->dbc()->sql_helper()->execute_no_return(
00284     -SQL      => $sql,
00285     -CALLBACK => sub {
00286       my @row = @{ shift @_ };
00287       push @{$databases},
00288         Bio::EnsEMBL::MetaData::DatabaseInfo->new( -SUBJECT  => $release,
00289                                                    -DBNAME   => $row[0],
00290                                                    -TYPE     => $row[1],
00291                                                    -DIVISION => $row[2] );
00292       return;
00293     },
00294     -PARAMS => $params );
00295 
00296   return $databases;
00297 } ## end sub fetch_databases_DataReleaseInfo
00298 
00299 =head2 fetch_databases_GenomeInfo
00300   Description: Implementation of fetch_databases for Bio::EnsEMBL::MetaData::GenomeInfo
00301   Arg        : Bio::EnsEMBL::MetaData::GenomeInfo
00302   Arg        : String - Optional division
00303   Returntype : Arrayref of Bio::EnsEMBL::MetaData::DatabaseInfo 
00304   Exceptions : none
00305   Caller     : general
00306   Status     : Stable
00307 =cut
00308 
00309 sub fetch_databases_GenomeInfo {
00310 
00311   my ( $self, $genome ) = @_;
00312   my $sql =
00313     q/select genome_database_id, dbname, type, species_id from genome_database 
00314 where genome_id=?/;
00315   my $params    = [ $genome->dbID() ];
00316   my $databases = [];
00317 
00318   $self->dbc()->sql_helper()->execute_no_return(
00319     -SQL      => $sql,
00320     -CALLBACK => sub {
00321       my @row = @{ shift @_ };
00322       push @{$databases},
00323         Bio::EnsEMBL::MetaData::DatabaseInfo->new( -SUBJECT    => $genome,
00324                                                    -DBID       => $row[0],
00325                                                    -DBNAME     => $row[1],
00326                                                    -TYPE       => $row[2],
00327                                                    -SPECIES_ID => $row[3] );
00328       return;
00329     },
00330     -PARAMS => $params );
00331 
00332   return $databases;
00333 } ## end sub fetch_databases_GenomeInfo
00334 
00335 1;