3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
24 Please email comments or questions to the
public Ensembl
25 developers list at <http:
27 Questions may also be sent to the Ensembl help desk at
46 # get the basic options for connecting to a database server
50 push(@$optsd,
"print");
52 # process the command line with the supplied options plus a reference to a help subroutine
53 my $opts = $cli->process_args($optsd,\&
usage);
55 # use the command line options to get an array of database details
56 for my $db_args (@{$cli->get_dba_args_for_opts($opts)}) {
57 # use the args to create a DBA
60 if(defined $opts->{print}) {
65 For adding secondary databases, a prefix can be supplied. For instance, to add a second set of
66 db params prefixed with dna (-dnahost -dbport etc.) use the prefix argument with get_dba_opts and
67 get_dba_args_for_opts:
68 # get the basic options for connecting to a database server
70 [ @{ $cli_helper->get_dba_opts() }, @{ $cli_helper->get_dba_opts(
'gc') } ];
71 # process the command line with the supplied options plus a help subroutine
72 my $opts = $cli_helper->process_args( $optsd, \&
usage );
74 my ($dna_dba_details) =
75 @{ $cli_helper->get_dba_args_for_opts( $opts, 1,
'dna' ) };
81 Utilities
for a more consistent approach to parsing and handling
EnsEMBL script command lines
89 package Bio::EnsEMBL::Utils::CliHelper;
95 use Getopt::Long qw(:config auto_version no_ignore_case);
101 my $dba_opts = [{args => [
'host',
'dbhost',
'h'], type =>
'=s'},
102 {args => [
'port',
'dbport',
'P'], type =>
':i'},
103 {args => [
'user',
'dbuser',
'u'], type =>
'=s'},
104 {args => [
'pass',
'dbpass',
'p'], type =>
':s'},
105 {args => [
'dbname',
'D'], type =>
':s'},
106 {args => [
'pattern',
'dbpattern'], type =>
':s'},
107 {args => [
'driver'], type =>
':s'},
108 {args => [
'species_id'], type =>
':i'},
109 {args => [
'species'], type =>
':i'},];
113 Description : Construct a
new instance of a CliHelper
object
115 Status : Under development
120 my ($class, @args) = @_;
121 my $self = bless({}, ref($class) || $class);
125 =head2 get_dba_opts()
127 Arg [1] : Optional prefix for dbnames e.g. dna
128 Description : Retrieves the standard options for connecting to one or more Ensembl databases
129 Returntype : Arrayref of option definitions
130 Status : Under development
135 my ($self, $prefix) = @_;
138 my $opt = join
'|',
map { $prefix . $_ } @{$_->{args}};
144 =head2 process_args()
146 Arg [1] : Arrayref of supported command line options (e.g. from get_dba_opts)
147 Arg [2] : Ref to subroutine to be invoked when -help or -? is supplied
148 Description : Retrieves the standard options for connecting to one or more Ensembl databases
149 Returntype : Hashref of parsed options
150 Status : Under development
155 my ($self, $opts_def, $usage_sub) = @_;
157 push @{$opts_def}, q/help|?/ => $usage_sub;
158 GetOptions($opts, @{$opts_def}) ||
159 croak
'Could not parse command line arguments';
163 =head2 get_dba_args_for_opts()
165 Arg [1] : Hash of options (e.g. parsed from command line options by process_args())
166 Arg [2] : If set to 1, the databases are assumed to have a single species only. Default is 0 if database name matches collection, 1 otherwise.
167 Arg [3] : Optional prefix to use when parsing e.g. dna
168 Description : Uses the parsed command line options to generate an array of DBAdaptor arguments
169 : (e.g. expands dbpattern, finds all species_ids for multispecies databases)
170 : These can then be passed directly to
Bio::
EnsEMBL::DBSQL::DBAdaptor->new()
171 Returntype : Arrayref of DBA argument hash refs
172 Status : Under development
176 sub get_dba_args_for_opts {
177 my ($self, $opts, $single_species_opt, $prefix) = @_;
180 my ($host, $port, $user, $pass, $dbname,
181 $pattern, $driver, $species, $species_id)
182 =
map { $prefix . $_ }
183 qw(host port user pass dbname pattern driver species species_id);
186 if (defined $opts->{$host}) {
189 -PASS => $opts->{$pass},
190 -HOST => $opts->{$host},
191 -PORT => $opts->{$port},
192 -DRIVER => $opts->{$driver}
195 if (defined $opts->{$dbname}) {
196 push @dbnames, $opts->{$dbname};
198 elsif (defined $opts->{$pattern}) {
199 # get a basic DBConnection and use to find out which dbs are involved
200 @dbnames = grep { m/$opts->{$pattern}/smx }
204 croak
'dbname or dbpattern arguments required';
206 for my $dbname (@dbnames) {
208 #Decipher group of DBAdaptor by capturing the name_name(_name?)_core_ code. Otherwise we don't know
209 my ($group) = $dbname =~
210 /^[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?_([a-z]+)(?:_\d+)?_\d+/;
211 # set multi where we have collections
212 my $multi = $dbname =~ m/_collection_/ ? 1 : 0;
214 my $single_species = $single_species_opt;
216 if (!defined $single_species) {
217 # if we're dealing with a collection, turn off single species mode by default
218 $single_species = $dbname =~ m/_collection_/ ? 0 : 1;
220 if ($single_species != 1) {
221 # for multispecies, get the list of species from meta
225 "SELECT species_id,meta_value FROM $dbname.meta WHERE meta_key='species.production_name'"
227 if (!defined $opts->{$species_id} &&
228 scalar(@{$species_ids}) == 0)
230 croak
"No species.production_name found in database";
233 # if we didn't get a list from meta, go ahead and use the supplied arguments if we have them
234 if (defined $opts->{$species_id}) {
235 $species_ids = [[$opts->{$species_id}, $opts->{$species}]];
237 # otherwise assume the default species
238 elsif(!defined $species_ids) {
239 $species_ids = [[1, undef]];
241 # deal with each species in turn
242 for my $species_id (@{$species_ids}) {
243 my $args = {-HOST => $opts->{$host},
244 -USER => $opts->{$user},
245 -PORT => $opts->{$port},
246 -PASS => $opts->{$pass},
248 -DRIVER => $opts->{$driver},
249 -SPECIES_ID => $species_id->[0],
250 -SPECIES => $species_id->[1],
251 -MULTISPECIES_DB => $multi};
252 $args->{-GROUP} = $group
if $group;
253 push(@db_args, $args);
255 } ## end
for my $dbname (@dbnames)
256 } ## end
if (defined $opts->{$host...})
258 croak
'(db)host arguments required';
261 } ## end sub get_dba_args_for_opts
263 =head2 get_dba_args_for_opts()
265 Arg [1] : Hash of options (e.g. parsed from command line options by process_args())
266 Arg [2] : If set to 1, the databases are assumed to have a single species only. Default is 0.
267 Arg [3] : Optional prefix to use when parsing e.g. dna
268 Description : Uses the parsed command line options to generate an array DBAdaptors.
269 : Note this can overload connections on a server
270 Returntype : Arrayref of
Bio::
EnsEMBL::DBSQL::DBAdaptor
271 Status : Under development
275 sub get_dbas_for_opts {
276 my ($self, $opts, $single_species, $prefix) = @_;
278 # get all the DBA details that we want to work with and create DBAs for each in turn
281 @{$self->get_dba_args_for_opts($opts, $single_species, $prefix)})
288 =head2 load_registry_for_opts
290 Arg [1] : Hash of options (e.g. parsed from command line options by process_args())
291 Arg [2] : Optional prefix to use when parsing e.g. dna or master
292 Description : Loads a Registry from the given options hash. If a C<registry>
293 option is given then the code will call C<load_all>. Otherwise
294 we use the database parameters given to call
295 C<load_registry_from_db()>.
296 Returntype : Integer of the number of DBAdaptors loaded
297 Status : Under development
301 sub load_registry_for_opts {
302 my ($self, $opts, $prefix) = @_;
304 if ($opts->{registry}) {
305 my $location = $opts->{registry};
308 my ($host, $port, $user, $pass) =
309 map { $prefix . $_ } qw(host port user pass);
310 my %args = (-HOST => $opts->{$host},
311 -PORT => $opts->{$port},
312 -USER => $opts->{$user},);
313 $args{-PASS} = $opts->{$pass};