EnsEMBL/Utils/ConfigRegistry.pm untested

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