Archive Ensembl HomeArchive Ensembl Home
BaseRunnable.pm
Go to the documentation of this file.
00001 
00002 =pod 
00003 
00004 =head1 NAME
00005 
00006 Bio::EnsEMBL::Compara::RunnableDB::BaseRunnable
00007 
00008 =head1 SYNOPSIS
00009 
00010         # from within a Compara Runnable:
00011     my $FamilyAdaptor = $self->compara_dba()->get_FamilyAdaptor();
00012 
00013     my $ExternalFooFeatureAdaptor = $self->compara_dba($self->param('external_source'))->get_FooFeatureAdaptor();
00014 
00015 =head1 DESCRIPTION
00016 
00017 All Compara RunnableDBs *should* inherit from this module in order to work with module parameters and compara_dba in a neat way.
00018 
00019 It inherits the parameter parsing functionality from Bio::EnsEMBL::Hive::Process
00020 and provides a convenience method for creating the compara_dba from almost anything that can provide connection parameters.
00021 
00022 Please refer to Bio::EnsEMBL::Hive::Process documentation to understand the basics of the RunnableDB interface.
00023 
00024 =cut
00025 
00026 package Bio::EnsEMBL::Compara::RunnableDB::BaseRunnable;
00027 
00028 use strict;
00029 use Bio::EnsEMBL::Hive::URLFactory;
00030 use Bio::EnsEMBL::Compara::DBSQL::DBAdaptor;
00031 use Carp;
00032 use base ('Bio::EnsEMBL::Hive::Process');
00033 
00034 
00035 =head2 compara_dba
00036 
00037     Description: this is an intelligent setter/getter of a Compara DBA. Resorts to magic in order to figure out how to connect.
00038 
00039     Example 1:   my $family_adaptor = $self->compara_dba()->get_FamilyAdaptor();    # implicit initialization and hashing
00040 
00041     Example 2:   my $external_foo_adaptor = $self->compara_dba( $self->param('db_conn') )->get_FooAdaptor();    # explicit initialization and hashing
00042 
00043 =cut
00044 
00045 sub compara_dba {
00046     my $self = shift @_;
00047 
00048     if(@_ or !$self->{'comparaDBA'}) {
00049         $self->{'comparaDBA'} = $self->go_figure_compara_dba( shift @_ || $self->param('compara_db') || $self );
00050     }
00051 
00052     return $self->{'comparaDBA'};
00053 }
00054 
00055 
00056 =head2 go_figure_compara_dba
00057 
00058     Description: this is a method that tries lots of different ways to find connection parameters
00059                  from a given object/hash and returns a Compara DBA. Does not hash anything, just does the detective magic.
00060 
00061 =cut
00062 
00063 sub go_figure_compara_dba {
00064     my ($self, $foo) = @_;
00065 
00066         
00067     if(UNIVERSAL::isa($foo, 'Bio::EnsEMBL::Compara::DBSQL::DBAdaptor')) {   # it is already a Compara adaptor - just return it
00068 
00069         return $foo;   
00070 
00071     } elsif(ref($foo) eq 'HASH') {  # simply a hash with connection parameters, plug them in:
00072 
00073         return Bio::EnsEMBL::Compara::DBSQL::DBAdaptor->new( %$foo );
00074 
00075     } elsif(UNIVERSAL::isa($foo, 'Bio::EnsEMBL::DBSQL::DBConnection')) { # a DBConnection itself, plug it in:
00076 
00077         return Bio::EnsEMBL::Compara::DBSQL::DBAdaptor->new( -DBCONN => $foo );
00078 
00079     } elsif(UNIVERSAL::can($foo, 'dbc') and UNIVERSAL::isa($foo->dbc, 'Bio::EnsEMBL::DBSQL::DBConnection')) { # another DBAdaptor, possibly Hive::DBSQL::DBAdaptor
00080 
00081         return Bio::EnsEMBL::Compara::DBSQL::DBAdaptor->new( -DBCONN => $foo->dbc );
00082 
00083     } elsif(UNIVERSAL::can($foo, 'db') and UNIVERSAL::can($foo->db, 'dbc') and UNIVERSAL::isa($foo->db->dbc, 'Bio::EnsEMBL::DBSQL::DBConnection')) { # another data adaptor or Runnable:
00084 
00085         return Bio::EnsEMBL::Compara::DBSQL::DBAdaptor->new( -DBCONN => $foo->db->dbc );
00086 
00087     } elsif(!ref($foo) and $foo=~m{^\w*://}) {
00088 
00089         return Bio::EnsEMBL::Hive::URLFactory->fetch( $foo . ';type=compara' );
00090 
00091     } else {
00092     
00093         unless(ref($foo)) {    # maybe it is simply a registry key?
00094         
00095             my $dba;
00096             eval {
00097                 require Bio::EnsEMBL::Registry;
00098                 $dba = Bio::EnsEMBL::Registry->get_DBAdaptor($foo, 'compara');
00099             };
00100             if($dba) {
00101                 return $dba;
00102             }
00103         }
00104 
00105         croak "Sorry, could not figure out how to make a Compara DBAdaptor out of $foo";
00106     }
00107 }
00108 
00109 1;