-
Notifications
You must be signed in to change notification settings - Fork 2
/
WGCNAshiny.Rmd
508 lines (444 loc) · 11.5 KB
/
WGCNAshiny.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
---
title: "Module Visualization"
author: "Brian Yandell"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: html_document
runtime: shiny
params:
echo: no
rootdir: ~/founder_diet_study
---
```{r}
rawdir <- file.path(params$rootdir, "RawData")
harmonizeddir <- file.path(params$rootdir, "HarmonizedData")
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = params$echo, warning = FALSE, message = FALSE)
knitr::opts_knit$set(root.dir = harmonizeddir)
```
```{r}
getwd()
```
```{r}
datadir <- "."
datadir <- file.path(datadir, "Normalized")
```
```{r}
devtools::install_cran("plotly") # not yet on UW dataviz
devtools::install_cran("markdown") # not yet on UW dataviz
devtools::install_cran("cowplot") # not yet on UW dataviz
devtools::install_cran("patchwork") # not yet on UW dataviz
devtools::install_cran("ggdendro") # not yet in UW dataviz
devtools::install_github("byandell/foundr")
```
This document selects a `dataset` and creates WGCNA modules. Selecting a `response` enables visualization of stats for the modules as well as the WGCNA dendogram and color panel. Selects a `facet response` and `color response` compares two types of WGCNA modules. The first figure plots kMEs while the second one shows correlation of eigentraits across those responses. Data can be downloaded by clicking the button.
### Explanation of Responses
The response for each trait can be partitioned into uncorrelated pieces that
have useful interpretations. Letting `value` be the trait value for an organism, it is partitioned into the `cellmean` and `noise`. The `cellmean` is the part of the response explained by the model, and the `noise` is the residual. The `cellmean` can be decomposed into main effects and interactions involving the factors `strain`, `sex` and `condition`. However, generally we are interested in whether there are different `strain` responses to `condition`, in which case we are interested in the `signal` composed of `strain:condition` and `strain:sex:condition` interactions, with the `rest` of the model components conveniently combined.
<center>
```
value = cellmean + noise
cellmean = signal + rest
value = signal + rest + noise
signal = strain:condition + strain:sex:condition
rest = strain + sex + condition + strain:sex + sex:condition
```
</center>
For each trait, the `cellmean` and `noise` terms are uncorrelated, as are the `signal` and `rest` terms, which add up to the `cellmean`. That means their variances add up ($V_i = V_s + V_r + V_n$ and $V_c = V_s + V_r$), and the covariances of uncorrelated terms are 0 ($C_{cn} = 0, C_{sr} = 0$). This makes it possible later to compare across different datasets in interesting ways.
Patterns emerge when comparing these components across traits within WGCNA modules. That is, a module identified with the `indivdual` response (the default approach) may represent primarily `signal`, which would be useful. However, if a module is primarily associated with `rest` or `noise`, it is less likely to yield interesting trait relationships. Further, comparing modules across responses might identify interesting subsets of modules for further study.
```{r}
datasets <- c("LivMet","PlaMet0","PlaMet120","Metab")
responses <- c("value","cellmean","signal","rest","noise")
```
```{r}
traitModule <- readRDS(file.path(datadir, "traitModule.rds"))
traitData <-
dplyr::filter(
readRDS(file.path(datadir, "traitData.rds")),
dataset %in% datasets)
traitSignal <-
dplyr::filter(
readRDS(file.path(datadir, "traitSignal.rds")),
dataset %in% datasets)
```
```{r}
shiny::fluidRow(
shiny::column(
6,
shiny::selectInput(
"dataset", "Dataset:",
datasets)),
shiny::column(
6,
shiny::selectInput(
"response", "Response:",
responses))
)
```
# WGCNA
```{r}
shiny::renderPlot({
shiny::req(input$dataset, input$response)
foundr::ggplot_listof_wgcnaModules(traitModule[[input$dataset]], input$response)
})
```
```{r}
DT::renderDataTable({
shiny::req(input$dataset, input$response)
DT::datatable(
dplyr::filter(
summary(traitModule[[input$dataset]]),
response == input$response),
options = list(paging =TRUE, pageLength = 5))
})
```
# Module Comparisons
```{r}
mods <- shiny::reactive({
shiny::req(input$dataset)
foundr::module_kMEs(traitModule[[input$dataset]])
})
```
```{r}
fmodules <- shiny::reactive({
shiny::req(mods(), input$responseF)
levels(mods()[[paste0(input$responseF, "_col")]])
})
```
```{r}
cmodules <- shiny::reactive({
shiny::req(mods(), input$responseC)
levels(mods()[[paste0(input$responseC, "_col")]])
})
```
```{r}
shiny::fluidRow(
shiny::column(
4,
shiny::selectInput(
"responseF", "Facet Response:",
"cellmean")),
shiny::column(
4,
shiny::selectInput(
"responseC", "Color Response:",
"value"))
)
```
```{r}
shiny::fluidRow(
shiny::column(
4,
shiny::selectInput(
"fmodules", "Facet Modules:",
c("gray","turquoise"),
multiple = TRUE)),
shiny::column(
4,
shiny::selectInput(
"cmodules", "Color Modules:",
c("gray","turquoise"),
multiple = TRUE)),
shiny::column(
2,
checkboxInput("abs", "Absolute kME?"))
)
```
```{r}
responseFs <- shiny::reactive({
shiny::req(input$response)
unique(c(input$response, responses))
})
```
```{r}
responseCs <- shiny::reactive({
shiny::req(input$responseF)
responses[responses != input$responseF]
})
```
```{r}
shiny::observeEvent(
input$response,
{
shiny::updateSelectInput(
session, "responseF",
choices = responseFs(),
selected = responseFs()[1])
})
```
```{r}
shiny::observeEvent(
input$responseF,
{
shiny::updateSelectInput(
session, "responseC",
choices = responseCs(),
selected = responseCs()[1])
shiny::updateSelectInput(
session, "fmodules",
choices = fmodules(),
selected = fmodules())
})
```
```{r}
shiny::observeEvent(
input$responseC,
{
shiny::updateSelectInput(
session, "cmodules",
choices = cmodules(),
selected = cmodules())
})
```
```{r}
moddata <- shiny::reactive({
shiny::req(mods(), input$responseF, input$responseC,
input$fmodules, input$cmodules)
foundr::subset_module_kMEs(mods(), input$responseF, input$responseC,
facetmodules = input$fmodules, colormodules = input$cmodules)
})
```
```{r}
plotly::renderPlotly({
shiny::req(moddata(), input$responseF, input$responseC)
print(
ggplot2::autoplot(
moddata(),
input$responseF, input$responseC, input$abs))
})
```
```{r}
DT::renderDataTable({
shiny::req(input$dataset, input$response)
DT::datatable(
dplyr::arrange(
tidyr::pivot_wider(
dplyr::select(
summary(traitModule[[input$dataset]]),
-maxkME, -minkME),
names_from = "response", values_from = "count",
values_fill = 0),
dplyr::desc(.data[[input$response]])),
options = list(paging =TRUE, pageLength = 5))
})
```
# Eigentrait Correlations
```{r}
eigens <- shiny::reactive({
shiny::req(input$dataset)
foundr::eigen_cor(traitModule[[input$dataset]])
})
```
```{r}
plotly::renderPlotly({
shiny::req(eigens(), input$responseF, input$responseC)
print(
ggplot2::autoplot(
eigens(),
input$responseF, input$responseC,
facetmodules = input$fmodules,
colormodules = input$cmodules))
})
```
The sign of correlation of modules could provide adjustment to direction in kME plots above. However, using the absolute value seems to show the strong relationship (or not).
```{r}
DT::renderDataTable({
shiny::req(eigens(), input$responseF, input$responseC)
DT::datatable(
foundr::subset_eigen_cor(eigens(), input$responseC, input$responseF),
options = list(paging =TRUE, pageLength = 5))
})
```
# Download Data
```{r}
shiny::downloadHandler(
filename = "blah.csv",
content = function(file) {
req(moddata())
write.csv(moddata(), file)
}
)
```
```{r}
DT::renderDataTable({
shiny::req(moddata())
dplyr::mutate(
moddata(),
dplyr::across(
dplyr::where(is.numeric),
function(x) signif(x, 4)))
})
```
# Compare Datasets
```{r}
shiny::fluidRow(
shiny::column(
4,
shiny::selectInput(
"datasetD", "Facet Dataset:",
datasets)),
shiny::column(
4,
shiny::selectInput(
"responseD", "Dataset Response:",
responses))
)
```
```{r}
shiny::fluidRow(
shiny::column(
4,
shiny::selectInput(
"fmodules2", "Facet Modules:",
c("gray","turquoise"),
multiple = TRUE)),
shiny::column(
4,
shiny::selectInput(
"cmodules2", "Color Modules:",
c("gray","turquoise"),
multiple = TRUE)),
shiny::column(
2,
checkboxInput("absD", "Absolute kME?"))
)
```
```{r}
dataset2 <- shiny::reactive({
shiny::req(input$datasetD)
datasets[input$datasetD != datasets][1]
})
```
```{r}
fmodules2 <- shiny::reactive({
shiny::req(metmods(), input$datasetD)
levels(metmods()[[paste0(input$datasetD, "_col")]])
})
```
```{r}
cmodules2 <- shiny::reactive({
shiny::req(metmods(), dataset2())
levels(metmods()[[paste0(dataset2(), "_col")]])
})
```
```{r}
shiny::observeEvent(
input$datasetD,
{
shiny::updateSelectInput(
session, "fmodules2",
choices = fmodules2(),
selected = fmodules2())
})
```
```{r}
shiny::observeEvent(
dataset2(),
{
shiny::updateSelectInput(
session, "cmodules2",
choices = cmodules2(),
selected = cmodules2())
})
```
```{r}
dmods <- shiny::reactive({
shiny::req(input$datasetD, dataset2(), input$responseD)
foundr::join_dataset_modules(
traitModule[c(input$datasetD, dataset2())],
input$responseD)
})
```
```{r}
metmods <- shiny::reactive({
shiny::req(dmods())
foundr::module_kMEs(dmods())
})
```
```{r}
moddata2 <- shiny::reactive({
shiny::req(metmods(), input$datasetD, dataset2(),
input$fmodules2, input$cmodules2)
foundr::subset_module_kMEs(
metmods(), input$datasetD, dataset2(),
facetmodules = input$fmodules2,
colormodules = input$cmodules2)
})
```
```{r}
plotly::renderPlotly({
shiny::req(moddata2(), input$datasetD, dataset2())
if(is.null(moddata2()))
foundr::plot_null("no moddata2 data")
else {
plotly::event_register(
plotly::ggplotly(
print(
ggplot2::autoplot(
moddata2(),
input$datasetD, dataset2(), input$absD))),
"plotly_click")
}
})
```
```{r}
pointKey <- reactive({
shiny::req(moddata2())
point <- plotly::event_data(
event = "plotly_click", priority = "event")
shiny::req(point) # to avoid error if no point is clicked
point$key
})
```
```{r}
shiny::renderTable({
shiny::req(moddata2(), pointKey())
dplyr::filter(
moddata2(),
trait == pointKey()) # use the key to find selected point
})
```
```{r}
pairData <- shiny::reactive({
shiny::req(pointKey())
foundr::traitSolos(
dplyr::filter(
traitData,
trait == pointKey()),
dplyr::filter(
traitSignal,
trait == pointKey()))
})
```
```{r}
shiny::renderPlot({
shiny::req(pairData())
foundr::ggplot_traitPairs(
foundr::traitPairs(pairData()))
})
```
```{r}
DT::renderDataTable({
shiny::req(pairData(), pointKey())
DT::datatable(
pairData(),
options = list(paging =TRUE, pageLength = 5))
})
```
```{r}
eigenMet <- shiny::reactive({
shiny::req(dmods())
foundr::eigen_cor(dmods())
})
```
```{r}
plotly::renderPlotly({
shiny::req(eigenMet(), dataset2(), input$datasetD)
print(
ggplot2::autoplot(
eigenMet(),
input$datasetD, dataset2(),
facetmodules = input$fmodules2,
colormodules = input$cmodules2))
})
```