-
Notifications
You must be signed in to change notification settings - Fork 10
/
convert_vep_vcf_to_tsv_capice.sh
executable file
·185 lines (152 loc) · 4.38 KB
/
convert_vep_vcf_to_tsv_capice.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
#!/bin/bash
# Stops script if any error occurs.
set -e
# Defines error echo.
errcho() { echo "$@" 1>&2; }
# Usage.
readonly USAGE="VEP VCF output to CAPICE TSV converter
Usage:
convert_vep_to_tsv_capice.sh -p <arg> -i <arg> -o <arg> [-t] [-f]
-p required: The path to the BCFTools image. (available at: https://download.molgeniscloud.org/downloads/vip/images/bcftools-1.14.sif)
-i required: The VEP output VCF.
-o required: The directory and output filename for the CAPICE .tsv.gz.
-f optional: enable force.
-t optional: enable train. Adds the ID column to the output.
Please note that this script expects apptainer binds to be set correctly by the system administrator.
Additional apptainer binds can be set by setting the environment variable APPTAINER_BIND.
If using SLURM, please export this environment variable to the sbatch instance too.
Example:
bash convert_vep_vcf_to_tsv_capice.sh -p /path/to/bcftools.sif -i vep_out.vcf.gz -o capice_in.tsv.gz
Requirements:
- Apptainer (although Singularity should work too, please change the script and adjust apptainer to singularity)
- BCFTools image. (available at: https://download.molgeniscloud.org/downloads/vip/images/bcftools-1.14.sif)
"
# Global variables
FORCE=false
TRAIN=false
main() {
digestCommandLine "$@"
processFile
}
digestCommandLine() {
while getopts p:i:o:hft flag
do
case "${flag}" in
p) bcftools_path=${OPTARG};;
i) input=${OPTARG};;
o) output=${OPTARG};;
h)
echo "${USAGE}"
exit;;
t)
TRAIN=true;;
f)
FORCE=true;;
\?)
errcho "Error: invalid option"
echo "${USAGE}"
exit 1;;
esac
done
if [[ ${TRAIN} == true ]]
then
HEADER="CHROM\tPOS\tID\tREF\tALT\t"
FORMAT="%CHROM\t%POS\t%ID\t%REF\t%ALT\t%CSQ\n"
else
HEADER="CHROM\tPOS\tREF\tALT\t"
FORMAT="%CHROM\t%POS\t%REF\t%ALT\t%CSQ\n"
fi
validateCommandLine
}
validateCommandLine() {
local valid_command_line=true
# Validate if BCFTools image is set & not empty
if [ -z "${bcftools_path}" ]
then
valid_command_line=false
errcho "BCFTools image not set/empty"
else
if [ ! -f "${bcftools_path}" ]
then
valid_command_line=false
errcho "BCFTools image does not exist"
fi
fi
# Validate if input is set & not empty.
if [ -z "${input}" ]
then
valid_command_line=false
errcho "input file not set/empty"
else
# Validate if input file exists.
if [ ! -f "${input}" ]
then
valid_command_line=false
errcho "input file does not exist"
else
# Validate allowed input filetype.
case $(file --mime-type -b "${input}") in
text/plain);;
application/*gzip);;
*)
valid_command_line=false
errcho "input file has invalid type (plain text/gzip allowed)";;
esac
fi
fi
# Validate if variable is set & not empty.
if [ -z "${output}" ]
then
valid_command_line=false
errcho "output file not set/empty"
else
# Validates proper output filename.
if [[ "${output}" != *.tsv.gz ]]
then
valid_command_line=false
errcho "output filename must end with '.tsv.gz'"
else
# Validates if output doesn't file already exist.
if [ -f "${output}" ]
then
if [[ ${FORCE} == true ]]
then
echo "output file exists, enforcing output"
rm "${output}"
else
errcho "output file exists and force flag is not called"
valid_command_line=false
fi
fi
fi
fi
# If a the command line arguments are invalid, exits with code 1.
if [[ "${valid_command_line}" == false ]]; then errcho "Exiting."; exit 1; fi
}
processFile() {
local output="${output%.gz}"
local args=()
args+=("exec")
args+=("${bcftools_path}")
args+=("bcftools")
args+=("+split-vep")
# Header
echo "Obtaining header"
header_args=("${args[@]}")
header_args+=("-l" "${input}")
present_features=$(apptainer "${header_args[@]}" | cut -f 2 | tr "\n" "\t" | sed "s/\t$//")
echo -e "${HEADER}$present_features" > ${output}
# VEP VCF file content
echo "Obtaining VCF content"
file_args=("${args[@]}")
file_args+=("-d")
file_args+=("-f" "${FORMAT}")
file_args+=("-A" "tab")
file_args+=("${input}")
apptainer "${file_args[@]}" >> ${output}
echo "BCFTools finished."
echo "Gzipping output file."
gzip "${output}"
echo "Done."
}
main "$@"