| 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 | =head1 AUTHOR | ||
| 30 | |||
| 31 | Juguang Xiao <juguang@tll.org.sg> | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | =head1 NAME | ||
| 36 | |||
| 37 | Bio::EnsEMBL::Utils::EasyArgv | ||
| 38 | |||
| 39 | =head1 SYNOPSIS | ||
| 40 | |||
| 41 | use Bio::EnsEMBL::Utils::EasyArgv; | ||
| 42 | |||
| 43 | my $db = get_ens_db_from_argv; # this method is exported. | ||
| 44 | |||
| 45 | use Getopt::Long; | ||
| 46 | |||
| 47 | my $others; | ||
| 48 | &GetOptions( 'others=s' => \$others ); | ||
| 49 | |||
| 50 | =head1 DESCRIPTION | ||
| 51 | |||
| 52 | This is a lazy but easy way to get the db-related arguments. All you | ||
| 53 | need to do is to invoke get_ens_db_from_argv before using standard | ||
| 54 | Getopt. The below options will be absorbed and removed from @ARGV. | ||
| 55 | |||
| 56 | db_file, host, db_host, dbhost, user, db_user, dbuser, pass, db_pass, | ||
| 57 | dbpass, dbname, db_name. | ||
| 58 | |||
| 59 | Now you can take advantage of Perl's do method to execute a file as perl | ||
| 60 | script and get returned the last line of it. For your most accessed db | ||
| 61 | setting, you can have a file named, say, ensdb_homo_core_18.perlobj, | ||
| 62 | with the content like | ||
| 63 | |||
| 64 | use strict; # The ceiling line | ||
| 65 | |||
| 66 | use Bio::EnsEMBL::DBSQL::DBAdaptor; | ||
| 67 | |||
| 68 | my $db = Bio::EnsEMBL::DBSQL::DBAdaptor->new( | ||
| 69 | -host => 'ensembldb.ensembl.org', | ||
| 70 | -user => 'anonymous', | ||
| 71 | -dbname => 'homo_sapiens_core_18_34' | ||
| 72 | ); | ||
| 73 | |||
| 74 | $db; # The floor line | ||
| 75 | |||
| 76 | In the your command line, you just need to write like | ||
| 77 | |||
| 78 | perl my_script.pl -db_file ensdb_homo_core_18.perlobj | ||
| 79 | |||
| 80 | rather than the verbose | ||
| 81 | |||
| 82 | -host ensembldb.ensembl.org -user anonymous \ | ||
| 83 | -dbname homo_sapiens_core_18_34 | ||
| 84 | |||
| 85 | =head1 METHODS | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | package Bio::EnsEMBL::Utils::EasyArgv; | ||
| 90 | $Bio::EnsEMBL::Utils::EasyArgv::VERSION = '114.0.0'; | ||
| 91 | use strict; | ||
| 92 | use vars qw($debug); | ||
| 93 | use Exporter (); | ||
| 94 | our @ISA= qw(Exporter); | ||
| 95 | our @EXPORT = qw(get_ens_db_from_argv | ||
| 96 | ); | ||
| 97 | use Bio::Root::Root; # For _load_module | ||
| 98 | use Getopt::Long; | ||
| 99 | |||
| 100 | sub _debug_print; | ||
| 101 | |||
| 102 | sub get_ens_db_from_argv { | ||
| 103 | my ($db_file, $host, $user, $pass, $dbname, $driver, $db_module); | ||
| 104 | $host = 'localhost'; | ||
| 105 | $driver ='mysql'; | ||
| 106 | $db_module = 'Bio::EnsEMBL::SQL::DBAdaptor'; | ||
| 107 | Getopt::Long::config('pass_through'); | ||
| 108 | &GetOptions( | ||
| 109 | 'db_file=s' => \$db_file, | ||
| 110 | 'driver|dbdriver|db_driver=s' => \$driver, | ||
| 111 | 'host|dbhost|db_host=s' => \$host, | ||
| 112 | 'user|dbuser|db_user=s' => \$user, | ||
| 113 | 'pass|dbpass|db_pass=s' => \$pass, | ||
| 114 | 'dbname|db_name=s' => \$dbname, | ||
| 115 | 'db_module=s' => \$db_module | ||
| 116 | ); | ||
| 117 | |||
| 118 | my $db; | ||
| 119 | if(defined $db_file){ | ||
| 120 | -e $db_file or die "'$db_file' is defined but does not exist\n"; | ||
| 121 | eval { $db = do($db_file) }; | ||
| 122 | $@ and die "'$db_file' is not a perlobj file\n"; | ||
| 123 | $db->isa('Bio::EnsEMBL::DBSQL::DBAdaptor') | ||
| 124 | or die "'$db_file' is not EnsEMBL DBAdaptor\n"; | ||
| 125 | _debug_print "I get a db from file\n"; | ||
| 126 | |||
| 127 | }elsif(defined $host and defined $user and defined $dbname){ | ||
| 128 | Bio::Root::Root::_load_module($db_module); | ||
| 129 | $db = $db_module->new( | ||
| 130 | -host => $host, | ||
| 131 | -user => $user, | ||
| 132 | -pass => $pass, | ||
| 133 | -dbname => $dbname, | ||
| 134 | -driver => $driver | ||
| 135 | ); | ||
| 136 | }else{ | ||
| 137 | die "Cannot get the db, due to the insufficient information\n"; | ||
| 138 | } | ||
| 139 | return $db; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub _debug_print { | ||
| 143 | print STDERR @_ if $debug; | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | 1; |