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

3
  Copyright (c) 1999-2013 The European Bioinformatics Institute and
4
  Genome Research Limited.  All rights reserved.
Ewan Birney's avatar
Ewan Birney committed
5

6
7
8
9
10
11
12
13
  This software is distributed under a modified Apache license.
  For license details, please see

    http://www.ensembl.org/info/about/code_licence.html

=head1 CONTACT

  Please email comments or questions to the public Ensembl
14
  developers list at <dev@ensembl.org>.
15
16
17
18
19

  Questions may also be sent to the Ensembl help desk at
  <helpdesk@ensembl.org>.

=cut
Ewan Birney's avatar
Ewan Birney committed
20
21
22
23
24
25
26

=head1 NAME

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

=head1 SYNOPSIS

27
28
29
30
31
    $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
32
33
34
35

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

36
37
    # equivalent to
    # Bio::EnsEMBL::DBLoader->new( $ENV{'ENSEMBL_DATABASE'} );
Ewan Birney's avatar
Ewan Birney committed
38
39
40

=head1 DESCRIPTION

41
42
This system provides a run-time loading of the database for ensembl,
allowing two things
Ewan Birney's avatar
Ewan Birney committed
43

44
45
    a) Only "using" the database module which is required for a
       particular implementation
Ewan Birney's avatar
Ewan Birney committed
46

47
48
    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
49
50
51
52


The string is parsed as follows:

53
54
55
56
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
57

Graham McVicker's avatar
Graham McVicker committed
58
=head1 METHODS
Ewan Birney's avatar
Ewan Birney committed
59
60
61
62

=cut

package Bio::EnsEMBL::DBLoader;
63

Ewan Birney's avatar
Ewan Birney committed
64
65
66
use strict;


Graham McVicker's avatar
Graham McVicker committed
67
=head2 new
Ewan Birney's avatar
Ewan Birney committed
68

Graham McVicker's avatar
Graham McVicker committed
69
70
71
72
73
74
75
76
77
  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     : ?
78
  Status     : Stable
Ewan Birney's avatar
Ewan Birney committed
79
80
81
82

=cut

sub new{
83
   my ($class,$string) = @_;
Ewan Birney's avatar
Ewan Birney committed
84
85
   my ($module,%hash);

86
   $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
87
88
89
   $module = $1;
   my $param = $2;

90
   &_load_module($module);
Ewan Birney's avatar
Ewan Birney committed
91
92
   my @param = split(/;/,$param);
   foreach my $keyvalue ( @param ) {
93
       $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
94
95
96
97

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

98
       $hash{"-$key"} = $value;
Ewan Birney's avatar
Ewan Birney committed
99
   }
Ewan Birney's avatar
Ewan Birney committed
100
   
101
102
   my @kv = %hash;

Ewan Birney's avatar
Ewan Birney committed
103
104
105
106
107
108
109
   return "$module"->new(%hash);
}


sub _load_module{
  my ($modulein) = @_;
  my ($module,$load,$m);
110
111
112
113

  $module = "_<$modulein.pm";
  $load = "$modulein.pm";
  $load =~ s/::/\//g;
Ewan Birney's avatar
Ewan Birney committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  
  return 1 if $main::{$module};
  eval {
    require $load;
  };
  if( $@ ) {
    print STDERR <<END;
$load: cannot be found
Exception $@

END
  ;
    return;
  }
  return 1;
}
130
131

1;