-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_visium_vis.R
208 lines (184 loc) · 10.8 KB
/
app_visium_vis.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
# Set working directory
wd <- getwd()
library(shiny)
library(Seurat)
library(ggplot2)
library(stringr)
# List of allowed metadata columns
allowed_metadata <- c("dapi_avg", "cd11c_avg", "lang_avg", "bcell_avg", "gland", "la_in", "la_out", "epi", "epi_dist",
"sm_out", "wall", "wall_dist", "capsule", "capsule_dist", "percent.mt")
# UI for Spatial App
ui <- fluidPage(
fluidRow(
column(3,
wellPanel(
titlePanel("Datasets"),
uiOutput("dataset_selector_spat"),
actionButton("update_dataset_spat", "Update Dataset", width = "100%")
),
wellPanel(
titlePanel("Metadata Selector"),
uiOutput("metadata_selector_spat") # Dropdown for metadata selection
),
fluidRow(
column(12, wellPanel(plotOutput("meta_plot", height = "600px")))
),
fluidRow(
column(6, textInput("pt_alpha3", "pt/alpha:", value = "1.6,1,1,1", width = "100%")),
column(6, textInput("min_max3", "Min_Max:", value = "NA,NA", width = "100%"))
)
),
column(9,
fluidRow(
column(6, wellPanel(plotOutput("feature_plot_spat1", height = "600px"))),
column(6, wellPanel(plotOutput("feature_plot_spat2", height = "600px")))
),
fluidRow(
column(3, textInput("filename_prefix_spat", "Filename prefix:", value = "user", width = "100%")),
column(3, textInput("feature_gene_spat1", "Gene to Plot 1:", value = "CD3E", width = "100%")),
column(3, textInput("feature_gene_spat2", "Gene to Plot 2:", value = "MS4A1", width = "100%")),
column(3,
tags$div(
HTML("<strong>Export Feature Plots:</strong>"),
actionButton("export_feature_spat", "Export Feature Plots", width = "100%")
)
)
),
fluidRow(
column(3, textInput("pt_alpha1", "pt/alpha:", value = "1.6,1,1,1", width = "100%")),
column(3, textInput("min_max1", "Min_Max:", value = "NA,NA", width = "100%")),
column(3, textInput("pt_alpha2", "pt/alpha:", value = "1.6,1,1,1", width = "100%")),
column(3, textInput("min_max2", "Min_Max:", value = "NA,NA", width = "100%"))
)
)
)
)
# Server for Spatial App
server <- function(input, output, session) {
# Reactive variable for Spatial data
seurat_data_spat <- reactiveVal(NULL)
# Dataset selector for Spatial
output$dataset_selector_spat <- renderUI({
rds_files <- list.files(path = "vis/", pattern = "*.rds", full.names = FALSE)
selectInput("selected_dataset_spat", "Choose a Spatial dataset:", choices = rds_files, selected = NULL)
})
# Load Spatial dataset and update UI
observeEvent(input$update_dataset_spat, {
req(input$selected_dataset_spat)
gc() # Clean memory before loading new data
data <- readRDS(file.path("vis/", input$selected_dataset_spat))
seurat_data_spat(data)
gc() # Clean memory after loading new data
# Extract metadata columns that match the allowed list
metadata_columns <- colnames(seurat_data_spat()@meta.data)
available_metadata <- metadata_columns[metadata_columns %in% allowed_metadata]
# If there are matching metadata columns, generate dropdown, else display a message
if (length(available_metadata) > 0) {
output$metadata_selector_spat <- renderUI({
selectInput("selected_metadata_spat", "Choose a Metadata Column:", choices = available_metadata, selected = available_metadata[1])
})
} else {
output$metadata_selector_spat <- renderUI({
tags$p("No relevant metadata columns found in this dataset.")
})
}
# First SpatialFeaturePlot
output$feature_plot_spat1 <- renderPlot({
feature_gene <- input$feature_gene_spat1
if (!(feature_gene %in% rownames(seurat_data_spat()))) {
feature_gene <- "CD3E"
ggtitle_text <- paste0(input$feature_gene_spat1, " is not found in the dataset, displaying default CD3E")
} else {
ggtitle_text <- feature_gene
}
SpatialFeaturePlot(seurat_data_spat(), features = input$feature_gene_spat1,
alpha = c(as.numeric(str_split(input$pt_alpha1, "\\,")[[1]][2]), as.numeric(str_split(input$pt_alpha1, "\\,")[[1]][3])),
pt.size = as.numeric(str_split(input$pt_alpha1, "\\,")[[1]][1]),
min.cutoff = if(is.na(str_split(input$min_max1, "\\,")[[1]][1])){NA}else{as.numeric(str_split(input$min_max1, "\\,")[[1]][1])},
max.cutoff = if(is.na(str_split(input$min_max1, "\\,")[[1]][2])){NA}else{as.numeric(str_split(input$min_max1, "\\,")[[1]][2])},
image.alpha = as.numeric(str_split(input$pt_alpha1, "\\,")[[1]][4])
) +
ggtitle(ggtitle_text) + NoLegend() + NoAxes()+RestoreLegend(position = "right")
})
# Second SpatialFeaturePlot
output$feature_plot_spat2 <- renderPlot({
feature_gene <- input$feature_gene_spat2
if (!(feature_gene %in% rownames(seurat_data_spat()))) {
feature_gene <- "MS4A1"
ggtitle_text <- paste0(input$feature_gene_spat2, " is not found in the dataset, displaying default MS4A1")
} else {
ggtitle_text <- feature_gene
}
SpatialFeaturePlot(seurat_data_spat(), features = feature_gene, alpha = c(as.numeric(str_split(input$pt_alpha2, "\\,")[[1]][2]), as.numeric(str_split(input$pt_alpha2, "\\,")[[1]][3])),
pt.size = as.numeric(str_split(input$pt_alpha2, "\\,")[[1]][1]),
min.cutoff = if(is.na(str_split(input$min_max2, "\\,")[[1]][1])){NA}else{as.numeric(str_split(input$min_max2, "\\,")[[1]][1])},
max.cutoff = if(is.na(str_split(input$min_max2, "\\,")[[1]][2])){NA}else{as.numeric(str_split(input$min_max2, "\\,")[[1]][2])},
image.alpha = as.numeric(str_split(input$pt_alpha2, "\\,")[[1]][4])
) +
ggtitle(ggtitle_text) + NoLegend() + NoAxes()+RestoreLegend(position = "right")
})
output$meta_plot <- renderPlot({
feature_gene <- input$selected_metadata_spat
ggtitle_text <- feature_gene
SpatialFeaturePlot(seurat_data_spat(), features = feature_gene, alpha = c(as.numeric(str_split(input$pt_alpha3, "\\,")[[1]][2]), as.numeric(str_split(input$pt_alpha3, "\\,")[[1]][3])),
pt.size = as.numeric(str_split(input$pt_alpha3, "\\,")[[1]][1]),
min.cutoff = if(is.na(str_split(input$min_max3, "\\,")[[1]][1])){NA}else{as.numeric(str_split(input$min_max3, "\\,")[[1]][1])},
max.cutoff = if(is.na(str_split(input$min_max3, "\\,")[[1]][2])){NA}else{as.numeric(str_split(input$min_max3, "\\,")[[1]][2])},
image.alpha = as.numeric(str_split(input$pt_alpha3, "\\,")[[1]][4])
) +
ggtitle(ggtitle_text) + NoLegend() + NoAxes()+RestoreLegend(position = "right")
})
})
# Export Feature Plots
observeEvent(input$export_feature_spat, {
req(seurat_data_spat())
current_time <- format(Sys.time(), "%Y%m%d_%H%M")
dataset_name_spat <- gsub("\\.rds$", "", input$selected_dataset_spat)
# Create output directory if missing
if (!dir.exists(file.path(wd, "output"))) {
dir.create(file.path(wd, "output"))
}
prefix_spat <- input$filename_prefix_spat
feature_filename_spat1 <- file.path(wd, "output", paste0(current_time, "_", dataset_name_spat, "_", prefix_spat, "_", input$feature_gene_spat1, "_Feature.pdf"))
feature_filename_spat2 <- file.path(wd, "output", paste0(current_time, "_", dataset_name_spat, "_", prefix_spat, "_", input$feature_gene_spat2, "_Feature.pdf"))
feature_filename_spat3 <- file.path(wd, "output", paste0(current_time, "_", dataset_name_spat, "_", prefix_spat, "_", input$selected_metadata_spat, "_Meta.pdf"))
# Export first FeaturePlot
pdf(feature_filename_spat1, width = width, height = height)
print(
SpatialFeaturePlot(seurat_data_spat(), features = input$feature_gene_spat1,
alpha = c(as.numeric(str_split(input$pt_alpha1, "\\,")[[1]][2]), as.numeric(str_split(input$pt_alpha1, "\\,")[[1]][3])),
pt.size = as.numeric(str_split(input$pt_alpha1, "\\,")[[1]][1]),
min.cutoff = if(is.na(str_split(input$min_max1, "\\,")[[1]][1])){NA}else{as.numeric(str_split(input$min_max1, "\\,")[[1]][1])},
max.cutoff = if(is.na(str_split(input$min_max1, "\\,")[[1]][2])){NA}else{as.numeric(str_split(input$min_max1, "\\,")[[1]][2])},
image.alpha = as.numeric(str_split(input$pt_alpha1, "\\,")[[1]][4]))+
NoLegend() + NoAxes()+RestoreLegend(position = "right")
)
dev.off()
# Export second FeaturePlot
pdf(feature_filename_spat2, width = width, height = height)
print(
SpatialFeaturePlot(seurat_data_spat(), features = input$feature_gene_spat2,
alpha = c(as.numeric(str_split(input$pt_alpha2, "\\,")[[1]][2]), as.numeric(str_split(input$pt_alpha2, "\\,")[[1]][3])),
pt.size = as.numeric(str_split(input$pt_alpha2, "\\,")[[1]][1]),
min.cutoff = if(is.na(str_split(input$min_max2, "\\,")[[1]][1])){NA}else{as.numeric(str_split(input$min_max2, "\\,")[[1]][1])},
max.cutoff = if(is.na(str_split(input$min_max2, "\\,")[[1]][2])){NA}else{as.numeric(str_split(input$min_max2, "\\,")[[1]][2])},
image.alpha = as.numeric(str_split(input$pt_alpha2, "\\,")[[1]][4]))+
NoLegend() + NoAxes()+RestoreLegend(position = "right")
)
dev.off()
# Export Mera
pdf(feature_filename_spat3, width = width, height = height)
print(
SpatialFeaturePlot(seurat_data_spat(), features = input$selected_metadata_spat,
alpha = c(as.numeric(str_split(input$pt_alpha3, "\\,")[[1]][2]),
as.numeric(str_split(input$pt_alpha3, "\\,")[[1]][3])),
pt.size = as.numeric(str_split(input$pt_alpha3, "\\,")[[1]][1]),
min.cutoff = if(is.na(str_split(input$min_max3, "\\,")[[1]][1])){NA}else{as.numeric(str_split(input$min_max3, "\\,")[[1]][1])},
max.cutoff = if(is.na(str_split(input$min_max3, "\\,")[[1]][2])){NA}else{as.numeric(str_split(input$min_max3, "\\,")[[1]][2])},
image.alpha = as.numeric(str_split(input$pt_alpha3, "\\,")[[1]][4])
) + NoLegend() + NoAxes()+RestoreLegend(position = "right")
)
dev.off()
})
}
runApp(shinyApp(ui, server))