forked from ljdursi/consensus_call_docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-one-tumour-indel.sh
executable file
·206 lines (179 loc) · 6.12 KB
/
merge-one-tumour-indel.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
#!/bin/bash
## merges indel files together
## requires mergevcf, bgzip, tabix
readonly REFERENCE=${USE_REFERENCE:-"/dbs/reference/genome.fa.gz"}
readonly TMPDIR=${USE_TMPDIR:-"/tmp"}
function usage {
>&2 echo "usage: $0 -b [/path/to/broad.indel.vcf.gz] -d [dkfz] -m [smufin] -s [sanger] -o [outfile: defaults to merged.vcf]"
>&2 echo " merges indel VCFs for sample"
>&2 echo " Input VCFs must be bgzip-ed and tabix-ed"
exit 1
}
function add_header_and_sort {
local file=$1
local header=$2
local grep_or_zgrep=grep
if [[ $file == *.gz ]]
then
local grep_or_zgrep=zgrep
fi
${grep_or_zgrep} "^##" "$file"
if [[ ! -z "$header" ]]
then
echo "$header"
fi
${grep_or_zgrep} -v "^##" "$file" \
| sort -k1,1 -k2,2n
}
readonly TAB=$'\t'
function cleanup {
# normalize, and get rid of known broad and sanger weirdnesses
local file=$1
vt decompose -s "$file" \
| vt normalize -r "$REFERENCE" - \
| sed -e "s/${TAB}${TAB}${TAB}$/${TAB}.${TAB}.${TAB}./" \
| grep -v '=$'
}
function make_cleaned {
local file=$1
local outfile=$2
if [[ -f "$file" ]]
then
cleanup "${file}" \
| bgzip > "${outfile}"
tabix -p vcf "${outfile}"
fi
}
outfile=merged.indel.vcf
while getopts "b:d:m:s:o:h" OPTION
do
case $OPTION in
b) readonly broadfile=${OPTARG}
;;
d) readonly dkfzfile=${OPTARG}
;;
m) readonly smufinfile=${OPTARG}
;;
s) readonly sangerfile=${OPTARG}
;;
h) usage
;;
o) outfile=${OPTARG}
;;
esac
done
if [[ -z "$dkfzfile" ]] || [[ -z "$sangerfile" ]]
then
>&2 echo "required argument missing: need dkfz (-d) and sanger (-s) files"
usage
fi
if [[ -z "${outfile}" ]]
then
>&2 echo "Invalid empty output filename"
usage
fi
if [[ ! -f "$dkfzfile" ]] || [[ ! -f "$sangerfile" ]]
then
>&2 echo "files missing: one of -b ${broadfile} -d ${dkfzfile} not found"
usage
fi
for file in "$smufinfile" "$broadfile" "$dkfzfile" "$sangerfile"
do
if [[ -f $file ]]
then
if [[ $file != *.gz ]] || [[ ! -f ${file}.tbi ]]
then
>&2 echo "Input VCF files must be bgziped and tabixed."
usage
fi
fi
done
newest=$dkfzfile
if [[ $sangerfile -nt $newest ]]; then newest=$sangerfile; fi
if [[ -f "${broadfile}" ]] && [[ $broadfile -nt $newest ]]; then newest=$broadfile; fi
if [[ -f "${smufinfile}" ]] && [[ $smufinfile -nt $newest ]]; then newest=$smufinfile; fi
if [[ -f "${outfile}.gz" ]] && [[ "${outfile}.gz" -nt "${newest}" ]]
then
>&2 echo "$0: ${outfile} exists and is newer than inputs; cowardly refusing to overwrite."
exit 1
fi
for caller in "broad" "dkfz" "sanger" "smufin"
do
declare oldfilename=${caller}file
declare newfilename=cleaned_${caller}file
declare $newfilename=${TMPDIR}/${caller}.indel.vcf.gz
make_cleaned "${!oldfilename}" "${!newfilename}"
done
##
## merge input files into one VCF
##
readonly MERGEDFILE=${TMPDIR}/merged.vcf
readonly MERGEDSORTEDFILE=${TMPDIR}/merged.sorted.vcf
if [[ -f "${broadfile}" ]] && [[ -f "${smufinfile}" ]]
then
mergevcf -l broad,dkfz,smufin,sanger \
"$cleaned_broadfile" \
"$cleaned_dkfzfile" \
"$cleaned_smufinfile" \
"$cleaned_sangerfile" \
--ncallers \
> "${MERGEDFILE}"
elif [[ -f "${broadfile}" ]]
then
mergevcf -l broad,dkfz,sanger \
"$cleaned_broadfile" \
"$cleaned_dkfzfile" \
"$cleaned_sangerfile" \
--ncallers \
> "${MERGEDFILE}"
else
mergevcf -l dkfz,smufin,sanger \
"$cleaned_dkfzfile" \
"$cleaned_sangerfile" \
"$cleaned_smufinfile" \
--ncallers \
> "${MERGEDFILE}"
fi
add_header_and_sort "${MERGEDFILE}" \
| grep -v "Callers=broad;" \
| bgzip > "${MERGEDSORTEDFILE}.gz"
tabix -p vcf "${MERGEDSORTEDFILE}.gz"
rm -f "${MERGEDFILE}"
##
## Annotate with the SGA annotations
##
readonly TEMPLATE="${TMPDIR}/vaf.annotate.single.conf"
cat > "${TEMPLATE}" << EOF
[[annotation]]
file="@@FILE@@"
fields = ["TumorVAF", "NormalVAF", "TumorVarDepth", "TumorTotalDepth", "NormalVarDepth", "NormalTotalDepth", "RepeatRefCount"]
names = ["TumorVAF", "NormalVAF", "TumorVarDepth", "TumorTotalDepth", "NormalVarDepth", "NormalTotalDepth", "RepeatRefCount"]
ops = ["self", "self", "self", "self", "self", "self", "self"]
EOF
readonly ANNOTATION_CONF="${TMPDIR}/vaf.indel.$$.conf"
rm -f "${ANNOTATION_CONF}"
for input_file in "$cleaned_broadfile" "$cleaned_dkfzfile" "$cleaned_sangerfile" "$cleaned_smufinfile"
do
if [[ -f "$input_file" ]]
then
sed -e "s#@@FILE@@#$input_file#" "${TEMPLATE}" >> "${ANNOTATION_CONF}"
echo " " >> "${ANNOTATION_CONF}"
fi
done
rm -f "${TEMPLATE}"
vcfanno -p 1 "${ANNOTATION_CONF}" "${MERGEDSORTEDFILE}.gz" \
| sed -e 's/\tFORMAT.*$//' \
| sed -e 's/^##INFO=<ID=TumorVAF,.*$/##INFO=<ID=TumorVAF,Number=1,Type=Float,Description="VAF of variant in tumor from sga">/' \
| sed -e 's/^##INFO=<ID=TumorVarDepth,.*$/##INFO=<ID=TumorVarDepth,Number=1,Type=Integer,Description="Tumor alt count from sga">/' \
| sed -e 's/^##INFO=<ID=TumorTotalDepth,.*$/##INFO=<ID=TumorTotalDepth,Number=1,Type=Integer,Description="Tumor total read depth from sga">/' \
| sed -e 's/^##INFO=<ID=NormalVAF,.*$/##INFO=<ID=NormalVAF,Number=1,Type=Float,Description="VAF of variant in normal from sga">/' \
| sed -e 's/^##INFO=<ID=NormalVarDepth,.*$/##INFO=<ID=NormalVarDepth,Number=1,Type=Integer,Description="Normal alt count from sga">/' \
| sed -e 's/^##INFO=<ID=NormalTotalDepth,.*$/##INFO=<ID=NormalTotalDepth,Number=1,Type=Integer,Description="Normal total read depth from sga">/' \
> "${outfile}"
rm -f "$cleaned_broadfile" "$cleaned_dkfzfile" "$cleaned_sangerfile" "$cleaned_smufinfile"
rm -f "${cleaned_broadfile}.tbi" "${cleaned_dkfzfile}.tbi" "${cleaned_sangerfile}.tbi" "${cleaned_smufinfile}.tbi"
rm -f "${ANNOTATION_CONF}"
rm -f "${MERGEDSORTEDFILE}.gz"
rm -f "${MERGEDSORTEDFILE}.gz.tbi"
bgzip -f "${outfile}"
tabix -p vcf "${outfile}.gz"