Repository navigation
Expand file tree
/
Copy pathapp.R
More file actions
317 lines (281 loc) · 12.3 KB
/
Copy pathapp.R
File metadata and controls
317 lines (281 loc) · 12.3 KB
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
## Import R package
library(RColorBrewer)
library(ggsci)
library(ggrepel)
library(spsComps)
library(shinyjs)
library(shinyBS)
library(shinythemes)
library(DT)
library(bs4Dash)
library(ggplot2)
## Set color
mycolors <- unique(c(brewer.pal(name = "Dark2", n = 8),
brewer.pal(name = "Accent", n = 7), brewer.pal(name = "Set1", n = 9),
brewer.pal(name = "Set3", n = 12), brewer.pal(name = "Set2", n = 8),
brewer.pal(name = "Paired", n = 5), brewer.pal(name = "Paired", n = 10)[7:10],
brewer.pal(name = "Pastel1", n = 9), brewer.pal(name = "Pastel2", n = 7)))
## Set user interface
ui <- dashboardPage(
dark = NULL,
help = NULL,
dashboardHeader(title = "MDD_GUI"),
dashboardSidebar(skin = "light", childIndent = TRUE, minified = FALSE,
## Set the input of cell majortype
checkboxGroupInput(inputId = "check", label = "MajorType",
choices = list("Excitatory Neurons",
"Inhibitory Neurons",
"Oligodendrocytes",
"Astrocytes",
"Microglia/Macrophage",
"Endothelial"),
selected = list("Excitatory Neurons", "Inhibitory Neurons", "Oligodendrocytes", "Astrocytes", "Microglia/Macrophage", "Endothelial")),
## Set the input of gene
textInput("gene",'Search for A Gene',value = 'PCP4'),
column(12, submitButton('search',icon("search")), align = 'center',
style = "margin-bottom: 10px;", style = "margin-top: -10px;")
),
## Set the body of display content
dashboardBody(
tags$style(HTML("
input:invalid {
background-color: #FFCCCC;
}")),
#### Set up shinyjs ####
useShinyjs(),
### shinyBS ###
bsAlert("alert"),
textOutput('text_error'),
## Boxes need to be put in a row (or column)
fluidRow(
## Show the information of this website
box(title = 'Info', id= "box_l",
helpText('Website for Visualizing the snRNA-seq Profile of the Brodmann Area 9 (BA9) of the Post-Mortem Brain Tissue of 17 Control Subjects and 17 Major Depressive Disorder (MDD) Cases.',
a('(See the paper more information. PMID: 32341540)', href = "https://doi.org/10.1038/s41593-020-0621-y",target="_blank")),
status = "info", width = 6),
## Show the flow of this study
box(title = 'The flow of the study', id= "box_r", img(src = "MDD_flow.png",style = 'width: 100%;'), status = "info", align='center'),
spsComps::heightMatcher("box_l","box_r")
),
fluidRow(
## Show the tSNE plot of cell majortypes
box(title = 'tSNE plot of MMD majortypes', id = 'box_m', status = "warning",
solidHeader = TRUE, collapsible = FALSE,
img(src = "MDD_Majortype.png", style = 'width: 90%;'), align='center'),
## Show the tSNE plot of cell subtypes
box(title = 'tSNE plot of MMD subtypes', id = 'box_s', status = "warning",
solidHeader = TRUE, collapsible = FALSE,
img(src = "MDD_subtype.png", style = 'width: 100%;'), align='center'),
spsComps::heightMatcher("box_m", "box_s"),
## Show the groups of cells
box(title = 'tSNE plot of MMD groups', id = 'box_t', status = "lightblue",
solidHeader = TRUE, collapsible = FALSE,
img(src = "MDD_group.png", style = 'width: 100%;'), align='center'),
## Location the tSNE plot of gene expression in cells
box(title = 'tSNE plot of gene expression', id = 'box_v', status = "lightblue",
solidHeader = TRUE, collapsible = FALSE,
plotOutput("tSNE_plot"),
downloadButton('tSNE_download', 'Download'),width = 6),
spsComps::heightMatcher("box_t", "box_v"),
## Location the violin plot of gene expression in cells
box(title = 'Violin plot of gene expression', status = "primary",
solidHeader = TRUE, collapsible = FALSE,
plotOutput("Violin_plot", height = 250),
downloadButton('Violin_download', 'Download'),width = 6),
## Location the box plot of gene expression in cells
box(title = 'Violin plot of gene expression between case and control', status = "primary",
solidHeader = TRUE, collapsible = FALSE,
plotOutput("Violin_plot_cc", height = 250),
downloadButton('Violin_download_cc', 'Download'),width = 6),
## Show mean of gene expression in cell majortypes
box(title = 'Summary of majortypes', status = 'success',
solidHeader = TRUE, collapsible = FALSE,
DTOutput("summary_m"), width = 6),
## Show mean of gene expression in cell subtypes
box(title = 'Summary of subtypes', status = 'success',
solidHeader = TRUE, collapsible = FALSE,
DTOutput("summary_s"), width = 6),
## Show the dataset of gene expression in cells
box(title = 'DataTable',
DTOutput("dataset"),
downloadButton('dataset_download', 'Download'), width = 12)
)
)
)
## Set server process
server <- function(session, input, output) {
## Input the expression data and meta data
datasetInput <- reactive({
mat.meta <- readRDS('scMDD.obj.meta.all.rds')
mat.exp <- readRDS('scMDD.obj.exp.all.rds')
if(input$gene %in% colnames(mat.exp)) {
mat.sub.exp <- as.matrix(mat.exp[,input$gene])
if(is.null(colnames(mat.sub.exp))) {
gene_name <- input$gene
colnames(mat.sub.exp) <- gene_name
mat.all <- cbind(mat.meta,mat.sub.exp)
return(mat.all)
}
} else{
return(as.matrix(0,nrow = nrow(mat.exp),ncol = 1))}
})
## data peocessing
datasetplt <- reactive({
dat.all <- datasetInput()
index.cell <- which(datasetInput()$MajorType %in% input$check)
dat.all$colours <- datasetInput()[,input$gene]
dat.plot <- dat.all[index.cell,]
return(dat.plot)
})
## Display of error
observeEvent(input$gene, {
if(!(input$gene %in% colnames(datasetInput()))) {
alert("ERROR: The input of 'Search for A Gene' may be incorrect or absent.")
}
})
## Display of error in bold
output$text_error <- renderText({
if(!(input$gene %in% colnames(datasetInput()))) {
shinyBS::createAlert(session, "alert", "myValueAlert", title = "Search for A Gene: Invalid input", style = "danger", content = "ERROR: The input of 'Search for A Gene' may be incorrect or absent.")
}
else{
shinyBS::closeAlert(session, "myValueAlert")
}
})
## Show the tSNE plot of gene expression in cells
output$tSNE_plot <- renderPlot({
dat.all <- datasetInput()
dat.all$colours <- datasetInput()[,input$gene]
ggplot(dat.all, aes(tSNE_1, tSNE_2)) + geom_point(aes(color = colours)) +
scale_colour_gradientn(colours=viridis:: plasma(100)) +
labs(x = '', y = '', title = 'Scatter Plot',col = 'Gene Expression') +
theme_classic() +
theme(plot.title = element_text(color = 'black',size = 15,face = 'bold',hjust = 0.5), axis.text =element_text(size = 12), legend.position = 'right')
})
## Download the tSNE plot of gene expression in cells
output$tSNE_download <- downloadHandler(
filename = function() {
paste0('tSNE plot of MMD.pdf')
},
content = function(file) {
dat.all <- datasetInput()
dat.all$colours <- datasetInput()[,input$gene]
pdf(file, width = 6, height = 5)
p1 <- ggplot(dat.all, aes(tSNE_1, tSNE_2)) + geom_point(aes(color = colours)) +
scale_colour_gradientn(colours=viridis:: plasma(100)) +
labs(x = '', y = '', title = 'Scatter Plot',col = 'Gene Expression') +
theme_classic() +
theme(plot.title = element_text(color = 'black',size = 15,face = 'bold',hjust = 0.5), axis.text =element_text(size = 12), legend.position = 'right')
print(p1)
dev.off()
}, contentType ="image/png"
)
## Show the violin plot of gene expression in cells
output$Violin_plot <- renderPlot({
dat.all <- datasetplt()
if (length(input$check) == 1) {
dat.all$plot_type <- dat.all[,'subtype']
}
else{
dat.all$plot_type <- dat.all[,'MajorType']
}
ggplot(dat.all) + geom_violin(aes(plot_type, colours, color = plot_type)) +
labs(x = '', y = '', title = 'Violin plot') +
scale_fill_manual(values = mycolors) +
theme_classic() +
theme(plot.title = element_text(color = 'black',size = 15,face = 'bold',hjust = 0.5), axis.text =element_text(size = 12), axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),legend.position = 'none')
})
## Download the violin plot of gene expression in cells
output$Violin_download <- downloadHandler(
filename = function() {
paste0('cluster_Vioplot', '_', input$gene, '.pdf', sep = '')
},
content = function(file) {
dat.all <- datasetplt()
if (length(input$check) == 1) {
dat.all$plot_type <- dat.all[,'subtype']
}
else{
dat.all$plot_type <- dat.all[,'MajorType']
}
pdf(file, width = 6, height = 5)
p2 <- ggplot(dat.all) + geom_violin(aes(plot_type, colours, color = plot_type)) +
labs(x = '', y = '', title = 'Violin plot') +
scale_fill_manual(values = mycolors) +
theme_classic() +
theme(plot.title = element_text(color = 'black',size = 15,face = 'bold',hjust = 0.5), axis.text =element_text(size = 12), axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),legend.position = 'none')
print(p2)
dev.off()
}
)
## Show the box plot of gene expression in cells
output$Violin_plot_cc <- renderPlot({
dat.all <- datasetplt()
if (length(input$check) == 1) {
dat.all$plot_type <- dat.all[,'subtype']
}
else{
dat.all$plot_type <- dat.all[,'MajorType']
}
ggplot(dat.all) + geom_violin(aes(plot_type, colours, color = Group)) +
labs(x = '', y = '', title = 'Violin plot') +
scale_fill_manual(values = mycolors) +
theme_classic() +
theme(plot.title = element_text(color = 'black',size = 15,face = 'bold',hjust = 0.5), axis.text =element_text(size = 12), axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),legend.position = 'right')
})
## Download the box plot of gene expression in cells
output$Violin_download_cc <- downloadHandler(
filename = function() {
paste0('case_control_violinplot', '_', input$gene, '.pdf', sep = '')
},
content = function(file) {
dat.all <- datasetplt()
if (length(input$check) == 1) {
dat.all$plot_type <- dat.all[,'subtype']
}
else{
dat.all$plot_type <- dat.all[,'MajorType']
}
pdf(file, width = 6, height = 5)
p3 <- ggplot(dat.all) + geom_violin(aes(plot_type, colours, color = Group)) +
labs(x = '', y = '', title = 'Violin plot') +
scale_fill_manual(values = mycolors) +
theme_classic() +
theme(plot.title = element_text(color = 'black',size = 15,face = 'bold',hjust = 0.5), axis.text =element_text(size = 12), axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),legend.position = 'right')
print(p3)
dev.off()
}
)
## Calculate the expression of majortypes
output$summary_m <- renderDT({
index.m <- which(datasetInput()$MajorType %in% input$check)
data.sub <- datasetInput()[index.m,]
data.set <- aggregate(data.sub[,7], list(data.sub$MajorType), mean)
datatable(data.set, colnames = c('MajorType', 'Mean'), options = list(pageLength = 6))
})
## Calculate the expression of subtypes
output$summary_s <- renderDT({
index.m <- which(datasetInput()$MajorType %in% input$check)
data.sub <- datasetInput()[index.m,]
data.set <- aggregate(data.sub[,7] ~ subtype, data.sub, mean)
datatable(data.set, colnames = c('Subtype', 'Mean'), options = list(pageLength = 6))
})
## View the expression of gene in each cells
output$dataset <- renderDT({
index.m <- which(datasetInput()$MajorType %in% input$check)
data.set <- datasetInput()[index.m, c('Clusters', 'MajorType', 'subtype', input$gene)]
})
## Download the expression of gene in each cells
output$dataset_download <- downloadHandler(
filename = function() {
paste0('Exp_MMD', input$gene, '_.csv', sep='')
},
content = function(file) {
index.m <- which(datasetInput()$MajorType %in% input$check)
data.set <- datasetInput()[index.m, c('Group', 'Clusters', 'MajorType', 'subtype', input$gene)]
write.csv(data.set, file)
}
)
}
## Run shinyApp
shinyApp(ui, server,option = list(launch.browser = TRUE))