-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_pairwise.R
456 lines (400 loc) · 14.3 KB
/
module_pairwise.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
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
PW_UI <- function(id) {
ns <- NS(id)
fluidPage(
theme =
shinytheme("flatly"),
useShinyjs(),
titlePanel(
"Differential Expression Tables and Plots"
),#end title
sidebarLayout(
sidebarPanel(
tagList(
# shinyWidgets::checkboxGroupButtons(
# inputId = (ns("pwc")),
# label = "Choose a Pairwise Comparison: LSC datasets",
# choices = c(1,2),
# selected = NULL,
# status = "primary"
#
# ),
#for BEAT and TCGA:
selectInput(
ns("DEmodel"),
label = "Choose a metadata variable for DE design",
choices = NULL
),
selectInput(
ns("pwc"),
label = "Choose a Pairwise Comparison",
choices = NULL
),
materialSwitch(
inputId =
(ns("PWDESeqtable")),
label =
"DE Table",
value =
FALSE,
right =
TRUE
),
hr(),
materialSwitch(
inputId =
(ns("PWvolcano")),
label =
"Volcano Plot",
value =
FALSE,
right =
TRUE
),
hr(),
materialSwitch(
inputId =
(ns("PWMA")),
label =
"MA Plot",
value =
FALSE,
right =
TRUE
),
materialSwitch(
inputId =
(ns("PWHeat")),
label =
"Heatmap",
value =
FALSE,
right =
TRUE
),
hr(),
conditionalPanel(
ns = ns,
condition = "input.PWvolcano == true",
h4("Volcano Plot Specific Options"),
#color palette choice for volcano plot
colorUI(ns("color10"), "Choose 1st color", "#0000FF"),
colorUI(ns("color11"), "Choose 2nd color", "028a0f"),
hr(),
downloadButton(
ns("downloadPWVolcano"),
label =
"Download Volcano Plot"
)
),
hr(),
conditionalPanel(
ns = ns,
condition = "input.PWMA == true",
h4("MA Plot Specific Options"),
#color palette choice for MA plot
colorUI(ns("color12"), "Choose 1st color", "#0000FF"),
colorUI(ns("color13"), "Choose 2nd color", "028a0f"),
hr(),
downloadButton(
ns("downloadPWMA"),
label =
"Download MA Plot"
)
),
hr(),
conditionalPanel(
ns = ns,
condition = "input.PWHeat == true",
h4("Heatmap Specific Options"),
#color palette choices for heatmap
colorUI(ns("color14"),"Choose 1st color", "#0000FF"),
colorUI(ns("color15"), "Choose 2nd color", "#FF0000"),
)
)
),
mainPanel(
conditionalPanel(
ns = ns,
condition = "input.PWDEtable == true",
DTOutput(ns("PWresults"))
),
conditionalPanel(
ns = ns,
condition = "input.PWvolcano == true",
girafeOutput(ns("PWvolplot"))
),
conditionalPanel(
ns = ns,
condition = "input.PWMA == true",
girafeOutput(ns("PWMAplot"))
),
conditionalPanel(
ns = ns,
condition = "input.PWHeat == true",
InteractiveComplexHeatmapOutput(heatmap_id = ns("pwht"))
)
)
)
)
}
PW_Server <- function(id, data_species, dataset_dds, dataset_choice) {
moduleServer(id, function(input, output, session) {
observe({
shinyjs::toggle(id = "pwc", condition = dataset_choice() %in% c("Ye_16", "Venaza", "Lagadinou", "BEAT","TCGA"))
})
observe({
shinyjs::toggle(id = "DEmodel", condition = dataset_choice() %in% c("BEAT", "TCGA"))
})
#all of the pairwise tests need to run on the fly from dds.wald
# ye_16:
#[1] "Intercept"
# [2] "Source_blood_vs_bone_marrow"
# [3] "Source_gonadal_adipose_tissue_vs_bone_marrow"
# [4] "Source_normal_bm_vs_bone_marrow"
# [5] "Source_spleen_vs_bone_marrow"
# venaza
# [1] "Intercept" "condition_24hr_vs_control"
# [3] "condition_6hr_vs_control" "patient_FJ_vs_1020"
# [5] "patient_RB_vs_1020"
# Lagadinou
# [1] "Intercept"
# [2] "Treatment_high_PTL_5uM_vs_high_no_drug"
# [3] "Treatment_low_no_drug_vs_high_no_drug"
# [4] "Treatment_low_PTL_5uM_vs_high_no_drug"
# [5] "Batch"
# BEAT: resultsNames(dds.wald1) (the large datasets need to be run completely on the fly from
#ddstxi in order to extract the correct metadata)
# [1] "Intercept" "quantile_q2_vs_q1" "quantile_q3_vs_q1"
# [4] "quantile_q4_vs_q1"
# create a data module for those, and do the contrast here
## update pairwise choice for each dataset
# observe({
# if(dataset_choice() == "Ye_16"){
# updateCheckboxGroupButtons(
# session = session,
# inputId = "pwc",
# choices = c("blood_vs_bm", "gat_vs_bm", "normBM_vs_bm", "spleen_vs_bm") ,
# selected = NULL
# )
# } else if(dataset_choice() == "Venaza"){
# updateCheckboxGroupButtons(
# session = session,
# inputId = "pwc",
# choices = c("24hr_vs_control", "6hr_vs_control"),
# selected = NULL
# )
# } else if(dataset_choice() == "Lagadinou") {
# updateCheckboxGroupButtons(
# session = session,
# inputId = "pwc",
# choices = c("high_PTL_5uM_vs_high_no_drug", "low_no_drug_vs_high_no_drug", "low_PTL_5uM_vs_high_no_drug")
# )
# }
# })
observe({
if(dataset_choice() == "BEAT"){
updateSelectInput(
session = session,
inputId = "DEmodel",
choices = c("ven response quantile", "FAB morphology", "Denovo vs relapse") ,
selected = NULL
)
} else if(dataset_choice() == "TCGA"){
updateSelectInput(
session = session,
inputId = "DEmodel",
choices = c("FAB morphology", "Molecular classification", "RAS mutation", "NPM1 mutation"),
selected = NULL
)
}
})
observe({
if(dataset_choice() == "Ye_16"){
updateSelectInput(
session = session,
inputId = "pwc",
choices = c("blood_vs_bm", "gat_vs_bm", "normBM_vs_bm", "spleen_vs_bm") ,
selected = NULL
)
} else if(dataset_choice() == "Venaza"){
updateSelectInput(
session = session,
inputId = "pwc",
choices = c("24hr_vs_control", "6hr_vs_control"),
selected = NULL
)
} else if(dataset_choice() == "Lagadinou") {
updateSelectInput(
session = session,
inputId = "pwc",
choices = c("high_PTL_5uM_vs_high_no_drug", "low_no_drug_vs_high_no_drug", "low_PTL_5uM_vs_high_no_drug"),
selected = NULL
)
}
})
## run pairwise contrast depending on pairwise choice for each dataset
#DE Table ####
## for BEAT dataset, the ensembl id's need to be modified to work:
# dds.res1$ensembl_gene_id <- str_sub(dds.res1$ensembl_gene_id, end=-4)
# reactive to switch between mouse or human t2g
dds.res <- reactive({
if(data_species() == "human") {
res <- data.frame(results(dataset_dds())) %>%
rownames_to_column(., var = 'ensembl_gene_id') %>%
dplyr::select(., ensembl_gene_id, baseMean, log2FoldChange, padj) %>%
left_join(unique(dplyr::select(t2g_hs, c(ensembl_gene_id, ext_gene))), ., by = 'ensembl_gene_id') %>%
dplyr::rename(., Gene = ext_gene) %>%
mutate(., DiffExp = ifelse(padj < 0.05 & log2FoldChange >= 0.5, 'up',
ifelse(padj < 0.05 & log2FoldChange <= -0.5, 'down', 'no'))) %>%
na.omit(.)
} else if(data_species() == "mouse") {
res <- data.frame(results(dataset_dds())) %>%
rownames_to_column(., var = 'ensembl_gene_id') %>%
dplyr::select(., ensembl_gene_id, baseMean, log2FoldChange, padj) %>%
left_join(unique(dplyr::select(t2g_mm, c(ensembl_gene_id, ext_gene))), ., by = 'ensembl_gene_id') %>%
dplyr::rename(., Gene = ext_gene) %>%
mutate(., DiffExp = ifelse(padj < 0.05 & log2FoldChange >= 0.5, 'up',
ifelse(padj < 0.05 & log2FoldChange <= -0.5, 'down', 'no'))) %>%
na.omit(.)
}
res
})
output$PWresults <- renderDataTable({
if (input$PWDESeqtable == TRUE) {
dds.res()
}
})
#create objects for color palettes from the palette module
colorDE <-
colorServer("color10")
color2DE <-
colorServer("color11")
#Volcano Plot ####
output$PWvolplot <-
renderGirafe({
#colors <- c(colorDE(), "grey",color2DE()) #object for colors on volcano based on user input
colors <- c(colorDE(), "grey", color2DE())
if(input$PWvolcano == TRUE) { #only create plot if the volcano switch is toggled
p<- ggplot(dds.res(), aes( #call in the DE results from the DE module
x = `log2FoldChange`,
y = -log10(padj),
col = DiffExp,
tooltip = Gene
)) +
geom_point_interactive(size = 1, alpha = 0.5) +
theme_light() +
scale_color_manual(values = colors) +
ggtitle("DE Volcano Plot") +
coord_cartesian(xlim = c(-10, 7))
girafe(code = print(p))
}
})
#MA Plot ####
color3DE <-
colorServer("color12")
color4DE <-
colorServer("color13")
output$PWMAplot <-
renderGirafe ({
#colors <- c(color3DE(), "grey",color4DE()) #object for colors on volcano based on user input
colors <- c(color3DE(), "grey", color4DE())#object for colors on volcano based on user input called from palette module
if(input$PWMA == TRUE) { #only call plot if the MA plot switch is toggled
ma <- ggplot(dds.res(),
aes(
x = log2(baseMean),
y = `log2FoldChange`,
col = DiffExp,
tooltip = Gene
)) +
geom_point_interactive(alpha = 0.8, size = 0.5) +
geom_hline(aes(yintercept = 0)) +
scale_color_manual(values = colors) +
theme_light() +
ylim(c(
min(dds.res()$`log2FoldChange`),
max(dds.res()$`log2FoldChange`)
)) +
ggtitle("DE MA Plot") +
xlab("log2 Mean Expression") +
ylab("Log2 Fold Change")
girafe(code = print(ma))
}
})
#Heatmap ####
#interactive heatmap needs to be wrapped in a reactive function to work
observe({
req(input$DPWHeat)
ns <- NS(id)
color5DE <-
colorServer("color14")
color6DE <-
colorServer("color15")
#object for batch corrected vsd matrix
# assay(vsd) <-
# limma::removeBatchEffect(assay(vsd),
# batch = samples$batch,
# design = model.matrix(~ condition, data = samples))
# # data frame for batch corrected vsd matrix
# vstlimma <-
# data.frame(assay(vsd)) %>%
# rownames_to_column(., var = "ensembl_gene_id") %>%
# left_join(unique(dplyr::select(t2g_hs, c(
# ensembl_gene_id, ext_gene
# ))), ., by = 'ensembl_gene_id') %>%
# na.omit(.)
#filter DE object for only significantly differentially expressed genes
dds.mat <- dds.res %>%
dplyr::filter(padj < 0.05 & abs(`log2FoldChange`) >= 2)
#filter vst counts matrix by sig expressed genes
vst.mat <- vst %>%
dplyr::filter(., ensembl_gene_id %in% dds.mat$ensembl_gene_id) %>%
column_to_rownames(., var = "ensembl_gene_id") %>%
dplyr::select(.,-ext_gene) %>%
as.matrix()
rownames(vst.mat) = dds.mat$Gene
vst.mat <- t(scale(t(vst.mat)))
#only show the first 100 genes for visualization in this example(can change)
vst.mat <- head(vst.mat, n = 100)
#create a colorRamp function based on user input in color palette choices
colors = colorRamp2(c(-2, 0, 2), c(color5DE(), "white", color6DE()))
#create heatmap object
# if (isTruthy(input$DESeqHeat)) {
ht = draw(ComplexHeatmap::Heatmap(
vst.mat,
name = "z scaled expression",
col = colors,
row_names_gp = gpar(fontsize = 4),
row_km = 2,
top_annotation = HeatmapAnnotation(class = anno_block(gp = gpar(fill = c("white", "white")),
labels = c("prim", "mono"),
labels_gp = gpar(col = "black", fontsize = 10))),
column_km = 2,
column_title = NULL,
row_title = NULL
))
makeInteractiveComplexHeatmap(input, output, session, ht, heatmap_id = ns("ht"))
#}
})
# download DE table
output$downloadPWDEtable <- downloadHandler(
filename = function() { paste("DESeqTable", '.csv', sep='') },
content = function(file) {
write.csv(CD_DE_DT(),file)
}
)
#download Volcano
output$downloadPWVolcano <- downloadHandler(
filename = function() { paste(input$sigvaluesbutton, '.png', sep='') },
content = function(file) {
ggsave(file, device = "png", width = 8, height = 6, units = "in",dpi = 72)
}
)
#download MA
output$downloadPWMA <- downloadHandler(
filename = function() { paste('DESeqMAplot', '.png', sep='') },
content = function(file) {
ggsave(file, device = "png", width = 8, height = 6, units = "in",dpi = 72)
}
)
})
}