| 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::DBLoader - Run time database loader | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | $db = | ||
| 38 | Bio::EnsEMBL::DBLoader->new( "Bio::EnsEMBL::DBSQL::DBAdaptor/" | ||
| 39 | . "host=localhost;" | ||
| 40 | . "dbname=homo_sapiens_core_19_34a;" | ||
| 41 | . "user=ensro;" ); | ||
| 42 | |||
| 43 | # $db is a database object | ||
| 44 | $db = Bio::EnsEMBL::DBLoader->standard(); | ||
| 45 | |||
| 46 | # equivalent to | ||
| 47 | # Bio::EnsEMBL::DBLoader->new( $ENV{'ENSEMBL_DATABASE'} ); | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | This system provides a run-time loading of the database for ensembl, | ||
| 52 | allowing two things | ||
| 53 | |||
| 54 | a) Only "using" the database module which is required for a | ||
| 55 | particular implementation | ||
| 56 | |||
| 57 | b) Providing a simple string method to indicate where the database | ||
| 58 | is, allowing per sites defaults and other things as such | ||
| 59 | |||
| 60 | |||
| 61 | The string is parsed as follows: | ||
| 62 | |||
| 63 | Before the / is the Perl database object to load, after are the | ||
| 64 | parameters to pass to that database. The parameters are series of | ||
| 65 | key=values separated by semi-colons. These are passed as a hash to the | ||
| 66 | new method of the database object | ||
| 67 | |||
| 68 | =head1 METHODS | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | package Bio::EnsEMBL::DBLoader; | ||
| 73 | $Bio::EnsEMBL::DBLoader::VERSION = '114.0.0'; | ||
| 74 | use strict; | ||
| 75 | |||
| 76 | |||
| 77 | =head2 new | ||
| 78 | |||
| 79 | Arg [1] : string $string | ||
| 80 | An Ensembl database locator string. | ||
| 81 | Example : Bio::EnsEMBL::DBSQL::DBLoader->new("Bio::EnsEMBL::DBSQL::DBAdaptor/host=localhost;dbname=homo_sapiens_core_19_34a;user=ensro;" | ||
| 82 | Description: Connects to an Ensembl database using the module specified in | ||
| 83 | the locator string. | ||
| 84 | Returntype : The module specified in the load string is returned. | ||
| 85 | Exceptions : thrown if the specified module cannot be instantiated or the | ||
| 86 | locator string cannot be parsed | ||
| 87 | Caller : ? | ||
| 88 | Status : Stable | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub new{ | ||
| 93 | my ($class,$string) = @_; | ||
| 94 | my ($module,%hash); | ||
| 95 | |||
| 96 | $string =~ /(\S+?)\/([\S+\s*]+)/ || die "Could not parse [$string] as a ensembl database locator. Needs database_module/params"; | ||
| 97 | $module = $1; | ||
| 98 | my $param = $2; | ||
| 99 | |||
| 100 | &_load_module($module); | ||
| 101 | my @param = split(/;/,$param); | ||
| 102 | foreach my $keyvalue ( @param ) { | ||
| 103 | $keyvalue =~ /(\S+?)=([\S*\s*]*)/ || do { warn("In loading $keyvalue, could not split into keyvalue for loading $module. Ignoring"); next; }; | ||
| 104 | |||
| 105 | my $key = $1; | ||
| 106 | my $value = $2; | ||
| 107 | |||
| 108 | $hash{"-$key"} = $value; | ||
| 109 | } | ||
| 110 | |||
| 111 | my @kv = %hash; | ||
| 112 | |||
| 113 | return "$module"->new(%hash); | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | sub _load_module{ | ||
| 118 | my ($modulein) = @_; | ||
| 119 | my ($module,$load,$m); | ||
| 120 | |||
| 121 | $module = "_<$modulein.pm"; | ||
| 122 | $load = "$modulein.pm"; | ||
| 123 | $load =~ s/::/\//g; | ||
| 124 | |||
| 125 | return 1 if $main::{$module}; | ||
| 126 | eval { | ||
| 127 | require $load; | ||
| 128 | }; | ||
| 129 | if( $@ ) { | ||
| 130 | print STDERR <<END; | ||
| 131 | $load: cannot be found | ||
| 132 | Exception $@ | ||
| 133 | |||
| 134 | END | ||
| 135 | ; | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | return 1; | ||
| 139 | } | ||
| 140 | |||
| 141 | 1; |