EnsEMBL/Registry.pm untested

filter: Statementstmt 0.0% 0 / 1076 Branchbran 0.0% 0 / 544 Conditioncond 0.0% 0 / 238 MC/DCmcdc 0.0% 0 / 147 Subroutinesub 0.0% 0 / 71 Podpod 0.0% 0 / 51 totaltotal 0.0% 0 / 2127 CC 273 SCAR 77.9
CC
273
Cov
0%
load_registry_from_db
8556.0
load_all
992.0
version_check
240.0
CRAP 2424.3
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
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
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
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.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::Registry
34  
35 =head1 SYNOPSIS
36  
37 use Bio::EnsEMBL::Registry;
38  
39 my $registry = 'Bio::EnsEMBL::Registry';
40  
41 $registry->load_all("configuration_file");
42  
43 $gene_adaptor = $registry->get_adaptor( 'Human', 'Core', 'Gene' );
44  
45 =head1 DESCRIPTION
46  
47 All Adaptors are stored/registered using this module. This module should
48 then be used to get the adaptors needed.
49  
50 The registry can be loaded from a configuration file using the load_all
51 method.
52  
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.
57  
58 For the Web server ENSEMBL_REGISTRY should be set in SiteDefs.pm. This
59 will then be passed on to load_all.
60  
61  
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
64 databases from it.
65  
66 The four types of registries are for db adaptors, dba adaptors, dna
67 adaptors and the standard type.
68  
69 =head2 db
70  
71 These are registries for backwards compatibility and enable the
72 subroutines to add other adaptors to connections.
73  
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.
77  
78 So if before we had
79  
80 my $sfa = $self->adaptor()->db()->get_db_adaptor('blast');
81  
82 We now want to change this to
83  
84 my $sfa =
85 Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "blast" );
86  
87  
88 =head2 DBA
89  
90 These are the stores for the DBAdaptors
91  
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
94  
95 my $db = Bio::EnsEMBL::DBSQL::DBAdaptor->new(...);
96 my $exon_adaptor = $db->get_ExonAdaptor;
97  
98 we should now have just
99  
100 my $exon_adaptor =
101 Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "exon" );
102  
103  
104 =head2 DNA
105  
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
108 core database.
109  
110 ## set the est db to use the core for getting dna data.
111 # Bio::EnsEMBL::Utils::ConfigRegistry->dnadb_add( "Homo Sapiens",
112 # "core", "Homo Sapiens", "est" );
113  
114  
115 =head2 adaptors
116  
117 This is the registry for all the general types of adaptors like
118 GeneAdaptor, ExonAdaptor, Slice Adaptor etc.
119  
120 These are accessed by the get_adaptor subroutine i.e.
121  
122 my $exon_adaptor =
123 Bio::EnsEMBL::Registry->get_adaptor( "human", "core", "exon" );
124  
125 =head1 METHODS
126  
127 =cut
128  
129  
130  
131 package Bio::EnsEMBL::Registry;
132 $Bio::EnsEMBL::Registry::VERSION = '114.0.0';
133 use strict;
134 use warnings;
135  
136 our $NEW_EVAL = 0;
137  
138 use Bio::EnsEMBL::DBSQL::DBAdaptor;
139 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
140 use Bio::EnsEMBL::Utils::Exception qw( deprecate throw warning );
141 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
142 use Bio::EnsEMBL::Utils::ConfigRegistry;
143 use Bio::EnsEMBL::ApiVersion;
144 use Bio::EnsEMBL::Utils::URI qw/parse_uri/;
145  
146 use DBI qw(:sql_types);
147  
148 use Scalar::Util qw/blessed/;
149  
150 use vars qw(%registry_register);
151  
152 # This is a map from group names to Ensembl DB adaptors. Used by
153 # load_all() and reset_DBAdaptor().
154 my %group2adaptor = (
155 'compara' => 'Bio::EnsEMBL::Compara::DBSQL::DBAdaptor',
156 'core' => 'Bio::EnsEMBL::DBSQL::DBAdaptor',
157 'estgene' => 'Bio::EnsEMBL::DBSQL::DBAdaptor',
158 'funcgen' => 'Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor',
159 'gene2phenotype' => 'Bio::EnsEMBL::G2P::DBSQL::DBAdaptor',
160 'regulation' => 'Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor',
161 'hive' => 'Bio::EnsEMBL::Hive::DBSQL::DBAdaptor',
162 'metadata' => 'Bio::EnsEMBL::MetaData::DBSQL::MetaDataDBAdaptor',
163 'ontology' => 'Bio::EnsEMBL::DBSQL::OntologyDBAdaptor',
164 'otherfeatures' => 'Bio::EnsEMBL::DBSQL::DBAdaptor',
165 'pipeline' => 'Bio::EnsEMBL::Pipeline::DBSQL::DBAdaptor',
166 'production' => 'Bio::EnsEMBL::Production::DBSQL::DBAdaptor',
167 'stable_ids' => 'Bio::EnsEMBL::DBSQL::DBAdaptor',
168 'taxonomy' => 'Bio::EnsEMBL::Taxonomy::DBSQL::TaxonomyDBAdaptor',
169 'variation' => 'Bio::EnsEMBL::Variation::DBSQL::DBAdaptor',
170 'vega' => 'Bio::EnsEMBL::DBSQL::DBAdaptor',
171 'vega_update' => 'Bio::EnsEMBL::DBSQL::DBAdaptor',
172 );
173  
174  
175 =head2 load_all
176  
177 Will load the registry with the configuration file which is
178 obtained from the first in the following and in that order.
179  
180 1) If an argument is passed to this method, this is used as the
181 name of the configuration file to read.
182  
183 2) If the environment variable ENSEMBL_REGISTRY is set, this is
184 used as the name of the configuration file to read.
185  
186 3) If the file .ensembl_init exist in the home directory, it is
187 used as the configuration file.
188  
189 Arg [1] : (optional) string
190 Name of file to load the registry from.
191  
192 Arg [2] : (optional) integer
193 If not 0, will print out all information.
194  
195 Arg [3] : (optional) integer
196 If not 0, the database connection will not be
197 cleared, if 0 or if not set the database connections
198 will be cleared (this is the default).
199  
200 Arg [4]: (optional) boolean
201 This option will turn off caching for slice features,
202 so, every time a set of features is retrieved,
203 they will come from the database instead of the
204 cache. This option is only recommended for advanced
205 users, specially if you need to store and retrieve
206 features. It might reduce performance when querying
207 the database if not used properly. If in doubt, do
208 not use it or ask in the developer mailing list.
209  
210 Arg [5]: (optional) boolean
211 This option will make load_all() throw if the configuration file
212 is missing and cannot be guessed from the environment
213  
214 Example : Bio::EnsEMBL::Registry->load_all();
215 Returntype : Int count of the DBAdaptor instances which can be found in the
216 registry due to this method being called. Will never be negative
217 Exceptions : Throws if $throw_if_missing is set and ($config_file is missing
218 and cannot be guessed from the environment
219 Status : Stable
220  
221 =cut
222  
223 sub load_all {
224 my ($class, $config_file, $verbose, $no_clear, $no_cache, $throw_if_missing ) = @_;
225  
226 if ( !defined($config_file) ) {
227 if ( defined( $ENV{ENSEMBL_REGISTRY} ) ) {
228 if (-e $ENV{ENSEMBL_REGISTRY}) {
229 $config_file = $ENV{ENSEMBL_REGISTRY};
230 } else {
231 warning("\$ENV{ENSEMBL_REGISTRY} points to a file ('$ENV{ENSEMBL_REGISTRY}') that does not exist.\n");
232 }
233 } elsif ( defined( $ENV{HOME} ) ) {
234 if (-e ($ENV{HOME} . "/.ensembl_init")) {
235 $config_file = $ENV{HOME} . "/.ensembl_init";
236 }
237 }
238 if ($throw_if_missing and !defined($config_file) ) {
239 throw("No registry configuration to load, and no default could be guessed.\n");
240 }
241 } elsif ($throw_if_missing and !(-e $config_file)) {
242 throw(sprintf("Configuration file '%s' does not exist. Registry configuration not loaded.\n", $config_file ));
243 }
244  
245 $verbose ||= 0;
246 $no_clear ||= 0;
247 $no_cache ||= 0;
248
249 my $original_count = $class->get_DBAdaptor_count();
250  
251 if ( !defined($config_file) ) {
252 if ($verbose) {
253 print( STDERR
254 "No default registry configuration to load.\n" );
255 }
256 } elsif ( !-e $config_file ) {
257 if ($verbose) {
258 printf( STDERR "Configuration file '%s' does not exist. "
259 . "Registry configuration not loaded.\n",
260 $config_file );
261 }
262 } else {
263 if ( defined( $registry_register{'seen'} ) ) {
264 if ( !$no_clear ) {
265 if ($verbose) {
266 print( STDERR "Clearing previously loaded "
267 . "registry configuration\n" );
268 }
269 $class->clear();
270 }
271 }
272 $registry_register{'seen'} = 1;
273  
274 if ($verbose) {
275 printf( STDERR
276 "Loading registry configuration from '%s'.\n",
277 $config_file );
278 }
279  
280 my $cfg;
281  
282 my $test_eval = eval { require Config::IniFiles };
283  
284 if ($@ or (!$test_eval)) {
285 # The user does not have the 'Config::IniFiles' module.
286 if ($verbose) {
287 print( STDERR "No Config::IniFiles module found, "
288 . "assuming this is not an ini-file\n" );
289 }
290 # If the configuration file *is* an ini-file, we can expect a
291 # load of compilation errors from the next eval...
292 } else {
293 # The user has the 'Config::IniFiles' module installed. See
294 # if this is an ini-file or not...
295 $cfg = Config::IniFiles->new( -file => $config_file );
296 }
297  
298 if ( defined $cfg ) {
299 my %default_adaptor_args = ();
300  
301 if ( $cfg->SectionExists('default') ) {
302 # The 'default' section is special. It contain default
303 # values that should be implicit to all other section in
304 # this configuration file. Aliases are added if there
305 # is also a 'species' setting.
306  
307 my $alias = $cfg->val( 'default', 'alias' );
308 $cfg->delval( 'default', 'alias' );
309  
310 my $species = $cfg->val( 'default', 'species' );
311  
312 if ( defined($alias) && defined($species) ) {
313 Bio::EnsEMBL::Utils::ConfigRegistry->add_alias(
314 -species => $species,
315 -alias => [ split( /\n/, $alias ) ]
316 );
317 }
318  
319 %default_adaptor_args =
320 map { '-' . $_ => $cfg->val( 'default', $_ ) }
321 $cfg->Parameters('default');
322 }
323  
324 foreach my $section ( $cfg->Sections() ) {
325 if ( $section eq 'default' )
326 { # We have already done the 'default' section.
327 next;
328 }
329  
330 my $group = $cfg->val( $section, 'group' )
331 || $cfg->val( 'default', 'group' );
332  
333 if ( !defined($group) ) {
334 printf( STDERR "Key 'group' is undefined "
335 . "for configuration section '%s', "
336 . "skipping this section.\n",
337 $section );
338 next;
339 }
340  
341 my $adaptor = $group2adaptor{ lc($group) };
342 if ( !defined($adaptor) ) {
343 printf( STDERR "Unknown group '%s' "
344 . "for configuration section '%s', "
345 . "skipping this section.\n",
346 $group, $section );
347 next;
348 }
349  
350 # Handle aliases. A section must have both an 'alias'
351 # setting and a 'species' setting for aliases to be
352 # added. The 'species' setting might be inherited from
353 # the 'default' section.
354  
355 my $alias = $cfg->val( $section, 'alias' );
356 $cfg->delval( $section, 'alias' );
357  
358 my $species = $cfg->val( $section, 'species' )
359 || $cfg->val( 'default', 'species' );
360  
361 if ( defined($alias) && defined($species) ) {
362 Bio::EnsEMBL::Utils::ConfigRegistry->add_alias(
363 -species => $species,
364 -alias => [ split( /\n/, $alias ) ]
365 );
366 }
367  
368 # Fill in the adaptor initialization arguments.
369 # We trust the user to provide sensible key-value pairs.
370 my %adaptor_args = %default_adaptor_args;
371 foreach my $parameter ( $cfg->Parameters($section) ) {
372 $adaptor_args{ '-' . $parameter } =
373 $cfg->val( $section, $parameter );
374  
375 # when set, do not use the feature cache in the
376 # different adaptors
377 if ($no_cache) {
378 $adaptor_args{'-no_cache'} = 1;
379 }
380 }
381 if ($verbose) {
382 printf( "Configuring adaptor '%s' "
383 . "for configuration section '%s'...\n",
384 $adaptor, $section );
385 }
386  
387 my $test_eval = eval "require $adaptor"; ## no critic
388 if ($@ or (!$test_eval)) { die($@) }
389  
390 $adaptor->new(%adaptor_args);
391  
392 } ## end foreach my $section ( $cfg->Sections...
393 } else {
394 # This is probably no ini-file but an old style piece
395 # of configuration written in Perl. We need to try to
396 # require() it.
397  
398 my $test_eval;
399 if($NEW_EVAL) {
400 require Bio::EnsEMBL::Utils::IO;
401 my $contents = Bio::EnsEMBL::Utils::IO::slurp($config_file);
402 $test_eval = eval $contents; ## no critic
403 }
404 else {
405 $test_eval = eval { require($config_file) };
406 # To make the web code avoid doing this again we delete first
407 delete $INC{$config_file};
408 }
409
410 #Now raise the exception just in case something above is
411 #catching this
412 if ($@ or (!$test_eval)) { die($@) }
413  
414 }
415 } ## end else [ if ( !defined($config_file...
416
417 my $count = $class->get_DBAdaptor_count() - $original_count;
418 return $count >= 0 ? $count : 0;
419 } ## end sub load_all
420  
421 =head2 clear
422  
423 Will clear the registry and disconnect from all databases.
424  
425 Example : Bio::EnsEMBL::Registry->clear();
426 Returntype : none
427 Exceptions : none
428 Status : Stable
429  
430 =cut
431  
432 sub clear{
433 my ($self);
434
435 foreach my $dba (@{$registry_register{'_DBA'}}){
436 if($dba->dbc->connected){
437 $dba->dbc->db_handle->disconnect();
438 }
439 }
440 %registry_register = ();
441 return;
442 }
443  
444 #
445 # db adaptors. (for backwards compatibility)
446 #
447  
448 =head2 add_db
449  
450 Arg [1] : db (DBAdaptor) to add adaptor to.
451 Arg [2] : name of the name to add the adaptor to in the registry.
452 Arg [3] : The adaptor to be added to the registry.
453 Example : Bio::EnsEMBL::Registry->add_db($db, "lite", $dba);
454 Returntype : none
455 Exceptions : none
456 Status : At Risk.
457 : This is here for backwards compatibility only and may
458 : be removed eventually. Solution is to make sure the
459 : db and the adaptor have the same species and the call
460 : is then no longer needed.
461  
462 =cut
463  
464 sub add_db {
465 my ( $class, $db, $name, $adap ) = @_;
466 #No warnings brought in due to some overzelous webcode triggering a lot of warnings.
467 no warnings 'uninitialized';
468 if ( lc( $db->species() ) ne lc( $adap->species ) ) {
469 $registry_register{_SPECIES}{ lc( $db->species() ) }
470 { lc( $db->group() ) }{'_special'}{ lc($name) } = $adap;
471 }
472 return;
473 }
474  
475 =head2 remove_db
476  
477 Arg [1] : db (DBAdaptor) to remove adaptor from.
478 Arg [2] : name to remove the adaptor from in the registry.
479 Example : my $db = Bio::EnsEMBL::Registry->remove_db($db, "lite");
480 Returntype : adaptor
481 Exceptions : none
482 Status : At Risk.
483 : This is here for backwards compatibility only and may
484 : be removed eventually. Solution is to make sure the
485 : db and the adaptor have the same species and the call
486 : is then no longer needed.
487  
488 =cut
489  
490 sub remove_db {
491 my ( $class, $db, $name ) = @_;
492  
493 my $ret =
494 $registry_register{_SPECIES}{ lc( $db->species() ) }
495 { lc( $db->group() ) }{'_special'}{ lc($name) };
496  
497 $registry_register{_SPECIES}{ lc( $db->species() ) }
498 { lc( $db->group() ) }{'_special'}{ lc($name) } = undef;
499  
500 return $ret;
501 }
502  
503 =head2 get_db
504  
505 Arg [1] : db (DBAdaptor) to get adaptor from.
506 Arg [2] : name to get the adaptor for in the registry.
507 Example : my $db = Bio::EnsEMBL::Registry->get_db("Human", "core", "lite");
508 Returntype : adaptor
509 Exceptions : See get_DBAdaptor()
510 Status : At Risk.
511 : This is here for backwards compatibility only and may
512 : be removed eventually. Solution is to make sure the
513 : db and the adaptor have the same species then call
514 : get_DBAdaptor instead.
515  
516 =cut
517  
518 sub get_db {
519 my ( $class, $db, $name ) = @_;
520  
521 my $ret = Bio::EnsEMBL::Registry->get_DBAdaptor( lc( $db->species ),
522 lc($name) );
523  
524 if ( defined($ret) ) { return $ret }
525  
526 return $registry_register{_SPECIES}{ lc( $db->species() ) }
527 { lc( $db->group() ) }{'_special'}{ lc($name) };
528 }
529  
530 =head2 get_all_db_adaptors
531  
532 Arg [1] : db (DBAdaptor) to get all the adaptors from.
533 Example : my $db = Bio::EnsEMBL::Registry->get_all_db_adaptors($db);
534 Returntype : adaptor
535 Exceptions : none
536 Status : At Risk.
537 : This is here for backwards compatibility only and
538 : may be removed eventually. Solution is to make
539 : sure the dbs all have the same species then call
540 : get_all_DBAdaptors(-species => "human");
541  
542  
543 =cut
544  
545 sub get_all_db_adaptors {
546 my ( $class, $db ) = @_;
547 my %ret = ();
548  
549 # we now also want to add all the DBAdaptors for the same species.
550 # as add_db_adaptor does not add if it is from the same species.
551  
552 foreach my $dba ( @{ $registry_register{'_DBA'} } ) {
553 if ( lc( $dba->species() ) eq lc( $db->species() ) ) {
554 $ret{ $dba->group() } = $dba;
555 }
556 }
557  
558 foreach my $key (
559 keys %{
560 $registry_register{_SPECIES}
561 { $class->get_alias( $db->species() ) }{ lc( $db->group() ) }
562 {'_special'} } )
563 {
564 $ret{$key} =
565 $registry_register{_SPECIES}
566 { $class->get_alias( $db->species() ) }{ lc( $db->group() ) }
567 {'_special'}{$key};
568 }
569  
570 return \%ret;
571 } ## end sub get_all_db_adaptors
572  
573  
574 #
575 # DBAdaptors
576 #
577  
578 =head2 add_DBAdaptor
579  
580 Arg [1] : name of the species to add the adaptor to in the registry.
581 Arg [2] : name of the group to add the adaptor to in the registry.
582 Arg [3] : DBAdaptor to be added to the registry.
583 Example : Bio::EnsEMBL::Registry->add_DBAdaptor("Human", "core", $dba);
584 Returntype : none
585 Exceptions : none
586 caller : internal
587 Status : Stable
588  
589 =cut
590  
591 sub add_DBAdaptor {
592 my ( $class, $species, $group, $adap ) = @_;
593  
594 if ( !defined($species) ) {
595 throw('Species not defined.');
596 }
597  
598 if ( !( $class->alias_exists($species) ) ) {
599 $class->add_alias( $species, $species );
600 }
601  
602 $species = $class->get_alias($species);
603  
604 $registry_register{_SPECIES}{$species}{ lc($group) }{'_DB'} = $adap;
605  
606 if ( !defined( $registry_register{'_DBA'} ) ) {
607 $registry_register{'_DBA'} = [$adap];
608 } else {
609 push( @{ $registry_register{'_DBA'} }, $adap );
610 }
611 return;
612 }
613  
614  
615  
616 =head2 get_DBAdaptor
617  
618 Arg [1] : name of the species to get the adaptor for in the registry.
619 Arg [2] : name of the group to get the adaptor for in the registry.
620 Arg [3] : if set will not give warnings when looking for alias.
621 Example : $dba = Bio::EnsEMBL::Registry->get_DBAdaptor("Human", "core");
622 Returntype : DBAdaptor
623 Exceptions : If $species is not defined and if no valid internal name
624 could be found for $species. If thrown check your API and DB
625 version
626 Status : Stable
627  
628 =cut
629  
630 sub get_DBAdaptor {
631 my ( $class, $species, $group, $no_alias_check ) = @_;
632  
633 if ( !defined($species) ) {
634 throw('Species not defined.');
635 }
636  
637 my $ispecies = $class->get_alias( $species, $no_alias_check );
638  
639 if ($group eq 'regulation') { $group = 'funcgen'; }
640  
641 if ( !defined($ispecies) ) {
642 if(! $no_alias_check) {
643 throw("Can not find internal name for species '$species'");
644 }
645 }
646 else { $species = $ispecies }
647  
648 return $registry_register{_SPECIES}{$species}{ lc($group) }{'_DB'};
649 }
650  
651 =head2 get_all_DBAdaptors
652  
653 Arg [SPECIES]: (optional) string
654 species name to get adaptors for
655 Arg [GROUP] : (optional) string
656 group name to get adaptors for
657 Example :
658 @dba =
659 @{ Bio::EnsEMBL::Registry->get_all_DBAdaptors() };
660  
661 @human_dbas =
662 @{ Bio::EnsEMBL::Registry->get_all_DBAdaptors(
663 -species => 'human'
664 ) };
665  
666 Returntype : list of DBAdaptors
667 Exceptions : none
668 Status : Stable
669  
670 =cut
671  
672 sub get_all_DBAdaptors {
673 my ( $class, @args ) = @_;
674  
675 my ( $species, $group ) = rearrange( [qw(SPECIES GROUP)], @args );
676  
677 if ( !defined($species) && !defined($group) ) {
678 return $registry_register{'_DBA'} || [];
679 }
680  
681 if ( defined($species) ) {
682 $species = $class->get_alias($species);
683 return [] unless $species;
684 }
685  
686 my @ret;
687 foreach my $dba ( @{ $registry_register{'_DBA'} } ) {
688 if ( ( !defined($species) || lc($species) eq lc( $dba->species() ) )
689 && ( !defined($group) || lc($group) eq lc( $dba->group() ) ) )
690 {
691 push( @ret, $dba );
692 }
693 }
694  
695 return \@ret;
696 }
697  
698 =head2 get_all_DBAdaptors_by_connection
699  
700 Arg [1] : DBConnection used to find DBAdaptors
701 Returntype : reference to list of DBAdaptors
702 Exceptions : none
703 Example : @dba = @{ Bio::EnsEMBL::Registry
704 ->get_all_DBAdaptors_by_connection($dbc) };
705 Status : Stable
706  
707 =cut
708  
709 sub get_all_DBAdaptors_by_connection {
710 my ( $self, $dbc_orig ) = @_;
711  
712 my @return;
713  
714 foreach my $dba ( @{ $registry_register{'_DBA'} } ) {
715 my $dbc = $dba->dbc();
716  
717 if ( defined($dbc)
718 && $dbc->can('equals')
719 && $dbc->equals($dbc_orig) )
720 {
721 push( @return, $dba );
722 }
723 }
724  
725 return \@return;
726 }
727  
728 =head2 get_all_DBAdaptors_by_dbname
729  
730 Arg [1] : string, name of database
731 Returntype : reference to list of DBAdaptors
732 Exceptions : none
733 Example : @dba = @{ Bio::EnsEMBL::Registry
734 ->get_all_DBAdaptors_by_dbname($dbname) };
735 Status : Stable
736  
737 =cut
738  
739 sub get_all_DBAdaptors_by_dbname {
740 my ( $self, $dbname ) = @_;
741  
742 my @return;
743  
744 foreach my $dba ( @{ $registry_register{'_DBA'} } ) {
745 my $dbc = $dba->dbc();
746  
747 if ( defined($dbc) && $dbc->dbname() eq $dbname ) {
748 push( @return, $dba );
749 }
750 }
751  
752 return \@return;
753 }
754  
755 =head2 remove_DBAdaptor
756  
757 Arg [1] : name of the species to get the adaptor for in the registry.
758 Arg [2] : name of the group to get the adaptor for in the registry.
759 Example : $dba = Bio::EnsEMBL::Registry->remove_DBAdaptor("Human", "core");
760 Returntype : none
761 Exceptions : none
762 Status : At risk
763  
764 =cut
765  
766 sub remove_DBAdaptor {
767 my ( $class, $species, $group ) = @_;
768  
769 $species = $class->get_alias($species);
770  
771 delete $registry_register{_SPECIES}{$species}{$group};
772 # This will remove the DBAdaptor and all the other adaptors
773  
774 # Now remove if from the _DBA array
775 my $index;
776  
777 foreach my $i ( 0 .. $#{ $registry_register{'_DBA'} } ) {
778 my $dba = $registry_register{'_DBA'}->[$i];
779  
780 if ( ( $dba->species eq $species )
781 && $dba->group eq $group )
782 {
783 $index = $i;
784 last;
785 }
786 }
787  
788 # Now remove from _DBA cache
789 if ( defined($index) ) {
790 splice( @{ $registry_register{'_DBA'} }, $index, 1 );
791 }
792  
793 return;
794 } ## end sub remove_DBAdaptor
795  
796  
797  
798 =head2 reset_DBAdaptor
799  
800 Arg [1]: string - species e.g. homo_sapiens
801 Arg [2]: string - DB group e.g. core
802 Arg [3]: string - new dbname
803 Args [4-7]: string - optional DB parameters, defaults to current db params if omitted
804 Arg [8]: hashref - Hash ref of additional parameters e.g. eFG dnadb params for auto selecting dnadb
805 Usage : $reg->reset_registry_db( 'homo_sapiens', 'core',
806 'homo_sapiens_core_37_35j' );
807 Description: Resets a DB within the registry.
808 Exceptions: Throws if mandatory params not supplied
809 Throws if species name is not already seen by the registry
810 Throws if no current DB for species/group available
811 Status : At risk
812  
813 =cut
814  
815 sub reset_DBAdaptor {
816 my (
817 $self, $species, $group, $dbname, $host,
818 $port, $user, $pass, $params
819 ) = @_;
820  
821 # Check mandatory params
822 if ( !( defined $species && defined $group && defined $dbname ) ) {
823 throw(
824 'Must provide at least a species, group, and dbname parameter '
825 . 'to redefine a DB in the registry' );
826 }
827  
828 # Validate species here
829 my $alias = $self->get_alias($species);
830 throw("Could not find registry alias for species:\t$species")
831 if ( !defined $alias );
832  
833 # Get all current defaults if not defined
834  
835 my $db = $self->get_DBAdaptor( $alias, $group );
836 my $class;
837  
838 if ($db) {
839 $class = ref($db);
840 $host ||= $db->dbc->host;
841 $port ||= $db->dbc->port;
842 $user ||= $db->dbc->username;
843 $pass ||= $db->dbc->password;
844 } else {
845 #Now we need to test mandatory params
846 $class = $group2adaptor{ lc($group) };
847  
848 if ( !( $host && $user ) ) {
849 throw("No comparable $alias $group DB present in Registry. "
850 . "You must pass at least a dbhost and dbuser" );
851 }
852 }
853  
854 $self->remove_DBAdaptor( $alias, $group );
855  
856 # ConfigRegistry should automatically add this to the Registry
857 $db = $class->new(
858 -user => $user,
859 -host => $host,
860 -port => $port,
861 -pass => $pass,
862 -dbname => $dbname,
863 -species => $alias,
864 -group => $group,
865 %{$params} );
866  
867 return $db;
868 } ## end sub reset_DBAdaptor
869  
870  
871 #
872 # DNA Adaptors
873 #
874  
875 =head2 add_DNAAdaptor
876  
877 Arg [1] : name of the species to add the adaptor to in the registry.
878 Arg [2] : name of the group to add the adaptor to in the registry.
879 Arg [3] : name of the species to get the dna from
880 Arg [4] : name of the group to get the dna from
881 Example : Bio::EnsEMBL::Registry->add_DNAAdaptor("Human", "estgene", "Human", "core");
882 Returntype : none
883 Exceptions : none
884 Status : Stable
885  
886 =cut
887  
888 sub add_DNAAdaptor {
889 my ( $class, $species, $group, $dnadb_species, $dnadb_group ) = @_;
890  
891 $species = $class->get_alias($species);
892 $dnadb_species = $class->get_alias($dnadb_species);
893 if ( $dnadb_group->isa('Bio::EnsEMBL::DBSQL::DBAdaptor') ) {
894 deprecated("");
895 } else {
896 $registry_register{_SPECIES}{$species}{ lc($group) }{'_DNA'} =
897 $dnadb_group;
898 $registry_register{_SPECIES}{$species}{ lc($group) }{'_DNA2'} =
899 $dnadb_species;
900 }
901 return;
902 }
903  
904 =head2 get_DNAAdaptor
905  
906 Arg [1] : name of the species to get the adaptor for in the registry.
907 Arg [2] : name of the group to get the adaptor for in the registry.
908 Example : $dnaAdap = Bio::EnsEMBL::Registry->get_DNAAdaptor("Human", "core");
909 Returntype : adaptor
910 Exceptions : none
911 Status : Stable
912  
913 =cut
914  
915 sub get_DNAAdaptor {
916 my ( $class, $species, $group ) = @_;
917  
918 $species = $class->get_alias($species);
919 my $new_group =
920 $registry_register{_SPECIES}{$species}{ lc($group) }{'_DNA'};
921 my $new_species =
922 $registry_register{_SPECIES}{$species}{ lc($group) }{'_DNA2'};
923  
924 if ( defined $new_group ) {
925 return $class->get_DBAdaptor( $new_species, $new_group );
926 }
927  
928 return;
929 }
930  
931 #
932 # General Adaptors
933 #
934  
935 =head2 add_adaptor
936  
937 Arg [1] : name of the species to add the adaptor to in the registry.
938 Arg [2] : name of the group to add the adaptor to in the registry.
939 Arg [3] : name of the type to add the adaptor to in the registry.
940 Arg [4] : The DBAdaptor to be added to the registry.
941 Arg [5] : (optional) Set to allow overwrites of existing adaptors.
942 Example : Bio::EnsEMBL::Registry->add_adaptor("Human", "core", "Gene", $adap);
943 Returntype : none
944 Exceptions : none
945 Caller : internal
946 Status : Stable
947  
948 =cut
949  
950 sub add_adaptor {
951 my ( $class, $species, $group, $type, $adap, $reset ) = @_;
952  
953 $species = $class->get_alias($species);
954 my $lc_group = lc($group);
955 my $lc_type = lc($type);
956  
957 # Since the adaptors are not stored initially, only their class paths
958 # when the adaptors are obtained, we need to store these instead. It
959 # is not necessarily an error if the registry is overwritten without
960 # the reset set but it is an indication that we are overwriting a
961 # database which should be a warning for now
962  
963 if ( defined($reset) )
964 { # JUST RESET THE HASH VALUE NO MORE PROCESSING NEEDED
965 $registry_register{_SPECIES}{$species}{ $lc_group }{ $lc_type } = $adap;
966 return;
967 }
968  
969 if (
970 defined(
971 $registry_register{_SPECIES}{$species}{ $lc_group }{ $lc_type }
972 ) )
973 {
974 # print STDERR (
975 # "Overwriting Adaptor in Registry for $species $group $type\n");
976 $registry_register{_SPECIES}{$species}{ $lc_group }{ $lc_type } = $adap;
977 return;
978 }
979 $registry_register{_SPECIES}{$species}{ $lc_group }{ $lc_type } = $adap;
980  
981 if ( !defined( $registry_register{_SPECIES}{$species}{'list'} ) ) {
982 $registry_register{_SPECIES}{$species}{'list'} = [$type];
983 }
984 else {
985 push( @{ $registry_register{_SPECIES}{$species}{'list'} }, $type );
986 }
987  
988 return;
989 } ## end sub add_adaptor
990  
991  
992 =head2 add_switchable_adaptor
993  
994 Arg [1] : String name of the species to add its switchable adaptor into the registry
995 Arg [2] : String name of the group to add its switchable adaptor into the registry
996 Arg [3] : String name of the type to add its switchable adaptor into the registry
997 Arg [4] : Reference switchable adaptor to insert
998 Arg [5] : Boolean override any existing switchable adaptor
999 Example : Bio::EnsEMBL::Registry->add_switchable_adaptor("Human", "core", "Gene", $my_other_source);
1000 Returntype : None
1001 Exceptions : Thrown if a valid internal name cannot be found for the given
1002 name. If thrown check your API and DB version. Also thrown if
1003 no type, group or switchable adaptor instance was given
1004  
1005 =cut
1006  
1007 sub add_switchable_adaptor {
1008 my ($class, $species, $group, $adaptor_type, $instance, $override) = @_;
1009
1010 my $ispecies = $class->get_alias($species);
1011 throw "Cannot decode given species ${species} to an internal registry name" if ! $species;
1012 throw "No group given" if ! $group;
1013 throw "No adaptor type given" if ! $adaptor_type;
1014 throw "No switchable adaptor given" if ! $instance;
1015 throw "Switchable adaptor was not a blessed reference" if ! blessed($instance);
1016  
1017 $group = lc($group);
1018 $adaptor_type = lc($adaptor_type);
1019 if($override) {
1020 $registry_register{_SWITCHABLE}{$species}{$group}{$adaptor_type} = $instance;
1021 return;
1022 }
1023  
1024 if(exists $registry_register{_SWITCHABLE}{$species}{$group}{$adaptor_type}) {
1025 my $existing_ref = ref($registry_register{_SWITCHABLE}{$species}{$group}{$adaptor_type});
1026 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";
1027 }
1028  
1029 $registry_register{_SWITCHABLE}{$species}{$group}{$adaptor_type} = $instance;
1030 return;
1031 }
1032  
1033 =head2 has_switchable_adaptor
1034  
1035 Arg [1] : String name of the species to add its switchable adaptor into the registry
1036 Arg [2] : String name of the group to add its switchable adaptor into the registry
1037 Arg [3] : String name of the type to add its switchable adaptor into the registry
1038 Example : Bio::EnsEMBL::Registry->has_switchable_adaptor("Human", "core", "Gene");
1039 Returntype : Boolean indicating if a switchable adaptor is available for your submitted combination
1040 Exceptions : Thrown if a valid internal name cannot be found for the given
1041 name. If thrown check your API and DB version. Also thrown if
1042 no type, group or switchable adaptor instance was given
1043  
1044 =cut
1045  
1046 sub has_switchable_adaptor {
1047 my ($class, $species, $group, $adaptor_type) = @_;
1048
1049 my $ispecies = $class->get_alias($species);
1050 throw "Cannot decode given species ${species} to an internal registry name" if ! $species;
1051 throw "No group given" if ! $group;
1052 throw "No adaptor type given" if ! $adaptor_type;
1053  
1054 $group = lc($group);
1055 $adaptor_type = lc($adaptor_type);
1056 return (defined $registry_register{_SWITCHABLE}{$species}{$group}{$adaptor_type}) ? 1 : 0;
1057 }
1058  
1059 =head2 remove_switchable_adaptor
1060  
1061 Arg [1] : name of the species to remove its switchable adaptor from the registry
1062 Arg [2] : name of the group to remove its switchable adaptor from the registry
1063 Arg [3] : name of the type to remove its switchable adaptor from the registry
1064 Example : $adap = Bio::EnsEMBL::Registry->remove_switchable_adaptor("Human", "core", "Gene");
1065 Returntype : The removed adaptor if one was removed. Otherwise undef
1066 Exceptions : Thrown if a valid internal name cannot be found for the given
1067 name. If thrown check your API and DB version. Also thrown if
1068 no type or group was given
1069  
1070 =cut
1071  
1072 sub remove_switchable_adaptor {
1073 my ($class, $species, $group, $adaptor_type) = @_;
1074 my $ispecies = $class->get_alias($species);
1075 throw "Cannot decode given species ${species} to an internal registry name" if ! $species;
1076 throw "No group given" if ! $group;
1077 throw "No adaptor type given" if ! $adaptor_type;
1078  
1079 $group = lc($group);
1080 $adaptor_type = lc($adaptor_type);
1081 if(defined $registry_register{_SWITCHABLE}{$ispecies}{$group}{$adaptor_type}) {
1082 my $adaptor = $registry_register{_SWITCHABLE}{$ispecies}{$group}{$adaptor_type};
1083 delete $registry_register{_SWITCHABLE}{$ispecies}{$group}{$adaptor_type};
1084 return $adaptor;
1085 }
1086 return;
1087 }
1088  
1089 =head2 get_adaptor
1090  
1091 Arg [1] : name of the species to add the adaptor to in the registry.
1092 Arg [2] : name of the group to add the adaptor to in the registry.
1093 Arg [3] : name of the type to add the adaptor to in the registry.
1094 Example : $adap = Bio::EnsEMBL::Registry->get_adaptor("Human", "core", "Gene");
1095 Description : Finds and returns the specified adaptor. This method will also check
1096 if the species, group and adaptor combination satisfy a DNADB condition
1097 (and will return that DNADB's implementation). Also we check for
1098 any available switchable adaptors and will return that if available.
1099 Returntype : adaptor
1100 Exceptions : Thrown if a valid internal name cannot be found for the given
1101 name. If thrown check your API and DB version. Also thrown if
1102 no type or group was given
1103 Status : Stable
1104  
1105 =cut
1106  
1107 sub get_adaptor {
1108 my ( $class, $species, $group, $type ) = @_;
1109  
1110 my $ispecies = $class->get_alias($species);
1111  
1112 if ( !defined($ispecies) ) {
1113 throw("Can not find internal name for species '$species'");
1114 }
1115 else { $species = $ispecies }
1116
1117 throw 'No adaptor group given' if ! defined $group;
1118 throw 'No adaptor type given' if ! defined $type;
1119  
1120 $group = lc($group);
1121 my $lc_type = lc($type);
1122
1123  
1124 if($type =~ /Adaptor$/i) {
1125 warning("Detected additional Adaptor string in given the type '$type'. Removing it to avoid possible issues. Alter your type to stop this message");
1126 $type =~ s/Adaptor$//i;
1127 }
1128  
1129 # For historical reasons, allow use of group 'regulation' to refer to
1130 # group 'funcgen'.
1131 if ( $group eq 'regulation' ) { $group = 'funcgen' }
1132  
1133 my %dnadb_adaptors = (
1134 'sequence' => 1,
1135 'assemblymapper' => 1,
1136 'karyotypeband' => 1,
1137 'repeatfeature' => 1,
1138 'coordsystem' => 1,
1139 'assemblyexceptionfeature' => 1
1140 );
1141  
1142 #Before looking for DNA adaptors we need to see if we have a switchable adaptor since they take preference
1143 if(defined $registry_register{_SWITCHABLE}{$species}{$group}{$lc_type}) {
1144 return $registry_register{_SWITCHABLE}{$species}{$group}{$lc_type};
1145 }
1146  
1147 # Look for a possible DNADB group alongside the species hash
1148 my $dnadb_group = $registry_register{_SPECIES}{$species}{ $group }{'_DNA'};
1149  
1150 # If we found one & this is an adaptor we should be replaced by a DNADB then
1151 # look up the species to use and replace the current group with the DNADB group
1152 # (groups are held in _DNA, species are in _DNA2)
1153 if ( defined($dnadb_group) && defined( $dnadb_adaptors{ $lc_type } ) ) {
1154 $species = $registry_register{_SPECIES}{$species}{ $group }{'_DNA2'};
1155 $group = $dnadb_group;
1156  
1157 # Once we have switched to the possibility of a DNADB call now check again for
1158 # a switchable adaptor
1159 if(defined $registry_register{_SWITCHABLE}{$species}{$group}{$lc_type}) {
1160 return $registry_register{_SWITCHABLE}{$species}{$group}{$lc_type};
1161 }
1162 }
1163  
1164 # No switchable adaptor? Ok then continue with the normal logic
1165 my $ret = $registry_register{_SPECIES}{$species}{ $group }{ $lc_type };
1166  
1167 if ( !defined($ret) ) { return }
1168 if ( ref($ret) ) { return $ret }
1169  
1170 # Not instantiated yet
1171  
1172 my $dba = $registry_register{_SPECIES}{$species}{ $group }{'_DB'};
1173 my $module = $ret;
1174  
1175 my $test_eval = eval "require $module"; ## no critic
1176 if ($@ or (!$test_eval)) {
1177 warning("'$module' cannot be found.\nException $@\n");
1178 return;
1179 }
1180  
1181 if (
1182 !defined(
1183 $registry_register{_SPECIES}{$species}{ $group }{'CHECKED'} )
1184 )
1185 {
1186 $registry_register{_SPECIES}{$species}{ $group }{'CHECKED'} = 1;
1187 $class->version_check($dba);
1188 }
1189  
1190 my $adap = "$module"->new($dba);
1191 Bio::EnsEMBL::Registry->add_adaptor( $species, $group, $type, $adap,
1192 'reset' );
1193 $ret = $adap;
1194  
1195 return $ret;
1196 } ## end sub get_adaptor
1197  
1198 =head2 get_all_adaptors
1199  
1200 Arg [SPECIES] : (optional) string
1201 species name to get adaptors for
1202 Arg [GROUP] : (optional) string
1203 group name to get adaptors for
1204 Arg [TYPE] : (optional) string
1205 type to get adaptors for
1206 Example : @adaps = @{Bio::EnsEMBL::Registry->get_all_adaptors()};
1207 Returntype : ref to list of adaptors
1208 Exceptions : none
1209 Status : Stable
1210  
1211 =cut
1212  
1213 sub get_all_adaptors{
1214 my ($class,@args)= @_;
1215 my ($species, $group, $type);
1216 my @ret=();
1217 my (%species_hash, %group_hash, %type_hash);
1218  
1219  
1220 if(@args == 1){ # Old species only one parameter
1221 warn("-SPECIES argument should now be used to get species adaptors");
1222 $species = $args[0];
1223 }
1224 else{
1225 # new style -SPECIES, -GROUP, -TYPE
1226 ($species, $group, $type) =
1227 rearrange([qw(SPECIES GROUP TYPE)], @args);
1228 }
1229  
1230 if(defined($species)){
1231 $species_hash{$species} = 1;
1232 }
1233 else{
1234 # get list of species
1235 foreach my $dba (@{$registry_register{'_DBA'}}){
1236 $species_hash{lc($dba->species())} = 1;
1237 }
1238 }
1239 if(defined($group)){
1240 $group_hash{$group} = 1;
1241 }
1242 else{
1243 foreach my $dba (@{$registry_register{'_DBA'}}){
1244 $group_hash{lc($dba->group())} = 1;
1245 }
1246 }
1247  
1248 if ( defined($type) ) {
1249 $type_hash{$type} = 1;
1250 } else {
1251 foreach my $dba ( @{ $registry_register{'_DBA'} } ) {
1252 foreach my $ty (
1253 @{ $registry_register{_SPECIES}{ lc( $dba->species ) }{'list'} }
1254 )
1255 {
1256 $type_hash{ lc($ty) } = 1;
1257 }
1258 }
1259 }
1260  
1261 ### NOW NEED TO INSTANTIATE BY CALLING get_adaptor
1262 foreach my $sp ( keys %species_hash ) {
1263 foreach my $gr ( keys %group_hash ) {
1264 foreach my $ty ( keys %type_hash ) {
1265 my $temp = $class->get_adaptor( $sp, $gr, $ty );
1266 if ( defined($temp) ) {
1267 push @ret, $temp;
1268 }
1269 }
1270 }
1271 }
1272  
1273 return (\@ret);
1274 }
1275  
1276  
1277 =head2 add_alias
1278  
1279 Arg [1] : name of the species to add alias for
1280 Arg [2] : name of the alias
1281 Example : Bio::EnsEMBL::Registry->add_alias("Homo Sapiens","Human");
1282 Description: add alternative name for the species.
1283 Returntype : none
1284 Exceptions : none
1285 Status : Stable
1286  
1287 =cut
1288  
1289 sub add_alias{
1290 my ($class, $species,$key) = @_;
1291  
1292 $registry_register{'_ALIAS'}{lc($key)} = lc($species);
1293 return;
1294 }
1295  
1296 =head2 remove_alias
1297  
1298 Arg [1] : name of the species to remove alias for
1299 Arg [2] : name of the alias
1300 Example : Bio::EnsEMBL::Registry->remove_alias("Homo Sapiens","Human");
1301 Description: remove alternative name for the species.
1302 Returntype : none
1303 Exceptions : none
1304 Status : Stable
1305  
1306 =cut
1307  
1308 sub remove_alias{
1309 my ($class, $species,$key) = @_;
1310  
1311 delete $registry_register{'_ALIAS'}{lc($key)};
1312 return;
1313 }
1314  
1315  
1316  
1317 =head2 get_alias
1318  
1319 Arg [1] : name of the possible alias to get species for
1320 Example : Bio::EnsEMBL::Registry->get_alias("Human");
1321 Description: get proper species name.
1322 Returntype : species name
1323 Exceptions : none
1324 Status : Stable
1325  
1326 =cut
1327  
1328 sub get_alias {
1329 my ( $class, $key, $no_warn ) = @_;
1330  
1331 if ( !defined( $registry_register{'_ALIAS'}{ lc($key) } ) ) {
1332 if ( ( !defined( $registry_register{_SPECIES}{ lc($key) } ) ) and
1333 ( !defined( $registry_register{_ALIAS}{ lc($key) } ) ) )
1334 {
1335 if ( ( !defined($no_warn) ) or ( !$no_warn ) ) {
1336 warning( "$key is not a valid species name " .
1337 "(check DB and API version)" );
1338 }
1339 return;
1340 }
1341 else { return $key }
1342 }
1343  
1344 return $registry_register{'_ALIAS'}{ lc($key) };
1345 }
1346  
1347 =head2 get_all_aliases
1348  
1349 Arg [1] : Species name to retrieve aliases for
1350 (may be an alias as well).
1351 Example : Bio::EnsEMBL::Registry->get_all_aliases('Homo sapiens');
1352 Description: Returns all known aliases for a given species (but not the
1353 species name/alias that was given).
1354 Returntype : ArrayRef of all known aliases
1355 Exceptions : none
1356 Status : Development
1357  
1358 =cut
1359  
1360 sub get_all_aliases {
1361 my ( $class, $key ) = @_;
1362  
1363 my $species = $registry_register{_ALIAS}{ lc($key) };
1364  
1365 my @aliases;
1366 if ( defined($species) ) {
1367 foreach my $alias ( keys( %{ $registry_register{_ALIAS} } ) ) {
1368 if ( $species ne $alias
1369 && $species eq $registry_register{_ALIAS}{ lc($alias) } )
1370 {
1371 push( @aliases, $alias );
1372 }
1373 }
1374 }
1375  
1376 return \@aliases;
1377 }
1378  
1379 =head2 alias_exists
1380  
1381 Arg [1] : name of the possible alias to get species for
1382 Example : Bio::EnsEMBL::Registry->alias_exists("Human");
1383 Description: does the species name exist.
1384 Returntype : 1 if exists else 0
1385 Exceptions : none
1386 Status : Stable
1387  
1388 =cut
1389  
1390 sub alias_exists {
1391 my ( $class, $key ) = @_;
1392  
1393 return defined( $registry_register{'_ALIAS'}{ lc($key) } );
1394 }
1395  
1396 =head2 set_disconnect_when_inactive
1397  
1398 Example : Bio::EnsEMBL::Registry->set_disconnect_when_inactive();
1399 Description: Set the flag to make sure that the database connection is dropped if
1400 not being used on each database.
1401 Returntype : none
1402 Exceptions : none
1403 Status : Stable
1404  
1405 =cut
1406  
1407 sub set_disconnect_when_inactive{
1408 foreach my $dba ( @{get_all_DBAdaptors()}){
1409 my $dbc = $dba->dbc;
1410 # Disconnect if connected
1411 $dbc->disconnect_if_idle() if $dbc->connected();
1412 $dbc->disconnect_when_inactive(1);
1413 }
1414 return;
1415 }
1416  
1417 =head2 set_reconnect_when_lost
1418  
1419 Example : Bio::EnsEMBL::Registry->set_reconnect_when_lost();
1420 Description: Set the flag to make sure that the database connection is not lost before it's used.
1421 This is useful for long running jobs (over 8hrs).
1422 Returntype : none
1423 Exceptions : none
1424 Status : Stable
1425  
1426 =cut
1427  
1428 sub set_reconnect_when_lost{
1429 foreach my $dba ( @{get_all_DBAdaptors()}){
1430 my $dbc = $dba->dbc;
1431 $dbc->reconnect_when_lost(1);
1432 }
1433 return;
1434 }
1435  
1436 =head2 disconnect_all
1437  
1438 Example : Bio::EnsEMBL::Registry->disconnect_all();
1439 Description: disconnect from all the databases.
1440 Returntype : none
1441 Exceptions : none
1442 Status : Stable
1443  
1444 =cut
1445  
1446 sub disconnect_all {
1447 foreach my $dba ( @{get_all_DBAdaptors()||[]} ){
1448 my $dbc = $dba->dbc;
1449 next unless $dbc;
1450 # Disconnect if connected
1451 $dbc->disconnect_if_idle() if $dbc->connected();
1452 }
1453 return;
1454 }
1455  
1456 =head get_DBAdaptor_count
1457  
1458 Example : Bio::EnsEMBL::Registry->get_DBAdaptor_count();
1459 Description : Returns the count of database adaptors currently held by
1460 the registry
1461 Returntype : Int count of database adaptors currently known
1462 Exceptions : None
1463
1464 =cut
1465  
1466 sub get_DBAdaptor_count {
1467 return scalar(@{$registry_register{'_DBA'}}) if(defined $registry_register{'_DBA'});
1468 return 0;
1469 }
1470  
1471 =head2 change_access
1472  
1473 Will change the username and password for a set of databases.
1474 if host,user or database names are missing then these are not checked.
1475 So for example if you do not specify a database then ALL databases on
1476 the specified host and port will be changed.
1477  
1478 Arg [1] : name of the host to change access on
1479 Arg [2] : port number to change access on
1480 Arg [3] : name of the user to change access on
1481 Arg [4] : name of the database to change access on
1482 Arg [5] : name of the new user
1483 Arg [6] : new password
1484  
1485 Example : Bio::EnsEMBL::Registry->get_alias("Human");
1486 Description: change username and password on one or more databases
1487 Returntype : none
1488 Exceptions : none
1489 Status : Stable
1490  
1491 =cut
1492  
1493 sub change_access{
1494 my ($self, $host,$port,$user,$dbname,$new_user,$new_pass) = @_;
1495 foreach my $dba ( @{$registry_register{'_DBA'}}){
1496 my $dbc = $dba->dbc;
1497 if((((!defined($host)) or ($host eq $dbc->host))) and
1498 (((!defined($port)) or ($port eq $dbc->port))) and
1499 (((!defined($user)) or ($user eq $dbc->username))) and
1500 ((!defined($dbname)) or ($dbname eq $dbc->dbname))){
1501 if($dbc->connected()){
1502 $dbc->db_handle->disconnect();
1503 $dbc->connected(undef);
1504 }
1505 # over write the username and password
1506 $dbc->username($new_user);
1507 $dbc->password($new_pass);
1508 }
1509 }
1510 return;
1511 }
1512  
1513  
1514  
1515 =head2 load_registry_from_url
1516  
1517 Arg [1] : string $url
1518 Arg [2] : (optional) integer
1519 If not 0, will print out all information.
1520 Arg [3] : (optional) integer
1521 This option will turn off caching for slice features, so,
1522 every time a set of features is retrieved, they will come
1523 from the database instead of the cache. This option is only
1524 recommended for advanced users, specially if you need to
1525 store and retrieve features. It might reduce performance when
1526 querying the database if not used properly. If in doubt, do
1527 not use it or ask in the developer mailing list.
1528  
1529 Example : load_registry_from_url(
1530 'mysql://anonymous@ensembldb.ensembl.org:3306');
1531
1532 load_registry_from_url(
1533 'mysql://anonymous@ensembldb.ensembl.org:3306/homo_sapiens_core_65_37?group=core&species=homo_sapiens'
1534 );
1535
1536 load_registry_from_url(
1537 'mysql://anonymous@ensembldb.ensembl.org:3306/homo_sapiens_core_65_37?group=core'
1538 );
1539
1540  
1541 Description: Will load the correct versions of the ensembl
1542 databases for the software release it can find on
1543 a database instance into the registry. Also adds
1544 a set of standard aliases. The url format is:
1545 mysql://[[username][:password]@]hostname[:port]. You
1546 can also request a specific version for the databases
1547 by adding a slash and the version number but your
1548 script may crash as the API version won't match the
1549 DB version.
1550
1551 You can also specify a database name which will cause the
1552 loading of a single DBAdaptor instance. Parameters are
1553 mapped from a normal URL parameter set to their DBAdaptor
1554 equivalent. Group must be defined.
1555
1556 Returntype : Int count of the DBAdaptor instances which can be found in the
1557 registry
1558  
1559 Exceptions : Thrown if the given URL does not parse according to the above
1560 scheme and if the specified database cannot be connected to
1561 (see L<load_registry_from_db> for more information)
1562 Status : Stable
1563
1564 =cut
1565  
1566 sub load_registry_from_url {
1567 my ( $self, $url, $verbose, $no_cache ) = @_;
1568
1569 if ( $url =~ /^mysql\:\/\/([^\@]+\@)?([^\:\/]+)(\:\d+)?(\/\d+)?\/?$/x ) {
1570 my $user_pass = $1;
1571 my $host = $2;
1572 my $port = $3;
1573 my $version = $4;
1574  
1575 $user_pass =~ s/\@$//;
1576 my ( $user, $pass ) = $user_pass =~ m/([^\:]+)(\:.+)?/x;
1577 $pass =~ s/^\://x if ($pass);
1578 $port =~ s/^\://x if ($port);
1579 $version =~ s/^\///x if ($version);
1580  
1581 return $self->load_registry_from_db(
1582 -host => $host,
1583 -user => $user,
1584 -pass => $pass,
1585 -port => $port,
1586 -db_version => $version,
1587 -verbose => $verbose,
1588 -no_cache => $no_cache
1589 );
1590 }
1591 my $uri = parse_uri($url);
1592 if($uri) {
1593 if($uri->scheme() eq 'mysql') {
1594 my %params = $uri->generate_dbsql_params();
1595 if($params{-DBNAME}) {
1596 $params{-SPECIES} = $params{-DBNAME} unless $params{-SPECIES};
1597 $params{-NO_CACHE} = 1 if $no_cache;
1598 my $group = $params{-GROUP};
1599 my $class = $self->_group_to_adaptor_class($group);
1600 if($verbose) {
1601 printf("Loading database '%s' from group '%s' with DBAdaptor class '%s' from url %s\n", $params{-DBNAME}, $group, $class, $url);
1602 }
1603 $class->new(%params);
1604 return 1;
1605 }
1606 }
1607 }
1608 throw("Only MySQL URLs are accepted. Given URL was '${url}'");
1609 } ## end sub load_registry_from_url
1610  
1611  
1612 =head2 load_registry_from_db
1613  
1614 Arg [HOST] : string
1615 The domain name of the database host to connect to.
1616  
1617 Arg [USER] : string
1618 The name of the database user to connect with.
1619  
1620 Arg [PASS] : (optional) string
1621 The password to be used to connect to the database.
1622  
1623 Arg [PORT] : (optional) integer
1624 The port to use when connecting to the database.
1625  
1626 Arg [VERBOSE]: (optional) boolean
1627 Whether to print database messages. This includes a listing
1628 of all available species & databases.
1629  
1630 Arg [SPECIES]: (optional) string
1631 By default, all databases that are found on the
1632 server and that corresponds to the correct release
1633 are probed for aliases etc. For some people,
1634 depending on where they are in the world, this might
1635 be a slow operation. With the '-species' argument,
1636 one may reduce the startup time by restricting the
1637 set of databases that are probed to those of a
1638 particular species.
1639  
1640 Note that the latin name of the species is required,
1641 e.g., 'homo sapiens', 'gallus gallus', 'callithrix
1642 jacchus' etc. It may be the whole species name,
1643 or only the first part of the name, e.g. 'homo',
1644 'gallus', or 'callithrix'. This will be used in
1645 matching against the name of the databases.
1646  
1647 Arg [DB_VERSION]: (optional) integer
1648 By default, only databases corresponding to the
1649 current API version are loaded. This argument
1650 allows the script to use databases from another
1651 version although it might not work properly. This
1652 argument should only be used for production or
1653 testing purposes and if you really know what you are
1654 doing.
1655  
1656 Arg [WAIT_TIMEOUT]: (optional) integer
1657 Time in seconds for the wait timeout to happen.
1658 Time after which the connection is deleted if not
1659 used. By default this is 28800 (8 hours), so set
1660 this to greater than this if your connection are
1661 getting deleted. Only set this if you are having
1662 problems and know what you are doing.
1663  
1664 Arg [-NO_CACHE]: (optional) boolean
1665 This option will turn off caching for slice features,
1666 so, every time a set of features is retrieved, they
1667 will come from the database instead of the cache. This
1668 option is only recommended for advanced users, specially
1669 if you need to store and retrieve features. It might
1670 reduce performance when querying the database if not
1671 used properly. If in doubt, do not use it or ask in the
1672 developer mailing list.
1673  
1674 Arg [SPECIES_SUFFIX]: (optional) string
1675 This option will append the string to the species name
1676 in the registry for all databases found on this server.
1677  
1678 Example :
1679  
1680 $registry->load_registry_from_db(
1681 -host => 'ensembldb.ensembl.org',
1682 -user => 'anonymous',
1683 -verbose => '1'
1684 );
1685  
1686 Description: Will load the correct versions of the Ensembl
1687 databases for the software release it can find on a
1688 database instance into the registry. Also adds a set
1689 of standard aliases.
1690  
1691 Returntype : Int count of the DBAdaptor instances which can be found in the
1692 registry due to this method call.
1693  
1694 Exceptions : Thrown if the given MySQL database cannot be connected to
1695 or there is any error whilst querying the database.
1696 Status : Stable
1697  
1698 =cut
1699  
1700 sub load_registry_from_db {
1701 my ( $self, @args ) = @_;
1702  
1703 my ( $host, $port, $user,
1704 $pass, $verbose, $db_version,
1705 $wait_timeout, $no_cache, $species, $species_suffix, $db_prefix )
1706 = rearrange( [ 'HOST', 'PORT',
1707 'USER', 'PASS',
1708 'VERBOSE', 'DB_VERSION',
1709 'WAIT_TIMEOUT', 'NO_CACHE',
1710 'SPECIES', 'SPECIES_SUFFIX', 'DB_PREFIX' ],
1711 @args );
1712  
1713 my $ignore_multi = 0;
1714  
1715 if ( defined($species) ) {
1716 $species = lc($species);
1717 $species =~ tr/ -/__/;
1718 $ignore_multi = 1;
1719 }
1720 if (!defined($species_suffix)) {
1721 $species_suffix = "";
1722 }
1723 if (defined($db_prefix)) {
1724 $db_prefix = $db_prefix . '_';
1725 } else {
1726 $db_prefix = '';
1727 }
1728  
1729 if(! defined $db_version) {
1730 # Do checking for the -DB_VERSION flag which can be mis-spelt. Regex assembled using:
1731 # perl -MRegexp::Assemble -e '$r=Regexp::Assemble->new(); $r->add($_) for ("-dbversion","-version","-verion","-verison"); print $r->re, "\n";'
1732 my %hashed_args = @args;
1733 my ($possible_key) = grep { $_ =~ /(?-xism:-(?:ver(?:is?|si)|dbversi)on)/xism } keys %hashed_args;
1734 if($possible_key) {
1735 my $msg = sprintf(q{Detected no -DB_VERSION flag but found '%s'; assuming a mis-spelling. Please fix}, $possible_key);
1736 warning($msg);
1737 $db_version = $hashed_args{$possible_key};
1738 }
1739 }
1740  
1741  
1742 my $ontology_db;
1743 my $ontology_version;
1744  
1745 my $taxonomy_db;
1746 my $taxonomy_db_versioned;
1747 my $ensembl_metadata_db;
1748 my $ensembl_metadata_db_versioned;
1749  
1750 my $production_dba_ok =
1751 eval { require Bio::EnsEMBL::Production::DBSQL::DBAdaptor; 1 };
1752 my $production_db;
1753 my $production_version;
1754  
1755 my $stable_ids_db;
1756 my $stable_ids_version;
1757  
1758 $user ||= "anonymous";
1759 if ( !defined($port) ) {
1760 $port = 3306;
1761 if ( $host eq "ensembldb.ensembl.org" && defined($db_version) && $db_version < 48 ) {
1762 $port = 4306;
1763 }
1764 }
1765  
1766 $wait_timeout ||= 0;
1767
1768 my $original_count = $self->get_DBAdaptor_count();
1769  
1770 my $err_pattern = 'Cannot %s to the Ensembl MySQL server at %s:%d; check your settings & DBI error message: %s';
1771  
1772 my $dbh = DBI->connect( "DBI:mysql:host=$host;port=$port", $user, $pass ) or
1773 throw(sprintf($err_pattern, 'connect', $host, $port, $DBI::errstr));
1774 $dbh->ping() or
1775 throw(sprintf($err_pattern, 'ping', $host, $port, $DBI::errstr));
1776
1777 my $res = $dbh->selectall_arrayref('SHOW DATABASES');
1778 my @dbnames = map { $_->[0] } @$res;
1779  
1780 my %temp;
1781 my $software_version = software_version();
1782  
1783 if ( defined($db_version) ) {
1784 $software_version = $db_version;
1785 }
1786  
1787 if ($verbose) {
1788 printf( "Will only load v%d databases\n", $software_version );
1789 }
1790  
1791 # From the list of all the databses create a tempory hash of those we
1792 # are interested in
1793  
1794 for my $db (@dbnames) {
1795 if ( $db =~ /^(\w+_collection_\w+(?:_\d+)?)_((\d+)_\w+)/ )
1796 { # NEEDS TO BE FIRST TO PICK UP COLLECTION DBS
1797 if ( $3 eq $software_version ) {
1798 $temp{$1} = $2;
1799 }
1800 } elsif ( $db =~ /^(.+)_(userdata)$/x ) {
1801 $temp{$1} = $2;
1802 } elsif (
1803 $db =~ /^(ensembl_compara # compara database
1804 (?:_\w+)*?) # optional ensembl genomes bit
1805 _
1806 (\d+)$/x )
1807 { # db version
1808 if ( $2 eq $software_version ) {
1809 $temp{$1} = $2;
1810 }
1811 } elsif ( $db =~ /^(ensembl_ancestral(?:_\w+?)*?)_(\d+)$/x ) {
1812 if ( $2 eq $software_version ) {
1813 $temp{$1} = $2;
1814 }
1815 } elsif ( $db =~ /^ensembl(?:genomes)?_ontology_(?:\d+_)?(\d+)/x ) {
1816 if ( $1 eq $software_version ) {
1817 $ontology_db = $db;
1818 $ontology_version = $1;
1819 }
1820 } elsif ( $db =~ /^ncbi_taxonomy$/ ) {
1821 $taxonomy_db = $db;
1822 }
1823 elsif ( $db =~ m{ \A ncbi_taxonomy_(\d+) \z }msx ) {
1824 if ( $1 eq $software_version ) {
1825 $taxonomy_db_versioned = $db;
1826 }
1827 } elsif ( $db =~ /^ensembl_metadata$/ ) {
1828 $ensembl_metadata_db = $db;
1829 }
1830 elsif ( $db =~ m{ \A ensembl_metadata_(\d+) \z }msx ) {
1831 if ( $1 eq $software_version ) {
1832 $ensembl_metadata_db_versioned = $db;
1833 }
1834 } elsif ( $production_dba_ok and $db =~ /^ensembl(?:genomes)?_production(_\d+)?/x ) {
1835 # production db can come with no version (i.e. that on ens-staging1),
1836 # but it's backed up with a release number
1837 my $version = $1;
1838 if ($version) {
1839 $version =~ s/_//;
1840 if ($software_version and $version eq $software_version) {
1841 $production_db = $db;
1842 $production_version = $version;
1843 }
1844 } else { # this is the default choice
1845 $production_db = $db if $db =~ /^ensembl(?:genomes)?_production$/;
1846 }
1847 } elsif ( $db =~ /^ensembl(?:genomes)?_stable_ids_(?:\d+_)?(\d+)/x ) {
1848 if ( $1 eq $software_version ) {
1849 $stable_ids_db = $db;
1850 $stable_ids_version = $1;
1851 }
1852  
1853 } elsif (
1854 $db =~ /^(?:$db_prefix)([a-z]+_[a-z0-9]+(?:_[a-z0-9]+)? # species name e.g. homo_sapiens or canis_lupus_familiaris
1855 _
1856 [a-z]+ # db type
1857 (?:_\d+)?) # optional end bit for ensembl genomes databases
1858 _
1859 (\d+) # database release
1860 _
1861 (\w+)$ # assembly number can have letters too e.g 37c
1862 /x
1863 )
1864 {
1865  
1866 # Species specific databases (core, cdna, vega etc.)
1867  
1868 my ( $sp_name, $db_rel, $assem ) = ( $1, $2, $3 );
1869 if ($db_prefix) { $sp_name = $db_prefix . $sp_name; }
1870  
1871 if ( !defined($species) || $sp_name =~ /^$species/ ) {
1872 if ( $db_rel eq $software_version ) {
1873 $temp{$sp_name} = $db_rel . "_" . $assem;
1874 }
1875 }
1876  
1877 } else {
1878 # warn( sprintf( "Skipping database '%s'\n", $db ) );
1879 }
1880 } ## end for my $db (@dbnames)
1881  
1882 @dbnames = ();
1883  
1884 foreach my $key ( keys %temp ) {
1885 push @dbnames, $key . "_" . $temp{$key};
1886 }
1887  
1888 # Register Core like databases
1889 my $core_like_dbs_found = 0;
1890 foreach my $type (qw(core cdna vega vega_update otherfeatures rnaseq ccds)) {
1891  
1892 my @dbs = grep { /^(?:$db_prefix)[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)? # species name
1893 _
1894 $type # the database type
1895 _
1896 (?:\d+_)? # optional end bit for ensembl genomes
1897 \d+ # database release
1898 _
1899 /x } @dbnames;
1900  
1901 if(@dbs) {
1902 $core_like_dbs_found = 1;
1903 }
1904  
1905 foreach my $database (@dbs) {
1906 if ( index( $database, 'collection' ) != -1 ) {
1907 # Skip multi-species databases.
1908 next;
1909 }
1910
1911  
1912 my ( $prefix, $species, $num ) =
1913 ( $database =~ /(^$db_prefix)([a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?) # species name
1914 _
1915 $type # type
1916 _
1917 (?:\d+_)? # optional endbit for ensembl genomes
1918 (\d+) # databases release
1919 _
1920 /x );
1921  
1922 if(!defined($species)){
1923 warn "Cannot extract species name from database '$database'";
1924 }
1925  
1926 my $dba =
1927 Bio::EnsEMBL::DBSQL::DBAdaptor->new(
1928 -group => $type,
1929 -species => $species.$species_suffix,
1930 -host => $host,
1931 -user => $user,
1932 -pass => $pass,
1933 -port => $port,
1934 -dbname => $database,
1935 -wait_timeout => $wait_timeout,
1936 -no_cache => $no_cache );
1937  
1938 if ($verbose) {
1939 printf( "Species '%s' loaded from database '%s'\n",
1940 $species, $database );
1941 }
1942 }
1943 }
1944  
1945 # Register multi-species databases
1946  
1947 my @multi_dbs = grep { /^\w+_collection_core_\w+$/ } @dbnames;
1948  
1949 if (!$ignore_multi) {
1950 foreach my $multidb (@multi_dbs) {
1951 my $sth = $dbh->prepare(
1952 sprintf(
1953 "SELECT species_id, meta_value FROM %s.meta "
1954 . "WHERE meta_key = 'species.db_name'",
1955 $dbh->quote_identifier($multidb) ) );
1956
1957 $sth->execute();
1958
1959 my ( $species_id, $species );
1960 $sth->bind_columns( \( $species_id, $species ) );
1961
1962 while ( $sth->fetch() ) {
1963 my $dba = Bio::EnsEMBL::DBSQL::DBAdaptor->new(
1964 -group => "core",
1965 -species => $species.$species_suffix,
1966 -species_id => $species_id,
1967 -multispecies_db => 1,
1968 -host => $host,
1969 -user => $user,
1970 -pass => $pass,
1971 -port => $port,
1972 -dbname => $multidb,
1973 -wait_timeout => $wait_timeout,
1974 -no_cache => $no_cache
1975 );
1976
1977 if ($verbose) {
1978 printf( "Species '%s' (id:%d) loaded from database '%s'\n",
1979 $species, $species_id, $multidb );
1980 }
1981 }
1982 } ## end foreach my $multidb (@multi_dbs)
1983 }
1984  
1985 if(!$core_like_dbs_found && $verbose) {
1986 print("No core-like databases found. Check your DB_VERSION (used '$software_version')\n");
1987 }
1988  
1989 # User upload DBs
1990  
1991 my @userupload_dbs = grep { /_userdata$/ } @dbnames;
1992 if (!$ignore_multi) {
1993 for my $userupload_db (@userupload_dbs) {
1994 if ( index( $userupload_db, 'collection' ) != -1 ) {
1995 # Skip multi-species databases.
1996 next;
1997 }
1998
1999 my ($species) = ( $userupload_db =~ /(^.+)_userdata$/ );
2000 my $dba =
2001 Bio::EnsEMBL::DBSQL::DBAdaptor->new(
2002 -group => "userupload",
2003 -species => $species.$species_suffix,
2004 -host => $host,
2005 -user => $user,
2006 -pass => $pass,
2007 -port => $port,
2008 -wait_timeout => $wait_timeout,
2009 -dbname => $userupload_db,
2010 -no_cache => $no_cache );
2011
2012 if ($verbose) {
2013 printf( "%s loaded\n", $userupload_db );
2014 }
2015 }
2016 }
2017  
2018 # Register multi-species userupload databases.
2019 my @userdata_multidbs = grep { /^.+_collection_userdata$/ } @dbnames;
2020  
2021 if (!$ignore_multi) {
2022 foreach my $multidb (@userdata_multidbs) {
2023 my $sth = $dbh->prepare(
2024 sprintf(
2025 "SELECT species_id, meta_value FROM %s.meta "
2026 . "WHERE meta_key = 'species.db_name'",
2027 $dbh->quote_identifier($multidb) ) );
2028
2029 $sth->execute();
2030
2031 my ( $species_id, $species );
2032 $sth->bind_columns( \( $species_id, $species ) );
2033
2034 while ( $sth->fetch() ) {
2035 my $dba = Bio::EnsEMBL::DBSQL::DBAdaptor->new(
2036 -group => "userupload",
2037 -species => $species.$species_suffix,
2038 -species_id => $species_id,
2039 -multispecies_db => 1,
2040 -host => $host,
2041 -user => $user,
2042 -pass => $pass,
2043 -port => $port,
2044 -dbname => $multidb,
2045 -wait_timeout => $wait_timeout,
2046 -no_cache => $no_cache
2047 );
2048
2049 if ($verbose) {
2050 printf( "Species '%s' (id:%d) loaded from database '%s'\n",
2051 $species, $species_id, $multidb );
2052 }
2053 }
2054 } ## end foreach my $multidb (@userdata_multidbs)
2055 }
2056  
2057 # Variation
2058  
2059 my $test_eval = eval "require Bio::EnsEMBL::Variation::DBSQL::DBAdaptor"; ## no critic
2060 if ($@or (!$test_eval)) {
2061 # Ignore variations as code required not there for this
2062 if ($verbose) {
2063 print(
2064 "Bio::EnsEMBL::Variation::DBSQL::DBAdaptor module not found "
2065 . "so variation databases will be ignored if found\n" );
2066 }
2067 }
2068 else {
2069 my @variation_dbs =
2070 grep { /^[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?_variation_(?:\d+_)?\d+_/ } @dbnames;
2071  
2072 if(! @variation_dbs && $verbose) {
2073 print("No variation databases found\n");
2074 }
2075  
2076 for my $variation_db (@variation_dbs) {
2077
2078 if ( index( $variation_db, 'collection' ) != -1 ) {
2079 # Skip multi-species databases.
2080 next;
2081 }
2082  
2083 my ( $species, $num ) =
2084 ( $variation_db =~ /(^[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?)_variation_(?:\d+_)?(\d+)_/ );
2085 my $dba =
2086 Bio::EnsEMBL::Variation::DBSQL::DBAdaptor->new(
2087 -group => "variation",
2088 -species => $species.$species_suffix,
2089 -host => $host,
2090 -user => $user,
2091 -pass => $pass,
2092 -port => $port,
2093 -wait_timeout => $wait_timeout,
2094 -dbname => $variation_db,
2095 -no_cache => $no_cache );
2096  
2097 if ($verbose) {
2098 printf( "%s loaded\n", $variation_db );
2099 }
2100 }
2101  
2102 # Register variation multispecies databases
2103 my @variation_multidbs =
2104 grep { /^\w+_collection_variation_\w+$/ } @dbnames;
2105  
2106 if (!$ignore_multi) {
2107 foreach my $multidb (@variation_multidbs) {
2108 my $sth = $dbh->prepare(
2109 sprintf( 'SELECT species_id, meta_value FROM %s.meta ',
2110 $dbh->quote_identifier($multidb) )
2111 . "WHERE meta_key = 'species.db_name'"
2112 );
2113
2114 $sth->execute();
2115
2116 my ( $species_id, $species );
2117 $sth->bind_columns( \( $species_id, $species ) );
2118
2119 while ( $sth->fetch() ) {
2120 my $dba = Bio::EnsEMBL::Variation::DBSQL::DBAdaptor->new(
2121 -group => 'variation',
2122 -species => $species.$species_suffix,
2123 -species_id => $species_id,
2124 -multispecies_db => 1,
2125 -host => $host,
2126 -user => $user,
2127 -pass => $pass,
2128 -port => $port,
2129 -dbname => $multidb,
2130 -wait_timeout => $wait_timeout,
2131 -no_cache => $no_cache
2132 );
2133
2134 if ($verbose) {
2135 printf( "Species '%s' (id:%d) loaded from database '%s'\n",
2136 $species, $species_id, $multidb );
2137 }
2138 }
2139 } ## end foreach my $multidb (@variation_multidbs)
2140 }
2141 }
2142  
2143 my $func_eval = eval "require Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor"; ## no critic
2144 if ($@ or (!$func_eval)) {
2145 if ($verbose) {
2146 # Ignore funcgen DBs as code required not there for this
2147 print("Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor module not found "
2148 . "so functional genomics databases will be ignored if found\n"
2149 );
2150 }
2151 } else {
2152 my @funcgen_dbs =
2153 grep { /^[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?_funcgen_(?:\d+_)?\d+_/ } @dbnames;
2154
2155 if(! @funcgen_dbs && $verbose) {
2156 print("No funcgen databases found\n");
2157 }
2158  
2159 for my $funcgen_db (@funcgen_dbs) {
2160 if ( index( $funcgen_db, 'collection' ) != -1 ) {
2161 # Skip multi-species databases.
2162 next;
2163 }
2164  
2165 my ( $species, $num ) =
2166 ( $funcgen_db =~ /(^[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?)_funcgen_(?:\d+_)?(\d+)_/ );
2167 my $dba = Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor->new(
2168 -group => "funcgen",
2169 -species => $species.$species_suffix,
2170 -host => $host,
2171 -user => $user,
2172 -pass => $pass,
2173 -port => $port,
2174 -wait_timeout => $wait_timeout,
2175 -dbname => $funcgen_db,
2176 -no_cache => $no_cache
2177 );
2178  
2179 if ($verbose) {
2180 printf( "%s loaded\n", $funcgen_db );
2181 }
2182 }
2183  
2184 # Register functional genomics multispecies databases
2185 my @funcgen_multidbs =
2186 grep { /^\w+_collection_funcgen_\w+$/ } @dbnames;
2187  
2188 if (!$ignore_multi) {
2189 foreach my $multidb (@funcgen_multidbs) {
2190 my $sth = $dbh->prepare(
2191 sprintf( 'SELECT species_id, meta_value FROM %s.meta ',
2192 $dbh->quote_identifier($multidb) )
2193 . "WHERE meta_key = 'species.db_name'"
2194 );
2195
2196 $sth->execute();
2197
2198 my ( $species_id, $species );
2199 $sth->bind_columns( \( $species_id, $species ) );
2200
2201 while ( $sth->fetch() ) {
2202 my $dba = Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor->new(
2203 -group => 'funcgen',
2204 -species => $species.$species_suffix,
2205 -species_id => $species_id,
2206 -multispecies_db => 1,
2207 -host => $host,
2208 -user => $user,
2209 -pass => $pass,
2210 -port => $port,
2211 -dbname => $multidb,
2212 -wait_timeout => $wait_timeout,
2213 -no_cache => $no_cache
2214 );
2215
2216 if ($verbose) {
2217 printf( "Species '%s' (id:%d) loaded from database '%s'\n",
2218 $species, $species_id, $multidb );
2219 }
2220 }
2221 } ## end foreach my $multidb (@funcgen_multidbs)
2222 }
2223 } ## end else [ if ($@) ]
2224  
2225 # Compara
2226  
2227 my @compara_dbs = grep { /^ensembl_compara/ } @dbnames;
2228  
2229 if (!$ignore_multi) {
2230 if (@compara_dbs) {
2231 my $comp_eval = eval "require Bio::EnsEMBL::Compara::DBSQL::DBAdaptor"; ## no critic
2232 if ($@ or (!$comp_eval)) {
2233 # Ignore Compara as code required not there for this
2234 if ($verbose) {
2235 printf(
2236 "Bio::EnsEMBL::Compara::DBSQL::DBAdaptor "
2237 . "not found so the following compara "
2238 . "databases will be ignored: %s\n",
2239 join( ', ', @compara_dbs ) );
2240 }
2241 } else {
2242 foreach my $compara_db (@compara_dbs) {
2243 # Looking for EnsEMBL Genomes Comparas.
2244 # ensembl_compara_bacteria_2_53 is registered as
2245 # 'bacteria', ensembl_compara_pan_homology_2_53 is
2246 # registered as 'pan_homology', ensembl_compara_53 is
2247 # registered as 'multi', and the alias 'compara' still
2248 # operates.
2249
2250 my ($species) =
2251 $compara_db =~ /^ensembl_compara_(\w+)(?:_\d+){2}$/xm;
2252
2253 $species ||= 'multi';
2254
2255 my $dba = Bio::EnsEMBL::Compara::DBSQL::DBAdaptor->new(
2256 -group => 'compara',
2257 -species => $species.$species_suffix,
2258 -host => $host,
2259 -user => $user,
2260 -pass => $pass,
2261 -port => $port,
2262 -wait_timeout => $wait_timeout,
2263 -dbname => $compara_db,
2264 -no_cache => $no_cache
2265 );
2266
2267 if ($verbose) {
2268 printf( "%s loaded\n", $compara_db );
2269 }
2270 } ## end foreach my $compara_db (@compara_dbs)
2271 } ## end else [ if ($@)
2272 } elsif ($verbose) {
2273 print("No Compara databases found\n");
2274 }
2275 }
2276  
2277 # Ancestral sequences
2278  
2279 my @ancestral_dbs =
2280 sort grep { /^ensembl_ancestral/ } @dbnames;
2281  
2282 if (@ancestral_dbs && !$ignore_multi) {
2283 foreach my $ancestral_db (@ancestral_dbs) {
2284 # Looking for Compara's "ancestral" databases.
2285 # ensembl_ancestral_plants_47_100 is registered with the 'plants'
2286 # prefix, while ensembl_ancestral_100 is not given any prefix for
2287 # backwards compatibility.
2288 # Similarly, contrary to the nomenclature, "Ancestral sequences"
2289 # is the species (production) name and "ancestral_sequences" is
2290 # an alias.
2291 my $alias;
2292 my ($division) = $ancestral_db =~ /^ensembl_ancestral_(\w+)(?:_\d+){2}$/xm;
2293 if ($division) {
2294 $species = (ucfirst $division).' Ancestral sequences'.$species_suffix;
2295 $alias = $division.'_ancestral_sequences'.$species_suffix;
2296 } else {
2297 $species = 'Ancestral sequences'.$species_suffix;
2298 $alias = 'ancestral_sequences'.$species_suffix;
2299 }
2300  
2301 my $dba = Bio::EnsEMBL::DBSQL::DBAdaptor->new(
2302 -group => 'core',
2303 -species => $species,
2304 -host => $host,
2305 -user => $user,
2306 -pass => $pass,
2307 -port => $port,
2308 -wait_timeout => $wait_timeout,
2309 -dbname => $ancestral_db,
2310 -no_cache => $no_cache
2311 );
2312  
2313 Bio::EnsEMBL::Utils::ConfigRegistry->add_alias(
2314 -species => $species,
2315 -alias => [$alias],
2316 );
2317  
2318 if ($verbose) {
2319 printf( "%s loaded\n", $ancestral_db );
2320 }
2321 }
2322 } elsif ($verbose) {
2323 print("No ancestral database found\n");
2324 }
2325  
2326 # Ontology
2327  
2328 if ( defined($ontology_version) && $ontology_version != 0 && !$ignore_multi) {
2329 require Bio::EnsEMBL::DBSQL::OntologyDBAdaptor;
2330  
2331 my $dba =
2332 Bio::EnsEMBL::DBSQL::OntologyDBAdaptor->new(
2333 '-species' => 'multi' . $species_suffix,
2334 '-group' => 'ontology',
2335 '-host' => $host,
2336 '-port' => $port,
2337 '-user' => $user,
2338 '-pass' => $pass,
2339 '-dbname' => $ontology_db, );
2340  
2341 if ($verbose) {
2342 printf( "%s loaded\n", $ontology_db );
2343 }
2344 }
2345 elsif ($verbose) {
2346 print("No ontology database found\n");
2347 }
2348  
2349 # Taxonomy
2350  
2351 if ( ( defined $taxonomy_db ) || ( defined $taxonomy_db_versioned ) ) {
2352
2353 my $has_taxonomy = eval {require Bio::EnsEMBL::Taxonomy::DBSQL::TaxonomyDBAdaptor};
2354 if($@ or (!defined $has_taxonomy)) {
2355 if($verbose) {
2356 print "ensembl_taxonomy API not found - ignoring $taxonomy_db\n";
2357 }
2358 } else {
2359  
2360 my $taxonomy_dbname;
2361 # Versioned database has priority over unversioned one.
2362 if ( defined $taxonomy_db_versioned ) {
2363 $taxonomy_dbname = $taxonomy_db_versioned;
2364 }
2365 else {
2366 $taxonomy_dbname = $taxonomy_db;
2367 }
2368  
2369 my $dba = Bio::EnsEMBL::Taxonomy::DBSQL::TaxonomyDBAdaptor->new(
2370 '-species' => 'multi' . $species_suffix,
2371 '-group' => 'taxonomy',
2372 '-host' => $host,
2373 '-port' => $port,
2374 '-user' => $user,
2375 '-pass' => $pass,
2376 '-dbname' => $taxonomy_dbname, );
2377  
2378 if ($verbose) {
2379 printf( "%s loaded\n", $taxonomy_dbname );
2380 }
2381 }
2382 }
2383 elsif ($verbose) {
2384 print("No taxonomy database found\n");
2385 }
2386
2387 # ensembl_metadata
2388  
2389 if ( ( defined $ensembl_metadata_db ) || ( defined $ensembl_metadata_db_versioned ) ) {
2390
2391 my $has_metadata = eval {require Bio::EnsEMBL::MetaData::DBSQL::MetaDataDBAdaptor};
2392 if($@ or (!defined $has_metadata)) {
2393 if($verbose) {
2394 print "ensembl_metadata API not found - ignoring $ensembl_metadata_db\n";
2395 }
2396 } else {
2397  
2398 my $metadata_dbname;
2399 # Versioned database has priority over unversioned one.
2400 if ( defined $ensembl_metadata_db_versioned ) {
2401 $metadata_dbname = $ensembl_metadata_db_versioned;
2402 }
2403 else {
2404 $metadata_dbname = $ensembl_metadata_db;
2405 }
2406  
2407 my $dba = Bio::EnsEMBL::MetaData::DBSQL::MetaDataDBAdaptor->new(
2408 '-species' => 'multi' . $species_suffix,
2409 '-group' => 'metadata',
2410 '-host' => $host,
2411 '-port' => $port,
2412 '-user' => $user,
2413 '-pass' => $pass,
2414 '-dbname' => $metadata_dbname, );
2415  
2416 if ($verbose) {
2417 printf( "%s loaded\n", $metadata_dbname );
2418 }
2419 }
2420 }
2421 elsif ($verbose) {
2422 print("No ensembl_metadata database found\n");
2423 }
2424  
2425 # Production
2426  
2427 if ( $production_dba_ok and defined($production_db) && !$ignore_multi) {
2428 # require Bio::EnsEMBL::Production::DBSQL::DBAdaptor;
2429  
2430 my $dba =
2431 Bio::EnsEMBL::Production::DBSQL::DBAdaptor->new(
2432 '-species' => 'multi' . $species_suffix,
2433 '-group' => 'production',
2434 '-host' => $host,
2435 '-port' => $port,
2436 '-user' => $user,
2437 '-pass' => $pass,
2438 '-dbname' => $production_db, );
2439  
2440 if ($verbose) {
2441 printf( "%s loaded\n", $production_db );
2442 }
2443 }
2444 elsif ($verbose) {
2445 print("No production database or adaptor found\n");
2446 }
2447  
2448 # Stable IDs
2449  
2450 if ( defined($stable_ids_db) && $stable_ids_version != 0 && !$ignore_multi) {
2451  
2452 my $dba =
2453 Bio::EnsEMBL::DBSQL::DBAdaptor->new(
2454 '-species' => 'multi' . $species_suffix,
2455 '-group' => 'stable_ids',
2456 '-host' => $host,
2457 '-port' => $port,
2458 '-user' => $user,
2459 '-pass' => $pass,
2460 '-dbname' => $stable_ids_db, );
2461  
2462 if ($verbose) {
2463 printf( "%s loaded\n", $stable_ids_db );
2464 }
2465  
2466 }
2467  
2468  
2469 Bio::EnsEMBL::Utils::ConfigRegistry->add_alias(
2470 -species => 'multi'.$species_suffix,
2471 -alias => ['compara'.$species_suffix] );
2472  
2473 Bio::EnsEMBL::Utils::ConfigRegistry->add_alias(
2474 -species => 'multi'.$species_suffix,
2475 -alias => ['ontology'.$species_suffix] );
2476  
2477 $production_dba_ok and
2478 Bio::EnsEMBL::Utils::ConfigRegistry->add_alias(
2479 -species => 'multi'.$species_suffix,
2480 -alias => ['production'.$species_suffix] );
2481  
2482 Bio::EnsEMBL::Utils::ConfigRegistry->add_alias(
2483 -species => 'multi'.$species_suffix,
2484 -alias => ['stable_ids'.$species_suffix] );
2485  
2486 # Register aliases as found in adaptor meta tables.
2487  
2488 $self->find_and_add_aliases( '-handle' => $dbh,
2489 '-species_suffix' => $species_suffix );
2490  
2491 $dbh->disconnect();
2492
2493 my $count = $self->get_DBAdaptor_count() - $original_count;
2494 return $count >= 0 ? $count : 0;
2495  
2496 } ## end sub load_registry_from_db
2497  
2498 =head2 _group_to_adaptor_class
2499  
2500 Arg [1] : The group you wish to decode to an adaptor class
2501 Example : Bio::EnsEMBL::Registry->_group_to_adaptor_class('core');
2502 Description : Has an internal lookup of groups to their adaptor classes
2503 Returntype : String
2504 Exceptions : Thrown if the group is unknown
2505 Status : Stable
2506  
2507 =cut
2508  
2509 sub _group_to_adaptor_class {
2510 my ($self, $group) = @_;
2511 my $class = $group2adaptor{$group};
2512 if (!defined $class) {
2513 $class = {
2514 cdna => 'Bio::EnsEMBL::DBSQL::DBAdaptor',
2515 rnaseq => 'Bio::EnsEMBL::DBSQL::DBAdaptor',
2516 }->{$group};
2517 }
2518 throw "Group '${group}' is unknown" if ! $class;
2519 return $class;
2520 }
2521  
2522  
2523 =head2 find_and_add_aliases
2524  
2525 Arg [ADAPTOR] : (optional) Bio::EnsEMBL::DBSQL::DBAdaptor
2526 The adaptor to use to retrieve aliases from.
2527  
2528 Arg [GROUP] : (optional) string
2529 The group you want to find aliases for. If not
2530 given assumes all types.
2531  
2532 Arg [HANDLE] : (optional) DBI database handle
2533 A connected database handle to use instead of
2534 the database handles stored in the DBAdaptors.
2535 Bypasses the use of MetaContainer.
2536  
2537 Arg [SPECIES_SUFFIX]: (optional) string
2538 This option will append the string to the species
2539 name in the registry for all databases.
2540  
2541 Example : Bio::EnsEMBL::Registry->find_and_add_aliases(
2542 -ADAPTOR => $dba,
2543 -GROUP => 'core'
2544 );
2545  
2546 Description : Looks in the meta container for each database for
2547 an entry called "species.alias". If any are found
2548 then the species adaptor is registered to that
2549 set of aliases. This can work across any adaptor
2550 which has a MetaContainer. If no MetaContainer
2551 can be returned from a given adaptor then no alias
2552 searching is performed.
2553  
2554 Return type : none
2555 Exceptions : Throws if an alias is found in more than one species.
2556 Status : Stable
2557  
2558 =cut
2559  
2560 sub find_and_add_aliases {
2561 my $class = shift ;
2562  
2563 my ($adaptor, $group, $dbh, $species_suffix ) =
2564 rearrange( [ 'ADAPTOR', 'GROUP', 'HANDLE', 'SPECIES_SUFFIX' ], @_ );
2565
2566 #Can be undef; needs to be something to avoid warnings
2567 $species_suffix ||= q{};
2568  
2569 my @dbas;
2570 if ( defined($adaptor) ) {
2571 @dbas = ($adaptor);
2572 } elsif ( defined($dbh) ) {
2573  
2574 if ( length($species_suffix) > 0 ) {
2575 my @full = @{ $class->get_all_DBAdaptors( '-GROUP' => $group ) };
2576  
2577 foreach my $db (@full) {
2578 if ( $db->species =~ /$species_suffix/ ) {
2579 push( @dbas, $db );
2580 }
2581 }
2582  
2583 } else {
2584 @dbas = @{ $class->get_all_DBAdaptors( '-GROUP' => $group ) };
2585 }
2586  
2587 } else {
2588 @dbas = @{ $class->get_all_DBAdaptors( '-GROUP' => $group ) };
2589 }
2590
2591 my $aliases_for_dbc = {};
2592  
2593 foreach my $dba (@dbas) {
2594 my @aliases;
2595 my $species = $dba->species();
2596  
2597 if ( defined($dbh) ) {
2598
2599 my $dbname = $dba->dbc()->dbname();
2600  
2601 if (!defined $aliases_for_dbc->{$dbname}) {
2602  
2603 my $sth = $dbh->prepare(sprintf("SELECT species_id,meta_value FROM %s.meta "
2604 . "WHERE meta_key = 'species.alias' ", $dbh->quote_identifier($dbname))
2605 );
2606  
2607 # Execute, and don't care about errors (there will be errors for
2608 # databases without a 'meta' table.
2609 $sth->{'PrintError'} = 0;
2610 $sth->{'RaiseError'} = 0;
2611 if (!$sth->execute()) { next }
2612 $sth->{'PrintError'} = $dbh->{'PrintError'};
2613 $sth->{'RaiseError'} = $dbh->{'RaiseError'};
2614  
2615 my $alias;
2616 my $species_id;
2617 $sth->bind_columns(\$species_id, \$alias);
2618 while ($sth->fetch()) {
2619 push(@{$aliases_for_dbc->{$dbname}{$species_id}}, $alias);
2620 }
2621 }
2622  
2623 @aliases = @{$aliases_for_dbc->{$dbname}{$dba->species_id()}||[]}
2624  
2625 } else {
2626 my $meta_container = eval { $dba->get_MetaContainer() };
2627  
2628 if ( defined($meta_container) ) {
2629 push( @aliases,
2630 @{ $meta_container->list_value_by_key('species.alias') }
2631 );
2632 }
2633  
2634 # Need to disconnect so we do not spam the MySQL servers trying to
2635 # get aliases. Can only call disonnect if dbc was defined.
2636 if ( defined( $dba->dbc() ) ) {
2637 $dba->dbc()->disconnect_if_idle();
2638 }
2639 }
2640  
2641 foreach my $alias (@aliases) {
2642 my $alias_suffix = $alias.$species_suffix;
2643 #Lowercase because stored aliases are lowercased
2644 my $lc_species = lc($species);
2645 my $lc_alias_suffix = lc($alias_suffix);
2646 if ( !$class->alias_exists( $alias_suffix )
2647 && $lc_species ne $lc_alias_suffix )
2648 {
2649 $class->add_alias( $species, $alias_suffix );
2650 } elsif (
2651 $lc_species ne $class->get_alias( $alias_suffix ) )
2652 {
2653 $class->remove_alias( $species, $alias_suffix );
2654 }
2655 }
2656  
2657 } ## end foreach my $dba (@dbas)
2658 return;
2659 } ## end sub find_and_add_aliases
2660  
2661  
2662 =head2 load_registry_from_multiple_dbs
2663  
2664 Arg [1] : Array of hashes, each hash being a set of arguments to
2665 load_registry_from_db() (see above).
2666  
2667 Example :
2668  
2669 $registry->load_registry_from_multiple_dbs( {
2670 '-host' => 'ensembldb.ensembl.org',
2671 '-user' => 'anonymous',
2672 '-verbose' => '1'
2673 },
2674 {
2675 '-host' => 'server.example.com',
2676 '-user' => 'anonymouse',
2677 '-password' => 'cheese',
2678 '-verbose' => '1'
2679 } );
2680  
2681 Description: Will call load_registry_from_db() (see above)
2682 multiple times and merge the resulting registries
2683 into one, effectively allowing a user to connect to
2684 databases on multiple database servers from within
2685 one program.
2686  
2687 If a database is found on more than one server, the
2688 first found instance of that database will be used.
2689  
2690 Returntype : Int count of the DBAdaptor instances which can be found in the
2691 registry
2692  
2693 =cut
2694  
2695 sub load_registry_from_multiple_dbs {
2696 my ( $self, @args ) = @_;
2697  
2698 my $original_count = $self->get_DBAdaptor_count();
2699  
2700 my %merged_register = %registry_register;
2701  
2702 foreach my $arg (@args) {
2703 local %registry_register = ();
2704  
2705 my $verbose;
2706  
2707 ($verbose) = rearrange( ['VERBOSE'], %{$arg} );
2708  
2709 $self->load_registry_from_db( %{$arg} );
2710  
2711 #
2712 # Merge the localized %registry_register into %merged_register.
2713 #
2714  
2715 # Merge the _SPECIES and _ALIAS sections of %registry_register.
2716 foreach my $section ( 'Species', 'Alias' ) {
2717 my $section_key = '_' . uc($section);
2718  
2719 while ( my ( $key, $value ) =
2720 each( %{ $registry_register{$section_key} } ) )
2721 {
2722 if ( !exists( $merged_register{$section_key}{$key} ) ) {
2723 $merged_register{$section_key}{$key} = $value;
2724 } elsif ($verbose) {
2725 printf( "%s '%s' found on multiple servers, "
2726 . "using first found\n",
2727 $section, $key );
2728 }
2729 }
2730 }
2731 } ## end foreach my $arg (@args)
2732  
2733 # Add the DBAs from the _SPECIES section into the _DBA section.
2734 foreach my $species_hash ( values( %{ $merged_register{_SPECIES} } ) )
2735 {
2736 foreach my $group_hash ( values( %{$species_hash} ) ) {
2737 if ( ref($group_hash) eq 'HASH' && exists( $group_hash->{_DB} ) )
2738 {
2739 push( @{ $merged_register{_DBA} }, $group_hash->{_DB} );
2740 }
2741 }
2742 }
2743  
2744 %registry_register = %merged_register;
2745
2746 my $count = $self->get_DBAdaptor_count() - $original_count;
2747 return $count >= 0 ? $count : 0;
2748 } ## end sub load_registry_from_multiple_dbs
2749  
2750 #
2751 # Web specific routines
2752 #
2753  
2754 =head2 set_default_track
2755  
2756 Sets a flag to say that that this species/group are a default track and do not
2757 need to be added as another web track.
2758  
2759 Arg [1] : name of the species to get the adaptors for in the registry.
2760 Arg [2] : name of the type to get the adaptors for in the registry.
2761 Example : $merged = Bio::EnsEMBL::Registry->set_default_track("Human","core");
2762 Returntype : none
2763 Exceptions : none
2764 Status : At Risk.
2765  
2766 =cut
2767  
2768 sub set_default_track {
2769 my ( $class, $species, $group ) = @_;
2770  
2771 $species = get_alias($species);
2772 $registry_register{'def_track'}{$species}{ lc($group) } = 1;
2773 return;
2774 }
2775  
2776 =head2 default_track
2777  
2778 Check flag to see if this is a default track
2779  
2780 Arg [1] : name of the species to get the adaptors for in the registry.
2781 Arg [2] : name of the type to get the adaptors for in the registry.
2782 Example : $merged = Bio::EnsEMBL::Registry->set_default_track("Human","core");
2783 Returntype : int
2784 Exceptions : none
2785 Status : At Risk.
2786  
2787 =cut
2788  
2789 sub default_track {
2790 my ( $class, $species, $group ) = @_;
2791  
2792 $species = get_alias($species);
2793 if (
2794 defined( $registry_register{'def_track'}{$species}{ lc($group) } ) )
2795 {
2796 return 1;
2797 }
2798  
2799 return 0;
2800 }
2801  
2802  
2803 =head2 add_new_tracks
2804  
2805 Will add new gene tracks to the configuration of the WEB server if they are
2806 not of the type default and the configuration already has genes in the display.
2807  
2808 Arg [1] : hash of the default configuration of the web page
2809 Returntype : none
2810 Exceptions : none
2811 Called by : UserConfig.pm
2812 Status : At Risk.
2813
2814 =cut
2815  
2816 sub add_new_tracks{
2817 my($class, $conf, $pos) = @_;
2818  
2819 my $start = 0;
2820 my $reg = $class;
2821 my $species_reg = $reg->get_alias($conf->{'species'},"nothrow");
2822 my %pars;
2823 # print STDERR "Species $species_reg check for default tracks\n";
2824 if(defined($species_reg)){
2825 foreach my $dba (@{$reg->get_all_DBAdaptors()}){
2826 if(!$reg->default_track($dba->species,$dba->group)){
2827 $pars{'available'} = "species ".$reg->get_alias($dba->species());
2828 $pars{'db_alias'} = $dba->group();
2829 # print STDERR "Adding new track for ".$dba->species."\t".$dba->group."\n";
2830 $conf->add_new_track_generictranscript('',$dba->group(), "black",$pos,%pars);
2831 $pos++;
2832 }
2833 }
2834 }
2835 return $pos;
2836  
2837 }
2838  
2839 =head2 no_version_check
2840
2841 getter/setter for whether to run the version checking
2842
2843 Arg[0] : (optional) int
2844 Returntype : int or undef if not set
2845 Exceptions : none
2846 Status : At Risk.
2847  
2848 =cut
2849
2850 sub no_version_check {
2851 my ( $self, $arg ) = @_;
2852 ( defined $arg )
2853 && ( $registry_register{'_no_version_check'} = $arg );
2854  
2855 return $registry_register{'_no_version_check'};
2856 }
2857  
2858 =head2 no_cache_warnings
2859  
2860 Arg[0] : boolean for turning the flag on and off
2861 Description : Turns off any warnings about not using caching in all available
2862 adaptors.
2863 Returntype : boolean Current status
2864 Exceptions : None
2865  
2866 =cut
2867  
2868 sub no_cache_warnings {
2869 my ($self, $arg) = @_;
2870 if(defined $arg) {
2871 $Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor::SILENCE_CACHE_WARNINGS = $arg;
2872 }
2873 return $Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor::SILENCE_CACHE_WARNINGS;
2874 }
2875  
2876
2877 =head2 version_check
2878
2879 run the database/API code version check for a DBAdaptor
2880
2881 Arg[0] : DBAdaptor to check
2882 Returntype : int 1 if okay, 0 if not the same
2883 Exceptions : none
2884 Status : At Risk.
2885  
2886 =cut
2887
2888
2889 sub version_check {
2890 my ( $self, $dba ) = @_;
2891  
2892 # Check the datbase and versions match
2893 # give warning if they do not.
2894 my $check = no_version_check();
2895  
2896 if ( (
2897 defined( $ENV{HOME} )
2898 and ( -e $ENV{HOME} . "/.ensemblapi_no_version_check" ) )
2899 or ( defined($check) and ( $check != 0 ) ) )
2900 {
2901 return 1;
2902 }
2903  
2904 my $mca =
2905 $self->get_adaptor( $dba->species(), $dba->group(),
2906 "MetaContainer" );
2907  
2908 my $database_version = 0;
2909 if ( defined($mca) ) {
2910 $database_version = $mca->get_schema_version();
2911 }
2912  
2913 if ( $database_version == 0 ) {
2914 # Try to work out the version
2915 if ( $dba->dbc()->dbname() =~ /^_test_db_/x ) {
2916 return 1;
2917 }
2918  
2919 # ensembl_metadata was unversioned prior to release 96
2920 # we now have multiple pattern for metadata db name (qrp and grch37) - valid until we merge those.
2921 if ( $dba->dbc()->dbname() =~ /ensembl_metadata(\_?(qrp|grch37)?)/s ) {
2922 return 1;
2923 }
2924 # ncbi_taxonomy was unversioned prior to release 100
2925 if ( $dba->dbc()->dbname() eq 'ncbi_taxonomy' ) {
2926 return 1;
2927 }
2928  
2929 if ( $dba->dbc()->dbname() =~ /(\d+)_\S+$/x ) {
2930 $database_version = $1;
2931 } elsif ( $dba->dbc()->dbname() =~ /ensembl_compara_(\d+)/x ) {
2932 $database_version = $1;
2933 } elsif ( $dba->dbc()->dbname() =~ /ensembl_help_(\d+)/x ) {
2934 $database_version = $1;
2935 } elsif ( $dba->dbc()->dbname() =~ / ensembl_metadata_(\d+) /msx ) {
2936 # Prior to release 96 metadata is supposed to be versionned on meta-1. Not the case since
2937 $database_version = $1;
2938 } elsif ( $dba->dbc()->dbname() =~ /ensembl_ontology_(\d+)/x ) {
2939 $database_version = $1;
2940 } elsif ( $dba->dbc()->dbname() =~ /ensembl_stable_ids_(\d+)/x ) {
2941 $database_version = $1;
2942 } elsif ( $dba->dbc()->dbname() =~ / ncbi_taxonomy_(\d+) /msx ) {
2943 $database_version = $1;
2944 } else {
2945 warn(
2946 sprintf(
2947 "No database version for database %s "
2948 . ". You must be using a post version 34 database "
2949 . "with version 34 or later code.\n"
2950 . "You need to update your database "
2951 . "or use the appropriate Ensembl software release "
2952 . "to ensure your script does not crash\n",
2953 $dba->dbc()->dbname() ) );
2954 }
2955 } ## end if ( $database_version...
2956  
2957 if ( $database_version != software_version() ) {
2958 warn(
2959 sprintf(
2960 "For %s there is a difference in the software release (%s) "
2961 . "and the database release (%s). "
2962 . "You should update one of these to ensure that your script "
2963 . "does not crash.\n",
2964 $dba->dbc()->dbname()."@".$dba->dbc()->host,
2965 software_version(), $database_version
2966 ) );
2967 return 0;
2968 }
2969  
2970 return 1; # Ok
2971 } ## end sub version_check
2972  
2973 =head2 get_all_species
2974  
2975 Arg [1] : String group type, such as core, or otherfeatures
2976 Description: Method for getting all valid species names found in available
2977 databases. This excludes the ancestral sequence databases, and
2978 any species from a non-core database. Specifying a group allows
2979 the list to apply to non-core database types.
2980 Example : my @species_names = @{ $reg->get_all_species() };
2981 Returntype : Listref of species names
2982
2983 =cut
2984  
2985 sub get_all_species {
2986 my ($self,$group) = @_;
2987 $group ||= 'core';
2988 my @species;
2989 foreach my $name (keys %{$registry_register{_SPECIES}}) {
2990 push @species, $name if (
2991 # limit species names to given db group and no ancestral dbs
2992 $registry_register{_SPECIES}->{$name}->{$group}
2993 && $name !~ /^ancestral/i
2994 );
2995 }
2996 return \@species;
2997 }
2998  
2999  
3000 =head2 get_species_and_object_type
3001  
3002 Description: Get the species name, object type (gene, transcript,
3003 translation, or exon etc.), and database type for a
3004 stable ID.
3005  
3006 Arg [1] : String stable_id
3007 The stable ID to find species and object type for.
3008  
3009 Arg [2] : String known_type (optional)
3010 The type of the stable ID, if it is known.
3011  
3012 Arg [3] : String known_species (optional)
3013 The species, if known
3014  
3015 Arg [4] : String known_db_type (optional)
3016 The database type, if known
3017
3018 Example : my $stable_id = 'ENST00000326632';
3019  
3020 my ( $species, $object_type, $db_type ) =
3021 $registry->get_species_and_object_type($stable_id);
3022  
3023 my $adaptor =
3024 $registry->get_adaptor( $species, $db_type,
3025 $object_type );
3026  
3027 my $object = $adaptor->fetch_by_stable_id($stable_id);
3028  
3029 Return type: Array consisting of the species name, object type,
3030 and database type. The array may be empty if no
3031 match is found.
3032  
3033 Exceptions : none
3034 Status : At Risk.
3035  
3036 =cut
3037  
3038 my %stable_id_stmts = (
3039 gene => 'SELECT m.meta_value '
3040 . 'FROM %1$s.gene '
3041 . 'JOIN %1$s.seq_region USING (seq_region_id) '
3042 . 'JOIN %1$s.coord_system USING (coord_system_id) '
3043 . 'JOIN %1$s.meta m USING (species_id) '
3044 . 'WHERE stable_id = ? '
3045 . 'AND m.meta_key = "species.production_name"',
3046 transcript => 'SELECT m.meta_value '
3047 . 'FROM %1$s.transcript '
3048 . 'JOIN %1$s.seq_region USING (seq_region_id) '
3049 . 'JOIN %1$s.coord_system USING (coord_system_id) '
3050 . 'JOIN %1$s.meta m USING (species_id) '
3051 . 'WHERE stable_id = ? '
3052 . 'AND m.meta_key = "species.production_name"',
3053 exon => 'SELECT m.meta_value '
3054 . 'FROM %1$s.exon '
3055 . 'JOIN %1$s.seq_region USING (seq_region_id) '
3056 . 'JOIN %1$s.coord_system USING (coord_system_id) '
3057 . 'JOIN %1$s.meta m USING (species_id) '
3058 . 'WHERE stable_id = ? '
3059 . 'AND m.meta_key = "species.production_name"',
3060 translation => 'SELECT m.meta_value '
3061 . 'FROM %1$s.translation tl '
3062 . 'JOIN %1$s.transcript USING (transcript_id) '
3063 . 'JOIN %1$s.seq_region USING (seq_region_id) '
3064 . 'JOIN %1$s.coord_system USING (coord_system_id) '
3065 . 'JOIN %1$s.meta m USING (species_id) '
3066 . 'WHERE tl.stable_id = ? '
3067 . 'AND m.meta_key = "species.production_name"',
3068 operon => 'SELECT m.meta_value '
3069 . 'FROM %1$s.operon '
3070 . 'JOIN %1$s.seq_region USING (seq_region_id) '
3071 . 'JOIN %1$s.coord_system USING (coord_system_id) '
3072 . 'JOIN %1$s.meta m USING (species_id) '
3073 . 'WHERE stable_id = ? '
3074 . 'AND m.meta_key = "species.production_name"',
3075 operontranscript => 'SELECT m.meta_value '
3076 . 'FROM %1$s.operon_transcript '
3077 . 'JOIN %1$s.seq_region USING (seq_region_id) '
3078 . 'JOIN %1$s.coord_system USING (coord_system_id) '
3079 . 'JOIN %1$s.meta m USING (species_id) '
3080 . 'WHERE stable_id = ? '
3081 . 'AND m.meta_key = "species.production_name"',
3082
3083 );
3084  
3085 my %compara_stable_id_stmts = (
3086 genetree => 'SELECT 1 FROM %1$s.gene_tree_root WHERE stable_id =?',
3087 family => 'SELECT 1 from %1$s.family where stable_id = ?',
3088 );
3089  
3090  
3091 sub get_species_and_object_type {
3092 my ($self, $stable_id, $known_type, $known_species, $known_db_type, $force_long_lookup, $use_archive) = @_;
3093  
3094 #get the stable_id lookup database adaptor
3095  
3096 my $stable_ids_dba = $self->get_DBAdaptor("multi", "stable_ids", 1);
3097  
3098 if ($stable_ids_dba && ! $force_long_lookup) {
3099 return $self->_lookup_db_get_species_and_object_type($stable_id, $known_type, $known_species, $known_db_type, $use_archive);
3100 }
3101 else {
3102 if(defined $known_type) {
3103 my $lc_known_type = lc $known_type;
3104 if(!exists $stable_id_stmts{$lc_known_type} && ! exists $compara_stable_id_stmts{$lc_known_type}) {
3105 return;
3106 }
3107 }
3108
3109 $known_db_type = 'core' if ! $known_db_type;
3110
3111 my %get_adaptors_args = ('-GROUP' => $known_db_type);
3112 $get_adaptors_args{'-species'} = $known_species if $known_species;
3113  
3114 my @dbas =
3115 sort { $a->dbc->host cmp $b->dbc->host || $a->dbc->port <=> $b->dbc->port }
3116 grep { $_->dbc->dbname !~ m{ \A ensembl_metadata | ncbi_taxonomy }msx }
3117 @{$self->get_all_DBAdaptors(%get_adaptors_args)};
3118
3119 foreach my $dba (@dbas) {
3120 my @results;
3121 my $dba_adaptor_type = $group2adaptor{$dba->group()};
3122 if($dba_adaptor_type eq 'Bio::EnsEMBL::DBSQL::DBAdaptor') {
3123 @results = $self->_core_get_species_and_object_type($stable_id, $known_type, $dba);
3124 }
3125 elsif($dba_adaptor_type eq 'Bio::EnsEMBL::Compara::DBSQL::DBAdaptor') {
3126 @results = $self->_compara_get_species_and_object_type($stable_id, $known_type, $dba);
3127 }
3128 return @results if scalar(@results) > 0;
3129 } ## end foreach my $dba ( sort { $a...})
3130 }
3131
3132 return;
3133 } ## end sub get_species_and_object_type
3134  
3135 sub _lookup_db_get_species_and_object_type {
3136 my ($self, $stable_id, $known_type, $known_species, $known_db_type, $use_archive) = @_;
3137  
3138 my $retired;
3139 my $stable_ids_dba = $self->get_DBAdaptor("multi", "stable_ids", 1);
3140  
3141 my ($species, $type, $db_type) = $self->stable_id_lookup($stable_id, $known_type, $known_species, $known_db_type);
3142  
3143 if (!$species && $use_archive) {
3144 ($species, $type, $db_type) = $self->archive_id_lookup($stable_id, $known_type, $known_species, $known_db_type);
3145 $retired = 1 if $species;
3146 }
3147  
3148 return ($species ,$type, $db_type, $retired);
3149 } ## end sub _lookup_db_get_species_and_object_type
3150  
3151  
3152 sub stable_id_lookup {
3153 my ($self, $stable_id, $known_type, $known_species, $known_db_type) = @_;
3154 my $retired;
3155 my $stable_ids_dba = $self->get_DBAdaptor("multi", "stable_ids", 1);
3156  
3157 my $statement = 'SELECT name, object_type, db_type FROM stable_id_lookup join species using(species_id) WHERE stable_id = ?';
3158 if ($known_species) {
3159 $statement .= ' AND name = ?';
3160 }
3161 if ($known_db_type) {
3162 $statement .= ' AND db_type = ?';
3163 }
3164 if ($known_type) {
3165 $statement .= ' AND object_type = ?';
3166 }
3167  
3168 my $sth = $stable_ids_dba->dbc()->prepare($statement);
3169 $sth->bind_param(1, $stable_id, SQL_VARCHAR);
3170 my $param_count = 1;
3171 if ($known_species) {
3172 $known_species = $self->get_alias($known_species);
3173 $param_count++;
3174 $sth->bind_param($param_count, $known_species, SQL_VARCHAR);
3175 }
3176 if ($known_db_type) {
3177 $param_count++;
3178 $sth->bind_param($param_count, $known_db_type, SQL_VARCHAR);
3179 }
3180 if ($known_type) {
3181 $param_count++;
3182 $sth->bind_param($param_count, $known_type, SQL_VARCHAR);
3183 }
3184 $sth->execute();
3185 my ($species, $type, $db_type) = $sth->fetchrow_array();
3186 $sth->finish();
3187  
3188 return ($species ,$type, $db_type);
3189 }
3190  
3191 sub archive_id_lookup {
3192 my ($self, $stable_id, $known_type, $known_species, $known_db_type) = @_;
3193 my $retired;
3194 my $stable_ids_dba = $self->get_DBAdaptor("multi", "stable_ids", 1);
3195  
3196 my $archive_statement = 'SELECT name, object_type, db_type FROM archive_id_lookup join species using(species_id) WHERE archive_id = ?';
3197 if ($known_species) {
3198 $archive_statement .= ' AND name = ?';
3199 }
3200 if ($known_db_type) {
3201 $archive_statement .= ' AND db_type = ?';
3202 }
3203 if ($known_type) {
3204 $archive_statement .= ' AND object_type = ?';
3205 }
3206  
3207 my $archive_sth = $stable_ids_dba->dbc()->prepare($archive_statement);
3208 $archive_sth->bind_param(1, $stable_id, SQL_VARCHAR);
3209 my $param_count = 1;
3210 if ($known_species) {
3211 $known_species = $self->get_alias($known_species);
3212 $param_count++;
3213 $archive_sth->bind_param($param_count, $known_species, SQL_VARCHAR);
3214 }
3215 if ($known_db_type) {
3216 $param_count++;
3217 $archive_sth->bind_param($param_count, $known_db_type, SQL_VARCHAR);
3218 }
3219 if ($known_type) {
3220 $param_count++;
3221 $archive_sth->bind_param($param_count, $known_type, SQL_VARCHAR);
3222 }
3223  
3224 $archive_sth->execute();
3225 my ($species, $type, $db_type) = $archive_sth->fetchrow_array();
3226 $archive_sth->finish();
3227  
3228 return ($species ,$type, $db_type);
3229 }
3230  
3231  
3232 # A level of abstraction because we need to test the stable_id as-is and then
3233 # try to chop off a version id if nothing is return, and try again
3234  
3235 sub _core_get_species_and_object_type {
3236 my ($self, $stable_id, $known_type, $dba) = @_;
3237  
3238 # Try looking up the species with the stable_is, as-is
3239 my @results = $self->_core_get_species_and_object_type_worker($stable_id, $known_type, $dba);
3240  
3241 if(@results) {
3242 return @results;
3243 } elsif(my $vindex = rindex($stable_id, '.')) {
3244 return $self->_core_get_species_and_object_type_worker(substr($stable_id,0,$vindex), $known_type, $dba)
3245 if(substr($stable_id,$vindex+1) =~ /^\d+$/);
3246 }
3247  
3248 return;
3249  
3250 }
3251  
3252 # Loop over a known set of object types for a core DB until we find a hit
3253 sub _core_get_species_and_object_type_worker {
3254 my ($self, $stable_id, $known_type, $dba) = @_;
3255 my @types = defined $known_type ? ($known_type) : ('Gene', 'Transcript', 'Translation', 'Exon', 'Operon', 'OperonTranscript');
3256 my ($species, $final_type, $final_db_type);
3257 foreach my $type (@types) {
3258 my $statement = sprintf $stable_id_stmts{lc $type}, $dba->dbc->dbname;
3259 my $sth = $dba->dbc()->prepare($statement);
3260 $sth->bind_param(1, $stable_id, SQL_VARCHAR);
3261 $sth->execute;
3262 $species = $sth->fetchall_arrayref->[0][0];
3263 $sth->finish;
3264 if(defined $species) {
3265 $final_type = $type;
3266 $final_db_type = $dba->group();
3267 last;
3268 }
3269 }
3270 $dba->dbc->disconnect_if_idle(); # always disconnect after lookup
3271 return ($species, $final_type, $final_db_type) if defined $species;
3272 return;
3273 }
3274  
3275 # A level of abstraction because we need to test the stable_id as-is and then
3276 # try to chop off a version id if nothing is return, and try again
3277  
3278 sub _compara_get_species_and_object_type {
3279 my ($self, $stable_id, $known_type, $dba) = @_;
3280  
3281 # Try looking up the species with the stable_is, as-is
3282 my @results = $self->_compara_get_species_and_object_type_worker($stable_id, $known_type, $dba);
3283  
3284 if(@results) {
3285 return @results;
3286 } elsif(my $vindex = rindex($stable_id, '.')) {
3287 return $self->_compara_get_species_and_object_type_worker(substr($stable_id,0,$vindex), $known_type, $dba)
3288 if(substr($stable_id,$vindex+1) =~ /^\d+$/);
3289 }
3290  
3291 return;
3292  
3293 }
3294  
3295 # Loop over a known set of object types for a compara DB until we find a hit
3296 sub _compara_get_species_and_object_type_worker {
3297 my ($self, $stable_id, $known_type, $dba) = @_;
3298 my @types = defined $known_type ? ($known_type) : ('GeneTree');
3299 my ($species, $final_type, $final_db_type);
3300 foreach my $type (@types) {
3301 my $statement = sprintf $compara_stable_id_stmts{lc $type}, $dba->dbc->dbname;
3302 my $sth = $dba->dbc()->prepare($statement);
3303 $sth->bind_param(1, $stable_id, SQL_VARCHAR);
3304 $sth->execute;
3305 my $found = $sth->fetchall_arrayref->[0][0];
3306 $sth->finish;
3307 if(defined $found) {
3308 $species = $dba->species();
3309 $final_type = $type;
3310 $final_db_type = $dba->group();
3311 last;
3312 }
3313 }
3314 $dba->dbc->disconnect_if_idle(); # always disconnect after lookup
3315 return ($species, $final_type, $final_db_type) if defined $species;
3316 return;
3317 }
3318  
3319 1;