-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLTBR.sh
executable file
·1084 lines (1053 loc) · 46.1 KB
/
LTBR.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/bash
function MS_eQTLGen_SCALLOP()
{
module load python/2.7
# init -- actually two versions of RNASeq results below gives the same LTBR.lz
zgrep ENSG00000111321 ${INF}/work/ensGtp.txt.gz | \
cut -f2 | \
zgrep -f - ${INF}/work/ensemblToGeneName.txt.gz
export rnaseq=tensorqtl_trans_MAF0.005_age_sex_rin_batch_readDepth_PC10_PEER20_merged_annotated.csv
export rnaseq=tensorqtl_allSNPs_MAF0.005_merged_annotated.csv
grep -w -e ${rsid1} -e ${rsid2} ${rnaseq}
# LocusZoom plot
read chr start end < st.tmp
awk -vFS="," -vchr=${chr} -vstart=${start} -vend=${end} -vgene=${gene} 'NR==1 || ($5==chr && $6>=start && $6<=end && index($0,gene)>0)' ${rnaseq} | \
tr "," "\t" > LTBR.lz
rm -f ld_cache.db
locuszoom --source 1000G_Nov2014 --build hg19 --pop EUR --metal LTBR.lz --delim tab title="INTERVAL-LTBR" \
--markercol variant_id --pvalcol pval --chr ${chr} --start ${b1} --end ${b2} \
--no-date --plotonly --prefix=INTERVAL --rundir .
mv INTERVAL_chr${chr}_${bracket}.pdf INTERVAL-LTBR-cis.pdf
# gunzip -c discovery_metav3.0.meta.gz | grep rs1800693
# CHR BP SNP A1 A2 N P OR
# 12 6440009 rs1800693 T C 14 1.017e-13 0.8808
# gunzip -c Final-metaanalysis-echip.txt.gz | grep 1800693
# CHR BP SNP A1 A2 N P P(R) OR OR(R) Q I
# 12 6440009 exm-rs1800693 T C 13 1.003e-26 2.11e-09 1.0332 1.0300 0.0351 46.00
(
echo -e "SNPid\tSNP\tchr\tpos\ta1\ta2\tb\tse\tp"
Rscript -e 'require(dplyr)
write.table(read.table(Sys.getenv("v3"),header=TRUE) %>%
mutate(A1=toupper(A1),A2=toupper(A2),
SNP=paste0("chr",CHR,":",BP,"_",if_else(A1>A2,paste0(A2,"_",A1),paste0(A1,"_",A2))),
b=log(OR),se=TwoSampleMR::get_se(b,P)) %>%
filter(CHR==as.integer(Sys.getenv("chr")) & BP>=as.integer(Sys.getenv("b1")) & BP < as.integer(Sys.getenv("b2"))) %>%
filter(!is.na(b) & !is.na(se) & b!=0 & se!=0) %>%
arrange(BP) %>%
select(CHR,BP,SNP,A1,A2,b,se,P),
col.names=FALSE,row.names=FALSE,quote=FALSE)' | \
sort -k1,1 | \
join -23 ${INF}/work/INTERVAL.rsid - | \
tr ' ' '\t'
) > ${INF}/MS/v3.lz
rm -rf ld_cache.db
locuszoom --source 1000G_Nov2014 --build hg19 --pop EUR --metal ${INF}/MS/v3.lz --delim tab title="MS" \
--markercol SNP --pvalcol p --chr ${chr} --start ${b1} --end ${b2} \
--no-date --plotonly --prefix="v3" --rundir .
qpdf v3_chr${chr}_${b1}-${b2}.pdf --pages . 1 -- v3-lz.pdf
# https://www.eqtlgen.org/trans-eqtls.html
# https://www.eqtlgen.org/cis-eqtls.html
export AF=2018-07-18_SNP_AF_for_AlleleB_combined_allele_counts_and_MAF_pos_added.txt.gz
export cis=2019-12-11-cis-eQTLsFDR-ProbeLevel-CohortInfoRemoved-BonferroniAdded.txt.gz
read chr start end < st.tmp
(
gunzip -c ~/rds/public_databases/eQTLGen/${AF} | \
awk -vchr=${chr} -vstart=${start} -vend=${end} -vgene=${gene} 'NR==1||($2==chr && $3>=start && $3<=end)' > eQTLGen.AF
gunzip -c ~/rds/public_databases/eQTLGen/${cis} | \
head -1
gunzip -c ~/rds/public_databases/eQTLGen/${cis} | \
awk -vchr=${chr} -vstart=${start} -vend=${end} -vgene=${gene} '$3==chr && $4>=start && $4<=end && index($0,gene)>0' | \
sort -k3,3n -k4,4n
) > eQTLGen.lz
rm -f ld_cache.db
locuszoom --source 1000G_Nov2014 --build hg19 --pop EUR --metal eQTLGen.lz --delim tab title="eQTLGen-LTBR" \
--markercol SNP --pvalcol Pvalue --chr ${chr} --start ${b1} --end ${b2} \
--no-date --plotonly --prefix=eQTLGen --rundir .
mv eQTLGen_chr${chr}_${bracket}.pdf eQTLGen-LTBR-cis.pdf
read chr start end < st.tmp
(
gunzip -c ${INF}/METAL/TNFB-1.tbl.gz | \
head -1 | \
awk '{$1=$3 " " $1;$3="";$12="P"};1' | \
awk '{$1=$1};1'
gunzip -c ${INF}/METAL/TNFB-1.tbl.gz | \
awk -vOFS="\t" -vchr=${chr} -vstart=${start} -vend=${end} '$1 == chr && $2 >= start && $2 <= end {$12=10^$12;print}' | \
sort -k3,3 | \
join -23 <(awk -vchrom=chr${chr} 'index($0,chrom)>0' INTERVAL.rsid) - | \
sort -k2,2n -k3,3n | \
cut -d' ' -f1 --complement
) | \
tr ' ' '\t' > TNFB.lz
rm -f ld_cache.db
locuszoom --source 1000G_Nov2014 --build hg19 --pop EUR --metal TNFB.lz --delim tab title="SCALLOP-TNFB" \
--markercol MarkerName --pvalcol P --chr ${chr} --start ${b1} --end ${b2} --gwas-cat whole-cat_significant-only \
--no-date --plotonly --prefix=TNFB --rundir .
mv TNFB_chr${chr}_${bracket}.pdf SCALLOP-TNFB-cis.pdf
}
function stack_assoc_plot_hyprcoloc()
{
join <(sed '1d' ${INF}/MS/v3.lz | awk '{print $1,$7/$8,$5,$6,$2}' | sort -k1,1) \
<(sed '1d' ${INF}/work/eQTLGen.lz | \
awk '{
chr=$3;pos=$4;A1=toupper($5);A2=toupper($6);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$7,A1,A2,$2,$3,$4
}' | \
sort -k1,1 \
) | \
join - <(sed '1d' ${INF}/work/TNFB.lz | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$10/$11,A1,A2,$1
}' | \
sort -k1,1 \
) | \
awk -vOFS="\t" '
{
if($3!=$7) $6=-$6
if($3!=$13) $12=-$12
print $1,$5,$10,$11,$3,$4,$2,$6,$12
}' | \
awk 'a[$1]++==0' | \
awk 'a[$2]++==0' | awk '$2!="NA"' > ${INF}/work/LTBR.gassoc
cut -d' ' -f1 ${INF}/work/LTBR.gassoc > ${INF}/work/LTBR.snpid
plink --bfile ${INF}/INTERVAL/cardio/INTERVAL --extract ${INF}/work/LTBR.snpid --r square --out ${INF}/work/LTBR
plink --bfile ${INF}/INTERVAL/cardio/INTERVAL --extract ${INF}/work/LTBR.snpid --freq --out ${INF}/work/LTBR
Rscript -e '
INF <- Sys.getenv("INF")
library(gassocplot)
d <- read.table(file.path(INF,"work","LTBR.gassoc"),col.names=c("snpid","marker","chr","pos","A1","A2","MS","LTBR","TNFB"))
markers <- d[c("marker","chr","pos")]
ld <- read.table(file.path(INF,"work","LTBR.ld"),col.names=with(d,marker),row.names=with(d,marker))
z <- d[c("MS","LTBR","TNFB")]
rownames(z) <- with(d,marker)
sap <- stack_assoc_plot(markers, z, ld, traits = c("MS","LTBR","TNFB"), ylab = "-log10(P)", top.marker="rs1800693",legend=TRUE)
pdf(file.path(INF,"plots","LTBR.pdf"),width=8,height=13)
grid::grid.draw(sap)
dev.off()
# stack_assoc_plot_save(sap, "LTBR.png", 5, width=8, height=13, dpi=300)
'
join <(sed '1d' ${INF}/MS/v3.lz | awk '{print $1,$7,$5,$6,$2}' | sort -k1,1) \
<(Rscript -e '
suppressMessages(library(dplyr))
cis_pQTL <- merge(read.delim("eQTLGen.lz") %>% filter(GeneSymbol=="LTBR"),
read.delim("eQTLGen.AF"),by="SNP") %>%
mutate(data.frame(gap::get_b_se(AlleleB_all,NrSamples,Zscore)))
write.table(cis_pQTL[c("SNP","SNPChr","SNPPos","AssessedAllele","OtherAllele","b")], sep = "\t",
row.names = FALSE, col.names = TRUE, quote=FALSE)
' | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$6,A1,A2,$1,$2,$3
}' | \
sort -k1,1 \
) | \
join - <(sed '1d' ${INF}/work/TNFB.lz | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$10,A1,A2,$1
}' | \
sort -k1,1 \
) | \
awk -vOFS="\t" '
{
if($3!=$7) $6=-$6
if($3!=$13) $12=-$12
print $1,$5,$10,$11,$3,$4,$2,$6,$12
}' | \
awk 'a[$1]++==0' | \
awk 'a[$2]++==0' | awk '$2!="NA"' > ${INF}/work/LTBR.beta
join <(sed '1d' ${INF}/MS/v3.lz | awk '{print $1,$8,$5,$6,$2}' | sort -k1,1) \
<(Rscript -e '
suppressMessages(library(dplyr))
cis_pQTL <- merge(read.delim("eQTLGen.lz") %>% filter(GeneSymbol=="LTBR"),read.delim("eQTLGen.AF"),by="SNP") %>%
mutate(data.frame(gap::get_b_se(AlleleB_all,NrSamples,Zscore)))
write.table(cis_pQTL[c("SNP","SNPChr","SNPPos","AssessedAllele","OtherAllele","se")], sep = "\t",
row.names = FALSE, col.names = TRUE, quote=FALSE)
' | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$6,A1,A2,$1,$2,$3
}' | \
sort -k1,1 \
) | \
join - <(sed '1d' ${INF}/work/TNFB.lz | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$11,A1,A2,$1
}' | \
sort -k1,1 \
) | \
awk -vOFS="\t" '
{
print $1,$5,$10,$11,$3,$4,$2,$6,$12
}' | \
awk 'a[$1]++==0' | \
awk 'a[$2]++==0' | awk '$2!="NA"' > ${INF}/work/LTBR.se
Rscript -e '
options(width=200)
id <- c("marker","chr","pos")
traits <- c("MS","LTBR","TNFB")
d <- read.table("LTBR.beta",col.names=c("snpid",id,"A1","A2",traits))
markers <- d[id]
betas <- as.matrix(d[traits])
rownames(betas) <- with(d,marker)
d <- read.table("LTBR.se",col.names=c("snpid",id,"A1","A2",traits))
ses <- as.matrix(d[traits])
rownames(ses) <- with(d,marker)
hyprcoloc::hyprcoloc(betas, ses, trait.names=traits, snp.id=with(markers,marker))
'
}
function PWCoCo()
{
join <(sed '1d' ${INF}/MS/rs1800693/EUR-v3.cma.cojo | awk '{if($12!="NA") print $2,$11/$12,$4,$4,$2}' | sort -k1,1) \
<(sed '1d' ${INF}/work/eQTLGen.lz | \
awk '{
chr=$3;pos=$4;A1=toupper($5);A2=toupper($6);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$7,A1,A2,$2,$3,$4
}' | \
sort -k1,1 \
) | \
join - <(sed '1d' ${INF}/work/TNFB.lz | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$10/$11,A1,A2,$1
}' | \
sort -k1,1 \
) | \
awk -vOFS="\t" '
{
if($3!=$7) $6=-$6
if($3!=$13) $12=-$12
print $1,$9,$10,$11,$3,$4,$2,$6,$12
}' | \
awk 'a[$1]++==0' | \
awk 'a[$2]++==0' | awk '$2!="NA"' > ${INF}/MS/rs1800693/LTBR.gassoc
cut -d' ' -f1 ${INF}/MS/rs1800693/LTBR.gassoc > ${INF}/MS/rs1800693/LTBR.snpid
plink --bfile ${INF}/INTERVAL/cardio/INTERVAL --extract ${INF}/MS/rs1800693/LTBR.snpid --r square --out ${INF}/MS/rs1800693/LTBR
plink --bfile ${INF}/INTERVAL/cardio/INTERVAL --extract ${INF}/MS/rs1800693/LTBR.snpid --freq --out ${INF}/MS/rs1800693/LTBR
Rscript -e '
INF <- Sys.getenv("INF")
library(gassocplot)
d <- read.table(file.path(INF,"MS","rs1800693","LTBR.gassoc"),col.names=c("snpid","marker","chr","pos","A1","A2","MS","LTBR","TNFB"))
markers <- d[c("marker","chr","pos")]
ld <- read.table(file.path(INF,"MS","rs1800693","LTBR.ld"),col.names=with(d,marker),row.names=with(d,marker))
z <- d[c("MS","LTBR","TNFB")]
rownames(z) <- with(d,marker)
sap <- stack_assoc_plot(markers, z, ld, traits = c("MS","LTBR","TNFB"), ylab = "-log10(P)", top.marker="rs2364485",legend=TRUE)
pdf(file.path(INF,"MS","rs1800693","LTBR.pdf"),width=8,height=13)
grid::grid.draw(sap)
dev.off()
'
join <(sed '1d' ${INF}/MS/rs1800693/EUR-v3.cma.cojo | awk '{if($12!="NA") print $2,$11,$4,$4,$2}' | sort -k1,1) \
<(Rscript -e '
INF <- Sys.getenv("INF")
suppressMessages(library(dplyr))
cis_pQTL <- merge(read.delim(file.path(INF,"work","eQTLGen.lz")) %>%
filter(GeneSymbol=="LTBR"),read.delim(file.path(INF,"work","eQTLGen.AF")),by="SNP") %>%
mutate(data.frame(gap::get_b_se(AlleleB_all,NrSamples,Zscore)))
write.table(cis_pQTL[c("SNP","SNPChr","SNPPos","AssessedAllele","OtherAllele","b")], sep = "\t",
row.names = FALSE, col.names = TRUE, quote=FALSE)
' | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$6,A1,A2,$1,$2,$3
}' | \
sort -k1,1 \
) | \
join - <(sed '1d' ${INF}/work/TNFB.lz | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$10,A1,A2,$1
}' | \
sort -k1,1 \
) | \
awk -vOFS="\t" '
{
if($3!=$7) $6=-$6
if($3!=$13) $12=-$12
print $1,$9,$10,$11,$3,$4,$2,$6,$12
}' | \
awk 'a[$1]++==0' | \
awk 'a[$2]++==0' | awk '$2!="NA"' > ${INF}/MS/rs1800693/LTBR.beta
join <(sed '1d' ${INF}/MS/rs1800693/EUR-v3.cma.cojo | awk '{if($12!="NA") print $2,$12,$4,$4,$2}' | sort -k1,1) \
<(Rscript -e '
INF <- Sys.getenv("INF")
suppressMessages(library(dplyr))
cis_pQTL <- merge(read.delim(file.path(INF,"work","eQTLGen.lz")) %>%
filter(GeneSymbol=="LTBR"),read.delim(file.path(INF,"work","eQTLGen.AF")),by="SNP") %>%
mutate(data.frame(gap::get_b_se(AlleleB_all,NrSamples,Zscore)))
write.table(cis_pQTL[c("SNP","SNPChr","SNPPos","AssessedAllele","OtherAllele","se")], sep = "\t",
row.names = FALSE, col.names = TRUE, quote=FALSE)
' | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$6,A1,A2,$1,$2,$3
}' | \
sort -k1,1 \
) | \
join - <(sed '1d' ${INF}/work/TNFB.lz | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$11,A1,A2,$1
}' | \
sort -k1,1 \
) | \
awk -vOFS="\t" '
{
print $1,$9,$10,$11,$3,$4,$2,$6,$12
}' | \
awk 'a[$1]++==0' | \
awk 'a[$2]++==0' | awk '$2!="NA"' > ${INF}/MS/rs1800693/LTBR.se
Rscript -e '
options(width=200)
INF <- Sys.getenv("INF")
source(file.path(INF,"rsid","LTBR.R"))
id <- c("marker","chr","pos")
traits <- c("MS","LTBR","TNFB")
b <- read.table(file.path(INF,"MS/rs1800693","LTBR.beta"),col.names=c("snpid",id,"A1","A2",traits))
markers <- b[id]
print(cor(b[traits]))
betas <- as.matrix(b[traits])
rownames(betas) <- with(b,marker)
se <- read.table(file.path(INF,"MS/rs1800693","LTBR.se"),col.names=c("snpid",id,"A1","A2",traits))
ses <- as.matrix(se[traits])
rownames(ses) <- with(se,marker)
hyprcoloc::hyprcoloc(betas, ses, trait.names=traits, snp.id=with(markers,marker))
LTBR(b[c("pos","MS","LTBR","TNFB")],se[c("pos","MS","LTBR","TNFB")],file.path(INF,"MS","rs1800693","LTBR.png"))
'
# --- rs2364485
join <(sed '1d' ${INF}/MS/rs2364485/EUR-v3.cma.cojo | awk '{if($12!="NA") print $2,$11/$12,$4,$4,$2}' | sort -k1,1) \
<(sed '1d' ${INF}/work/eQTLGen.lz | \
awk '{
chr=$3;pos=$4;A1=toupper($5);A2=toupper($6);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$7,A1,A2,$2,$3,$4
}' | \
sort -k1,1 \
) | \
join - <(sed '1d' ${INF}/work/TNFB.lz | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$10/$11,A1,A2,$1
}' | \
sort -k1,1 \
) | \
awk -vOFS="\t" '
{
if($3!=$7) $6=-$6
if($3!=$13) $12=-$12
print $1,$9,$10,$11,$3,$4,$2,$6,$12
}' | \
awk 'a[$1]++==0' | \
awk 'a[$2]++==0' | awk '$2!="NA"' > ${INF}/MS/rs2364485/LTBR.gassoc
cut -d' ' -f1 ${INF}/MS/rs2364485/LTBR.gassoc > ${INF}/MS/rs2364485/LTBR.snpid
plink --bfile ${INF}/INTERVAL/cardio/INTERVAL --extract ${INF}/MS/rs2364485/LTBR.snpid --r square --out ${INF}/MS/rs2364485/LTBR
plink --bfile ${INF}/INTERVAL/cardio/INTERVAL --extract ${INF}/MS/rs2364485/LTBR.snpid --freq --out ${INF}/MS/rs2364485/LTBR
Rscript -e '
INF <- Sys.getenv("INF")
library(gassocplot)
d <- read.table(file.path(INF,"MS","rs2364485","LTBR.gassoc"),col.names=c("snpid","marker","chr","pos","A1","A2","MS","LTBR","TNFB"))
markers <- d[c("marker","chr","pos")]
ld <- read.table(file.path(INF,"MS","rs2364485","LTBR.ld"),col.names=with(d,marker),row.names=with(d,marker))
z <- d[c("MS","LTBR","TNFB")]
rownames(z) <- with(d,marker)
sap <- stack_assoc_plot(markers, z, ld, traits = c("MS","LTBR","TNFB"), ylab = "-log10(P)", top.marker="rs1800693",legend=TRUE)
pdf(file.path(INF,"MS","rs2364485","LTBR.pdf"),width=8,height=13)
grid::grid.draw(sap)
dev.off()
'
join <(sed '1d' ${INF}/MS/rs2364485/EUR-v3.cma.cojo | awk '{if($12!="NA") print $2,$11,$4,$4,$2}' | sort -k1,1) \
<(Rscript -e '
INF <- Sys.getenv("INF")
suppressMessages(library(dplyr))
cis_pQTL <- merge(read.delim(file.path(INF,"work","eQTLGen.lz")) %>%
filter(GeneSymbol=="LTBR"),read.delim(file.path(INF,"work","eQTLGen.AF")),by="SNP") %>%
mutate(data.frame(gap::get_b_se(AlleleB_all,NrSamples,Zscore)))
write.table(cis_pQTL[c("SNP","SNPChr","SNPPos","AssessedAllele","OtherAllele","b")], sep = "\t",
row.names = FALSE, col.names = TRUE, quote=FALSE)
' | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$6,A1,A2,$1,$2,$3
}' | \
sort -k1,1 \
) | \
join - <(sed '1d' ${INF}/work/TNFB.lz | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$10,A1,A2,$1
}' | \
sort -k1,1 \
) | \
awk -vOFS="\t" '
{
if($3!=$7) $6=-$6
if($3!=$13) $12=-$12
print $1,$9,$10,$11,$3,$4,$2,$6,$12
}' | \
awk 'a[$1]++==0' | \
awk 'a[$2]++==0' | awk '$2!="NA"' > ${INF}/MS/rs2364485/LTBR.beta
join <(sed '1d' ${INF}/MS/rs2364485/EUR-v3.cma.cojo | awk '{if($12!="NA") print $2,$12,$4,$4,$2}' | sort -k1,1) \
<(Rscript -e '
INF <- Sys.getenv("INF")
suppressMessages(library(dplyr))
cis_pQTL <- merge(read.delim(file.path(INF,"work","eQTLGen.lz")) %>%
filter(GeneSymbol=="LTBR"),read.delim(file.path(INF,"work","eQTLGen.AF")),by="SNP") %>%
mutate(data.frame(gap::get_b_se(AlleleB_all,NrSamples,Zscore)))
write.table(cis_pQTL[c("SNP","SNPChr","SNPPos","AssessedAllele","OtherAllele","se")], sep = "\t",
row.names = FALSE, col.names = TRUE, quote=FALSE)
' | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$6,A1,A2,$1,$2,$3
}' | \
sort -k1,1 \
) | \
join - <(sed '1d' ${INF}/work/TNFB.lz | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,$11,A1,A2,$1
}' | \
sort -k1,1 \
) | \
awk -vOFS="\t" '
{
print $1,$9,$10,$11,$3,$4,$2,$6,$12
}' | \
awk 'a[$1]++==0' | \
awk 'a[$2]++==0' | awk '$2!="NA"' > ${INF}/MS/rs2364485/LTBR.se
Rscript -e '
options(width=200)
INF <- Sys.getenv("INF")
source(file.path(INF,"rsid","LTBR.R"))
id <- c("marker","chr","pos")
traits <- c("MS","LTBR","TNFB")
b <- read.table(file.path(INF,"MS/rs2364485","LTBR.beta"),col.names=c("snpid",id,"A1","A2",traits))
markers <- b[id]
print(cor(b[traits]))
betas <- as.matrix(b[traits])
rownames(betas) <- with(b,marker)
se <- read.table(file.path(INF,"MS/rs2364485","LTBR.se"),col.names=c("snpid",id,"A1","A2",traits))
ses <- as.matrix(se[traits])
rownames(ses) <- with(se,marker)
hyprcoloc::hyprcoloc(betas, ses, trait.names=traits, snp.id=with(markers,marker))
LTBR(b[c("pos","MS","LTBR","TNFB")],se[c("pos","MS","LTBR","TNFB")],file.path(INF,"MS","rs2364485","LTBR.png"))
'
}
function blood_cell_traits()
{
# tabix
export ext=_EUR_buildGRCh37.tsv.gz
ls ${dir}/*EUR* | xargs -I{} basename {} ${ext} | \
parallel -C' ' --env dir --env ext '
(
echo chromosome base_pair_location snpid effect_allele other_allele effect_allele_frequency beta standard_error p_value | \
tr " " "\t"
gunzip -c ${dir}/{}${ext} | \
sed "1d" | \
sort -k1,1n -k2,2n | \
awk -vOFS="\t" "{
if (\$3<\$4) snpid=\"chr\"\$1\":\"\$2\"_\"\$3\"_\"\$4;
else snpid=\"chr\"\$1\":\"\$2\"_\"\$4\"_\"\$3;
print \$1, \$2, snpid, \$3, \$4, \$5, \$6, \$7, \$8
}"
) | \
bgzip -f > ${dir}/tsv/EUR-{}.gz
tabix -f -S1 -s1 -b2 -e2 ${dir}/tsv/EUR-{}.gz
'
# LocusZoom
module load python/2.7
ls ${dir}/tsv/*gz | xargs -I{} basename {} .gz | \
grep -e wbc -e mono -e neut -e lymph -e eo -e baso | \
parallel -C' ' --env INF --env dir --env region --env chr --env start --env end '
(
echo snpid rsid chromosome base_pair_location effect_allele other_allele effect_allele_frequency beta standard_error p_value
join -23 ${INF}/work/INTERVAL.rsid <(tabix ${dir}/tsv/{}.gz ${region}) | \
sort -k3,3n -k4,4n
) | \
awk "!index(\$2,\":\")" | \
tr " " "\t" > ${INF}/MS/{}.lz
rm -rf ld_cache.db
locuszoom --source 1000G_Nov2014 --build hg19 --pop EUR --metal ${INF}/MS/{}.lz --delim tab title="{}" \
--markercol rsid --pvalcol p_value --chr ${chr} --start ${start} --end ${end} \
--no-date --plotonly --prefix="{}" --rundir .
qpdf {}_chr${chr}_${start}-${end}.pdf --pages . 1 -- {}-lz.pdf
'
qpdf --empty --pages *lz.pdf -- blood-cell-traits.pdf
# MA
echo wbc mono neut lymph eo baso | \
tr ' ' '\n' | \
parallel -C' ' --env INF '
(
echo SNP A1 A2 ref b se p N
awk -vN=562132 "NR>1 && \$10 <= 5e-8 {print \$1,\$5,\$6,\$7,\$8,\$9,\$10,N}" ${INF}/MS/EUR-{}.lz
) > ${INF}/MS/EUR-{}.ma
sed "1d" ${INF}/MS/EUR-{}.lz | \
cut -f1 > ${INF}/MS/EUR-{}.snpid
sed "1d" ${INF}/MS/EUR-{}.lz | \
sort -k10,10g | \
awk "NR==1{print \$1}" > ${INF}/MS/EUR-{}.top
'
}
function cojo()
{
# MS
(
echo SNP A1 A2 freq b se p N
awk 'NR>1{print $1,$5,$6,$7,$8,$9}' ${INF}/MS/v3.lz | \
join - <(awk 'NR>1{print $2,$3,$4,$5}' ${INF}/work/LTBR.frq) | \
awk -v N=115803 '
{
if($1==$7) freq=$9; else freq=1-$9
print $1,$2,$3,freq,$4,$5,$6,N
}'
) > ${INF}/MS/EUR-v3.ma
sed "1d" ${INF}/MS/EUR-v3.ma | \
cut -d" " -f1 > ${INF}/MS/EUR-v3.snpid
sed "1d" ${INF}/MS/EUR-v3.ma | \
sort -k7,7g | \
awk "NR==1{print \$1}" > ${INF}/MS/EUR-v3.top
# pruning
module load plink/2.00-alpha
for cell in v3 # wbc mono neut lymph eo baso
do
plink2 --bfile ${INF}/INTERVAL/cardio/INTERVAL --chr 12 --extract ${INF}/MS/EUR-${cell}.snpid \
--geno 0.1 --mind 0.1 --maf 0.005 --indep-pairwise 1000kb 1 0.1 --out ${INF}/MS/EUR-${cell}
if [ $(grep -w -f ${INF}/MS/EUR-${cell}.top ${INF}/MS/EUR-${cell}.prune.in | wc -l) -eq 0 ]; then
export top=$(cat ${INF}/MS/EUR-${cell}.top)
export i=$(grep -w -f ${INF}/MS/EUR-${cell}.prune.in ${INF}/INTERVAL/cardio/INTERVAL.bim | \
awk 'function abs(x) {if (x<0) return -x; else return x;} {
top=ENVIRON["top"];split(top,a,":");split(a[2],b,"_")
d=abs($4-b[1]);
print $1, $2, $4, d}' | \
sort -r -k4,4n | \
awk 'NR==1 {print $2}' \
)
sed -i 's/'"$i"'/'"$top"'/g' ${INF}/MS/EUR-${cell}.prune.in
fi
sort ${INF}/MS/EUR-${cell}.prune.in > ${INF}/MS/EUR-${cell}.prune
export P_threshold=1e-5
# drop the --extract option with v3
if [ "${cell}" == "v3" ]; then
gcta-1.9 --bfile ${INF}/INTERVAL/cardio/INTERVAL --chr 12 \
--cojo-file ${INF}/MS/EUR-${cell}.ma \
--cojo-slct \
--cojo-p ${P_threshold} \
--maf 0.005 \
--cojo-collinear 0.9 \
--out ${INF}/MS/EUR-${cell}
echo chr12:6440009_C_T ${INF}/MS/rs1800693/EUR-v3.top
gcta-1.9 --bfile ${INF}/INTERVAL/cardio/INTERVAL --chr 12 \
--cojo-file ${INF}/MS/EUR-${cell}.ma \
--cojo-cond ${INF}/MS/rs1800693/EUR-${cell}.top \
--cojo-p ${P_threshold} \
--maf 0.005 \
--cojo-collinear 0.9 \
--out ${INF}/MS/rs1800693/EUR-${cell}
echo chr12:6514963_A_C ${INF}/MS/rs2354485/EUR-v3.top
gcta-1.9 --bfile ${INF}/INTERVAL/cardio/INTERVAL --chr 12\
--cojo-file ${INF}/MS/EUR-${cell}.ma \
--cojo-cond ${INF}/MS/rs2364485/EUR-${cell}.top \
--cojo-p ${P_threshold} \
--maf 0.005 \
--cojo-collinear 0.9 \
--out ${INF}/MS/rs2364485/EUR-${cell}
else
gcta-1.9 --bfile ${INF}/INTERVAL/cardio/INTERVAL --chr 12 \
--cojo-file ${INF}/MS/EUR-${cell}.ma \
--extract ${INF}/MS/EUR-${cell}.prune \
--cojo-slct \
--cojo-p ${P_threshold} \
--maf 0.005 \
--cojo-collinear 0.9 \
--out ${INF}/MS/EUR-${cell}
gcta-1.9 --bfile ${INF}/INTERVAL/cardio/INTERVAL \
--cojo-file ${INF}/MS/EUR-${cell}.ma \
--extract ${INF}/MS/EUR-${cell}.prune \
--cojo-cond ${INF}/MS/EUR-${cell}.top \
--cojo-p ${P_threshold} \
--maf 0.005 \
--cojo-collinear 0.9 \
--out ${INF}/MS/EUR-${cell}
fi
done
}
function coloc()
{
join <(sed '1d' ${INF}/MS/EUR-v3.ma | awk '{print $1,$2,$3,$4,$5,$6,$8}' | sort -k1,1) \
<(Rscript -e '
suppressMessages(library(dplyr))
cis_pQTL <- merge(read.delim("eQTLGen.lz") %>% filter(GeneSymbol=="LTBR"),read.delim("eQTLGen.AF"),by="SNP") %>%
mutate(data.frame(gap::get_b_se(AlleleB_all,NrSamples,Zscore)))
write.table(cis_pQTL[c("SNP","SNPChr","SNPPos","AssessedAllele","OtherAllele","AlleleB_all","b","se","NrSamples")], sep = "\t",
row.names = FALSE, col.names = TRUE, quote=FALSE)
' | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,A1,A2,$6,$7,$8,$9
}' | \
sort -k1,1 \
) | \
join - <(sed '1d' ${INF}/work/TNFB.lz | \
awk '{
chr=$2;pos=$3;A1=toupper($4);A2=toupper($5);
if(A1<A2) snpid="chr"chr":"pos"_"A1"_"A2;else snpid="chr"chr":"pos"_"A2"_"A1;
print snpid,A1,A2,$6,$10,$11,$18,$1
}' | \
sort -k1,1 \
) | \
awk -vOFS="\t" '
{
if($2!=$8) $11=-$11
if($2!=$14) $17=-$17
print
}' | \
awk 'a[$1]++==0' | \
awk 'a[$20]++==0' > ${INF}/work/LTBR.coloc
Rscript -e '
options(width=200)
INF <- Sys.getenv("INF")
MS_LTBR_TNFB <- read.table(file.path(INF,"work","LTBR.coloc"))
MS <- MS_LTBR_TNFB[c(1:7,20)]
LTBR <- MS_LTBR_TNFB[c(1,8:13,20)]
TNFB <- MS_LTBR_TNFB[c(1,14:20)]
names(MS) <- names(LTBR) <- names(TNFB) <- c("snpid","A1","A2","MAF","beta","se","N","rsid")
beta_se <- function()
# unreasonable results from here:
{
MS <- within(MS,{sdY=gap::get_sdy(MAF,N,beta,se);varbeta=se^2})
LTBR <- within(LTBR,{sdY=gap::get_sdy(MAF,N,beta,se);varbeta=se^2})
TNFB <- within(TNFB,{sdY=gap::get_sdy(MAF,N,beta,se);varbeta=se^2})
}
# the following is inline with above:
{
MS <- within(MS,{pvalues=as.numeric(gap::pvalue(beta/se))})
LTBR <- within(LTBR,{pvalues=as.numeric(gap::pvalue(beta/se))})
TNFB <- within(TNFB,{pvalues=as.numeric(gap::pvalue(beta/se))})
}
require(coloc)
MS <- c(as.list(MS),type="quant")
LTBR <- c(as.list(LTBR),type="quant")
TNFB <- c(as.list(TNFB),type="quant")
abf12 <- coloc.abf(MS,LTBR)
abf13 <- coloc.abf(MS,TNFB)
abf23 <- coloc.abf(LTBR,TNFB)
'
}
function gsmr()
{
# MS
cat ${INF}/MS/EUR-v3.ma | gzip -f > ${INF}/MS/gsmr_MS.ma.gz
# prot
(
echo SNP A1 A2 freq b se p N
zcat ${INF}/METAL/${prot}-1.tbl.gz | \
awk -vchr=${chr} -vstart=${start} -vend=${end} 'NR>1 && $1==chr && $2 >= start && $2 < end {print $3,toupper($4),toupper($5),$6,$10,$11,10^$12,$18}'
) | gzip -f > ${INF}/MS/gsmr_${prot}.ma.gz
# control files
if [ ! -f ${INF}/MS/gsmr_ref_data ]; then echo ${INF}/work/INTERVAL > ${INF}/MS/gsmr_ref_data; fi
if [ ! -f ${INF}/MS/gsmr_MS ]; then echo MS ${INF}/MS/gsmr_MS.ma.gz > ${INF}/MS/gsmr_MS; fi
if [ ! -f ${INF}/MS/gsmr_${prot} ]; then echo ${prot} ${INF}/MS/gsmr_${prot}.ma.gz > ${INF}/MS/gsmr_${prot}; fi
gcta-1.9 --mbfile ${INF}/MS/gsmr_ref_data --gsmr-file ${INF}/MS/gsmr_MS ${INF}/MS/gsmr_${prot} \
--gsmr-direction 0 \
--clump-r2 0.05 --gwas-thresh 1e-5 --diff-freq 0.4 --heidi-thresh 0.05 --gsmr-snp-min 10 --effect-plot \
--out ${INF}/MS/gsmr_${prot}-MS
Rscript -e '
prot <- Sys.getenv("prot")
source("http://cnsgenomics.com/software/gcta/res/gsmr_plot.r")
gsmr_data <- read_gsmr_data(paste0("MS/gsmr_",prot,"-MS.eff_plot.gz"))
gsmr_summary(gsmr_data)
pdf(paste0("MS/gsmr_",prot,"-MS.eff_plot.pdf"))
plot_gsmr_effect(gsmr_data, prot, "MS", colors()[75])
dev.off()
'
}
function mr()
{
# all significant SNPs
(
zcat ${INF}/METAL/${prot}-1.tbl.gz | head -1
zcat ${INF}/METAL/${prot}-1.tbl.gz | \
awk 'NR>1 && length($4)==1 && length($5)==1 && $12<=-5' | \
sort -k1,1n -k2,2n | \
gzip -f > ${INF}/MS/${prot}.p.gz
# HLA
zcat ${INF}/MS/${prot}.p.gz | \
awk 'NR>1 && !($1 == 6 && $2 >= 25392021 && $2 < 33392022)'
zcat ${INF}/MS/${prot}.p.gz | \
awk '$1 == 6 && $2 >= 25392021 && $2 < 33392022' | \
sort -k12,12g | \
awk 'NR==1'
) > ${INF}/MS/${prot}.p
export lines=$(expr $(wc -l ${INF}/MS/${prot}.p | cut -d' ' -f1) - 1)
if [ $lines -eq 0 ]; then rm ${INF}/MS/${prot}.p; fi
(
awk -vprot=${prot} -vOFS="\t" 'BEGIN{print prot, "rsid", "chr", "pos", "beta", "se", "snpid", "A1", "A2", "EAF", "P", "N"}'
awk -vFS="\t" '{split($3,a,"_"); print a[1],$1,$2,$10,$11,$3,toupper($4),toupper($5),$6,-$12,$18}' ${INF}/MS/${prot}.p | \
sort -k1,1 | \
join -12 -21 ${INF}/work/snp_pos - | \
awk 'a[$7]++==0' | \
awk -vprot=${prot} -vOFS="\t" '{print prot, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12}' | \
sort -k3,3n -k4,4n
) > ${INF}/MS/${prot}-pQTL.dat
Rscript -e '
options(echo=FALSE,width=200)
INF <- Sys.getenv("INF")
prot <- Sys.getenv("prot")
rsid <- Sys.getenv("rsid")
pQTL <- file.path(INF,"MS",paste0(prot,"-pQTL.dat"))
library(TwoSampleMR)
x <- read_exposure_data(pQTL,
clump = FALSE,
sep = "\t",
phenotype_col = "TNFB",
snp_col = "rsid",
beta_col = "beta",
se_col = "se",
eaf_col = "EAF",
effect_allele_col = "A1",
other_allele_col = "A2",
pval_col = "P",
samplesize_col = "N",
id_col = "rsid",
log_pval = TRUE)
pdf(file.path(INF,"MS","MS-forestplot.pdf"))
for(id in c("ieu-b-18","ukb-b-17670","finn-a-G6_MS"))
{
cat("--",pQTL,"-",id,"--\n")
y <- extract_outcome_data(with(x,SNP), id, proxies = TRUE, rsq = 0.8)
xy <- mr(harmonise_data(x, y))
forest_plot(xy)
print(xy)
}
dev.off()
'
# alternative form
export start=$(expr ${pos} - ${M})
export end=$(expr ${pos} + ${M})
if [ ${get_data} == "yes" ]; then
(
awk -vprot=${prot} -vOFS="\t" 'BEGIN{print prot, "rsid", "chr", "pos", "beta", "se", "snpid", "A1", "A2", "EAF", "P", "N"}'
gunzip -c ${INF}/METAL/${prot}-1.tbl.gz | \
awk -vFS="\t" -vchr=${chr} -vstart=${start} -vend=${end} '(NR>1 && $1 == chr && $2 >= start && $2 <= end) {
split($3,a,"_"); print a[1],$1,$2,$10,$11,$3,toupper($4),toupper($5),$6,-$12,$18
}' | \
sort -k1,1 | \
join -12 -21 ${INF}/work/snp_pos - | \
awk 'a[$7]++==0' | \
awk -vprot=${prot} -vOFS="\t" '{print prot, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12}' | \
sort -k3,3n -k4,4n
) > ${INF}/MS/${prot}-pQTL-${rsid}.dat
fi
Rscript -e '
options(echo=FALSE,width=200)
INF <- Sys.getenv("INF")
prot <- Sys.getenv("prot")
rsid <- Sys.getenv("rsid")
pQTL <- file.path(INF,"MS",paste0(prot,"-pQTL-",rsid,".dat"))
library(TwoSampleMR)
x <- subset(read_exposure_data(pQTL,
clump = TRUE,
sep = "\t",
phenotype_col = "TNFB",
snp_col = "rsid",
beta_col = "beta",
se_col = "se",
eaf_col = "EAF",
effect_allele_col = "A1",
other_allele_col = "A2",
pval_col = "P",
samplesize_col = "N",
id_col = "rsid",
log_pval = TRUE), pval<1e-5)
for(id in c("ieu-b-18","ukb-b-17670","finn-a-G6_MS"))
{
cat("##",pQTL,"-",id,"##\n")
y <- extract_outcome_data(with(x,SNP), id, proxies = TRUE, rsq = 0.8)
xy <- mr(harmonise_data(x, y))
print(xy)
}
'
# pQTLs
(
awk -vOFS="\t" 'BEGIN{print "Phenotype", "SNP", "chr", "pos", "beta", "se", "snpid", "effect_allele", "other_allele", "eaf", "pval", "N"}'
zgrep -e chr12:6514963_A_C -e chr12:6440009_C_T -e chr6:31540757_A_C -e chr6:31073047_A_G ${INF}/METAL/${prot}-1.tbl.gz | \
awk -vFS="\t" '{split($3,a,"_"); print a[1],$1,$2,$10,$11,$3,toupper($4),toupper($5),$6,10^$12,$18}' | \
sort -k1,1 | \
join -12 -21 ${INF}/work/snp_pos - | \
awk 'a[$7]++==0' | \
awk -vprot=${prot} -vOFS="\t" '{print prot, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12}' | \
sort -k3,3n -k4,4n
) > ${INF}/MS/${prot}-pQTL.dat
Rscript -e '
library(pQTLtools)
INF <- Sys.getenv("INF")
prot <- Sys.getenv("prot")
pqtl <- file.path(INF,"MS",paste0(prot,"-pQTL.dat"))
ivs <- read.table(pqtl,as.is=TRUE,header=TRUE)
setwd(file.path(INF,"MS"))
for(id in c("ieu-b-18","ukb-b-17670","finn-a-G6_MS"))
{
pqtlMR(subset(ivs,SNP%in%"rs2364485"),id,prefix=paste0(prot,"-",id,"-rs2364485"))
pqtlMR(subset(ivs,SNP%in%"rs1800693"),id,prefix=paste0(prot,"-",id,"-rs1800693"))
pqtlMR(subset(ivs,SNP%in%"rs9263621"),id,prefix=paste0(prot,"-",id,"-rs9263621"))
pqtlMR(subset(ivs,SNP%in%"rs2229092"),id,prefix=paste0(prot,"-",id,"-rs2229092"))
}
'
# +/- 0.5Mb
(
# cis pQTLs
# chr6:31540757_A_C rs2229092
# chr6:31073047_A_G rs9263621
export chr=6
export rsid=rs2229092
export pos=31540757
export rsid=rs9263621
export pos=31073047
# trans pQTL
# chr12:6514963_A_C rs2364485
# chr12:111865049_C_G rs7310615; MS:chr12:6440009_C_T rs1800693
# r2(rs1800693,rs2364485)=0.0029, r2(rs1800693,rs7310615)=0.0023, r2(rs2364485,rs7310615)=0.0013
export chr=12
export rsid=rs2364485
export pos=6514963
export rsid=rs1800693
export pos=6440009
export rsid=rs7310615
export pos=111865049
) >> ${INF}/MS/${prot}-MS-MR.log
}
# --- legacy ---
function info()
{
Rscript -e '
# https://gwas.mrcieu.ac.uk/datasets/?trait__icontains=multiple%20sclerosis
ms_ids <- c("ukb-b-17670","ieu-b-18","ieu-a-1025","ebi-a-GCST005531","ieu-a-1024","ebi-a-GCST001198","ieu-a-820","ieu-a-821","finn-a-G6_MS")
ms_gwasinfo <- ieugwasr::gwasinfo(ms_ids)
cols <- c("id","year","ncase","ncontrol","nsnp","build","author","pmid","population")
ms_gwasinfo[cols]
# id year ncase ncontrol nsnp build author pmid population
# 1 ukb-b-17670 2018 1679 461254 9851867 HG19/GRCh37 Ben Elsworth NA European
# 2 ieu-b-18 2019 47429 68374 6304359 HG19/GRCh37 Patsopoulos NA 31604244 European
# 3 ieu-a-1025 2013 14498 24091 156632 HG19/GRCh37 Beecham 24076602 European
# 4 ebi-a-GCST005531 2013 14498 24091 132089 HG19/GRCh37 Beecham AH 24076602 European
# 5 ieu-a-1024 2011 9722 17376 465435 HG19/GRCh37 Sawcer S 21833088 European
# 6 ebi-a-GCST001198 2011 9772 16849 463040 HG19/GRCh37 Sawcer S 21833088 European
# 7 ieu-a-820 2007 931 1862 327095 HG19/GRCh37 Hafler DA 17660530 European
# 8 ieu-a-821 2009 978 883 514572 HG19/GRCh37 Baranzini SE 19010793 European
# 9 finn-a-G6_MS 2020 378 44677 16152119 HG19/GRCh37 NA NA European
# 2,9 with no VCF
# https://gwas.mrcieu.ac.uk/files/ieu-a-1025/ieu-a-1025.vcf.gz
mr_info <- as.data.frame(epigraphdb::mr(outcome_trait = "Multiple sclerosis", pval_threshold = 1e-8))
select <- c("ukb-a-100","ukb-a-104","ieu-a-294","ieu-a-295","ieu-a-971")
subset(mr_info,exposure.id%in%select,select=-c(outcome.trait,mr.selection,mr.method,mr.moescore))
# exposure.id exposure.trait outcome.id mr.b mr.se mr.pval
# ieu-a-971 Ulcerative colitis ieu-a-1025 -0.4839367 0.06111146 2.395840e-15
# ieu-a-295 Inflammatory bowel disease ieu-a-1025 -0.2593652 0.03799687 8.733802e-12
# ieu-a-294 Inflammatory bowel disease ieu-a-1025 0.1232999 0.01697710 1.375433e-10
# ukb-a-104 Non-cancer illness code self-reported: ulcerative colitis ieu-a-1025 20.4956226 3.23034883 2.228468e-10
# ukb-a-100 Non-cancer illness code self-reported: psoriasis ieu-a-1025 11.0119963 1.86965907 3.865659e-09
mr_summary <- epigraphdb::mr(outcome_trait="Multiple sclerosis")
names(mr_summary)[c(2,3,5,6,7)] <- c("exposure","outcome","b","se","pval")
tryx::volcano_plot(mr_summary)
'
}
function ieu_id_ma()
# ieu-b-18
(
echo SNP A1 A2 freq b se p N
bcftools query -f "%CHROM %POS %ALT %REF %AF [%ES] [%SE] [%LP] [%SS]\n" -r ${region} \
~/rds/results/public/gwas/multiple_sclerosis/${ieu_id}.vcf.gz | \
awk '{if ($3<$4) snpid="chr"$1":"$2"_"$3"_"$4;else snpid="chr"$1":"$2"_"$4"_"$3;print snpid, $3, $4, $5, $6, $7, $8, $9}' | \
awk 'a[$1]++==0 && $8<5 {$7=10^-$7};1'
) > ${INF}/MS/EUR-${ieu_id}.ma
function misc()
{
export start=6400000
export end=6520000
export M=0
echo Multiple sclerosis
# rs1800693 chr12:6440009
# rs2364485 chr12:6514963
# 2018, the rsid is incomplete
zgrep -w -e 6440009 -e 6514963 ${v3}
# 12 6440009 rs1800693 T C 14 1.017e-13 0.8808
# 12 6514963 rs2364485 C A 15 5.778e-06 0.9041
gunzip -c ${v3} | \
sed '1d' | \
awk '!/^$/' | \
awk -vchr=${chr} -vstart=${start} -vend=${end} -vM=${M} -vOFS="\t" '
{
if ($1==chr && $2>=start-M && $2<=end+M)
{
if ($4<$5) snpid="chr" $1 ":" $2 "_" $4 "_" $5;
else snpid="chr" $1 ":" $2 "_" $5 "_" $4
print "chr"$1":"$2,$1,$2,$7,snpid,$4,$5
}
}' | \
sort -k1,1 | \
join -12 -21 ${INF}/work/snp_pos - | \
awk -vOFS="\t" '{print $2,$3,$4,$5,$6,$7,$8}' > ${INF}/work/${prot}-QTL.lz
echo 2013
zgrep -w -e rs1800693 -e rs2364485 data/imsgc_2013_24076602_ms_efo0003885_1_ichip.sumstats.tsv.gz
zgrep -v -w -e NaN -e Infinity -e 0.0 data/imsgc_2013_24076602_ms_efo0003885_1_ichip.sumstats.tsv.gz | \
sed '1d' | \
awk '!/^$/' | \
awk -vchr=${chr} -vstart=${start} -vend=${end} -vM=${M} -vOFS="\t" '
{
if ($1==chr && $2>=start-M && $2<=end+M)
{
if ($4<$5) snpid="chr" $1 ":" $2 "_" $4 "_" $5;
else snpid="chr" $1 ":" $2 "_" $5 "_" $4
print $3,$1,$2,$7/$8,snpid,$5,$4
}
}' > work/${prot}-2013.lz
echo SCALLOP/INF
# chr12:6514963_A_C
gunzip -c ${INF}/METAL/${prot}-1.tbl.gz | \
sed '1d' | \
awk -vFS="\t" -vchr=${chr} -vstart=${start} -vend=${end} -vM=${M} '
{
if ($1 == chr && $2 >= start-M && $2 <= end+M)
{
split($3,a,"_")
print a[1],$1,$2,$10/$11,$3,toupper($4),toupper($5)
}
}' | \
sort -k1,1 | \
join -12 -21 ${INF}/work/snp_pos - | \
awk 'a[$6]++==0' | \
awk -vOFS="\t" '{print $2, $3, $4, $5, $6, $7, $8}' > ${INF}/work/${prot}-pQTL.lz
cut -f5 ${INF}/work/${prot}-pQTL.lz > ${INF}/work/${prot}-pQTL.snpid
plink --bfile ${INF}/INTERVAL/cardio/INTERVAL --extract work/${prot}-pQTL.snpid \
--r2 inter-chr yes-really --ld-snps chr12:6514963_A_C --ld-window-r2 0 --out ${INF}/work/${prot}-pQTL
(
awk -vOFS="\t" 'BEGIN{print "snpid","rsid","chr","pos","z","A1","A2","r2"}'
join -15 -t$'\t' ${INF}/work/${prot}-pQTL.lz <(awk -vOFS="\t" 'NR>1 {print $6,$7}' ${INF}/work/${prot}-pQTL.ld | sort -k1,1)
) > ${INF}/work/${prot}-pQTL.txt
echo eQTLGen
zgrep -w ${gene} ~/rds/public_databases/eQTLGen/2019-12-11-cis-eQTLsFDR-ProbeLevel-CohortInfoRemoved-BonferroniAdded.txt.gz | \
awk -vchr=${chr} -vstart=${start} -vend=${end} -vM=${M} -vOFS="\t" '
{
if ($5<$6) snpid="chr" $3 ":" $4 "_" $5 "_" $6;
else snpid="chr" $3 ":" $4 "_" $6 "_" $5
if($3==chr && $4>=start-M && $4 <=end+M) print $2,$3,$4,$7,snpid,$5,$6
}' > ${INF}/work/${prot}-eQTL.lz
join -a2 -e "NA" -o2.5,2.1,2.2,2.3,1.4,1.6,1.7,2.1,2.2,2.3,2.4,2.6,2.7 \
-j5 <(sort -k5,5 ${INF}/work/${prot}-QTL.lz) <(sort -k5,5 ${INF}/work/${prot}-pQTL.lz) | \
join -a1 -e "NA" -25 -o1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.10,1.11,1.12,1.13,2.1,2.2,2.3,2.4,2.6,2.7 \
- <(sort -k5,5 ${INF}/work/${prot}-eQTL.lz) | \
awk -vOFS="\t" '
{
# 2013 with beta/se
# if($6!="NA" && $12!="NA" && $11!="NA" && $6!=12) $11=-$11
# if($6!="NA" && $18!="NA" && $17!="NA" && $6!=18) $17=-$17
print $1,$2,$3,$4,$5,$11,$17
}' | \
awk 'a[$1]++==0' | \
awk 'a[$2]++==0' | \
sort -k3,3n -k4,4n > ${INF}/work/${prot}.z
cut -f1 ${INF}/work/${prot}.z > ${INF}/work/${prot}.snpid
plink --bfile ${INF}/INTERVAL/cardio/INTERVAL --extract ${INF}/work/${prot}.snpid --make-bed --out ${INF}/work/${prot}
cut -f2 ${INF}/work/${prot}.bim > ${INF}/work/${prot}.snpid
plink --bfile work/${prot} --extract ${INF}/work/${prot}.snpid --r square --out ${INF}/work/${prot}
grep -f ${INF}/work/${prot}.snpid ${INF}/work/${prot}.z > work/${prot}.gassoc
Rscript -e '