This repository was archived by the owner on Aug 10, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathCLdb
More file actions
executable file
·258 lines (182 loc) · 5.27 KB
/
Copy pathCLdb
File metadata and controls
executable file
·258 lines (182 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env perl
use strict;
use warnings;
=pod
=head1 NAME
CLdb -- CRISPR locus database (v0.2)
=head1 SYNOPSIS
CLdb [options] -- subcommand [subcommand_options]
=head2 Options
=over
=item --list
List all subcommands.
=item --perldoc
Get perldoc of subcommand.
=item --sql
SQL passed to subcommand for limiting queries.
(eg., --sql 'loci.subtype == "I-B" or loci.subtype == "I-C"').
NOTE: The sql statement must go in SINGLE quotes!
=item --config
Config file (if not ~/.CLdb)
=item --config-params
List params set by config
=item -v Verbose output
=item -h This help message
=back
=head2 For more information:
perldoc CLdb
=head1 DESCRIPTION
CLdb is a local database of CRISPR-Cas systems
and a set of tools to analyses that data.
This is the main command which calls all
subcommands (much like git).
=head2 Command completion
Subcommands will be listed after ' -- '.
For example: `CLdb -- <tab><tab>`
will list all primary subcommands for CLdb.
Some sub-subcommands exist.
For example: `CLdb -- arrayBlast -- run`
Make sure command completion is enabled
by editing, then sourcing the files in
the CLdb/extras/ folder
=head2 Config file
For most subcommands, the CLdb database location
must be provided (via the '-database' flag).
You can skip this by making a CLdb config file
with the CLdb database location assigned to the
`DATABASE` variable (Example: DATABASE = '/home/nick/CLdb.sqlite/').
=head1 AUTHOR
Nick Youngblut <nyoungb2@illinois.edu>
=head1 AVAILABILITY
sharchaea.life.uiuc.edu:/home/git/CLdb/
=head1 COPYRIGHT
Copyright 2010, 2011
This software is licensed under the terms of the GPLv3
=cut
use Data::Dumper;
use Getopt::Long;
use Pod::Usage;
use FindBin;
use lib "$FindBin::RealBin/../lib";
use IPC::Cmd qw/run can_run/;
use File::Spec;
#--- option parsing ---#
pod2usage("$0: No files given.") if ((@ARGV == 0) && (-t STDIN));
sub Pod2Usage{ pod2usage({@_, -exitval => 0}) }
my ($verbose, $listSubcmds, $getPerlDoc,
$sql, $config, $config_params);
GetOptions (
"--list" => \$listSubcmds,
"--perldoc" => \$getPerlDoc,
"--sql=s" => \$sql,
"--config=s" => \$config,
"--config-params" => \$config_params,
"--verbose" => \$verbose,
"-help|?" => \&Pod2Usage
);
#-- intial assertions/defaults --#
my $bindir = File::Spec->catdir($FindBin::RealBin, 'bin');
# config
my $database = CLdb_path($config);
list_config_params({DATABASE => $database}) if $config_params;
# check that script exists
my $scripts_r = list_scripts($bindir, $listSubcmds);
pod2usage("ERROR: No subcommand given.") unless $ARGV[0];
(my $subcmd = $ARGV[0]) =~ s/\.pl$//i;
if (! grep(/^$subcmd(\.pl)*$/, @$scripts_r)){
die "ERROR: '" . $ARGV[0] . "' is not a a valid subcommand\n";
}
# sql
$sql = check_sql($sql);
#--- MAIN ---#
call_subcommand($bindir, \@ARGV, $getPerlDoc, $sql, $database);
#--- subroutines ---#
sub call_subcommand{
my $bindir = shift or die $!; # CLdb bin directory
my $argv_r = shift or die $!; # subcommand args
my $getPerlDoc = shift; # calling perldoc for subcommand
my $sql = shift; # sql for subcommand (-q)
my $database = shift; # database for subcommand (-database)
# check that subcommand exists
(my $subcmd = $argv_r->[0]) =~ s/(\.pl)*$/.pl/i;
$subcmd = File::Spec->join($bindir, $subcmd);
unless (can_run($subcmd)){
die "ERROR: '$subcmd' is not executable.\n"
}
# adding quotes to any args w/ spaces
@$argv_r = map{/ / ? join('', '"', $_, '"') : $_} @$argv_r;
# perldoc
if ($getPerlDoc){
print `perldoc -T $subcmd`;
}
# calling subcommand
else{
$subcmd = join(" ", $subcmd, "-database $database") if $database;
$subcmd = join(" ", $subcmd, "-q \"$sql\"") if $sql;
$subcmd = join(" ", $subcmd, @$argv_r[1..$#$argv_r]);
print `$subcmd`;
}
}
sub list_scripts{
my $bindir = shift or die $!;
my $listSubcmds = shift;
# getting scripts
opendir INDIR, $bindir or die $!;
my @scripts = grep(/\.pl$/i, readdir INDIR);
closedir INDIR or die $!;
# listing or returning
if ($listSubcmds){
map(s/\.pl//i, @scripts);
print join("\n", sort @scripts), "\n";
exit;
}
else{
return \@scripts;
}
}
sub check_sql{
$sql = shift;
return '' unless $sql;
if ($sql =~ /\'/){
die "ERROR: the --sql statement must be wrapped in SINGLE quotes"
}
$sql =~ s/^\s*(and )*/AND /i;
$sql =~ s/ *$/ /;
$sql =~ s/(?<=[^\\])("|')/\\$1/g;
return $sql;
}
sub CLdb_path{
my $config = shift;
my $default_config = glob("~/.CLdb");
# checking config if provided
if ($config){
return _parse_config($config);
}
# checking default config location
elsif (-e $default_config){
return _parse_config($default_config);
}
return undef;
}
sub _parse_config{
my $config = shift;
open FH, $config
or die "Cannot open config file: $config\n";
my $file_path = undef;
while(<FH>){
if(/^\s*DATABASE\s*[=:]/){
(my $t, $file_path) = split /\s*[=:]\s*/, $_, 2;
}
}
my $msg = "WARNING: could not find 'DATABASE' variable in CLdb config\n";
print STDERR $msg unless $file_path;
return $file_path;
}
sub list_config_params{
my $params_r = shift;
print STDERR "#-- Config params --#\n";
while (my ($k,$v) = each %$params_r){
print STDERR "$k = $v\n";
}
exit;
}