generated from CCBR/CCBR_SnakemakeTemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tranquil
executable file
·622 lines (479 loc) · 16.8 KB
/
tranquil
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#!/usr/bin/env bash
set -eo pipefail
module purge
# Author: Vishal Koparde, Ph.D.
# CCBR, NCI
# (c) 2021
#
# wrapper script to run the snakemake pipeline
# a) on an interactive node (runlocal) OR
# b) submit to the slurm load scheduler (run)
#
# DISCLAIMER: This wrapper only works on BIOWULF
# setting python and snakemake versions
# only for BIOWULF
PYTHON_VERSION="python/3.7"
SNAKEMAKE_VERSION="snakemake/5.24.1"
# are you on biowulf or frce?
if [ "$HOSTNAME" == "biowulf.nih.gov" ]; then
HOST="BIOWULF"
elif [ "$HOSTNAME" == "fsitgl-head01p.ncifcrf.gov" ];then
HOST="FRCE"
fi
# set extra singularity bindings
if [ "$HOST" == "BIOWULF" ];then
EXTRA_SINGULARITY_BINDS="/lscratch,/data/CCBR_Pipeliner/"
elif [ "$HOST" == "FRCE" ];then
EXTRA_SINGULARITY_BINDS="/scratch/cluster_scratch/${USER},/mnt/projects/CCBR-Pipelines"
else
err Can only run on BIOWULF or FRCE
fi
SCRIPTNAME="$0"
SCRIPTDIRNAME=$(readlink -f $(dirname $0))
SCRIPTBASENAME=$(readlink -f $(basename $0))
function get_git_commitid_tag() {
# This function gets the latest git commit id and tag
# @Input:
# @param1: PIPELINE_HOME folder which is a initialized git repo folder path
# @Output:
# @param1: tab-delimited commit id and tag
cd $1
gid=$(git rev-parse HEAD)
tag=$(git describe --tags $gid 2>/dev/null)
echo -ne "$gid\t$tag"
}
echo "#################################################################"
echo "#################################################################"
PIPELINE_HOME=$(readlink -f $(dirname "$0"))
RESOURCESDIR="$PIPELINE_HOME/resources"
SCRIPTSDIR="$PIPELINE_HOME/workflow/scripts"
echo -ne "Pipeline Dir: \t\t $PIPELINE_HOME\n"
SNAKEFILE="${PIPELINE_HOME}/workflow/Snakefile"
echo -ne "Snakefile: \t\t $SNAKEFILE\n"
VERSION=$(cat ${PIPELINE_HOME}/VERSION)
echo -ne "Version: \t\t $VERSION\n"
echo -ne "Host: \t\t\t $HOST\n"
function usage_only() {
# This script prints only the usage without any pre-amble
cat << EOF
#################################################################
#################################################################
USAGE:
bash ${SCRIPTNAME} -m/--runmode=<RUNMODE> -w/--workdir=<WORKDIR>
Required Arguments:
1. RUNMODE: [Type: String] Valid options:
*) init : initialize workdir
*) run : run with slurm
*) reset : DELETE workdir dir and re-init it
*) dryrun : dry run snakemake to generate DAG
*) unlock : unlock workdir if locked by snakemake
*) runlocal : run without submitting to sbatch
2. WORKDIR: [Type: String]: Absolute or relative path to the
output folder with write permissions.
#################################################################
#################################################################
EOF
}
function usage() {
# This function prints generic usage of the wrapper script.
# @Input: None
# @Output: Usage information about the script
cat << EOF
#################################################################
#################################################################
Running ${SCRIPTBASENAME} ...
TRANQUIL (TRna AbundaNce QUantification pIpeLine)
EOF
usage_only
}
function err() {
# This is a generic error message function. It prints the message, then the
# usage and exits with non-zero exit code.
# @Input:
# @param1: Message to print before printing the usage and exiting
# @Ouput:
# @param2: echo the error message with the usage to the screen
cat << EOF
#################################################################
##### ERROR ############ ERROR ########## ERROR #################
#################################################################
$@
EOF
usage_only && exit 1 1>&2;
}
function _set_config() {
sed -e "s/PIPELINE_HOME/${PIPELINE_HOME//\//\\/}/g" \
-e "s/WORKDIR/${WORKDIR//\//\\/}/g" \
-e "s/WORKRESOURCESDIR/${WORKRESOURCESDIR//\//\\/}/g" \
-e "s/WORKSCRIPTSDIR/${WORKSCRIPTSDIR//\//\\/}/g" \
${PIPELINE_HOME}/config/config.yaml > $WORKDIR/config.yaml
}
function _set_test_samplemanifest() {
sed -e "s/PIPELINE_HOME/${PIPELINE_HOME//\//\\/}/g" \
-e "s/WORKDIR/${WORKDIR//\//\\/}/g" \
${PIPELINE_HOME}/config/samples.tsv > $WORKDIR/samples.tsv
}
function init() {
# This function initializes the workdir by:
# 1. creating the working dir
# 2. copying essential files like config.yaml, samples.tsv and resources folder into the workdir
# 3. setting up logs and stats folders
# create output folder
if [ -d $WORKDIR ];then err "Folder $WORKDIR already exists!"; fi
mkdir -p $WORKDIR
echo "COPYING resources ..."
if [ ! -d $WORKRESOURCESDIR ];then
mkdir -p $WORKRESOURCESDIR
cp -a $RESOURCESDIR/. $WORKRESOURCESDIR/
fi
echo "COPYING scripts ..."
if [ ! -d $WORKSCRIPTSDIR ];then
mkdir -p $WORKSCRIPTSDIR
cp -a $SCRIPTSDIR/. $WORKSCRIPTSDIR/
fi
# copy config and samples files
_set_config
_set_test_samplemanifest
cp ${PIPELINE_HOME}/config/contrasts.tsv $WORKDIR/contrasts.tsv
#create log and stats folders
if [ ! -d $WORKDIR/logs ]; then
mkdir -p $WORKDIR/logs
echo -ne "Logs Dir: \t\t $WORKDIR/logs\n"
fi
if [ ! -d $WORKDIR/stats ];then
mkdir -p $WORKDIR/stats
echo -ne "Stats Dir: \t\t $WORKDIR/stats\n"
fi
cat << EOF
#################################################################
#################################################################
Done Initializing $WORKDIR.
You can now edit
$WORKDIR/config.yaml
$WORKDIR/samples.tsv
and
$WORKDIR/contrasts.tsv
#################################################################
#################################################################
EOF
}
function check_essential_files() {
# Checks if files essential to start running the pipeline exist in the workdir
# By default config.yaml and samples.tsv are considered essential files.
if [ ! -d $WORKDIR ];then err "Folder $WORKDIR does not exist!"; fi
for f in config.yaml samples.tsv contrasts.tsv ; do
if [ ! -f $WORKDIR/$f ]; then
err "Error: '${f}' file not found in workdir ... initialize first!"
fi
done
}
function reconfig(){
# Rebuild config file and replace the config.yaml in the WORKDIR
# this is only for dev purposes when new key-value pairs are being
# updated in the config file in PIPELINE_HOME
check_essential_files
_set_config
echo "$WORKDIR/config.yaml has been updated!"
}
function runcheck(){
# Check "job-essential" files and load required modules
check_essential_files
if [ "$HOST" == "BIOWULF" ]; then
module load $PYTHON_VERSION
module load $SNAKEMAKE_VERSION
module load singularity
elif [ "$HOST" == "FRCE" ];then
module load singularity/3.7.2 > /dev/null
# Add my own version of python and snakemake to the PATH
export PATH=/mnt/projects/CCBR-Pipelines/bin/:${PATH}
fi
}
function dryrun() {
# check essential files, load modules and do Dry-run
runcheck
run "--dry-run"
}
function unlock() {
# check essential files, load modules and
# unlock the workdir if previous snakemake run ended abruptly
runcheck
run "--unlock"
}
function _exe_in_path() {
name_of_exe=$1
path_to_exe=$(which $name_of_exe 2>/dev/null)
if [ ! -x "$path_to_exe" ] ; then
err $path_to_exe NOT FOUND!
fi
}
function set_singularity_binds(){
# this functions tries find what folders to bind
# "Biowulf specific"
# assumes that config.yaml and samples.tsv in the WORKDIR are essential
# files with the most uptodate information
# required dos2unix in path
_exe_in_path dos2unix
echo "$PIPELINE_HOME" > ${WORKDIR}/tmp1
echo "$WORKDIR" >> ${WORKDIR}/tmp1
grep -o '\/.*' <(cat ${WORKDIR}/config.yaml ${WORKDIR}/samples.tsv)| \
dos2unix | \
tr '\t' '\n' | \
grep -v ' \|\/\/' | \
sort | \
uniq >> ${WORKDIR}/tmp1
grep gpfs ${WORKDIR}/tmp1|awk -F'/' -v OFS='/' '{print $1,$2,$3,$4,$5}' | \
grep "[a-zA-Z0-9]" | \
sort | uniq > ${WORKDIR}/tmp2
grep -v gpfs ${WORKDIR}/tmp1|awk -F'/' -v OFS='/' '{print $1,$2,$3}' | \
grep "[a-zA-Z0-9]" | \
sort | uniq > ${WORKDIR}/tmp3
while read a;do
readlink -f $a
done < ${WORKDIR}/tmp3 | grep "[a-zA-Z0-9]"> ${WORKDIR}/tmp4
binds=$(cat ${WORKDIR}/tmp2 ${WORKDIR}/tmp3 ${WORKDIR}/tmp4 | sort | uniq | tr '\n' ',')
rm -f ${WORKDIR}/tmp?
binds=$(echo $binds | awk '{print substr($1,1,length($1)-1)}')
SINGULARITY_BINDS="-B $EXTRA_SINGULARITY_BINDS,$binds"
}
function printbinds(){
# set the singularity binds and print them
# singularity binds are /lscratch,/data/CCBR_Pipeliner,
# plus paths deduced from config.yaml and samples.tsv using
# set_singularity binds function
set_singularity_binds
echo $SINGULARITY_BINDS
}
function runlocal() {
# If the pipeline is fired up on an interactive node (with sinteractive),
# this function runs the pipeline
runcheck
set_singularity_binds
if [ "$SLURM_JOB_ID" == "" ];then err "runlocal can only be done on an interactive node"; fi
module load singularity
run "local"
}
function runslurm() {
# Submit the execution of the pipeline to the biowulf job scheduler (slurm)
runcheck
set_singularity_binds
run "slurm"
}
function _get_file_modtime() {
# get the modification time for a file
filename=$1
modtime=$(stat $filename|grep Modify|awk '{print $2,$3}'|awk -F"." '{print $1}'|sed "s/ //g"|sed "s/-//g"|sed "s/://g")
echo $modtime
}
function create_runinfo() {
# Create a runinfo.yaml file in the WORKDIR
if [ -f ${WORKDIR}/runinfo.yaml ];then
modtime=$(_get_file_modtime ${WORKDIR}/runinfo.yaml)
mv ${WORKDIR}/runinfo.yaml ${WORKDIR}/stats/runinfo.yaml.${modtime}
fi
echo "Pipeline Dir: $PIPELINE_HOME" > ${WORKDIR}/runinfo.yaml
echo "Git Commit/Tag: $GIT_COMMIT_TAG" >> ${WORKDIR}/runinfo.yaml
userlogin=$(whoami)
username=$(pinky -l $userlogin|grep "In real life:"|awk -F"In real life:" '{print $2}')
echo "Login: $userlogin" >> ${WORKDIR}/runinfo.yaml
echo "Name: $username" >> ${WORKDIR}/runinfo.yaml
g=$(groups)
echo "Groups: $g" >> ${WORKDIR}/runinfo.yaml
d=$(date)
echo "Date/Time: $d" >> ${WORKDIR}/runinfo.yaml
echo "#################################################################" >> ${WORKDIR}/runinfo.yaml
echo "##########################config.yaml############################" >> ${WORKDIR}/runinfo.yaml
echo "#################################################################" >> ${WORKDIR}/runinfo.yaml
cat ${WORKDIR}/config.yaml >> ${WORKDIR}/runinfo.yaml
echo "#################################################################" >> ${WORKDIR}/runinfo.yaml
echo "##########################samples.tsv############################" >> ${WORKDIR}/runinfo.yaml
echo "#################################################################" >> ${WORKDIR}/runinfo.yaml
cat ${WORKDIR}/samples.tsv >> ${WORKDIR}/runinfo.yaml
echo "#################################################################" >> ${WORKDIR}/runinfo.yaml
}
function preruncleanup() {
# Cleanup function to rename/move files related to older runs to prevent overwriting them.
echo "Running..."
# check initialization
check_essential_files
cd $WORKDIR
## Archive previous run files
if [ -f ${WORKDIR}/snakemake.log ];then
modtime=$(_get_file_modtime ${WORKDIR}/snakemake.log)
mv ${WORKDIR}/snakemake.log ${WORKDIR}/stats/snakemake.${modtime}.log
if [ -f ${WORKDIR}/snakemake.log.HPC_summary.txt ];then
mv ${WORKDIR}/snakemake.log.HPC_summary.txt ${WORKDIR}/stats/snakemake.${modtime}.log.HPC_summary.txt
fi
if [ -f ${WORKDIR}/snakemake.stats ];then
mv ${WORKDIR}/snakemake.stats ${WORKDIR}/stats/snakemake.${modtime}.stats
fi
fi
nslurmouts=$(find ${WORKDIR} -maxdepth 1 -name "slurm-*.out" |wc -l)
if [ "$nslurmouts" != "0" ];then
for f in $(ls ${WORKDIR}/slurm-*.out);do mv ${f} ${WORKDIR}/logs/;done
fi
create_runinfo
}
function run() {
# RUN function
# argument1 can be:
# 1. local or
# 2. dryrun or
# 3. unlock or
# 4. slurm
if [ "$1" == "local" ];then
preruncleanup
snakemake -s $SNAKEFILE \
--directory $WORKDIR \
--printshellcmds \
--use-singularity \
--singularity-args "$SINGULARITY_BINDS" \
--use-envmodules \
--latency-wait 120 \
--configfile ${WORKDIR}/config.yaml \
--cores all \
--stats ${WORKDIR}/snakemake.stats \
2>&1|tee ${WORKDIR}/snakemake.log
if [ "$?" -eq "0" ];then
snakemake -s $SNAKEFILE \
--report ${WORKDIR}/runlocal_snakemake_report.html \
--directory $WORKDIR \
--configfile ${WORKDIR}/config.yaml
fi
elif [ "$1" == "slurm" ];then
preruncleanup
# if QOS is other than "global" and is supplied in the cluster.json file then add " --qos={cluster.qos}" to the
# snakemake command below
cat > ${WORKDIR}/submit_script.sbatch << EOF
#!/bin/bash
#SBATCH --job-name="TRANQUIL"
#SBATCH --mem=10g
#SBATCH --partition="norm"
#SBATCH --time=96:00:00
#SBATCH --cpus-per-task=2
#SBATCH --mail-type=BEGIN,END,FAIL
module purge
EOF
if [ "$HOST" == "BIOWULF" ]; then
cat >> ${WORKDIR}/submit_script.sbatch << EOF
module load $PYTHON_VERSION
module load $SNAKEMAKE_VERSION
module load singularity
EOF
elif [ "$HOST" == "FRCE" ];then
cat >> ${WORKDIR}/submit_script.sbatch << EOF
export PATH=/mnt/projects/CCBR-Pipelines/bin/:${PATH}
module load singularity/3.10.5
EOF
fi
cat >> ${WORKDIR}/submit_script.sbatch << EOF
cd \$SLURM_SUBMIT_DIR
snakemake -s $SNAKEFILE \\
--directory $WORKDIR \\
--use-singularity \\
--singularity-args "$SINGULARITY_BINDS" \\
--printshellcmds \\
--latency-wait 120 \\
--configfile ${WORKDIR}/config.yaml \\
--cluster-config ${WORKRESOURCESDIR}/cluster.json \\
EOF
if [ "$HOST" == "BIOWULF" ]; then
cat >> ${WORKDIR}/submit_script.sbatch << EOF
--cluster "sbatch --gres "lscratch:256" --cpus-per-task {cluster.threads} -p {cluster.partition} -t {cluster.time} --mem {cluster.mem} --job-name {cluster.name} --output {cluster.output} --error {cluster.error}" \\
EOF
elif [ "$HOST" == "FRCE" ];then
cat >> ${WORKDIR}/submit_script.sbatch << EOF
--cluster "sbatch --cpus-per-task {cluster.threads} -p {cluster.partition} -t {cluster.time} --mem {cluster.mem} --job-name {cluster.name} --output {cluster.output} --error {cluster.error}" \\
EOF
fi
cat >> ${WORKDIR}/submit_script.sbatch << EOF
-j 500 \\
--rerun-incomplete \\
--keep-going \\
--stats ${WORKDIR}/snakemake.stats \\
2>&1|tee ${WORKDIR}/snakemake.log
if [ "\$?" -eq "0" ];then
snakemake -s $SNAKEFILE \\
--directory $WORKDIR \\
--report ${WORKDIR}/runslurm_snakemake_report.html \\
--configfile ${WORKDIR}/config.yaml
fi
bash <(curl https://raw.githubusercontent.com/CCBR/Tools/master/Biowulf/gather_cluster_stats.sh 2>/dev/null) ${WORKDIR}/snakemake.log > ${WORKDIR}/snakemake.log.HPC_summary.txt
EOF
sbatch ${WORKDIR}/submit_script.sbatch
elif [ "$1" == "--dry-run" ];then
if [ -f ${WORKDIR}/dryrun.log ];then
modtime=$(_get_file_modtime ${WORKDIR}/dryrun.log)
mv ${WORKDIR}/dryrun.log ${WORKDIR}/logs/dryrun.log.${modtime}
fi
snakemake $1 -s $SNAKEFILE \
--directory $WORKDIR \
--use-envmodules \
--printshellcmds \
--latency-wait 120 \
--configfile ${WORKDIR}/config.yaml \
--cluster-config ${WORKRESOURCESDIR}/cluster.json \
--cluster "sbatch --cpus-per-task {cluster.threads} -p {cluster.partition} -t {cluster.time} --mem {cluster.mem} --job-name {cluster.name} --output {cluster.output} --error {cluster.error}" \
-j 500 \
--rerun-incomplete \
--keep-going \
--stats ${WORKDIR}/snakemake.stats > ${WORKDIR}/dryrun.log 2>&1
cat ${WORKDIR}/dryrun.log
else # for unlock
snakemake $1 -s $SNAKEFILE \
--directory $WORKDIR \
--use-envmodules \
--printshellcmds \
--latency-wait 120 \
--configfile ${WORKDIR}/config.yaml \
--cluster-config ${WORKRESOURCESDIR}/cluster.json \
--cluster "sbatch --cpus-per-task {cluster.threads} -p {cluster.partition} -t {cluster.time} --mem {cluster.mem} --job-name {cluster.name} --output {cluster.output} --error {cluster.error}" \
-j 500 \
--rerun-incomplete \
--keep-going \
--stats ${WORKDIR}/snakemake.stats
fi
}
function reset() {
# Delete the workdir and re-initialize it
echo -ne "Working Dir: \t\t $WORKDIR\n"
if [ ! -d $WORKDIR ];then err "Folder $WORKDIR does not exist!";fi
echo "Deleting $WORKDIR"
rm -rf $WORKDIR
echo "Re-Initializing $WORKDIR"
init
}
function main(){
# Main function which parses all arguments
if [ $# -eq 0 ]; then usage && exit 1; fi
for i in "$@"; do
case $i in
-m=*|--runmode=*)
RUNMODE="${i#*=}"
;;
-w=*|--workdir=*)
WORKDIR="${i#*=}"
WORKRESOURCESDIR="${WORKDIR}/resources"
WORKSCRIPTSDIR="${WORKDIR}/scripts"
;;
*)
err "Unknown argument!" # unknown option
;;
esac
done
WORKDIR=$(readlink -f "$WORKDIR")
case $RUNMODE in
init) init && exit 0;;
dryrun) dryrun && exit 0;;
unlock) unlock && exit 0;;
run) runslurm && exit 0;;
runlocal) runlocal && exit 0;;
reset) reset && exit 0;;
dry) dryrun && exit 0;; # hidden option
local) runlocal && exit 0;; # hidden option
reconfig) reconfig && exit 0;; # hidden option for debugging
printbinds) printbinds && exit 0;; # hidden option
*) err "Unknown RUNMODE \"$RUNMODE\"";;
esac
}
# call the main function
main "$@"