-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
348 lines (321 loc) · 12.4 KB
/
server.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
shiny_env = new.env()
# Define server logic required to draw the boxplot and render metadata table
server <- function(input, output, session) {
rv <- reactiveValues()
rv$run2 <- 0
rv$init <- 0
rv$actuallygo <- 0
rv$old <- ""
rv$plot_temp <- data.frame()
rv$go <- 0
rv$tabinit_plot <- 0
rv$starttutorial <- 0
rv$data_prev <- data.frame()
# hide some checkboxes
removeModal()
hide("Find")
# init
observeEvent(rv$init, {
if (rv$init == 0) {
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[["gene"]])) {
updateSelectizeInput(session,
inputId = "geneID",
selected = query[["gene"]],
choices = autocomplete_list,
server = T
)
} else {
updateSelectizeInput(session,
inputId = "geneID",
selected = "name",
choices = autocomplete_list,
server = T
)
}
rv$init <- 1
rv$run2 <- 1
}
})
# sortable or not
jqui_sortable(ui = "#sidediv", operation = "enable", options = list(cancel = ".datatables"))
jqui_sortable(ui = "#tabMain", operation = "enable")
observe({
if (input$doLock == TRUE) {
jqui_sortable(ui = "#sidediv", operation = "destroy")
jqui_sortable(ui = "#tabMain", operation = "destroy")
} else if (input$doLock != TRUE) {
jqui_sortable(ui = "#sidediv", operation = "enable", options = list(cancel = ".form-control"))
jqui_sortable(ui = "#tabMain", operation = "enable")
}
})
observeEvent(input$geneID, {
if (rv$run2 == 1 & input$geneID != "" & !is.null(input$geneID)) {
rv$run2 <- 1
shinyjs::click("Find")
}
})
# find on clicking button
onclick(
"geneID",
updateSelectizeInput(session,
inputId = "geneID",
selected = "",
choices = autocomplete_list,
server = T
)
)
# jump to plot
observeEvent(input$Find, {
if ((input$geneID != "") & (input$geneID != rv$old)) {
rv$actuallygo <- rv$actuallygo + 1
if (rv$init == 1) {
rv$init <<- 2
} else {
updateQueryString(paste0("?gene=", input$geneID), mode = "push")
}
}
})
# query
inid <- eventReactive(rv$actuallygo,
{
rv$old <<- input$geneID
shinyjs::runjs("window.scrollTo(0, 0)")
input$geneID
},
ignoreNULL = T,
ignoreInit = T
)
# dimplot1
dimPlot1 <- reactive({
gene1 <- inid()
slide1 <- input$sel1
if (gene1 %in% cats) {
if (slide1 == "umap") {
g <- DimPlot(s, group.by = gene1, label = TRUE) +
scale_color_viridis(discrete = TRUE, option = "turbo", alpha = 0.24, guide = guide_legend(ncol = 1)) +
ggtitle("") +
labs(colour = gene1) +
guides(col = guide_legend(ncol = 1))
} else if (str_detect(slide1, "H&E")) {
g <- SpatialDimPlot(s, group.by = gene1, images = str_remove(slide1, "_H&E"), alpha = 0, image.alpha = 0.67, stroke = 0) +
scale_fill_viridis(discrete = TRUE, option = "turbo", alpha = 0) +
ggtitle("") +
guides(col = guide_legend(ncol = 1)) +
theme(legend.text = element_text(color = "white"), legend.title = element_text(color = "white"))
} else {
g <- SpatialDimPlot(s, group.by = gene1, images = slide1, alpha = 0.34, image.alpha = 0.23, stroke = 0) +
scale_fill_viridis(discrete = TRUE, option = "turbo", alpha = 0.24) +
ggtitle("") +
guides(col = guide_legend(ncol = 1))
}
} else {
if (slide1 == "umap") {
g <- FeaturePlot(s, gene1, order = TRUE) +
scale_color_gradient(low = "#F5F5F5", high = "red") +
ggtitle("") +
labs(colour = gene1)
} else if (str_detect(slide1, "H&E")) {
g <- SpatialFeaturePlot(s, gene1, images = str_remove(slide1, "_H&E"), alpha = 0, image.alpha = 0.67, stroke = 0) +
theme(legend.position = "right") +
scale_fill_gradient(low = "#F5F5F5", high = "red") +
guides(fill = guide_legend(override.aes = list(alpha = 0))) +
theme(legend.text = element_text(color = "white"), legend.title = element_text(color = "white"))
} else {
g <- SpatialFeaturePlot(s, gene1, images = slide1, alpha = c(0.23, 0.67), image.alpha = 0.23, stroke = 0) +
theme(legend.position = "right") +
scale_fill_gradient(low = "#F5F5F5", high = "red")
}
}
})
dimPlotr1 <- reactive({
g <- dimPlot1()
output$dimPlot1 <- renderPlot(g)
plotOutput("dimPlot1", width = as.numeric(input$plotw) * 100, height = as.numeric(input$ploth) * 100)
})
# dimplot-plotly
dimPlotlyr1 <- reactive({
g <- dimPlot1()
output$dimPlot1 <- renderPlotly(ggplotly(g, tooltip = "text") %>%
layout(hovermode = "closest") %>%
config(displayModeBar = FALSE))
plotlyOutput("dimPlot1", width = as.numeric(input$plotw) * 100, height = as.numeric(input$ploth) * 100)
})
# actually draw dimplot
output$dimPlotUI1 <- renderUI({
if (rv$init >= 1 & rv$run2 >= 1) {
dimPlotr1()
} else {
plotOutput("dimPlot1", width = as.numeric(input$plotw) * 100, height = as.numeric(input$ploth) * 100)
}
})
# dimplot2
dimPlot2 <- reactive({
gene2 <- inid()
slide2 <- input$sel2
if (gene2 %in% cats) {
if (slide2 == "umap") {
g <- DimPlot(s, group.by = gene2, label = TRUE) +
scale_color_viridis(discrete = TRUE, option = "turbo", alpha = 0.24, guide = guide_legend(ncol = 1)) +
ggtitle("") +
labs(colour = gene2) +
guides(col = guide_legend(ncol = 1))
} else if (str_detect(slide2, "H&E")) {
g <- SpatialDimPlot(s, group.by = gene2, images = str_remove(slide2, "_H&E"), alpha = 0, image.alpha = 0.67, stroke = 0) +
scale_fill_viridis(discrete = TRUE, option = "turbo", alpha = 0) +
ggtitle("") +
guides(col = guide_legend(ncol = 1)) +
theme(legend.text = element_text(color = "white"), legend.title = element_text(color = "white"))
} else {
g <- SpatialDimPlot(s, group.by = gene2, images = slide2, alpha = 0.34, image.alpha = 0.23, stroke = 0) +
scale_fill_viridis(discrete = TRUE, option = "turbo", alpha = 0.24) +
ggtitle("") +
guides(col = guide_legend(ncol = 1))
}
} else {
if (slide2 == "umap") {
g <- FeaturePlot(s, gene2, order = TRUE) +
scale_color_gradient(low = "#F5F5F5", high = "red") +
ggtitle("") +
labs(colour = gene2)
} else if (str_detect(slide2, "H&E")) {
g <- SpatialFeaturePlot(s, gene2, images = str_remove(slide2, "_H&E"), alpha = 0, image.alpha = 0.67, stroke = 0) +
theme(legend.position = "right") +
scale_fill_gradient(low = "#F5F5F5", high = "red") +
guides(fill = guide_legend(override.aes = list(alpha = 0))) +
theme(legend.text = element_text(color = "white"), legend.title = element_text(color = "white"))
} else {
g <- SpatialFeaturePlot(s, gene2, images = slide2, alpha = c(0.23, 0.67), image.alpha = 0.23, stroke = 0) +
theme(legend.position = "right") +
scale_fill_gradient(low = "#F5F5F5", high = "red")
}
}
})
dimPlotr2 <- reactive({
g <- dimPlot2()
output$dimPlot2 <- renderPlot(g)
plotOutput("dimPlot2", width = as.numeric(input$plotw) * 100, height = as.numeric(input$ploth) * 100)
})
# dimplot-plotly
dimPlotlyr2 <- reactive({
g <- dimPlot2()
output$dimPlot2 <- renderPlotly(ggplotly(g, tooltip = "id") %>%
layout(hovermode = "closest") %>%
config(displayModeBar = FALSE))
plotlyOutput("dimPlot2", width = as.numeric(input$plotw) * 100, height = as.numeric(input$ploth) * 100)
})
# actually draw dimplot
output$dimPlotUI2 <- renderUI({
if (rv$init >= 1 & rv$run2 >= 1) {
dimPlotr2()
} else {
plotOutput("dimPlot2", width = as.numeric(input$plotw) * 100, height = as.numeric(input$ploth) * 100)
}
})
# violinplot
violinPlot1 <- reactive({
gene3 <- inid()
if (gene3 %in% cats) {
df2 <- [email protected] %>% select(sample, gene3) %>%
setNames(c("sample", "gene3")) %>%
group_by(sample, gene3) %>% summarise(n = n()) %>%
group_by(sample) %>% mutate(n = n/sum(n)) %>%
mutate(sample = factor(sample, levels = mixedsort(unique(s$sample))))
g <- ggplot(df2,
aes(x = sample, y = n, fill = gene3)) +
geom_col() +
theme_classic() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), axis.text = element_text(size = 8), axis.title = element_text(size = 8), legend.text = element_text(size = 8)) +
xlab("") +
ylab("Fraction") +
labs(fill = "") +
ggtitle("") +
scale_fill_viridis(discrete = TRUE, option = "turbo")
} else {
g <- ggplot(FetchData(s, c("name", gene3)) %>% setNames(c("sample", "gene3")),
aes(x = sample, y = gene3, fill = sample)) +
geom_violin(width = 1.2, alpha = 0.5) +
theme_classic() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), axis.text = element_text(size = 8), axis.title = element_text(size = 8), legend.text = element_text(size = 8)) +
xlab("") +
ylab("Expression") +
labs(fill = "") +
ggtitle("") +
NoLegend()
}
})
violinPlotr1 <- reactive({
g <- violinPlot1()
output$violinPlot1 <- renderPlot(g)
plotOutput("violinPlot1", width = as.numeric(input$plotw) * 200, height = as.numeric(input$ploth) * 100)
})
# dimplot-plotly
violinPlotly1 <- reactive({
g <- violinPlot1()
output$violinPlot1 <- renderPlotly(ggplotly(g, tooltip = "id") %>%
layout(hovermode = "closest") %>%
config(displayModeBar = FALSE))
plotlyOutput("violinPlot1", width = as.numeric(input$plotw) * 200, height = as.numeric(input$ploth) * 100)
})
# actually draw dimplot
output$violinPlotUI1 <- renderUI({
if (rv$init >= 1 & rv$run2 >= 1) {
violinPlotr1()
} else {
plotOutput("violinPlot1", width = as.numeric(input$plotw) * 200, height = as.numeric(input$ploth) * 100)
}
})
observeEvent(input$tutorial, {
introjs(session,
options = list(
"nextLabel" = ">",
"prevLabel" = "<",
"skipLabel" = "skip",
"overlayOpacity" = -1
) # ,events = list("onexit" = I("alert('abc')"))
)
})
observeEvent(rv$run2, {
if (start_tutorial & rv$starttutorial == 0) {
rv$starttutorial <- 1
introjs(session, options = list(
"nextLabel" = ">",
"prevLabel" = "<",
"skipLabel" = "skip",
"overlayOpacity" = -1
)) # events = list("onexit" = I("document.getElementsByClassName('introjs-nextbutton').blur()")))
}
})
output$intro <- renderUI({
clean <- a(" (link) ",
href = manuscriptL,
target="_blank"
)
tagList(tags$h6(manuscriptD, clean))
})
output$intro2 <- renderUI({
clean2 <- a("Pediatric Neuro-oncology Cell Atlas",
href = url,
target="_blank"
)
tagList(tags$h6("This is part of an ongoing effort of the ", clean2,
"lead by the Department of Pediatrics, the Morgan Adams Foundation Pediatric Brain Tumor Research Program, and the RNA Bioscience Initiative at the University of Colorado Anschutz Medical Campus."))
})
output$rawdata <- renderUI({
url <- str_c(
"https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=", geoN
)
clean <- a(geoN,
href = url,
target="_blank"
)
tagList(tags$h6("Data and metadata are deposited on NCBI Gene Expression Omnibus at ", clean))
})
output$GitHub <- renderUI({
clean <- a(paste0("v", versionN),
href = giturl,
target="_blank"
)
tagList(tags$p(icon("github-square"), clean))
})
}