Archive Ensembl HomeArchive Ensembl Home
EasyArgv.pm
Go to the documentation of this file.
00001 =head1 LICENSE
00002 
00003   Copyright (c) 1999-2012 The European Bioinformatics Institute and
00004   Genome Research Limited.  All rights reserved.
00005 
00006   This software is distributed under a modified Apache license.
00007   For license details, please see
00008 
00009     http://www.ensembl.org/info/about/code_licence.html
00010 
00011 =head1 CONTACT
00012 
00013   Please email comments or questions to the public Ensembl
00014   developers list at <dev@ensembl.org>.
00015 
00016   Questions may also be sent to the Ensembl help desk at
00017   <helpdesk@ensembl.org>.
00018 
00019 =head1 AUTHOR
00020 
00021 Juguang Xiao <juguang@tll.org.sg>
00022 
00023 =cut
00024 
00025 =head1 NAME
00026 
00027 Bio::EnsEMBL::Utils::EasyArgv
00028 
00029 =head1 SYNOPSIS
00030 
00031   use Bio::EnsEMBL::Utils::EasyArgv;
00032 
00033   my $db = get_ens_db_from_argv;    # this method is exported.
00034 
00035   use Getopt::Long;
00036 
00037   my $others;
00038   &GetOptions( 'others=s' => \$others );
00039 
00040 =head1 DESCRIPTION
00041 
00042 This is a lazy but easy way to get the db-related arguments. All you
00043 need to do is to invoke get_ens_db_from_argv before using standard
00044 Getopt. The below options will be absorbed and removed from @ARGV.
00045 
00046 db_file, host, db_host, dbhost, user, db_user, dbuser, pass, db_pass,
00047 dbpass, dbname, db_name.
00048 
00049 Now you can take advantage of Perl's do method to execute a file as perl
00050 script and get returned the last line of it. For your most accessed db
00051 setting, you can have a file named, say, ensdb_homo_core_18.perlobj,
00052 with the content like
00053 
00054   use strict;    # The ceiling line
00055 
00056   use Bio::EnsEMBL::DBSQL::DBAdaptor;
00057 
00058   my $db = Bio::EnsEMBL::DBSQL::DBAdaptor->new(
00059     -host   => 'ensembldb.ensembl.org',
00060     -user   => 'anonymous',
00061     -dbname => 'homo_sapiens_core_18_34'
00062   );
00063 
00064   $db;           # The floor line
00065 
00066 In the your command line, you just need to write like 
00067 
00068   perl my_script.pl -db_file ensdb_homo_core_18.perlobj
00069 
00070 rather than the verbose
00071 
00072   -host ensembldb.ensembl.org -user anonymous \
00073   -dbname homo_sapiens_core_18_34
00074 
00075 =head1 METHODS
00076 
00077 =cut
00078 
00079 package Bio::EnsEMBL::Utils::EasyArgv;
00080 
00081 use strict;
00082 use vars qw($debug);
00083 use Exporter ();
00084 our @ISA= qw(Exporter);
00085 our @EXPORT = qw(get_ens_db_from_argv
00086 );
00087 use Bio::Root::Root; # For _load_module
00088 use Getopt::Long;
00089 
00090 sub _debug_print;
00091 
00092 sub get_ens_db_from_argv {
00093     my ($db_file, $host, $user, $pass, $dbname, $driver, $db_module);
00094     $host = 'localhost';
00095     $driver ='mysql';
00096     $db_module = 'Bio::EnsEMBL::SQL::DBAdaptor';
00097     Getopt::Long::config('pass_through');
00098     &GetOptions(
00099         'db_file=s' => \$db_file,
00100         'driver|dbdriver|db_driver=s' => \$driver,
00101         'host|dbhost|db_host=s' => \$host,
00102         'user|dbuser|db_user=s' => \$user,
00103         'pass|dbpass|db_pass=s' => \$pass,
00104         'dbname|db_name=s' => \$dbname,
00105         'db_module=s' => \$db_module
00106     );
00107 
00108     my $db;
00109     if(defined $db_file){
00110         -e $db_file or die "'$db_file' is defined but does not exist\n";
00111         eval { $db = do($db_file) };
00112         $@ and die "'$db_file' is not a perlobj file\n";
00113         $db->isa('Bio::EnsEMBL::DBSQL::DBAdaptor')
00114             or die "'$db_file' is not EnsEMBL DBAdaptor\n";
00115         _debug_print "I get a db from file\n";
00116         
00117     }elsif(defined $host and defined $user and defined $dbname){
00118         Bio::Root::Root::_load_module($db_module);
00119         $db = $db_module->new(
00120             -host => $host,
00121             -user => $user,
00122             -pass => $pass,
00123             -dbname => $dbname,
00124             -driver => $driver
00125         );
00126     }else{
00127         die "Cannot get the db, due to the insufficient information\n";
00128     }
00129     return $db;
00130 }
00131 
00132 sub _debug_print {
00133     print STDERR @_ if $debug;
00134 }
00135 
00136 
00137 1;