-
Notifications
You must be signed in to change notification settings - Fork 28
/
HDL.data.wrangling.R
168 lines (146 loc) · 6.67 KB
/
HDL.data.wrangling.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
library(dplyr)
args <- commandArgs(trailingOnly = TRUE)
fn <- gsub(x = args[grep(x = args, pattern = "gwas.file=")], pattern = "gwas.file=", replacement = "")
LD.path <- gsub(x = args[grep(x = args, pattern = "LD.path=")], pattern = "LD.path=", replacement = "")
GWAS.type <- gsub(x = args[grep(x = args, pattern = "GWAS.type=")], pattern = "GWAS.type=", replacement = "")
output.file <- gsub(x = args[grep(x = args, pattern = "output.file=")], pattern = "output.file=", replacement = "")
log.file <- gsub(x = args[grep(x = args, pattern = "log.file=")], pattern = "log.file=", replacement = "")
SNP <- gsub(x = args[grep(x = args, pattern = "SNP=")], pattern = "SNP=", replacement = "")
A1 <- gsub(x = args[grep(x = args, pattern = "A1=")], pattern = "A1=", replacement = "")
A2 <- gsub(x = args[grep(x = args, pattern = "A2=")], pattern = "A2=", replacement = "")
N <- gsub(x = args[grep(x = args, pattern = "N=")], pattern = "N=", replacement = "")
b <- gsub(x = args[grep(x = args, pattern = "b=")], pattern = "b=", replacement = "")
se <- gsub(x = args[grep(x = args, pattern = "se=")], pattern = "se=", replacement = "")
Z <- gsub(x = args[grep(x = args, pattern = "Z=")], pattern = "Z=", replacement = "")
if(length(output.file) == 0)
output.file <- fn
if(length(log.file) != 0){
log.file <- paste(log.file, "txt", sep = ".")
if(file.exists(log.file) == T){
system(paste0("rm ",log.file))
}
}
library(data.table)
data.table.version <- packageVersion("data.table")
if(data.table.version < "1.12.1"){
message("Searching for the updated version of package 'data.table' in user's library:")
detach("package:data.table", unload=TRUE)
library(data.table, lib.loc = Sys.getenv("R_LIBS_USER"))
}
smart.reader <- function(path){
path.split <- unlist(strsplit(path, split = "\\."))
file.type <- path.split[length(path.split)]
if(file.type == "rds"){
return(readRDS(path))
} else if(file.type == "gz" | file.type == "bgz"){
options(datatable.fread.input.cmd.message=FALSE)
return(fread(input = paste("zcat < ",path)))
} else{
try_error <- try(return(fread(path)))
if(class(try_error) == "try-error"){
error.message <- "This file type is not supported by fread function in data.table package. Please reformat it to .txt, .csv or .tsv."
if(output.file != ""){
cat(error.message, file = output.file, append = T)
}
stop(error.message)
}
}
}
time.start <- date()
cat("Program starts on",time.start,"\n")
cat("Loading GWAS summary statistics from",fn,"\n")
if(length(log.file) != 0){
cat("Program starts on",time.start,"\n", file = log.file, append = T)
cat("Loading GWAS summary statistics from",fn,"\n", file = log.file, append = T)
}
gwas.all <- smart.reader(fn)
cat("Data are loaded successfully. Data wrangling starts. \n")
if(length(log.file) != 0){
cat("Data are loaded successfully. Data wrangling starts. \n", file = log.file, append = T)
}
LD.files <- list.files(LD.path)
if(any(grepl(x = LD.files, pattern = ".*_snp_counter.*"))){
snp_counter_file <- LD.files[grep(x = LD.files, pattern = ".*_snp_counter.*")]
snp_list_file <- LD.files[grep(x = LD.files, pattern = ".*_snp_list.*")]
load(file=paste(LD.path, snp_counter_file, sep = "/"))
load(file=paste(LD.path, snp_list_file, sep = "/"))
if("nsnps.list.imputed" %in% ls()){
snps.name.list <- snps.list.imputed.vector
nsnps.list <- nsnps.list.imputed
}
if(is.null(names(nsnps.list))) names(nsnps.list) <- as.character(1:length(nsnps.list))
} else{
error.message <- "It seems this directory does not contain all files needed for HDL. Please check your LD.path again. The current version of HDL only support pre-computed LD reference panels."
if(output.file != ""){
cat(error.message, file = output.file, append = T)
}
stop(error.message)
}
## the Neale's UKB GWAS format ##
if(length(GWAS.type) != 0){
if(GWAS.type == "UKB.Neale"){
dictionary_file <- LD.files[grep(x = LD.files, pattern = "snp.dictionary.*")]
load(file=paste(LD.path, dictionary_file, sep = "/"))
gwas.hdl.df <- gwas.all %>%
inner_join(snp.dictionary %>% filter(rsid %in% snps.name.list), by = "variant") %>%
select(rsid, alt, ref, n_complete_samples, tstat) %>%
rename(SNP = rsid, A1 = alt, A2 = ref, N = n_complete_samples, Z = tstat)
}
}
## non built-in format
if(length(GWAS.type) == 0){
if(length(Z) == 0 & (length(b) == 0 | length(se) == 0)){
error.message <- "Z-score is not available and either b or se is missing. Please check."
if(length(log.file) != 0){
cat(error.message, file = log.file, append = T)
}
stop(error.message)
}
if(length(Z) != 0){
gwas.hdl.df <- gwas.all %>%
rename_(SNP = SNP, A1 = A1, A2 = A2, N = N, Z = Z) %>%
filter(SNP %in% snps.name.list)
} else{
gwas.hdl.df <- gwas.all %>%
rename_(SNP = SNP, A1 = A1, A2 = A2, N = N, b = b, se = se) %>%
filter(SNP %in% snps.name.list)
# b is likely to be OR in stead of log(OR)
if(abs(median(gwas.hdl.df$b) - 1) < 0.1){
cat("Taking log(b) because b is likely to be OR in stead of log(OR). \n")
if(length(log.file) != 0){
cat("Taking log(b) because b is likely to be OR in stead of log(OR). \n", file = log.file, append = T)
}
gwas.hdl.df <- gwas.hdl.df %>% mutate(b = log(b)) %>%
mutate(Z = (b/se)) %>% select(SNP, A1, A2, N, Z)
}
}
}
gwas.hdl.df$Z[!is.finite(gwas.hdl.df$Z)] <- NA
cat("Data wrangling completed. \n")
if(length(log.file) != 0){
cat("Data wrangling completed. \n", file = log.file, append = T)
}
k1 <- sum(gwas.hdl.df$SNP %in% snps.name.list)
k1.percent <- paste("(",round(100*k1 / length(snps.name.list), 2), "%)", sep="")
cat(k1, "out of", length(snps.name.list), k1.percent, "SNPs in reference panel are available in GWAS."," \n")
if(length(log.file) != 0){
cat(k1, "out of", length(snps.name.list), k1.percent, "SNPs in reference panel are available in GWAS."," \n", file = log.file, append = T)
}
if(k1 < length(snps.name.list)*0.99){
cat("More than 1% SNPs in reference panel are missed in GWAS. This missing rate is too high to be used in HDL. Please check. \n")
if(length(log.file) != 0){
cat("More than 1% SNPs in reference panel are missed in GWAS. This missing rate is too high to be used in HDL. Please check. \n", file = log.file, append = T)
}
}
gwas.hdl.df$A1 <- toupper(gwas.hdl.df$A1)
gwas.hdl.df$A2 <- toupper(gwas.hdl.df$A2)
fn.rds <- paste0(output.file, ".hdl.rds")
saveRDS(gwas.hdl.df, fn.rds)
cat("The output is saved to", fn.rds, "\n")
if(length(log.file) != 0){
cat("The output is saved to", fn.rds, "\n", file = log.file, append = T)
}
if(length(log.file) != 0){
cat("The log is saved to", log.file, "\n")
cat("The log is saved to", log.file, "\n", file = log.file, append = T)
}