-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
370 lines (315 loc) · 12.4 KB
/
Copy pathserver.R
File metadata and controls
370 lines (315 loc) · 12.4 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
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
selected_var <- "Entropy"
selected_Method <- "Both"
data_tool <- "individual"
selected_patient <- "MS"
selected_event <- "T12"
selected_perc = "001"
VarsStat <- c(
"sex", "clinical_presentation", "gc_treatment", "subtentorial_lesions",
"spinal_cord", "gadolinium_contrast", "lesion_burden",
"WORSENING", "EDSS_DIAGNOSI", "EDSS_PROGRESSIONE","age"
)
names(VarsStat) <- c(
"Sex", "Clinical Presentation", "Treatment with Glucocorticoids", "Subtentorial Lesions",
"Spinal Cord", "Gadolinium Contrast", "Lesion Burden",
"Worsening", "EDSS Diagnosis", "EDSS Progression","Age"
)
source("Functions.R")
server <- function(input, output, session) {
dataReact <- reactiveValues(plotLIST = NULL, dataClustered = NULL)
# Data Explorer: Load and display bacterial p-value tables
output$bacteriaTable <- renderDT({
# req(input$data_perc)
# req(data_tool)
# req(input$data_discriminant)
# Construct file path based on selected percentage
file_path <- paste0("www/tables/001_bacteria_maaslin.tsv")
# Read the table
data <- read.table(file_path, header = TRUE, sep = "\t", stringsAsFactors = FALSE)
cols_to_keep <- "species"
pattern <- paste0("P.val_", data_tool, "_", input$data_discriminant)
cols_to_keep <- c(cols_to_keep, grep(pattern, colnames(data), value = TRUE))
data_filtered <- data[, cols_to_keep, drop = FALSE]
# Create more readable column names
col_names <- colnames(data_filtered)
col_names <- gsub("P.val_", "", col_names)
col_names <- gsub("_gadolinium_contrast", " (Gadolinium)", col_names)
col_names <- gsub("_lesion_burden", " (Lesion Burden)", col_names)
col_names <- gsub("_spinal_cord", " (Spinal Cord)", col_names)
col_names <- gsub("_subtentorial", " (Subtentorial)", col_names)
col_names <- gsub("maaslin", "MaAsLin3", col_names, ignore.case = TRUE)
col_names <- gsub("limma", "limma", col_names, ignore.case = TRUE)
col_names <- gsub("lefse", "LEfSe", col_names, ignore.case = TRUE)
col_names <- gsub("aldex2", "ALDEx2", col_names, ignore.case = TRUE)
col_names <- gsub("ancombc2", "ANCOM-BC2", col_names, ignore.case = TRUE)
col_names <- gsub("species", "Species", col_names)
colnames(data_filtered) <- col_names
# Create caption with filter info
caption_text <- paste0("Bacterial P-values Table (", input$data_perc, "% abundance threshold")
caption_text <- paste0(caption_text, ")")
# Create a more user-friendly data table with filtering options
dt <- datatable(data_filtered,
options = list(
pageLength = 25,
scrollX = TRUE,
searchHighlight = TRUE,
dom = "Bfrtip",
columnDefs = list(
list(className = "dt-left", targets = 0),
list(className = "dt-center", targets = 1:(ncol(data_filtered) - 1))
)
),
filter = "top",
rownames = FALSE,
caption = htmltools::tags$caption(
style = "caption-side: top; text-align: left; color: black; font-size: 120%;",
caption_text
)
)
# Apply formatting only to numeric columns (skip first column which is species)
if (ncol(data_filtered) > 1) {
dt <- dt %>%
formatRound(columns = 2:ncol(data_filtered), digits = 4) %>%
formatStyle(
columns = 2:ncol(data_filtered),
backgroundColor = styleInterval(c(0.01, 0.05), c("#ffe6e6", "#fff9e6", "white"))
)
}
dt
})
# Placeholder for other analysis table - you can replace this with your actual data
output$otherTable <- renderDT({
# This is a placeholder - replace with your actual table file when ready
# For example: data <- read.table("www/tables/other_analysis.tsv", header = TRUE, sep = "\t")
placeholder_data <- data.frame(
Message = "Other analysis table will be loaded here. Please add your table file to www/tables/ folder."
)
datatable(placeholder_data,
options = list(
pageLength = 10,
dom = "t"
),
rownames = FALSE
)
})
#observeEvent(input$button_start, {
# req(input$selected_var)
# req(selected_perc)
# req(selected_patient)
# reduce = input$reduce
observe({
reduce <- TRUE
isolate({
Data <- DataComplete %>%
as.data.frame() %>%
filter(
Alpha %in% selected_var,
Method == selected_Method,
Subset == selected_perc
) %>%
mutate(MetricType = paste0(Discriminant, "_", Alpha)) %>%
select(-Subset, -Alpha, -Discriminant, -Method) %>%
tidyr::spread(key = MetricType, value = Value)
if (selected_patient != "All") {
Data[grepl(pattern = selected_patient, x = Data$Patient), ] -> Data
}
rownames(Data) <- Data$Patient
Data <- Data %>% select(-Patient)
Data <- Data[, colSums(abs(Data), na.rm = T) > 0]
Data <- Data %>% na.omit()
# Remove columns with zero variance
Data <- Data[, sapply(Data, function(x) var(x, na.rm = TRUE) != 0)]
dataReact$plotLIST <- plotLIST <- cluster.generation(data = Data, Kmeans.before = reduce)
output$FeatureComp <- renderDT(
{
round(plotLIST$pca_res$rotation, digits = 3)
},
rownames = T,
options = list(dom = "t", scrollX = TRUE)
)
# Display results
output$pcaPlotvar <- renderPlot({
plotLIST$plPCAvar
})
output$pcaPlot <- renderPlot({
plotLIST$plPCA
})
output$inputData <- renderDT({
datatable(plotLIST$Data,
options = list(pageLength = 20, scrollX = TRUE),
rownames = T,
filter = "top"
)
})
kbest <- as.numeric(names(plotLIST$AllClusteringIndex$bestK[1]))
output$clustChoicePlot <- renderPlot({
plotLIST$silhouette +
geom_vline(aes(
xintercept = kbest,
color = paste0(
"Best k considering ",
plotLIST$AllClusteringIndex$bestK[1], " indexes over ",
sum((plotLIST$AllClusteringIndex$bestK))
)
), linetype = "dashed") +
labs(col = "") + theme(legend.position = "top")
})
bestk <- as.data.frame(dataReact$plotLIST$AllClusteringIndex$bestK)
colnames(bestk) <- c("Number of\n clusters", "Number of indexes\n in accordance")
output$ClusterIndexesTable <- renderTable(bestk)
updateSliderInput(session, "clusterSlider", value = kbest, min = 2, max = 6, step = 1)
# updateSelectInput(session = session, "StatVar",
# choices = c("", colnames(Metadata %>% select(-id,-Event,-EventTime))))
})
})
output$downloadInputData <- downloadHandler(
filename = function() {
"MSexplorer_data.csv"
},
content = function(file) {
write.csv(dataReact$plotLIST$Data, file, row.names = T)
}
)
observeEvent(input$clusterStart, {
shinybusy::show_modal_spinner()
isolate({
req(input$clusterSlider != 0)
req(dataReact$plotLIST)
Nclust <- input$clusterSlider
paletteCluster <- RColorBrewer::brewer.pal(Nclust, "Set1")[1:Nclust]
names(paletteCluster) <- paste0(1:Nclust)
plotLIST <- cluster.plot(
data = dataReact$plotLIST$Data,
Kmeans.before = T, # input$reduce,
k = Nclust,
palette = paletteCluster
)
output$PCA3d <- renderPlotly({
PCA3dplot(
pca_res = dataReact$plotLIST$pca_res,
pca_data_df = plotLIST$pca_data_df,
palette = paletteCluster
)
})
output$clustPlot <- renderPlot({
plotLIST$plCL2
})
output$clusterMappingTable <- renderDT({
req(dataReact$dataClustered)
datatable(dataReact$dataClustered,
options = list(pageLength = 20, scrollX = TRUE),
rownames = FALSE,
filter = "top"
)
})
output$downloadClusterTable <- downloadHandler(
filename = function() {
paste0("cluster_mapping_", Sys.Date(), ".csv")
},
content = function(file) {
write.csv(dataReact$dataClustered, file, row.names = FALSE)
}
)
dataperc <- plotLIST$dataClustered %>%
mutate(Status = ifelse(grepl("MS", id), "MS", "HD")) %>%
group_by(Cluster, Status) %>%
summarise(count = n()) %>%
group_by(Cluster) %>%
mutate(perc = count / sum(count))
pl <- dataperc %>% ggplot(aes(x = as.factor(Cluster), y = perc * 100, fill = factor(Status))) +
geom_bar(stat = "identity", width = 0.7) +
labs(x = "Cluster", y = "%", fill = "Status") +
theme_minimal(base_size = 14) +
geom_text(aes(label = paste0(round(perc * 100, 1), "%")),
position = position_stack(vjust = 0.5),
col = "white", size = 4, fontface = "bold"
)
output$percPlot <- renderPlot({
pl
})
dataReact$dataClustered <- plotLIST$dataClustered
})
shinybusy::remove_modal_spinner()
})
output$SurvivalPlot <- renderPlot({
dataClustered <- req(dataReact$dataClustered)
vardata <- merge(dataClustered, Metadata) %>% select(-id)
if (selected_event == "T24") {
vardata <- vardata %>%
select(-Event, -EventTime) %>%
rename(Event = Event_T24, EventTime = EventTime_T24)
}
vardata <- vardata %>%
filter(!is.na(Event)) %>%
mutate(Cluster = paste(Cluster))
list2env(list(vardata = vardata), envir = .GlobalEnv)
fit <- survfit(Surv(vardata$EventTime, vardata$Event) ~ Cluster, data = vardata)
list2env(list(fit = fit), envir = .GlobalEnv)
Nclust <- as.numeric(max(dataClustered$Cluster))
paletteCluster <- RColorBrewer::brewer.pal(Nclust, "Set1")[1:Nclust]
names(paletteCluster) <- paste0("Cluster=", 1:Nclust)
ggsurv <- survminer::ggsurvplot(
fit = fit, data = vardata,
xlab = "Weeks", ylab = "Worsening", palette = paletteCluster,
size = 2, pval = TRUE, risk.table = TRUE, conf.int = F,
risk.table.col = "strata", ggtheme = theme_bw(),
surv.median.line = "hv"
)
print(fit)
ggsurv
})
output$ChordDiagram <- renderPlot({
dataClustered <- req(dataReact$dataClustered)
vardata <- merge(dataClustered, Metadata) %>%
select(-id)
Nclust <- as.numeric(max(dataClustered$Cluster))
# paletteCluster = viridisLite::turbo(Nclust)
paletteCluster <- RColorBrewer::brewer.pal(Nclust, "Set1")[1:Nclust]
names(paletteCluster) <- paste0(1:Nclust)
# saveRDS( merge(
# Metadata %>% select(id, EDSS_DIAGNOSI, EDSS_PROGRESSIONE),
# dataClustered %>% select(id, Cluster)
# ),"DataCLandEDSS.Rds")
pl <- chordDiagram(Metadata, dataClustered)
pl & scale_fill_manual(values = paletteCluster) & scale_color_manual(values = paletteCluster)
})
observe({
dataClustered <- req(dataReact$dataClustered)
StatVar <- input$StatVar
vardata <- merge(dataClustered, Metadata) %>% select(-id, -EventTime, -Event)
vars <- colnames(vardata %>% select(-Cluster))
if (StatVar == "") {
table <- statistical_tests(vardata)
table = table[na.omit( match(table$Variable, VarsStat)) ,]
table$Variable <- names(VarsStat)[match(table$Variable, VarsStat)]
output$statTable <- renderTable(table)
} else {
res <- test_indipendence(
info_tibble = vardata,
variable_1 = "Cluster", variable_2 = input$StatVar,
palette = viridisLite::cividis(length(unique(vardata[[input$StatVar]])))
)
output$statMosaic <- renderPlot({
res$plot & theme(
legend.position = "bottom",
axis.title.x = element_text(
face = "bold.italic",
size = rel(1.2)
),
axis.text = element_text(
face = "italic",
size = rel(1)
)
)
})
}
})
output$butt_download <- downloadHandler(
filename = function() {
paste0("Cluster_", selected_var, ifelse(input$reduce, "_PCAreduced", ""), ".Rds")
},
content = function(file) {
saveRDS(dataReact$dataClustered, file)
}
)
}