-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFM_AGomez_03_GWAS_3_Imputation.sh
264 lines (220 loc) · 9.28 KB
/
TFM_AGomez_03_GWAS_3_Imputation.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
#!/bin/bash
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3 RETURN
mkdir -p logs
exec 1>logs/"`date +"%Y%m%d_%H:%M"`"_Imputation.out 2>&1
# ################################################################
# IMPUTE chromosomes
#
# Impute chromosomes
# chromosomes: ALL
# Imputation Software: minimac4
#
# Usage:
# sh 01_ImputeData.sh <source_file> <dest_path> <reference_panel> <ncores>
#
# Example:
# sh 01_ImputeData.sh /PROJECTES/SPORTOMICS/data_raw/futfem_20230331_GSA.ped /PROJECTES/SPORTOMICS/data_imputed/ 1000G 16
#
# Reference Panel: 1000G Phase 3 v5
# - /PROJECTES/PUBLICDATA/REFERENCES/reference_panels/1000GP_Phase3_v5/*.m3vcf
#
# Input:
# - Source file:
# A file with data to be imputed file formats allowed:
# - .vcf files
# - .ped files
# - .pgen
# - .bgen
#
# - Destination path:
# Path to be used to impute data and save the final imputed file
# IMPORTANT!!: There must be enough space to save the intermediate and final imputation results
#
# - Reference panel:
# One of the panels defined in /PROJECTES/SPORTOMICS/data_qc/gwas/futfem/GSA_20230331/scripts/refpanels.txt,
# use only the variable name
# - 1000G
# - JAPAN
# NOTE: New reference panels can be added in refpanels.txt
#
# - ncores:
# Number of cores to be used during the process
#
# Output:
# - Imputed data in vcf format
#
# Needed modules (HPC):
# module load bio/PLINK/2.00a3.6-GCC-11.3.0
# module load bio/BCFtools/1.10.2-GCC-9.3.0
# module load lang/Java/15.0.1 # Java
# module load lang/R/4.2.1-foss-2022a
# Also needs:
# Samtools
# Samtools installed in my conda environment genomics:
# source activate "genomics"
#
# Author: BRGE - Dolors Pelegrí
# Contact: [email protected]
# ################################################################
toImpute=$1 # Path with plink files
dest_path=$2 # Path to upload imputed files
ref_p=$3
cores=$4
# Harmonize with reference panel before imputation
function harmonize() {
fchr=$1
fftoImpute=$2
fref_panel=$3
fref_panel_sufix=$4
fcores=$5
if ! [[ -f "${fref_panel}/vcf/ALL.chr${fchr}.$fref_panel_sufix.vcf.gz.tbi" ]]; then
tabix -p vcf $fref_panel/vcf/ALL.chr$fchr.$fref_panel_sufix.vcf.gz # Create tabix index
fi
java -jar -Xms16g -Xmx16g -jar /PROJECTES/PUBLICDATA/software/GenotypeHarmonizer-1.4.25/GenotypeHarmonizer.jar --input $fftoImpute"_"chr$fchr.vcf.gz --output $fftoImpute"_"chr$fchr"_harm" --keep --ref $fref_panel/vcf/ALL.chr$fchr.$fref_panel_sufix
# ped to vcf
plink2 --threads $fcores \
--bfile $fftoImpute"_"chr$fchr"_harm" \
--snps-only just-acgt \
--export vcf vcf-dosage=DS id-delim=+ \
--out $fftoImpute"_"chr$fchr"_harm"
bgzip -c $fftoImpute"_"chr$fchr"_harm".vcf > $fftoImpute"_"chr$fchr"_harm".vcf.gz
bcftools index $fftoImpute"_"chr$fchr"_harm".vcf.gz
}
# #. DEBUG.#
# dest_path="/PROJECTES/SPORTOMICS/data_qc/gwas/futfem/GSA_20230331/ImputedNotPreQC/"
# toImpute="/PROJECTES/SPORTOMICS/data_raw/gwas/futfem/GSA_20230331/PLINK_140423_1220/futfem_20230331_GSA.ped"
# cores=8
# var1="1000G"
minimac4="/PROJECTES/PUBLICDATA/software/minimac4-1.0.2-Linux/bin/minimac4"
ftoImpute=""
basef="$(basename -- $toImpute)"
filename=$(echo $basef | grep -Po '.*(?=\.)')
extension="${basef##*.}"
# Create destination path
mkdir -p $desti_path
# Set ref_panel
ref_panel=$(sed -n 's/^'$ref_p'=\(.*\)/\1/p' < "/PROJECTES/PUBLICDATA/REFERENCES/reference_panels/refpanels.txt")
if [ -z "${ref_panel}" ];
then
echo "Unknown Reference panel \"$ref_p\""
return 0
fi
# Convert data to VCF
if [[ -f "${toImpute}" ]];
then
echo "Input format: $extension"
if [[ "$extension" != "vcf" ]];
then
if [[ "$extension" == "ped" ]];
then
echo "Converting \"$filename.$extension\" to .vcf format"
# First, we need to convert file to pgen sorting chr
plink2 --threads $cores \
--pedmap $(echo $toImpute | grep -Po '.*(?=\.)') \
--make-pgen --sort-vars --chr 1-22, X, Y \
--out $dest_path$filename"_tmp_plink"
plink2 --threads $cores \
--pfile $dest_path$filename"_tmp_plink" \
--make-pgen --rm-dup error list \
--out $dest_path$filename
rm $dest_path$filenam*_tmp_plink*
# Now from pgen to vcf
specialFormat="--pfile $dest_path$filename "
elif [[ "$extension" == "bed" ]]; then
echo "Converting \"$filename.$extension\" to .vcf format"
specialFormat="--bfile $(echo $toImpute | grep -Po '.*(?=\.)') "
elif [[ "$extension" == "gen" ]]; then
echo "Converting \"$filename.$extension\" to .vcf format"
specialFormat="--bgen $(echo $toImpute | grep -Po '.*(?=\.)') "
else
echo "Unknown format \"$extension\""
exit 1
fi
plink_command="plink2 --threads $cores $specialFormat --chr 1-22, X, Y --snps-only just-acgt --export vcf vcf-dosage=DS id-delim=+ --out $dest_path$filename"
eval " $plink_command"
fi
if [[ "$extension" == "vcf" ]]; then
cp $toImpute $dest_path
fi
ftoImpute=$dest_path$filename
#..# ftoImpute=$dest_path$filename.vcf
bgzip -c $ftoImpute.vcf > $dest_path$filename".vcf.gz"
bcftools index $ftoImpute".vcf.gz"
# Remove duplicates
bcftools norm \
-d none $dest_path$filename".vcf.gz" \
--output-type z \
-o $ftoImpute"_nodupli.vcf.gz"
bcftools index $ftoImpute"_nodupli.vcf.gz"
else
echo "Error: File \"$toImpute\" does not exists "
exit 1
fi
# IMPUTE DATA - Autosomal Chromosomes
# splits data by chr before imputation
chrs=($(seq 1 1 22))
for chr in ${chrs[@]};
do
if [[ -f "${ftoImpute}.vcf.gz" ]]; then
bcftools view -Oz -r $chr $ftoImpute"_nodupli.vcf.gz" > $ftoImpute"_"chr$chr.vcf.gz
tabix -p vcf $ftoImpute"_"chr$chr.vcf.gz
harmonize $chr $ftoImpute $ref_panel phase3_v5.shapeit2_mvncall_integrated.noSingleton.genotypes $cores
# Impute data
eval ${minimac4} --refHaps ${ref_panel}${chr}.1000g.Phase3.v5.With.Parameter.Estimates.m3vcf.gz \
--haps $ftoImpute"_"chr$chr"_harm".vcf.gz \
--prefix $ftoImpute"_"chr$chr"_harm_imp" \
--format GT,DS,GP \
--ignoreDuplicates --rsid --log --cpus $cores
bcftools index $ftoImpute"_"chr$chr"_harm_imp.dose.vcf.gz" #Not tested
else
echo "Error: File \"$toImpute\" does not exists "
fi
done
# IMPUTE DATA - Chromosome X
chrs=("X")
for chr in ${chrs[@]}; do
if [[ -f "${ftoImpute}.vcf.gz" ]]; then
bcftools view -Oz -r $chr $ftoImpute"_nodupli.vcf.gz" > $ftoImpute"_"chr$chr.vcf.gz
tabix -p vcf $ftoImpute"_"chr$chr.vcf.gz
harmonize $chr $ftoImpute $ref_panel Non.Pseudo.Auto.phase3_v5.shapeit2_mvncall_integrated.noSingleton.genotypes $cores
# Impute data
eval ${minimac4} --refHaps ${ref_panel}${chr}.Non.Pseudo.Auto.1000g.Phase3.v5.With.Parameter.Estimates.m3vcf.gz \
--haps $ftoImpute"_"chr$chr"_harm".vcf.gz \
--prefix $ftoImpute"_"chr$chr"_harm_imp" \
--format GT,DS,GP \
--ignoreDuplicates --log --cpus $cores
bcftools index $ftoImpute"_"chr$chr"_harm_imp.dose.vcf.gz" #Not tested
else
echo "Error: File \"$toImpute\" does not exists "
fi
done
bcftools concat ${dest_path}*_harm_imp*.vcf.gz \
--output-type z \
-o ${dest_path}${filename}_harm_imp.vcf.gz
bcftools index ${dest_path}${filename}_harm_imp.vcf.gz
# ANNOTATION (3-Steps)
# 0. Remove all ID annotations from .vcf file
# 1. Annotate SNPs using initial file (from Lab)
# 2. Anootate not annotated SNPs with dbSNP database
# Remove all rsID from imputed file previous annotation steps
bcftools annotate -x ID ${dest_path}${filename}_harm_imp.vcf.gz \
--output-type z \
-o ${dest_path}${filename}_harm_imp_rsID_stp0.vcf.gz
bcftools index ${dest_path}${filename}_harm_imp_rsID_stp0.vcf.gz
# Annotate rsID after imputation (database: dbSNP) and Index file
bcftools annotate \
-a /PROJECTES/PUBLICDATA/REFERENCES/GRCh37/dbSNP/GRCh37_b151/All_20180423.vcf.gz \
-c ID ${dest_path}${filename}_harm_imp_rsID_stp0.vcf.gz \
--output-type z \
-o ${dest_path}${filename}_harm_imp_rsID_stp1.vcf.gz
bcftools index ${dest_path}${filename}_harm_imp_rsID_stp1.vcf.gz
# Annotate rsID after imputation with dbSNP database -
# ID annotated only if ID is present in source file and missing in target file
bcftools annotate \
-a /PROJECTES/PUBLICDATA/REFERENCES/GRCh37/dbSNP/GRCh37_b151/All_20180423.vcf.gz \
-c +ID ${dest_path}${filename}_harm_imp_rsID_stp1.vcf.gz \
--output-type z \
-o ${dest_path}${filename}_harm_imp_rsID.vcf.gz
#..# bgzip -c ${dest_path}${filename}_imp_rsID.vcf > ${dest_path}${filename}_imp_rsID.vcf.gz
bcftools index ${dest_path}${filename}_harm_imp_rsID.vcf.gz