-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge_multisample.sh
277 lines (243 loc) · 7.87 KB
/
merge_multisample.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
#!/usr/bin/env bash
###########################################################################
######### ###########
######### mergemultisample ###########
######### @uthor : D Baux david.baux<at>inserm.fr ###########
######### Date : 10/05/2021 ###########
######### ###########
###########################################################################
###########################################################################
###########
########### Script to semi-automate MobiDL treatment of families
###########
###########################################################################
### input: a txt file of format:
## RUN_PATH=
## BASE_JSON=
## DISEASE_FILE=
## GENES_OF_INTEREST=
## ACHAB_TODO=
## NUM_FAM=
## TRIO=[0|1]
## # if yes
## CI=
## FATHER=
## MOTHER=
## # if no
## AFFECTED=sample1,sample2...
## HEALTHY=sample1,sample2...
VERSION=1.0
USAGE="
Program: merge_multisample
Version: ${VERSION}
Contact: Baux David <david.baux@inserm.fr>
Usage: bash merge_multisample.sh -f /path/to/input/file -b /path/to/bcftools
"
if [ $# -eq 0 ]; then
echo "${USAGE}"
echo "Error Message : No arguments provided"
echo ""
exit 1
fi
usage ()
{
echo 'This script prepares achab for families.'
echo 'Usage : bash merge_multisample.sh'
echo ' Mandatory arguments :'
echo ' * -f|--family-file <path to input file>'
echo ' Optional arguments :'
echo ' * -b|--bcftools <path to bcftools>'
echo ' * -t|--threads <int>'
echo ' * -s|--slurm'
echo 'The slurm argument if provided will launch bcftools in a srun command.'
}
RED='\033[0;31m'
LIGHTRED='\033[1;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# -- Script log
VERBOSITY=3
# -- Log variables
ERROR=1
WARNING=2
INFO=3
DEBUG=4
# -- Log functions got from cww.sh -- simplified here
error() { log "${ERROR}" "[${RED}error${NC}]" "$1" ; }
warning() { log "${WARNING}" "[${YELLOW}warn${NC}]" "$1" ; }
info() { log "${INFO}" "[${BLUE}info${NC}]" "$1" ; }
debug() { log "${DEBUG}" "[${LIGHTRED}debug${NC}]" "$1" ; }
# -- Print log
echoerr() { echo -e "$@" 1>&2 ; }
log() {
if [ "${VERBOSITY}" -ge "$1" ]; then
echoerr "[`date +'%Y-%m-%d %H:%M:%S'`] $2 - MM version : ${VERSION} - $3"
fi
}
# -- Options
BCFTOOLS=$(which bcftools)
THREADS=1
SLURM=0
# -- Parse command line
while [ "$1" != "" ];do
case $1 in
-b | --bcftools) shift
BCFTOOLS=$1
;;
-f | --family-file) shift
FAMILY_FILE=$1
;;
-t | --threads) shift
if [[ "$1" =~ ^[0-9]+$ ]]
then
THREADS=$1
fi
;;
-s | --slurm)
SLURM=1
;;
-v | --verbosity) shift
# Check if verbosity level argument is an integer before assignment
if ! [[ "$1" =~ ^[0-9]+$ ]]
then
error "\"$1\" must be an integer !"
echo " "
help
else
VERBOSITY=$1
((VERBOSITYCOUNTER++))
fi
;;
-h | --help) usage
exit
;;
* ) usage
exit 1
esac
shift
done
if [[ ! -x "${BCFTOOLS}" ]]; then
error "bcftools path ${BCFTOOLS} seems to be wrong."
usage
exit 1
fi
if [[ ! -r "${FAMILY_FILE}" ]]; then
error "File ${FAMILY_FILE} does not seem to exist."
usage
exit 1
fi
# -- get variables from conf file
source ${FAMILY_FILE}
if [[ ! -d "${RUN_PATH}" || ! -f "${BASE_JSON}" || ! "${RUN_ID}" || ! "${NUM_FAM}" || ! "${TRIO}" || ! -f "${DISEASE_FILE}" || "${GENES_OF_INTEREST}" == '' || ! -d "${ACHAB_TODO}" ]]; then
error "There is an error with one of the params of the Family file."
usage
exit 1
fi
if [ ! "${AFFECTED}" ]; then
error "There should be at least one sample affected."
usage
exit 1
fi
if [[ "${TRIO}" == 1 ]]; then
if [[ ! "${CI}" || ! "${FATHER}" || ! "${MOTHER}" ]]; then
error "At least one member of the trio is lacking."
usage
exit 1
fi
fi
# RUN_PATH from autoDLML.sh comes with a / in the end - remove it
RUN_PATH=${RUN_PATH%\/}
# -- Debug
debug "BCFTOOLS:${BCFTOOLS}"
debug "FAMILY_FILE:${FAMILY_FILE}"
debug "THREADS:${THREADS}"
debug "SLURM:${SLURM}"
debug "RUN_PATH:${RUN_PATH}"
debug "BASE_JSON:${BASE_JSON}"
debug "DISEASE_FILE:${DISEASE_FILE}"
debug "GENES_OF_INTEREST:${GENES_OF_INTEREST}"
debug "RUN_ID:${RUN_ID}"
debug "NUM_FAM:${NUM_FAM}"
debug "TRIO:${TRIO}"
debug "AFFECTED:${AFFECTED}"
debug "CI:${CI}"
debug "FATHER:${FATHER}"
debug "MOTHER:${MOTHER}"
# -- build the VCF list
if [[ "${TRIO}" == 1 ]]; then
VCFS="${RUN_PATH}/${RUN_ID}/MobiDL/${CI}/panelCapture/${CI}.vcf.gz,"
VCFS+="${RUN_PATH}/${RUN_ID}/MobiDL/${FATHER}/panelCapture/${FATHER}.vcf.gz,"
VCFS+="${RUN_PATH}/${RUN_ID}/MobiDL/${MOTHER}/panelCapture/${MOTHER}.vcf.gz"
else
# -- build the list from the list of affected and HEALTHY
# first split the sample list and rebuild the VCF list (with path)
# VCF_AFFECTED
IFS="," read -a VCF_AFFECTED <<< "${AFFECTED}"
IFS="," read -a VCF_HEALTHY <<< "${HEALTHY}"
VCFS=""
for VCF_AFF in "${VCF_AFFECTED[@]}"; do
VCFS+="${RUN_PATH}/${RUN_ID}/MobiDL/${VCF_AFF}/panelCapture/${VCF_AFF}.vcf.gz,"
done
for VCF_HEA in "${VCF_HEALTHY[@]}"; do
VCFS+="${RUN_PATH}/${RUN_ID}/MobiDL/${VCF_HEA}/panelCapture/${VCF_HEA}.vcf.gz,"
done
fi
IFS="," read -a VCF_ARRAY <<< "${VCFS}"
# -- prepare output
mkdir -p "${RUN_PATH}/${RUN_ID}/MobiDL/${NUM_FAM}"
cp "${BASE_JSON}" "${RUN_PATH}/${RUN_ID}/MobiDL/${NUM_FAM}/captainAchab_inputs.json"
cp "${DISEASE_FILE}" "${RUN_PATH}/${RUN_ID}/MobiDL/${NUM_FAM}/disease.txt"
# -- merge VCF
MERGE_CMD="${BCFTOOLS} merge --threads ${THREADS} "
for VCF in "${VCF_ARRAY[@]}"; do
MERGE_CMD+="${VCF} "
done
# MERGE_CMD+="> ${RUN_PATH}/${RUN_ID}/MobiDL/${NUM_FAM}/${NUM_FAM}.vcf"
SLURM_CMD=" "
if [ "${SLURM}" -eq 1 ];then
SLURM_CMD="srun -N1 -c${THREADS} "
fi
info "launching bcftools"
debug "${SLURM_CMD}${MERGE_CMD} > ${RUN_PATH}/${RUN_ID}/MobiDL/${NUM_FAM}/${NUM_FAM}.vcf"
${SLURM_CMD}${MERGE_CMD} > "${RUN_PATH}/${RUN_ID}/MobiDL/${NUM_FAM}/${NUM_FAM}.vcf"
if [ $? -eq 0 ]; then
# -- temp modif between monster and cluster
MONSTER_PREFIX="/RS_IURC/data"
CLUSTER_PREFIX="/mnt/data140"
JSON_RUN_PATH="${CLUSTER_PREFIX}${RUN_PATH#$MONSTER_PREFIX}"
JSON_DISEASE_FILE="${CLUSTER_PREFIX}${DISEASE_FILE#$MONSTER_PREFIX}"
info "${JSON_RUN_PATH}"
info "${JSON_DISEASE_FILE}"
# -- prepare vars by escaping "/"
RUN_SED=${JSON_RUN_PATH////\\/}
DISEASE_SED=${JSON_DISEASE_FILE////\\/}
GENES_SED=${GENES_OF_INTEREST////\\/}
# -- sed the JSON
if [[ ${TRIO} == 0 ]]; then
sed -i -e "s/\( \"captainAchab\.sampleID\": \"\).*/\1${NUM_FAM}\",/" \
-e "s/\( \"captainAchab\.affected\": \"\).*/\1${AFFECTED}\",/" \
-e "s/\( \"captainAchab\.inputVcf\": \"\).*/\1${RUN_SED}\/${RUN_ID}\/MobiDL\/${NUM_FAM}\/${NUM_FAM}\.vcf\",/" \
-e "s/\( \"captainAchab\.diseaseFile\": \"\).*/\1${DISEASE_SED}\",/" \
-e "s/\( \"captainAchab\.genesOfInterest\": \"\).*/\1${GENES_SED}\",/" \
-e "s/\( \"captainAchab\.outDir\": \"\).*/\1${RUN_SED}\/${RUN_ID}\/MobiDL\/${NUM_FAM}\/\",/" \
"${RUN_PATH}/${RUN_ID}/MobiDL/${NUM_FAM}/captainAchab_inputs.json"
else
sed -i -e "s/\( \"captainAchab\.sampleID\": \"\).*/\1${NUM_FAM}\",/" \
-e "s/\( \"captainAchab\.affected\": \"\).*/\1${AFFECTED}\",/" \
-e "s/\( \"captainAchab\.inputVcf\": \"\).*/\1${RUN_SED}\/${RUN_ID}\/MobiDL\/${NUM_FAM}\/${NUM_FAM}\.vcf\",/" \
-e "s/\( \"captainAchab\.diseaseFile\": \"\).*/\1${DISEASE_SED}\",/" \
-e "s/\( \"captainAchab\.checkTrio\": \"\).*/\1--trio\",/" \
-e "s/\( \"captainAchab\.caseSample\": \"\).*/\1${CI}\",/" \
-e "s/\( \"captainAchab\.fatherSample\": \"\).*/\1${FATHER}\",/" \
-e "s/\( \"captainAchab\.motherSample\": \"\).*/\1${MOTHER}\",/" \
-e "s/\( \"captainAchab\.genesOfInterest\": \"\).*/\1${GENES_SED}\",/" \
-e "s/\( \"captainAchab\.outDir\": \"\).*/\1${RUN_SED}\/${RUN_ID}\/MobiDL\/${NUM_FAM}\/\",/" \
"${RUN_PATH}/${RUN_ID}/MobiDL/${NUM_FAM}/captainAchab_inputs.json"
fi
info "JSON ${RUN_PATH}/${RUN_ID}/MobiDL/${NUM_FAM}/captainAchab_inputs.json seded"
rsync -az "${RUN_PATH}/${RUN_ID}/MobiDL/${NUM_FAM}" "${ACHAB_TODO}"
info "FAM ${NUM_FAM} sent to Achab"
else
error "bcftools failed: ${MERGE_CMD}"
fi