forked from covid19-hg/META_ANALYSIS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
munge_sumstats_beforeQC_obtain_QClist.wdl
490 lines (400 loc) · 15.2 KB
/
munge_sumstats_beforeQC_obtain_QClist.wdl
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
task clean_filter {
String docker
File sumstat_file
String af_col
String N_col
String Ncontrol_col
String Ncase_col
String info_col
Float min_mac
Float min_info
String outfile = sub(basename(sumstat_file, ".gz"), "\\.bgz$", "") + ".munged.MAC." + min_mac + ".INFO." + min_info + ".gz"
String dollar = "$"
command <<<
#TODO use pandas or something
echo "Biobank meta-analysis - clean and filter sumstats"
echo "${sumstat_file}"
echo ""
echo "`date` original number of variants"
gunzip -c ${sumstat_file} | tail -n+2 | wc -l
chr_col=$(gunzip -c ${sumstat_file} | head -1 | tr '\t ' '\n' | grep -nx "#CHR\|CHR" | head -1 | cut -d ':' -f1)
pos_col=$(gunzip -c ${sumstat_file} | head -1 | tr '\t ' '\n' | grep -nx "POS" | head -1 | cut -d ':' -f1)
N_col_num=$(gunzip -c ${sumstat_file} | head -1 | tr '\t ' '\n' | grep -nx "${N_col}" | head -1 | cut -d ':' -f1)
Ncase_col_num=$(gunzip -c ${sumstat_file} | head -1 | tr '\t ' '\n' | grep -nx "${Ncase_col}" | head -1 | cut -d ':' -f1)
Ncontrol_col_num=$(gunzip -c ${sumstat_file} | head -1 | tr '\t ' '\n' | grep -nx "${Ncontrol_col}" | head -1 | cut -d ':' -f1)
printf "`date` col CHR "${dollar}{chr_col}" col POS "${dollar}{pos_col}"\n"
if [ -z "$N_col_num" ]; then
gunzip -c ${sumstat_file} | awk -v infoHeader=${info_col} ' \
#BEGIN{FS="\t| "; OFS="\t"}
BEGIN{FS="[ \t]+"; OFS="\t"}
NR==1 {
for (i=1;i<=NF;i++) { sub("^CHR", "#CHR", $i); a[$i]=i; if ($i=="POS") pos=i }
gsub("Pvalue", "p.value", $0);
gsub(infoHeader, "imputationInfo", $0)
print $0
} NR>1 {
sub("^0", "", $a["#CHR"]); sub("^chr", "", $a["#CHR"]); sub("^X", "23", $a["#CHR"]);
if ($a["#CHR"] ~ /^[0-9]+$/ && $a["p.value"] != 0 && $a["BETA"] < 1e6 && $a["BETA"] > -1e6 && ($a["${af_col}"]*2*($a["${Ncase_col}"]+$a["${Ncontrol_col}"]))>${min_mac} && (2*($a["${Ncase_col}"]+$a["${Ncontrol_col}"])*(1-$a["${af_col}"]))>${min_mac} && $a["imputationInfo"]>${min_info}) {
printf $1
for (i=2; i<=NF; i++) {
if (i==pos) {
printf "\t%d", $i
} else {
printf "\t"$i
}
}
printf "\n"
}
}' | \
sort -k$chr_col,${dollar}{chr_col}g -k$pos_col,${dollar}{pos_col}g -u | \
bgzip > ${outfile}
else
gunzip -c ${sumstat_file} | awk ' \
BEGIN{FS="\t| "; OFS="\t"}
NR==1 {
for (i=1;i<=NF;i++) { sub("^CHR", "#CHR", $i); a[$i]=i; if ($i=="POS") pos=i }
gsub("Pvalue", "p.value", $0);
print $0
} NR>1 {
sub("^0", "", $a["#CHR"]); sub("^chr", "", $a["#CHR"]); sub("^X", "23", $a["#CHR"]);
if ($a["#CHR"] ~ /^[0-9]+$/ && $a["p.value"] != 0 && $a["BETA"] < 1e6 && $a["BETA"] > -1e6 && (2*$a["${N_col}"]*$a["${af_col}"])>${min_mac} && (2*$a["${N_col}"]*(1-$a["${af_col}"]))>${min_mac} && $a["${info_col}"]>${min_info}) {
printf $1
for (i=2; i<=NF; i++) {
if (i==pos) {
printf "\t%d", $i
} else {
printf "\t"$i
}
}
printf "\n"
}
}' | \
sort -k$chr_col,${dollar}{chr_col}g -k$pos_col,${dollar}{pos_col}g -u | \
bgzip > ${outfile}
fi
tabix -S 1 -s $chr_col -b $pos_col -e $pos_col ${outfile}
echo "`date` new number of variants"
gunzip -c ${outfile} | tail -n+2 | wc -l
echo "`date` headers"
gunzip -c ${outfile} | head -1 | tr '\t' '\n'
gunzip -c ${outfile} | tail -n+2 | cut -f $chr_col | uniq > chr.tmp
echo "`date` $(wc -l chr.tmp | cut -d' ' -f1) chromosomes"
cat chr.tmp
echo "`date` unique number of fields"
gunzip -c ${outfile} | awk 'BEGIN{FS="\t"} {print NF}' | sort -u > n.tmp
cat n.tmp
if [ $(wc -l n.tmp | cut -d' ' -f1) != 1 ]; then echo "file not square"; exit 1; fi
if [ $(wc -l chr.tmp | cut -d' ' -f1) -lt 22 ]; then echo "less than 22 chromosomes"; exit 1; fi
echo "`date` done"
>>>
output {
File out = outfile
File tbi = outfile + ".tbi"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "2 GB"
disks: "local-disk 200 HDD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task lift {
String docker
File sumstat_file
File tbi_file = sumstat_file + ".tbi"
String base = basename(sumstat_file)
File b37_ref
File b38_ref
String dollar = "$"
command <<<
echo "Biobank meta-analysis - lift over sumstats if needed"
echo "${sumstat_file}"
echo "${b37_ref}"
echo "${b38_ref}"
echo ""
mv ${sumstat_file} ${base}
mv ${tbi_file} ${base}.tbi
tabix -R ${b37_ref} ${base} | wc -l > b37.txt
tabix -R ${b38_ref} ${base} | wc -l > b38.txt
echo "`date` `cat b37.txt` chr 21 variants build 37"
echo "`date` `cat b38.txt` chr 21 variants build 38"
if ((`cat b37.txt` == 0 && `cat b38.txt` == 0)); then
echo "`date` no chr 21 variants found in either build, quitting"
exit 1
fi
if ((`cat b37.txt` > `cat b38.txt`)); then
echo "`date` lifting to build 38"
time /META_ANALYSIS/scripts/lift.py -chr "#CHR" -pos POS -ref Allele1 -alt Allele2 \
-chain_file /META_ANALYSIS/data/hg19ToHg38.over.chain.gz -tmp_path /cromwell_root/ \
${base} > ${base}.lift.out 2> ${base}.lift.err
gunzip -c ${base}.lifted.gz | \
cut -f2- | awk '
BEGIN { FS=OFS="\t" }
NR==1 { for (i=1;i<=NF;i++) a[$i]=i; print $0 }
NR>1 {
temp=$a["#CHR"]; $a["#CHR"]=$a["anew_chr"]; $a["anew_chr"]=temp; temp=$a["POS"]; $a["POS"]=$a["anew_pos"]; $a["anew_pos"]=temp;
sub("^0", "", $a["#CHR"]); sub("^chr", "", $a["#CHR"]); sub("^X", "23", $a["#CHR"]);
if ($a["#CHR"] ~ /^[0-9]+$/) {
print $0
}
}' | bgzip > ${base}
else
echo "`date` presumably already in build 38"
fi
>>>
output {
File out = base
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "20 GB"
disks: "local-disk 200 HDD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task harmonize {
String docker
File sumstat_file
String base = basename(sumstat_file)
File gnomad_ref
String gnomad_ref_base = basename(gnomad_ref)
Int n
String options
command <<<
echo "Biobank meta-analysis - harmonize sumstats to reference"
echo "${sumstat_file}"
echo "${gnomad_ref}"
echo ""
mv ${sumstat_file} ${base}
mv ${gnomad_ref} ${gnomad_ref_base}
echo "`date` harmonizing stats with gnomAD"
python3 /META_ANALYSIS/scripts/harmonize.py ${base} ${gnomad_ref_base} ${n} ${options}\
| bgzip > ${base}.${gnomad_ref_base} && \
tabix -S 1 -s 1 -b 2 -e 2 ${base}.${gnomad_ref_base} && \
echo "`date` done"
>>>
output {
File out = base + "." + gnomad_ref_base
File out_tbi = base + "." + gnomad_ref_base + ".tbi"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "2 GB"
disks: "local-disk 200 SSD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task plot {
File sumstat_file
String base = basename(sumstat_file)
String pop
String docker
command <<<
mv ${sumstat_file} ${base} && \
Rscript - <<EOF
require(ggplot2)
require(data.table)
options(bitmapType='cairo')
data <- fread("${base}")
png("${base}_AF.png", width=1000, height=1000, units="px")
p <- ggplot(data, aes_string(x="AF_Allele2", y="AF_gnomad_v3_b38_ref_${pop}")) +
geom_point(alpha=0.1) +
xlab("AF_${base}") +
theme_minimal(base_size=18)
print(p)
dev.off()
EOF
echo "`date` QQ plot start"
/plot_scripts/QQplot.r --input=${base} --prefix="${base}" --af=AF_Allele2 --pvalue=p.value
echo "`date` Manhattan plot start"
/plot_scripts/ManhattanPlot.r --input=${base} --PVAL=p.value --knownRegionFlank=1000000 --prefix="${base}" --ismanhattanplot=TRUE --isannovar=FALSE --isqqplot=FALSE --CHR="#CHR" --POS=POS --ALLELE1=Allele1 --ALLELE2=Allele2
>>>
output {
Array[File] pngs = glob("*.png")
File out = base + ".regions.txt"
File out_tophits = base + ".tophits.txt"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: 15*ceil(size(sumstat_file, "G")) + " GB"
disks: "local-disk 200 HDD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task plot2 {
File sumstat_file
String base = basename(sumstat_file)
String pop
String docker
command <<<
mv ${sumstat_file} ${base} && \
Rscript - <<EOF
require(ggplot2)
require(data.table)
options(bitmapType='cairo')
data <- fread("${base}")
png("${base}_AF.png", width=1000, height=1000, units="px")
p <- ggplot(data, aes_string(x="AF_Allele2", y="AF_gnomad_v3_b38_ref_${pop}")) +
geom_point(alpha=0.1) +
xlab("AF_${base}") +
theme_minimal(base_size=18)
print(p)
dev.off()
EOF
echo "`date` QQ plot start"
/plot_scripts/QQplot.r --input=${base} --prefix="${base}" --af=AF_Allele2 --pvalue=p.value
echo "`date` Manhattan plot start"
/plot_scripts/ManhattanPlot.r --input=${base} --PVAL=p.value --knownRegionFlank=1000000 --prefix="${base}" --ismanhattanplot=TRUE --isannovar=FALSE --isqqplot=FALSE --CHR="#CHR" --POS=POS --ALLELE1=Allele1 --ALLELE2=Allele2
>>>
output {
Array[File] pngs = glob("*.png")
File out = base + ".regions.txt"
File out_tophits = base + ".tophits.txt"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: 15*ceil(size(sumstat_file, "G")) + " GB"
disks: "local-disk 200 HDD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task filterbyfc_comparetoLOCObbk_qc {
String docker
File sumstat_file
String base = basename(sumstat_file)
File rmlistfile
String rmlistfile_base = basename(rmlistfile)
File amlistfile
String amlistfile_base = basename(amlistfile)
String outfile = sub(basename(sumstat_file, ".gz"), "\\.bgz$", "") + ".postGWASQC.gz"
command <<<
echo "Biobank meta-analysis - postGWASQC-filterbyfc_comparetoLOCObbk"
echo "${sumstat_file}"
echo ""
mv ${sumstat_file} ${base}
mv ${rmlistfile} ${rmlistfile_base}
mv ${amlistfile} ${amlistfile_base}
python3 /META_ANALYSIS/scripts/harmonize_postGWASQC.py ${base} ${rmlistfile_base} ${amlistfile_base} \
| bgzip > ${outfile} && \
tabix -S 1 -s 1 -b 2 -e 2 ${outfile} && \
echo "`date` done"
>>>
output {
File out = outfile
File out_tbi = outfile + ".tbi"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "2 GB"
disks: "local-disk 200 SSD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task filterbyfc_comparetognomAD_qc {
File sumstat_file
String base = basename(sumstat_file)
String bbkpop
String docker
command <<<
mv ${sumstat_file} ${base} && \
echo "`date` postGWASQC comparing af to gnomAD start"
/plot_scripts/postGWASQC_af_compareto_gnomAD.r --infile=${base} --bbkpop=${bbkpop} --minNingnomAD=100
echo "`date` postGWASQC comparing af to gnomAD end"
bgzip -c ${bbkpop}_postGWASQC_comparetognomAD.txt > ${bbkpop}_postGWASQC_comparetognomAD.txt.gz
tabix -S 1 -s 1 -b 2 -e 2 ${bbkpop}_postGWASQC_comparetognomAD.txt.gz
>>>
output {
File out = bbkpop + "_postGWASQC_comparetognomAD.txt.gz"
File out_tbi = bbkpop + "_postGWASQC_comparetognomAD.txt.gz.tbi"
File out_alleleflip = bbkpop + "_postGWASQC_comparetognomAD_ambiguity.txt"
}
runtime {
docker: "${docker}"
cpu: "1"
#memory: 30*ceil(size(sumstat_file, "G")) + " GB"
memory: 30*ceil(size(sumstat_file, "G")) + " GB"
disks: "local-disk 200 HDD"
zones: "us-central1-f"
preemptible: 2
noAddress: false
}
}
task filterbyfc_comparetognomAD_qc_mean3SD {
File sumstat_file
String base = basename(sumstat_file)
String bbkpop
String docker
command <<<
mv ${sumstat_file} ${base} && \
echo "`date` postGWASQC comparing af to gnomAD start"
/plot_scripts/evaluateMD_compareto_gnomAD.r --infile=${base} --bbkpop=${bbkpop} --minNingnomAD=100
echo "`date` postGWASQC comparing af to gnomAD end"
# Array[File] pngs = glob("*.png")
# File out = bbkpop + "_squaremd_summary.txt"
>>>
output {
File out2 = bbkpop + "_postGWASQC_comparetognomAD_MDge3SDfromMean.txt"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: 20*ceil(size(sumstat_file, "G")) + " GB"
disks: "local-disk 200 HDD"
zones: "us-central1-f"
preemptible: 1
noAddress: false
}
}
workflow munge_sumstats {
File sumstats_loc
Array[Array[String]] sumstat_files = read_tsv(sumstats_loc)
String gnomad_ref_template
scatter (sumstat_file in sumstat_files) {
#call clean_filter {
# input: sumstat_file=sumstat_file[0], info_col=sumstat_file[3]
#}
#call lift {
# input: sumstat_file=clean_filter.out
# #input: sumstat_file=sumstat_file[0]
#}
#call harmonize {
# #input: sumstat_file=sumstat_file[0], gnomad_ref=sub(gnomad_ref_template, "POP", sumstat_file[1]), n=sumstat_file[2]
# input: sumstat_file=lift.out, gnomad_ref=sub(gnomad_ref_template, "POP", sumstat_file[1]), n=sumstat_file[2]
#}
#call plot {
# input: sumstat_file=harmonize.out, pop=sumstat_file[1]
#}
call filterbyfc_comparetognomAD_qc {
#input: sumstat_file=harmonize.out, bbkpop=sumstat_file[1]
input: sumstat_file=sumstat_file[0], bbkpop=sumstat_file[1]
}
call filterbyfc_comparetognomAD_qc_mean3SD {
input: sumstat_file=filterbyfc_comparetognomAD_qc.out, bbkpop=sumstat_file[1]
#input: sumstat_file=sumstat_file[0], bbkpop=sumstat_file[1]
}
#call filterbyfc_comparetoLOCObbk_qc {
# input: sumstat_file=harmonize.out, rmlistfile=sumstat_file[4], amlistfile=sumstat_file[5]
#}
#call plot2 {
# input: sumstat_file=filterbyfc_comparetoLOCObbk_qc.out, pop=sumstat_file[1]
#}
}
}