forked from covid19-hg/META_ANALYSIS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeta.sub.wdl
466 lines (370 loc) · 12.9 KB
/
meta.sub.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
task run_range {
String docker
String pheno
String method
File conf
# not used but needed to localize files - locally referenced to in the conf file
Array[File] summary_stats
String opts
String chrom
Int n_studies = length(summary_stats)
command <<<
echo "`date` Biobank meta-analysis - run meta"
echo "docker: ${docker}"
echo "pheno: ${pheno}"
echo "method: ${method}"
echo "options: ${opts}"
echo "chrom: ${chrom}"
echo "conf: ${conf}"
echo "n studies: ${n_studies}"
printf "${sep='\n' summary_stats}\n"
/META_ANALYSIS/scripts/meta_analysis.py ${opts} --chrom ${chrom} ${conf} "${pheno}_${method}_chr${chrom}_meta" ${method} && \
echo "`date` done"
>>>
output {
File out = pheno + "_" + method + "_chr" + chrom + "_meta.gz"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "2 GB"
disks: "local-disk 200 HDD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task gather {
String docker
Array[File] meta_stats
String pheno
String method
File conf
Array[String] summary_stats
String opts
Int n_studies = length(summary_stats)
Int n_pieces = length(meta_stats)
Int min_n_studies
Int loglog_ylim
command <<<
echo "`date` Biobank HGI meta-analysis - gather results"
echo "docker: ${docker}"
echo "pheno: ${pheno}"
echo "method: ${method}"
echo "options: ${opts}"
echo "conf: ${conf}"
echo "n result pieces: ${n_pieces}"
echo "n studies: ${n_studies}"
printf "${sep='\n' summary_stats}\n"
echo "`date` gathering result pieces into one"
cat <(gunzip -c ${meta_stats[0]} | head -1) \
<(for file in ${sep=" " meta_stats}; do
gunzip -c $file | awk '
NR==1 {for (i=1;i<=NF;i++) a[$i]=i}
NR>1 && $a["all_meta_N"] >= ${min_n_studies}
';
done) \
| bgzip > ${pheno}_${method}_meta.gz
echo "`date` done"
>>>
output {
File out = pheno + "_" + method + "_meta.gz"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "20 GB"
disks: "local-disk 200 SSD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task add_rsids_af {
File file
String base = basename(file, ".gz")
File file_ref
String method
Float p_thresh
String docker
command <<<
python3 <<EOF | bgzip > ${base}.gz
import gzip, numpy
fp_ref = gzip.open('${file_ref}', 'rt')
ref_has_lines = True
ref_chr = 1
ref_pos = 0
ref_h_idx = {h:i for i,h in enumerate(fp_ref.readline().strip().split('\t'))}
with gzip.open('${file}', 'rt') as f:
header = f.readline().strip()
h_idx = {h:i for i,h in enumerate(header.split('\t'))}
n_idx = []
af_idx = []
for i,h in enumerate(header.split('\t')):
if h.endswith('_AF_Allele2'):
af_idx.append(i)
if h.endswith('_N') and not h.endswith('_meta_N'):
n_idx.append(i)
if len(n_idx) != len(af_idx):
raise Exception('Unexpected header in ${file} (' + str(len(n_idx)) + ') _N fields, (' + str(len(af_idx)) + ') AF fields')
print(header + '\tall_meta_sample_N\tall_meta_AF\trsid')
for line in f:
line = line.strip()
s = line.split('\t')
chr = int(s[h_idx['#CHR']])
pos = int(s[h_idx['POS']])
ref = s[h_idx['REF']]
alt = s[h_idx['ALT']]
ref_vars = []
while ref_has_lines and int(ref_chr) < chr or (int(ref_chr) == chr and ref_pos < pos):
ref_line = fp_ref.readline().strip().split('\t')
try:
ref_chr = ref_line[ref_h_idx['#chr']]
ref_pos = int(ref_line[ref_h_idx['pos']])
except IndexError:
ref_has_lines = False
while ref_has_lines and int(ref_chr) == chr and ref_pos == pos:
ref_vars.append(ref_line)
ref_line = fp_ref.readline().strip().split('\t')
try:
ref_chr = ref_line[ref_h_idx['#chr']]
ref_pos = int(ref_line[ref_h_idx['pos']])
except IndexError:
ref_has_lines = False
rsid = 'NA'
for r in ref_vars:
if r[ref_h_idx['ref']] == ref and r[ref_h_idx['alt']] == alt:
rsid = r[ref_h_idx['rsid']]
break
af_total = 0
n_sum = 0
for i,idx in enumerate(af_idx):
if s[idx] != 'NA':
af_total = af_total + float(s[idx]) * int(s[n_idx[i]])
n_sum = n_sum + int(s[n_idx[i]])
af_total = af_total / n_sum
print(line + '\t' + str(n_sum) + '\t' + numpy.format_float_scientific(af_total, precision=3) + '\t' + rsid)
EOF
echo "`date` filtering p-value ${p_thresh}"
gunzip -c ${base}.gz | awk '
NR==1 {for (i=1;i<=NF;i++) a[$i]=i; print $0}
NR>1 && $a["all_${method}_meta_p"] < ${p_thresh}
' > ${base}_${p_thresh}.txt
echo "`date` tabixing"
tabix -s 1 -b 2 -e 2 ${base}.gz
echo "`date` done"
>>>
output {
File out = base + ".gz"
File out_tbi = base + ".gz.tbi"
File sign = base + "_" + p_thresh + ".txt"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "2 GB"
disks: "local-disk 200 SSD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task filter_cols {
String docker
File file
String pheno
String method
Float p_thresh
String out_template
String outfile = sub(out_template, "\\{PHENO\\}", pheno)
command <<<
echo "`date` Biobank meta-analysis - filter columns"
echo "docker: ${docker}"
echo "file: ${file}"
echo "pheno: ${pheno}"
echo "out_template: ${out_template}"
echo "`date` filtering columns"
gunzip -c ${file} | awk '
BEGIN {FS=OFS="\t"}
NR==1 {for(i=1;i<=NF;i++) { a[$i]=i; if (i<6||$i~"_AF_Allele2$"||$i~"_AF_fc$"||$i~"_N$"||$i~"^all_"||$i~"^rsid") use[i]=1 }}
NR>=1 {printf $1; for(i=2;i<=NF;i++) if(use[i]==1) printf "\t"$i; printf "\n"}' | \
bgzip > ${outfile}
echo "`date` tabixing"
tabix -s 1 -b 2 -e 2 ${outfile}
echo "`date` filtering p-value ${p_thresh}"
gunzip -c ${outfile} | awk '
NR==1 {for (i=1;i<=NF;i++) a[$i]=i; print $0}
NR>1 && $a["all_${method}_meta_p"] < ${p_thresh}
' > ${outfile}_${p_thresh}.txt
echo "`date` done"
>>>
output {
File out = outfile
File out_tbi = outfile + ".tbi"
File sign = outfile + "_" + p_thresh + ".txt"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "2 GB"
disks: "local-disk 200 SSD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task lift {
String docker
File file
String base = basename(file, ".txt.gz")
String method
Float p_thresh
command <<<
echo "`date` Biobank meta-analysis - liftover to 37"
echo "docker: ${docker}"
echo "file: ${file}"
echo "method: ${method}"
echo "p_thresh: ${p_thresh}"
TEMP=/cromwell_root
TMP=/cromwell_root
TMPDIR=/cromwell_root
mv ${file} ${base}.gz
echo "`date` liftover"
time /META_ANALYSIS/scripts/lift.py -chr "#CHR" -pos POS -ref REF -alt ALT \
-chain_file /liftover/hg38ToHg19.over.chain.gz -tmp_path /cromwell_root/ \
${base}.gz > ${base}.lift.out 2> ${base}.lift.err
gunzip -c ${base}.gz.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]+$/) {
$a["SNP"] = $a["#CHR"]":"$a["POS"]":"$a["REF"]":"$a["ALT"]
print $0
}
}' | bgzip > ${base}.b37.txt.gz
echo "`date` tabixing"
tabix -s1 -b2 -e2 ${base}.b37.txt.gz
echo "`date` filtering p-value ${p_thresh}"
gunzip -c ${base}.b37.txt.gz | awk '
NR==1 {for (i=1;i<=NF;i++) a[$i]=i; print $0}
NR>1 && $a["all_${method}_meta_p"] < ${p_thresh}
' > ${base}.b37_${p_thresh}.txt
#echo "`date` plotting qq and manhattan"
gunzip -c ${base}.b37.txt.gz | awk '
BEGIN {FS=OFS="\t"}
NR==1 {for(i=1;i<=NF;i++) a[$i]=i;}
{print $a["#CHR"],$a["POS"],$a["all_${method}_meta_p"],$a["all_meta_AF"],$a["REF"],$a["ALT"]}
' > ${base}.b37_meta_p_forplot.txt
echo "`date` done"
>>>
output {
File lift_out = base + ".lift.out"
File lift_err = base + ".lift.err"
File out = base + ".b37.txt.gz"
File out_tbi = base + ".b37.txt.gz.tbi"
File sign = base + ".b37_" + p_thresh + ".txt"
File out_plot = base + ".b37_meta_p_forplot.txt"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "20 GB"
disks: "local-disk 200 SSD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task plot {
String docker
File file
String base = basename(file)
String method
command <<<
mv ${file} ${base}
#head -n 1 ${base} > ${base}.temp
#sort -k1,1g -k2,2g <(tail -n +2 ${base}) >> ${base}.temp
#mv ${base}.temp ${base}
echo "`date` Manhattan plot start"
/plot_scripts/ManhattanPlot.r --input=${base} --PVAL="all_${method}_meta_p" --knownRegionFlank=1000000 --prefix="${base}_${method}_meta" --ismanhattanplot=TRUE --isannovar=FALSE --isqqplot=FALSE --CHR="#CHR" --POS="POS" --ALLELE1=REF --ALLELE2=ALT
echo "`date` QQ plot start"
/plot_scripts/QQplot.r --input=${base} --prefix="${base}_${method}_meta" --af=all_meta_AF --pvalue=all_${method}_meta_p
echo "`date` QQ plot done"
>>>
output {
File out1 = base + "_" + method + "_meta.regions.txt"
File out2 = base + "_" + method + "_meta.tophits.txt"
Array[File] pngs = glob("*.png")
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "20 GB"
disks: "local-disk 200 SSD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
task annovar {
String docker
File file_tophit
File file_meta
String base = basename(file_tophit)
String base_meta = basename(file_meta)
command <<<
mv ${file} ${base}
mv ${file_meta} ${base_meta}
echo "`date` annovar start"
perl /annovar/table_annovar.pl ${base} /annovar/humandb -buildver hg38 -out ${base}_annovar -remove -protocol refGene -operation gx -nastring NA -polish
zcat ${base_meta} | head -n 1 > ${base}_meta.txt
zgrep <(awk '{print $1"\t"$2"\t"$4"\t"$5}' ${base}) ${base_meta} >> ${base}_meta.txt
Rscript /annovar/mergeAnnovar.r --input_anno=${base}_annovar.hg38_multianno.txt --input_meta=${base}_meta.txt --outfile=${base}_meta_annovar.txt
>>>
output {
File out = base + "_meta_annovar.txt"
}
runtime {
docker: "${docker}"
cpu: "1"
memory: "20 GB"
disks: "local-disk 200 SSD"
zones: "us-central1-f"
preemptible: 0
noAddress: false
}
}
workflow run_meta {
String pheno
Int min_n_studies
String conf
String method
String opts
Array[String] summary_stats
scatter (chr in range(23)) {
call run_range {
input: pheno=pheno, method=method, opts=opts, conf=conf, summary_stats=summary_stats, chrom=chr+1
}
}
call gather {
input: pheno=pheno, method=method, opts=opts, conf=conf, summary_stats=summary_stats, meta_stats=run_range.out, min_n_studies=min_n_studies
}
call add_rsids_af {
input: file=gather.out, method=method
}
call filter_cols {
input: pheno=pheno, file=add_rsids_af.out, method=method
}
call lift {
input: file=filter_cols.out, method=method
}
call plot {
input: file=add_rsid_af.out, method=method
}
call annovar {
input: file_tophit=plot.out2, file_meta=lift.out
}
}