Archive Ensembl HomeArchive Ensembl Home
ConfigRegistry.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::Utils::ConfigRegistry;
00024 
00025 =head1 SYNOPSIS
00026 
00027 
00028   Bio::EnsEMBL::Utils::ConfigRegistry->load_core($dba);
00029 
00030 
00031 =head1 DESCRIPTION
00032 
00033 The ConfigRegistry will "Register" a set of adaptors based on the type
00034 of database that is being loaded.
00035 
00036 =head1 METHODS
00037 
00038 =cut
00039 
00040 package Bio::EnsEMBL::Utils::ConfigRegistry;
00041 
00042 use strict;
00043 use warnings;
00044 
00045 use Bio::EnsEMBL::Registry;
00046 my $reg = "Bio::EnsEMBL::Registry";
00047 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
00048 use Bio::EnsEMBL::DBSQL::DBConnection;
00049 use Bio::EnsEMBL::DBSQL::DBAdaptor;
00050 
00051 use Bio::EnsEMBL::Utils::Exception qw(warning throw  deprecate stack_trace_dump);
00052 
00053 
00054 
00055 sub gen_load {
00056   my ($dba) = @_;
00057   my $config_sub;
00058 
00059   # At some point we hope to set the group in the DBadaptor, hence this
00060   # long check etc. should be simpler.
00061 
00062   if ( $dba->isa('Bio::EnsEMBL::Compara::DBSQL::DBAdaptor') ) {
00063     if ( !defined( $dba->group() ) ) {
00064       $dba->group('compara');
00065     }
00066     $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_compara;
00067   } elsif ( $dba->isa('Bio::EnsEMBL::Lite::DBAdaptor') ) {
00068     if ( !defined( $dba->group() ) ) {
00069       $dba->group('lite');
00070     }
00071     $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_lite;
00072   } elsif ( $dba->isa('Bio::EnsEMBL::External::BlastAdaptor') ) {
00073     if ( !defined( $dba->group() ) ) {
00074       $dba->group('blast');
00075     }
00076     $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_blast;
00077   } elsif ( $dba->isa('Bio::EnsEMBL::ExternalData::SNPSQL::DBAdaptor') )
00078   {
00079     if ( !defined( $dba->group() ) ) {
00080       $dba->group('SNP');
00081     }
00082     $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_SNP;
00083   } elsif ( $dba->isa('Bio::EnsEMBL::Pipeline::DBSQL::DBAdaptor') ) {
00084     if ( !defined( $dba->group() ) ) {
00085       $dba->group('pipeline');
00086     }
00087     $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_pipeline;
00088   } elsif ( $dba->isa('Bio::EnsEMBL::Hive::DBSQL::DBAdaptor') ) {
00089     if ( !defined( $dba->group() ) ) {
00090       $dba->group('hive');
00091     }
00092     $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_hive;
00093   } elsif (
00094     $dba->isa('Bio::EnsEMBL::ExternalData::Haplotype::DBAdaptor') )
00095   {
00096     if ( !defined( $dba->group() ) ) {
00097       $dba->group('haplotype');
00098     }
00099     $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_haplotype;
00100   } elsif ( $dba->isa('Bio::EnsEMBL::Variation::DBSQL::DBAdaptor') ) {
00101     if ( !defined( $dba->group() ) ) {
00102       $dba->group('variation');
00103     }
00104     $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_variation;
00105   } elsif ( $dba->isa('Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor') ) {
00106     if ( !defined( $dba->group() ) ) {
00107       $dba->group('funcgen');
00108     }
00109     $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_funcgen;
00110   } elsif ( $dba->isa('Bio::Ensembl::DBSQL::OntologyTermAdaptor') ) {
00111     if ( !defined( $dba->group() ) ) {
00112       $dba->group('ontology');
00113     }
00114     $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_ontology;
00115   } elsif ( $dba->isa('Bio::EnsEMBL::DBSQL::DBAdaptor') ) {
00116     #vega uses the core DBAdaptor so test if vega is in the dbname
00117     if ( !defined( $dba->group() ) ) {
00118       $dba->group('core');
00119     }
00120 
00121     if ( $dba->group eq "estgene" ) {
00122       $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_estgene;
00123     } elsif ( $dba->group eq "otherfeatures" ) {
00124       $config_sub =
00125         \&Bio::EnsEMBL::Utils::ConfigRegistry::load_otherfeatures;
00126     } elsif ( $dba->group eq 'vega' || $dba->group eq 'vega_update' ) {
00127       $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_vega;
00128     } else {
00129       $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_core;
00130     }
00131 
00132   } else {
00133     # none standard DBA adaptor
00134     if ( !defined( $dba->group() ) ) {
00135       $dba->group('none_standard');
00136     }
00137     $config_sub =
00138       \&Bio::EnsEMBL::Utils::ConfigRegistry::load_and_attach_dnadb_to_core;
00139     #    throw("Unknown DBAdaptor type $dba\n");
00140   }
00141 
00142   # return if the connection and species, group are the same
00143 
00144   if ( defined( $dba->species ) ) {
00145     my $db_reg = $reg->get_DBAdaptor( $dba->species, $dba->group, 1 );
00146     if ( defined($db_reg) ) {
00147       if ( $dba->dbc->equals( $db_reg->dbc ) ) { return $db_reg }
00148       else {
00149         my $msg =
00150           sprintf( 'WARN: Species (%s) and group (%s) '
00151             . 'same for two seperate databases',
00152           $dba->species(), $dba->group() );
00153 
00154         warn "${msg}\nModify species name for one of these\n";
00155         $dba->species(
00156           find_unique_species( $dba->species, $dba->group ) );
00157       }
00158     }
00159   } else {    # no species
00160     
00161     my @db_reg =
00162       @{ $reg->get_all_DBAdaptors_by_connection( $dba->dbc ) };
00163 
00164     foreach my $db_adaptor (@db_reg) {
00165       if ( $db_adaptor->group eq $dba->group ) {
00166         # found same db connection and group
00167         return $db_adaptor;
00168       }
00169     }
00170 
00171     $dba->species( find_unique_species( "DEFAULT", $dba->group ) );
00172     if ( $dba->species ne "DEFAULT" ) {
00173       warn "WARN: For multiple species "
00174         . "use species attribute in DBAdaptor->new()\n";
00175     }
00176   }
00177 
00178   Bio::EnsEMBL::Registry->add_DBAdaptor( $dba->species(), $dba->group(),
00179     $dba );
00180 
00181   #call the loading subroutine. (add the adaptors to the DBAdaptor)
00182   &{$config_sub}($dba);
00183 
00184   return $dba;
00185 } ## end sub gen_load
00186 
00187 
00188 
00189 sub find_unique_species {
00190   my ( $species, $group ) = @_;
00191 
00192   $reg->add_alias( $species, $species );
00193 
00194   my $i    = 0;
00195   my $free = 0;
00196 
00197   while ( !$free ) {
00198     if ( $i == 0 ) {
00199       if ( !defined( $reg->get_DBAdaptor( $species, $group ) ) ) {
00200         $free = 1;
00201         $i    = "";
00202       } else {
00203         $i = 1;
00204       }
00205     } else {
00206       # set needed self alias
00207       $reg->add_alias( $species . $i, $species . $i );
00208 
00209       if ( !defined( $reg->get_DBAdaptor( $species . $i, $group ) ) ) {
00210         $free = 1;
00211       } else {
00212         $i++;
00213       }
00214     }
00215   }
00216 
00217   $species .= $i;
00218   return ($species);
00219 } ## end sub find_unique_species
00220 
00221 
00222 
00223 sub load_adaptors {
00224   my ($dba) = @_;
00225 
00226   my %pairs = %{ $dba->get_available_adaptors() };
00227 
00228   while ( my ( $key, $value ) = each(%pairs) ) {
00229     Bio::EnsEMBL::Registry->add_adaptor( $dba->species(), $dba->group(),
00230       $key, $value );
00231   }
00232 }
00233 
00234 sub load_and_attach_dnadb_to_core {
00235   my ($dba) = @_;
00236 
00237   load_adaptors($dba);
00238   $reg->add_DNAAdaptor( $dba->species(), $dba->group(), $dba->species(),
00239     'core' );
00240 }
00241 
00242 
00243 =head2 load_core
00244   Arg [1]    : DBAdaptor with DBConnection already attached
00245   Returntype : DBAdaptor
00246   Exceptions : none
00247 =cut
00248 sub load_core      { load_adaptors(@_) }
00249 
00250 
00251 #
00252 # 1) core. no need to add dnadb
00253 # 2) not core add dnadb
00254 # 3) 
00255 #
00256 
00257 =head2 load_compara
00258   Arg [1]    : DBAdaptor with DBConnection already attached
00259   Returntype : DBAdaptor
00260   Exceptions : none
00261 =cut
00262 sub load_compara   { load_adaptors(@_) }
00263 =head2 load_hive
00264   Arg [1]    : DBAdaptor with DBConnection already attached
00265   Returntype : DBAdaptor
00266   Exceptions : none
00267 =cut
00268 sub load_hive      { load_adaptors(@_) }
00269 =head2 load_pipeline
00270   Arg [1]    : DBAdaptor with DBConnection already attached
00271   Returntype : DBAdaptor
00272   Exceptions : none
00273 =cut
00274 sub load_pipeline  { load_adaptors(@_) }
00275 =head2 load_SNP
00276   Arg [1]    : DBAdaptor with DBConnection already attached
00277   Returntype : DBAdaptor
00278   Exceptions : none
00279 =cut
00280 sub load_SNP       { load_adaptors(@_) }
00281 sub load_haplotype { load_adaptors(@_) }
00282 sub load_ontology  { load_adaptors(@_) }
00283 
00284 
00285 # these that need to attach to the core to get the sequence data
00286 
00287 sub load_estgene       { load_and_attach_dnadb_to_core(@_) }
00288 
00289 sub load_variation { load_and_attach_dnadb_to_core(@_) }
00290 sub load_funcgen   { load_and_attach_dnadb_to_core(@_) }
00291 =head2 load_otherfeatures
00292   Arg [1]    : DBAdaptor with DBConnection alredy attached
00293   Returntype : DBAdaptor
00294   Exceptions : none
00295 
00296 =cut
00297 sub load_otherfeatures { load_and_attach_dnadb_to_core(@_) }
00298 =head2 load_vega
00299   Arg [1]    : DBAdaptor with DBConnection already attached
00300   Returntype : DBAdaptor
00301   Exceptions : none
00302 =cut
00303 sub load_vega          { load_and_attach_dnadb_to_core(@_) }
00304 
00305 
00306 sub add_alias {
00307   my ( $class, @args ) = @_;
00308 
00309   my ( $species, $aliases ) = rearrange( [qw(SPECIES ALIAS)], @args );
00310 
00311   # Make sure it exists itself
00312   Bio::EnsEMBL::Registry->add_alias( $species, $species );
00313 
00314   if ( defined($aliases) ) {
00315     foreach my $ali (@$aliases) {
00316       Bio::EnsEMBL::Registry->add_alias( $species, $ali );
00317     }
00318   }
00319 }
00320 
00321 #
00322 # overwrite/load new types. Done this way to enable no changes to CVS for
00323 # external users. External users should add there own "GROUPS" in the file
00324 # User_defined_load.
00325 #
00326 
00327 eval{ require Bio::EnsEMBL::Utils::User_defined_load };
00328 
00329 1;