/
other_formats.R
275 lines (256 loc) · 8.67 KB
/
other_formats.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
#' Convert TwoSampleMR format to MendelianRandomization format
#'
#' The MendelianRandomization package offers MR methods that can
#' be used with the same data used in the TwoSampleMR package. This
#' function converts from the TwoSampleMR format to the MRInput class.
#'
#' @param dat Output from the [harmonise_data()] function.
#' @param get_correlations Default `FALSE`. If `TRUE` then extract the LD matrix for the SNPs from the European 1000 genomes data on the MR-Base server.
#' @param pop If `get_correlations` is `TRUE` then use the following
#'
#' @export
#' @return List of MRInput objects for each exposure/outcome combination
dat_to_MRInput <- function(dat, get_correlations=FALSE, pop="EUR")
{
if (!requireNamespace("MendelianRandomization", quietly = TRUE)) {
stop(
"Package \"MendelianRandomization\" must be installed to use this function.",
call. = FALSE
)
}
out <- plyr::dlply(dat, c("exposure", "outcome"), function(x)
{
x <- plyr::mutate(x)
message("Converting:")
message(" - exposure: ", x$exposure[1])
message(" - outcome: ", x$outcome[1])
if(get_correlations)
{
message(" - obtaining LD matrix")
ld <- ld_matrix(unique(x$SNP), pop=pop)
out <- harmonise_ld_dat(x, ld)
if(is.null(out))
{
return(NULL)
}
x <- out$x
ld <- out$ld
MendelianRandomization::mr_input(
bx = x$beta.exposure,
bxse = x$se.exposure,
by = x$beta.outcome,
byse = x$se.outcome,
exposure = x$exposure[1],
outcome = x$outcome[1],
snps = x$SNP,
effect_allele=x$effect_allele.exposure,
other_allele=x$other_allele.exposure,
eaf = x$eaf.exposure,
correlation = ld
)
} else {
MendelianRandomization::mr_input(
bx = x$beta.exposure,
bxse = x$se.exposure,
by = x$beta.outcome,
byse = x$se.outcome,
exposure = x$exposure[1],
outcome = x$outcome[1],
snps = x$SNP,
effect_allele=x$effect_allele.exposure,
other_allele=x$other_allele.exposure,
eaf = x$eaf.exposure
)
}
})
return(out)
}
#' Harmonise LD matrix against summary data
#'
#' LD matrix returns with rsid_ea_oa identifiers. Make sure that they are oriented to the same effect allele as the summary dataset. Summary dataset can be exposure dataset or harmonised dartaset.
#'
#' @param x Exposure dataset or harmonised dataset
#' @param ld Output from [ld_matrix()]
#'
#' @export
#' @return List of exposure dataset and harmonised LD matrix
harmonise_ld_dat <- function(x, ld)
{
snpnames <- do.call(rbind, strsplit(rownames(ld), split="_"))
i1 <- snpnames[,1] %in% x$SNP
ld <- ld[i1,i1]
snpnames <- snpnames[i1,]
i2 <- x$SNP %in% snpnames[,1]
x <- x[i2,]
stopifnot(all(snpnames[,1] == x$SNP))
x$effect_allele.exposure <- as.character(x$effect_allele.exposure)
x$other_allele.exposure <- as.character(x$other_allele.exposure)
# Set1 x and ld alleles match
snpnames <- data.frame(snpnames, stringsAsFactors=FALSE)
snpnames <- merge(subset(x, select=c(SNP, effect_allele.exposure, other_allele.exposure)), snpnames, by.x="SNP", by.y="X1")
snpnames <- snpnames[match(x$SNP, snpnames$SNP),]
snpnames$keep <- (snpnames$X2 == snpnames$effect_allele.exposure & snpnames$X3 == snpnames$other_allele.exposure) |
(snpnames$X3 == snpnames$effect_allele.exposure & snpnames$X2 == snpnames$other_allele.exposure)
# What happens if everything is gone?
if(nrow(x) == 0)
{
message(" - none of the SNPs could be aligned to the LD reference panel")
return(NULL)
}
if(any(!snpnames$keep))
{
message(" - the following SNPs could not be aligned to the LD reference panel: \n- ", paste(subset(snpnames, !keep)$SNP, collapse="\n - "))
}
snpnames$flip1 <- snpnames$X2 != snpnames$effect_allele.exposure
x <- subset(x, SNP %in% snpnames$SNP)
temp1 <- x$effect_allele.exposure[snpnames$flip1]
temp2 <- x$other_allele.exposure[snpnames$flip1]
x$beta.exposure[snpnames$flip1] <- x$beta.exposure[snpnames$flip1] * -1
x$beta.outcome[snpnames$flip1] <- x$beta.outcome[snpnames$flip1] * -1
x$effect_allele.exposure[snpnames$flip1] <- temp2
x$other_allele.exposure[snpnames$flip1] <- temp1
rownames(ld) <- snpnames$SNP
colnames(ld) <- snpnames$SNP
if(any(!snpnames$keep))
{
message("Removing ", sum(!snpnames$keep), " variants due to harmonisation issues")
ld <- ld[snpnames$keep, snpnames$keep]
x <- x[snpnames$keep, ]
}
return(list(x=x, ld=ld))
}
#' Wrapper for MR-PRESSO
#'
#' See <https://github.com/rondolab/MR-PRESSO> for more details.
#'
#' @param dat Output from [harmonise_data()].
#' @param NbDistribution Number of bootstrap replications. The default is `1000`.
#' @param SignifThreshold Outlier significance threshold. The default is `0.05`.
#'
#' @export
#' @return List of results for every exposure/outcome combination
run_mr_presso <- function(dat, NbDistribution = 1000, SignifThreshold = 0.05)
{
dat <- subset(dat, mr_keep)
d <- subset(dat, !duplicated(paste(id.exposure, " - ", id.outcome)), select=c(exposure, outcome, id.exposure, id.outcome))
res <- list()
attributes(res)$id.exposure <- d$id.exposure
attributes(res)$id.outcome <- d$id.outcome
attributes(res)$exposure <- d$exposure
attributes(res)$outcome <- d$outcome
for(j in 1:nrow(d))
{
x <- subset(dat, exposure == d$exposure[j] & outcome == d$outcome[j])
message(x$exposure[1], " - ", x$outcome[1])
res[[j]] <- MRPRESSO::mr_presso(BetaOutcome = "beta.outcome", BetaExposure = "beta.exposure", SdOutcome = "se.outcome", SdExposure = "se.exposure", OUTLIERtest = TRUE, DISTORTIONtest = TRUE, data = x, NbDistribution = NbDistribution, SignifThreshold = SignifThreshold)
}
return(res)
}
#' Convert dat to RadialMR format
#'
#' Creates a list of RadialMR format datasets for each exposure-outcome pair.
#'
#' @param dat Output from [harmonise_data()].
#'
#' @export
#' @return List of RadialMR format datasets
dat_to_RadialMR <- function(dat)
{
out <- plyr::dlply(dat, c("exposure", "outcome"), function(x)
{
x <- plyr::mutate(x)
message("Converting:")
message(" - exposure: ", x$exposure[1])
message(" - outcome: ", x$outcome[1])
d <- subset(x, mr_keep)
d <- RadialMR::format_radial(d$beta.exposure, d$beta.outcome, d$se.exposure, d$se.outcome, RSID=d$SNP)
return(d)
})
return(out)
}
#' Radial IVW analysis
#'
#' @param b_exp Vector of genetic effects on exposure.
#' @param b_out Vector of genetic effects on outcome.
#' @param se_exp Standard errors of genetic effects on exposure.
#' @param se_out Standard errors of genetic effects on outcome.
#' @param parameters List of parameters.
#'
#' @export
#' @return List with the following elements:
#' \describe{
#' \item{b}{causal effect estimate}
#' \item{se}{standard error}
#' \item{pval}{p-value}
#' }
mr_ivw_radial <- function(b_exp, b_out, se_exp, se_out, parameters=default_parameters())
{
if (sum(!is.na(b_exp) & !is.na(b_out) & !is.na(se_exp) &
!is.na(se_out)) < 2)
return(list(b = NA, se = NA, pval = NA, nsnp = NA))
d <- RadialMR::format_radial(BXG=b_exp, BYG=b_out, seBXG=se_exp, seBYG=se_out, RSID=1:length(b_exp))
out <- RadialMR::ivw_radial(d, alpha=0.05, weights=3)
b <- out$coef[1,1]
se <- out$coef[1,2]
pval <- 2 * stats::pnorm(abs(b/se), lower.tail = FALSE)
Q_df <- out$df
Q <- out$qstatistic
Q_pval <- stats::pchisq(Q, Q_df, lower.tail=FALSE)
return(list(b = b, se = se, pval = pval, nsnp = length(b_exp),
Q = Q, Q_df = Q_df, Q_pval = Q_pval))
}
#' Perform MRMix analysis on harmonised dat object
#'
#' See <https://github.com/gqi/MRMix> for more details.
#'
#' @param dat Output from [harmonise_data()]. Ensures that no eaf.exposure values are missing.
#'
#' @export
#' @return List of results, with one list item for every exposure/outcome pair in dat object
run_mrmix <- function(dat)
{
plyr::dlply(dat, c("id.exposure", "id.outcome"), function(x)
{
message("Analysing ", x$id.exposure[1], " against ", x$id.outcome[1])
if(grepl("log odds", x$units.exposure[1]))
{
xunit <- "binary"
} else if(grepl("^SD", x$units.exposure[1]))
{
xunit <- "n"
} else {
xunit <- "continuous"
}
if(grepl("log odds", x$units.outcome[1]))
{
yunit <- "binary"
} else if(grepl("^SD", x$units.outcome[1]))
{
yunit <- "n"
} else {
yunit <- "continuous"
}
index <- is.na(x$eaf.exposure)
if(any(index))
{
warning(paste0(x$id.exposure[1], ".", x$id.outcome[1], " - Some eaf.exposure values are missing, using eaf.outcome in their place"))
x$eaf.exposure[index] <- x$eaf.outcome[index]
}
l <- MRMix::standardize(
x$beta.exposure,
x$beta.outcome,
x$se.exposure,
x$se.outcome,
xunit,
yunit,
x$samplesize.exposure,
x$samplesize.outcome,
x$eaf.exposure
)
x$beta.exposure <- l$betahat_x_std
x$beta.outcome <- l$betahat_y_std
x$se.exposure <- l$sx_std
x$se.outcome <- l$sy_std
MRMix::MRMix(x$beta.exposure, x$beta.outcome, x$se.exposure, x$se.outcome)
})
}