EnsEMBL/DBSQL/DBAdaptor.pm untested

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