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