-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMRPRESSO.r
247 lines (210 loc) · 9.86 KB
/
MRPRESSO.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
# Set working directory
setwd("../UKB/IS-EWAS&MR/MR_PRESSO")
workdir <- getwd()
# Load packages
pkgs <- c(
"dplyr", "tidyr", "readxl", "readr", "data.table", "tidyverse", "survival",
"gwasglue", "gwasvcf", "MRInstruments", "VariantAnnotation",
"TwoSampleMR", "MRPRESSO", "RadialMR", "mr.raps", "MendelianRandomization"
)
inst <- lapply(pkgs, require, character.only = TRUE)
# Load data
plink_freq <- read.table(
"../UKB/IS-EWAS&MR/data_archived/Plink/your_output.frq",
header = TRUE
)
# Check the status of the API
ieugwasr::api_status()
ieugwasr::get_opengwas_jwt()
ieugwasr::user()
# 1 IEU: GWAS ID----
## 1.1 Exposure data P<5*10-8 ----
IEU_ID <- read.csv("../UKB/IS-EWAS&MR/data_archived/GWAS_Exposure_ID.csv") %>%
filter(GWAS_ID != "") %>%
dplyr::pull(GWAS_ID) %>%
as.character()
# Create empty data frame
result_combine_f <- data.frame()
# MR-PRESSO
for (i in IEU_ID) {
print(paste("Current loop index:", i))
# Load Exposure Data
exposure_dat <- read.csv(paste0(
"../UKB/IS-EWAS&MR/MR_10-8/Exposure_Resources/", i, "_clumped.csv"
))
trait1_exposure_data_clumped <- exposure_dat
if (!is.null(exposure_dat)) {
# Load Outcome Data
outcome_dat <- TwoSampleMR::extract_outcome_data(
snps = trait1_exposure_data_clumped$SNP,
outcomes = "ebi-a-GCST006908",
proxies = TRUE, # LD proxies
# By default if a particular requested SNP is not present in the outcome GWAS then a SNP (proxy) that is in LD with the requested SNP (target) will be searched for instead.
# LD proxies are defined using 1000 genomes European sample data.
# The effect of the proxy SNP on the outcome is returned, along with the proxy SNP, the effect allele of the proxy SNP, and the corresponding allele (in phase) for the target SNP.
rsq = 0.8,
align_alleles = 1, # Try to align tag alleles to target alleles
palindromes = 1, # If TRUE, palindromic SNPs will be flipped to match the effect allele of the target SNP
maf_threshold = 0.3, # Minimum MAF for proxy SNPs
opengwas_jwt = ieugwasr::get_opengwas_jwt(),
splitsize = 10000, # Number of SNPs to process at a time
proxy_splitsize = 500 # Number of proxy SNPs to process at a time
)
if (!is.null(outcome_dat)) {
# Harmonise data
trait1_trait2_dat <- TwoSampleMR::harmonise_data(
exposure_dat = trait1_exposure_data_clumped,
outcome_dat = outcome_dat,
action = 2
)
# Perform MR
trait1_trait2_results <- TwoSampleMR::mr(
trait1_trait2_dat,
parameters = TwoSampleMR::default_parameters()
)
trait1_trait2_results_with_or <- TwoSampleMR::generate_odds_ratios(
trait1_trait2_results
)
result_combine <- filter(
trait1_trait2_results_with_or,
method == "Inverse variance weighted"
)
# MR-PRESSO needs >3 SNPs
if (nrow(trait1_trait2_dat) > 2) {
trait1_trait2_run <- TwoSampleMR::run_mr_presso(
dat = trait1_trait2_dat,
NbDistribution = 1000, # 8000
SignifThreshold = 0.05
)
result_combine$MR_PRESSO_Global_pval <-
trait1_trait2_run[[1]]$`MR-PRESSO results`$`Global Test`$Pvalue
result_combine$Main_MR_results.Effect_Raw <-
trait1_trait2_run[[1]]$`Main MR results`$`Causal Estimate`[1]
result_combine$Main_MR_results.Sd_Raw <-
trait1_trait2_run[[1]]$`Main MR results`$Sd[1]
result_combine$Main_MR_results.pval_Raw <-
trait1_trait2_run[[1]]$`Main MR results`$`P-value`[1]
result_combine$Main_MR_results.Effect_Corre <-
trait1_trait2_run[[1]]$`Main MR results`$`Causal Estimate`[2]
result_combine$Main_MR_results.Sd_Corre <-
trait1_trait2_run[[1]]$`Main MR results`$Sd[2]
result_combine$Main_MR_results.pval_Corre <-
trait1_trait2_run[[1]]$`Main MR results`$`P-value`[2]
result_combine$MR_PRESSO.Results.Distortion.pval <-
trait1_trait2_run[[1]]$`MR-PRESSO results`$`Distortion Test`$Pvalue
if (is.null(result_combine$MR_PRESSO.Results.Distortion.pval)) {
result_combine$MR_PRESSO.Results.Distortion.pval <- NA
}
result_combine$MR_PRESSO_Global_pval <-
as.character(result_combine$MR_PRESSO_Global_pval)
result_combine$MR_PRESSO.Results.Distortion.pval <-
as.character(result_combine$MR_PRESSO.Results.Distortion.pval)
result_combine_f <- dplyr::bind_rows(
result_combine_f, result_combine
)
write.csv(
result_combine_f,
"../UKB/IS-EWAS&MR/MR_PRESSO/Results_10-8/result_combine_run.csv",
row.names = FALSE
)
}
}
}
}
# 2 FinnGen: PhenoCode----
## 2.1 Exposure data P<5*10-8----
PhenoCode <- read.csv(
"../UKB/IS-EWAS&MR/data_archived/FinnGen/PhenoCode_5e-8.csv"
) %>%
filter(PhenoCode != "") %>%
dplyr::pull(PhenoCode) %>%
as.character()
# Create empty data frame
result_combine_f <- data.frame()
# MR-PRESSO
for (i in PhenoCode) {
print(paste("Current loop index:", i))
# Load Exposure Data
exposure_dat <- read.csv(paste0(
"../UKB/IS-EWAS&MR/MR/EWAS 68-Forward-FinnGen/MR_10-8/Exposure_Resources/",
i,
"_clumped.csv"
))
trait1_exposure_data_clumped <- exposure_dat
if (!is.null(exposure_dat)) {
# Load Outcome Data
outcome_dat <- TwoSampleMR::extract_outcome_data(
snps = trait1_exposure_data_clumped$SNP,
outcomes = "ebi-a-GCST006908",
proxies = TRUE, # LD proxies
# By default if a particular requested SNP is not present in the outcome GWAS then a SNP (proxy) that is in LD with the requested SNP (target) will be searched for instead.
# LD proxies are defined using 1000 genomes European sample data.
# The effect of the proxy SNP on the outcome is returned, along with the proxy SNP, the effect allele of the proxy SNP, and the corresponding allele (in phase) for the target SNP.
rsq = 0.8,
align_alleles = 1, # Try to align tag alleles to target alleles
palindromes = 1, # If TRUE, palindromic SNPs will be flipped to match the effect allele of the target SNP
maf_threshold = 0.3, # Minimum MAF for proxy SNPs
opengwas_jwt = ieugwasr::get_opengwas_jwt(),
splitsize = 10000, # Number of SNPs to process at a time
proxy_splitsize = 500 # Number of proxy SNPs to process at a time
)
if (!is.null(outcome_dat)) {
# Harmonise data
trait1_trait2_dat <- TwoSampleMR::harmonise_data(
exposure_dat = trait1.exposure_data_clumped,
outcome_dat = outcome_dat,
action = 2
)
# Perform MR
trait1_trait2_results <- TwoSampleMR::mr(
trait1_trait2_dat,
parameters = TwoSampleMR::default_parameters()
)
trait1_trait2_results_with_or <-
TwoSampleMR::generate_odds_ratios(trait1_trait2_results)
result_combine <- filter(
trait1_trait2_results_with_or,
method == "Inverse variance weighted"
)
# MR-PRESSO needs >3 SNPs
if (nrow(trait1_trait2_dat) > 2) {
run_mr_presso <- TwoSampleMR::run_mr_presso(
dat = trait1_trait2_dat,
NbDistribution = 8000, # 8000
SignifThreshold = 0.05
)
result_combine$MR_PRESSO_Global_pval <-
run_mr_presso[[1]]$`MR-PRESSO results`$`Global Test`$Pvalue
result_combine$Main_MR_results.Effect_Raw <-
run_mr_presso[[1]]$`Main MR results`$`Causal Estimate`[1]
result_combine$Main_MR_results.Sd_Raw <-
run_mr_presso[[1]]$`Main MR results`$Sd[1]
result_combine$Main_MR_results.pval_Raw <-
run_mr_presso[[1]]$`Main MR results`$`P-value`[1]
result_combine$Main_MR_results.Effect_Corre <-
run_mr_presso[[1]]$`Main MR results`$`Causal Estimate`[2]
result_combine$Main_MR_results.Sd_Corre <-
run_mr_presso[[1]]$`Main MR results`$Sd[2]
result_combine$Main_MR_results.pval_Corre <-
run_mr_presso[[1]]$`Main MR results`$`P-value`[2]
result_combine$MR_PRESSO.Results.Distortion.pval <-
run_mr_presso[[1]]$`MR-PRESSO results`$`Distortion Test`$Pvalue
if (is.null(result_combine$MR_PRESSO.Results.Distortion.pval)) {
result_combine$MR_PRESSO.Results.Distortion.pval <- NA
}
result_combine$MR_PRESSO_Global_pval <-
as.character(result_combine$MR_PRESSO_Global_pval)
result_combine$MR_PRESSO.Results.Distortion.pval <-
as.character(result_combine$MR_PRESSO.Results.Distortion.pval)
result_combine_f <- dplyr::bind_rows(
result_combine_f, result_combine
)
write.csv(
result_combine_f,
"../UKB/IS-EWAS&MR/MR_PRESSO/Results_10-8/result_combine_run.csv",
row.names = FALSE
)
}
}
}
}