-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerAnalysis.Rmd
382 lines (322 loc) · 17 KB
/
powerAnalysis.Rmd
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
---
title: "Conducting power analysis for eQTL mapping"
author: "Anthony Hung"
date: "2021-01-19"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
---
# Introduction
Here, we plot power curves under the assumptions of different sample sizes for an eQTL study. We also look at eQTL effect sizes from previously published response-eQTL studies to contextualize the power curves.
Power Curve derivations based on Abhishek Sarkar's calculations: https://users.rcc.uchicago.edu/~aksarkar/hypoxia/reqtl.html
# Load libraries
```{r load}
library(ggplot2)
library(dplyr)
library(tidyverse)
library(ashr)
```
# Plot a power curve
First, we need to specify the heritability for our model.
```{r power curve}
heritability <- 0.16 # median cis-heritability based on Gusev et al. 2016, Wheeler et al. 2016
lambda <- sqrt(heritability / (1-heritability))
```
Next, we make the plot for sample sizes ranging from 3 to 100. Here, vertical lines representing the average .99 quantile cdf eQTL effect sizes across conditions in each of the three external studies analyzed below are also plotted.
```{r curve}
colors <- cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
samp_size <- c(3, 10, 20, 40, 58, 100) # sample sizes for power curve
alpha <- 5e-6 #for FWER of 0.05 and a list of 10000 genes, we have an adjusted alpha (based on bonferroni correction) of 5e-6
power_function <- function(x, n){
pnorm(qnorm(alpha/2) + x * sqrt(n))
}
#The False positive rate for calling an eQTL discovered in one condition (out of two total condition) as a condition specific eQTL (a.k.a. a "response eQTL") is computed by this function.
FPR_function <- function(x, n){
1 - pnorm(qnorm(alpha/2) + x * sqrt(n))
}
p <- ggplot(data.frame(x = c(0, 2)), aes(x = x)) +
stat_function(fun = power_function, args = list(n = 3),
aes(colour = "3", linetype = "Power"), size = 1.5) +
stat_function(fun = FPR_function, args = list(n = 3),
aes(colour = "3", linetype = "dynamic QTL FPR"), size = 1, alpha = 0.7) +
stat_function(fun = power_function, args = list(n = 10),
aes(colour = "10", linetype = "Power"), size = 1.5) +
stat_function(fun = FPR_function, args = list(n = 10),
aes(colour = "10", linetype = "dynamic QTL FPR"), size = 1, alpha = 0.7) +
stat_function(fun = power_function, args = list(n = 30),
aes(colour = "30", linetype = "Power"), size = 1.5) +
stat_function(fun = FPR_function, args = list(n = 30),
aes(colour = "30", linetype = "dynamic QTL FPR"), size = 1, alpha = 0.7) +
stat_function(fun = power_function, args = list(n = 58),
aes(colour = "58", linetype = "Power"), size = 1.5) +
stat_function(fun = FPR_function, args = list(n = 58),
aes(colour = "58", linetype = "dynamic QTL FPR"), size = 1, alpha = 0.7) +
stat_function(fun = power_function, args = list(n = 100),
aes(colour = "100", linetype = "Power"), size = 1.5) +
stat_function(fun = FPR_function, args = list(n = 100),
aes(colour = "100", linetype = "dynamic QTL FPR"), size = 1, alpha = 0.7) +
# annotate("rect", xmin = 0.25340626, xmax = 0.2652244, ymin = -Inf, ymax = Inf, alpha = .2, fill = "blue") +
annotate("segment", x = 0.2593153, xend = 0.2593153, y = -Inf, yend = Inf, alpha = 0.3, color = "blue", size = 1) +
# annotate("rect", xmin = 0.7232331, xmax = 0.8596644, ymin = -Inf, ymax = Inf, alpha = .2, fill = "purple") +
annotate("segment", x = 0.793114125, xend = 0.793114125, y = -Inf, yend = Inf, alpha = 0.3, color = "purple", size = 1) +
#annotate("rect", xmin = 0.6035850, xmax = 0.6097121, ymin = -Inf, ymax = Inf, alpha = .2, fill = "brown") +
annotate("segment", x = 0.605953025, xend = 0.605953025, y = -Inf, yend = Inf, alpha = 0.3, color = "brown", size = 1) +
# stat_function(fun = function(x) x^2/(1+x^2),
# aes(colour = "heritability"), size = 1, alpha = 0.5) +
scale_x_continuous(name = "Standardized Effect Size",
limits=c(0, 2)) +
scale_y_continuous(name = "Power",
limits = c(0,1)) +
ggtitle("eQTL Power Curves") +
scale_colour_manual("Sample Size", breaks = c("3", "10", "30", "58", "100"), values = colors) +
scale_linetype_manual("Curve Type", breaks = c("Power", "dynamic QTL FPR"), values = c("dynamic QTL FPR" = "dotted", "Power" = "solid")) +
theme_bw() +
geom_hline(yintercept = .8, linetype = "dashed", color = "red")
p
```
# Import outside data on eQTLs to see distributions of standardized effect sizes
After plotting our power curve, we would like to get a sense of the range of standardized effect sizes in several previously published response-eQTL papers.
```{r ecdf function}
#This function creates an empirical cdf from a vector of effect sizes and prints out several quantiles of the ecdf
get_ecdf <- function(vector_of_SES){
SES_ecdf <- ecdf(vector_of_SES)
print(quantile(SES_ecdf, c(.75, .95, .99)))
print(plot(SES_ecdf, ylim = c(-2,2)))
}
```
# Ward et al hypoxia response eQTL study
```{r hypoxia}
#michelle hypoxia reQTL study
conditionA <- as_tibble(read.table('data/poweranalysis/ward_et_al_2020/A-adjust.txt.gz', fill = TRUE, stringsAsFactors = FALSE))
conditionB <- as_tibble(read.table('data/poweranalysis/ward_et_al_2020/B-adjust.txt.gz', fill = TRUE, stringsAsFactors = FALSE))
conditionC <- as_tibble(read.table('data/poweranalysis/ward_et_al_2020/C-adjust.txt.gz', fill = TRUE, stringsAsFactors = FALSE))
conditionD <- as_tibble(read.table('data/poweranalysis/ward_et_al_2020/D-adjust.txt.gz', fill = TRUE, stringsAsFactors = FALSE))
zz <- gzfile('data/poweranalysis/ward_et_al_2020/reqtls.txt.gz','rt')
hypoxia <- as_tibble(read.table(zz, fill = TRUE, stringsAsFactors = FALSE))
names(hypoxia) <- hypoxia %>%
dplyr::slice(1) %>%
unlist()
hypoxia <- hypoxia %>%
dplyr::slice(-1)
## find SNP-gene pairs that were tested in all conditions and compute allelic effect sizes
common <- sort(Reduce(intersect, list(paste0(conditionA$TEST.SNP.CHROM, conditionA$TEST.SNP.POS, " ", conditionA$PHENO),
paste0(conditionB$TEST.SNP.CHROM, conditionB$TEST.SNP.POS, " ", conditionB$PHENO),
paste0(conditionC$TEST.SNP.CHROM, conditionC$TEST.SNP.POS, " ", conditionC$PHENO),
paste0(conditionD$TEST.SNP.CHROM, conditionD$TEST.SNP.POS, " ", conditionD$PHENO)
)))
length(common)
conditionA <- conditionA %>%
dplyr::mutate(temp = paste0(TEST.SNP.CHROM, TEST.SNP.POS, " ", PHENO)) %>%
dplyr::filter(temp %in% common) %>%
dplyr::mutate(slope = log(ALPHA/BETA))%>%
dplyr::mutate(zscore = sign(ALPHA - BETA) * sqrt(qchisq(p = p_adjusted + 1e-30, df = 1))) %>%
dplyr::select(-temp)
conditionB <- conditionB %>%
dplyr::mutate(temp = paste0(TEST.SNP.CHROM, TEST.SNP.POS, " ", PHENO)) %>%
dplyr::filter(temp %in% common) %>%
dplyr::mutate(slope = log(ALPHA/BETA))%>%
dplyr::mutate(zscore = sign(ALPHA - BETA) * sqrt(qchisq(p = p_adjusted + 1e-30, df = 1))) %>%
dplyr::select(-temp)
conditionC <- conditionC %>%
dplyr::mutate(temp = paste0(TEST.SNP.CHROM, TEST.SNP.POS, " ", PHENO)) %>%
dplyr::filter(temp %in% common) %>%
dplyr::mutate(slope = log(ALPHA/BETA))%>%
dplyr::mutate(zscore = sign(ALPHA - BETA) * sqrt(qchisq(p = p_adjusted + 1e-30, df = 1))) %>%
dplyr::select(-temp)
conditionD <- conditionD %>%
dplyr::mutate(temp = paste0(TEST.SNP.CHROM, TEST.SNP.POS, " ", PHENO)) %>%
dplyr::filter(temp %in% common) %>%
dplyr::mutate(slope = log(ALPHA/BETA))%>%
dplyr::mutate(zscore = sign(ALPHA - BETA) * sqrt(qchisq(p = p_adjusted + 1e-30, df = 1))) %>%
dplyr::select(-temp)
dim(conditionA)
dim(conditionB)
dim(conditionC)
dim(conditionD)
# For each condiiton in the study, compute the empirical CDF and quantiles after obtaining the effect sizes by dividing the zscore by the sqrt of the number of individuals in the study (15).
#conditionA
get_ecdf(conditionA$zscore/sqrt(15))
#conditionB
get_ecdf(conditionB$zscore/sqrt(15))
#conditionC
get_ecdf(conditionC$zscore/sqrt(15))
#conditionD
get_ecdf(conditionD$zscore/sqrt(15))
```
## Alasoo et al 2019 IFN gamma and salmonella reQTL study
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6548559/#FN5
data on Zenodo: https://zenodo.org/record/1158560
Run the following chunk to download the data to the repository.
```{bash, eval=FALSE}
cd data/poweranalysis/alasoo_et_al/
#load in data from eQTL analysis downloaded using wget from zenodo
wget https://zenodo.org/record/1158560/files/RNA_FastQTL_SL1344_500kb_pvalues.sorted.txt.gz
wget https://zenodo.org/record/1158560/files/RNA_FastQTL_naive_500kb_pvalues.sorted.txt.gz
wget https://zenodo.org/record/1158560/files/RNA_FastQTL_IFNg_SL1344_500kb_pvalues.sorted.txt.gz
wget https://zenodo.org/record/1158560/files/RNA_FastQTL_IFNg_500kb_pvalues.sorted.txt.gz
```
```{r ifn_salmonella}
# load data into R
n_sorted <- as_tibble(read.table("data/poweranalysis/alasoo_et_al/RNA_FastQTL_naive_500kb_pvalues.sorted.txt.gz"))
sal_sorted <- as_tibble(read.table("data/poweranalysis/alasoo_et_al/RNA_FastQTL_SL1344_500kb_pvalues.sorted.txt.gz"))
IFN_sorted <- as_tibble(read.table("data/poweranalysis/alasoo_et_al/RNA_FastQTL_IFNg_500kb_pvalues.sorted.txt.gz"))
salIFN_sorted <- as_tibble(read.table("data/poweranalysis/alasoo_et_al/RNA_FastQTL_IFNg_SL1344_500kb_pvalues.sorted.txt.gz"))
#for sorted data,
#V1 = gene
#V2 = chromosome
#V3 = location of SNP
#V4 = SNP
#V5 = distance to gene from SNP?
#V6 = p-value
#v7 = slope of regression
#We can use the p-value to obtain the zscore (pvalue -> Zscore)
#then use slopes and zscores to obtain SE (slope/zscore = SE)
n_sorted <- n_sorted %>%
dplyr::filter(V6 != 1) %>%
dplyr::mutate(zscore = if_else(V7 < 0, true = qnorm(V6/2), false = qnorm(1-V6/2))) %>%
dplyr::mutate(SE = V7/zscore)
sal_sorted <- sal_sorted %>%
dplyr::filter(V6 != 1) %>%
dplyr::mutate(zscore = if_else(V7 < 0, true = qnorm(V6/2), false = qnorm(1-V6/2))) %>%
dplyr::mutate(SE = V7/zscore)
IFN_sorted <- IFN_sorted %>%
dplyr::filter(V6 != 1) %>%
dplyr::mutate(zscore = if_else(V7 < 0, true = qnorm(V6/2), false = qnorm(1-V6/2))) %>%
dplyr::mutate(SE = V7/zscore)
salIFN_sorted <- salIFN_sorted %>%
dplyr::filter(V6 != 1) %>%
dplyr::mutate(zscore = if_else(V7 < 0, true = qnorm(V6/2), false = qnorm(1-V6/2))) %>%
dplyr::mutate(SE = V7/zscore)
# use ashr to estimate true effect sizes from the data in order to compute SES
n_sorted_ash_out <- ash(betahat = n_sorted$V7, sebetahat = n_sorted$SE, df = 84)
sal_sorted_ash_out <- ash(betahat = sal_sorted$V7, sebetahat = sal_sorted$SE, df = 84)
IFN_sorted_ash_out <- ash(betahat = IFN_sorted$V7, sebetahat = IFN_sorted$SE, df = 84)
salIFN_sorted_ash_out <- ash(betahat = salIFN_sorted$V7, sebetahat = salIFN_sorted$SE, df = 84)
#add columns to datasets corresponding to the SES computed from ash betas and sds
n_sorted$SES <- n_sorted_ash_out$result$PosteriorMean/n_sorted_ash_out$result$PosteriorSD
sal_sorted$SES <- sal_sorted_ash_out$result$PosteriorMean/sal_sorted_ash_out$result$PosteriorSD
IFN_sorted$SES <- IFN_sorted_ash_out$result$PosteriorMean/IFN_sorted_ash_out$result$PosteriorSD
salIFN_sorted$SES <- salIFN_sorted_ash_out$result$PosteriorMean/salIFN_sorted_ash_out$result$PosteriorSD
#Run the ecdf function separately on each of the different conditions
#naive
get_ecdf(n_sorted$SES)
get_ecdf(n_sorted_ash_out$result$PosteriorMean)
#salmonella condition
get_ecdf(sal_sorted$SES)
#IFN treated condition
get_ecdf(IFN_sorted$SES)
#salmonella and ifn treated condition
get_ecdf(salIFN_sorted$SES)
```
## Caliskan et al 2015 Rhinovirus reQTL study
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4395341/
Data output from MatrixeQTL kindly provided by Dr. Minal Caliskan.
```{r rhinovirus}
rhinovirus_data <- as.tibble(read.table("data/poweranalysis/caliskan_et_al/eQTL_results_cis_response.txt.gz",
sep = "\t",
header = TRUE))
#obtain standard errors by using beta and t.stat
rhinovirus_data <- rhinovirus_data %>%
dplyr::mutate(se = beta/t.stat)
#add column to denote which condition the association comes from; separate into two datasets
rhinovirus_data <- rhinovirus_data %>%
mutate(condition = ifelse(grepl("_f", SNP), "f", "r"))
rhinovirus_data_f <- rhinovirus_data %>%
dplyr::filter(condition == "f")
rhinovirus_data_r <- rhinovirus_data %>%
dplyr::filter(condition == "r")
# use ashr to estimate true effect sizes from the data in order to compute SES (separately for each half of the data)
rhinovirus_f_ashr_out <- ash(betahat = rhinovirus_data_f$beta, sebetahat = rhinovirus_data_f$se, df = 96)
rhinovirus_r_ashr_out <- ash(betahat = rhinovirus_data_r$beta, sebetahat = rhinovirus_data_r$se, df = 96)
#add column for SES (based on ash output)
rhinovirus_data_f$SES <- rhinovirus_f_ashr_out$result$PosteriorMean/rhinovirus_f_ashr_out$result$PosteriorSD
rhinovirus_data_r$SES <- rhinovirus_r_ashr_out$result$PosteriorMean/rhinovirus_r_ashr_out$result$PosteriorSD
#Run the ecdf function separately on each of the different conditions
#uninfected
get_ecdf(rhinovirus_data_f$SES)
get_ecdf(rhinovirus_f_ashr_out$result$PosteriorMean)
#rhinovirus
get_ecdf(rhinovirus_data_r$SES)
get_ecdf(rhinovirus_r_ashr_out$result$PosteriorMean)
```
# Number of genes meeting the SES threshold
For the supplementary table, we are interested in the number of eQTLs that meet the SES threshold in each study for each sample size.
```{r SES Threshold}
# Compute the SES thresholds for 80% power at each sample size
alpha <- 5e-6 #for FWER of 0.05 (assuming 10,000 genes)
#for Power = 0.8, what is the Standardized effect size?
#n = 100
n100_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(100)
#n = 58
n58_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(58)
#n = 30
n30_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(30)
#n = 10
n10_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(10)
#Ward et al study
# See how many genes have at least one test that meets each of the 3 SES thresholds determined above in each condition.
n_egenes_ward <- matrix(NA, nrow = 4, ncol = 4)
colnames(n_egenes_ward) <- c("n=100", "n=58", "n=30", "n=10")
rownames(n_egenes_ward) <- c("conditionA", "conditionB", "conditionC", "conditionD")
i <- 1
for(condition in list(conditionA, conditionB, conditionC, conditionD)){
j <- 1
for(threshold in c(n100_ses_threshold, n58_ses_threshold, n30_ses_threshold, n10_ses_threshold)){
n_egenes_ward[i,j] <- length(unique(condition %>% dplyr::filter(zscore/sqrt(15) > threshold | zscore/sqrt(15) < -threshold) %>% pull(PHENO)))
j <- j + 1
}
i <- i + 1
}
#Alasoo et al study
# See how many genes have at least one test that meets each of the 3 SES thresholds determined above in each condition.
alpha <- 0.05/15786 #for FWER of 0.05 (assuming 15786 genes)
#for Power = 0.8, what is the SES?
#n = 100
n100_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(100)
#n = 58
n58_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(58)
#n = 30
n30_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(30)
#n = 10
n10_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(10)
n_egenes_alasoo <- matrix(NA, nrow = 4, ncol = 4)
colnames(n_egenes_alasoo) <- c("n=100", "n=58", "n=30", "n=10")
rownames(n_egenes_alasoo) <- c("naive", "IFN", "salmonella", "IFN + salmonella")
i <- 1
for(condition in list(n_sorted, IFN_sorted, sal_sorted, salIFN_sorted)){
j <- 1
for(threshold in c(n100_ses_threshold, n58_ses_threshold, n30_ses_threshold, n10_ses_threshold)){
n_egenes_alasoo[i,j] <- length(unique(condition %>% dplyr::filter(SES > threshold | SES < -threshold) %>% pull(V1)))
j <- j + 1
}
i <- i + 1
}
#Caliskan et al study
# See how many genes have at least one test that meets each of the 3 SES thresholds determined above in each condition.
alpha <- 0.05/length(unique(rhinovirus_data_f$gene)) #for FWER of 0.05 (assuming 10893 genes)
#for Power = 0.8, what is the SES?
#n = 100
n100_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(100)
#n = 58
n58_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(58)
#n = 30
n30_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(30)
#n = 10
n10_ses_threshold <- (qnorm(.8) - qnorm(alpha/2))/sqrt(10)
n_egenes_caliskan <- matrix(NA, nrow = 2, ncol = 4)
colnames(n_egenes_caliskan) <- c("n=100", "n=58", "n=30", "n=10")
rownames(n_egenes_caliskan) <- c("f", "r")
i <- 1
for(condition in list(rhinovirus_data_f, rhinovirus_data_r)){
j <- 1
for(threshold in c(n100_ses_threshold, n58_ses_threshold, n30_ses_threshold, n10_ses_threshold)){
n_egenes_caliskan[i,j] <- length(unique(condition %>% dplyr::filter(SES > threshold | SES < -threshold) %>% pull(gene)))
j <- j + 1
}
i <- i + 1
}
#These values populate the supplementary table
n_egenes_ward
n_egenes_alasoo
n_egenes_caliskan
```