-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscRNAviz-app.R
executable file
·291 lines (229 loc) · 9.64 KB
/
scRNAviz-app.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
library(shiny)
library(Seurat)
library(ggplot2)
library(plotly)
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
init_empty <- list(
placeholder = 'Please select an option below',
onInitialize = I('function() { this.setValue(""); }'))
rna_datasets <- list(messmer_et_al_2019 = "./data/messmer_et_al_2019.rds",
deng_et_al_2014 = "./data/deng_et_al_2014.rds",
mohammed_et_al_2017 = "./data/mohammed_et_al_2017.rds",
nakamura_et_al_2017 = "./data/nakamura_et_al_2017.rds",
nowotschin_et_al_2019 = "./data/nowotschin_et_al_2019.rds",
posfai_et_al_2017 = "./data/posfai_et_al_2017.rds",
stirparo_et_al_2018 = "./data/stirparo_et_al_2018.rds",
sara = "./data/sara-processed.rds"
)
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("Single cell RNA visualizer"),
# Sidebar layout with a input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Selector for choosing dataset ----
selectizeInput(inputId = "dataset",
label = "Dataset:",
options = init_empty,
multiple = FALSE,
choices = c("Messmer et al. (2019)",
"Deng et al. (2014)",
"Mohammned et al. (2017)",
"Nakamura et al. (2017)",
"Nowotschin et al. (2019)",
"Posfai et al. (2017)",
"Stirparo et al. (2018)",
"Sara")),
hr(),
conditionalPanel(
condition = "input.dataset != ''",
# Input: Visualization method ----
fluidRow(
column(5,
selectizeInput(inputId = "viz",
options = init_empty,
label = "Choose a visualization method:",
choices = c("PCA","t-SNE", "UMAP"))),
column(7,
selectizeInput(inputId = "metadata1",
label = "Choose metadata 1:",
options = NULL,
choices = NULL),
selectizeInput(inputId = "metadata2",
label = "Choose metadata 2:",
options = NULL,
choices = NULL)
)
),
hr(),
# Input: Gene ----
# Note: Changes made to the caption in the textInput control
# are updated in the output area immediately as you type
# textInput(inputId = "gene",
# label = "Check for gene expression:",
# value = "DNMT3L")
#
fluidRow(
column(8,
selectizeInput(inputId = "gene",
label = "Check for gene expression:",
choices = NULL,
options = NULL,
multiple = FALSE)),
column(4,
checkboxInput("sort","Sort cells"))
))
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Verbatim text for data summary ----
#verbatimTextOutput("summary"),
# Output: HTML table with requested number of observations ----
#tableOutput("view")
# Output: Histogram ----
fluidRow(
column(6,
plotlyOutput(outputId = "metadata1Plot")),
column(6,
plotlyOutput(outputId = "metadata2Plot"))
),
hr(),
fluidRow(
column(3),
column(6,
plotlyOutput(outputId = "genePlot")),
column(3)
),
hr(),
fluidRow(
column(6,
plotlyOutput(outputId = "metadata1VlnPlot")),
column(6,
plotlyOutput(outputId = "metadata2VlnPlot"))
),
)
)
)
# Define server logic to summarize and view selected dataset ----
server <- function(input, output, session) {
# Return the requested dataset ----
# By declaring datasetInput as a reactive expression we ensure
# that:
#
# 1. It is only called when the inputs it depends on changes
# 2. The computation and result are shared by all the callers,
# i.e. it only executes a single time
datasetInput <- reactive({
switch(input$dataset,
"Messmer et al. (2019)" = readRDS(rna_datasets$messmer_et_al_2019),
"Deng et al. (2014)" = readRDS(rna_datasets$deng_et_al_2014),
"Mohammned et al. (2017)" = readRDS(rna_datasets$mohammed_et_al_2017),
"Nakamura et al. (2017)" = readRDS(rna_datasets$nakamura_et_al_2017),
"Nowotschin et al. (2019)" = readRDS(rna_datasets$nowotschin_et_al_2019),
"Posfai et al. (2017)" = readRDS(rna_datasets$posfai_et_al_2017),
"Stirparo et al. (2018)" = readRDS(rna_datasets$stirparo_et_al_2018),
"Sara" = readRDS(rna_datasets$sara)
)
})
vizInput <- reactive({
switch(input$viz,
"PCA" = "pca",
"t-SNE" = "tsne",
"UMAP" = "umap")
})
observeEvent(input$dataset, {
req(input$dataset)
updateSelectizeInput(session, 'gene',
choices = c(sort(rownames(datasetInput()))),
server = TRUE)
updateSelectizeInput(session, 'metadata1',
choices = c(sort(colnames(datasetInput()@meta.data))),
selected = "seurat_clusters",
server = TRUE)
updateSelectizeInput(session, 'metadata2',
choices = c(sort(colnames(datasetInput()@meta.data))),
server = TRUE)
})
# This expression that generates a plot is wrapped in a call
# to renderPlot to indicate that:
#
# 1. It is "reactive" and therefore should be automatically
# re-executed when inputs (input$bins) change
# 2. Its output type is a plot
output$metadata1Plot <- renderPlotly({
req(input$dataset, input$viz, input$metadata1)
seurat_object <- datasetInput()
viz_method <- vizInput()
if (is.factor([email protected][[input$metadata1]])) {
ggplotly(DimPlot(seurat_object, reduction = viz_method, group.by = input$metadata1) + ggtitle(input$metadata1))
} else {
ggplotly(FeaturePlot(seurat_object, reduction = viz_method, features = input$metadata1) + ggtitle(input$metadata1))
}
})
output$metadata2Plot <- renderPlotly({
req(input$dataset, input$viz, input$metadata2)
seurat_object <- datasetInput()
viz_method <- vizInput()
if (is.factor([email protected][[input$metadata2]])) {
ggplotly(DimPlot(seurat_object, reduction = viz_method, group.by = input$metadata2) + ggtitle(input$metadata2))
} else {
ggplotly(FeaturePlot(seurat_object, reduction = viz_method, features = input$metadata2) + ggtitle(input$metadata2))
}
})
output$genePlot <- renderPlotly({
req(input$gene, input$viz)
seurat_object <- datasetInput()
viz_method <- vizInput()
plot_title <- paste0(input$gene," expression")
ggplotly(FeaturePlot(seurat_object, reduction = viz_method, features = c(input$gene), sort = input$sort) + ggtitle(plot_title))
})
output$metadata1VlnPlot <- renderPlotly({
req(input$dataset,input$gene)
factor_metadata <- is.factor(datasetInput()@meta.data[[input$metadata1]])
req(factor_metadata)
seurat_object <- datasetInput()
plot_title <- paste0(input$gene," expression by ", input$metadata1)
ggplotly(VlnPlot(seurat_object, features = c(input$gene), group.by = input$metadata1) + ggtitle(plot_title))
})
output$metadata2VlnPlot <- renderPlotly({
req(input$dataset,input$gene)
factor_metadata <- is.factor(datasetInput()@meta.data[[input$metadata2]])
req(factor_metadata)
seurat_object <- datasetInput()
plot_title <- paste0(input$gene," expression by ",input$metadata2)
ggplotly(VlnPlot(seurat_object, features = c(input$gene), group.by = input$metadata2) + ggtitle(plot_title))
})
# Create caption ----
# The output$caption is computed based on a reactive expression
# that returns input$caption. When the user changes the
# "caption" field:
#
# 1. This function is automatically called to recompute the output
# 2. New caption is pushed back to the browser for re-display
#
# Note that because the data-oriented reactive expressions
# below don't depend on input$caption, those expressions are
# NOT called when input$caption changes
# output$caption <- renderText({
# input$caption
# })
# Generate a summary of the dataset ----
# The output$summary depends on the datasetInput reactive
# expression, so will be re-executed whenever datasetInput is
# invalidated, i.e. whenever the input$dataset changes
# output$summary <- renderPrint({
# dataset <- datasetInput()
# summary(dataset)
# })
# Show the first "n" observations ----
# The output$view depends on both the databaseInput reactive
# expression and input$obs, so it will be re-executed whenever
# input$dataset or input$obs is changed
#output$view <- renderTable({
# head(datasetInput(), n = input$obs)
#})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)