EnsEMBL/Utils/ConfigRegistry.pm untested

filter: Statementstmt 0.0% 0 / 139 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 259 CC 33 SCAR 59.6
CC
33
Cov
0%
gen_load
812.0
find_unique_species
20.0
add_alias
6.0
CRAP 387.9
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::Utils::ConfigRegistry;
34  
35 =head1 SYNOPSIS
36  
37  
38 Bio::EnsEMBL::Utils::ConfigRegistry->load_core($dba);
39  
40  
41 =head1 DESCRIPTION
42  
43 The ConfigRegistry will "Register" a set of adaptors based on the type
44 of database that is being loaded.
45  
46 =head1 METHODS
47  
48 =cut
49  
50 package Bio::EnsEMBL::Utils::ConfigRegistry;
51 $Bio::EnsEMBL::Utils::ConfigRegistry::VERSION = '114.0.0';
52 use strict;
53 use warnings;
54  
55 use Bio::EnsEMBL::Registry;
56 my $reg = "Bio::EnsEMBL::Registry";
57 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
58 use Bio::EnsEMBL::DBSQL::DBConnection;
59 use Bio::EnsEMBL::DBSQL::DBAdaptor;
60  
61 use Bio::EnsEMBL::Utils::Exception qw(warning throw);
62  
63  
64  
65 sub gen_load {
66 my ($dba) = @_;
67 my $pre_hook;
68 my $config_sub;
69  
70 # At some point we hope to set the group in the DBadaptor, hence this
71 # long check etc. should be simpler.
72  
73 if ( $dba->isa('Bio::EnsEMBL::Compara::DBSQL::DBAdaptor') ) {
74 if ( !defined( $dba->group() ) ) {
75 $dba->group('compara');
76 }
77 $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_compara;
78 } elsif ( $dba->isa('Bio::EnsEMBL::Lite::DBAdaptor') ) {
79 if ( !defined( $dba->group() ) ) {
80 $dba->group('lite');
81 }
82 $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_lite;
83 } elsif ( $dba->isa('Bio::EnsEMBL::Pipeline::DBSQL::DBAdaptor') ) {
84 if ( !defined( $dba->group() ) ) {
85 $dba->group('pipeline');
86 }
87 $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_pipeline;
88 } elsif ( $dba->isa('Bio::EnsEMBL::Hive::DBSQL::DBAdaptor') ) {
89 if ( !defined( $dba->group() ) ) {
90 $dba->group('hive');
91 }
92 $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_hive;
93 } elsif ( $dba->isa('Bio::EnsEMBL::Variation::DBSQL::DBAdaptor') ) {
94 if ( !defined( $dba->group() ) ) {
95 $dba->group('variation');
96 }
97 $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_variation;
98 } elsif ( $dba->isa('Bio::EnsEMBL::Funcgen::DBSQL::DBAdaptor') ) {
99 if ( !defined( $dba->group() ) ) {
100 $dba->group('funcgen');
101 }
102 $pre_hook = \&Bio::EnsEMBL::Utils::ConfigRegistry::pre_funcgen_hook;
103 $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_funcgen;
104 } elsif ( $dba->isa('Bio::EnsEMBL::DBSQL::OntologyDBAdaptor') || $dba->isa('Bio::Ensembl::DBSQL::OntologyTermAdaptor') ) {
105 if ( !defined( $dba->group() ) ) {
106 $dba->group('ontology');
107 }
108 $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_ontology;
109 } elsif ( $dba->isa('Bio::EnsEMBL::DBSQL::DBAdaptor') ) {
110 #vega uses the core DBAdaptor so test if vega is in the dbname
111 if ( !defined( $dba->group() ) ) {
112 $dba->group('core');
113 }
114  
115 if ( $dba->group eq "estgene" ) {
116 $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_estgene;
117 } elsif ( $dba->group eq "otherfeatures" ) {
118 $config_sub =
119 \&Bio::EnsEMBL::Utils::ConfigRegistry::load_otherfeatures;
120 } elsif ( $dba->group eq "rnaseq" ) {
121 $config_sub =
122 \&Bio::EnsEMBL::Utils::ConfigRegistry::load_rnaseq;
123 } elsif ( $dba->group eq 'vega' || $dba->group eq 'vega_update' ) {
124 $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_vega;
125 } else {
126 $config_sub = \&Bio::EnsEMBL::Utils::ConfigRegistry::load_core;
127 }
128  
129 } else {
130 # none standard DBA adaptor
131 if ( !defined( $dba->group() ) ) {
132 $dba->group('none_standard');
133 }
134 $config_sub =
135 \&Bio::EnsEMBL::Utils::ConfigRegistry::load_and_attach_dnadb_to_core;
136 # throw("Unknown DBAdaptor type $dba\n");
137 }
138
139 #Run the pre-hook if one was defined
140 $pre_hook->($dba) if $pre_hook;
141  
142 # return if the connection and species, group are the same
143  
144 if ( defined( $dba->species ) ) {
145 my $db_reg = $reg->get_DBAdaptor( $dba->species, $dba->group, 1 );
146 if ( defined($db_reg) ) {
147 if ( $dba->dbc->equals( $db_reg->dbc ) ) { return $db_reg }
148 else {
149 my $msg =
150 sprintf( 'WARN: Species (%s) and group (%s) '
151 . 'same for two seperate databases',
152 $dba->species(), $dba->group() );
153  
154 warn "${msg}\nModify species name for one of these\n";
155 $dba->species(
156 find_unique_species( $dba->species, $dba->group ) );
157 }
158 }
159 } else { # no species
160
161 my @db_reg =
162 @{ $reg->get_all_DBAdaptors_by_connection( $dba->dbc ) };
163  
164 foreach my $db_adaptor (@db_reg) {
165 if ( $db_adaptor->group eq $dba->group ) {
166 # found same db connection and group
167 return $db_adaptor;
168 }
169 }
170  
171 $dba->species( find_unique_species( "DEFAULT", $dba->group ) );
172 if ( $dba->species ne "DEFAULT" ) {
173 warn "WARN: For multiple species "
174 . "use species attribute in DBAdaptor->new()\n";
175 }
176 }
177  
178 Bio::EnsEMBL::Registry->add_DBAdaptor( $dba->species(), $dba->group(),
179 $dba );
180  
181 #call the loading subroutine. (add the adaptors to the DBAdaptor)
182 &{$config_sub}($dba);
183  
184 return $dba;
185 } ## end sub gen_load
186  
187  
188  
189 sub find_unique_species {
190 my ( $species, $group ) = @_;
191  
192 $reg->add_alias( $species, $species );
193  
194 my $i = 0;
195 my $free = 0;
196  
197 while ( !$free ) {
198 if ( $i == 0 ) {
199 if ( !defined( $reg->get_DBAdaptor( $species, $group ) ) ) {
200 $free = 1;
201 $i = "";
202 } else {
203 $i = 1;
204 }
205 } else {
206 # set needed self alias
207 $reg->add_alias( $species . $i, $species . $i );
208  
209 if ( !defined( $reg->get_DBAdaptor( $species . $i, $group ) ) ) {
210 $free = 1;
211 } else {
212 $i++;
213 }
214 }
215 }
216  
217 $species .= $i;
218 return ($species);
219 } ## end sub find_unique_species
220  
221  
222  
223 sub load_adaptors {
224 my ($dba) = @_;
225  
226 my %pairs = %{ $dba->get_available_adaptors() };
227  
228 while ( my ( $key, $value ) = each(%pairs) ) {
229 Bio::EnsEMBL::Registry->add_adaptor( $dba->species(), $dba->group(),
230 $key, $value );
231 }
232 }
233  
234 sub load_and_attach_dnadb_to_core {
235 my ($dba) = @_;
236  
237 load_adaptors($dba);
238 $reg->add_DNAAdaptor( $dba->species(), $dba->group(), $dba->species(),
239 'core' );
240 }
241  
242  
243 =head2 load_core
244 Arg [1] : DBAdaptor with DBConnection already attached
245 Returntype : DBAdaptor
246 Exceptions : none
247 =cut
248 sub load_core { load_adaptors(@_) }
249  
250  
251 #
252 # 1) core. no need to add dnadb
253 # 2) not core add dnadb
254 # 3)
255 #
256  
257 =head2 load_compara
258 Arg [1] : DBAdaptor with DBConnection already attached
259 Returntype : DBAdaptor
260 Exceptions : none
261 =cut
262 sub load_compara { load_adaptors(@_) }
263  
264 =head2 load_hive
265 Arg [1] : DBAdaptor with DBConnection already attached
266 Returntype : DBAdaptor
267 Exceptions : none
268 =cut
269 sub load_hive { load_adaptors(@_) }
270  
271 =head2 load_pipeline
272 Arg [1] : DBAdaptor with DBConnection already attached
273 Returntype : DBAdaptor
274 Exceptions : none
275 =cut
276 sub load_pipeline { load_adaptors(@_) }
277  
278 =head2 load_SNP
279 Arg [1] : DBAdaptor with DBConnection already attached
280 Returntype : DBAdaptor
281 Exceptions : none
282 =cut
283 sub load_SNP { load_adaptors(@_) }
284  
285 sub load_haplotype { load_adaptors(@_) }
286  
287 sub load_ontology { load_adaptors(@_) }
288  
289  
290 # these that need to attach to the core to get the sequence data
291  
292 sub load_estgene { load_and_attach_dnadb_to_core(@_) }
293  
294 sub load_variation { load_and_attach_dnadb_to_core(@_) }
295  
296 sub load_funcgen { load_and_attach_dnadb_to_core(@_) }
297  
298 =head2 load_otherfeatures
299 Arg [1] : DBAdaptor with DBConnection alredy attached
300 Returntype : DBAdaptor
301 Exceptions : none
302  
303 =cut
304 sub load_otherfeatures { load_and_attach_dnadb_to_core(@_) }
305  
306 sub load_rnaseq { load_and_attach_dnadb_to_core(@_) }
307  
308 =head2 load_vega
309 Arg [1] : DBAdaptor with DBConnection already attached
310 Returntype : DBAdaptor
311 Exceptions : none
312 =cut
313 sub load_vega { load_and_attach_dnadb_to_core(@_) }
314  
315  
316 sub add_alias {
317 my ( $class, @args ) = @_;
318  
319 my ( $species, $aliases ) = rearrange( [qw(SPECIES ALIAS)], @args );
320  
321 # Make sure it exists itself
322 Bio::EnsEMBL::Registry->add_alias( $species, $species );
323  
324 if ( defined($aliases) ) {
325 foreach my $ali (@$aliases) {
326 Bio::EnsEMBL::Registry->add_alias( $species, $ali );
327 }
328 }
329 }
330  
331 # WARNING: "CONVENIENCE METHOD" for retriving the species name when one was
332 # not set. Regulation DB requirement
333 sub pre_funcgen_hook {
334 my ($dba) = @_;
335 if(! $dba->species() ) {
336 warn "Setting name";
337 my $name = $dba->dbc()->sql_helper()->execute_single_result(
338 -SQL => 'select meta_value from meta where meta_key =?',
339 -PARAMS => ['species.production_name'],
340 );
341 $dba->dbc()->disconnect_if_idle();
342 $dba->species($name);
343 }
344 return;
345 }
346  
347 #
348 # overwrite/load new types. Done this way to enable no changes to CVS for
349 # external users. External users should add there own "GROUPS" in the file
350 # User_defined_load.
351 #
352  
353 eval{ require Bio::EnsEMBL::Utils::User_defined_load };
354  
355 1;