EnsEMBL/Utils/CliHelper.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 169 CC 13 SCAR 43.3
CC
13
Cov
0%
get_dba_args_for_opts
156.0
load_registry_for_opts
6.0
new
2.0
CRAP 76.2
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  
22 =head1 CONTACT
23  
24 Please email comments or questions to the public Ensembl
25 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
26  
27 Questions may also be sent to the Ensembl help desk at
28 <http://www.ensembl.org/Help/Contact>.
29  
30 =cut
31  
32 =head1 NAME
33  
34 Bio::EnsEMBL::Utils::CliHelper
35  
36 =head1 VERSION
37  
38 $Revision$
39  
40 =head1 SYNOPSIS
41  
42 use Bio::EnsEMBL::Utils::CliHelper;
43  
44 my $cli = Bio::EnsEMBL::Utils::CliHelper->new();
45  
46 # get the basic options for connecting to a database server
47 my $optsd = $cli->get_dba_opts();
48  
49 # add another option
50 push(@$optsd,"print");
51  
52 # process the command line with the supplied options plus a reference to a help subroutine
53 my $opts = $cli->process_args($optsd,\&usage);
54
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
58 my $dba = new Bio::EnsEMBL::DBSQL::DBAdaptor(%{$db_args});
59 ...
60 if(defined $opts->{print}) {
61 ...
62 }
63 }
64
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
69 my $optsd =
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 );
73 # get the dna details
74 my ($dna_dba_details) =
75 @{ $cli_helper->get_dba_args_for_opts( $opts, 1, 'dna' ) };
76 my $dna_db =
77 Bio::EnsEMBL::DBSQL::DBAdaptor->new( %{$dna_dba_details} ) );
78  
79 =head1 DESCRIPTION
80  
81 Utilities for a more consistent approach to parsing and handling EnsEMBL script command lines
82  
83 =head1 METHODS
84  
85 See subroutines.
86  
87 =cut
88  
89 package Bio::EnsEMBL::Utils::CliHelper;
90 $Bio::EnsEMBL::Utils::CliHelper::VERSION = '114.0.0_50'; # TRIAL
91 $Bio::EnsEMBL::Utils::CliHelper::VERSION = '114.0.050';
92 use warnings;
93 use strict;
94  
95 use Carp;
96 use Getopt::Long qw(:config auto_version no_ignore_case);
97  
98 use Bio::EnsEMBL::Registry;
99 use Bio::EnsEMBL::DBSQL::DBConnection;
100 use Bio::EnsEMBL::DBSQL::DBAdaptor;
101  
102 my $dba_opts = [{args => ['host', 'dbhost', 'h'], type => '=s'},
103 {args => ['port', 'dbport', 'P'], type => ':i'},
104 {args => ['user', 'dbuser', 'u'], type => '=s'},
105 {args => ['pass', 'dbpass', 'p'], type => ':s'},
106 {args => ['dbname', 'D'], type => ':s'},
107 {args => ['pattern', 'dbpattern'], type => ':s'},
108 {args => ['driver'], type => ':s'},
109 {args => ['species_id'], type => ':i'},
110 {args => ['species'], type => ':i'},];
111  
112 =head2 new()
113  
114 Description : Construct a new instance of a CliHelper object
115 Returntype : Bio::EnsEMBL::Utils:CliHelper
116 Status : Under development
117  
118 =cut
119  
120 sub new {
121 my ($class, @args) = @_;
122 my $self = bless({}, ref($class) || $class);
123 return $self;
124 }
125  
126 =head2 get_dba_opts()
127  
128 Arg [1] : Optional prefix for dbnames e.g. dna
129 Description : Retrieves the standard options for connecting to one or more Ensembl databases
130 Returntype : Arrayref of option definitions
131 Status : Under development
132  
133 =cut
134  
135 sub get_dba_opts {
136 my ($self, $prefix) = @_;
137 $prefix ||= '';
138 my @dba_opts = map {
139 my $opt = join '|', map { $prefix . $_ } @{$_->{args}};
140 $opt . $_->{type};
141 } @{$dba_opts};
142 return \@dba_opts;
143 }
144  
145 =head2 process_args()
146  
147 Arg [1] : Arrayref of supported command line options (e.g. from get_dba_opts)
148 Arg [2] : Ref to subroutine to be invoked when -help or -? is supplied
149 Description : Retrieves the standard options for connecting to one or more Ensembl databases
150 Returntype : Hashref of parsed options
151 Status : Under development
152  
153 =cut
154  
155 sub process_args {
156 my ($self, $opts_def, $usage_sub) = @_;
157 my $opts = {};
158 push @{$opts_def}, q/help|?/ => $usage_sub;
159 GetOptions($opts, @{$opts_def}) ||
160 croak 'Could not parse command line arguments';
161 return $opts;
162 }
163  
164 =head2 get_dba_args_for_opts()
165  
166 Arg [1] : Hash of options (e.g. parsed from command line options by process_args())
167 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.
168 Arg [3] : Optional prefix to use when parsing e.g. dna
169 Description : Uses the parsed command line options to generate an array of DBAdaptor arguments
170 : (e.g. expands dbpattern, finds all species_ids for multispecies databases)
171 : These can then be passed directly to Bio::EnsEMBL::DBSQL::DBAdaptor->new()
172 Returntype : Arrayref of DBA argument hash refs
173 Status : Under development
174  
175 =cut
176  
177 sub get_dba_args_for_opts {
178 my ($self, $opts, $single_species_opt, $prefix) = @_;
179 $prefix ||= '';
180  
181 my ($host, $port, $user, $pass, $dbname,
182 $pattern, $driver, $species, $species_id)
183 = map { $prefix . $_ }
184 qw(host port user pass dbname pattern driver species species_id);
185
186 my @db_args;
187 if (defined $opts->{$host}) {
188 my $dbc =
189 Bio::EnsEMBL::DBSQL::DBConnection->new(-USER => $opts->{$user},
190 -PASS => $opts->{$pass},
191 -HOST => $opts->{$host},
192 -PORT => $opts->{$port},
193 -DRIVER => $opts->{$driver}
194 );
195 my @dbnames;
196 if (defined $opts->{$dbname}) {
197 push @dbnames, $opts->{$dbname};
198 }
199 elsif (defined $opts->{$pattern}) {
200 # get a basic DBConnection and use to find out which dbs are involved
201 @dbnames = grep { m/$opts->{$pattern}/smx }
202 @{$dbc->sql_helper()->execute_simple(q/SHOW DATABASES/)};
203 }
204 else {
205 croak 'dbname or dbpattern arguments required';
206 }
207 for my $dbname (@dbnames) {
208  
209 #Decipher group of DBAdaptor by capturing the name_name(_name?)_core_ code. Otherwise we don't know
210 my ($group) = $dbname =~
211 /^[a-z]+_[a-z0-9]+(?:_[a-z0-9]+)?_([a-z]+)(?:_\d+)?_\d+/;
212 # set multi where we have collections
213 my $multi = $dbname =~ m/_collection_/ ? 1 : 0;
214 my $species_ids;
215 my $single_species = $single_species_opt;
216  
217 if (!defined $single_species) {
218 # if we're dealing with a collection, turn off single species mode by default
219 $single_species = $dbname =~ m/_collection_/ ? 0 : 1;
220 }
221 if ($single_species != 1) {
222 # for multispecies, get the list of species from meta
223 $species_ids =
224 $dbc->sql_helper()
225 ->execute(
226 "SELECT species_id,meta_value FROM $dbname.meta WHERE meta_key='species.production_name'"
227 );
228 if (!defined $opts->{$species_id} &&
229 scalar(@{$species_ids}) == 0)
230 {
231 croak "No species.production_name found in database";
232 }
233 }
234 # if we didn't get a list from meta, go ahead and use the supplied arguments if we have them
235 if (defined $opts->{$species_id}) {
236 $species_ids = [[$opts->{$species_id}, $opts->{$species}]];
237 }
238 # otherwise assume the default species
239 elsif(!defined $species_ids) {
240 $species_ids = [[1, undef]];
241 }
242 # deal with each species in turn
243 for my $species_id (@{$species_ids}) {
244 my $args = {-HOST => $opts->{$host},
245 -USER => $opts->{$user},
246 -PORT => $opts->{$port},
247 -PASS => $opts->{$pass},
248 -DBNAME => $dbname,
249 -DRIVER => $opts->{$driver},
250 -SPECIES_ID => $species_id->[0],
251 -SPECIES => $species_id->[1],
252 -MULTISPECIES_DB => $multi};
253 $args->{-GROUP} = $group if $group;
254 push(@db_args, $args);
255 }
256 } ## end for my $dbname (@dbnames)
257 } ## end if (defined $opts->{$host...})
258 else {
259 croak '(db)host arguments required';
260 }
261 return \@db_args;
262 } ## end sub get_dba_args_for_opts
263  
264 =head2 get_dba_args_for_opts()
265  
266 Arg [1] : Hash of options (e.g. parsed from command line options by process_args())
267 Arg [2] : If set to 1, the databases are assumed to have a single species only. Default is 0.
268 Arg [3] : Optional prefix to use when parsing e.g. dna
269 Description : Uses the parsed command line options to generate an array DBAdaptors.
270 : Note this can overload connections on a server
271 Returntype : Arrayref of Bio::EnsEMBL::DBSQL::DBAdaptor
272 Status : Under development
273  
274 =cut
275  
276 sub get_dbas_for_opts {
277 my ($self, $opts, $single_species, $prefix) = @_;
278  
279 # get all the DBA details that we want to work with and create DBAs for each in turn
280 my $dbas;
281 for my $args (
282 @{$self->get_dba_args_for_opts($opts, $single_species, $prefix)})
283 {
284 push @{$dbas}, Bio::EnsEMBL::DBSQL::DBAdaptor->new(%{$args});
285 }
286 return $dbas;
287 }
288  
289 =head2 load_registry_for_opts
290  
291 Arg [1] : Hash of options (e.g. parsed from command line options by process_args())
292 Arg [2] : Optional prefix to use when parsing e.g. dna or master
293 Description : Loads a Registry from the given options hash. If a C<registry>
294 option is given then the code will call C<load_all>. Otherwise
295 we use the database parameters given to call
296 C<load_registry_from_db()>.
297 Returntype : Integer of the number of DBAdaptors loaded
298 Status : Under development
299  
300 =cut
301  
302 sub load_registry_for_opts {
303 my ($self, $opts, $prefix) = @_;
304 $prefix ||= q{};
305 if ($opts->{registry}) {
306 my $location = $opts->{registry};
307 return Bio::EnsEMBL::Registry->load_all($location);
308 }
309 my ($host, $port, $user, $pass) =
310 map { $prefix . $_ } qw(host port user pass);
311 my %args = (-HOST => $opts->{$host},
312 -PORT => $opts->{$port},
313 -USER => $opts->{$user},);
314 $args{-PASS} = $opts->{$pass};
315 return Bio::EnsEMBL::Registry->load_registry_from_db(%args);
316 }
317  
318 1;