-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalign_prism.sh
executable file
·527 lines (469 loc) · 17.4 KB
/
align_prism.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/bin/bash
declare -a references_array
declare -a parameters_array
declare -a files_array
function get_opts() {
DRY_RUN=no
DEBUG=no
HPC_TYPE=slurm
FILES=""
OUT_DIR=""
SAMPLE_RATE=""
MAX_TASKS=50
ALIGNER=blastn
FORCE=no
PARAMETERS=""
REFERENCES=none
NUM_REFERENCES=1
MEM_PER_CPU=8G
MAX_WALL_TIME=2
NUM_THREADS=8
tardis_environment_include=""
help_text="
\n
./align_prism.sh [-h] [-n] [-d] [-f] [-j num_threads] [-B mem_per_cpu] [-W max_walltime ] [-s SAMPLE_RATE] -a aligner -r [ref name | file of ref names ] -p [ parameters or file of parameters ] -O outdir [-C local|slurm ] input_file_names\n
\n
\n
example:\n
./align_prism.sh -n -D /dataset/Tash_FL1_Ryegrass/ztmp/For_Alan -O /dataset/Tash_FL1_Ryegrass/ztmp/seq_qc/test/fastqc /dataset/Tash_FL1_Ryegrass/ztmp/For_Alan/*.fastq.gz\n
\n
"
# defaults:
while getopts ":nhfO:C:s:m:a:r:p:B:j:W:e:" opt; do
case $opt in
n)
DRY_RUN=yes
;;
d)
DEBUG=yes
;;
f)
FORCE=yes
;;
h)
echo -e $help_text
exit 0
;;
O)
OUT_DIR=$OPTARG
;;
C)
HPC_TYPE=$OPTARG
;;
s)
SAMPLE_RATE=$OPTARG
;;
a)
ALIGNER=$OPTARG
;;
p)
PARAMETERS=$OPTARG
;;
r)
REFERENCES=$OPTARG
# also assign to array (if arg is a filename containing db names, this will be updated later)
references_array=($OPTARG)
NUM_REFERENCES=${#references_array[*]}
;;
e)
tardis_environment_include=$OPTARG # code in this file will be included in wrapper - e.g. conda activate some_env
;;
m)
MAX_TASKS=$OPTARG
;;
B)
MEM_PER_CPU=$OPTARG
;;
W)
MAX_WALL_TIME=$OPTARG
;;
j)
NUM_THREADS=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
FILE_STRING=$@
# this is needed because of the way we process args a "$@" - which
# is needed in order to parse parameter sets to be passed to the
# aligner (which are space-separated)
declare -a files="(${FILE_STRING})";
NUM_FILES=${#files[*]}
for ((i=0;$i<$NUM_FILES;i=$i+1)) do
files_array[$i]=${files[$i]}
done
if [ $NUM_FILES == 0 ]; then
echo "exiting as no input files specified"
exit 0
fi
}
function test_if_file_of_dbnames() {
# test whether the arg is a file that contains the name of blast or other databases, by checking if each record in it is the
# name of a file , or a plausible basename of a database. This is somewhat fallible , and it is better to
# specify multiple references on the command line
is_file_of_dbnames=1
rp=`realpath $1`
if [ -z "$1" ]; then
is_file_of_dbnames=0
elif [[ ( ! -f $1 ) && ( ! -h $1 ) ]]; then
is_file_of_dbnames=0
elif [[ ( ! -f $rp ) && ( ! -h $rp ) ]]; then
is_file_of_dbnames=0
else
IFS=$'\n'
# if the first and last 50 records are filenames, or suffixes of common index files, assume file_of_dbnames
for record in `awk '/\S+/{print}' $1 | head -50`; do
if [[ ( ! -f $record ) && ( ! -h $record ) ]]; then
is_file_of_dbnames=0
fi
# check for possibility of list of blast database names
if [[ ( -f ${record}.nal ) || ( -h ${record}.nal ) ]]; then
is_file_of_dbnames=1
elif [[ ( -f ${record}.pal ) || ( -h ${record}.pal ) ]]; then
is_file_of_dbnames=1
elif [[ ( -f ${record}.nin ) || ( -h ${record}.nin ) ]]; then
is_file_of_dbnames=1
elif [[ ( -f ${record}.00.nin ) || ( -h ${record}.00.nin ) ]]; then
is_file_of_dbnames=1
elif [[ ( -f ${record}.pin ) || ( -h ${record}.pin ) ]]; then
is_file_of_dbnames=1
elif [[ ( -f ${record}.00.pin ) || ( -h ${record}.00.pin ) ]]; then
is_file_of_dbnames=1
fi
if [ $is_file_of_dbnames == 0 ]; then
break
fi
done
for record in `awk '/\S+/{print}' $1 | tail -50`; do
if [[ ( ! -f $record ) && ( ! -h $record ) ]]; then
is_file_of_dbnames=0
fi
# check for possibility of list of blast database names
if [[ ( -f ${record}.nal ) || ( -h ${record}.nal ) ]]; then
is_file_of_dbnames=1
elif [[ ( -f ${record}.pal ) || ( -h ${record}.pal ) ]]; then
is_file_of_dbnames=1
elif [[ ( -f ${record}.nin ) || ( -h ${record}.nin ) ]]; then
is_file_of_dbnames=1
elif [[ ( -f ${record}.00.nin ) || ( -h ${record}.00.nin ) ]]; then
is_file_of_dbnames=1
elif [[ ( -f ${record}.pin ) || ( -h ${record}.pin ) ]]; then
is_file_of_dbnames=1
elif [[ ( -f ${record}.00.pin ) || ( -h ${record}.00.pin ) ]]; then
is_file_of_dbnames=1
fi
if [ $is_file_of_dbnames == 0 ]; then
break
fi
done
unset IFN
fi
}
function test_if_f() {
# test whether the arg is a link or file
is_f=1
if [ -z "$1" ]; then
is_f=0
elif [[ ( ! -f $1 ) && ( ! -h $1 ) ]]; then
is_f=0
fi
}
function check_opts() {
python -c "x=int(\"$NUM_THREADS\")" > /dev/null 2>&1
if [ $? != 0 ]; then
echo "error bad value for NUM_THREADS ( $NUM_THREADS )"
exit 1
fi
python -c "x=int(\"$MAX_WALL_TIME\")" > /dev/null 2>&1
if [ $? != 0 ]; then
echo "error bad value for MAX_WALL_TIME ( $MAX_WALL_TIME )"
exit 1
fi
if [ -z "$OUT_DIR" ]; then
echo "must specify an output folder (-O option)"
exit 1
fi
OUT_DIR=`realpath $OUT_DIR`
if [ ! -d $OUT_DIR ]; then
echo "OUT_DIR $OUT_DIR not found"
exit 1
fi
if [[ $HPC_TYPE != "local" && $HPC_TYPE != "slurm" ]]; then
echo "HPC_TYPE must be one of local, slurm"
exit 1
fi
if [[ $ALIGNER != "blastn" && $ALIGNER != "qblastn" && $ALIGNER != "bwa" && $ALIGNER != "qblastx" && $ALIGNER != "tblastx" && $ALIGNER != "blastp" && $ALIGNER != "blastx" ]]; then
echo "ALIGNER must be one of blastn, qblastn, bwa, blastx, qblastx, tblastx, blastp, blastx"
exit 1
fi
if [ "$REFERENCES" == none ]; then
echo "must specify one or more references to align against (-r [ref name | file of ref names ] )"
exit 1
fi
test_if_file_of_dbnames $REFERENCES
if [[ ( $is_file_of_dbnames != 1 ) && ( $NUM_REFERENCES == 1 ) ]]; then
# just one reference, specified on command line
# see how parameters are supplied
test_if_f $PARAMETERS
if [ -z "$PARAMETERS" ]; then
echo "warning , no alignment parameters supplied, $ALIGNER defaults will apply"
parameters_array[0]=$PARAMETERS
NUM_PARAMETERS=1
elif [ $is_f != 1 ]; then
parameters_array[0]=$PARAMETERS
NUM_PARAMETERS=1
else # we have a filename listing a number n of alt parameters ( - so will duplicate reference n times )
index=0
for record in `awk '/\S+/{print}' $PARAMETERS`; do
parameters_array[$index]=$record
let index=$index+1
done
NUM_PARAMETERS=${#parameters_array[*]}
echo "cloning $NUM_PARAMETERS references"
for ((i=0;$i<$NUM_PARAMETERS;i=$i+1)) do
references_array[$i]=$REFERENCES
done
fi
else # we need to allow for multiple references and may need to pull out the list of references from a file
# if they were listed in a file
if [ $is_file_of_dbnames == 1 ]; then
index=0
IFS=$'\n'
for record in `awk '/\S+/{print}' $REFERENCES`; do
references_array[$index]=$record
let index=$index+1
done
NUM_REFERENCES=${#references_array[*]}
fi
test_if_f $PARAMETERS
#( its not possible to specify multiple parameter sets on command-line - must supply in a file)
if [ -z "$PARAMETERS" ]; then
echo "warning , no alignment parameters supplied, $ALIGNER defaults will apply"
NUM_PARAMETERS=1
fi
if [ $is_f != 1 ]; then
echo "cloning $NUM_REFERENCES parameter sets"
for ((i=0;$i<$NUM_REFERENCES;i=$i+1)) do
parameters_array[$i]=$PARAMETERS
done
NUM_PARAMETERS=1
else
index=0
for record in `awk '/\S+/{print}' $PARAMETERS`; do
parameters_array[$index]=$record
let index=$index+1
done
NUM_PARAMETERS=${#parameters_array[*]}
fi
unset IFS
if [ ${#parameters_array[*]} != ${#references_array[*]} ]; then
echo "error - have ${#references_array[*]} references but ${#parameters_array[*]} parameter sets - must have same number of each !"
exit 1
fi
if [ ! -z "$tardis_environment_include" ]; then
if [ ! -f $tardis_environment_include ]; then
echo "error - tardis_environment_include (i.e. -e otion) must be a file containing code to be included in the target build wrapper (e.g. to activate an environment etc.)"
exit 1
fi
fi
fi
}
function echo_opts() {
echo OUT_DIR=$OUT_DIR
echo DRY_RUN=$DRY_RUN
echo DEBUG=$DEBUG
echo HPC_TYPE=$HPC_TYPE
echo FILES=${files_array[*]}
echo SAMPLE_RATE=$SAMPLE_RATE
echo NUM_THREADS=$NUM_THREADS
echo ALIGNER=$ALIGNER
echo REFEREENCES
for ((i=0;$i<$NUM_REFERENCES;i=$i+1)) do
echo ${references_array[$i]}
done
echo PARAMETERS
for ((i=0;$i<$NUM_PARAMETERS;i=$i+1)) do
echo ${parameters_array[$i]}
done
}
#
# edit this method to set required environment (or set up
# before running this script)
#
function configure_env() {
cd $SEQ_PRISMS_BIN
cp ./align_prism.sh $OUT_DIR
cp ./align_prism.mk $OUT_DIR
cp ./mapping_stats_plots.r $OUT_DIR
# if there is not already a tardis config file in the workign folder,
# write one to set max tasks and (indirectly) mem per cpu (via also
# configuring a slurm job file , and pointing tardis at that )
if [ ! -f $OUT_DIR/tardis.toml ]; then
cat $SEQ_PRISMS_BIN/etc/default_slurm_array_job | sed "s/_mem-per-cpu_/${MEM_PER_CPU}/g" - | sed "s/_max-wall-time_/${MAX_WALL_TIME}/g" - > $OUT_DIR/slurm_array_job
cat >$OUT_DIR/tardis.toml <<EOF
max_tasks = $MAX_TASKS
jobtemplatefile = "$OUT_DIR/slurm_array_job"
EOF
fi
cd $OUT_DIR
# echo "
#source activate /dataset/bioinformatics_dev/active/conda-env/biopython
#PATH=\"$OUT_DIR:$PATH\"
#PYTHONPATH=\"$OUT_DIR:$PYTHONPATH\"
#" > $OUT_DIR/configure_env.sh
}
function check_env() {
if [ -z "$SEQ_PRISMS_BIN" ]; then
echo "SEQ_PRISMS_BIN not set - exiting"
exit 1
fi
}
function link_inputs() {
# ensure unique monikers for inputs by accessing them via unique links
for ((j=0;$j<$NUM_FILES;j=$j+1)) do
file=`realpath ${files_array[$j]}`
base=`basename $file`
link_base=$base
if [ -h $OUT_DIR/$link_base ]; then
count=2
while [ -h $OUT_DIR/${count}${link_base} ]; do
let count=$count+1
done
ln -s $file $OUT_DIR/${count}${link_base}
files_array[$j]=$OUT_DIR/${count}${link_base}
else
ln -s $file $OUT_DIR/${link_base}
files_array[$j]=$OUT_DIR/${link_base}
fi
done
}
function get_targets() {
# make a target moniker for each combination of input file and reference, and write associated
# alignment wrapper, which will be called by make
rm -f $OUT_DIR/alignment_targets.txt
for ((i=0;$i<${#references_array[*]};i=$i+1)) do
for ((j=0;$j<$NUM_FILES;j=$j+1)) do
file=${files_array[$j]}
file_base=`basename $file`
ref_base=`basename ${references_array[$i]}`
parameters_moniker=`echo ${parameters_array[$i]} | sed 's/ //g' | sed 's/\//\./g' | sed 's/-//g' | sed "s/'//g" | sed 's/\\\//g' | sed 's/"//g' `
alignment_moniker=${file_base}.${ALIGNER}.${ref_base}.${parameters_moniker}
alignment_moniker=`echo $alignment_moniker | awk '{printf("%s\n", substr($1,1,230));}' -`
echo $OUT_DIR/${alignment_moniker}.align_prism >> $OUT_DIR/alignment_targets.txt
# generate wrapper
sample_phrase=""
if [ ! -z $SAMPLE_RATE ]; then
sample_phrase="-s $SAMPLE_RATE"
fi
reference=${references_array[$i]}
parameters=`echo ${parameters_array[$i]} | sed 's/"//g'`
aligner_filename=$OUT_DIR/${alignment_moniker}.align_prism.sh
if [ -f aligner_filename ]; then
if [ ! $FORCE == yes ]; then
echo "found existing aligner $aligner_filename - will re-use (use -f to force rebuild of aligners) "
continue
fi
fi
tardis_include_phrase=""
if [ ! -z "$tardis_environment_include" ]; then
tardis_include_phrase="--shell-include-file $tardis_environment_include "
fi
tardis_dry_run_phrase=""
if [ $DRY_RUN == "yes" ]; then
tardis_dry_run_phrase="--dry-run"
fi
if [ $ALIGNER == bwa ]; then
echo "#!/bin/bash
rm -f $OUT_DIR/${alignment_moniker}.bam
tardis $tardis_dry_run_phrase --hpctype $HPC_TYPE -d $OUT_DIR $sample_phrase $tardis_include_phrase bwa aln $parameters $reference _condition_fastq_input_$file \> _condition_throughput_$OUT_DIR/${alignment_moniker}.sai \; bwa samse $reference _condition_throughput_$OUT_DIR/${alignment_moniker}.sai _condition_fastq_input_$file \> _condition_sam_output_$OUT_DIR/${alignment_moniker}.bam
tardis $tardis_dry_run_phrase --hpctype $HPC_TYPE -q -d $OUT_DIR $tardis_include_phrase samtools flagstat $OUT_DIR/${alignment_moniker}.bam > $OUT_DIR/${alignment_moniker}.stats
" > $aligner_filename
elif [ $ALIGNER == blastn ]; then
echo "#!/bin/bash
rm -f $OUT_DIR/${alignment_moniker}.results.gz
tardis $tardis_dry_run_phrase --hpctype $HPC_TYPE -d $OUT_DIR $sample_phrase $tardis_include_phrase blastn -db $reference -query _condition_fasta_input_$file $parameters \> _condition_text_output_$OUT_DIR/${alignment_moniker}.results
" > $aligner_filename
elif [ $ALIGNER == tblastx ]; then
echo "#!/bin/bash
rm -f $OUT_DIR/${alignment_moniker}.results.gz
tardis $tardis_dry_run_phrase --hpctype $HPC_TYPE -d $OUT_DIR $sample_phrase $tardis_include_phrase tblastx -db $reference -query _condition_fasta_input_$file $parameters \> _condition_text_output_$OUT_DIR/${alignment_moniker}.results
" > $aligner_filename
elif [ $ALIGNER == blastx ]; then
echo "#!/bin/bash
rm -f $OUT_DIR/${alignment_moniker}.results.gz
tardis $tardis_dry_run_phrase --hpctype $HPC_TYPE -d $OUT_DIR $sample_phrase $tardis_include_phrase blastx -db $reference -query _condition_fasta_input_$file $parameters \> _condition_text_output_$OUT_DIR/${alignment_moniker}.results
" > $aligner_filename
elif [ $ALIGNER == blastp ]; then
echo "#!/bin/bash
rm -f $OUT_DIR/${alignment_moniker}.results.gz
tardis $tardis_dry_run_phrase --hpctype $HPC_TYPE -d $OUT_DIR $sample_phrase $tardis_include_phrase blastp -db $reference -query _condition_fasta_input_$file $parameters \> _condition_text_output_$OUT_DIR/${alignment_moniker}.results
" > $aligner_filename
elif [ $ALIGNER == qblastn ]; then
echo "#!/bin/bash
rm -f $OUT_DIR/${alignment_moniker}.results.gz
tardis $tardis_dry_run_phrase --hpctype $HPC_TYPE -d $OUT_DIR $sample_phrase $tardis_include_phrase blastn -db $reference -query _condition_fastq2fasta_input_$file $parameters \> _condition_text_output_$OUT_DIR/${alignment_moniker}.results
" > $aligner_filename
elif [ $ALIGNER == qblastx ]; then
echo "#!/bin/bash
rm -f $OUT_DIR/${alignment_moniker}.results.gz
tardis $tardis_dry_run_phrase --hpctype $HPC_TYPE -d $OUT_DIR $sample_phrase $tardis_include_phrase blastx -db $reference -query _condition_fastq2fasta_input_$file $parameters \> _condition_text_output_$OUT_DIR/${alignment_moniker}.results
" > $aligner_filename
else
echo "unsupported aligner $ALIGNER "
exit 1
fi
chmod +x $aligner_filename
done
done
}
function fake_prism() {
echo "dry run ! "
make -n -f align_prism.mk -d -k --no-builtin-rules -j $NUM_THREADS `cat $OUT_DIR/alignment_targets.txt` > $OUT_DIR/align_prism.log 2>&1
echo "dry run : summary commands are
TBA - probably another make
"
exit 0
}
function run_prism() {
make -f align_prism.mk -d -k --no-builtin-rules -j $NUM_THREADS `cat $OUT_DIR/alignment_targets.txt` > $OUT_DIR/align_prism.log 2>&1
# will call another make to do summaries
}
function html_prism() {
echo "tba" > $OUT_DIR/align_prism.html 2>&1
}
function clean() {
nohup rm -rf $OUT_DIR/tardis_* > $OUT_DIR/align_clean.log 2>&1 &
}
function main() {
get_opts "$@"
check_opts
echo_opts
check_env
link_inputs
get_targets
configure_env
if [ $DRY_RUN != "no" ]; then
fake_prism
else
run_prism
if [ $? == 0 ] ; then
html_prism
clean
echo "*** finished ***"
else
echo "error state from run - skipping clean and html page generation"
exit 1
fi
fi
}
set -x
main "$@"
set +x