DBLoader.pm 3.46 KB
Newer Older
1
=head1 LICENSE
Ewan Birney's avatar
Ewan Birney committed
2

3
Copyright [1999-2013] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Ewan Birney's avatar
Ewan Birney committed
4

5 6 7 8 9 10 11 12 13 14 15 16 17
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut
18 19 20 21 22


=head1 CONTACT

  Please email comments or questions to the public Ensembl
Magali Ruffier's avatar
Magali Ruffier committed
23
  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
24 25

  Questions may also be sent to the Ensembl help desk at
Magali Ruffier's avatar
Magali Ruffier committed
26
  <http://www.ensembl.org/Help/Contact>.
27 28

=cut
Ewan Birney's avatar
Ewan Birney committed
29 30 31 32 33 34 35

=head1 NAME

Bio::EnsEMBL::DBLoader - Run time database loader

=head1 SYNOPSIS

36 37 38 39 40
    $db =
      Bio::EnsEMBL::DBLoader->new( "Bio::EnsEMBL::DBSQL::DBAdaptor/"
        . "host=localhost;"
        . "dbname=homo_sapiens_core_19_34a;"
        . "user=ensro;" );
Ewan Birney's avatar
Ewan Birney committed
41 42 43 44

    # $db is a database object
    $db = Bio::EnsEMBL::DBLoader->standard();

45 46
    # equivalent to
    # Bio::EnsEMBL::DBLoader->new( $ENV{'ENSEMBL_DATABASE'} );
Ewan Birney's avatar
Ewan Birney committed
47 48 49

=head1 DESCRIPTION

50 51
This system provides a run-time loading of the database for ensembl,
allowing two things
Ewan Birney's avatar
Ewan Birney committed
52

53 54
    a) Only "using" the database module which is required for a
       particular implementation
Ewan Birney's avatar
Ewan Birney committed
55

56 57
    b) Providing a simple string method to indicate where the database
       is, allowing per sites defaults and other things as such
Ewan Birney's avatar
Ewan Birney committed
58 59 60 61


The string is parsed as follows:

62 63 64 65
Before the / is the Perl database object to load, after are the
parameters to pass to that database.  The parameters are series of
key=values separated by semi-colons.  These are passed as a hash to the
new method of the database object
Ewan Birney's avatar
Ewan Birney committed
66

Graham McVicker's avatar
Graham McVicker committed
67
=head1 METHODS
Ewan Birney's avatar
Ewan Birney committed
68 69 70 71

=cut

package Bio::EnsEMBL::DBLoader;
72

Ewan Birney's avatar
Ewan Birney committed
73 74 75
use strict;


Graham McVicker's avatar
Graham McVicker committed
76
=head2 new
Ewan Birney's avatar
Ewan Birney committed
77

Graham McVicker's avatar
Graham McVicker committed
78 79 80 81 82 83 84 85 86
  Arg [1]    : string $string
               An Ensembl database locator string.
  Example    : Bio::EnsEMBL::DBSQL::DBLoader->new("Bio::EnsEMBL::DBSQL::DBAdaptor/host=localhost;dbname=homo_sapiens_core_19_34a;user=ensro;"
  Description: Connects to an Ensembl database using the module specified in
               the locator string.
  Returntype : The module specified in the load string is returned.
  Exceptions : thrown if the specified module cannot be instantiated or the
               locator string cannot be parsed
  Caller     : ?
87
  Status     : Stable
Ewan Birney's avatar
Ewan Birney committed
88 89 90 91

=cut

sub new{
92
   my ($class,$string) = @_;
Ewan Birney's avatar
Ewan Birney committed
93 94
   my ($module,%hash);

95
   $string =~ /(\S+?)\/([\S+\s*]+)/ || die "Could not parse [$string] as a ensembl database locator. Needs database_module/params";
Ewan Birney's avatar
Ewan Birney committed
96 97 98
   $module = $1;
   my $param = $2;

99
   &_load_module($module);
Ewan Birney's avatar
Ewan Birney committed
100 101
   my @param = split(/;/,$param);
   foreach my $keyvalue ( @param ) {
102
       $keyvalue =~ /(\S+?)=([\S*\s*]*)/ || do { warn("In loading $keyvalue, could not split into keyvalue for loading $module. Ignoring"); next; };
Ewan Birney's avatar
Ewan Birney committed
103 104 105 106

       my $key = $1;
       my $value = $2;

107
       $hash{"-$key"} = $value;
Ewan Birney's avatar
Ewan Birney committed
108
   }
Ewan Birney's avatar
Ewan Birney committed
109
   
110 111
   my @kv = %hash;

Ewan Birney's avatar
Ewan Birney committed
112 113 114 115 116 117 118
   return "$module"->new(%hash);
}


sub _load_module{
  my ($modulein) = @_;
  my ($module,$load,$m);
119 120 121 122

  $module = "_<$modulein.pm";
  $load = "$modulein.pm";
  $load =~ s/::/\//g;
Ewan Birney's avatar
Ewan Birney committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  
  return 1 if $main::{$module};
  eval {
    require $load;
  };
  if( $@ ) {
    print STDERR <<END;
$load: cannot be found
Exception $@

END
  ;
    return;
  }
  return 1;
}
139 140

1;