EnsEMBL/Registry.pm untested

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