-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 15.1 KB
/
.Rhistory
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
508
509
510
511
512
print(input$text)
print(length(input$text))
#if(length(reactive(as.character(input$text())))<64)
#{
# sheet_append(ss, data.frame(input$text))
#}
}
attempts(attempts() + 1)
# Commented the below section of code that saves the emial data in aws path
# write.table(input$text, "emails.csv", append = TRUE, row.names = FALSE, col.names = FALSE)
# put_object(
# file = "./emails.csv",
# object = "emails.csv",
# bucket = "sensoryomics-emails1",
# region = "us-east-2",
# key = "AKIA6I57NTAM2H7MHLTT",
# secret = "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"
# )
}
})
updateSelectizeInput(session, 'geneName', choices = gene_names, server = TRUE, selected = gene_names[1], options = list(maxItems = 6, maxOptions = 40))
updateSelectizeInput(session, 'selected_gene', choices = colnames(cleaned_data2[,4:20000]), server = TRUE, selected = col_names[4], options = list(maxItems = 6, maxOptions = 40))
updateSelectizeInput(session, 'Data_Set', choices = dataset_names, server = TRUE, selected = dataset_names[1], options = list(maxItems = 1, maxOptions = 10))
updateSelectizeInput(session, 'dataset', choices = dataset_names, server = TRUE, selected = dataset_names[1], options = list(maxItems = 1, maxOptions = 10))
# Create subsetted reactive list
sub_list <- reactive(subset(gene_names, (gene_names %in% input$geneName)))
debounce(sub_list, 5000)
sub_list_NPP <- reactive(subset(columnames, (columnames %in% input$geneName)))
#sub_list_NPP <- reactive(subset(columnames, (columnames %in% input$selected_gene)))
debounce(sub_list_NPP, 5000)
observe({
#shinyjs::toggle("p1", condition = isTRUE(length(sub_list_NPP())>0))
shinyjs::toggle("p2", condition = isTRUE(length(sub_list_NPP())>1))
shinyjs::toggle("p3", condition = isTRUE(length(sub_list_NPP())>2))
shinyjs::toggle("p4", condition = isTRUE(length(sub_list_NPP())>3))
shinyjs::toggle("p5", condition = isTRUE(length(sub_list_NPP())>4))
shinyjs::toggle("p6", condition = isTRUE(length(sub_list_NPP())>5))
})
# Validation message display code
# output$p1 <- renderPlotly({
# box_plots()
# })
box_vals <- reactiveValues()
plot_list <- reactiveVal(list())
# Create a list to store the generated plotly plots
plotly_plot_list <- reactiveVal(list())
# Create a list to store ggplot2 plots
ggplot_list <- reactiveVal(list())
output$p1 <- renderPlotly({
validate(
need(input$geneName,HTML("Please Select the Gene!"))
)
selection <- which(input$geneName[1]==colnames(cleaned_data2), arr.ind = TRUE)
bp1 <- graph_gene(selection)
#plotly_plot_list(c(plotly_plot_list(), list(bp1)))
plot_list <- plot_list(list(bp1))
box_vals$plt <- bp1
return(bp1)
})
output$p2 <- renderPlotly({
if (length(input$geneName)<2){
return(NULL)
}else{
selection <- which(input$geneName[2]==colnames(cleaned_data2), arr.ind = TRUE)
bp2 <- graph_gene(selection)
#plotly_plot_list(c(plotly_plot_list(), list(bp2)))
#box_vals$plt <- bp2
return(bp2)
}
})
output$p3 <- renderPlotly({
if (length(input$geneName)<3){
return(NULL)
}else{
selection <- which(input$geneName[3]==colnames(cleaned_data2), arr.ind = TRUE)
bp3 <- graph_gene(selection)
#plotly_plot_list(append(plotly_plot_list(), list(bp3)))
#box_vals$plt <- bp3
return(bp3)
}
})
output$p4 <- renderPlotly({
if (length(input$geneName)<4){
return(NULL)
}else{
selection <- which(input$geneName[4]==colnames(cleaned_data2), arr.ind = TRUE)
bp4 <- graph_gene(selection)
#plotly_plot_list(append(plotly_plot_list(), list(bp4)))
return(bp4)
}
})
output$p5 <- renderPlotly({
if (length(input$geneName)<5){
return(NULL)
}else{
selection <- which(input$geneName[5]==colnames(cleaned_data2), arr.ind = TRUE)
bp5 <- graph_gene(selection)
#plotly_plot_list(append(plotly_plot_list(), list(bp5)))
return(bp5)
}
})
output$p6 <- renderPlotly({
if (length(input$geneName)<6){
return(NULL)
}else{
selection <- which(input$geneName[6]==colnames(cleaned_data2), arr.ind = TRUE)
bp6 <- graph_gene(selection)
#plotly_plot_list(append(plotly_plot_list(), list(bp6)))
return(bp6)
}
})
######### rendering the table based on dataset choosen #######
## creating reactive table for data set selected
select_dataset_main <- reactive({
debounce(sub_list, 5000)
tbl <- reactive(subset(AE_rna, (gene_names %in% sub_list())))
debounce(tbl, 5000)
tbl()
return(tbl())
})
# Output for the table on the all gene data tab
output$datatable <- renderDataTable(select_dataset_main(),
options = list(scrollX = TRUE, scrollY = TRUE, autoWidth = TRUE)
)
##
select_dataset_npp <- reactive({
debounce(sub_list_NPP, 5000)
tbl1 <- reactive(subset(updated_data_main, (GeneName %in% c("pain state","Age","sex",sub_list_NPP()))))
debounce(tbl1, 5000)
tbl1()
return(tbl1())
})
# Output for the table on the all gene data tab
output$datatable_npp <- renderDataTable(select_dataset_npp(),
options = list(scrollX = TRUE, scrollY = TRUE, autoWidth = TRUE)
)
##############################################################
# This is used to store the plots that will be downloaded
vals <- reactiveValues()
# Create a reactive plot list that will be used to display the plots on the page
plts <- reactive({
if (input$plt == "Dot Plot") {
plt <- DotPlot(drg, features = sub_list())
vals$plt <- plt
return(plt)
} else if (input$plt == "Dim Plot") {
plt <- DimPlot(drg)
vals$plt <- plt
return(plt)
} else if (input$plt == "Violin Plot") {
L = length(sub_list())
plt <- VlnPlot(drg, features = sub_list(), combine = FALSE)
nc = 2
if (L <= 2) {
nc = 1
}
plt1 <- VlnPlot(drg, features = sub_list(), ncol = nc, combine = TRUE)
vals$plt <- plt1
return(plt)
} else if (input$plt == "Ridge Plot") {
L = length(sub_list())
plt <- RidgePlot(drg, features = sub_list(), combine = FALSE)
nc = 2
if (L <= 2) {
nc = 1
}
plt1 <- RidgePlot(drg, features = sub_list(), ncol = nc, combine = TRUE)
vals$plt <- plt1
return(plt)
} else if (input$plt == "Feature Plot") {
L = length(sub_list())
plt <- FeaturePlot(drg, features = sub_list(), combine = FALSE)
nc = 2
if (L <= 2) {
nc = 1
}
plt1 <- FeaturePlot(drg, features = sub_list(), ncol = nc, combine = TRUE)
vals$plt <- plt1
return(plt)
}
})
#trying to make the page length increase with no of selections increase
observe({
shinyjs::toggle("plot2", condition = isTRUE(length(sub_list())>1 && input$plt != "Dot Plot" && input$plt != "Dim Plot"))
shinyjs::toggle("plot3", condition = isTRUE(length(sub_list())>2 && input$plt != "Dot Plot" && input$plt != "Dim Plot"))
shinyjs::toggle("plot4", condition = isTRUE(length(sub_list())>3 && input$plt != "Dot Plot" && input$plt != "Dim Plot"))
shinyjs::toggle("plot5", condition = isTRUE(length(sub_list())>4 && input$plt != "Dot Plot" && input$plt != "Dim Plot"))
shinyjs::toggle("plot6", condition = isTRUE(length(sub_list())>5 && input$plt != "Dot Plot" && input$plt != "Dim Plot"))
})
# Outputs for the first-sixth plot positions
output$plot1 <- renderPlot({
validate(
need(input$geneName,HTML("Please Select the Gene!"))
)
if (input$plt == "Dot Plot" | input$plt == "Dim Plot") {
plts()
} else if (input$plt == "Violin Plot" | input$plt == "Ridge Plot" | input$plt == "Feature Plot") {
plts()[1]
}
})
output$plot2 <- renderPlot({
if (input$plt == "Violin Plot" | input$plt == "Ridge Plot" | input$plt == "Feature Plot") {
plts()[2]
}
})
output$plot3 <- renderPlot({
if (input$plt == "Violin Plot" | input$plt == "Ridge Plot" | input$plt == "Feature Plot") {
plts()[3]
}
})
output$plot4 <- renderPlot({
if (input$plt == "Violin Plot" | input$plt == "Ridge Plot" | input$plt == "Feature Plot") {
plts()[4]
}
})
output$plot5 <- renderPlot({
if (input$plt == "Violin Plot" | input$plt == "Ridge Plot" | input$plt == "Feature Plot") {
plts()[5]
}
})
output$plot6 <- renderPlot({
if (input$plt == "Violin Plot" | input$plt == "Ridge Plot" | input$plt == "Feature Plot") {
plts()[6]
}
})
## creating reactive table for data set selected
select_dataset <- reactive({
if(input$Data_Set == "Spatial Transcriptomics (DRG)"){
#ds_selected <- "DRG_Human_Noiceptor"
return(AE_rna)
}
else if(input$Data_Set=="Neuropathic Pain (DRG)"){
#ds_selected <- "Neuropathic_Pain"
return(updated_data)
}
})
# Output for the table on the all gene data tab
output$dataset_table <- renderDataTable(select_dataset(), options = list(
scrollX = TRUE
)
)
# Output for the download button
output$download_plots <- downloadHandler(
filename = function() {
paste(input$plt,".pdf",sep = "")
},
content = function(file) {
pdf(file, width = 15, height = 15)
print(vals$plt)
dev.off()
}
)
# Output for the download button
output$download_boxplots <- downloadHandler(
filename = function() {
paste("Neuropathic Pain Plots",".pdf",sep = "")
},
content = function(file) {
# Create a PDF file to save all the plots
pdf(NULL)
pdf(file, width = 8, height = 6)
print(box_vals$plt)
dev.off()
}
)
# Creating a reactive expression to generate dynamic text description
dynamic_text <- reactive({
selected_option <- input$Data_Set
# You can use if-else or switch statements to define the dynamic text based on the selection
if (selected_option == "Spatial Transcriptomics (DRG)") {
return(HTML("<p><b>Description:</b> You have selected <i><b><span style='color:#4D0202;'>Spatial Transcriptomics (DRG)</span></b></i> dataset. This data was generated using single-neuron resolution approach, more detais visit- <a href='https://pubmed.ncbi.nlm.nih.gov/35171654/'>Article Link</a>. Here you can find the average gene expression for each of the 12 neuronal subtypes.</p>"))
#return (dataset1_desc)
#return("Description: You selected spatial transcriptomics (DRG) dataset. This data was generated using single-neuron resolution approach (more detais - https://pubmed.ncbi.nlm.nih.gov/35171654/). Here you can find the average gene expression for each of the 12 neuronal subtypes.</p>")
} else if (selected_option == "Neuropathic Pain (DRG)") {
return(HTML("<p><b>Description:</b> You have selected <i><b><span style='color:#4D0202;'>Neuropathic Pain (DRG)</span></b></i> dataset.This data was generated using sequenced human dorsal root ganglia, more details visit- <a href='https://academic.oup.com/brain/article/146/2/749/6648727?login=true'>Journal Link</a>. Here you can find the quantile normalized TPMs of neuron-enriched samples.</p>"))
#return (dataset2_desc)
#return("You have selected Neuropathic Pain (DRG) dataset.")
} else {
return("Selet the Dataset you want to view.")
}
})
# creating a reactive expression to change the dataset for download handler
output$download_dataset <- downloadHandler(
filename = function(){
paste(input$Data_Set,"csv",sep = ".")
},
content = function(file){
write.csv(select_dataset(),file,sep = ",")
}
)
# Display the dynamic text in the textOutput element
output$description_text <- renderUI({
dynamic_text()
})
end <- Sys.time()
print(end - beginning)
})
}
graph_gene <- function(gene){
# turning Pain States in categories
cleaned_data2$painState <- as.factor(cleaned_data2$painState)
# get all the column names and the gene name
col_names <- colnames(cleaned_data2)
gene_name <- col_names[gene] # change this value to change which gene shows up
# load the name of the gene into a variable
gene_data <- as.numeric(cleaned_data2[[gene_name]])
## plotly graph
mrg <- list(l = 55, r = 50,
b = 50, t = 50,
pad = 20)
p <- plot_ly(
data = cleaned_data2,
y = ~gene_data,
x = ~painState,
type = "box",
color = ~sex,
showlegend = TRUE,boxpoints = "all", jitter = 0.3, pointpos = 0, marker = list(color = 'black'), colors = c("#ff5A1D","#366676")
) %>% layout(boxmode = "group",
title = list(text = paste0("<b>",gene_name, "</b>")),
xaxis = list(title = "<b>Pain State</b>",
zeroline = FALSE),
yaxis = list(title = paste0(c(rep(" ", 20),
"<b>TPM</b>",
rep(" ", 20),
rep("\n ", 1)),
collapse = ""),
zeroline = FALSE,font = list(size = 15), standoff = 50L), margin = mrg,showlegend = TRUE, legend = list(font = list(size = 15)))
config(p, displayModeBar = TRUE, toImageButtonOptions = list(format= 'png', # one of png, svg, jpeg, webp
filename= paste0(gene_name,' - Average TPM Boxplot'),
height= 500,
width= 1000,
scale= 1 ))
}
# Run the application
shinyApp(ui = ui, server = server)
runApp('App1.R')
library(shiny); runApp('App1.R')
runApp()
runApp()
runApp()
runApp()
shiny::runApp()
runApp()
shiny::runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
shiny::runApp()
runApp()
runApp()
shiny::runApp()
runApp()
runApp()
runApp()
runApp()
shiny::runApp()
runApp()
runApp()
shiny::runApp()
shiny::runApp()
runApp()
shiny::runApp()
runApp()
shiny::runApp()
shiny::runApp()
runApp()
shiny::runApp()
runApp()
runApp()
runApp()
shiny::runApp()
runApp()
shiny::runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
# Load required libraries
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(shinyjs)
library(profvis)
library(shinythemes)
library(DT)
library(Seurat)
library(ggplot2)
library(rpivotTable)
library(tidyverse)
library(gridExtra)
library(cowplot)
library(ggpubr)
library(dplyr)
library(shinyalert)
library(aws.s3)
library(Matrix)
library(readxl)
library(data.table)
library(plotly)
library(shinyvalidate)
library(orca)
library(shinyLP)
library(future)
library(promises)
library(googlesheets4)
options(warn = -1) # help in suppressing the warnings in the console
# Google sheets authentication
gs4_auth(cache=".secrets", email="[email protected]")
ss <- gs4_get("https://docs.google.com/spreadsheets/d/1zQLx_i-Y4sUikD79r6fx8vJFi-TuBWilAlC66HtXXSE/edit?usp=sharing")
# Import the data
#drg <- drg.combined
# read rds
drg <- readRDS("drg.combined.rds") #As already exists in the work space, no need to read a
DefaultAssay(drg) <- "RNA"
AE <- AverageExpression(drg)
AE_rna <- AE$RNA
updated_data <- read_excel("cleaned_data2.xlsx") #As already exists in the work space, no need to read again
#print(class(updated_data))
updated_data_main <- as.data.frame(updated_data)
# clean the data
updated_data <- updated_data[1:20000,]
col_names <- transpose(updated_data[1])
# Load the cleaned data object as cleaned_data2
cleaned_data2 <- readRDS("cleaned_data2.rds")
columnames <- colnames(cleaned_data2[,4:20000])
#Check if there is equal number of the Gene types in the both the data sets or not?
#columnames <- colnames(cleaned_data2[,4:20000])
## All genes addition
gene_check <- read.csv("all_genes.csv")
gene_names1 <- as.character(gene_check$gene_names)
gene_names <- rownames(AE_rna)
dataset_names <- c("Spatial Transcriptomics (DRG)","Neuropathic Pain (DRG)")
#ds_selected <- "DRG_Human_Noiceptor"
plot_list <- c("Dot Plot", "Violin Plot", "Ridge Plot", "Feature Plot", "Dim Plot")
# Adding background color for filter and search in tables
callback <- c(
"$('#DataTables_Table_0_length select').css('background-color', 'white');",
"$('#DataTables_Table_0_filter input').css('background-color', 'white');"
)
callback1 <- c(
"$('#DataTables_Table_1_length select').css('background-color', 'white');",
"$('#DataTables_Table_1_filter input').css('background-color', 'white');"
)
runApp()
runApp()
shiny::runApp()
shiny::runApp()
runApp()
runApp()
runApp()
shiny::runApp()
runApp()
runApp()
shiny::runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
shiny::runApp()
shiny::runApp()
runApp()
runApp()
shiny::runApp()
runApp()
shiny::runApp()
shiny::runApp()
runApp()
runApp()
shiny::runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
shiny::runApp()
runApp()
shiny::runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()