-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_helper_functions.sh
executable file
·122 lines (96 loc) · 3.34 KB
/
shell_helper_functions.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
#!/bin/bash
## Helper functions for shell scripts.
__EXEC_DIR="${EXEC_DIR:-`dirname $BASH_SOURCE`}"
# All the Snakefiles are designed to run via these helper functions, and
# in fact have bootstrapping scripts on them that source this file.
export DRY_RUN=${DRY_RUN:-0}
LOCAL_CORES=${LOCAL_CORES:-4}
## Dump out the right Snakemake profile for this cluster
function gen_profile(){
env TOOLBOX=$(find_toolbox) PATH="$(dirname "$0"):$PATH" gen_profile.py --clobber
}
function gen_local_profile(){
gen_profile.py --template None -c "$LOCAL_CORES" --clobber
}
find_toolbox() {
# The toolbox used by the pipeline can be set by setting TOOLBOX in the
# environment (or environ.sh). Otherwise look for it in the program dir.
_toolbox="$( cd $__EXEC_DIR && readlink -f ${TOOLBOX:-toolbox} )"
echo "$_toolbox"
if ! [ -e "$_toolbox/" ] ; then
echo "WARNING - find_toolbox - No such directory ${_toolbox}" >&2
fi
}
find_templates() {
#Similarly for PanDoc templates
_def_templates="$(readlink -f $(dirname "$BASH_SOURCE")/templates)"
echo "${TEMPLATES:-$_def_templates}"
if ! [ -e "${TEMPLATES:-$_def_templates}/" ] ; then
echo "WARNING - find_templates - No such directory ${TEMPLATES:-$_def_templates}" >&2
fi
}
# Functions to run a Snakefile
find_snakefile() {
#Is it in the CWD (or an absolute path)?
if [ -e "$1" ] ; then
echo "$1"
#Maybe it's in the folder with this script
elif [ -e "$__EXEC_DIR/$1" ] ; then
echo "$__EXEC_DIR/$1"
#I give up. Echo back the name so I get a sensible error
else
echo "$1"
fi
}
### SEE doc/snakemake_be_careful.txt
snakerun_drmaa() {
if [ "${CLUSTER_PARTITION:-}" = none ] ; then
snakerun_single "$@"
return
fi
snakefile=`find_snakefile "$1"` ; shift
# Ensure the active VEnv gets enabled on cluster nodes:
if [ -n "${VIRTUAL_ENV:-}" ] ; then
export SNAKE_PRERUN="${VIRTUAL_ENV}/bin/activate"
fi
mkdir -p ./slurm_output
# Save out the profile, which includes setting the right jobscript
# TODO - maybe this should not be clobbered, to allow for manual
# tweaking of the config?
gen_profile || return 1
echo
echo "Running $snakefile in $(pwd -P) on the SLURM cluster"
if [ -n "${EXTRA_SNAKE_FLAGS:-}" ] ; then
echo "with extra flags: ${EXTRA_SNAKE_FLAGS}"
fi
[ "${VERBOSE:-0}" == 0 ] || set -x
snakemake \
-s "$snakefile" "$@" \
--profile ./snakemake_profile ${EXTRA_SNAKE_FLAGS:-}
}
snakerun_single() {
snakefile=`find_snakefile "$1"` ; shift
gen_local_profile || return 1
echo
echo "Running $snakefile in $(pwd -P) in local mode"
if [ -n "${EXTRA_SNAKE_FLAGS:-}" ] ; then
echo "with extra flags: ${EXTRA_SNAKE_FLAGS}"
fi
snakemake \
-s "$snakefile" "$@" \
--profile ./snakemake_profile ${EXTRA_SNAKE_FLAGS:-}
}
snakerun_touch() {
snakefile=`find_snakefile "$1"` ; shift
echo
echo "Running $snakefile --touch in $(pwd -P) to update file timestamps"
snakemake -s "$snakefile" --quiet --touch "$@"
echo "DONE"
}
if [ "$0" = "$BASH_SOURCE" ] ; then
echo "Source this file in your BASH script to make use of the helper functions."
echo
echo "Here is the cluster config..."
echo
env TOOLBOX=$(find_toolbox) "$__EXEC_DIR"/gen_profile.py --print
fi