forked from StatGenPRD/STOPGAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTOPGAP2_functions.R
2126 lines (1723 loc) · 99.4 KB
/
STOPGAP2_functions.R
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
#--------STOPGAP2 R functions ----------------#
# Author: Judong Shen and KIjoung Song
# Trim white spaces
trimWhiteSpace <- function (x) {
sub("[ \t\n\r]*$", "", sub("^[ \t\n\r]*", "", x))
}
sbind = function(x, y, fill=NA) {
sbind.fill = function(d, cols){
for(c in cols)
d[[c]] = fill
d
}
x = sbind.fill(x, setdiff(names(y),names(x)))
y = sbind.fill(y, setdiff(names(x),names(y)))
rbind(x, y)
}
# Download GraspFullDataset2.zip data
# url <- c("https://s3.amazonaws.com/NHLBI_Public/GRASP/GraspFullDataset2.zip")
# file <- c("GraspFullDataset2.zip")
# download.file(url, file)
# grasp.import: use unix commands/R scripts to filter the grasp data
# Assume the input data is a *.zip file originally downloaded from the GRASP website.
grasp.import <- function(file = "./Data/GraspFullDataset2.zip", p.thres=1E-04)
{
cat(paste("Start processing the ", file, " data ...", sep=""), "\n")
system(paste("zcat", file, "| head -n 1 > header.txt", sep=" "))
vars <- names(read.delim("header.txt",
header = TRUE, comment.char = "", sep = "\t", as.is = TRUE))
vars.keep <- c("SNPid.dbSNP134.", "PMID", "Pvalue",
"Phenotype","Initial.Sample.Description", "Replication.Sample.Description")
# Check whether all the variables we want to keep are in this version of GRASP data
stopifnot(all(vars.keep %in% vars))
if (!all(vars.keep %in% vars)){
cat("Not all variables we want to keep are in this version of GRASP data, please double check before running!", "\n")
}
no <- match(vars.keep, vars)
no <- paste(no, collapse =",")
p.no <- match("Pvalue", vars.keep)
system(paste("zcat ", file, " | cut -f", no, " > tmp.txt", sep=""))
system(paste("awk '$", p.no, "<=", p.thres,"' tmp.txt > grasp.txt", sep=""))
system(paste("cut -f", no, " header.txt > header1.txt", sep=""))
system("cat header1.txt grasp.txt > grasp_analysis.txt")
# Clean up
unlink(c("header.txt","header1.txt", "tmp.txt", "grasp.txt"))
# Read in the data
grasp <- read.delim("grasp_analysis.txt",
na.strings = c("", "NA"), strip.white = TRUE)
if(any(grep("^Gene expression", grasp$Phenotype))) grasp <- grasp[-grep("^Gene expression", grasp$Phenotype),]
#ignore warning() because it was occurred by '\x'(UTF-8 mode).
grasp$PMID <- trimWhiteSpace(grasp$PMID)
save(grasp, file = "grasp.RData", compress = TRUE)
invisible(grasp)
}
# NHGRI.import: filter the NHGRI data
NHGRI.import <- function(file = "./Data/gwas_catalog_v1.0-associations_e85_r2016-08-01.tsv", p.thres=1E-04)
{
cat(paste("Start processing the NHGRI ", file, " data ...", sep=""), "\n")
NHGRI <- read.delim(file,
header = TRUE, sep = "\t", as.is = TRUE)
# NHGRI <- NHGRI[-grep("^[a-z | A-Z]", NHGRI$P.VALUE), ]
NHGRI <- NHGRI[!is.na(NHGRI$P.VALUE),]
NHGRI$P.VALUE <- as.numeric(NHGRI$P.VALUE)
NHGRI <- subset(NHGRI, P.VALUE <= p.thres)
vars.keep <- c("PUBMEDID","SNPS", "P.VALUE", "DISEASE.TRAIT", "INITIAL.SAMPLE.SIZE", "REPLICATION.SAMPLE.SIZE")
NHGRI <- NHGRI[,vars.keep]
names(NHGRI)[names(NHGRI)=="INITIAL.SAMPLE.SIZE"] <- "Initial.Sample.Size"
names(NHGRI)[names(NHGRI)=="REPLICATION.SAMPLE.SIZE"] <- "Replication.Sample.Size"
# Remove the rows with haplotypes (multiple SNPs) instead of single SNPs
nhgri <- subset(NHGRI, !grepl(",", SNPS))
nhgri <- subset(NHGRI, !grepl("x", SNPS))
save(nhgri, file = "nhgri.RData", compress = TRUE)
invisible(nhgri)
}
# gwasdb.import: filter the gwasdb data
gwasdb.import <- function(file = "./Data/gwasdb_20150819_snp_trait.gz", p.thres=1E-04)
{
cat(paste("Start processing the gwasdb data ", file, " ...", sep=""), "\n")
system(paste("zcat", file, "| head -n 1 > header.txt", sep=" "))
vars <- names(read.delim("header.txt",
header = TRUE, comment.char = "", sep = "\t", as.is = TRUE))
vars <- vars[-4]
gwasdb <- read.delim(gzfile(file),
header = FALSE, sep = "\t", as.is = TRUE, skip=1)
colnames(gwasdb) <- vars
#gwasdb <- gwasdb[-grep("^[a-z | A-Z]", gwasdb$P_VALUE), ]
gwasdb <- gwasdb[!is.na(gwasdb$P_VALUE),]
gwasdb$P_VALUE <- as.numeric(gwasdb$P_VALUE)
gwasdb <- subset(gwasdb, P_VALUE <= p.thres)
if(any(grep("^Gene expression", gwasdb$GWAS_TRAIT))) gwasdb <- gwasdb[-grep("^Gene expression", gwasdb$GWAS_TRAIT),]
vars.keep <- c("PMID","SNPID.dbSNP","ORI_SNPID", "P_VALUE", "GWAS_TRAIT", "GWAS_INITIAL_SAMPLE_SIZE","SOURCE")
gwasdb <- gwasdb[,vars.keep]
gwasdb$SNPID.dbSNP <- trimWhiteSpace(gwasdb$SNPID.dbSNP)
gwasdb$ORI_SNP <- trimWhiteSpace(gwasdb$ORI_SNPID)
gwasdb$PMID <- trimWhiteSpace(gwasdb$PMID)
save(gwasdb, file = "gwasdb.RData", compress = TRUE)
invisible(gwasdb)
}
# phewas.import: use unix commands/R scripts to filter the phewas data
# Assume the input data is a *.csv file originally downloaded from the PheWAS website.
phewas.import <- function(file = "./Data/phewas-catalog.csv", p.thres=1E-04)
{
cat(paste("Start processing the ", file, " data ...", sep=""), "\n")
phewas <- read.csv(file,header = TRUE)
# phewas <- phewas[-grep("^[a-z | A-Z]", phewas$p.value), ]
phewas <- phewas[!is.na(phewas$p.value),]
phewas$p.value <- as.numeric(phewas$p.value)
phewas <- subset(phewas, p.value <= p.thres)
vars.keep <- c("snp","phewas.phenotype","cases", "p.value","phewas.code")
phewas <- phewas[,vars.keep]
phewas$snp <- trimWhiteSpace(phewas$snp)
phewas$phewas.phenotype <- trimWhiteSpace(phewas$phewas.phenotype)
save(phewas, file = "phewas.RData", compress = TRUE)
invisible(phewas)
}
# venn.plot: venn diagram plot
venn.plot <- function(data.plot,file.name){
# png(file=file.name,width=10.5,height=10.5,units="in",res=300)
pdf(file=file.name, height = 8, width = 8)
par(mar=c(1,1,1,1))
venn(data.plot)
dev.off()
}
#binding two data frames that don't have the same set of columns
sbind = function(x, y, fill=NA) {
sbind.fill = function(d, cols){
for(c in cols)
d[[c]] = fill
d
}
x = sbind.fill(x, setdiff(names(y),names(x)))
y = sbind.fill(y, setdiff(names(x),names(y)))
rbind(x, y)
}
# gwas.filter: filter the gwas data based on disease names and PUBMEDID
gwas.filter <- function(data){
# Remove the gwas data with some disease names that will not be used
no <- grep("methylation levels", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("differential exon", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("differential splicing", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("transcript initiation", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("transcript termination", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("gene expression", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("methylation qtl", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("qtl", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("exon skipping", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("differential", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("desialylated glycan peak", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("complex transcript isoform variation", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("recombination", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("expression", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("intron retention", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("transcript levels", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("allele-specific methylation", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("serum metabolite", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
no <- grep("cell lines", trimWhiteSpace(data$disease))
if (length(no)>0){
data <- data[-no,]
}
x=c("21886157", # Serum ratio of ...
"18403759", # YKL-40 levels
"21572414", # "Urinary metabolites"
"20037589", # arg-ptc
"22341974", # craniofacial morphology variation
"22595970", # protein abundance levels
"19043545",
"19798445",
"22286219",
"23382691", "23281178","22286219", "17903293", "19043545", "17554300", "19862010","21150878","22190364","22377632")
# Further remove records in some publications
data <- subset(data, !(as.character(PUBMEDID) %in% x))
# write.table(data,
# "stopgap_grasp_nhgri_gwasdb_merged_filtered.txt",
# sep = "\t", row.names = FALSE, quote = F, na = "")
# save(data, file = "stopgap_4sources_clean.RData", compress = TRUE)
data
}
# data.merge: merge the grasp, nhgri and gwasdb datasets by first using nhgri records, supplementing
# by grasp and gwasdb datasets respectively
# Merge order: NHGRI > GRASP > GWASDB > phewas
data.merge <- function(nhgri, grasp, gwasdb, phewas){
cat("Start merging the data from five sources: nhgri, grasp, gwasdb, and PheWAS ...", "\n")
load("nhgri.RData")
load("grasp.RData")
load("gwasdb.RData")
load("phewas.RData")
nhgri <- rename(nhgri, c(SNPS = 'snp_id',
P.VALUE = "pvalue",
DISEASE.TRAIT = 'disease',
Initial.Sample.Size = 'Initial.Sample',
Replication.Sample.Size = "Replication.Sample"))
nhgri<-nhgri[,c("snp_id","PUBMEDID","pvalue","disease","Initial.Sample","Replication.Sample")]
nhgri$pvalue <- as.numeric(format(nhgri$pvalue, digits=3, scientific=T))
nhgri <- nhgri[grepl("rs",nhgri$snp_id),]
nhgri$Source <- "nhgri"
# Remove redundant rows in nhgri data ("PUBMEDID", "snp_id", "pvalue", "disease", "Initial.Sample", "Replication.Sample" "Source" )
nhgri <- nhgri[!duplicated(nhgri),]
nhgri <- gwas.filter(nhgri)
grasp <- rename(grasp, c(SNPid.dbSNP134. = 'snp_id',
PMID = 'PUBMEDID',
Pvalue = "pvalue",
Phenotype = 'disease',
Initial.Sample.Description = 'Initial.Sample',
Replication.Sample.Description = 'Replication.Sample'))
grasp<-grasp[,c("snp_id","PUBMEDID","pvalue","disease","Initial.Sample","Replication.Sample")]
grasp$snp_id <- paste("rs",grasp$snp_id,sep="")
grasp$Source <- "grasp"
grasp <- subset(grasp, !is.na(pvalue))
grasp$pvalue <- as.numeric(format(grasp$pvalue, digits=3, scientific=T))
grasp <- gwas.filter(grasp)
gwasdb <- rename(gwasdb, c(PMID = 'PUBMEDID',
SNPID.dbSNP = "gwasdb_SNP_ID",
ORI_SNP = 'snp_id',
P_VALUE = "pvalue",
GWAS_TRAIT = 'disease',
GWAS_INITIAL_SAMPLE_SIZE = 'Initial.Sample',
SOURCE = "Source_gwasdb"))
gwasdb<-gwasdb[,c("snp_id","PUBMEDID","pvalue","disease","Initial.Sample")]
gwasdb <- gwasdb[grepl("rs",gwasdb$snp_id),]
# Maually change some of the mis-spelled pvalue here
gwasdb$pvalue[gwasdb$pvalue=="1.49 E-9"] <- "1.49E-9"
gwasdb$pvalue[gwasdb$pvalue=="1.05 E-14"] <- "1.05E-14"
gwasdb$pvalue[gwasdb$pvalue=="1.3E?\\05"] <- "1.3E-05"
gwasdb$pvalue[gwasdb$pvalue=="1.78E?05"] <- "1.78E-05"
gwasdb <- subset(gwasdb, pvalue!="")
gwasdb$Source <- "gwasdb"
gwasdb$pvalue <- as.numeric(format(gwasdb$pvalue, digits=3, scientific=T))
gwasdb <- gwas.filter(gwasdb)
phewas <- rename(phewas, c(phewas.code= 'PUBMEDID',
snp = 'snp_id',
p.value = "pvalue",
phewas.phenotype = 'disease',
cases='Initial.Sample'))
phewas <- phewas[,c("snp_id","pvalue","disease","Initial.Sample","PUBMEDID")]
phewas$PUBMEDID <- paste("PW",phewas$PUBMEDID,sep="")
phewas <- subset(phewas, !is.na(pvalue))
phewas <- subset(phewas, !is.na(snp_id))
phewas <- phewas[grepl("rs",phewas$snp_id),]
phewas$pvalue <- as.numeric(format(phewas$pvalue, digits=3, scientific=T))
phewas$Source <- "PheWAS"
grasp$id <- paste(grasp$snp_id, grasp$PUBMEDID,sep="_")
nhgri$id <- paste(nhgri$snp_id, nhgri$PUBMEDID,sep="_")
gwasdb$id <- paste(gwasdb$snp_id, gwasdb$PUBMEDID,sep="_")
# id1 <- unique(grasp$id); id2 <- unique(nhgri$id);
# id3 <- unique(gwasdb$id)
# data.plot=list(GRASP=id1,NHGRI=id2,GWASDB=id3)
# file.name="grasp_nhgri_gwasdb_venn.pdf"
# venn.plot(data.plot,file.name)
# data.plot=list(GRASP=id1,NHGRI=id2,GWASDB=id3,PheWAS=id4,Exome=id5)
# file.name="grasp_nhgri_gwasdb_phewas_exome_venn.png"
# venn.plot(data.plot,file.name)
# Subset of grasp data with id (SNP_PMID) not in nhgri data
grasp1 <- subset(grasp, !(id %in% nhgri$id))
grasp1$Source <- "grasp"
# Remove redundant rows in grasp1 data
grasp1 <- grasp1[!duplicated(grasp1),]
# Subset of grasp data with id (SNP_PMID) not in nhgri
gwasdb1 <- subset(gwasdb, !(id %in% c(nhgri$id, grasp$id)))
gwasdb1$Source <- "gwasdb"
# Remove redundant rows in grasp1 data
gwasdb1 <- gwasdb1[!duplicated(gwasdb1),]
data <- sbind(nhgri, grasp1)
data <- sbind(data, gwasdb1)
data <- sbind(data, phewas)
data$id <- paste(data$snp_id, data$PUBMEDID,sep="_")
data$snp_id <- trimWhiteSpace(data$snp_id)
data <- data[,-match(c("id"), names(data))]
gwas.data <- data
#grasp has a strange chracter like \U3e32393cs which should convert to "'".
gwas.data$disease=gsub("\U3e32393cs","'",gwas.data$disease)
#exclude additional "b" from some snps
gwas.data$snp_id=gsub("b","",gwas.data$snp_id)
#write.table(data,
# "stopgap_grasp_nhgri_gwasdb_merged.txt",
# sep = "\t", row.names = FALSE, quote = F, na = "")
# save(data, file = "stopgap_4sources.RData", compress = TRUE)
save(gwas.data, file = "stopgap_4sources_clean.RData", compress = TRUE)
invisible(data)
}
# new.gwassnps: identify the unique GWAS SNPs for LD calculation
gwas.snps <- function(gwas.file="stopgap_4sources_clean.RData"){
load(gwas.file)
snps <- sort(unique(gwas.data$snp_id))
writeLines(snps, "stopgap2_SNPs.txt", sep = "\n", useBytes = FALSE)
invisible(snps)
}
# Coordinate lookup and LD calculation based on the 1KG data for the new SNPs identified in this version
# run.ld: Calculate the LD SNPs based on the
# rslist is the list of rs IDs with # prefixed comment lines allowed
# https://connect.gsk.com/sites/genetics/GeneticsWIKI/Wiki%20Pages/STOPGAP%20-%20LD%20Calculation.aspx
# before running Get_rsID_coord.py, please add the following path at UNIX
# PATH=$PATH\:/GWD/bioinfo/projects/GXapp/EntrezDirect/edirect
run.ld <- function(){
# Input SNPs: stopgap2_SNPs.txt
system("mkdir STOPGAP2_LDResults")
#(1) Coordinate lookup
system("python2.7 ./Get_rsID_coord.py -o ./STOPGAP_LD -r ./Example/stopgap2_snps.txt")
#(2) LD Calculation example for chr. 1
system("./LDcalc.sh 1 ./STOPGAP_LD ./STOPGAP_LD")
}
# Update rsIDs in the gwas data (data) to dbSNP141 version by using the ./STOPGAP2_LDResults/rsID_Coordinates.txt file
# Write out the new GWAS data as "stopgap_4sources_dbSNP141.RData"
rsID.update <- function(gwas.file="stopgap_4sources.RData",
coor.file="./STOPGAP2_LDResults/rsID_Coordinates.txt")
{
load(gwas.file)
gwas.data <- gwas.filter(data)
gwas.data$disease <- tolower(trimWhiteSpace(gwas.data$disease))
rsid.coor <- read.delim(coor.file,
header = TRUE, comment.char = "", sep = "\t", as.is = TRUE)
names(rsid.coor)[names(rsid.coor)=="X.rsID_searched"] <- "rsID "
gwas.data$rsid <- rsid.coor$rsID[match(gwas.data$snp_id, rsid.coor$inputrsIDs )]
names <- names(gwas.data)[!(names(gwas.data) %in% c("snp_id", "rsid"))]
gwas.data <- gwas.data[,c("snp_id", "rsid", names)]
save(gwas.data, file = "stopgap_4sources_dbSNP141.RData", compress = TRUE)
invisible(gwas.data)
}
# Find all variants in LD with GWAS variants at r2 > 0.5
ld.snps <- function(gwas.data="stopgap_4sources_dbSNP141.RData",
ld.path="./STOPGAP2_LDResults"){
cat("Start finding all the SNPs with LD r2 > 0.5 ...", "\n")
load(gwas.data)
gwas.ld.snps <- gwas.data[,c("snp_id", "rsid")]
gwas.ld.snps <- gwas.ld.snps[!duplicated(gwas.ld.snps),]
ld.files <- list.files(ld.path, pattern="*.labeled.ld$")
n <- length(ld.files)
chrs <- unlist(lapply( strsplit(ld.files,"\\."),function(x)x[[1]]))
chrs <- gsub("chr", "", chrs)
m.no <- match(c(as.character(1:22), "X"), chrs)
ld.snps.r2 <- list()
k <- 0
for (i in m.no){
k <- k + 1
cat(k, "\n")
file <- ld.files[i]
file <- paste(ld.path, file, sep="/")
ld.data <- read.delim(file,
header = TRUE, comment.char = "", sep = "", as.is = TRUE)
names(ld.data)[names(ld.data)=="R.2"] <- "r2"
ld.data <- ld.plusTarget(ld.data)
if (nrow(ld.data)>0){
# ld data includes more SNPs than in the gwas data, so filter down it first.
ld.data <- subset(ld.data, SNP.gwas %in% gwas.ld.snps$rsid)
} else {
ld.data <- NA
}
ld.snps.r2[[k]] <- ld.data
}
ld.snps.r2 <- do.call("rbind", ld.snps.r2)
system("mkdir ./STOPGAP2_LDResults/gwas_LD")
save(ld.snps.r2, file = "./STOPGAP2_LDResults/gwas_LD/ld.snps.r2.RData", compress = TRUE)
ld.snps <- ld.snps.r2[,c("CHR.ld", "POS.ld", "SNP.ld")]
ld.snps <- ld.snps[!duplicated(ld.snps),]
save(ld.snps, file = "./STOPGAP2_LDResults/gwas_LD/ld.snps.RData", compress = TRUE)
invisible(ld.snps.r2)
}
# updata.ld.data: updata ld data:
# Remove SNP.ld with two positions in both ld.snps and ld.snps.r2
# Use chr:pos to replace the missing ld SNP ids in both ld.snps and ld.snps.r2
updata.ld.data <- function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.RData",
ld.r2.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.r2.RData"){
load(ld.file)
load(ld.r2.file)
# Remove SNP.ld with two positions in both ld.snps and ld.snps.r2
snps.rm <- names(table(ld.snps$SNP.ld))[which(table(ld.snps$SNP.ld)==2)]
ld.snps.r2 <- subset(ld.snps.r2, !(SNP.ld %in% snps.rm))
ld.snps <- subset(ld.snps, !(SNP.ld %in% snps.rm))
# Use chr:pos to replace the missing ld SNP ids in both ld_snps and ld.snps.r2
miss.no <- which(ld.snps$SNP.ld==".")
ld.snps$SNP.ld[miss.no] <- paste(ld.snps$CHR.ld[miss.no], ld.snps$POS.ld[miss.no], sep=":")
miss.no1 <- which(ld.snps.r2$SNP.ld==".")
ld.snps.r2$SNP.ld[miss.no1] <- paste(ld.snps.r2$CHR.ld[miss.no1], ld.snps.r2$POS.ld[miss.no1], sep=":")
save(ld.snps.r2, file = "./STOPGAP2_LDResults/gwas_LD/ld.snps.r2.updated.RData", compress = TRUE)
save(ld.snps, file = "./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.RData", compress = TRUE)
invisible(ld.snps.r2)
}
# Update the ld.snps.r2 and ld_snps data by including the GWAS SNPs data which don't have any LD information there
update.ld.snps.r2 <- function(gwas.file="stopgap_4sources_dbSNP141.RData",
ld.file = "./STOPGAP2_LDResults/gwas_LD/ld.snps.r2.updated.RData",
rsid.file = "./STOPGAP2_LDResults/rsID_Coordinates.txt"){
cat("Update the ld.snps.r2 data by including the (154526-147706) GWAS SNPs data there ...", "\n")
load(gwas.file)
load(ld.file)
coord <- read.delim(rsid.file,
header = TRUE, comment.char = "", sep = "\t", as.is = TRUE)
names(coord)[names(coord)=="X.rsIDsearched"] <- "rsID"
coord$chr <- unlist(lapply( strsplit(coord$GRCh37_Coordinate,":"),function(x)x[[1]]))
coord$pos <- unlist(lapply( strsplit(coord$GRCh37_Coordinate,":"),function(x)x[[2]]))
coord <- coord[!duplicated(coord),]
# gwas data with rsid not in the ld.snps.r2 data
gwas.data1 <- subset(gwas.data, !(rsid %in% unique(ld.snps.r2$SNP.ld)) )
gwas.data1$chr <- coord$chr[match(gwas.data1$rsid, coord$rsID)]
gwas.data1$pos <- coord$pos[match(gwas.data1$rsid, coord$rsID)]
gwas.data1$REF <- coord$REF[match(gwas.data1$rsid, coord$rsID)]
gwas.data1$ALT <- coord$ALT[match(gwas.data1$rsid, coord$rsID)]
gwas.data2 <- gwas.data1[,c("chr", "pos", "rsid", "REF", "ALT")]
gwas.data2 <- cbind(gwas.data2, gwas.data2)
gwas.data2$r2 <- 1
names(gwas.data2) <- names(ld.snps.r2)
ld.snps.r2 <- rbind(ld.snps.r2, gwas.data2)
save(ld.snps.r2, file = "./STOPGAP2_LDResults/gwas_LD/ld.snps.r2.updated.plusGWASsnps.RData", compress = TRUE)
ld.snps <- ld.snps.r2[,c("CHR.ld", "POS.ld", "SNP.ld")]
ld.snps <- ld.snps[!duplicated(ld.snps),]
save(ld.snps, file = "./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.RData", compress = TRUE)
invisible(ld.snps.r2)
}
# Update the ld.snps.r2 and ld_snps data by removing the LD SNPs more than 500kb away from the GWAS SNPs,
# If any GWAS SNPs get deleted, put them back by including r2=1 there
update1.ld.snps.r2 <- function(gwas.file="stopgap_4sources_dbSNP141.RData",
ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.r2.updated.plusGWASsnps.RData",
dis = 10000){
cat("Update the ld.snps.r2 data by removing the LD SNPs more than 10kb away from the GWAS SNPs ...", "\n")
load(gwas.file)
load(ld.file)
ld.snps.r2$POS.gwas <- as.numeric(ld.snps.r2$POS.gwas)
ld.snps.r2$POS.ld <- as.numeric(ld.snps.r2$POS.ld)
ld.snps.r2 <- subset(ld.snps.r2, abs(POS.gwas-POS.ld)<=dis)
ld.snps.r2 <- ld.snps.r2[!duplicated(ld.snps.r2),]
save(ld.snps.r2, file = "./STOPGAP2_LDResults/gwas_LD/ld.snps.r2.updated.plusGWASsnps.500kb.RData", compress = TRUE)
ld.snps <- ld.snps.r2[,c("CHR.ld", "POS.ld", "SNP.ld")]
ld.snps <- ld.snps[!duplicated(ld.snps),]
save(ld.snps, file = "./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.500kb.RData", compress = TRUE)
gwas.r2 <- merge(gwas.data[,c("rsid", "PUBMEDID", "pvalue")], ld.snps.r2[,c("SNP.gwas", "SNP.ld", "r2", "CHR.ld", "POS.ld")],
by.x = "rsid", by.y="SNP.gwas", all = TRUE)
names(gwas.r2) <- c("snp.gwas", "pubmedid", "pvalue", "snp.ld", "r2", "chr.ld", "pos.ld")
gwas.r2 <- gwas.r2[!duplicated(gwas.r2),]
system("mkdir LocusClustering")
save(gwas.r2, file = "./LocusClustering/gwas_r2.RData", compress = TRUE)
invisible(ld.snps.r2)
}
# Update the ld.snps.r2 data by adding the AF information from 1KG phase I data ...
r2.frq <- function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.r2.updated.plusGWASsnps.500kb.RData",
frq.file="./Data/1KG_AF/gws.frq.RData"){
cat("Update the ld.snps.r2 data by adding the AF information from 1KG phase I data ...", "\n")
load(ld.file)
load(frq.file)
# frq.1kg
id.no <- match("ID", names(frq.1kg))
ld.snps.r2 <- merge(ld.snps.r2, frq.1kg[,-id.no],
by.x = c("CHR.ld","POS.ld", "REF.ld","ALT.ld"),
by.y=c("CHROM", "POS", "REF", "ALT"), all.x = TRUE, all.y = FALSE)
save(ld.snps.r2, file = "./STOPGAP2_LDResults/gwas_LD/ld.snps.r2.updated.plusGWASsnps.500kb.AF.RData", compress = TRUE)
invisible(ld.snps.r2)
}
#
#STEP 4. add a genomic evidence resorce to the ld snps
#
# ld.rdb.dhscor: add the Cat.rdb to the ld snps
ld.rdb <- function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.500kb.RData",
rdb.file="./Data/genomic_resource/regulomedb14.RData"){
load(ld.file)
load(rdb.file)
# add the Cat.rdb to the ld snps
ld.snps$Cat.rdb <- rdb$Cat.rdb[match(ld.snps$SNP.ld, rdb$SNP)]
gwas.ld.rdb=ld.snps
save(gwas.ld.rdb, file = "./VarGeneMapping/gwas.ld.rdb.RData", compress = TRUE)
invisible(gwas.ld.rdb)
}
# For each LD SNP, (1) identify each dhscor where SNP falls within promoter or distal genomic region;
# (2) add gene, cor (if in promoter, set cor=1), and an indicator if it is in a distal or promoter region.
ld.dhscor <- function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.500kb.RData",
dhscor.file="./Data/genomic_resource/dhscor.RData",
mpos.rng.p = c("Chr.pdhs", "Start.pdhs", "Stop.pdhs"),
mpos.rng.d = c("Chr.ddhs", "Start.ddhs", "Stop.ddhs")){
load(ld.file)
load(dhscor.file)
system("mkdir ./VarGeneMapping")
dhscor$Chr.ddhs <- gsub("chr", "", dhscor$Chr.ddhs)
dhscor$Chr.pdhs <- gsub("chr", "", dhscor$Chr.pdhs)
dhscor$Gene <- as.character(dhscor$Gene)
dhscor.p <- dhscor[,c("Chr.pdhs", "Start.pdhs", "Stop.pdhs", "Gene")]
dhscor.d <- dhscor[,c("Chr.ddhs", "Start.ddhs", "Stop.ddhs", "Gene", "fpr")]
## Run it by chromosome to make it easier memory-wise
chr <- unique(ld.snps$CHR.ld)
cpos<- list()
for(i in chr) {
cat(i, "\n")
# promoter
ld.snps.i <- subset(ld.snps, CHR.ld==i)
dhscor.p.i <- subset(dhscor.p, Chr.pdhs==i)
dhscor.p.i$fpr <- 0
## Expand range as one row per position for dhscor.p.i
dhscor.p.i$Length <- dhscor.p.i[, mpos.rng.p[3]] - dhscor.p.i[, mpos.rng.p[2]] + 1
dhscor.p.i.exp <- data.frame(Position = as.vector(unlist(apply(dhscor.p.i[, mpos.rng.p[2:3]], 1,
function(x) x[1]:x[2]))),
Gene = rep(as.character(dhscor.p.i$Gene), dhscor.p.i$Length),
fpr = rep(dhscor.p.i$fpr, dhscor.p.i$Length))
dhscor.p.i.exp <- dhscor.p.i.exp[!duplicated(dhscor.p.i.exp),]
tmp <- merge(ld.snps.i, dhscor.p.i.exp, by.x = "POS.ld",
by.y = "Position", all.x = TRUE, all.y = FALSE)
tmp1 <- tmp[!duplicated(tmp),]
tmp1$type <- NA
tmp1$type[!is.na(tmp1$fpr)] <- "p"
# distal
dhscor.d.i <- subset(dhscor.d, Chr.ddhs==i)
## Expand range as one row per position for dhscor.d.i
dhscor.d.i$Length <- dhscor.d.i[, mpos.rng.d[3]] - dhscor.d.i[, mpos.rng.d[2]] + 1
dhscor.d.i.exp <- data.frame(Position = as.vector(unlist(apply(dhscor.d.i[, mpos.rng.d[2:3]], 1,
function(x) x[1]:x[2]))),
Gene = rep(as.character(dhscor.d.i$Gene), dhscor.d.i$Length),
fpr = rep(dhscor.d.i$fpr, dhscor.d.i$Length))
dhscor.d.i.exp <- dhscor.d.i.exp[!duplicated(dhscor.d.i.exp),]
tmp <- merge(ld.snps.i, dhscor.d.i.exp, by.x = "POS.ld",
by.y = "Position", all.x = TRUE, all.y = FALSE)
tmp2 <- tmp[!duplicated(tmp),]
tmp2$type <- NA
tmp2$type[!is.na(tmp2$fpr)] <- "d"
tmp <- merge(tmp1, tmp2,
by = c("CHR.ld","POS.ld", "SNP.ld","Gene", "fpr", "type"),
all.x = TRUE, all.y = TRUE)
cpos[[i]] <- tmp
}
res <- do.call("rbind", cpos)
res$id=paste(res$SNP.ld,res$Gene,res$type,sep="_")
c4<-aggregate(fpr ~ id, res,mean)
res=res[!duplicated(res$id),]
res$fpr=NULL
res=merge(res,c4,by="id",all.x=T,all.y=F)
res$id <- NULL
gwas.ld.dhscor <- res
# system("mkdir VarGeneMapping")
save(gwas.ld.dhscor, file = "./VarGeneMapping/gwas.ld.dhscor.RData", compress = TRUE)
invisible(res)
}
# ----- Merge the CHIA-PET data
# ld.chiapet: For each LD SNP, (1) indentify each chiapet where SNP falls within promoter or distal genomic region;
# (2) add gene, cor (if in promoter, set cor=1), and an indicator if it is in a distal or promoter region.
ld.chiapet <- function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.500kb.RData",
chiapet.file="./Data/genomic_resource/CHIA.PET_5celltypes.RData",
mpos.rng.d = c("dChr", "dStart", "dEnd")){
cat("Start merging CHIA-PET data...", "\n")
load(ld.file)
load(chiapet.file)
names(CHIA.PET)[names(CHIA.PET)=="genes"] <- "Gene"
CHIA.PET$CellType <- gsub(".bed", "", CHIA.PET$CellType)
CHIA.PET$Gene <- as.character(CHIA.PET$Gene)
CHIA.PET.d <- CHIA.PET[,c("dChr", "dStart", "dEnd", "Gene", "CellType")]
## Run it by chromosome to make it easier memory-wise
chr <- unique(ld.snps$CHR.ld)
cpos<- list()
for(i in chr) {
cat(i, "\n")
# distal
CHIA.PET.d.i <- subset(CHIA.PET.d, dChr==i)
ld.snps.i <- subset(ld.snps, CHR.ld==i)
## Expand range as one row per position for chiapet.d.i
CHIA.PET.d.i$Length <- CHIA.PET.d.i[, mpos.rng.d[3]] - CHIA.PET.d.i[, mpos.rng.d[2]] + 1
CHIA.PET.d.i.exp <- data.frame(Position = as.vector(unlist(apply(CHIA.PET.d.i[, mpos.rng.d[2:3]], 1,
function(x) x[1]:x[2]))),
Gene = rep(as.character(CHIA.PET.d.i$Gene), CHIA.PET.d.i$Length),
CellType = rep(CHIA.PET.d.i$CellType, CHIA.PET.d.i$Length))
CHIA.PET.d.i.exp <- CHIA.PET.d.i.exp[!duplicated(CHIA.PET.d.i.exp),]
tmp <- merge(ld.snps.i, CHIA.PET.d.i.exp, by.x = "POS.ld",
by.y = "Position", all.x = TRUE, all.y = FALSE)
tmp2 <- tmp[!duplicated(tmp),]
tmp2$type <- NA
tmp2$type[!is.na(tmp2$CellType)] <- "d"
# Remove rows with all NAs for the "Gene", "CellType" and "type" columns before merging
#tmp2$id <- paste(tmp2$Gene, tmp2$CellType, tmp2$type, sep="_")
#tmp3 <- subset(tmp2, id!="NA_NA_NA")
tmp <- tmp2
cpos[[i]] <- tmp
}
res <- do.call("rbind", cpos)
gwas.ld.chiapet <- res
save(gwas.ld.chiapet, file = "./VarGeneMapping/gwas.ld.chiapet.RData", compress = TRUE)
invisible(res)
}
# ld.eQTL: For each LD SNP, (1) identify each eQTL where SNP falls within promoter or distal genomic region;
# (2) add gene, cor (if in promoter, set cor=1), and an indicator if it is in a distal or promoter region.
ld.eQTL <- function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.500kb.RData",
eQTL.file="./Data/genomic_resource/eQTL_single_44Tissues.RData"){
cat("Start merging eQTL data...", "\n")
load(ld.file)
load(eQTL.file)
# "rs_id_dbSNP142_GRCh37p13","gene_name","gene_type","Tissue","Score"
eQTL <- eQTL[,c("ID", "hg19", "tissue.pvalue")]
names(eQTL) <- c("SNP", "Gene", "Tissue.abb.pvalue")
gwas.ld.eQTL.s <- merge(ld.snps, eQTL, by.x = "SNP.ld",
by.y = "SNP", all.x = TRUE, all.y = FALSE)
save(gwas.ld.eQTL.s, file = "./VarGeneMapping/gwas.ld.eQTL.single.RData", compress = TRUE)
invisible(gwas.ld.eQTL.s)
}
ld.multi.eQTL <- function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.500kb.RData" ,
eQTL.multi.file="./Data/genomic_resource/eQTL_multi_9Tissues.RData"){
cat("Start merging eQTL data...", "\n")
load(ld.file)
# Gene chrom Gene.ID start end strand length source
load(eQTL.multi.file)
gwas.ld.eQTL.m <- merge(ld.snps, meQTL, by.x = "SNP.ld",
by.y = "snp", all.x = TRUE, all.y = FALSE)
# gwas.ld.eQTL.m$Tissue <- ifelse(is.na(gwas.ld.eQTL.m$Gene),NA,"AS;AT;HLV;Lu;MS;NT;SSEL;Th;WB")
gwas.ld.eQTL.m=gwas.ld.eQTL.m[,c("SNP.ld","CHR.ld","POS.ld","Gene")]
save(gwas.ld.eQTL.m, file = "./VarGeneMapping/gwas.ld.eQTL.multi.RData", compress = TRUE)
invisible(gwas.ld.eQTL.m)
}
# ld.fantom5: For each LD SNP, (1) indentify each fantom5 where SNP falls within promoter or distal genomic region;
# (2) add gene, tissue and an indicator if it is in a distal region (note no promoter region here).
ld.fantom5 <- function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.500kb.RData",
fantom5.file="./Data/genomic_resource/FANTOM5.RData",
mpos.rng.d = c("chr", "startpos", "endpos")){
load(ld.file)
load(fantom5.file)
names(fantom5)[names(fantom5)=="gene"] <- "Gene"
names(fantom5)[names(fantom5)=="X.chrom"] <- "chr"
fantom5$Gene <- as.character(fantom5$Gene)
fantom5.d <- fantom5[,c("chr", "startpos", "endpos", "Gene", "Transcript", "fpr")]
fantom5.d$chr <- gsub("chr", "", fantom5.d$chr)
## Run it by chromosome to make it easier memory-wise
chr <- unique(ld.snps$CHR.ld)
cpos<- list()
for(i in chr) {
cat(i, "\n")
# no promoter
ld.snps.i <- subset(ld.snps, CHR.ld==i)
# distal
fantom5.d.i <- subset(fantom5.d, chr==i)
## Expand range as one row per position for fantom5.d.i
fantom5.d.i$Length <- fantom5.d.i[, mpos.rng.d[3]] - fantom5.d.i[, mpos.rng.d[2]] + 1
fantom5.d.i.exp <- data.frame(Position = as.vector(unlist(apply(fantom5.d.i[, mpos.rng.d[2:3]], 1,
function(x) x[1]:x[2]))),
Gene = rep(as.character(fantom5.d.i$Gene), fantom5.d.i$Length),
Tissue = rep(fantom5.d.i$Transcript, fantom5.d.i$Length),
fpr = rep(fantom5.d.i$fpr, fantom5.d.i$Length))
fantom5.d.i.exp <- fantom5.d.i.exp[!duplicated(fantom5.d.i.exp),]
tmp <- merge(ld.snps.i, fantom5.d.i.exp, by.x = "POS.ld",
by.y = "Position", all.x = TRUE, all.y = FALSE)
tmp2 <- tmp[!duplicated(tmp),]
tmp2$type <- NA
tmp2$type[!is.na(tmp2$Tissue)] <- "d"
tmp <- tmp2[,c("CHR.ld","POS.ld", "SNP.ld", "Gene", "Tissue", "fpr", "type")]
cpos[[i]] <- tmp
}
res <- do.call("rbind", cpos)
gwas.ld.fantom5 <- res
save(gwas.ld.fantom5, file = "./VarGeneMapping/gwas.ld.fantom5.RData", compress = TRUE)
invisible(res)
}
# ld.CHIC: For each LD SNP, (1) indentify each CHIC where SNP falls within promoter or distal genomic region;
# (2) add gene, tissue and an indicator if it is in a distal region (note no promoter region here).
ld.CHIC <- function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.500kb.RData",
CHIC.file="./Data/genomic_resource/chic.RData",
mpos.rng.d = c("chr", "startpos", "endpos")){
load(ld.file)
load(CHIC.file)
CHIC$Gene <- as.character(CHIC$Gene)
CHIC.d <- CHIC[,c("chr", "startpos", "endpos", "Gene", "tissue")]
CHIC.d$chr <- gsub("chr", "", CHIC.d$chr)
## Run it by chromosome to make it easier memory-wise
chr <- unique(ld.snps$CHR.ld)
cpos<- list()
for(i in chr) {
cat(i, "\n")
# no promoter
ld.snps.i <- subset(ld.snps, CHR.ld==i)
# distal
CHIC.d.i <- subset(CHIC.d, chr==i)
## Expand range as one row per position for CHIC.d.i
CHIC.d.i$Length <- CHIC.d.i[, mpos.rng.d[3]] - CHIC.d.i[, mpos.rng.d[2]] + 1
CHIC.d.i.exp <- data.frame(Position = as.vector(unlist(apply(CHIC.d.i[, mpos.rng.d[2:3]], 1,
function(x) x[1]:x[2]))),
Gene = rep(as.character(CHIC.d.i$Gene), CHIC.d.i$Length),
Tissue = rep(CHIC.d.i$tissue, CHIC.d.i$Length))
CHIC.d.i.exp <- CHIC.d.i.exp[!duplicated(CHIC.d.i.exp),]
tmp <- merge(ld.snps.i, CHIC.d.i.exp, by.x = "POS.ld",
by.y = "Position", all.x = TRUE, all.y = FALSE)
tmp2 <- tmp[!duplicated(tmp),]
tmp2$type <- NA
tmp2$type[!is.na(tmp2$Tissue)] <- "d"
tmp <- tmp2[,c("CHR.ld","POS.ld", "SNP.ld", "Gene", "Tissue", "type")]
cpos[[i]] <- tmp
}
res <- do.call("rbind", cpos)
gwas.ld.CHIC <- res
save(gwas.ld.CHIC, file = "./VarGeneMapping/gwas.ld.CHIC.RData", compress = TRUE)
invisible(res)
}
# add cato to ld snps
ld.cato <-function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.500kb.RData",
cato.file="./Data/genomic_resource/cato.RData"){
load(ld.file)
load(cato.file)
# "chrom" "snpChromStart" "snpChromEnd" "rsid"
# "pred.fit.pctSig" "strand" "motifname" "position"
# "ref.allele" "nonref.allele" "Cell_types"
cato <- cato[,c("chrom","rsid","pred.fit.pctSig","motifname","Cell_types")]
gwas.ld.cato <- merge(ld.snps, cato, by.x = "SNP.ld",
by.y = "rsid", all.x = TRUE, all.y = FALSE)
save(gwas.ld.cato, file = "./VarGeneMapping/gwas.ld.cato.RData", compress = TRUE)
invisible(gwas.ld.cato)
}
# gwas.ld.deltaSVM.RData is generated and downloaded at Data/genomic_resource
ld.deltaSVM <-function(){
system("cp ./Data/genomic_resource/gwas.ld.deltaSVM.RData ./VarGeneMapping")
}
# gwas.ld.phylop.RData is generated and downloaded at Data/genomic_resource
ld.phylop <-function(){
system("cp ./Data/genomic_resource/gwas.ld.phylop.RData ./VarGeneMapping")
}
## nearest.gene: Take a merged data set of genes and SNPs on a single
## chromosome, sorted by position (optional; this gets repeated) with
## an indicator for whether the entry is a SNP or a Gene, and identify
## the nearest gene.
nearest.gene <- function(snp.gene)
{
## Make sure it is sorted by position
snp.gene <- snp.gene[order(snp.gene$POS),]
snps <- grep("SNP", snp.gene$type)
genes <- grep("Gene", snp.gene$type)
genes <- c(genes, snps[length(snps)] + 1) # Add dummy gene to end
## Initialize previous and next gene values for first variant
prev.gene <- next.gene <- numeric(length(snps))
if(genes[1] > snps[1]){
prev.gene[1] <- NA
x <- 0
}else {
prev.gene[1] <- genes[x <- which(genes == max(genes[genes < snps[1]],
na.rm = TRUE))]
}
next.gene[1] <- genes[x + 1]
for(i in 2:length(snps)) {
if(next.gene[i - 1] > snps[i]) { # Same genes as for previous SNP
prev.gene[i] <- prev.gene[i - 1]
next.gene[i] <- next.gene[i - 1]
} else { # Figure out where the new SNP position falls
prev.gene[i] <- genes[x <- which(genes == max(genes[genes < snps[i]],
na.rm = TRUE))]
next.gene[i] <- genes[x + 1]
}
}
## Remove the trailing dummy gene
genes <- genes[-length(genes)]
## Calculate distances to adjacent genes
prev.dist <- snp.gene$POS[snps] - snp.gene$POS[prev.gene]
next.dist <- snp.gene$POS[next.gene] - snp.gene$POS[snps]
snp.gene$prev.gene <- snp.gene$prev.dist <- rep(NA, nrow(snp.gene))
snp.gene$prev.gene[snps] <- snp.gene$ID[prev.gene]
snp.gene$prev.dist[snps] <- prev.dist
snp.gene$next.gene <- snp.gene$next.dist <- rep(NA, nrow(snp.gene))
snp.gene$next.gene[snps] <- snp.gene$ID[next.gene]
snp.gene$next.dist[snps] <- next.dist
## Keep only SNPs
snp.gene <- snp.gene[snps,]
## Get nearest gene and distance
near.ind <- apply(snp.gene[, c("prev.dist", "next.dist")], 1,
function(x) which(x == min(x, na.rm = TRUE)))
near.ind <- as.integer(gsub("1:2","1",near.ind))
snp.gene$near.dist <- snp.gene$near.gene <- rep(NA, nrow(snp.gene))
snp.gene$near.gene[near.ind == 1] <- snp.gene$prev.gene[near.ind == 1]
snp.gene$near.gene[near.ind == 2] <- snp.gene$next.gene[near.ind == 2]
snp.gene$near.dist[near.ind == 1] <- snp.gene$prev.dist[near.ind == 1]
snp.gene$near.dist[near.ind == 2] <- snp.gene$next.dist[near.ind == 2]
invisible(snp.gene)
}
#fing gene location based on gencode V19
ld.posgene <- function(ld.file="./STOPGAP2_LDResults/gwas_LD/ld.snps.updated.plusGWASsnps.500kb.RData",
Posgene.file="/Data/genomic_resource/gencode_v19_clean.RData"){
load(ld.file)
load(Posgene.file) # source: gencode=20224
gencode$Gene=trimWhiteSpace(gencode$Gene)
ch.d=c(as.character(1:22),"X")
all.result=list()
k=0
for(i in ch.d){
k<-k+1
ld.snps.i <- subset(ld.snps, CHR.ld==i)
ld.snps.i <- ld.snps.i[order(ld.snps.i$POS.ld),]
gencode.i <- subset(gencode, chrom==i)
gencode.i <- gencode.i[order(gencode.i$start),]
x1=ld.snps.i[,c("POS.ld","SNP.ld")]