EnsEMBL/DBSQL/DBAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 230 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 38 Subroutinesub 0.0% 0 / 45 Podpod 0.0% 0 / 35 totaltotal 0.0% 0 / 426 CC 40 SCAR 30.9
CC
40
Cov
0%
new
110.0
add_ExternalFeatureAdaptor
42.0
dbc
20.0
CRAP 22.1
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::DBSQL::DBAdaptor
34  
35 =head1 SYNOPSIS
36  
37 $db = Bio::EnsEMBL::DBSQL::DBAdaptor->new(
38 -user => 'root',
39 -dbname => 'pog',
40 -host => 'caldy',
41 -driver => 'mysql'
42 );
43  
44 $gene_adaptor = $db->get_GeneAdaptor();
45  
46 $gene = $gene_adaptor->fetch_by_stable_id($stable_id);
47  
48 $slice =
49 $db->get_SliceAdaptor()->fetch_by_chr_start_end( 'X', 1, 10000 );
50  
51 =head1 DESCRIPTION
52  
53 Formerly this class provided database connectivity and a means
54 to retrieve object adaptors. This class is now provided for
55 convenience and backwards compatibility, and delegates its connection
56 responsibilities to the DBConnection class (no longer inherited from)
57 and its object adaptor retrieval to the static Bio::EnsEMBL::Registry.
58  
59 Please use Bio::EnsEMBL::Registry to retrieve object adaptors.
60  
61 =head1 METHODS
62  
63 =cut
64  
65 package Bio::EnsEMBL::DBSQL::DBAdaptor;
66 $Bio::EnsEMBL::DBSQL::DBAdaptor::VERSION = '114.0.0_50'; # TRIAL
67 $Bio::EnsEMBL::DBSQL::DBAdaptor::VERSION = '114.0.050';
68 use strict;
69  
70 use Bio::EnsEMBL::DBSQL::DBConnection;
71 use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
72 use Bio::EnsEMBL::Utils::SeqRegionCache;
73 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
74 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
75 use Bio::EnsEMBL::Utils::Scalar qw(check_ref scope_guard);
76 use Bio::EnsEMBL::Utils::ConfigRegistry;
77  
78  
79 my $REGISTRY = "Bio::EnsEMBL::Registry";
80 require Bio::EnsEMBL::Registry;
81  
82 =head2 new
83  
84 Arg [-DNADB]: (optional) Bio::EnsEMBL::DBSQL::DBAdaptor DNADB
85 All sequence, assembly, contig information etc, will
86 be retrieved from this database instead.
87  
88 Arg [-NO_CACHE]: (optional) int 1
89 This option will turn off caching for slice features,
90 so, every time a set of features is retrieved,
91 they will come from the database instead of the
92 cache. This option is only recommended for advanced
93 users, specially if you need to store and retrieve
94 features. It might reduce performance when querying
95 the database if not used properly. If in doubt, do
96 not use it or ask in the developer mailing list.
97  
98 Arg [-ADD_ALIASES]: (optional) boolean
99 Used to automatically load aliases for this
100 species into the Registry upon loading.
101  
102 Arg [-ADD_SPECIES_ID]: (optional) boolean
103 Used to automatically load the species id
104 based on the species if none is defined.
105  
106 Arg [..] : Other args are passed to superclass
107 Bio::EnsEMBL::DBSQL::DBConnection
108  
109 Example : $db = new Bio::EnsEMBL::DBSQL::DBAdaptor(
110 -user => 'root',
111 -dbname => 'pog',
112 -host => 'caldy',
113 -driver => 'mysql'
114 );
115  
116 $db = new Bio::EnsEMBL::DBSQL::DBAdaptor(
117 -species => 'Homo_sapiens',
118 -group => 'core',
119 -user => 'root',
120 -dbname => 'pog',
121 -host => 'caldy',
122 -driver => 'mysql'
123 );
124  
125 $db = new Bio::EnsEMBL::DBSQL::DBAdaptor(
126 -species => 'staphylococcus_aureus',
127 -group => 'core',
128 -user => 'root',
129 -dbname => 'staphylococcus_collection_1_52_1a',
130 -multispecies_db => 1,
131 -host => 'caldy',
132 -driver => 'mysql'
133 );
134  
135 Description: Constructor for DBAdaptor.
136 Returntype : Bio::EnsEMBL::DBSQL::DBAdaptor
137 Exceptions : none
138 Caller : general
139 Status : Stable
140  
141 =cut
142  
143 sub new {
144 my ( $class, @args ) = @_;
145  
146 my $self = bless {}, $class;
147  
148 my ( $is_multispecies, $species, $species_id, $group, $con, $dnadb,
149 $no_cache, $dbname, $add_aliases, $add_species_id )
150 = rearrange( [
151 'MULTISPECIES_DB', 'SPECIES', 'SPECIES_ID', 'GROUP',
152 'DBCONN', 'DNADB', 'NO_CACHE', 'DBNAME',
153 'ADD_ALIASES', 'ADD_SPECIES_ID'
154 ],
155 @args
156 );
157  
158 if ( defined($con) ) { $self->dbc($con) }
159 else {
160 if(! defined $dbname) {
161 throw "-DBNAME is a required parameter when creating a DBAdaptor";
162 }
163 $self->dbc( new Bio::EnsEMBL::DBSQL::DBConnection(@args) );
164 }
165  
166 if ( defined($species) ) { $self->species($species); }
167 if ( defined($group) ) { $self->group($group) }
168  
169 $self = Bio::EnsEMBL::Utils::ConfigRegistry::gen_load($self);
170  
171 if (defined $species_id) {
172 $self->species_id($species_id);
173 } elsif ($add_species_id and defined $species) {
174 $self->find_and_add_species_id();
175 } else {
176 $self->species_id(1);
177 }
178  
179 $self->is_multispecies( defined($is_multispecies)
180 && $is_multispecies == 1 );
181  
182 if ( defined($dnadb) ) { $self->dnadb($dnadb) }
183 if ( $no_cache ) { $self->no_cache($no_cache) }
184 if ( $add_aliases ) { $self->find_and_add_aliases($add_aliases) }
185  
186 return $self;
187 } ## end sub new
188  
189 =head2 clear_caches
190  
191 Example : $dba->clear_caches();
192 Description : Loops through all linked adaptors and clears their
193 caches if C<clear_cache()> is implemented. Not all caches
194 are cleared & the DBAdaptor instance should be removed from
195 the registry to clear these remaining essential caches.
196 Returntype : None
197 Exceptions : None
198  
199 =cut
200  
201 sub clear_caches {
202 my ($self) = @_;
203 my $adaptors = $REGISTRY->get_all_adaptors(
204 $self->species(), $self->group());
205 foreach my $adaptor (@{$adaptors}) {
206 if($adaptor->can('clear_cache')) {
207 $adaptor->clear_cache();
208 }
209 }
210 return;
211 }
212  
213 =head2 find_and_add_aliases
214  
215 Example : $dba->find_and_add_aliases();
216 Description : When executed we delegate to the find_and_add_aliases
217 method in Bio::EnsEMBL::Registry which scans the
218 database's MetaContainer for species.alias entries
219 indicating alternative names for this species. This
220 is best executed on a core DBAdaptor instance.
221 Returntype : None
222 Exceptions : None
223  
224 =cut
225  
226 sub find_and_add_aliases {
227 my ($self) = @_;
228 $REGISTRY->find_and_add_aliases(-ADAPTOR => $self);
229 return;
230 }
231  
232 =head2 find_and_add_species_id
233  
234 Description :
235 Returntype : None
236 Exceptions : None
237  
238 =cut
239  
240 sub find_and_add_species_id {
241 my ($self) = @_;
242 my $species = $self->species;
243 defined $species or throw "Undefined species";
244  
245 my $dbc = $self->dbc;
246 my $sth = $dbc->prepare(sprintf "SELECT DISTINCT species_id FROM %s.meta " .
247 "WHERE meta_key='species.alias' AND INSTR(meta_value, ?) > 0",
248 $dbc->db_handle->quote_identifier($dbc->dbname));
249 $sth->bind_param(1, "$species");
250 $sth->execute() or
251 throw "Error querying for species_id: perhaps the DB doesn't have a meta table?\n" .
252 "$DBI::err .... $DBI::errstr\n";
253  
254 my $species_id;
255 $sth->bind_columns(\$species_id);
256 $sth->fetch;
257  
258 throw "Undefined species_id" unless defined $species_id;
259 throw "Something wrong retrieving the species_id"
260 unless $species_id >= 1;
261  
262 $self->species_id($species_id);
263 return;
264 }
265  
266 =head2 dbc
267  
268 Arg[1] : (optional) Bio::EnsEMBL::DBSQL::DBConnection
269  
270 Example : $dbc = $dba->dbc();
271 Description: Getter/Setter for DBConnection.
272 Returntype : Bio::EnsEMBL::DBSQL::DBConnection
273 Exceptions : throws if argument not a Bio::EnsEMBL::DBSQL::DBConnection
274 Caller : general
275 Status : Stable
276  
277 =cut
278  
279 sub dbc{
280 my $self = shift;
281
282 if(@_){
283 my $arg = shift;
284 if(defined($arg)){
285 if(!$arg->isa('Bio::EnsEMBL::DBSQL::DBConnection')){
286 throw("$arg is no a DBConnection\n");
287 }
288 }
289 $self->{_dbc} = $arg;
290 }
291 return $self->{_dbc};
292 }
293  
294 =head2 add_db_adaptor
295  
296 Arg [1] : string $name
297 the name of the database to attach to this database
298 Arg [2] : Bio::EnsEMBL::DBSQL::DBConnection
299 the db adaptor to attach to this database
300 Example : $db->add_db_adaptor('lite', $lite_db_adaptor);
301 Description: Attaches another database instance to this database so
302 that it can be used in instances where it is required.
303 Returntype : none
304 Exceptions : none
305 Caller : EnsWeb
306 Status : At Risk
307  
308 =cut
309  
310 sub add_db_adaptor {
311 my ($self, $name, $adaptor) = @_;
312  
313 unless($name && $adaptor && ref $adaptor) {
314 throw('adaptor and name arguments are required');
315 }
316  
317 $REGISTRY->add_db($self, $name, $adaptor);
318  
319 }
320  
321  
322 =head2 remove_db_adaptor
323  
324 Arg [1] : string $name
325 the name of the database to detach from this database.
326 Example : $lite_db = $db->remove_db_adaptor('lite');
327 Description: Detaches a database instance from this database and returns
328 it.
329 Returntype : none
330 Exceptions : none
331 Caller : ?
332 Status : At Risk
333  
334 =cut
335  
336 sub remove_db_adaptor {
337 my ($self, $name) = @_;
338 return $REGISTRY->remove_db($self, $name);
339 }
340  
341  
342 =head2 get_all_db_adaptors
343  
344 Arg [1] : none
345 Example : @attached_dbs = values %{$db->get_all_db_adaptors()};
346 Description: returns all of the attached databases as
347 a hash reference of key/value pairs where the keys are
348 database names and the values are the attached databases
349 Returntype : hash reference with Bio::EnsEMBL::DBSQL::DBConnection values
350 Exceptions : none
351 Caller : Bio::EnsEMBL::DBSQL::ProxyAdaptor
352 Status : At Risk
353 : please use Bio::EnsEMBL::Registry->get_all_db_adaptors
354  
355 =cut
356  
357 sub get_all_db_adaptors {
358 my ($self) = @_;
359 return $REGISTRY->get_all_db_adaptors($self);
360 }
361  
362  
363  
364 =head2 get_db_adaptor
365  
366 Arg [1] : string $name
367 the name of the attached database to retrieve
368 Example : $lite_db = $db->get_db_adaptor('lite');
369 Description: returns an attached db adaptor of name $name or undef if
370 no such attached database exists
371 Returntype : Bio::EnsEMBL::DBSQL::DBConnection
372 Exceptions : none
373 Caller : ?
374 Status : At Risk
375 : please use Bio::EnsEMBL::Registry->get_db_adaptors
376  
377 =cut
378  
379 sub get_db_adaptor {
380 my ($self, $name) = @_;
381  
382 return $REGISTRY->get_db($self, $name);
383 }
384  
385 =head2 get_available_adaptors
386  
387 Example : my %pairs = %{$dba->get_available_adaptors()};
388 Description: gets a hash of the available adaptors
389 ReturnType : reference to a hash
390 Exceptions : none
391 Caller : Bio::EnsEMBL::Utils::ConfigRegistry
392 Status : Stable
393  
394 =cut
395  
396 sub get_available_adaptors {
397 my $adaptors = {
398 # Firstly those that just have an adaptor named after there object
399 # in the main DBSQL directory.
400 AltAlleleGroup => 'Bio::EnsEMBL::DBSQL::AltAlleleGroupAdaptor',
401 Analysis => 'Bio::EnsEMBL::DBSQL::AnalysisAdaptor',
402 ArchiveStableId => 'Bio::EnsEMBL::DBSQL::ArchiveStableIdAdaptor',
403 AssemblyExceptionFeature => 'Bio::EnsEMBL::DBSQL::AssemblyExceptionFeatureAdaptor',
404 AssemblyMapper => 'Bio::EnsEMBL::DBSQL::AssemblyMapperAdaptor',
405 AssemblySlice => 'Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor',
406 Attribute => 'Bio::EnsEMBL::DBSQL::AttributeAdaptor',
407 Biotype => 'Bio::EnsEMBL::DBSQL::BiotypeAdaptor',
408 CoordSystem => 'Bio::EnsEMBL::DBSQL::CoordSystemAdaptor',
409 DataFile => 'Bio::EnsEMBL::DBSQL::DataFileAdaptor',
410 DBEntry => 'Bio::EnsEMBL::DBSQL::DBEntryAdaptor',
411 DensityFeature => 'Bio::EnsEMBL::DBSQL::DensityFeatureAdaptor',
412 DensityType => 'Bio::EnsEMBL::DBSQL::DensityTypeAdaptor',
413 DnaAlignFeature => 'Bio::EnsEMBL::DBSQL::DnaAlignFeatureAdaptor',
414 Exon => 'Bio::EnsEMBL::DBSQL::ExonAdaptor',
415 Gene => 'Bio::EnsEMBL::DBSQL::GeneAdaptor',
416 IntronSupportingEvidence => 'Bio::EnsEMBL::DBSQL::IntronSupportingEvidenceAdaptor',
417 KaryotypeBand => 'Bio::EnsEMBL::DBSQL::KaryotypeBandAdaptor',
418 MiscFeature => 'Bio::EnsEMBL::DBSQL::MiscFeatureAdaptor',
419 MiscSet => 'Bio::EnsEMBL::DBSQL::MiscSetAdaptor',
420 Operon => 'Bio::EnsEMBL::DBSQL::OperonAdaptor',
421 OperonTranscript => 'Bio::EnsEMBL::DBSQL::OperonTranscriptAdaptor',
422 PredictionExon => 'Bio::EnsEMBL::DBSQL::PredictionExonAdaptor',
423 PredictionTranscript => 'Bio::EnsEMBL::DBSQL::PredictionTranscriptAdaptor',
424 ProteinAlignFeature => 'Bio::EnsEMBL::DBSQL::ProteinAlignFeatureAdaptor',
425 ProteinFeature => 'Bio::EnsEMBL::DBSQL::ProteinFeatureAdaptor',
426 RNAProduct => 'Bio::EnsEMBL::DBSQL::RNAProductAdaptor',
427 RepeatConsensus => 'Bio::EnsEMBL::DBSQL::RepeatConsensusAdaptor',
428 RepeatFeature => 'Bio::EnsEMBL::DBSQL::RepeatFeatureAdaptor',
429 SeqRegionSynonym => 'Bio::EnsEMBL::DBSQL::SeqRegionSynonymAdaptor',
430 Sequence => 'Bio::EnsEMBL::DBSQL::SequenceAdaptor',
431 SimpleFeature => 'Bio::EnsEMBL::DBSQL::SimpleFeatureAdaptor',
432 Slice => 'Bio::EnsEMBL::DBSQL::SliceAdaptor',
433 SupportingFeature => 'Bio::EnsEMBL::DBSQL::SupportingFeatureAdaptor',
434 Transcript => 'Bio::EnsEMBL::DBSQL::TranscriptAdaptor',
435 TranscriptSupportingFeature => 'Bio::EnsEMBL::DBSQL::TranscriptSupportingFeatureAdaptor',
436 Translation => 'Bio::EnsEMBL::DBSQL::TranslationAdaptor',
437 UnmappedObject => 'Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor',
438  
439 # Those whose adaptors are in Map::DBSQL
440 Ditag => 'Bio::EnsEMBL::Map::DBSQL::DitagAdaptor',
441 DitagFeature => 'Bio::EnsEMBL::Map::DBSQL::DitagFeatureAdaptor',
442 Marker => 'Bio::EnsEMBL::Map::DBSQL::MarkerAdaptor',
443 MarkerFeature => 'Bio::EnsEMBL::Map::DBSQL::MarkerFeatureAdaptor',
444  
445 # Finally the exceptions... those that have non-standard mapping
446 # between object / adaptor ....
447 GenomeContainer => 'Bio::EnsEMBL::DBSQL::GenomeContainer',
448 MetaContainer => 'Bio::EnsEMBL::DBSQL::MetaContainer',
449 MetaCoordContainer => 'Bio::EnsEMBL::DBSQL::MetaCoordContainer',
450 };
451 return $adaptors;
452 } ## end sub get_available_adaptors
453  
454 ###########################################################
455 #
456 # Support for DAS
457 #
458 ###########################################################
459  
460 =head2 add_DASFeatureFactory
461  
462 Arg [1] : Bio::EnsEMBL::ExternalFeatureFactory $value
463 Example : none
464 Description: Attaches a DAS Feature Factory to this method.
465 ExternalFeatureFactory objects are not really used right now.
466 They may be reintroduced or taken out completely. The fate
467 of this function is unknown (although it is presently needed).
468 Returntype : none
469 Exceptions : none
470 Caller : EnsWeb
471 Status : At Risk
472 : with the new web code this may not be needed/supported
473  
474 =cut
475  
476 sub add_DASFeatureFactory{
477
478 my ($self,$value) = @_;
479
480 push(@{$self->{'_das_ff'}},$value);
481 }
482  
483  
484 sub remove_all_DASFeatureFactories {
485 $_[0]->{'_das_ff'} = [];
486 }
487 =head2 _each_DASFeatureFactory
488  
489 Args : none
490 Example : none
491 Description: Not sure if this is used, or if it should be removed. It
492 does not seem to be used at the moment
493 Returntype : Bio::EnsEMBL::ExternalFeatureFactory
494 Exceptions : none
495 Caller : ??
496 Status : At Risk
497 : with the new web code this may not be needed/supported
498  
499 =cut
500  
501 sub _each_DASFeatureFactory{
502 my ($self) = @_;
503  
504 return @{$self->{'_das_ff'}||[]}
505 }
506  
507  
508 ##################################################################
509 #
510 # SUPPORT FOR EXTERNAL FEATURE FACTORIES
511 #
512 ##################################################################
513  
514  
515  
516 =head2 add_ExternalFeatureAdaptor
517  
518 Arg [1] : Bio::EnsEMBL::External::ExternalFeatureAdaptor
519 Example : $db_adaptor->add_ExternalFeatureAdaptor($xfa);
520 Description: Adds an external feature adaptor to this database adaptor.
521 Adding the external adaptor in this way allows external
522 features to be obtained from Slices and from RawContigs.
523  
524 The external feature adaptor which is passed to this method
525 will have its db attribute set to this DBAdaptor object via
526 the db accessor method.
527  
528 ExternalFeatureAdaptors passed to this method are stored
529 internally in a hash keyed on the string returned by the
530 ExternalFeatureAdaptors track_name method.
531
532 If the track name method is not implemented then the
533 a default key named 'External features' is assigned. In the
534 event of duplicate key names, a number is appended to the
535 key name, and incremented for each subsequent adaptor with the
536 same track name. For example, if no track_names are specified
537 then the the external feature adaptors will be stored under the
538 keys 'External features', 'External features2'
539 'External features3' etc.
540 Returntype : none
541 Exceptions : none
542 Caller : general
543
544 =cut
545  
546 sub add_ExternalFeatureAdaptor {
547 my ($self, $adaptor) = @_;
548  
549 unless($adaptor && ref $adaptor &&
550 $adaptor->isa('Bio::EnsEMBL::External::ExternalFeatureAdaptor')) {
551 throw("[$adaptor] is not a " .
552 "Bio::EnsEMBL::External::ExternalFeatureAdaptor");
553 }
554  
555 unless(exists $self->{'_xf_adaptors'}) {
556 $self->{'_xf_adaptors'} = {};
557 }
558  
559 my $track_name = $adaptor->{'_track_name'};
560 if(!$track_name) {
561 $track_name = $adaptor->track_name();
562 }
563  
564 #use a generic track name if one hasn't been defined
565 unless(defined $track_name) {
566 $track_name = "External features";
567 }
568  
569 #if the track name exists add numbers to the end until a free name is found
570 if(exists $self->{'_xf_adaptors'}->{"$track_name"}) {
571 my $num = 2;
572 $num++ while(exists $self->{'_xf_adaptors'}->{"$track_name$num"});
573 $self->{'_xf_adaptors'}->{"$track_name$num"} = $adaptor;
574 } else {
575 $self->{'_xf_adaptors'}->{"$track_name"} = $adaptor;
576 }
577  
578 $adaptor->ensembl_db($self);
579 }
580  
581  
582  
583 =head2 get_ExternalFeatureAdaptors
584  
585 Arg [1] : none
586 Example : @xfas = values %{$db_adaptor->get_ExternalFeatureAdaptors};
587 Description: Retrieves all of the ExternalFeatureAdaptors which have been
588 added to this DBAdaptor. The ExternalFeatureAdaptors are
589 returned in a reference to a hash keyed on the track names
590 of the external adaptors
591 Returntype : Reference to a hash of ExternalFeatureAdaptors keyed on
592 their track names.
593 Exceptions : none
594 Caller : general
595  
596 =cut
597  
598 sub get_ExternalFeatureAdaptors {
599 my $self = shift;
600  
601 return $self->{'_xf_adaptors'};
602 }
603  
604  
605 =head2 add_ExternalFeatureFactory
606  
607 Arg [1] : Bio::EnsEMBL::DB::ExternalFeatureFactoryI $value
608 Example : $db_adaptor->add_ExternalFeatureFactory
609 Description: It is recommended that add_ExternalFeatureAdaptor be used
610 instead. See documentation for
611 Bio::EnsEMBL::External::ExternalFeatureAdaptor
612  
613 Adds an external feature factory to the core database
614 so that features from external sources can be displayed in
615 ensembl. This method is still available mainly for legacy
616 support for external EnsEMBL installations.
617 Returntype : none
618 Exceptions : none
619 Caller : external
620  
621 =cut
622  
623 sub add_ExternalFeatureFactory{
624 my ($self,$value) = @_;
625  
626 $self->add_ExternalFeatureAdaptor($value);
627 }
628  
629 #
630 # OVERWRITABLE STANDARD ADAPTORS
631 #
632  
633 =head2 get_adaptor
634  
635 Arg [1] : Canonical data type for which an adaptor is required.
636 Example : $db_adaptor->get_adaptor("Protein")
637 Description: Gets an adaptor object for a standard data type.
638 Returntype : Adaptor Object of arbitrary type or undef
639 Exceptions : none
640 Caller : external
641 Status : Medium Risk
642 : please use the Registry method, as at some time this
643 : may no longer be supported.
644
645 =cut
646  
647 sub get_adaptor {
648 my ($self, $canonical_name, @other_args) = @_;
649 return $REGISTRY->get_adaptor($self->species(),$self->group(),$canonical_name);
650 }
651  
652  
653  
654 =head2 set_adaptor
655  
656 Arg [1] : Canonical data type for new adaptor.
657 Arg [2] : Object defining the adaptor for arg1.
658 Example : $aa = Bio::EnsEMBL::DBSQL::GeneAdaptor->new($db_adaptor);
659 : $db_adaptor->set_adaptor("Gene", $ga)
660 Description: Stores the object which represents the adaptor for the
661 arg1 data type.
662 Returntype : none
663 Exceptions : none
664 Caller : external
665 Status : Medium Risk
666 : please use the Registry method, as at some time this
667 : may no longer be supported.
668
669 =cut
670  
671 sub set_adaptor {
672 my ($self, $canonical_name, $module) = @_;
673 $REGISTRY->add_adaptor($self->species(),$self->group(),$canonical_name,$module);
674 return $module;
675 }
676  
677  
678 #
679 # GENERIC FEATURE ADAPTORS
680 #
681  
682 =head2 get_GenericFeatureAdaptors
683  
684 Arg [1] : List of names of feature adaptors to get. If no
685 adaptor names are given, all the defined adaptors are returned.
686 Example : $db->get_GenericFeature("SomeFeature", "SomeOtherFeature")
687 Description: Returns a hash containing the named feature adaptors (or
688 all feature adaptors).
689 Returntype : reference to a Hash containing the named
690 feature adaptors (or all feature adaptors).
691 Exceptions : If any of the the named generic feature adaptors do not exist.
692 Caller : external
693  
694 =cut
695  
696 sub get_GenericFeatureAdaptors {
697  
698 my ($self, @names) = @_;
699  
700 my %adaptors = ();
701  
702 if (!@names) {
703 %adaptors = %{$self->{'generic_feature_adaptors'}};
704 } else {
705 foreach my $name (@names) {
706 if (!exists($self->{'generic_feature_adaptors'}->{$name})) {
707 throw("No generic feature adaptor has been defined for $name" );
708 }
709  
710  
711 $adaptors{$name} = $self->{'generic_feature_adaptors'}->{$name};
712 }
713 }
714  
715 return \%adaptors;
716 }
717  
718  
719 =head2 add_GenericFeatureAdaptor
720  
721 Arg [1] : The name of the feature.
722 Arg [2] : Adaptor object for a generic feature.
723 Example : $db->add_GenericFeatureAdaptor("SomeFeature",
724 "Bio::EnsEMBL::DBSQL::SomeFeatureAdaptor")
725 Description: Stores the object which represents the adaptor for the
726 named feature type.
727 Returntype : none
728 Exceptions :
729 Caller : external
730  
731 =cut
732  
733 sub add_GenericFeatureAdaptor {
734 my ($self, $name, $adaptor_obj) = @_;
735
736 # check that $adaptor is an object that subclasses BaseFeatureAdaptor
737 if (!$adaptor_obj->isa("Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor")) {
738 throw("$name is a " . ref($adaptor_obj) . "which is not a " .
739 "subclass of Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor" );
740 }
741  
742 $self->{'generic_feature_adaptors'}->{$name} = $adaptor_obj;
743 }
744  
745 =head2 species
746  
747 Arg [1] : (optional) string $arg
748 The new value of the species used by this DBAdaptor.
749 Example : $species = $dba->species()
750 Description: Getter/Setter for the species of to use for
751 this connection. There is currently no point in setting
752 this value after the connection has already been established
753 by the constructor.
754 Returntype : string
755 Exceptions : none
756 Caller : new
757 Status : Stable
758  
759 =cut
760  
761 sub species {
762 my ( $self, $arg ) = @_;
763  
764 if ( defined($arg) ) {
765 $self->{_species} = $arg;
766 }
767  
768 $self->{_species};
769 }
770  
771 =head2 all_species
772  
773 Args : NONE
774 Example : @all_species = @{$dba->all_species()};
775 Description: Returns the names of all species contained in the
776 database to which this DBAdaptor is connected.
777 Returntype : array reference
778 Exceptions : none
779 Caller : general
780 Status : Stable
781  
782 =cut
783  
784 sub all_species {
785 my ($self) = @_;
786 if ( !$self->is_multispecies() ) { return [ $self->species() ] }
787 return $self->{'_all_species'} if exists $self->{_all_species};
788 my $sql = "SELECT meta_value FROM meta WHERE meta_key = 'species.db_name'";
789 $self->{_all_species} = $self->dbc->sql_helper()->execute_simple(-SQL => $sql);
790 return $self->{'_all_species'};
791 } ## end sub all_species
792  
793  
794 =head2 is_multispecies
795  
796 Arg [1] : (optional) boolean $arg
797 Example : if ($dba->is_multispecies()) { }
798 Description: Getter/Setter for the is_multispecies boolean of
799 to use for this connection. There is currently no
800 point in setting this value after the connection has
801 already been established by the constructor.
802 Returntype : boolean
803 Exceptions : none
804 Caller : new
805 Status : Stable
806  
807 =cut
808  
809 sub is_multispecies {
810 my ( $self, $arg ) = @_;
811  
812 if ( defined($arg) ) {
813 $self->{_is_multispecies} = $arg;
814 }
815  
816 return $self->{_is_multispecies};
817 }
818  
819  
820 =head2 species_id
821  
822 Arg [1] : (optional) string $arg
823 The new value of the species_id used by this DBAdaptor
824 when dealing with multi-species databases.
825 Example : $species_id = $dba->species_id()
826 Description: Getter/Setter for the species_id of to use for this
827 connection. There is currently no point in setting
828 this value after the connection has already been
829 established by the constructor.
830 Returntype : string
831 Exceptions : none
832 Caller : new
833 Status : Stable
834  
835 =cut
836  
837 sub species_id {
838 my ( $self, $arg ) = @_;
839  
840 if ( defined($arg) ) {
841 $self->{_species_id} = $arg;
842 }
843  
844 return $self->{_species_id};
845 }
846  
847  
848 =head2 no_cache
849  
850 Arg [1] : (optional) int $arg
851 The new value of the no cache attribute used by this DBAdaptor.
852 Example : $no_cache = $dba->no_cache();
853 Description: Getter/Setter for the no_cache to use for
854 this connection. There is currently no point in setting
855 this value after the connection has already been established
856 by the constructor.
857 Returntype : int
858 Exceptions : none
859 Caller : new
860 Status : Stable
861  
862 =cut
863  
864 sub no_cache {
865 my ($self, $arg ) = @_;
866  
867 if ( defined $arg ){
868 if ($arg != 1 && $arg != 0){
869 throw("$arg is not allowed for this attribute. Only value 1|0 is allowed");
870 }
871 $self->{_no_cache} = $arg;
872 }
873 $self->{_no_cache};
874 }
875  
876  
877 =head2 group
878  
879 Arg [1] : (optional) string $arg
880 The new value of the group used by this DBAdaptor.
881 Example : $group = $dba->group()
882 Description: Getter/Setter for the group of to use for
883 this connection. There is currently no point in setting
884 this value after the connection has already been established
885 by the constructor.
886 Returntype : string
887 Exceptions : none
888 Caller : new
889 Status : Stable
890  
891 =cut
892  
893 sub group {
894 my ($self, $arg ) = @_;
895 ( defined $arg ) &&
896 ( $self->{_group} = $arg );
897 $self->{_group};
898 }
899  
900 =head2 get_SeqRegionCache
901  
902 Arg [1] : none
903 Example : my $srcache = $dba->get_SeqRegionCache();
904 Description: Retrieves a seq_region cache for this database
905 Returntype : Bio::EnsEMBL::Utils::SeqRegionCache
906 Exceptions : none
907 Caller : SliceAdaptor, AssemblyMapperAdaptor
908 Status : Stable
909  
910 =cut
911  
912 sub get_SeqRegionCache {
913 my $self = shift;
914  
915 # use the cache from the database where seq_regions are stored
916 if($self != $self->dnadb()) {
917 return $self->dnadb()->get_SeqRegionCache();
918 }
919  
920 if(!$self->{'seq_region_cache'}) {
921 $self->{'seq_region_cache'} = Bio::EnsEMBL::Utils::SeqRegionCache->new();
922 }
923  
924 return $self->{'seq_region_cache'};
925 }
926  
927  
928  
929 #convenient method to retrieve the schema_build version for the database being used
930  
931 sub _get_schema_build{
932 my ($self) = @_;
933  
934 #avoided using dnadb by default to avoid obfuscation of behaviour
935
936 my @dbname = split/_/, $self->dbc->dbname();
937  
938 #warn "dbname is $schema_build";
939  
940 my $schema_build = pop @dbname;
941 $schema_build = pop(@dbname).'_'.$schema_build;
942  
943  
944 return $schema_build;
945 }
946  
947  
948 =head2 dnadb
949  
950 Title : dnadb
951 Usage : my $dnadb = $db->dnadb();
952 Function: returns the database adaptor where the dna lives
953 Useful if you only want to keep one copy of the dna
954 on disk but have other databases with genes and features in
955 Returns : dna database adaptor
956 Args : Bio::EnsEMBL::DBSQL::BaseAdaptor
957 Status : Medium Risk.
958 : Use the Registry method add_DNAAdaptor/get_DNAAdaptor instead
959  
960 =cut
961  
962 sub dnadb {
963 my $self = shift;
964  
965 if(@_) {
966 my $arg = shift;
967 $REGISTRY->add_DNAAdaptor($self->species(),$self->group(),$arg->species(),$arg->group());
968 }
969  
970 # return $self->{'dnadb'} || $self;
971 return $REGISTRY->get_DNAAdaptor($self->species(),$self->group()) || $self;
972 }
973  
974  
975 use vars '$AUTOLOAD';
976  
977 sub AUTOLOAD {
978 my ( $self, @args ) = @_;
979  
980 my $type;
981 if ( $AUTOLOAD =~ /^.*::get_(\w+)Adaptor$/ ) {
982 $type = $1;
983 } elsif ( $AUTOLOAD =~ /^.*::get_(\w+)$/ ) {
984 $type = $1;
985 } else {
986 throw( sprintf( "Could not work out type for %s\n", $AUTOLOAD ) );
987 }
988
989 my $ret = $REGISTRY->get_adaptor( $self->species(), $self->group(), $type );
990 return $ret if $ret;
991
992 warning( sprintf(
993 "Could not find %s adaptor in the registry for %s %s\n",
994 $type, $self->species(), $self->group() ) );
995  
996 throw( sprintf(
997 "Could not get adaptor %s for %s %s\n",
998 $type, $self->species(), $self->group() ) );
999  
1000 } ## end sub AUTOLOAD
1001  
1002 sub DESTROY { } # required due to AUTOLOAD
1003  
1004 =head2 to_hash
1005  
1006 Example : my $hash = $dba->to_hash();
1007 my $new_dba = $dba->new(%{$hash});
1008 Description: Provides a hash which is compatible with the
1009 parameters for DBAdaptor's new() method. This can be
1010 useful during serialisation but be aware that Registry
1011 Returntype : Hash
1012 Exceptions : none
1013 Caller : general
1014 Status : New
1015  
1016 =cut
1017  
1018 sub to_hash {
1019 my ($self) = @_;
1020 my $hash = $self->dbc()->to_hash();
1021 $hash->{-SPECIES} = $self->species();
1022 $hash->{-GROUP} = $self->group();
1023 $hash->{-SPECIES_ID} = $self->species_id();
1024 $hash->{-MULTISPECIES_DB} = $self->is_multispecies();
1025 return $hash;
1026 }
1027  
1028 #########################
1029 # Switchable adaptor methods
1030 #########################
1031  
1032 =head2 switch_adaptor
1033  
1034 Arg [1] : String name of the adaptor type to switch out
1035 Arg [2] : Reference The switchable adaptor implementation
1036 Arg [3] : (optional) CodeRef Provide a subroutine reference as a callback. The
1037 adaptor will be switched before your codeblock is executed and
1038 the adaptor switched back to the original once your code has finished running
1039 Arg [4] : (optional) Boolean override any existing switchable adaptor
1040 Example : $dba->switch_adaptor("sequence", $my_replacement_sequence_adaptor);
1041 $dba->switch_adaptor("sequence", $my_other_replacement_sequence_adaptor, 1);
1042 $dba->switch_adaptor("sequence", $my_replacement_sequence_adaptor, sub {
1043 #Make calls as normal without having to do manual cleanup
1044 });
1045 Returntype : None
1046 Description : Provides a wrapper around the Registry add_switchable_adaptor() method
1047 defaulting both species and group to the current DBAdaptor. Callbacks are
1048 also available providing automatic resource cleanup.
1049  
1050 The method also remembers the last switch you did. It will not remember
1051 multiple switches though.
1052 Exceptions : Thrown if no switchable adaptor instance was given
1053  
1054 =cut
1055  
1056 sub switch_adaptor {
1057 my ($self, $adaptor_name, $instance, $callback, $force) = @_;
1058 my ($species, $group) = ($self->species(), $self->group());
1059 $REGISTRY->add_switchable_adaptor($species, $group, $adaptor_name, $instance, $force);
1060 $self->{_last_switchable_adaptor} = $adaptor_name;
1061 if(check_ref($callback, 'CODE')) {
1062 #Scope guard will reset the adaptor once it falls out of scope. They
1063 #are implemented as callback DESTROYS so are neigh on impossible
1064 #to stop executing
1065 my $guard = scope_guard(sub { $REGISTRY->remove_switchable_adaptor($species, $group, $adaptor_name); } );
1066 $callback->();
1067 }
1068 return;
1069 }
1070  
1071 =head2 has_switched_adaptor
1072  
1073 Arg [1] : String name of the adaptor type to switch back in
1074 Example : $dba->has_switchable_adaptor("sequence"); #explicit switching back
1075 Returntype : Boolean indicating if the given adaptor is being actively switched
1076 Description : Provides a wrapper around the Registry has_switchable_adaptor() method
1077 defaulting both species and group to the current DBAdaptor. This will
1078 inform if the specified adaptor is being switched out
1079 Exceptions : None
1080  
1081 =cut
1082  
1083 sub has_switched_adaptor {
1084 my ($self, $adaptor_name) = @_;
1085 return $REGISTRY->has_switchable_adaptor($self->species, $self->group, $adaptor_name);
1086 }
1087  
1088 =head2 revert_adaptor
1089  
1090 Arg [1] : (optional) String name of the adaptor type to switch back in
1091 Example : $dba->revert_adaptor(); #implicit switching back whatever was the last switch_adaptor() call
1092 $dba->revert_adaptor("sequence"); #explicit switching back
1093 Returntype : The removed adaptor
1094 Description : Provides a wrapper around the Registry remove_switchable_adaptor() method
1095 defaulting both species and group to the current DBAdaptor. This will remove
1096 a switchable adaptor. You can also remove the last adaptor you switched
1097 in without having to specify any parameter.
1098 Exceptions : Thrown if no switchable adaptor name was given or could be found in the internal
1099 last adaptor variable
1100  
1101 =cut
1102  
1103 sub revert_adaptor {
1104 my ($self, $adaptor_name) = @_;
1105 $adaptor_name ||= $self->{_last_switchable_adaptor};
1106 throw "Not given an adaptor name to remove and cannot find the name of the last switched adaptor" if ! $adaptor_name;
1107 my $deleted_adaptor = $REGISTRY->remove_switchable_adaptor($self->species, $self->group, $adaptor_name);
1108 delete $self->{last_switch};
1109 return $deleted_adaptor;
1110 }
1111  
1112  
1113 1;