-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiamonddb.sh
325 lines (247 loc) · 6.77 KB
/
diamonddb.sh
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env bash
# safety first
set \
-o errexit \
-o pipefail \
-o noglob \
-o nounset \
app=$(basename "$0" .sh)
version=$(git describe --always --long --dirty 2> /dev/null) ||
version="0.3.1"
# get utilities
source libscddl.sh
# -----------------------------------------------------------------------------
# usage
# -----------------------------------------------------------------------------
function usage { cat << EOF
$app $version
USAGE
$app [options] [--] prefix in map nodes
DESCRIPTION
create diamond database from NCBI data sets
ARGUMENTS
prefix the local data set directory,
example: /data/db
the data set will be put in:
\$prefix/diamond/\$dataset/\$(date +%F)
in input reference data set
passed to diamond makedb --in
example: blast/db/FASTA/pdbaa
map protein accession to taxid mapping data set
passed to diamond makedb --taxonmap
example:
pub/taxonomy/accesion2taxid/pdb.accession2taxid
nodes taxonomy data set
passed to diamond makedb --taxonnodes
example: pub/taxonomy/taxdump
-- ends option parsing
OPTIONS
-p, --parallel cores use \$cores parallelism,
default: number of cores available
OUTPUT OPTIONS
--color[=WHEN] whether to use colored output
WHEN can be 'always', 'yes', 'never', 'no', or 'auto'
--syslog write output to syslog, useful for cron jobs
note: systemd timers usually do not need this because
output is automatically sent to the journal
--debug output every command as it executes
-v, --verbose enables verbose output
-q, --quiet disables both debug and verbose
OTHER OPTIONS
-?, --help shows this help text
--version shows this tools version
EOF
}
# -----------------------------------------------------------------------------
# configuration
# -----------------------------------------------------------------------------
cores=$(grep -c ^processor /proc/cpuinfo)
color=auto
syslog=no
debug=no
verbose=no
ignore_next_arg=no
for arg in "$@"
do
if [[ $ignore_next_arg == yes ]]
then
ignore_next_arg=no
continue
fi
case "$arg" in
-\?|--help)
usage
exit
;;
--version)
echo "$app $version"
exit
;;
-p|--parallel)
shift
cores=${1:?"parallel option has no argument"}
[[ $cores =~ ^[0-9]+$ ]] ||
bailout "parallel option argument is not a number: $cores"
ignore_next_arg=yes
shift
;;
--color|--color=always|--color=yes)
color=yes
shift
;;
--color=never|--color=no)
color=no
shift
;;
--color=auto)
color=auto
shift
;;
--syslog)
syslog=yes
shift
;;
--debug)
debug=yes
shift
;;
--debug=yes|--debug=no)
debug=${arg##--debug=}
shift
;;
-q|--quiet)
debug=no
verbose=no
shift
;;
-v|--verbose)
verbose=yes
shift
;;
--verbose=yes|--verbose=no)
verbose=${arg##--verbose=}
shift
;;
--)
shift
break
;;
-*)
bailout "unrecognized option: $arg"
;;
*)
break
;;
esac
done
set +o nounset
prefix=$1
shift || bailout "missing argument: prefix"
in=$1
shift || bailout "missing argument: in"
map=$1
shift || bailout "missing argument: map"
nodes=$1
shift || bailout "missing argument: nodes"
set -o nounset
if [[ "$*" != "" ]]
then
bailout "trailing arguments: $*"
fi
# trim trailing slashes
shopt -s extglob
prefix="${prefix%%+(/)}"
in="${in%%+(/)}"
map="${map%%+(/)}"
nodes="${nodes%%+(/)}"
shopt -u extglob
# -----------------------------------------------------------------------------
# verbose: output configuration
# -----------------------------------------------------------------------------
if [[ $verbose == yes ]]
then
cat << EOF
prefix: $prefix
in: $in
map: $map
nodes: $nodes
parallel: $cores CPU cores
versions:
- $app $version
- $(diamond --version)
EOF
diamond_verbosity="--verbose"
else
diamond_verbosity="--quiet"
fi
# -----------------------------------------------------------------------------
# debug mode
# -----------------------------------------------------------------------------
[[ $debug == yes ]] &&
set -o xtrace
# -----------------------------------------------------------------------------
# check arguments
# -----------------------------------------------------------------------------
[[ -d $prefix ]] ||
bailout "local data set directory does not exist: $prefix"
# -----------------------------------------------------------------------------
# external tools
# -----------------------------------------------------------------------------
tool.available diamond
# -----------------------------------------------------------------------------
# preparation
# -----------------------------------------------------------------------------
download_date=$(date +%F)
name=$(basename "$in")
output_dir="$prefix"/diamond/"$name"/"$download_date"
output="$output_dir/$name.dmnd"
app_log="$output_dir/.scddl.log"
[[ -e $output ]] &&
bailout 'output already exists, not overwriting'
datasets=()
for dataset in "$in" "$map" "$nodes"
do
d_path="$prefix/ncbi/$dataset/$download_date"
if [[ -e "$d_path" ]]
then
log.verbose "$dataset already exists, not downloading again"
else
log.verbose "adding to download list: $dataset"
datasets+=("$dataset")
fi
done
d_in="$prefix/ncbi/$in/$download_date"
d_tm="$prefix/ncbi/$map/$download_date"
d_tn="$prefix/ncbi/$nodes/$download_date"
# -----------------------------------------------------------------------------
# application
# -----------------------------------------------------------------------------
if [[ ${#datasets[@]} -gt 0 ]]
then
log.verbose "downloading datasets"
bash \
"$(dirname "$0")"/ncbidl.sh \
--debug="$debug" \
--verbose="$verbose" \
--parallel "$cores" \
"$prefix" \
"${datasets[@]}" ||
bailout 'downloading data sets failed'
else
log.verbose "all datasets already downloaded"
fi
log.verbose "generating diamond db"
mkdir -p "$output_dir"
_taxonmap="$(find "$d_tm" -name '*.accession2taxid' | head -1)"
run.log \
"$app_log" \
diamond \
makedb \
$diamond_verbosity \
--threads "$cores" \
--in "$d_in"/"$name" \
--taxonmap "$_taxonmap" \
--taxonnodes "$d_tn"/nodes.dmp \
--db "$output" ||
bailout 'generating diamond db failed'
log.verbose "done"