Repository navigation
Expand file tree
/
Copy pathbrainseq_development.Rmd
More file actions
executable file
·399 lines (312 loc) · 12.5 KB
/
Copy pathbrainseq_development.Rmd
File metadata and controls
executable file
·399 lines (312 loc) · 12.5 KB
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
---
title: "DLPFC brain development"
author: "Leo"
date: "11/17/2017"
output:
html_document:
code_folding: show
toc: true
toc_float: true
---
## Setup
```{r 'setup'}
library('SummarizedExperiment')
library('recount')
library('edge')
library('devtools')
library('ggplot2')
library('reshape2')
## Load gene data and keep controls only
load('../expr_cutoff/rse_gene.Rdata')
rse_gene <- rse_gene[, colData(rse_gene)$Dx == 'Control']
## Add mds info
load('../genotype_data/mds_extracted_from_BrainSeq_Phase2_RiboZero_Genotypes_n546.Rdata')
m <- match(colData(rse_gene)$BrNum, rownames(mds))
## Drop those that don't match
colData(rse_gene)$BrNum[which(is.na(m))]
rse_gene <- rse_gene[, !is.na(m)]
colData(rse_gene) <- cbind(colData(rse_gene), mds[m[!is.na(m)], ])
rm(m, mds)
## Set as factor
colData(rse_gene)$Region <- relevel(factor(colData(rse_gene)$Region), 'DLPFC')
colData(rse_gene)$Race <- relevel(factor(colData(rse_gene)$Race), ref = 'CAUC')
colData(rse_gene)$Sex <- relevel(factor(colData(rse_gene)$Sex), ref = 'F')
## Add age linear splines
fetal <- ifelse(colData(rse_gene)$Age < 0, 1,0)
birth <- colData(rse_gene)$Age
birth[birth < 0] <- 0 # linear spline
infant <- colData(rse_gene)$Age - 1
infant[infant < 0] <- 0 # linear spline
child <- colData(rse_gene)$Age - 10
child[child < 0] <- 0 # linear spline
teen <- colData(rse_gene)$Age - 20
teen[teen < 0] <- 0 # linear spline
adult <- colData(rse_gene)$Age - 50
adult[adult < 0] <- 0 # linear spline
colData(rse_gene)$fetal <- fetal
colData(rse_gene)$birth <- birth
colData(rse_gene)$infant <- infant
colData(rse_gene)$child <- child
colData(rse_gene)$teen <- teen
colData(rse_gene)$adult <- adult
rm(fetal, birth, infant, child, teen, adult)
## To simplify later code
pd <- as.data.frame(colData(rse_gene))
pd <- pd[, match(c('Age', 'fetal', 'birth', 'infant', 'child', 'teen', 'adult', 'Sex', 'snpPC1', 'snpPC2', 'snpPC3', 'snpPC4', 'snpPC5', 'Region', 'Race'), colnames(pd))]
rpkm <- assays(rse_gene)$rpkm
DLPFC <- colData(rse_gene)$Region == 'DLPFC'
## Define models
fm_mod <- ~Age + fetal + birth + infant + child + teen + adult + Sex + snpPC1 + snpPC2 + snpPC3 + snpPC4 + snpPC5
fm_mod0 <- ~ Age + Sex + snpPC1 + snpPC2 + snpPC3 + snpPC4 + snpPC5
fm_mod_all <- ~Age *Region + fetal * Region + birth *Region + infant *Region + child * Region + teen * Region + adult * Region + Sex + Region + snpPC1 + snpPC2 + snpPC3 + snpPC4 + snpPC5
fm_mod0_all <- ~ Age + fetal + birth + infant + child + teen + adult + Sex + Region + snpPC1 + snpPC2 + snpPC3 + snpPC4 + snpPC5
get_mods <- function(pd, int = FALSE) {
### adjust for race, sex
if(int) {
mod = model.matrix(fm_mod_all, data=pd)
mod0 = model.matrix(fm_mod0_all, data=pd)
} else {
mod = model.matrix(fm_mod, data=pd)
mod0 = model.matrix(fm_mod0, data=pd)
}
return(list(mod = mod, mod0 = mod0))
}
```
## DLPFC
```{r 'dlpfc'}
### DLPFC
de_obj_dlpfc <- build_models(data = rpkm[, DLPFC], full.model = fm_mod, null.model = fm_mod0, cov = pd[DLPFC, ])
ef_obj_dlpfc <- fit_models(de_obj_dlpfc, stat.type = "lrt")
de_lrt_dlpfc <- lrt(de_obj_dlpfc, nullDistn = "normal")
summary(de_lrt_dlpfc)
hist(qvalueObj(de_lrt_dlpfc))
summary(qvalueObj(de_lrt_dlpfc)$qvalues)
```
## HIPPO
```{r 'hippo'}
### HIPPO
de_obj_hippo <- build_models(data = rpkm[, !DLPFC], full.model = fm_mod, null.model = fm_mod0, cov = pd[!DLPFC, ])
ef_obj_hippo <- fit_models(de_obj_hippo, stat.type = "lrt")
de_lrt_hippo <- lrt(de_obj_hippo, nullDistn = "normal")
summary(de_lrt_hippo)
hist(qvalueObj(de_lrt_hippo))
summary(qvalueObj(de_lrt_hippo)$qvalues)
```
## Quick region comparison
```{r 'quick_comp'}
### Compare quickly
stopifnot(identical(rownames(de_obj_dlpfc), rownames(de_obj_hippo)))
comp <- data.frame(
DLPFC = qvalueObj(de_lrt_dlpfc)$qvalues,
HIPPO = qvalueObj(de_lrt_hippo)$qvalues,
gene_id = rownames(de_obj_dlpfc),
stringsAsFactors = FALSE
)
dim(comp)
## Using the LRT model results
table(comp$DLPFC < 1e-04, comp$HIPPO < 1e-04)
#save(comp, file = 'edge_comp.Rdata')
```
## Both regions
```{r 'interaction_mod'}
de_obj <- build_models(data = rpkm, full.model = fm_mod_all, null.model = fm_mod0_all, cov = pd)
system.time( ef_obj <- fit_models(de_obj, stat.type = "lrt") )
system.time( de_lrt <- lrt(de_obj, nullDistn = "normal") )
summary(de_lrt)
hist(qvalueObj(de_lrt))
summary(qvalueObj(de_lrt)$qvalues)
```
## Explore a gene
The following code explores a single gene based on the `de_lrt_dlpfc`, `de_odp_dlpfc`, `de_lrt_hippo`, `de_odp_hippo` and `de_lrt` results.
```{r 'explore_gene', fig.width = 14, fig.height = 7}
gene_explore <- function(ef, de, pd) {
fitVals <- fitFull(ef)
fitVals0 <- fitNull(ef)
i <- which.min(qvalueObj(de)$qvalue)
print(qvalueObj(de)$qvalue[i])
df <- data.frame(age= pd$Age, sex= pd$Sex, race = pd$Race, region = pd$Region, null.model = fitVals0[i, ], full.model = fitVals[i, ], raw = exprs(de)[i, ])
df <- melt(data = df, id.vars=c("age", "sex", 'race', 'region'))
df$value <- log(df$value + 1)
p1 <- ggplot(df, aes(log10(age + 1), value, color=sex, shape = race)) + geom_point() + facet_wrap(region~variable) + ylab('log(RPKM + 1)') + xlab('log10(age + 1)') + geom_vline(xintercept = log10(c(0, 1, 10, 20, 50) + 1))
p2 <- ggplot(df, aes(age, value, color=sex, shape = race)) + geom_point() + facet_wrap(region~variable) + ylab('log(RPKM + 1)') + geom_vline(xintercept = c(0, 1, 10, 20, 50))
return(list(p1, p2))
}
## DLPFC lrt
gene_explore(ef_obj_dlpfc, de_lrt_dlpfc, pd[DLPFC, ])
## HIPPO lrt
gene_explore(ef_obj_hippo, de_lrt_hippo, pd[!DLPFC, ])
```
```{r 'explore_gene_both', fig.width = 14, fig.height = 10}
## both
gene_explore(ef_obj, de_lrt, pd)
```
## Limma
This portion uses `limma` to fit the region by age interaction model and calculate a global F statistic.
```{r 'limma'}
### Both
mods <- get_mods( colData(rse_gene), int = TRUE)
sapply(mods, colnames)
library('limma')
library('edgeR')
library('qvalue')
ind <- data.frame(
brnum = colData(rse_gene)$BrNum,
rnum = colData(rse_gene)$RNum,
stringsAsFactors = FALSE
)
brnum <- ind$brnum[match(rownames(pd), ind$rnum)]
design <- mods$mod
dge <- DGEList(counts = assays(rse_gene)$counts)
dge <- calcNormFactors(dge)
v <- voom(dge, design, plot = TRUE)
## Page 49 (50 in the pdf) of limmaUsersGuide()
## Didn't run in my laptop (likely needs more mem)
if(FALSE) {
corfit <- duplicateCorrelation(v$E, design, block=brnum) ## Takes like 4-5 hrs to run
corfit$consensus.correlation
# [1] 0.2358895
# > sapply(corfit, head)
# $consensus.correlation
# [1] 0.2358895
#
# $cor
# [1] 0.2358895
#
# $atanh.correlations
# [1] 0.5662411 0.3102444 0.2684879 0.5457452 0.8571016 0.4324565
#
# > sapply(corfit, length)
# consensus.correlation cor atanh.correlations
# 1 1 24652
fit <- lmFit(v, design, block=brnum, correlation = corfit$consensus.correlation) ## Takes like 32 min to run
} else {
fit <- lmFit(v, design)
}
fit <- eBayes(fit)
colnames(design)[grep(':', colnames(design))]
top <- topTable(fit, coef = grep(':', colnames(design)), n = nrow(rse_gene), sort.by = 'none')
qval <- qvalue(top$P.Value)
summary(qval)
hist(qval)
summary(qval$qvalues)
summary(top$adj.P.Val)
sapply(c(1e-04, 0.001, 0.01, 0.025, 0.05, 0.1, 1), function(x) { sum(top$adj.P.Val < x)})
# log2FC <- fit$coefficients[, 2]
# pvalue <- fit$p.value[, 2]
# qvalue <- qvalue(pvalue)$qvalues
```
```{r, eval = FALSE}
fit2 <- eBayes(lmFit(v, design))
top2 <- topTable(fit2, coef = grep(':', colnames(design)), n = nrow(rse_gene), sort.by = 'none')
stopifnot(identical(rownames(top), rownames(top2)))
save(v, design, corfit, fit, top, brnum, fit2, top2, file = 'test_corfit_nov20.Rdata')
summary(qval)
addmargins(table('Using corr' = top$adj.P.Val < 0.05, 'Naive' = top2$adj.P.Val < 0.05))
# Call:
# qvalue(p = top$P.Value)
#
# pi0: 0.05126835
#
# Cumulative number of significant calls:
#
# <1e-04 <0.001 <0.01 <0.025 <0.05 <0.1 <1
# p-value 18289 19225 20441 21017 21552 22103 24652
# q-value 19398 20722 22645 23739 24621 24652 24652
# local FDR 17828 18734 19951 20539 21099 21745 24647
summary(qvalue(top2$P.Value))
# Call:
# qvalue(p = top2$P.Value)
#
# pi0: 0.07523708
#
# Cumulative number of significant calls:
#
# <1e-04 <0.001 <0.01 <0.025 <0.05 <0.1 <1
# p-value 17668 18584 19834 20451 20998 21598 24652
# q-value 18585 19884 21766 22839 23929 24652 24652
# local FDR 17065 17925 19089 19708 20252 20866 24652
rho <- tanh(corfit$atanh.correlations)
# > summary(corfit$atanh.correlations)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# -0.2825 0.1274 0.2314 0.2648 0.3600 2.1013
# > summary(rho)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# -0.2752 0.1267 0.2273 0.2478 0.3452 0.9705
add.alpha <- function(col, alpha=1){
if(missing(col))
stop("Please provide a vector of colours.")
apply(sapply(col, col2rgb)/255, 2,
function(x)
rgb(x[1], x[2], x[3], alpha=alpha))
}
pdf('test_corfit.pdf')
hist(qval)
hist(qvalue(top2$P.Value))
plot(density(corfit$atanh.correlations))
plot(density(rho))
plot(top$F, top2$F, col = add.alpha('black', 1/10), pch = 20)
abline(a = 0, b = 1, col = 'red')
plot(log10(top$F + 1), log10(top2$F + 1), col = add.alpha('black', 1/10), pch = 20)
abline(a = 0, b = 1, col = 'red')
plot((log10(top$F + 1) + log10(top2$F + 1) ) / 2, log10(top$F + 1) - log10(top2$F + 1), col = add.alpha('black', 1/10), pch = 20)
abline(h = 0, col = 'red')
abline(v = 3, col = 'blue')
abline(h = 0.1, col = 'grey80')
abline(h = - 0.1, col = 'grey80')
addmargins(table('close to 0 on Y' = abs(log10(top$F + 1) - log10(top2$F + 1)) < 0.1, 'less than 3 on x' = (log10(top$F + 1) + log10(top2$F + 1) ) / 2 < 3))
# less than 3 on x
# close to 0 on Y FALSE TRUE Sum
# FALSE 640 9165 9805
# TRUE 1702 13145 14847
# Sum 2342 22310 24652
plot(-log10(top$P.Value), -log10(top2$P.Value), col = add.alpha('black', 1/10), pch = 20)
abline(a = 0, b = 1, col = 'red')
plot(-log10(top$adj.P.Val), -log10(top2$adj.P.Val), col = add.alpha('black', 1/10), pch = 20)
abline(a = 0, b = 1, col = 'red')
stopifnot(identical(colnames(top), colnames(top2)))
for(i in grep('Region', colnames(top))) {
plot(top[, i], top2[, i], main = colnames(top)[i], xlab = 'Coef: using corr', ylab = 'Coef: naive', col = add.alpha('black', 1/10), pch = 20)
abline(h = 0, col = 'grey80')
abline(v = 0, col = 'grey80')
}
#plot(rowSums(top[, grep('Region', colnames(top))]), rowSums(top2[, grep('Region', colnames(top))]), main = 'Coef sums', xlab = 'Coef: using corr', ylab = 'Coef: naive')
dev.off()
pdf('test_corfit_anon.pdf')
plot(density(corfit$atanh.correlations))
plot(density(tanh(corfit$atanh.correlations)))
plot(top$F, top2$F, col = add.alpha('black', 1/10), pch = 20, xlab = 'F-stat: using corr', ylab = 'F-stat: naive')
abline(a = 0, b = 1, col = 'red')
plot(log10(top$F + 1), log10(top2$F + 1), col = add.alpha('black', 1/10), pch = 20, xlab = 'log10(F-stat + 1): using corr', ylab = 'log10(F-stat + 1): naive')
abline(a = 0, b = 1, col = 'red')
plot((log10(top$F + 1) + log10(top2$F + 1) ) / 2, log10(top$F + 1) - log10(top2$F + 1), col = add.alpha('black', 1/10), pch = 20, xlab = '[log10(F-stat corr + 1) + log10(F-stat naive + 1) ] / 2', ylab = 'log10(F-stat corr + 1) - log10(F-stat naive + 1)')
abline(h = 0, col = 'red')
abline(v = 3, col = 'blue')
abline(h = 0.1, col = 'grey80')
abline(h = - 0.1, col = 'grey80')
addmargins(table('close to 0 on Y' = abs(log10(top$F + 1) - log10(top2$F + 1)) < 0.1, 'less than 3 on x' = (log10(top$F + 1) + log10(top2$F + 1) ) / 2 < 3))
# less than 3 on x
# close to 0 on Y FALSE TRUE Sum
# FALSE 640 9165 9805
# TRUE 1702 13145 14847
# Sum 2342 22310 24652
plot(-log10(top$P.Value), -log10(top2$P.Value), xlab = '-log10(P.Value): using corr', ylab = '-log10(P.Value): naive')
abline(a = 0, b = 1, col = 'red')
plot(-log10(top$adj.P.Val), -log10(top2$adj.P.Val), xlab = '-log10(adj.P.Val): using corr', ylab = '-log10(adj.P.Val): naive')
abline(a = 0, b = 1, col = 'red')
stopifnot(identical(colnames(top), colnames(top2)))
for(i in grep('Region', colnames(top))) {
plot(top[, i], top2[, i], main = paste('Coef', i), xlab = 'Coef: using corr', ylab = 'Coef: naive', col = add.alpha('black', 1/10), pch = 20)
abline(h = 0, col = 'grey80')
abline(v = 0, col = 'grey80')
}
dev.off()
```
## Reproducibility
```{r}
## Reproducibility information
print('Reproducibility information:')
Sys.time()
proc.time()
options(width = 120)
session_info()
```