GenomeComparaInfoAdaptor.pm
Go to the documentation of this file.
00001 
00002 =head1 LICENSE
00003 
00004 Copyright [1999-2014] 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::GenomeComparaInfoAdaptor
00025 
00026 =head1 SYNOPSIS
00027 
00028 # metadata_db is an instance of MetaDataDBAdaptor
00029 my $adaptor = $metadata_db->get_GenomeComparaInfoAdaptor();
00030 my @comparas = $adaptor->fetch_all_by_method('PROTEIN_TREES');
00031 for my $genome (@{@comparas[0]->genomes()}) {
00032   print $genome->name()."\n";
00033 }
00034 
00035 =head1 DESCRIPTION
00036 
00037 Adaptor for storing and retrieving GenomeComparaInfo objects from MySQL genome_info database
00038 
00039 =head1 SEE ALSO
00040 
00041 Bio::EnsEMBL::MetaData::GenomeComparaInfo
00042 Bio::EnsEMBL::MetaData::GenomeInfo
00043 
00044 =head1 Author
00045 
00046 Dan Staines
00047 
00048 =cut
00049 
00050 package Bio::EnsEMBL::MetaData::DBSQL::GenomeComparaInfoAdaptor;
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::Utils::Exception qw( throw );
00060 use List::MoreUtils qw(natatime);
00061 use Scalar::Util 'refaddr';
00062 
00063 =head2 set_release
00064   Arg        : Ensembl release number
00065   Description: Set release to use when querying 
00066   Returntype : None
00067   Exceptions : none
00068   Caller     : general
00069   Status     : Stable
00070 =cut
00071 
00072 sub set_ensembl_release {
00073   my ( $self, $release ) = @_;
00074   my $release_info =
00075     $self->db()->get_DataReleaseInfoAdaptor()
00076     ->fetch_by_ensembl_release($release);
00077   if ( !defined $release_info ) {
00078     throw "Could not find Ensembl release $release";
00079   }
00080   else {
00081     $self->data_release($release_info);
00082   }
00083   return;
00084 }
00085 
00086 =head2 set_ensembl_genomes_release
00087   Arg        : Ensembl Genomes release number
00088   Description: Set release to use when querying 
00089   Returntype : None
00090   Exceptions : none
00091   Caller     : general
00092   Status     : Stable
00093 =cut
00094 
00095 sub set_ensembl_genomes_release {
00096   my ( $self, $release ) = @_;
00097   my $release_info;
00098   if ( !defined $release ) {
00099     $release_info =
00100       $self->db()->get_DataReleaseInfoAdaptor()
00101       ->fetch_current_ensembl_genomes_release($release);
00102   }
00103   else {
00104     $release_info =
00105       $self->db()->get_DataReleaseInfoAdaptor()
00106       ->fetch_by_ensembl_genomes_release($release);
00107   }
00108   if ( !defined $release_info ) {
00109     throw "Could not find Ensembl Genomes release $release";
00110   }
00111   else {
00112     $self->data_release($release_info);
00113   }
00114   return;
00115 }
00116 
00117 =head2 data_release
00118   Arg        : Bio::EnsEMBL::MetaData::DataReleaseInfo
00119   Description: Default release to use when querying 
00120   Returntype : None
00121   Exceptions : none
00122   Caller     : general
00123   Status     : Stable
00124 =cut
00125 
00126 sub data_release {
00127   my ( $self, $release ) = @_;
00128   if ( defined $release ) {
00129     if ( !defined $self->{data_release} ||
00130          refaddr($release) != refaddr( $self->{data_release} ) )
00131     {
00132       $self->{data_release} = $release;
00133       $self->db()->get_GenomeInfoAdaptor()->data_release($release);
00134     }
00135   }
00136   if ( !defined $self->{data_release} ) {
00137     # default to current Ensembl release
00138     $self->{data_release} =
00139       $self->db()->get_DataReleaseInfoAdaptor()
00140       ->fetch_current_ensembl_release();
00141     if ( defined $self->{data_release} ) {
00142       $self->db()->get_GenomeInfoAdaptor()
00143         ->data_release( $self->{data_release} );
00144     }
00145   }
00146   if ( defined $self->{data_release} && !defined $self->{data_release}->dbID() )
00147   {
00148     $self->db()->get_DataReleaseInfoAdaptor()->store( $self->{data_release} );
00149   }
00150   return $self->{data_release};
00151 } ## end sub data_release
00152 
00153 =head2 fetch_databases 
00154   Arg        : (optional) release
00155   Description: Fetch all compara-associated databases for the specified release
00156   Returntype : Arrayref of strings
00157   Exceptions : none
00158   Caller     : general
00159   Status     : Stable
00160 =cut
00161 
00162 sub fetch_databases {
00163   my ( $self, $release ) = @_;
00164   if ( !defined $release ) {
00165     $release = $self->data_release();
00166   }
00167   return $self->dbc()->sql_helper()->execute_simple(
00168     -SQL => q/select distinct c.dbname from compara_analysis c
00169       where data_release_id=?/,
00170     -PARAMS => [ $self->data_release()->dbID() ] );
00171 }
00172 
00173 =head2 fetch_databases 
00174   Arg        : division
00175   Arg        : (optional) release
00176   Description: Fetch all compara-associated databases for the specified release
00177   Returntype : Arrayref of strings
00178   Exceptions : none
00179   Caller     : general
00180   Status     : Stable
00181 =cut
00182 
00183 sub fetch_division_databases {
00184   my ( $self, $division, $release ) = @_;
00185   if ( !defined $release ) {
00186     $release = $self->data_release();
00187   }
00188   return $self->dbc()->sql_helper()->execute_simple(
00189     -SQL => qw/select distinct c.dbname from compara_analysis c
00190       join division d using (division_id)
00191       where data_release_id=? and (d.name=? OR d.short_name=?)/,
00192     -PARAMS => [ $self->data_release()->dbID(), $division, $division ] );
00193 }
00194 
00195 sub fetch_all_by_division {
00196   my ( $self, $division ) = @_;
00197   return $self->_fetch_generic_with_args( { division => $division } );
00198 }
00199 
00200 =head2 fetch_all_by_method
00201   Arg        : Method of compara analyses to retrieve
00202   Description: Fetch compara specified compara analysis
00203   Returntype : array ref of  Bio::EnsEMBL::MetaData::GenomeComparaInfo
00204   Exceptions : none
00205   Caller     : general
00206   Status     : Stable
00207 =cut
00208 
00209 sub fetch_all_by_method {
00210   my ( $self, $method ) = @_;
00211   return $self->_fetch_generic_with_args( { method => $method } );
00212 }
00213 
00214 =head2 fetch_by_dbname_method_set
00215   Arg        : DBName of compara analyses to retrieve
00216   Arg        : Method of compara analyses to retrieve
00217   Arg        : Set of compara analyses to retrieve
00218   Description: Fetch specified compara analysis
00219   Returntype : Bio::EnsEMBL::MetaData::GenomeComparaInfo
00220   Exceptions : none
00221   Caller     : general
00222   Status     : Stable
00223 =cut
00224 
00225 sub fetch_by_dbname_method_set {
00226   my ( $self, $dbname, $method, $set_name ) = @_;
00227   return
00228     _first_element(
00229                $self->_fetch_generic_with_args(
00230                  { dbname => $dbname, method => $method, set_name => $set_name }
00231                ) );
00232 }
00233 
00234 =head1 METHODS
00235 =cut
00236 
00237 =head2 update
00238   Arg        : Bio::EnsEMBL::MetaData::GenomeComparaInfo
00239   Description: Updates the supplied object and all associated  genomes
00240   Returntype : None
00241   Exceptions : none
00242   Caller     : internal
00243   Status     : Stable
00244 =cut
00245 
00246 sub update {
00247   my ( $self, $compara ) = @_;
00248   if ( !defined $compara->dbID() ) {
00249     croak "Cannot update compara object with no dbID";
00250   }
00251   $self->dbc()->sql_helper()->execute_update(
00252     -SQL => q/update compara_analysis 
00253       set method=?,division_id=?,set_name=?,dbname=?, data_release_id=?
00254       where compara_analysis_id=?/,
00255     -PARAMS => [ $compara->method(),
00256                  $self->_get_division_id( $compara->division() ),
00257                  $compara->set_name(),
00258                  $compara->dbname(),
00259                  $self->data_release()->dbID(),
00260                  $compara->dbID() ] );
00261   $self->_store_compara_genomes($compara);
00262   $self->_store_cached_obj($compara);
00263   return;
00264 }
00265 
00266 =head2 store
00267   Arg        : Bio::EnsEMBL::MetaData::GenomeComparaInfo
00268   Description: Stores the supplied object and all associated  genomes (if not already stored)
00269   Returntype : None
00270   Exceptions : none
00271   Caller     : internal
00272   Status     : Stable
00273 =cut
00274 
00275 sub store {
00276   my ( $self, $compara ) = @_;
00277   # check if it already exists
00278   if ( !defined $compara->dbID() ) {
00279     # find out if compara exists first
00280     my ($dbID) =
00281       @{
00282       $self->dbc()->sql_helper()->execute_simple(
00283         -SQL =>
00284 q/select compara_analysis_id from compara_analysis where division_id=? and method=? and set_name=? and dbname=?/,
00285         -PARAMS => [ $self->_get_division_id( $compara->division() ),
00286                      $compara->method(), $compara->set_name(),
00287                      $compara->dbname() ] ) };
00288     if ( defined $dbID ) {
00289       $compara->dbID($dbID);
00290       $compara->adaptor($self);
00291     }
00292   }
00293   if ( defined $compara->dbID() ) {
00294     return $self->update($compara);
00295   }
00296   $self->dbc()->sql_helper()->execute_update(
00297     -SQL =>
00298 q/insert into compara_analysis(method,division_id,set_name,dbname,data_release_id)
00299         values(?,?,?,?,?)/,
00300     -PARAMS => [ $compara->method(),
00301                  $self->_get_division_id( $compara->division() ),
00302                  $compara->set_name(),
00303                  $compara->dbname(),
00304                  $self->data_release()->dbID() ],
00305     -CALLBACK => sub {
00306       my ( $sth, $dbh, $rv ) = @_;
00307       $compara->dbID( $dbh->{mysql_insertid} );
00308     } );
00309   $self->_store_compara_genomes($compara);
00310   $self->_store_cached_obj($compara);
00311   return;
00312 } ## end sub store
00313 
00314 sub _store_compara_genomes {
00315   my ( $self, $compara ) = @_;
00316   $self->dbc()->sql_helper()->execute_update(
00317      -SQL => q/delete from genome_compara_analysis where compara_analysis_id=?/,
00318      -PARAMS => [ $compara->dbID() ] );
00319   if ( defined $compara->genomes() ) {
00320     for my $genome ( @{ $compara->genomes() } ) {
00321       if ( !defined $genome->dbID() ) {
00322         $self->get_GenomeInfoAdaptor()->store($genome);
00323       }
00324       $self->dbc()->sql_helper()->execute_update(
00325         -SQL =>
00326           q/insert into genome_compara_analysis(genome_id,compara_analysis_id)
00327         values(?,?)/,
00328         -PARAMS => [ $genome->dbID(), $compara->dbID() ] );
00329     }
00330   }
00331   return;
00332 }
00333 
00334 =head2 _fetch_children
00335     Arg      : Arrayref of Bio::EnsEMBL::MetaData::GenomeInfo
00336   Description: Fetch all children of specified genome info object
00337   Returntype : none
00338   Exceptions : none
00339   Caller     : internal
00340   Status     : Stable
00341 =cut
00342 
00343 sub _fetch_children {
00344   my ( $self, $compara ) = @_;
00345   $self->_fetch_compara_genomes($compara);
00346   return;
00347 }
00348 
00349 sub _fetch_compara_genomes {
00350   my ( $self, $compara ) = @_;
00351 # add genomes on one by one (don't nest the fetch here as could run of connections)
00352   if ( !defined $compara->{genomes} ) {
00353     my $genomes = [];
00354     for my $genome_id (
00355       @{$self->dbc()->sql_helper()->execute_simple(
00356           -SQL =>
00357 q/select distinct(genome_id) from genome_compara_analysis where compara_analysis_id=?/,
00358           -PARAMS => [ $compara->dbID() ] );
00359       } )
00360     {
00361       push @$genomes, $self->db()->get_GenomeInfoAdaptor()->fetch_by_dbID($genome_id);
00362     }
00363     $compara->genomes($genomes);
00364   }
00365   return;
00366 }
00367 
00368 my $base_compara_fetch_sql =
00369 q/select compara_analysis_id as dbID, method,division.name as division,set_name,dbname,data_release_id from compara_analysis join division using (division_id)/;
00370 
00371 sub _get_base_sql {
00372   return $base_compara_fetch_sql;
00373 }
00374 
00375 sub _get_id_field {
00376   return 'compara_analysis_id';
00377 }
00378 
00379 sub _get_obj_class {
00380   return 'Bio::EnsEMBL::MetaData::GenomeComparaInfo';
00381 }
00382 
00383 # override to add release clause
00384 sub _args_to_sql {
00385   my ( $self, $sql_in, $args ) = @_;
00386   if ( !defined $args->{ _get_id_field() } ) {
00387     # if we're not searching by dbID, add release as a clause
00388     if ( defined $self->data_release()->dbID() ) {
00389       $args->{data_release_id} = $self->data_release()->dbID();
00390     }
00391   }
00392   return $self->SUPER::_args_to_sql( $sql_in, $args );
00393 }
00394 
00395 1;