3 Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
4 Copyright [2016-2024] EMBL-European Bioinformatics Institute
6 Licensed under the Apache License, Version 2.0 (the
"License");
7 you may not use
this file except in compliance with the License.
8 You may obtain a copy of the License at
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an
"AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License
for the specific language governing permissions and
16 limitations under the License.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
39 my $registry =
'Bio::EnsEMBL::Registry';
41 $registry->
load_all(
"configuration_file");
43 $gene_adaptor = $registry->get_adaptor(
'Human',
'Core',
'Gene' );
47 All Adaptors are stored/registered
using this module. This module should
48 then be used to get the adaptors needed.
50 The registry can be loaded from a configuration file
using the load_all
53 If a filename is passed to load_all then
this is used. Else
if the
54 environment variable ENSEMBL_REGISTRY is set to the name on an existing
55 configuration file, then
this is used. Else
if the file .ensembl_init
56 in your home directory exist, it is used.
58 For the Web server ENSEMBL_REGISTRY should be set in SiteDefs.pm. This
59 will then be passed on to load_all.
62 The registry can also be loaded via the method load_registry_from_db
63 which given a database host will load the latest versions of the Ensembl
66 The four types of registries are
for db adaptors, dba adaptors, dna
67 adaptors and the standard type.
71 These are registries
for backwards compatibility and enable the
72 subroutines to add other adaptors to connections.
74 e.g. get_all_db_adaptors, get_db_adaptor, add_db_adaptor,
75 remove_db_adaptor are the old DBAdaptor subroutines which are now
76 redirected to the Registry.
80 my $sfa = $self->adaptor()->db()->get_db_adaptor(
'blast');
82 We now want to change
this to
90 These are the stores
for the DBAdaptors
92 The Registry will create all the DBConnections needed now
if you set up
93 the configuration correctly. So instead of the old commands like
96 my $exon_adaptor = $db->get_ExonAdaptor;
98 we should now have just
106 This is an
internal Registry and allows the configuration of a dnadb.
107 An example here is to set the est database to get its dna data from the
110 ## set the est db to use the core
for getting dna data.
112 #
"core",
"Homo Sapiens",
"est" );
117 This is the registry
for all the general types of adaptors like
118 GeneAdaptor, ExonAdaptor,
Slice Adaptor etc.
120 These are accessed by the
get_adaptor subroutine i.e.
131 package Bio::EnsEMBL::Registry;
145 use DBI qw(:sql_types);
147 use Scalar::Util qw/blessed/;
149 use vars qw(%registry_register);
151 # This is a map from group names to Ensembl DB adaptors. Used by
152 # load_all() and reset_DBAdaptor().
153 my %group2adaptor = (
154 'compara' =>
'Bio::EnsEMBL::Compara::DBSQL::DBAdaptor',
155 'core' =>
'Bio::EnsEMBL::DBSQL::DBAdaptor',
156 'estgene' =>
'Bio::EnsEMBL::DBSQL::DBAdaptor',
157 'funcgen' =>
'Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor',
158 'gene2phenotype' =>
'Bio::EnsEMBL::G2P::DBSQL::DBAdaptor',
159 'regulation' =>
'Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor',
160 'hive' =>
'Bio::EnsEMBL::Hive::DBSQL::DBAdaptor',
161 'metadata' =>
'Bio::EnsEMBL::MetaData::DBSQL::MetaDataDBAdaptor',
162 'ontology' =>
'Bio::EnsEMBL::DBSQL::OntologyDBAdaptor',
163 'otherfeatures' =>
'Bio::EnsEMBL::DBSQL::DBAdaptor',
164 'pipeline' =>
'Bio::EnsEMBL::Pipeline::DBSQL::DBAdaptor',
165 'production' =>
'Bio::EnsEMBL::Production::DBSQL::DBAdaptor',
166 'stable_ids' =>
'Bio::EnsEMBL::DBSQL::DBAdaptor',
167 'taxonomy' =>
'Bio::EnsEMBL::Taxonomy::DBSQL::TaxonomyDBAdaptor',
168 'variation' =>
'Bio::EnsEMBL::Variation::DBSQL::DBAdaptor',
169 'vega' =>
'Bio::EnsEMBL::DBSQL::DBAdaptor',
170 'vega_update' =>
'Bio::EnsEMBL::DBSQL::DBAdaptor',
176 Will load the registry with the configuration file which is
177 obtained from the first in the following and in that order.
179 1) If an argument is passed to
this method,
this is used as the
180 name of the configuration file to read.
182 2) If the environment variable ENSEMBL_REGISTRY is set,
this is
183 used as the name of the configuration file to read.
185 3) If the file .ensembl_init exist in the home directory, it is
186 used as the configuration file.
188 Arg [1] : (optional)
string
189 Name of file to load the registry from.
191 Arg [2] : (optional) integer
192 If not 0, will print out all information.
194 Arg [3] : (optional) integer
195 If not 0, the database connection will not be
196 cleared,
if 0 or
if not set the database connections
197 will be cleared (
this is the
default).
199 Arg [4]: (optional)
boolean
200 This option will turn off caching
for slice features,
201 so, every time a set of features is retrieved,
202 they will come from the database instead of the
203 cache. This option is only recommended
for advanced
204 users, specially
if you need to store and retrieve
205 features. It might reduce performance when querying
206 the database
if not used properly. If in doubt,
do
207 not use it or ask in the developer mailing list.
209 Arg [5]: (optional)
boolean
210 This option will make load_all()
throw if the configuration file
211 is missing and cannot be guessed from the environment
214 Returntype : Int count of the DBAdaptor instances which can be found in the
215 registry due to
this method being called. Will never be negative
216 Exceptions : Throws
if $throw_if_missing is set and ($config_file is missing
217 and cannot be guessed from the environment
223 my ($class, $config_file, $verbose, $no_clear, $no_cache, $throw_if_missing ) = @_;
225 if ( !defined($config_file) ) {
226 if ( defined( $ENV{ENSEMBL_REGISTRY} ) ) {
227 if (-e $ENV{ENSEMBL_REGISTRY}) {
228 $config_file = $ENV{ENSEMBL_REGISTRY};
230 warning(
"\$ENV{ENSEMBL_REGISTRY} points to a file ('$ENV{ENSEMBL_REGISTRY}') that does not exist.\n");
232 } elsif ( defined( $ENV{HOME} ) ) {
233 if (-e ($ENV{HOME} .
"/.ensembl_init")) {
234 $config_file = $ENV{HOME} .
"/.ensembl_init";
237 if ($throw_if_missing and !defined($config_file) ) {
238 throw(
"No registry configuration to load, and no default could be guessed.\n");
240 } elsif ($throw_if_missing and !(-e $config_file)) {
241 throw(sprintf(
"Configuration file '%s' does not exist. Registry configuration not loaded.\n", $config_file ));
248 my $original_count = $class->get_DBAdaptor_count();
250 if ( !defined($config_file) ) {
253 "No default registry configuration to load.\n" );
255 } elsif ( !-e $config_file ) {
257 printf( STDERR
"Configuration file '%s' does not exist. "
258 .
"Registry configuration not loaded.\n",
262 if ( defined( $registry_register{
'seen'} ) ) {
265 print( STDERR
"Clearing previously loaded "
266 .
"registry configuration\n" );
271 $registry_register{
'seen'} = 1;
275 "Loading registry configuration from '%s'.\n",
281 my $test_eval = eval { require Config::IniFiles };
283 if ($@ or (!$test_eval)) {
284 # The user does not have the 'Config::IniFiles' module.
286 print( STDERR
"No Config::IniFiles module found, "
287 .
"assuming this is not an ini-file\n" );
289 # If the configuration file *is* an ini-file, we can expect a
290 # load of compilation errors from the next eval...
292 # The user has the 'Config::IniFiles' module installed. See
293 # if this is an ini-file or not...
294 $cfg = Config::IniFiles->new( -file => $config_file );
297 if ( defined $cfg ) {
298 my %default_adaptor_args = ();
300 if ( $cfg->SectionExists(
'default') ) {
301 # The 'default' section is special. It contain default
302 # values that should be implicit to all other section in
303 # this configuration file. Aliases are added if there
304 # is also a 'species' setting.
306 my $alias = $cfg->val(
'default',
'alias' );
307 $cfg->delval(
'default',
'alias' );
309 my $species = $cfg->val(
'default',
'species' );
311 if ( defined($alias) && defined($species) ) {
313 -species => $species,
314 -alias => [ split( /\n/, $alias ) ]
318 %default_adaptor_args =
319 map {
'-' . $_ => $cfg->val(
'default', $_ ) }
320 $cfg->Parameters(
'default');
323 foreach my $section ( $cfg->Sections() ) {
324 if ( $section eq
'default' )
325 { # We have already done the
'default' section.
329 my $group = $cfg->val( $section,
'group' )
330 || $cfg->val(
'default',
'group' );
332 if ( !defined($group) ) {
333 printf( STDERR
"Key 'group' is undefined "
334 .
"for configuration section '%s', "
335 .
"skipping this section.\n",
340 my $adaptor = $group2adaptor{ lc($group) };
341 if ( !defined($adaptor) ) {
342 printf( STDERR
"Unknown group '%s' "
343 .
"for configuration section '%s', "
344 .
"skipping this section.\n",
349 # Handle aliases. A section must have both an 'alias'
350 # setting and a 'species' setting for aliases to be
351 # added. The 'species' setting might be inherited from
352 # the 'default' section.
354 my $alias = $cfg->val( $section,
'alias' );
355 $cfg->delval( $section,
'alias' );
357 my $species = $cfg->val( $section,
'species' )
358 || $cfg->val(
'default',
'species' );
360 if ( defined($alias) && defined($species) ) {
362 -species => $species,
363 -alias => [ split( /\n/, $alias ) ]
367 # Fill in the adaptor initialization arguments.
368 # We trust the user to provide sensible key-value pairs.
369 my %adaptor_args = %default_adaptor_args;
370 foreach my $parameter ( $cfg->Parameters($section) ) {
371 $adaptor_args{
'-' . $parameter } =
372 $cfg->val( $section, $parameter );
374 # when set, do not use the feature cache in the
377 $adaptor_args{
'-no_cache'} = 1;
381 printf(
"Configuring adaptor '%s' "
382 .
"for configuration section '%s'...\n",
383 $adaptor, $section );
386 my $test_eval = eval
"require $adaptor"; ## no critic
387 if ($@ or (!$test_eval)) { die($@) }
389 $adaptor->new(%adaptor_args);
391 } ## end
foreach my $section ( $cfg->Sections...
393 # This is probably no ini-file but an old style piece
394 # of configuration written in Perl. We need to try to
401 $test_eval = eval $contents; ## no critic
404 $test_eval = eval { require($config_file) };
405 # To make the web code avoid doing this again we delete first
406 delete $INC{$config_file};
409 #Now raise the exception just in case something above is
411 if ($@ or (!$test_eval)) { die($@) }
414 } ## end
else [
if ( !defined($config_file...
416 my $count = $class->get_DBAdaptor_count() - $original_count;
417 return $count >= 0 ? $count : 0;
418 } ## end sub load_all
422 Will clear the registry and disconnect from all databases.
434 foreach my $dba (@{$registry_register{
'_DBA'}}){
435 if($dba->dbc->connected){
436 $dba->dbc->db_handle->disconnect();
439 %registry_register = ();
444 # db adaptors. (for backwards compatibility)
449 Arg [1] : db (DBAdaptor) to add adaptor to.
450 Arg [2] : name of the name to add the adaptor to in the registry.
451 Arg [3] : The adaptor to be added to the registry.
456 : This is here
for backwards compatibility only and may
457 : be removed eventually. Solution is to make sure the
458 : db and the adaptor have the same species and the call
459 : is then no longer needed.
464 my ( $class, $db, $name, $adap ) = @_;
465 #No warnings brought in due to some overzelous webcode triggering a lot of warnings.
466 no warnings
'uninitialized';
467 if ( lc( $db->species() ) ne lc( $adap->species ) ) {
468 $registry_register{_SPECIES}{ lc( $db->species() ) }
469 { lc( $db->group() ) }{
'_special'}{ lc($name) } = $adap;
476 Arg [1] : db (DBAdaptor) to remove adaptor from.
477 Arg [2] : name to remove the adaptor from in the registry.
482 : This is here
for backwards compatibility only and may
483 : be removed eventually. Solution is to make sure the
484 : db and the adaptor have the same species and the call
485 : is then no longer needed.
490 my ( $class, $db, $name ) = @_;
493 $registry_register{_SPECIES}{ lc( $db->species() ) }
494 { lc( $db->group() ) }{
'_special'}{ lc($name) };
496 $registry_register{_SPECIES}{ lc( $db->species() ) }
497 { lc( $db->group() ) }{
'_special'}{ lc($name) } = undef;
504 Arg [1] : db (DBAdaptor) to get adaptor from.
505 Arg [2] : name to get the adaptor
for in the registry.
508 Exceptions : See get_DBAdaptor()
510 : This is here
for backwards compatibility only and may
511 : be removed eventually. Solution is to make sure the
512 : db and the adaptor have the same species then call
513 : get_DBAdaptor instead.
518 my ( $class, $db, $name ) = @_;
523 if ( defined($ret) ) {
return $ret }
525 return $registry_register{_SPECIES}{ lc( $db->species() ) }
526 { lc( $db->group() ) }{
'_special'}{ lc($name) };
529 =head2 get_all_db_adaptors
531 Arg [1] : db (DBAdaptor) to get all the adaptors from.
536 : This is here
for backwards compatibility only and
537 : may be removed eventually. Solution is to make
538 : sure the dbs all have the same species then call
539 : get_all_DBAdaptors(-species =>
"human");
544 sub get_all_db_adaptors {
545 my ( $class, $db ) = @_;
548 # we now also want to add all the DBAdaptors for the same species.
549 # as add_db_adaptor does not add if it is from the same species.
551 foreach my $dba ( @{ $registry_register{
'_DBA'} } ) {
552 if ( lc( $dba->species() ) eq lc( $db->species() ) ) {
553 $ret{ $dba->group() } = $dba;
559 $registry_register{_SPECIES}
560 { $class->get_alias( $db->species() ) }{ lc( $db->group() ) }
564 $registry_register{_SPECIES}
565 { $class->get_alias( $db->species() ) }{ lc( $db->group() ) }
570 } ## end sub get_all_db_adaptors
579 Arg [1] : name of the species to add the adaptor to in the registry.
580 Arg [2] : name of the group to add the adaptor to in the registry.
581 Arg [3] : DBAdaptor to be added to the registry.
591 my ( $class, $species, $group, $adap ) = @_;
593 if ( !defined($species) ) {
594 throw(
'Species not defined.');
597 if ( !( $class->alias_exists($species) ) ) {
598 $class->add_alias( $species, $species );
601 $species = $class->get_alias($species);
603 $registry_register{_SPECIES}{$species}{ lc($group) }{
'_DB'} = $adap;
605 if ( !defined( $registry_register{
'_DBA'} ) ) {
606 $registry_register{
'_DBA'} = [$adap];
608 push( @{ $registry_register{
'_DBA'} }, $adap );
617 Arg [1] : name of the species to get the adaptor
for in the registry.
618 Arg [2] : name of the group to get the adaptor
for in the registry.
619 Arg [3] :
if set will not give warnings when looking
for alias.
621 Returntype : DBAdaptor
622 Exceptions : If $species is not defined and
if no valid
internal name
623 could be found
for $species. If thrown check your API and DB
630 my ( $class, $species, $group, $no_alias_check ) = @_;
632 if ( !defined($species) ) {
633 throw(
'Species not defined.');
636 my $ispecies = $class->get_alias( $species, $no_alias_check );
638 if ($group eq
'regulation') { $group =
'funcgen'; }
640 if ( !defined($ispecies) ) {
641 if(! $no_alias_check) {
642 throw(
"Can not find internal name for species '$species'");
645 else { $species = $ispecies }
647 return $registry_register{_SPECIES}{$species}{ lc($group) }{
'_DB'};
650 =head2 get_all_DBAdaptors
652 Arg [SPECIES]: (optional)
string
653 species name to get adaptors
for
654 Arg [GROUP] : (optional)
string
655 group name to get adaptors
for
665 Returntype : list of DBAdaptors
671 sub get_all_DBAdaptors {
672 my ( $class, @args ) = @_;
674 my ( $species, $group ) = rearrange( [qw(SPECIES GROUP)], @args );
676 if ( !defined($species) && !defined($group) ) {
677 return $registry_register{
'_DBA'} || [];
680 if ( defined($species) ) {
681 $species = $class->get_alias($species);
682 return [] unless $species;
686 foreach my $dba ( @{ $registry_register{
'_DBA'} } ) {
687 if ( ( !defined($species) || lc($species) eq lc( $dba->species() ) )
688 && ( !defined($group) || lc($group) eq lc( $dba->group() ) ) )
697 =head2 get_all_DBAdaptors_by_connection
699 Arg [1] : DBConnection used to find DBAdaptors
700 Returntype : reference to list of DBAdaptors
708 sub get_all_DBAdaptors_by_connection {
709 my ( $self, $dbc_orig ) = @_;
713 foreach my $dba ( @{ $registry_register{
'_DBA'} } ) {
714 my $dbc = $dba->dbc();
717 && $dbc->can(
'equals')
718 && $dbc->equals($dbc_orig) )
720 push( @
return, $dba );
727 =head2 get_all_DBAdaptors_by_dbname
729 Arg [1] : string, name of database
730 Returntype : reference to list of DBAdaptors
738 sub get_all_DBAdaptors_by_dbname {
739 my ( $self, $dbname ) = @_;
743 foreach my $dba ( @{ $registry_register{
'_DBA'} } ) {
744 my $dbc = $dba->dbc();
746 if ( defined($dbc) && $dbc->dbname() eq $dbname ) {
747 push( @
return, $dba );
754 =head2 remove_DBAdaptor
756 Arg [1] : name of the species to get the adaptor
for in the registry.
757 Arg [2] : name of the group to get the adaptor
for in the registry.
765 sub remove_DBAdaptor {
766 my ( $class, $species, $group ) = @_;
768 $species = $class->get_alias($species);
770 delete $registry_register{_SPECIES}{$species}{$group};
771 # This will remove the DBAdaptor and all the other adaptors
773 # Now remove if from the _DBA array
776 foreach my $i ( 0 .. $#{ $registry_register{
'_DBA'} } ) {
777 my $dba = $registry_register{
'_DBA'}->[$i];
779 if ( ( $dba->species eq $species )
780 && $dba->group eq $group )
787 # Now remove from _DBA cache
788 if ( defined($index) ) {
789 splice( @{ $registry_register{
'_DBA'} }, $index, 1 );
793 } ## end sub remove_DBAdaptor
797 =head2 reset_DBAdaptor
799 Arg [1]:
string - species e.g. homo_sapiens
800 Arg [2]:
string - DB group e.g. core
801 Arg [3]:
string -
new dbname
802 Args [4-7]:
string - optional DB parameters, defaults to current db params
if omitted
803 Arg [8]: hashref - Hash ref of additional parameters e.g. eFG dnadb params
for auto selecting dnadb
804 Usage : $reg->reset_registry_db(
'homo_sapiens',
'core',
805 'homo_sapiens_core_37_35j' );
806 Description: Resets a DB within the registry.
807 Exceptions: Throws
if mandatory params not supplied
808 Throws
if species name is not already seen by the registry
809 Throws
if no current DB
for species/group available
814 sub reset_DBAdaptor {
816 $self, $species, $group, $dbname, $host,
817 $port, $user, $pass, $params
820 # Check mandatory params
821 if ( !( defined $species && defined $group && defined $dbname ) ) {
823 'Must provide at least a species, group, and dbname parameter '
824 .
'to redefine a DB in the registry' );
827 # Validate species here
828 my $alias = $self->get_alias($species);
829 throw(
"Could not find registry alias for species:\t$species")
830 if ( !defined $alias );
832 # Get all current defaults if not defined
834 my $db = $self->get_DBAdaptor( $alias, $group );
839 $host ||= $db->dbc->host;
840 $port ||= $db->dbc->port;
841 $user ||= $db->dbc->username;
842 $pass ||= $db->dbc->password;
844 #Now we need to test mandatory params
845 $class = $group2adaptor{ lc($group) };
847 if ( !( $host && $user ) ) {
848 throw(
"No comparable $alias $group DB present in Registry. "
849 .
"You must pass at least a dbhost and dbuser" );
853 $self->remove_DBAdaptor( $alias, $group );
855 # ConfigRegistry should automatically add this to the Registry
867 } ## end sub reset_DBAdaptor
874 =head2 add_DNAAdaptor
876 Arg [1] : name of the species to add the adaptor to in the registry.
877 Arg [2] : name of the group to add the adaptor to in the registry.
878 Arg [3] : name of the species to get the dna from
879 Arg [4] : name of the group to get the dna from
888 my ( $class, $species, $group, $dnadb_species, $dnadb_group ) = @_;
890 $species = $class->get_alias($species);
891 $dnadb_species = $class->get_alias($dnadb_species);
892 if ( $dnadb_group->isa(
'Bio::EnsEMBL::DBSQL::DBAdaptor') ) {
895 $registry_register{_SPECIES}{$species}{ lc($group) }{
'_DNA'} =
897 $registry_register{_SPECIES}{$species}{ lc($group) }{
'_DNA2'} =
903 =head2 get_DNAAdaptor
905 Arg [1] : name of the species to get the adaptor
for in the registry.
906 Arg [2] : name of the group to get the adaptor
for in the registry.
915 my ( $class, $species, $group ) = @_;
917 $species = $class->get_alias($species);
919 $registry_register{_SPECIES}{$species}{ lc($group) }{
'_DNA'};
921 $registry_register{_SPECIES}{$species}{ lc($group) }{
'_DNA2'};
923 if ( defined $new_group ) {
924 return $class->get_DBAdaptor( $new_species, $new_group );
936 Arg [1] : name of the species to add the adaptor to in the registry.
937 Arg [2] : name of the group to add the adaptor to in the registry.
938 Arg [3] : name of the type to add the adaptor to in the registry.
939 Arg [4] : The DBAdaptor to be added to the registry.
940 Arg [5] : (optional) Set to allow overwrites of existing adaptors.
950 my ( $class, $species, $group, $type, $adap, $reset ) = @_;
952 $species = $class->get_alias($species);
953 my $lc_group = lc($group);
954 my $lc_type = lc($type);
956 # Since the adaptors are not stored initially, only their class paths
957 # when the adaptors are obtained, we need to store these instead. It
958 # is not necessarily an error if the registry is overwritten without
959 # the reset set but it is an indication that we are overwriting a
960 # database which should be a warning for now
962 if ( defined($reset) )
963 { # JUST RESET THE HASH VALUE NO MORE PROCESSING NEEDED
964 $registry_register{_SPECIES}{$species}{ $lc_group }{ $lc_type } = $adap;
970 $registry_register{_SPECIES}{$species}{ $lc_group }{ $lc_type }
974 # "Overwriting Adaptor in Registry for $species $group $type\n");
975 $registry_register{_SPECIES}{$species}{ $lc_group }{ $lc_type } = $adap;
978 $registry_register{_SPECIES}{$species}{ $lc_group }{ $lc_type } = $adap;
980 if ( !defined( $registry_register{_SPECIES}{$species}{
'list'} ) ) {
981 $registry_register{_SPECIES}{$species}{
'list'} = [$type];
984 push( @{ $registry_register{_SPECIES}{$species}{
'list'} }, $type );
988 } ## end sub add_adaptor
991 =head2 add_switchable_adaptor
993 Arg [1] : String name of the species to add its switchable adaptor into the registry
994 Arg [2] : String name of the group to add its switchable adaptor into the registry
995 Arg [3] : String name of the type to add its switchable adaptor into the registry
996 Arg [4] : Reference switchable adaptor to insert
997 Arg [5] : Boolean
override any existing switchable adaptor
1000 Exceptions : Thrown
if a valid
internal name cannot be found
for the given
1001 name. If thrown check your API and DB version. Also thrown
if
1002 no type, group or switchable adaptor instance was given
1006 sub add_switchable_adaptor {
1007 my ($class, $species, $group, $adaptor_type, $instance, $override) = @_;
1009 my $ispecies = $class->get_alias($species);
1010 throw "Cannot decode given species ${species} to an internal registry name" if ! $species;
1011 throw "No group given" if ! $group;
1012 throw "No adaptor type given" if ! $adaptor_type;
1013 throw "No switchable adaptor given" if ! $instance;
1014 throw "Switchable adaptor was not a blessed reference" if ! blessed($instance);
1016 $group = lc($group);
1017 $adaptor_type = lc($adaptor_type);
1019 $registry_register{_SWITCHABLE}{$species}{$group}{$adaptor_type} = $instance;
1023 if(exists $registry_register{_SWITCHABLE}{$species}{$group}{$adaptor_type}) {
1024 my $existing_ref = ref($registry_register{_SWITCHABLE}{$species}{$group}{$adaptor_type});
1025 throw "Cannot switch adaptors for ${species}, ${group} and ${adaptor_type} because one is already set ($existing_ref). Use the override flag or revert_switchable_adaptor";
1028 $registry_register{_SWITCHABLE}{$species}{$group}{$adaptor_type} = $instance;
1032 =head2 has_switchable_adaptor
1034 Arg [1] : String name of the species to add its switchable adaptor into the registry
1035 Arg [2] : String name of the group to add its switchable adaptor into the registry
1036 Arg [3] : String name of the type to add its switchable adaptor into the registry
1038 Returntype : Boolean indicating
if a switchable adaptor is available
for your submitted combination
1039 Exceptions : Thrown
if a valid
internal name cannot be found
for the given
1040 name. If thrown check your API and DB version. Also thrown
if
1041 no type, group or switchable adaptor instance was given
1045 sub has_switchable_adaptor {
1046 my ($class, $species, $group, $adaptor_type) = @_;
1048 my $ispecies = $class->get_alias($species);
1049 throw "Cannot decode given species ${species} to an internal registry name" if ! $species;
1050 throw "No group given" if ! $group;
1051 throw "No adaptor type given" if ! $adaptor_type;
1053 $group = lc($group);
1054 $adaptor_type = lc($adaptor_type);
1055 return (defined $registry_register{_SWITCHABLE}{$species}{$group}{$adaptor_type}) ? 1 : 0;
1058 =head2 remove_switchable_adaptor
1060 Arg [1] : name of the species to remove its switchable adaptor from the registry
1061 Arg [2] : name of the group to remove its switchable adaptor from the registry
1062 Arg [3] : name of the type to remove its switchable adaptor from the registry
1064 Returntype : The removed adaptor
if one was removed. Otherwise undef
1065 Exceptions : Thrown
if a valid
internal name cannot be found
for the given
1066 name. If thrown check your API and DB version. Also thrown
if
1067 no type or group was given
1071 sub remove_switchable_adaptor {
1072 my ($class, $species, $group, $adaptor_type) = @_;
1073 my $ispecies = $class->get_alias($species);
1074 throw "Cannot decode given species ${species} to an internal registry name" if ! $species;
1075 throw "No group given" if ! $group;
1076 throw "No adaptor type given" if ! $adaptor_type;
1078 $group = lc($group);
1079 $adaptor_type = lc($adaptor_type);
1080 if(defined $registry_register{_SWITCHABLE}{$ispecies}{$group}{$adaptor_type}) {
1081 my $adaptor = $registry_register{_SWITCHABLE}{$ispecies}{$group}{$adaptor_type};
1082 delete $registry_register{_SWITCHABLE}{$ispecies}{$group}{$adaptor_type};
1090 Arg [1] : name of the species to add the adaptor to in the registry.
1091 Arg [2] : name of the group to add the adaptor to in the registry.
1092 Arg [3] : name of the type to add the adaptor to in the registry.
1094 Description : Finds and returns the specified adaptor. This method will also check
1095 if the species, group and adaptor combination satisfy a DNADB condition
1096 (and will
return that DNADB
's implementation). Also we check for
1097 any available switchable adaptors and will return that if available.
1098 Returntype : adaptor
1099 Exceptions : Thrown if a valid internal name cannot be found for the given
1100 name. If thrown check your API and DB version. Also thrown if
1101 no type or group was given
1107 my ( $class, $species, $group, $type ) = @_;
1109 my $ispecies = $class->get_alias($species);
1111 if ( !defined($ispecies) ) {
1112 throw("Can not find internal name for species '$species
'");
1114 else { $species = $ispecies }
1116 throw 'No adaptor group given
' if ! defined $group;
1117 throw 'No adaptor type given
' if ! defined $type;
1119 $group = lc($group);
1120 my $lc_type = lc($type);
1123 if($type =~ /Adaptor$/i) {
1124 warning("Detected additional Adaptor string in given the type '$type
'. Removing it to avoid possible issues. Alter your type to stop this message");
1125 $type =~ s/Adaptor$//i;
1128 # For historical reasons, allow use of group 'regulation
' to refer to
1130 if ( $group eq 'regulation
' ) { $group = 'funcgen
' }
1132 my %dnadb_adaptors = (
1134 'assemblymapper
' => 1,
1135 'karyotypeband
' => 1,
1136 'repeatfeature
' => 1,
1138 'assemblyexceptionfeature
' => 1
1141 #Before looking for DNA adaptors we need to see if we have a switchable adaptor since they take preference
1142 if(defined $registry_register{_SWITCHABLE}{$species}{$group}{$lc_type}) {
1143 return $registry_register{_SWITCHABLE}{$species}{$group}{$lc_type};
1146 # Look for a possible DNADB group alongside the species hash
1147 my $dnadb_group = $registry_register{_SPECIES}{$species}{ $group }{'_DNA
'};
1149 # If we found one & this is an adaptor we should be replaced by a DNADB then
1150 # look up the species to use and replace the current group with the DNADB group
1151 # (groups are held in _DNA, species are in _DNA2)
1152 if ( defined($dnadb_group) && defined( $dnadb_adaptors{ $lc_type } ) ) {
1153 $species = $registry_register{_SPECIES}{$species}{ $group }{'_DNA2
'};
1154 $group = $dnadb_group;
1156 # Once we have switched to the possibility of a DNADB call now check again for
1157 # a switchable adaptor
1158 if(defined $registry_register{_SWITCHABLE}{$species}{$group}{$lc_type}) {
1159 return $registry_register{_SWITCHABLE}{$species}{$group}{$lc_type};
1163 # No switchable adaptor? Ok then continue with the normal logic
1164 my $ret = $registry_register{_SPECIES}{$species}{ $group }{ $lc_type };
1166 if ( !defined($ret) ) { return }
1167 if ( ref($ret) ) { return $ret }
1169 # Not instantiated yet
1171 my $dba = $registry_register{_SPECIES}{$species}{ $group }{'_DB
'};
1174 my $test_eval = eval "require $module"; ## no critic
1175 if ($@ or (!$test_eval)) {
1176 warning("'$module
' cannot be found.\nException $@\n");
1182 $registry_register{_SPECIES}{$species}{ $group }{'CHECKED
'} )
1185 $registry_register{_SPECIES}{$species}{ $group }{'CHECKED
'} = 1;
1186 $class->version_check($dba);
1189 my $adap = "$module"->new($dba);
1190 Bio::EnsEMBL::Registry->add_adaptor( $species, $group, $type, $adap,
1195 } ## end sub get_adaptor
1197 =head2 get_all_adaptors
1199 Arg [SPECIES] : (optional) string
1200 species name to get adaptors for
1201 Arg [GROUP] : (optional) string
1202 group name to get adaptors for
1203 Arg [TYPE] : (optional) string
1204 type to get adaptors for
1205 Example : @adaps = @{Bio::EnsEMBL::Registry->get_all_adaptors()};
1206 Returntype : ref to list of adaptors
1212 sub get_all_adaptors{
1213 my ($class,@args)= @_;
1214 my ($species, $group, $type);
1216 my (%species_hash, %group_hash, %type_hash);
1219 if(@args == 1){ # Old species only one parameter
1220 warn("-SPECIES argument should now be used to get species adaptors");
1221 $species = $args[0];
1224 # new style -SPECIES, -GROUP, -TYPE
1225 ($species, $group, $type) =
1226 rearrange([qw(SPECIES GROUP TYPE)], @args);
1229 if(defined($species)){
1230 $species_hash{$species} = 1;
1233 # get list of species
1234 foreach my $dba (@{$registry_register{'_DBA
'}}){
1235 $species_hash{lc($dba->species())} = 1;
1238 if(defined($group)){
1239 $group_hash{$group} = 1;
1242 foreach my $dba (@{$registry_register{'_DBA
'}}){
1243 $group_hash{lc($dba->group())} = 1;
1247 if ( defined($type) ) {
1248 $type_hash{$type} = 1;
1250 foreach my $dba ( @{ $registry_register{'_DBA
'} } ) {
1252 @{ $registry_register{_SPECIES}{ lc( $dba->species ) }{'list
'} }
1255 $type_hash{ lc($ty) } = 1;
1260 ### NOW NEED TO INSTANTIATE BY CALLING get_adaptor
1261 foreach my $sp ( keys %species_hash ) {
1262 foreach my $gr ( keys %group_hash ) {
1263 foreach my $ty ( keys %type_hash ) {
1264 my $temp = $class->get_adaptor( $sp, $gr, $ty );
1265 if ( defined($temp) ) {
1278 Arg [1] : name of the species to add alias for
1279 Arg [2] : name of the alias
1280 Example : Bio::EnsEMBL::Registry->add_alias("Homo Sapiens","Human");
1281 Description: add alternative name for the species.
1289 my ($class, $species,$key) = @_;
1291 $registry_register{'_ALIAS
'}{lc($key)} = lc($species);
1297 Arg [1] : name of the species to remove alias for
1298 Arg [2] : name of the alias
1299 Example : Bio::EnsEMBL::Registry->remove_alias("Homo Sapiens","Human");
1300 Description: remove alternative name for the species.
1308 my ($class, $species,$key) = @_;
1310 delete $registry_register{'_ALIAS
'}{lc($key)};
1318 Arg [1] : name of the possible alias to get species for
1319 Example : Bio::EnsEMBL::Registry->get_alias("Human");
1320 Description: get proper species name.
1321 Returntype : species name
1328 my ( $class, $key, $no_warn ) = @_;
1330 if ( !defined( $registry_register{'_ALIAS
'}{ lc($key) } ) ) {
1331 if ( ( !defined( $registry_register{_SPECIES}{ lc($key) } ) ) and
1332 ( !defined( $registry_register{_ALIAS}{ lc($key) } ) ) )
1334 if ( ( !defined($no_warn) ) or ( !$no_warn ) ) {
1335 warning( "$key is not a valid species name " .
1336 "(check DB and API version)" );
1340 else { return $key }
1343 return $registry_register{'_ALIAS
'}{ lc($key) };
1346 =head2 get_all_aliases
1348 Arg [1] : Species name to retrieve aliases for
1349 (may be an alias as well).
1350 Example : Bio::EnsEMBL::Registry->get_all_aliases('Homo sapiens
');
1351 Description: Returns all known aliases for a given species (but not the
1352 species name/alias that was given).
1353 Returntype : ArrayRef of all known aliases
1355 Status : Development
1359 sub get_all_aliases {
1360 my ( $class, $key ) = @_;
1362 my $species = $registry_register{_ALIAS}{ lc($key) };
1365 if ( defined($species) ) {
1366 foreach my $alias ( keys( %{ $registry_register{_ALIAS} } ) ) {
1367 if ( $species ne $alias
1368 && $species eq $registry_register{_ALIAS}{ lc($alias) } )
1370 push( @aliases, $alias );
1380 Arg [1] : name of the possible alias to get species for
1381 Example : Bio::EnsEMBL::Registry->alias_exists("Human");
1382 Description: does the species name exist.
1383 Returntype : 1 if exists else 0
1390 my ( $class, $key ) = @_;
1392 return defined( $registry_register{'_ALIAS
'}{ lc($key) } );
1395 =head2 set_disconnect_when_inactive
1397 Example : Bio::EnsEMBL::Registry->set_disconnect_when_inactive();
1398 Description: Set the flag to make sure that the database connection is dropped if
1399 not being used on each database.
1406 sub set_disconnect_when_inactive{
1407 foreach my $dba ( @{get_all_DBAdaptors()}){
1408 my $dbc = $dba->dbc;
1409 # Disconnect if connected
1410 $dbc->disconnect_if_idle() if $dbc->connected();
1411 $dbc->disconnect_when_inactive(1);
1416 =head2 set_reconnect_when_lost
1418 Example : Bio::EnsEMBL::Registry->set_reconnect_when_lost();
1419 Description: Set the flag to make sure that the database connection is not lost before it's used.
1420 This is useful
for long running jobs (over 8hrs).
1427 sub set_reconnect_when_lost{
1428 foreach my $dba ( @{get_all_DBAdaptors()}){
1429 my $dbc = $dba->dbc;
1430 $dbc->reconnect_when_lost(1);
1435 =head2 disconnect_all
1438 Description: disconnect from all the databases.
1445 sub disconnect_all {
1446 foreach my $dba ( @{get_all_DBAdaptors()||[]} ){
1447 my $dbc = $dba->dbc;
1449 # Disconnect if connected
1450 $dbc->disconnect_if_idle()
if $dbc->connected();
1455 =head get_DBAdaptor_count
1458 Description : Returns the count of database adaptors currently held by
1460 Returntype : Int count of database adaptors currently known
1465 sub get_DBAdaptor_count {
1466 return scalar(@{$registry_register{
'_DBA'}})
if(defined $registry_register{
'_DBA'});
1470 =head2 change_access
1472 Will change the username and password
for a set of databases.
1473 if host,user or database names are missing then these are not checked.
1474 So
for example
if you
do not specify a database then ALL databases on
1475 the specified host and port will be changed.
1477 Arg [1] : name of the host to change access on
1478 Arg [2] : port number to change access on
1479 Arg [3] : name of the user to change access on
1480 Arg [4] : name of the database to change access on
1481 Arg [5] : name of the
new user
1482 Arg [6] :
new password
1485 Description: change username and password on one or more databases
1493 my ($self, $host,$port,$user,$dbname,$new_user,$new_pass) = @_;
1494 foreach my $dba ( @{$registry_register{
'_DBA'}}){
1495 my $dbc = $dba->dbc;
1496 if((((!defined($host)) or ($host eq $dbc->host))) and
1497 (((!defined($port)) or ($port eq $dbc->port))) and
1498 (((!defined($user)) or ($user eq $dbc->username))) and
1499 ((!defined($dbname)) or ($dbname eq $dbc->dbname))){
1500 if($dbc->connected()){
1501 $dbc->db_handle->disconnect();
1502 $dbc->connected(undef);
1504 # over write the username and password
1505 $dbc->username($new_user);
1506 $dbc->password($new_pass);
1514 =head2 load_registry_from_url
1516 Arg [1] :
string $url
1517 Arg [2] : (optional) integer
1518 If not 0, will print out all information.
1519 Arg [3] : (optional) integer
1520 This option will turn off caching
for slice features, so,
1521 every time a set of features is retrieved, they will come
1522 from the database instead of the cache. This option is only
1523 recommended
for advanced users, specially
if you need to
1524 store and retrieve features. It might reduce performance when
1525 querying the database
if not used properly. If in doubt,
do
1526 not use it or ask in the developer mailing list.
1528 Example : load_registry_from_url(
1529 'mysql://anonymous@ensembldb.ensembl.org:3306');
1531 load_registry_from_url(
1532 'mysql://anonymous@ensembldb.ensembl.org:3306/homo_sapiens_core_65_37?group=core&species=homo_sapiens'
1535 load_registry_from_url(
1536 'mysql://anonymous@ensembldb.ensembl.org:3306/homo_sapiens_core_65_37?group=core'
1540 Description: Will load the correct versions of the ensembl
1541 databases
for the software release it can find on
1542 a database instance into the registry. Also adds
1543 a set of standard aliases. The url format is:
1545 can also request a specific version
for the databases
1546 by adding a slash and the version number but your
1547 script may crash as the API version won
't match the
1550 You can also specify a database name which will cause the
1551 loading of a single DBAdaptor instance. Parameters are
1552 mapped from a normal URL parameter set to their DBAdaptor
1553 equivalent. Group must be defined.
1555 Returntype : Int count of the DBAdaptor instances which can be found in the
1558 Exceptions : Thrown if the given URL does not parse according to the above
1559 scheme and if the specified database cannot be connected to
1560 (see L<load_registry_from_db> for more information)
1565 sub load_registry_from_url {
1566 my ( $self, $url, $verbose, $no_cache ) = @_;
1568 if ( $url =~ /^mysql\:\/\/([^\@]+\@)?([^\:\/]+)(\:\d+)?(\/\d+)?\/?$/x ) {
1574 $user_pass =~ s/\@$//;
1575 my ( $user, $pass ) = $user_pass =~ m/([^\:]+)(\:.+)?/x;
1576 $pass =~ s/^\://x if ($pass);
1577 $port =~ s/^\://x if ($port);
1578 $version =~ s/^\///x if ($version);
1580 return $self->load_registry_from_db(
1585 -db_version => $version,
1586 -verbose => $verbose,
1587 -no_cache => $no_cache
1590 my $uri = parse_uri($url);
1592 if($uri->scheme() eq 'mysql
') {
1593 my %params = $uri->generate_dbsql_params();
1594 if($params{-DBNAME}) {
1595 $params{-SPECIES} = $params{-DBNAME} unless $params{-SPECIES};
1596 $params{-NO_CACHE} = 1 if $no_cache;
1597 my $group = $params{-GROUP};
1598 my $class = $self->_group_to_adaptor_class($group);
1600 printf("Loading database '%s
' from group '%s
' with DBAdaptor class '%s
' from url %s\n", $params{-DBNAME}, $group, $class, $url);
1602 $class->new(%params);
1607 throw("Only MySQL URLs are accepted. Given URL was '${url}
'");
1608 } ## end sub load_registry_from_url
1611 =head2 load_registry_from_db
1614 The domain name of the database host to connect to.
1617 The name of the database user to connect with.
1619 Arg [PASS] : (optional) string
1620 The password to be used to connect to the database.
1622 Arg [PORT] : (optional) integer
1623 The port to use when connecting to the database.
1625 Arg [VERBOSE]: (optional) boolean
1626 Whether to print database messages. This includes a listing
1627 of all available species & databases.
1629 Arg [SPECIES]: (optional) string
1630 By default, all databases that are found on the
1631 server and that corresponds to the correct release
1632 are probed for aliases etc. For some people,
1633 depending on where they are in the world, this might
1634 be a slow operation. With the '-species
' argument,
1635 one may reduce the startup time by restricting the
1636 set of databases that are probed to those of a
1639 Note that the latin name of the species is required,
1640 e.g., 'homo sapiens
', 'gallus gallus
', 'callithrix
1641 jacchus
' etc. It may be the whole species name,
1642 or only the first part of the name, e.g. 'homo
',
1643 'gallus
', or 'callithrix
'. This will be used in
1644 matching against the name of the databases.
1646 Arg [DB_VERSION]: (optional) integer
1647 By default, only databases corresponding to the
1648 current API version are loaded. This argument
1649 allows the script to use databases from another
1650 version although it might not work properly. This
1651 argument should only be used for production or
1652 testing purposes and if you really know what you are
1655 Arg [WAIT_TIMEOUT]: (optional) integer
1656 Time in seconds for the wait timeout to happen.
1657 Time after which the connection is deleted if not
1658 used. By default this is 28800 (8 hours), so set
1659 this to greater than this if your connection are
1660 getting deleted. Only set this if you are having
1661 problems and know what you are doing.
1663 Arg [-NO_CACHE]: (optional) boolean
1664 This option will turn off caching for slice features,
1665 so, every time a set of features is retrieved, they
1666 will come from the database instead of the cache. This
1667 option is only recommended for advanced users, specially
1668 if you need to store and retrieve features. It might
1669 reduce performance when querying the database if not
1670 used properly. If in doubt, do not use it or ask in the
1671 developer mailing list.
1673 Arg [SPECIES_SUFFIX]: (optional) string
1674 This option will append the string to the species name
1675 in the registry for all databases found on this server.
1679 $registry->load_registry_from_db(
1680 -host => 'ensembldb.ensembl.org
',
1681 -user => 'anonymous
',
1685 Description: Will load the correct versions of the Ensembl
1686 databases for the software release it can find on a
1687 database instance into the registry. Also adds a set
1688 of standard aliases.
1690 Returntype : Int count of the DBAdaptor instances which can be found in the
1691 registry due to this method call.
1693 Exceptions : Thrown if the given MySQL database cannot be connected to
1694 or there is any error whilst querying the database.
1699 sub load_registry_from_db {
1700 my ( $self, @args ) = @_;
1702 my ( $host, $port, $user,
1703 $pass, $verbose, $db_version,
1704 $wait_timeout, $no_cache, $species, $species_suffix, $db_prefix )
1705 = rearrange( [ 'HOST
', 'PORT
',
1707 'VERBOSE
', 'DB_VERSION
',
1708 'WAIT_TIMEOUT
', 'NO_CACHE
',
1709 'SPECIES
', 'SPECIES_SUFFIX
', 'DB_PREFIX
' ],
1712 my $ignore_multi = 0;
1714 if ( defined($species) ) {
1715 $species = lc($species);
1716 $species =~ tr/ -/__/;
1719 if (!defined($species_suffix)) {
1720 $species_suffix = "";
1722 if (defined($db_prefix)) {
1723 $db_prefix = $db_prefix . '_
';
1728 if(! defined $db_version) {
1729 # Do checking for the -DB_VERSION flag which can be mis-spelt. Regex assembled using:
1730 # perl -MRegexp::Assemble -e '$r=Regexp::Assemble->new(); $r->add($_)
for (
"-dbversion",
"-version",
"-verion",
"-verison"); print $r->re,
"\n";
'
1731 my %hashed_args = @args;
1732 my ($possible_key) = grep { $_ =~ /(?-xism:-(?:ver(?:is?|si)|dbversi)on)/xism } keys %hashed_args;
1734 my $msg = sprintf(q{Detected no -DB_VERSION flag but found '%s
'; assuming a mis-spelling. Please fix}, $possible_key);
1736 $db_version = $hashed_args{$possible_key};
1742 my $ontology_version;
1745 my $taxonomy_db_versioned;
1746 my $ensembl_metadata_db;
1747 my $ensembl_metadata_db_versioned;
1749 my $production_dba_ok =
1750 eval { require Bio::EnsEMBL::Production::DBSQL::DBAdaptor; 1 };
1752 my $production_version;
1755 my $stable_ids_version;
1757 $user ||= "anonymous";
1758 if ( !defined($port) ) {
1760 if ( $host eq "ensembldb.ensembl.org" && defined($db_version) && $db_version < 48 ) {
1765 $wait_timeout ||= 0;
1767 my $original_count = $self->get_DBAdaptor_count();
1769 my $err_pattern = 'Cannot %s to the Ensembl MySQL server at %s:%d; check your settings & DBI error message: %s
';
1771 my $dbh = DBI->connect( "DBI:mysql:host=$host;port=$port", $user, $pass ) or
1772 throw(sprintf($err_pattern, 'connect
', $host, $port, $DBI::errstr));
1774 throw(sprintf($err_pattern, 'ping
', $host, $port, $DBI::errstr));
1776 my $res = $dbh->selectall_arrayref('SHOW DATABASES
');
1777 my @dbnames = map { $_->[0] } @$res;
1780 my $software_version = software_version();
1782 if ( defined($db_version) ) {
1783 $software_version = $db_version;
1787 printf( "Will only load v%d databases\n", $software_version );
1790 # From the list of all the databses create a tempory hash of those we
1793 for my $db (@dbnames) {
1794 if ( $db =~ /^(\w+_collection_\w+(?:_\d+)?)_((\d+)_\w+)/ )
1795 { # NEEDS TO BE FIRST TO PICK UP COLLECTION DBS
1796 if ( $3 eq $software_version ) {
1799 } elsif ( $db =~ /^(.+)_(userdata)$/x ) {
1802 $db =~ /^(ensembl_compara # compara database
1803 (?:_\w+)*?) # optional ensembl genomes bit
1807 if ( $2 eq $software_version ) {
1810 } elsif ( $db =~ /^(ensembl_ancestral(?:_\w+?)*?)_(\d+)$/x ) {
1811 if ( $2 eq $software_version ) {
1814 } elsif ( $db =~ /^ensembl(?:genomes)?_ontology_(?:\d+_)?(\d+)/x ) {
1815 if ( $1 eq $software_version ) {
1817 $ontology_version = $1;
1819 } elsif ( $db =~ /^ncbi_taxonomy$/ ) {
1822 elsif ( $db =~ m{ \A ncbi_taxonomy_(\d+) \z }msx ) {
1823 if ( $1 eq $software_version ) {
1824 $taxonomy_db_versioned = $db;
1826 } elsif ( $db =~ /^ensembl_metadata$/ ) {
1827 $ensembl_metadata_db = $db;
1829 elsif ( $db =~ m{ \A ensembl_metadata_(\d+) \z }msx ) {
1830 if ( $1 eq $software_version ) {
1831 $ensembl_metadata_db_versioned = $db;
1833 } elsif ( $production_dba_ok and $db =~ /^ensembl(?:genomes)?_production(_\d+)?/x ) {
1834 # production db can come with no version (i.e. that on ens-staging1),
1835 # but it's backed up with a release number
1839 if ($software_version and $version eq $software_version) {
1840 $production_db = $db;
1841 $production_version = $version;
1843 }
else { #
this is the
default choice
1844 $production_db = $db
if $db =~ /^ensembl(?:genomes)?_production$/;
1846 } elsif ( $db =~ /^ensembl(?:genomes)?_stable_ids_(?:\d+_)?(\d+)/x ) {
1847 if ( $1 eq $software_version ) {
1848 $stable_ids_db = $db;
1849 $stable_ids_version = $1;
1853 $db =~ /^(?:$db_prefix)([a-z]+_[a-z0-9]+(?:_[a-z0-9]+)? # species name e.g. homo_sapiens or canis_lupus_familiaris
1856 (?:_\d+)?) # optional end bit
for ensembl genomes databases
1858 (\d+) # database release
1860 (\w+)$ # assembly number can have letters too e.g 37c
1865 # Species specific databases (core, cdna, vega etc.)
1867 my ( $sp_name, $db_rel, $assem ) = ( $1, $2, $3 );
1868 if ($db_prefix) { $sp_name = $db_prefix . $sp_name; }
1870 if ( !defined($species) || $sp_name =~ /^$species/ ) {
1871 if ( $db_rel eq $software_version ) {
1872 $temp{$sp_name} = $db_rel .
"_" . $assem;
1877 # warn( sprintf( "Skipping database '%s'\n", $db ) );
1879 } ## end
for my $db (@dbnames)
1883 foreach my $key ( keys %temp ) {
1884 push @dbnames, $key .
"_" . $temp{$key};
1887 # Register Core like databases
1888 my $core_like_dbs_found = 0;
1889 foreach my $type (qw(core cdna vega vega_update otherfeatures rnaseq ccds)) {
1891 my @dbs = grep { /^(?:$db_prefix)[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)? # species name
1893 $type # the database type
1895 (?:\d+_)? # optional end bit
for ensembl genomes
1896 \d+ # database release
1901 $core_like_dbs_found = 1;
1904 foreach my $database (@dbs) {
1905 if ( index( $database,
'collection' ) != -1 ) {
1906 # Skip multi-species databases.
1911 my ( $prefix, $species, $num ) =
1912 ( $database =~ /(^$db_prefix)([a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?) # species name
1916 (?:\d+_)? # optional endbit
for ensembl genomes
1917 (\d+)
# databases release
1921 if(!defined($species)){
1922 warn
"Cannot extract species name from database '$database'";
1928 -species => $species.$species_suffix,
1933 -dbname => $database,
1934 -wait_timeout => $wait_timeout,
1935 -no_cache => $no_cache );
1938 printf(
"Species '%s' loaded from database '%s'\n",
1939 $species, $database );
1944 # Register multi-species databases
1946 my @multi_dbs = grep { /^\w+_collection_core_\w+$/ } @dbnames;
1948 if (!$ignore_multi) {
1949 foreach my $multidb (@multi_dbs) {
1950 my $sth = $dbh->prepare(
1952 "SELECT species_id, meta_value FROM %s.meta "
1953 .
"WHERE meta_key = 'species.db_name'",
1954 $dbh->quote_identifier($multidb) ) );
1958 my ( $species_id, $species );
1959 $sth->bind_columns( \( $species_id, $species ) );
1961 while ( $sth->fetch() ) {
1964 -species => $species.$species_suffix,
1965 -species_id => $species_id,
1966 -multispecies_db => 1,
1971 -dbname => $multidb,
1972 -wait_timeout => $wait_timeout,
1973 -no_cache => $no_cache
1977 printf(
"Species '%s' (id:%d) loaded from database '%s'\n",
1978 $species, $species_id, $multidb );
1981 } ## end
foreach my $multidb (@multi_dbs)
1984 if(!$core_like_dbs_found && $verbose) {
1985 print(
"No core-like databases found. Check your DB_VERSION (used '$software_version')\n");
1990 my @userupload_dbs = grep { /_userdata$/ } @dbnames;
1991 if (!$ignore_multi) {
1992 for my $userupload_db (@userupload_dbs) {
1993 if ( index( $userupload_db,
'collection' ) != -1 ) {
1994 # Skip multi-species databases.
1998 my ($species) = ( $userupload_db =~ /(^.+)_userdata$/ );
2001 -group =>
"userupload",
2002 -species => $species.$species_suffix,
2007 -wait_timeout => $wait_timeout,
2008 -dbname => $userupload_db,
2009 -no_cache => $no_cache );
2012 printf(
"%s loaded\n", $userupload_db );
2017 # Register multi-species userupload databases.
2018 my @userdata_multidbs = grep { /^.+_collection_userdata$/ } @dbnames;
2020 if (!$ignore_multi) {
2021 foreach my $multidb (@userdata_multidbs) {
2022 my $sth = $dbh->prepare(
2024 "SELECT species_id, meta_value FROM %s.meta "
2025 .
"WHERE meta_key = 'species.db_name'",
2026 $dbh->quote_identifier($multidb) ) );
2030 my ( $species_id, $species );
2031 $sth->bind_columns( \( $species_id, $species ) );
2033 while ( $sth->fetch() ) {
2035 -group =>
"userupload",
2036 -species => $species.$species_suffix,
2037 -species_id => $species_id,
2038 -multispecies_db => 1,
2043 -dbname => $multidb,
2044 -wait_timeout => $wait_timeout,
2045 -no_cache => $no_cache
2049 printf(
"Species '%s' (id:%d) loaded from database '%s'\n",
2050 $species, $species_id, $multidb );
2053 } ## end
foreach my $multidb (@userdata_multidbs)
2058 my $test_eval = eval
"require Bio::EnsEMBL::Variation::DBSQL::DBAdaptor"; ## no critic
2059 if ($@or (!$test_eval)) {
2060 # Ignore variations as code required not there for this
2063 "Bio::EnsEMBL::Variation::DBSQL::DBAdaptor module not found "
2064 .
"so variation databases will be ignored if found\n" );
2069 grep { /^[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?_variation_(?:\d+_)?\d+_/ } @dbnames;
2071 if(! @variation_dbs && $verbose) {
2072 print(
"No variation databases found\n");
2075 for my $variation_db (@variation_dbs) {
2077 if ( index( $variation_db,
'collection' ) != -1 ) {
2078 # Skip multi-species databases.
2082 my ( $species, $num ) =
2083 ( $variation_db =~ /(^[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?)_variation_(?:\d+_)?(\d+)_/ );
2085 Bio::EnsEMBL::Variation::DBSQL::DBAdaptor->new(
2086 -group =>
"variation",
2087 -species => $species.$species_suffix,
2092 -wait_timeout => $wait_timeout,
2093 -dbname => $variation_db,
2094 -no_cache => $no_cache );
2097 printf(
"%s loaded\n", $variation_db );
2101 # Register variation multispecies databases
2102 my @variation_multidbs =
2103 grep { /^\w+_collection_variation_\w+$/ } @dbnames;
2105 if (!$ignore_multi) {
2106 foreach my $multidb (@variation_multidbs) {
2107 my $sth = $dbh->prepare(
2108 sprintf(
'SELECT species_id, meta_value FROM %s.meta ',
2109 $dbh->quote_identifier($multidb) )
2110 .
"WHERE meta_key = 'species.db_name'"
2115 my ( $species_id, $species );
2116 $sth->bind_columns( \( $species_id, $species ) );
2118 while ( $sth->fetch() ) {
2119 my $dba = Bio::EnsEMBL::Variation::DBSQL::DBAdaptor->new(
2120 -group =>
'variation',
2121 -species => $species.$species_suffix,
2122 -species_id => $species_id,
2123 -multispecies_db => 1,
2128 -dbname => $multidb,
2129 -wait_timeout => $wait_timeout,
2130 -no_cache => $no_cache
2134 printf(
"Species '%s' (id:%d) loaded from database '%s'\n",
2135 $species, $species_id, $multidb );
2138 } ## end
foreach my $multidb (@variation_multidbs)
2142 my $func_eval = eval
"require Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor"; ## no critic
2143 if ($@ or (!$func_eval)) {
2145 # Ignore funcgen DBs as code required not there for this
2146 print(
"Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor module not found "
2147 .
"so functional genomics databases will be ignored if found\n"
2152 grep { /^[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?_funcgen_(?:\d+_)?\d+_/ } @dbnames;
2154 if(! @funcgen_dbs && $verbose) {
2155 print(
"No funcgen databases found\n");
2158 for my $funcgen_db (@funcgen_dbs) {
2159 if ( index( $funcgen_db,
'collection' ) != -1 ) {
2160 # Skip multi-species databases.
2164 my ( $species, $num ) =
2165 ( $funcgen_db =~ /(^[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?)_funcgen_(?:\d+_)?(\d+)_/ );
2166 my $dba = Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor->new(
2167 -group =>
"funcgen",
2168 -species => $species.$species_suffix,
2173 -wait_timeout => $wait_timeout,
2174 -dbname => $funcgen_db,
2175 -no_cache => $no_cache
2179 printf(
"%s loaded\n", $funcgen_db );
2183 # Register functional genomics multispecies databases
2184 my @funcgen_multidbs =
2185 grep { /^\w+_collection_funcgen_\w+$/ } @dbnames;
2187 if (!$ignore_multi) {
2188 foreach my $multidb (@funcgen_multidbs) {
2189 my $sth = $dbh->prepare(
2190 sprintf(
'SELECT species_id, meta_value FROM %s.meta ',
2191 $dbh->quote_identifier($multidb) )
2192 .
"WHERE meta_key = 'species.db_name'"
2197 my ( $species_id, $species );
2198 $sth->bind_columns( \( $species_id, $species ) );
2200 while ( $sth->fetch() ) {
2201 my $dba = Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor->new(
2202 -group =>
'funcgen',
2203 -species => $species.$species_suffix,
2204 -species_id => $species_id,
2205 -multispecies_db => 1,
2210 -dbname => $multidb,
2211 -wait_timeout => $wait_timeout,
2212 -no_cache => $no_cache
2216 printf(
"Species '%s' (id:%d) loaded from database '%s'\n",
2217 $species, $species_id, $multidb );
2220 } ## end
foreach my $multidb (@funcgen_multidbs)
2222 } ## end
else [
if ($@) ]
2226 my @compara_dbs = grep { /^ensembl_compara/ } @dbnames;
2228 if (!$ignore_multi) {
2230 my $comp_eval = eval
"require Bio::EnsEMBL::Compara::DBSQL::DBAdaptor"; ## no critic
2231 if ($@ or (!$comp_eval)) {
2232 # Ignore Compara as code required not there for this
2235 "Bio::EnsEMBL::Compara::DBSQL::DBAdaptor "
2236 .
"not found so the following compara "
2237 .
"databases will be ignored: %s\n",
2238 join(
', ', @compara_dbs ) );
2241 foreach my $compara_db (@compara_dbs) {
2242 # Looking for EnsEMBL Genomes Comparas.
2243 # ensembl_compara_bacteria_2_53 is registered as
2244 # 'bacteria', ensembl_compara_pan_homology_2_53 is
2245 # registered as 'pan_homology', ensembl_compara_53 is
2246 # registered as 'multi', and the alias 'compara' still
2250 $compara_db =~ /^ensembl_compara_(\w+)(?:_\d+){2}$/xm;
2252 $species ||=
'multi';
2254 my $dba = Bio::EnsEMBL::Compara::DBSQL::DBAdaptor->new(
2255 -group =>
'compara',
2256 -species => $species.$species_suffix,
2261 -wait_timeout => $wait_timeout,
2262 -dbname => $compara_db,
2263 -no_cache => $no_cache
2267 printf(
"%s loaded\n", $compara_db );
2269 } ## end
foreach my $compara_db (@compara_dbs)
2270 } ## end
else [
if ($@)
2271 } elsif ($verbose) {
2272 print(
"No Compara databases found\n");
2276 # Ancestral sequences
2279 sort grep { /^ensembl_ancestral/ } @dbnames;
2281 if (@ancestral_dbs && !$ignore_multi) {
2282 foreach my $ancestral_db (@ancestral_dbs) {
2283 # Looking for Compara's "ancestral" databases.
2284 # ensembl_ancestral_plants_47_100 is registered with the 'plants'
2285 # prefix, while ensembl_ancestral_100 is not given any prefix for
2286 # backwards compatibility.
2287 # Similarly, contrary to the nomenclature, "Ancestral sequences"
2288 # is the species (production) name and "ancestral_sequences" is
2291 my ($division) = $ancestral_db =~ /^ensembl_ancestral_(\w+)(?:_\d+){2}$/xm;
2293 $species = (ucfirst $division).
' Ancestral sequences'.$species_suffix;
2294 $alias = $division.
'_ancestral_sequences'.$species_suffix;
2296 $species =
'Ancestral sequences'.$species_suffix;
2297 $alias =
'ancestral_sequences'.$species_suffix;
2302 -species => $species,
2307 -wait_timeout => $wait_timeout,
2308 -dbname => $ancestral_db,
2309 -no_cache => $no_cache
2313 -species => $species,
2318 printf(
"%s loaded\n", $ancestral_db );
2321 } elsif ($verbose) {
2322 print(
"No ancestral database found\n");
2327 if ( defined($ontology_version) && $ontology_version != 0 && !$ignore_multi) {
2332 '-species' =>
'multi' . $species_suffix,
2333 '-group' =>
'ontology',
2338 '-dbname' => $ontology_db, );
2341 printf(
"%s loaded\n", $ontology_db );
2345 print(
"No ontology database found\n");
2350 if ( ( defined $taxonomy_db ) || ( defined $taxonomy_db_versioned ) ) {
2352 my $has_taxonomy = eval {require Bio::EnsEMBL::Taxonomy::DBSQL::TaxonomyDBAdaptor};
2353 if($@ or (!defined $has_taxonomy)) {
2355 print
"ensembl_taxonomy API not found - ignoring $taxonomy_db\n";
2359 my $taxonomy_dbname;
2360 # Versioned database has priority over unversioned one.
2361 if ( defined $taxonomy_db_versioned ) {
2362 $taxonomy_dbname = $taxonomy_db_versioned;
2365 $taxonomy_dbname = $taxonomy_db;
2368 my $dba = Bio::EnsEMBL::Taxonomy::DBSQL::TaxonomyDBAdaptor->new(
2369 '-species' =>
'multi' . $species_suffix,
2370 '-group' =>
'taxonomy',
2375 '-dbname' => $taxonomy_dbname, );
2378 printf(
"%s loaded\n", $taxonomy_dbname );
2383 print(
"No taxonomy database found\n");
2388 if ( ( defined $ensembl_metadata_db ) || ( defined $ensembl_metadata_db_versioned ) ) {
2390 my $has_metadata = eval {require Bio::EnsEMBL::MetaData::DBSQL::MetaDataDBAdaptor};
2391 if($@ or (!defined $has_metadata)) {
2393 print
"ensembl_metadata API not found - ignoring $ensembl_metadata_db\n";
2397 my $metadata_dbname;
2398 # Versioned database has priority over unversioned one.
2399 if ( defined $ensembl_metadata_db_versioned ) {
2400 $metadata_dbname = $ensembl_metadata_db_versioned;
2403 $metadata_dbname = $ensembl_metadata_db;
2406 my $dba = Bio::EnsEMBL::MetaData::DBSQL::MetaDataDBAdaptor->new(
2407 '-species' =>
'multi' . $species_suffix,
2408 '-group' =>
'metadata',
2413 '-dbname' => $metadata_dbname, );
2416 printf(
"%s loaded\n", $metadata_dbname );
2421 print(
"No ensembl_metadata database found\n");
2426 if ( $production_dba_ok and defined($production_db) && !$ignore_multi) {
2427 # require Bio::EnsEMBL::Production::DBSQL::DBAdaptor;
2430 Bio::EnsEMBL::Production::DBSQL::DBAdaptor->new(
2431 '-species' =>
'multi' . $species_suffix,
2432 '-group' =>
'production',
2437 '-dbname' => $production_db, );
2440 printf(
"%s loaded\n", $production_db );
2444 print(
"No production database or adaptor found\n");
2449 if ( defined($stable_ids_db) && $stable_ids_version != 0 && !$ignore_multi) {
2453 '-species' =>
'multi' . $species_suffix,
2454 '-group' =>
'stable_ids',
2459 '-dbname' => $stable_ids_db, );
2462 printf(
"%s loaded\n", $stable_ids_db );
2469 -species =>
'multi'.$species_suffix,
2470 -alias => [
'compara'.$species_suffix] );
2473 -species =>
'multi'.$species_suffix,
2474 -alias => [
'ontology'.$species_suffix] );
2476 $production_dba_ok and
2478 -species =>
'multi'.$species_suffix,
2479 -alias => [
'production'.$species_suffix] );
2482 -species =>
'multi'.$species_suffix,
2483 -alias => [
'stable_ids'.$species_suffix] );
2485 # Register aliases as found in adaptor meta tables.
2487 $self->find_and_add_aliases(
'-handle' => $dbh,
2488 '-species_suffix' => $species_suffix );
2492 my $count = $self->get_DBAdaptor_count() - $original_count;
2493 return $count >= 0 ? $count : 0;
2495 } ## end sub load_registry_from_db
2497 =head2 _group_to_adaptor_class
2499 Arg [1] : The group you wish to decode to an adaptor
class
2501 Description : Has an
internal lookup of groups to their adaptor classes
2503 Exceptions : Thrown
if the group is unknown
2508 sub _group_to_adaptor_class {
2509 my ($self, $group) = @_;
2510 my $class = $group2adaptor{$group};
2511 if (!defined $class) {
2513 cdna =>
'Bio::EnsEMBL::DBSQL::DBAdaptor',
2514 rnaseq =>
'Bio::EnsEMBL::DBSQL::DBAdaptor',
2517 throw "Group '${group}' is unknown" if ! $class;
2522 =head2 find_and_add_aliases
2525 The adaptor to use to retrieve aliases from.
2527 Arg [GROUP] : (optional)
string
2528 The group you want to find aliases
for. If not
2529 given assumes all types.
2531 Arg [HANDLE] : (optional) DBI database handle
2532 A connected database handle to use instead of
2533 the database handles stored in the DBAdaptors.
2534 Bypasses the use of MetaContainer.
2536 Arg [SPECIES_SUFFIX]: (optional)
string
2537 This option will append the
string to the species
2538 name in the registry
for all databases.
2545 Description : Looks in the meta container
for each database
for
2546 an entry called
"species.alias". If any are found
2547 then the species adaptor is registered to that
2548 set of aliases. This can work across any adaptor
2549 which has a MetaContainer. If no MetaContainer
2550 can be returned from a given adaptor then no alias
2551 searching is performed.
2554 Exceptions : Throws
if an alias is found in more than one species.
2559 sub find_and_add_aliases {
2562 my ($adaptor, $group, $dbh, $species_suffix ) =
2563 rearrange( [
'ADAPTOR',
'GROUP',
'HANDLE',
'SPECIES_SUFFIX' ], @_ );
2565 #Can be undef; needs to be something to avoid warnings
2566 $species_suffix ||= q{};
2569 if ( defined($adaptor) ) {
2571 } elsif ( defined($dbh) ) {
2573 if ( length($species_suffix) > 0 ) {
2574 my @full = @{ $class->get_all_DBAdaptors(
'-GROUP' => $group ) };
2576 foreach my $db (@full) {
2577 if ( $db->species =~ /$species_suffix/ ) {
2583 @dbas = @{ $class->get_all_DBAdaptors(
'-GROUP' => $group ) };
2587 @dbas = @{ $class->get_all_DBAdaptors(
'-GROUP' => $group ) };
2590 my $aliases_for_dbc = {};
2592 foreach my $dba (@dbas) {
2594 my $species = $dba->species();
2596 if ( defined($dbh) ) {
2598 my $dbname = $dba->dbc()->dbname();
2600 if (!defined $aliases_for_dbc->{$dbname}) {
2602 my $sth = $dbh->prepare(sprintf(
"SELECT species_id,meta_value FROM %s.meta "
2603 .
"WHERE meta_key = 'species.alias' ", $dbh->quote_identifier($dbname))
2606 # Execute, and don't care about errors (there will be errors for
2607 # databases without a 'meta' table.
2608 $sth->{
'PrintError'} = 0;
2609 $sth->{
'RaiseError'} = 0;
2610 if (!$sth->execute()) { next }
2611 $sth->{
'PrintError'} = $dbh->{
'PrintError'};
2612 $sth->{
'RaiseError'} = $dbh->{
'RaiseError'};
2616 $sth->bind_columns(\$species_id, \$alias);
2617 while ($sth->fetch()) {
2618 push(@{$aliases_for_dbc->{$dbname}{$species_id}}, $alias);
2622 @aliases = @{$aliases_for_dbc->{$dbname}{$dba->species_id()}||[]}
2625 my $meta_container = eval { $dba->get_MetaContainer() };
2627 if ( defined($meta_container) ) {
2629 @{ $meta_container->list_value_by_key(
'species.alias') }
2633 # Need to disconnect so we do not spam the MySQL servers trying to
2634 # get aliases. Can only call disonnect if dbc was defined.
2635 if ( defined( $dba->dbc() ) ) {
2636 $dba->dbc()->disconnect_if_idle();
2640 foreach my $alias (@aliases) {
2641 my $alias_suffix = $alias.$species_suffix;
2642 #Lowercase because stored aliases are lowercased
2643 my $lc_species = lc($species);
2644 my $lc_alias_suffix = lc($alias_suffix);
2645 if ( !$class->alias_exists( $alias_suffix )
2646 && $lc_species ne $lc_alias_suffix )
2648 $class->add_alias( $species, $alias_suffix );
2650 $lc_species ne $class->get_alias( $alias_suffix ) )
2652 $class->remove_alias( $species, $alias_suffix );
2656 } ## end
foreach my $dba (@dbas)
2658 } ## end sub find_and_add_aliases
2661 =head2 load_registry_from_multiple_dbs
2663 Arg [1] : Array of hashes, each hash being a set of arguments to
2664 load_registry_from_db() (see above).
2668 $registry->load_registry_from_multiple_dbs( {
2669 '-host' =>
'ensembldb.ensembl.org',
2670 '-user' =>
'anonymous',
2674 '-host' =>
'server.example.com',
2675 '-user' =>
'anonymouse',
2676 '-password' =>
'cheese',
2680 Description: Will call load_registry_from_db() (see above)
2681 multiple times and merge the resulting registries
2682 into one, effectively allowing a user to connect to
2683 databases on multiple database servers from within
2686 If a database is found on more than one server, the
2687 first found instance of that database will be used.
2689 Returntype : Int count of the DBAdaptor instances which can be found in the
2694 sub load_registry_from_multiple_dbs {
2695 my ( $self, @args ) = @_;
2697 my $original_count = $self->get_DBAdaptor_count();
2699 my %merged_register = %registry_register;
2701 foreach my $arg (@args) {
2702 local %registry_register = ();
2706 ($verbose) = rearrange( [
'VERBOSE'], %{$arg} );
2708 $self->load_registry_from_db( %{$arg} );
2711 # Merge the localized %registry_register into %merged_register.
2714 # Merge the _SPECIES and _ALIAS sections of %registry_register.
2715 foreach my $section (
'Species',
'Alias' ) {
2716 my $section_key =
'_' . uc($section);
2718 while ( my ( $key, $value ) =
2719 each( %{ $registry_register{$section_key} } ) )
2721 if ( !exists( $merged_register{$section_key}{$key} ) ) {
2722 $merged_register{$section_key}{$key} = $value;
2723 } elsif ($verbose) {
2724 printf(
"%s '%s' found on multiple servers, "
2725 .
"using first found\n",
2730 } ## end
foreach my $arg (@args)
2732 # Add the DBAs from the _SPECIES section into the _DBA section.
2733 foreach my $species_hash ( values( %{ $merged_register{_SPECIES} } ) )
2735 foreach my $group_hash ( values( %{$species_hash} ) ) {
2736 if ( ref($group_hash) eq
'HASH' && exists( $group_hash->{_DB} ) )
2738 push( @{ $merged_register{_DBA} }, $group_hash->{_DB} );
2743 %registry_register = %merged_register;
2745 my $count = $self->get_DBAdaptor_count() - $original_count;
2746 return $count >= 0 ? $count : 0;
2747 } ## end sub load_registry_from_multiple_dbs
2750 # Web specific routines
2753 =head2 set_default_track
2755 Sets a flag to say that that
this species/group are a
default track and
do not
2756 need to be added as another web track.
2758 Arg [1] : name of the species to get the adaptors
for in the registry.
2759 Arg [2] : name of the type to get the adaptors
for in the registry.
2767 sub set_default_track {
2768 my ( $class, $species, $group ) = @_;
2770 $species = get_alias($species);
2771 $registry_register{
'def_track'}{$species}{ lc($group) } = 1;
2775 =head2 default_track
2777 Check flag to see
if this is a
default track
2779 Arg [1] : name of the species to get the adaptors
for in the registry.
2780 Arg [2] : name of the type to get the adaptors
for in the registry.
2789 my ( $class, $species, $group ) = @_;
2791 $species = get_alias($species);
2793 defined( $registry_register{
'def_track'}{$species}{ lc($group) } ) )
2802 =head2 add_new_tracks
2804 Will add
new gene tracks to the configuration of the WEB server
if they are
2805 not of the type
default and the configuration already has genes in the display.
2807 Arg [1] : hash of the
default configuration of the web page
2810 Called by : UserConfig.pm
2816 my($class, $conf, $pos) = @_;
2820 my $species_reg = $reg->get_alias($conf->{
'species'},
"nothrow");
2822 # print STDERR "Species $species_reg check for default tracks\n";
2823 if(defined($species_reg)){
2824 foreach my $dba (@{$reg->get_all_DBAdaptors()}){
2825 if(!$reg->default_track($dba->species,$dba->group)){
2826 $pars{
'available'} =
"species ".$reg->get_alias($dba->species());
2827 $pars{
'db_alias'} = $dba->group();
2828 # print STDERR "Adding new track for ".$dba->species."\t".$dba->group."\n";
2829 $conf->add_new_track_generictranscript(
'',$dba->group(),
"black",$pos,%pars);
2838 =head2 no_version_check
2840 getter/setter
for whether to
run the version checking
2842 Arg[0] : (optional)
int
2843 Returntype :
int or undef
if not set
2849 sub no_version_check {
2850 my ( $self, $arg ) = @_;
2852 && ( $registry_register{
'_no_version_check'} = $arg );
2854 return $registry_register{
'_no_version_check'};
2857 =head2 no_cache_warnings
2859 Arg[0] :
boolean for turning the flag on and off
2860 Description : Turns off any warnings
about not
using caching in all available
2862 Returntype :
boolean Current status
2867 sub no_cache_warnings {
2868 my ($self, $arg) = @_;
2870 $Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor::SILENCE_CACHE_WARNINGS = $arg;
2872 return $Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor::SILENCE_CACHE_WARNINGS;
2876 =head2 version_check
2878 run the database/API code version check
for a DBAdaptor
2880 Arg[0] : DBAdaptor to check
2881 Returntype :
int 1
if okay, 0
if not the same
2889 my ( $self, $dba ) = @_;
2891 # Check the datbase and versions match
2892 # give warning if they do not.
2893 my $check = no_version_check();
2896 defined( $ENV{HOME} )
2897 and ( -e $ENV{HOME} .
"/.ensemblapi_no_version_check" ) )
2898 or ( defined($check) and ( $check != 0 ) ) )
2904 $self->get_adaptor( $dba->species(), $dba->group(),
2907 my $database_version = 0;
2908 if ( defined($mca) ) {
2909 $database_version = $mca->get_schema_version();
2912 if ( $database_version == 0 ) {
2913 # Try to work out the version
2914 if ( $dba->dbc()->dbname() =~ /^_test_db_/x ) {
2918 # ensembl_metadata was unversioned prior to release 96
2919 # we now have multiple pattern for metadata db name (qrp and grch37) - valid until we merge those.
2920 if ( $dba->dbc()->dbname() =~ /ensembl_metadata(\_?(qrp|grch37)?)/s ) {
2923 # ncbi_taxonomy was unversioned prior to release 100
2924 if ( $dba->dbc()->dbname() eq
'ncbi_taxonomy' ) {
2928 if ( $dba->dbc()->dbname() =~ /(\d+)_\S+$/x ) {
2929 $database_version = $1;
2930 } elsif ( $dba->dbc()->dbname() =~ /ensembl_compara_(\d+)/x ) {
2931 $database_version = $1;
2932 } elsif ( $dba->dbc()->dbname() =~ /ensembl_help_(\d+)/x ) {
2933 $database_version = $1;
2934 } elsif ( $dba->dbc()->dbname() =~ / ensembl_metadata_(\d+) /msx ) {
2935 # Prior to release 96 metadata is supposed to be versionned on meta-1. Not the case since
2936 $database_version = $1;
2937 } elsif ( $dba->dbc()->dbname() =~ /ensembl_ontology_(\d+)/x ) {
2938 $database_version = $1;
2939 } elsif ( $dba->dbc()->dbname() =~ /ensembl_stable_ids_(\d+)/x ) {
2940 $database_version = $1;
2941 } elsif ( $dba->dbc()->dbname() =~ / ncbi_taxonomy_(\d+) /msx ) {
2942 $database_version = $1;
2946 "No database version for database %s "
2947 .
". You must be using a post version 34 database "
2948 .
"with version 34 or later code.\n"
2949 .
"You need to update your database "
2950 .
"or use the appropriate Ensembl software release "
2951 .
"to ensure your script does not crash\n",
2952 $dba->dbc()->dbname() ) );
2954 } ## end
if ( $database_version...
2956 if ( $database_version != software_version() ) {
2959 "For %s there is a difference in the software release (%s) "
2960 .
"and the database release (%s). "
2961 .
"You should update one of these to ensure that your script "
2962 .
"does not crash.\n",
2963 $dba->dbc()->dbname().
"@".$dba->dbc()->host,
2964 software_version(), $database_version
2970 } ## end sub version_check
2972 =head2 get_all_species
2974 Arg [1] : String group type, such as core, or otherfeatures
2975 Description: Method
for getting all valid species names found in available
2976 databases. This excludes the ancestral sequence databases, and
2977 any species from a non-core database. Specifying a group allows
2978 the list to apply to non-core database types.
2979 Example : my @species_names = @{ $reg->get_all_species() };
2980 Returntype : Listref of species names
2984 sub get_all_species {
2985 my ($self,$group) = @_;
2988 foreach my $name (keys %{$registry_register{_SPECIES}}) {
2989 push @species, $name
if (
2990 # limit species names to given db group and no ancestral dbs
2991 $registry_register{_SPECIES}->{$name}->{$group}
2992 && $name !~ /^ancestral/i
2999 =head2 get_species_and_object_type
3001 Description: Get the species name,
object type (gene,
transcript,
3002 translation, or
exon etc.), and database type
for a
3005 Arg [1] : String stable_id
3006 The stable ID to find species and
object type
for.
3008 Arg [2] : String known_type (optional)
3009 The type of the stable ID,
if it is known.
3011 Arg [3] : String known_species (optional)
3012 The species,
if known
3014 Arg [4] : String known_db_type (optional)
3015 The database type,
if known
3017 Example : my $stable_id =
'ENST00000326632';
3019 my ( $species, $object_type, $db_type ) =
3020 $registry->get_species_and_object_type($stable_id);
3023 $registry->get_adaptor( $species, $db_type,
3026 my $object = $adaptor->fetch_by_stable_id($stable_id);
3028 Return type: Array consisting of the species name,
object type,
3029 and database type. The array may be empty
if no
3037 my %stable_id_stmts = (
3038 gene =>
'SELECT m.meta_value '
3040 .
'JOIN %1$s.seq_region USING (seq_region_id) '
3041 .
'JOIN %1$s.coord_system USING (coord_system_id) '
3042 .
'JOIN %1$s.meta m USING (species_id) '
3043 .
'WHERE stable_id = ? '
3044 .
'AND m.meta_key = "species.production_name"',
3046 .
'FROM %1$s.transcript '
3047 .
'JOIN %1$s.seq_region USING (seq_region_id) '
3048 .
'JOIN %1$s.coord_system USING (coord_system_id) '
3049 .
'JOIN %1$s.meta m USING (species_id) '
3050 .
'WHERE stable_id = ? '
3051 .
'AND m.meta_key = "species.production_name"',
3052 exon =>
'SELECT m.meta_value '
3054 .
'JOIN %1$s.seq_region USING (seq_region_id) '
3055 .
'JOIN %1$s.coord_system USING (coord_system_id) '
3056 .
'JOIN %1$s.meta m USING (species_id) '
3057 .
'WHERE stable_id = ? '
3058 .
'AND m.meta_key = "species.production_name"',
3059 translation =>
'SELECT m.meta_value '
3060 .
'FROM %1$s.translation tl '
3061 .
'JOIN %1$s.transcript USING (transcript_id) '
3062 .
'JOIN %1$s.seq_region USING (seq_region_id) '
3063 .
'JOIN %1$s.coord_system USING (coord_system_id) '
3064 .
'JOIN %1$s.meta m USING (species_id) '
3065 .
'WHERE tl.stable_id = ? '
3066 .
'AND m.meta_key = "species.production_name"',
3067 operon =>
'SELECT m.meta_value '
3068 .
'FROM %1$s.operon '
3069 .
'JOIN %1$s.seq_region USING (seq_region_id) '
3070 .
'JOIN %1$s.coord_system USING (coord_system_id) '
3071 .
'JOIN %1$s.meta m USING (species_id) '
3072 .
'WHERE stable_id = ? '
3073 .
'AND m.meta_key = "species.production_name"',
3074 operontranscript =>
'SELECT m.meta_value '
3075 .
'FROM %1$s.operon_transcript '
3076 .
'JOIN %1$s.seq_region USING (seq_region_id) '
3077 .
'JOIN %1$s.coord_system USING (coord_system_id) '
3078 .
'JOIN %1$s.meta m USING (species_id) '
3079 .
'WHERE stable_id = ? '
3080 .
'AND m.meta_key = "species.production_name"',
3084 my %compara_stable_id_stmts = (
3085 genetree =>
'SELECT 1 FROM %1$s.gene_tree_root WHERE stable_id =?',
3086 family =>
'SELECT 1 from %1$s.family where stable_id = ?',
3090 sub get_species_and_object_type {
3091 my ($self, $stable_id, $known_type, $known_species, $known_db_type, $force_long_lookup, $use_archive) = @_;
3093 #get the stable_id lookup database adaptor
3095 my $stable_ids_dba = $self->get_DBAdaptor(
"multi",
"stable_ids", 1);
3097 if ($stable_ids_dba && ! $force_long_lookup) {
3098 return $self->_lookup_db_get_species_and_object_type($stable_id, $known_type, $known_species, $known_db_type, $use_archive);
3101 if(defined $known_type) {
3102 my $lc_known_type = lc $known_type;
3103 if(!exists $stable_id_stmts{$lc_known_type} && ! exists $compara_stable_id_stmts{$lc_known_type}) {
3108 $known_db_type =
'core' if ! $known_db_type;
3110 my %get_adaptors_args = (
'-GROUP' => $known_db_type);
3111 $get_adaptors_args{
'-species'} = $known_species
if $known_species;
3114 sort { $a->dbc->host cmp $b->dbc->host || $a->dbc->port <=> $b->dbc->port }
3115 grep { $_->dbc->dbname !~ m{ \A ensembl_metadata | ncbi_taxonomy }msx }
3116 @{$self->get_all_DBAdaptors(%get_adaptors_args)};
3118 foreach my $dba (@dbas) {
3120 my $dba_adaptor_type = $group2adaptor{$dba->group()};
3121 if($dba_adaptor_type eq
'Bio::EnsEMBL::DBSQL::DBAdaptor') {
3122 @results = $self->_core_get_species_and_object_type($stable_id, $known_type, $dba);
3124 elsif($dba_adaptor_type eq
'Bio::EnsEMBL::Compara::DBSQL::DBAdaptor') {
3125 @results = $self->_compara_get_species_and_object_type($stable_id, $known_type, $dba);
3127 return @results
if scalar(@results) > 0;
3128 } ## end
foreach my $dba ( sort { $a...})
3132 } ## end sub get_species_and_object_type
3134 sub _lookup_db_get_species_and_object_type {
3135 my ($self, $stable_id, $known_type, $known_species, $known_db_type, $use_archive) = @_;
3138 my $stable_ids_dba = $self->get_DBAdaptor(
"multi",
"stable_ids", 1);
3140 my ($species, $type, $db_type) = $self->stable_id_lookup($stable_id, $known_type, $known_species, $known_db_type);
3142 if (!$species && $use_archive) {
3143 ($species, $type, $db_type) = $self->archive_id_lookup($stable_id, $known_type, $known_species, $known_db_type);
3144 $retired = 1
if $species;
3147 return ($species ,$type, $db_type, $retired);
3148 } ## end sub _lookup_db_get_species_and_object_type
3151 sub stable_id_lookup {
3152 my ($self, $stable_id, $known_type, $known_species, $known_db_type) = @_;
3154 my $stable_ids_dba = $self->get_DBAdaptor(
"multi",
"stable_ids", 1);
3156 my $statement =
'SELECT name, object_type, db_type FROM stable_id_lookup join species using(species_id) WHERE stable_id = ?';
3157 if ($known_species) {
3158 $statement .=
' AND name = ?';
3160 if ($known_db_type) {
3161 $statement .=
' AND db_type = ?';
3164 $statement .=
' AND object_type = ?';
3167 my $sth = $stable_ids_dba->dbc()->prepare($statement);
3168 $sth->bind_param(1, $stable_id, SQL_VARCHAR);
3169 my $param_count = 1;
3170 if ($known_species) {
3171 $known_species = $self->get_alias($known_species);
3173 $sth->bind_param($param_count, $known_species, SQL_VARCHAR);
3175 if ($known_db_type) {
3177 $sth->bind_param($param_count, $known_db_type, SQL_VARCHAR);
3181 $sth->bind_param($param_count, $known_type, SQL_VARCHAR);
3184 my ($species, $type, $db_type) = $sth->fetchrow_array();
3187 return ($species ,$type, $db_type);
3190 sub archive_id_lookup {
3191 my ($self, $stable_id, $known_type, $known_species, $known_db_type) = @_;
3193 my $stable_ids_dba = $self->get_DBAdaptor(
"multi",
"stable_ids", 1);
3195 my $archive_statement =
'SELECT name, object_type, db_type FROM archive_id_lookup join species using(species_id) WHERE archive_id = ?';
3196 if ($known_species) {
3197 $archive_statement .=
' AND name = ?';
3199 if ($known_db_type) {
3200 $archive_statement .=
' AND db_type = ?';
3203 $archive_statement .=
' AND object_type = ?';
3206 my $archive_sth = $stable_ids_dba->dbc()->prepare($archive_statement);
3207 $archive_sth->bind_param(1, $stable_id, SQL_VARCHAR);
3208 my $param_count = 1;
3209 if ($known_species) {
3210 $known_species = $self->get_alias($known_species);
3212 $archive_sth->bind_param($param_count, $known_species, SQL_VARCHAR);
3214 if ($known_db_type) {
3216 $archive_sth->bind_param($param_count, $known_db_type, SQL_VARCHAR);
3220 $archive_sth->bind_param($param_count, $known_type, SQL_VARCHAR);
3223 $archive_sth->execute();
3224 my ($species, $type, $db_type) = $archive_sth->fetchrow_array();
3225 $archive_sth->finish();
3227 return ($species ,$type, $db_type);
3231 # A level of abstraction because we need to test the stable_id as-is and then
3232 # try to chop off a version id if nothing is return, and try again
3234 sub _core_get_species_and_object_type {
3235 my ($self, $stable_id, $known_type, $dba) = @_;
3237 # Try looking up the species with the stable_is, as-is
3238 my @results = $self->_core_get_species_and_object_type_worker($stable_id, $known_type, $dba);
3242 } elsif(my $vindex = rindex($stable_id,
'.')) {
3243 return $self->_core_get_species_and_object_type_worker(substr($stable_id,0,$vindex), $known_type, $dba)
3244 if(substr($stable_id,$vindex+1) =~ /^\d+$/);
3251 # Loop over a known set of object types for a core DB until we find a hit
3252 sub _core_get_species_and_object_type_worker {
3253 my ($self, $stable_id, $known_type, $dba) = @_;
3254 my @types = defined $known_type ? ($known_type) : (
'Gene',
'Transcript',
'Translation',
'Exon',
'Operon',
'OperonTranscript');
3255 my ($species, $final_type, $final_db_type);
3256 foreach my $type (@types) {
3257 my $statement = sprintf $stable_id_stmts{lc $type}, $dba->dbc->dbname;
3258 my $sth = $dba->dbc()->prepare($statement);
3259 $sth->bind_param(1, $stable_id, SQL_VARCHAR);
3261 $species = $sth->fetchall_arrayref->[0][0];
3263 if(defined $species) {
3264 $final_type = $type;
3265 $final_db_type = $dba->group();
3269 $dba->dbc->disconnect_if_idle(); # always disconnect after lookup
3270 return ($species, $final_type, $final_db_type)
if defined $species;
3274 # A level of abstraction because we need to test the stable_id as-is and then
3275 # try to chop off a version id if nothing is return, and try again
3277 sub _compara_get_species_and_object_type {
3278 my ($self, $stable_id, $known_type, $dba) = @_;
3280 # Try looking up the species with the stable_is, as-is
3281 my @results = $self->_compara_get_species_and_object_type_worker($stable_id, $known_type, $dba);
3285 } elsif(my $vindex = rindex($stable_id,
'.')) {
3286 return $self->_compara_get_species_and_object_type_worker(substr($stable_id,0,$vindex), $known_type, $dba)
3287 if(substr($stable_id,$vindex+1) =~ /^\d+$/);
3294 # Loop over a known set of object types for a compara DB until we find a hit
3295 sub _compara_get_species_and_object_type_worker {
3296 my ($self, $stable_id, $known_type, $dba) = @_;
3297 my @types = defined $known_type ? ($known_type) : (
'GeneTree');
3298 my ($species, $final_type, $final_db_type);
3299 foreach my $type (@types) {
3300 my $statement = sprintf $compara_stable_id_stmts{lc $type}, $dba->dbc->dbname;
3301 my $sth = $dba->dbc()->prepare($statement);
3302 $sth->bind_param(1, $stable_id, SQL_VARCHAR);
3304 my $found = $sth->fetchall_arrayref->[0][0];
3306 if(defined $found) {
3307 $species = $dba->species();
3308 $final_type = $type;
3309 $final_db_type = $dba->group();
3313 $dba->dbc->disconnect_if_idle(); # always disconnect after lookup
3314 return ($species, $final_type, $final_db_type)
if defined $species;