-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.P337_BAL_fMRI.Rmd
331 lines (273 loc) · 8.85 KB
/
2.P337_BAL_fMRI.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
---
title: "P337: fMRI analysis"
subtitle: "Bronchial lavage (BAL) pre/post allergen challenge"
author: "Kim Dill-McFarland, [email protected]"
output:
html_document:
toc: yes
toc_depth: 4
toc_float:
collapsed: no
date: "version `r format(Sys.time(), '%B %d, %Y')`"
editor_options:
chunk_output_type: console
---
# Background
The purpose of this workflow is to identify differentially activated brain regions (fMRI).
# Setup
Load packages
```{r message=FALSE, warning=FALSE}
# Data manipulation and figures
library(tidyverse)
library(readxl)
# Multi-panel figures for ggplot
library(cowplot)
#Define ggplot colors
logFC.cols <- c("Down, FDR < 0.5"="lightblue",
"Down, FDR < 0.2"="blue",
"Down, FDR < 0.05"="darkblue",
"Down, FDR < 0.01"="blue",
"Down, FDR < 0.001"="lightblue",
"NS"="grey",
"Up, FDR < 0.5"="pink",
"Up, FDR < 0.2"="red",
"Up, FDR < 0.05"="darkred",
"Up, FDR < 0.01"="red",
"Up, FDR < 0.001"="pink")
# Print tty table to knit file
library(knitr)
library(kableExtra)
options(knitr.kable.NA = '')
```
Set seed
```{r}
set.seed(4389)
```
Scripts
```{r}
#Extract pvalues from limma output
source("https://raw.githubusercontent.com/kdillmcfarland/R_bioinformatic_scripts/master/limma.extract.pval.R")
#Reverse %in%
`%notin%` <- Negate(`%in%`)
```
# Load data
```{r}
#Time point 1 = visit 4
temp <- read_excel(sheet="T1",
"data_raw/addtl.data/extraced.clusters.Matt.Altman_wbaseline_psychdata.xlsx") %>%
#long format
pivot_longer(-idnum, names_to="neuro") %>%
#add visit variable
mutate(visit="V4")
#Time point 2 = visit 5
neuro <- read_excel(sheet="T2",
"data_raw/addtl.data/extraced.clusters.Matt.Altman_wbaseline_psychdata.xlsx") %>%
#long format
pivot_longer(-idnum, names_to="neuro") %>%
#add visit variable
mutate(visit="V5") %>%
#Combine with other visit
full_join(temp) %>%
#Format idnum to match RNAseq data
mutate(idnum = paste("MA",idnum, sep="")) %>%
#wide format
mutate(name = paste(idnum,visit, sep="_")) %>%
select(-idnum, -visit) %>%
pivot_wider() %>%
filter(neuro !="BDI" & neuro !="LSI") %>%
column_to_rownames("neuro")
attach("data_clean/P337_BAL_data.RData")
meta <- data.frame(libID=colnames(neuro)) %>%
separate(libID, into = c("ptID","visit"), remove = FALSE) %>%
inner_join(dat.BAL.abund.norm.voom$targets %>%
select(donorID, visit, age_mo, race, sex, EOS.pct:Epi.pct) %>%
distinct() %>%
rename(ptID=donorID)) %>%
mutate(age_yrs = age_mo/12)
neuro.subset <- neuro %>%
select(all_of(meta$libID))
dat <- list()
dat$targets <- meta
dat$E <- neuro.subset
```
This includes in the following samples.
```{r echo=FALSE}
meta %>%
count(visit) %>%
kable(align="c", caption="Total donors") %>%
kable_styling(bootstrap_options = "striped", full_width = FALSE)
```
# PCA (proteins)
```{r echo=FALSE, warning=FALSE, message=FALSE, fig.height=4}
# Calculate PCA
PCA <- as.data.frame(dat$E) %>%
t() %>%
prcomp()
PC1.label <- paste("PC1 (", summary(PCA)$importance[2,1]*100, "%)", sep="")
PC2.label <-paste("PC2 (", summary(PCA)$importance[2,2]*100, "%)", sep="")
# Extract PC values
PCA.dat <- as.data.frame(PCA$x) %>%
rownames_to_column("libID") %>%
# Select PCs
dplyr::select(libID, PC1:PC3) %>%
# Merge with metadata
left_join(dat$targets, by="libID")
PCA <- ggplot(PCA.dat, aes(PC1, PC2)) +
geom_point(aes(color=visit),
size=3) +
#Beautify
theme_classic() +
labs(x=PC1.label, y=PC2.label,
title="fMRI") +
coord_fixed(ratio=1) +
guides(color=guide_legend(title.position="top",
title.hjust = 0.5))
PCA2 <- ggplot(PCA.dat, aes(PC1, PC2, color=ptID)) +
geom_point(size=3) +
#Beautify
theme_classic() +
labs(x=PC1.label, y=PC2.label,
title="fMRI") +
coord_fixed(ratio=1)
PCA
PCA2
dir.create("figs/", showWarnings = FALSE)
ggsave("figs/PCA_P337_fMRI.png",
plot_grid(PCA, PCA2, align = "hv", ncol=1),
height=7, width=5)
```
# Define significant proteins
## Linear model: visit
```{r}
# Define model
model.visit <- model.matrix(~ visit, data=dat$targets)
colnames(model.visit) <- c("(Intercept)", "visit")
#block by donor
consensus.corr <- duplicateCorrelation(
dat$E,
model.visit,
block=dat$targets$ptID)$consensus.correlation
consensus.corr
# Fit model to transformed count data. Calculate eBayes
efitQW <- eBayes(
lmFit(dat$E, model.visit,
block=dat$targets$ptID,
correlation=consensus.corr))
```
```{r warning=FALSE, message=FALSE}
#Extract p-values from results
extract.pval(model=model.visit,
voom.dat=dat$E,
eFit=efitQW,
name="P337_fMRI",
summary=TRUE,
contrasts=FALSE,
FC.group = TRUE)
#Write to disk
dir.create(path="results/fMRI/",
showWarnings = FALSE, recursive = TRUE)
write_csv(P337_fMRI,
file = "results/fMRI/P337_fMRI_visit.csv")
```
### Summarize gene model
```{r echo=FALSE}
P337_fMRI.summ %>%
filter(group != "total (nonredundant)") %>%
kable(align=c("l","l","c","c","c","c","c","c"),
col.names = c("Variable", "Fold change",
"0.05", "0.1", "0.2","0.3","0.4","0.5")) %>%
kable_styling(bootstrap_options = "striped", full_width = FALSE) %>%
add_header_above(c(" "=2, "Genes with FDR <"=6))
```
```{r echo=FALSE, message=FALSE, warning=FALSE, fig.height=5, fig.width=9}
P337_fMRI %>%
filter(group != "(Intercept)") %>%
mutate(col.group = ifelse(adj.P.Val <= 0.05 & FC.group=="up",
"Up, FDR < 0.05",
ifelse(adj.P.Val <= 0.05 & FC.group=="down",
"Down, FDR < 0.05",
ifelse(adj.P.Val <= 0.2 & FC.group=="up",
"Up, FDR < 0.2",
ifelse(adj.P.Val <= 0.2 & FC.group=="down",
"Down, FDR < 0.2",
ifelse(adj.P.Val <= 0.5 & FC.group=="up",
"Up, FDR < 0.5",
ifelse(adj.P.Val <= 0.5 & FC.group=="down",
"Down, FDR < 0.5",
"NS"))))))) %>%
arrange(group,-adj.P.Val) %>%
ggplot(aes(x=AveExpr, y=logFC, color=col.group)) +
geom_point(size=2) +
scale_color_manual(values=logFC.cols) +
facet_grid(~group, scales = "free_y")+
theme_classic() +
labs(x="Average score", y="Post-Pre score change", color="") +
guides(color = guide_legend(reverse = TRUE)) +
theme(text = element_text(size=18),
legend.position = "bottom") +
guides(color=guide_legend(nrow=3, byrow=TRUE))
```
## Linear model: covariates
```{r age}
# Define model
model.age <- model.matrix(~ visit + age_yrs, data=dat$targets)
colnames(model.age) <- c("(Intercept)", "visit", "age")
#block by donor
consensus.corr <- duplicateCorrelation(
dat$E,
model.age,
block=dat$targets$ptID)$consensus.correlation
consensus.corr
# Fit model to transformed count data. Calculate eBayes
efitQW <- eBayes(
lmFit(dat$E, model.age,
block=dat$targets$ptID,
correlation=consensus.corr))
#Extract p-values from results
extract.pval(model=model.age,
voom.dat=dat$E,
eFit=efitQW,
name="P337_fMRI_age",
summary=TRUE,
contrasts=FALSE,
FC.group = TRUE)
```
```{r sex}
# Define model
model.sex <- model.matrix(~ visit + sex, data=dat$targets)
colnames(model.sex) <- c("(Intercept)", "visit", "sex")
#block by donor
consensus.corr <- duplicateCorrelation(
dat$E,
model.sex,
block=dat$targets$ptID)$consensus.correlation
consensus.corr
# Fit model to transformed count data. Calculate eBayes
efitQW <- eBayes(
lmFit(dat$E, model.sex,
block=dat$targets$ptID,
correlation=consensus.corr))
#Extract p-values from results
extract.pval(model=model.sex,
voom.dat=dat$E,
eFit=efitQW,
name="P337_fMRI_sex",
summary=TRUE,
contrasts=FALSE,
FC.group = TRUE)
```
### Summarize gene models
```{r echo=FALSE}
bind_rows(P337_fMRI_age.summ,P337_fMRI_sex.summ) %>%
filter(group != "total (nonredundant)") %>%
kable(align=c("l","l","c","c","c","c","c","c"),
col.names = c("Variable", "Fold change",
"0.05", "0.1", "0.2","0.3","0.4","0.5")) %>%
kable_styling(bootstrap_options = "striped", full_width = FALSE) %>%
add_header_above(c(" "=2, "Genes with FDR <"=6))
```
# R session
```{r}
sessionInfo()
```
***