forked from EBI-Metagenomics/pipeline-v5
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrun_wf.sh
More file actions
executable file
·265 lines (209 loc) · 6.84 KB
/
Copy pathrun_wf.sh
File metadata and controls
executable file
·265 lines (209 loc) · 6.84 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
259
260
261
262
263
#!/bin/bash
METAGOFLOW_VERSION="https://github.com/emo-bon/MetaGOflow/releases/tag/v1.0.0"
# default values #
SCRIPT_PATH=$(realpath "$0")
PIPELINE_DIR=$(dirname "${SCRIPT_PATH}")
MEMORY=10G
NUM_CORES=1
LIMIT_QUEUE=100
YML="${PIPELINE_DIR}/Installation/templates/default.yml"
DB_DIR="${PIPELINE_DIR}/ref-dbs/"
TOOLS="${PIPELINE_DIR}/tools/"
_usage() {
echo "
metaGOflow interface.
Script arguments.
Resources:
-m Memory to use to with toil --defaultMemory. (optional, default ${MEMORY})
-c Number of cpus to use with toil --defaultCores. (optional, default ${NUM_CORES})
-l Limit number of jobs to schedule. (optional, default ${LIMIT_QUEUE})
Pipeline parameters:
-y template yml file. (optional, default ../templates/rna_prediction_template.yml})
-f Forward reads fasta file path.
-r Reverse reads fasta file path.
-n Name of run and prefix to output files.
-d Path to run directory.
-s Run workflow using Singularity (docker is the by default container technology) ('true' or 'false')
-b Keep tmp folder.
"
}
# [TODO] Consider adding a -t argument to run using toil.
while getopts :y:f:r:e:u:k:c:d:m:n:l:bsph option; do
case "${option}" in
y) YML=${OPTARG} ;;
f)
FORWARD_READS=${OPTARG}
echo "Presented paired-end forward read: ${FORWARD_READS}"
;;
r)
REVERSE_READS=${OPTARG}
printf "Presented paired-end reverse path: ${REVERSE_READS}\n"
;;
e) ENA_RUN_ID=${OPTARG} ;;
u) ENA_USERNAME=${OPTARG} ;;
k) ENA_PASSWORD=${OPTARG} ;;
c) NUM_CORES=${OPTARG} ;;
d) RUN_DIR=${OPTARG} ;;
m) MEMORY=${OPTARG} ;;
n) NAME=${OPTARG} ;;
l) LIMIT_QUEUE=${OPTARG} ;;
b) KEEP_TMP="--keep-tmp" ;;
s) SINGULARITY="--singularity" ;;
p) PRIVATE_DATA="-p" ;;
h)
_usage
exit 0
;;
:)
usage
exit 1
;;
\?)
echo ""
echo "Invalid option -${OPTARG}" >&2
_usage
exit 1
;;
esac
done
# ----------------------------- sanity check arguments ----------------------------- #
_check_mandatory() {
# Check if the argument is empty or null
# $1 variable
# $2 name to show
if [ -z "$1" ]; then
echo "Error." >&2
echo "Option ${2} is mandatory " >&2
echo "type -h to get help"
exit 1
fi
}
_check_reads() {
# check forward and reverse reads both present
# check if single reads then no other readsgiven
# BASH SYNTAX:
# to check if a variable has value:
# [ -z "$var" ] && echo "Empty"
if [ -z "$1" ] && [ -n "$2" ]; then
echo "Error"
echo "only reverse reads given, provide forward with -f"
exit 1
fi
if [ -n "$1" ] && [ -z "$2" ]; then
echo "Error"
echo "only forward reads given, provide reverse with -r"
exit 1
fi
}
_check_mandatory "$NAME" "-n"
_check_mandatory "$RUN_DIR" "-d"
_check_reads "$FORWARD_READS" "$REVERSE_READS"
# ----------------------------- environment & variables ----------------------------- #
# load required environments and packages before running
export CWD=$(pwd)
export TOIL_SLURM_ARGS="--array=1-${LIMIT_QUEUE}%20" #schedule 100 jobs 20 running at one time
export CWL="${PIPELINE_DIR}/workflows/gos_wf.cwl"
# work dir
export WORK_DIR=${RUN_DIR}/work-dir
export JOB_TOIL_FOLDER=${WORK_DIR}/job-store-wf
export TMPDIR=${RUN_DIR}/tmp
# result dir
export OUT_DIR=${RUN_DIR}
export LOG_DIR=${OUT_DIR}/log-dir/${NAME}
export OUT_DIR_FINAL=${OUT_DIR}/results
export CACHE_DIR=${OUT_DIR}/cache
mkdir -p "${OUT_DIR_FINAL}" "${TMPDIR}"
export EXTENDED_CONFIG_YAML_TMP=${RUN_DIR}/"${NAME}"_temp.yml
export EXTENDED_CONFIG_YAML=${RUN_DIR}/"${NAME}".yml
export FUNCTIONAL_ANNOTATION=${OUT_DIR}/results/functional-annotation/
# Get study id in case of ENA fetch tool
if [[ $ENA_RUN_ID != "" ]];
then
echo "metaGOflow is about to fetch data from ENA"
# Run cwl for the ENA fetch tool
cp tools/fetch-tool/get_raw_data_run.cwl .
printf "
run_accession_number: ${ENA_RUN_ID}
private_data: true
ena_api_username: ${ENA_USERNAME}
ena_api_password: ${ENA_PASSWORD}
" > get_raw_data_run-test.yml
cwl-runner ${SINGULARITY} --outdir ${OUT_DIR} --debug get_raw_data_run.cwl get_raw_data_run-test.yml
rm get_raw_data_run.cwl
rm get_raw_data_run-test.yml
# Get the accession id of the corresponding study
ENA_STUDY_ID=$(curl -X POST "https://www.ebi.ac.uk/ena/browser/api/xml?accessions="$ENA_RUN_ID"&expanded=true" \
-H "accept: application/xml" | grep -A 1 "ENA-STUDY" | tail -1 | sed 's/.*<ID>// ; s/<\/ID>//')
export PATH_ENA_RAW_DATA=${PIPELINE_DIR}/${OUT_DIR}/raw_data_from_ENA/${ENA_STUDY_ID}/raw/
fi
# ----------------------------- prepare yml file ----------------------------- #
echo "Writing yaml file"
# DO NOT leave spaces after "\" in the end of a line
python utils/create_yml.py \
-y "${YML}" \
-o "${EXTENDED_CONFIG_YAML_TMP}" \
-l "${PATH_ENA_RAW_DATA}" \
-f "${PIPELINE_DIR}/${FORWARD_READS}" \
-r "${PIPELINE_DIR}/${REVERSE_READS}" \
-d "${DB_DIR}" \
-t "${TOOLS}" \
-e "${ENA_RUN_ID}"
mv eosc-wf.yml ${RUN_DIR}/
cat ${RUN_DIR}/eosc-wf.yml ${EXTENDED_CONFIG_YAML_TMP} > ${EXTENDED_CONFIG_YAML}
rm ${EXTENDED_CONFIG_YAML_TMP}
rm ${RUN_DIR}/eosc-wf.yml
cp config.yml ${RUN_DIR}/
# ----------------------------- running pipeline ----------------------------- #
# Run the metaGOflow workflow using cwl-runner (could use instead cwltool)
echo "metaGOflow is ready to go!"
cwl-runner --parallel ${SINGULARITY} --outdir ${OUT_DIR_FINAL} ${CWL} ${EXTENDED_CONFIG_YAML}
# ----------------------- edit output structure --------------------------- #
if [[ $KEEP_TMP != "" ]];
then
echo "Keep temporary output directory."
mv ${TMPDIR} ${CWD}
else
rm -rf ${TMPDIR}
fi
if [ -z "$FUNCTIONAL_ANNOTATION" ]; then
cd ${FUNCTIONAL_ANNOTATION}
count=`ls -1 *.chunks 2>/dev/null | wc -l`
if [ $count != 0 ]
then
rm *.chunks
fi
count=`ls -1 *CDS.I5_001.tsv.gz 2>/dev/null | wc -l`
if [ $count != 0 ]
then
fullfile=*.merged.CDS.I5_001.tsv.gz
prefix=$(echo $fullfile | sed 's/[^_]*$//')
prefix=${prefix::-1}
ls *.merged.CDS.I5_*.tsv.gz | xargs -I {} cat {} > allfiles.gz
ls *.merged.CDS.I5_*.tsv.gz | xargs -I {} rm {}
mv allfiles.gz ${prefix}".tsv.gz"
fi
fi
cd ${CWD}
# ----------------------- build RO-crate --------------------------- #
if [ -z "$ENA_RUN_ID" ]; then
ENA_RUN_ID="None"
else
rm -r ${OUT_DIR}/raw_data_from_ENA
fi
# Init the RO-Crate
rocrate init -c ${RUN_DIR}
# Edit the RO-Crate
if [[ $KEEP_TMP != "" ]];
then
export KEEP_TMP="True"
else
export KEEP_TMP="False"
fi
python utils/edit-ro-crate.py ${OUT_DIR} ${EXTENDED_CONFIG_YAML} ${ENA_RUN_ID} ${METAGOFLOW_VERSION} ${KEEP_TMP}
# Bring back temporary folder if kept.
if [[ $KEEP_TMP == "True" ]];
then
echo "Keep temporary output directory."
mv ${CWD}/tmp ${TMPDIR}
fi
echo "metaGOflow has been completed."