-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
416 lines (316 loc) · 12.7 KB
/
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
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
library(shiny)
library(umap)
library(Rtsne)
library(ggplot2)
library(dplyr)
library(stats) # For PCA
library(tcltk)
library(shinyFiles)
library(DT)
# Define the UI
ui <- fluidPage(
titlePanel("Dimensionality Reduction Visualizer"),
sidebarLayout(
sidebarPanel(
#Select Folder
shinyDirButton("folder", "Select Folder", "Choose a folder"),
#Debugging
textOutput("test"),
#List identified features to select
checkboxGroupInput("features", "Select Features to analyze:", choices = NULL),
#Select cell condition
textInput("condition_input_text", "Enter a condition:", ""),
#actionButton for text input
actionButton("add_condition","Add Condition"),
uiOutput("condition_checkboxes"),
#Select well documentation
selectInput("well_input","Select text showing well", choices = NULL),
#Choose plot to determine
selectInput("chart","Type of Dimensionality Reduction", c("PCA","t-SNE","UMAP"), selected = "PCA"),
actionButton("confirm", "Confirm selection")
),
mainPanel(
# Data table of compiled dataframe
DTOutput("data_table"),
# Output plot
uiOutput("dynamicPlot")
)
)
)
# Define the server
server <- function(input, output, session) {
#Initialize values that are reactive and will change based on input (folder, condition_inputs, )
#-------------------------------------------------------------------------------
#Defines drives available to use. May need to be updated.
volumes <- c(Home = fs::path_home(), "C:" = "C:/", "D:" = "D:/", "Z:" = "Z:/")
master_data <- reactiveVal(NULL)
#Data for the dataframe, set to NULL until structured data generated.
structured_data <- reactiveVal(NULL)
#Data for found features, Updated after initial parsing of csv files.
found_features <- reactiveVal(NULL)
#Reactive value to be updated after creation of df
wells <- reactiveVal(NULL)
#Reactive value to store selected conditions
selected_conditions <- reactiveVal(character())
test_text <- reactiveVal(NULL)
shinyDirChoose(input, "folder", roots = volumes, session = session)
#Reaction to selection of folder.
#-------------------------------------------------------------------------------
#Event for when an input$folder is generated with the folder input (shinyDirChoose)
observeEvent(input$folder, {
#Must have a valid input for req(input$folder)
req(input$folder)
#Generate a folder path to work with based on the value for input$folder.
folder_path <- parseDirPath(volumes, input$folder)
#Ensure the folder_path is valid
if(!is.null(folder_path)){
#Create subfolders list of all folders, including main directory.
subfolders <- list.dirs(folder_path)
}
#Create empty character vector for of csv_files.
csv_files <- c()
#Iterate through all directories from subfolders list.
for(subfolder in subfolders){
#Append the csv_files list with the csv_files for the iteration of the subfolder.
csv_files <- c(csv_files,
list.files(subfolder, pattern = "*.csv", full.names = TRUE)
)
}
#Remove overview csv files. (different size than others)
csv_files <- csv_files[!grepl("Overall\\.csv", csv_files)]
#Generate list of nucleus csv_files.
nucleus_files <- csv_files[grepl("Nucleus", csv_files)]
csv_files <- csv_files[!grepl("Nucleus", csv_files)]
#Generate features based on csv files.
features <- gsub(".*(Cell_[^/]+)\\.csv", "\\1", csv_files)
features <- unique(features)
found_features(features)
#Create empty dataframe.
result_df <- data.frame()
#Create working list
temp_list <- list()
#incrementer set to 0
counter <- 0
#Iterate csv files
for(i in seq_along(csv_files)){
#Read CSV
temp_data <- read.csv(csv_files[i], skip = 3)
#Select feature from
feature = features[counter + 1]
#Extract values from first column as feature
temp_list[[feature]] <- temp_data[[1]]
#IDcolumn
id_col <- temp_data[6]
#
orig_name <- temp_data[['Original.Image.Name']]
wells <- orig_name
condition <- orig_name
temp_list[["Well_ID"]] <- wells
temp_list[["Condition"]] <- condition
temp_list[["ID"]] <- id_col
counter <- counter + 1
if(counter >= length(features)){
temp_df <- as.data.frame(temp_list)
result_df <- rbind(result_df, temp_df)
temp_list <- list()
counter <- 0
}
}
structured_data(result_df)
master_data(result_df)
#Use the first entry in the well and condition field to select the data.
#samples <- NA
})
observe({
req(found_features())
updateCheckboxGroupInput(
session,
"features",
choices = found_features(),
selected = found_features())
})
# Reaction to selection from condition input
#-------------------------------------------------------------------------------
# When "Add Condition" button is clicked
observeEvent(input$add_condition, {
req(input$condition_input_text) # Ensure user entered a value
# Get current selected conditions
current_conditions <- selected_conditions()
# Avoid adding duplicates
if (!(input$condition_input_text %in% current_conditions)) {
selected_conditions(c(current_conditions, input$condition_input_text))
}
})
# Render dynamic checkboxes for selected conditions
output$condition_checkboxes <- renderUI({
req(selected_conditions()) # Ensure there are selected conditions
checkboxGroupInput("condition_input", "Select conditions to use:", choices = selected_conditions(), selected = selected_conditions())
})
# Render dynamic checkboxes for selected conditions
observeEvent(input$confirm, {
req(master_data(), input$condition_input) # Ensure data and selection exist
df <- master_data() # Retrieve current dataframe
# Ensure "Condition" column exists
if (!"Condition" %in% colnames(df)) {
warning("Column 'Condition' not found in the dataframe!")
return()
}
# Extract all condition strings
conds <- df$Condition
# Ensure there are valid conditions to process
if (length(conds) == 0 || all(is.na(conds))) {
warning("No valid conditions found in the dataframe!")
return()
}
# Initialize new condition column
extracted_conditions <- character(length(conds))
for (i in seq_along(conds)) {
if (is.na(conds[i])) next # Skip NA values
condition_parts <- unlist(strsplit(conds[i], "_")) # Split by "_"
# Look for the selected condition(s) in the split text
match_index <- which(condition_parts %in% input$condition_input)
if (length(match_index) > 0) {
for (index in match_index) {
# Find neighboring text
prefix <- if (index > 1) condition_parts[index - 1] else ""
suffix <- if (index < length(condition_parts)) condition_parts[index + 1] else ""
# Construct regex pattern dynamically
if (prefix != "" && suffix != "") {
pattern <- paste0(".*_", prefix, "_(.*?)_", suffix, "_.*")
} else if (prefix == "") {
pattern <- paste0("^(.*?)_", suffix, "_.*")
} else if (suffix == "") {
pattern <- paste0(".*_", prefix, "_(.*?)$")
} else {
pattern <- paste0("^(.*?)$")
}
# Extract using regex
extracted_value <- gsub(pattern, "\\1", conds[i])
# Save extracted value
extracted_conditions[i] <- extracted_value
}
}
}
# Update Condition column
df$Condition <- extracted_conditions
df <- df[df$Condition != "" & !is.na(df$Condition), ]
structured_data(df) # Update the dataframe
})
# Reaction to selection of well input
#-------------------------------------------------------------------------------
#Alter the well_id column of the dataframe to identify the cells individually.
observe({
req(structured_data()) # Ensure the dataframe exists
well <- structured_data()$Well_ID # Extract condition column
if (length(well) == 0 || all(is.na(well))) {
return() # Exit if there are no valid Well_ID entries
}
# Extract first non-NA entry safely
first_well <- na.omit(well)[1]
if (!is.na(first_well) && nzchar(first_well)) {
# Split by "_"
well_list <- unique(unlist(strsplit(first_well, "_")))
# Update the reactive conditions list
wells(well_list)
}
})
observe({
req(wells())
updateSelectInput(session, "well_input", choices = wells())
})
observe({
req(structured_data()) # Ensure the dataframe exists
# Extract "Well_ID" column
well_col <- structured_data()$Well_ID
# Ensure there are valid well IDs to process
if (length(well_col) == 0 || all(is.na(well_col))) {
return()
}
# Extract all well identifiers using regex pattern (Letter followed by a number)
extracted_wells <- unique(unlist(regmatches(well_col, gregexpr("[A-Za-z]\\d+|\\d+[A-Za-z]", well_col))))
# If wells were found, update the reactive value
if (length(extracted_wells) > 0) {
wells(sort(extracted_wells)) # Sort alphabetically (optional)
}
})
# Update the select input for wells
observe({
req(wells())
updateSelectInput(session, "well_input", choices = wells())
})
# Extract Well ID based on user input
observeEvent(input$confirm, {
req(structured_data(), input$well_input) # Ensure data and selection exist
df <- structured_data() # Retrieve current dataframe
if (!"Well_ID" %in% names(df)) return() # Ensure Well_ID column exists
# Apply regex substitution to extract only the well ID (letter + number)
df$Well_ID <- gsub(".*?([A-Za-z]\\d+).*", "\\1", df$Well_ID)
# Update the structured dataframe
structured_data(df)
})
#Creation of the PCA, UMAP, and t-SNE graphs.
#-------------------------------------------------------------------------------
# Reactive trigger for generating the plot only when "Confirm" is clicked
observeEvent(input$confirm, {
#Requires data_frame
req(structured_data())
df <- structured_data()
# Remove ID column if present
if ("ID" %in% colnames(df)) {
df <- df[, !(colnames(df) %in% "ID")]
}
# Keep only numeric columns
numeric_cols <- sapply(df, is.numeric)
df_numeric <- df[, numeric_cols, drop = FALSE]
# Remove constant columns
df_numeric <- df_numeric[, apply(df_numeric, 2, function(x) length(unique(x)) > 1), drop = FALSE]
# Ensure valid data
if (ncol(df_numeric) < 2) {
output$dynamicPlot <- renderUI({
tags$p("Not enough numeric data for dimensionality reduction")
})
return()
}
# Perform dimensionality reduction
result_df <- NULL
if (input$chart == "PCA") {
pca_result <- prcomp(df_numeric, center = TRUE, scale. = TRUE)
result_df <- as.data.frame(pca_result$x[, 1:2])
} else if (input$chart == "t-SNE") {
df_numeric <- scale(df_numeric)
tsne_result <- Rtsne(df_numeric, dims = 2, perplexity = 30, verbose = FALSE, max_iter = 500)
result_df <- as.data.frame(tsne_result$Y)
} else if (input$chart == "UMAP") {
df_numeric <- scale(df_numeric)
umap_result <- umap(df_numeric)
result_df <- as.data.frame(umap_result$layout)
}
colnames(result_df) <- c("Dim1", "Dim2")
result_df$Condition <- if ("Condition" %in% colnames(df)) df$Condition else "Unknown"
# Dynamically render the plot output UI
output$dynamicPlot <- renderUI({
plotOutput("dimPlot", height = "100%", width = "100%")
})
# Render the plot
output$dimPlot <- renderPlot({
ggplot(result_df, aes(x = Dim1, y = Dim2, color = Condition)) +
geom_point(size = 2, alpha = 0.7) +
theme_minimal() +
labs(title = paste(input$chart, "Projection"), x = "Dimension 1", y = "Dimension 2") +
theme(legend.position = "right")
}, height = function() session$clientData$output_dimPlot_width * 0.75)
})
#Outputs defined
#-------------------------------------------------------------------------------
#
output$data_table <- renderDT({
req(structured_data())
datatable(structured_data(),options = list(pageLength= 10, autowidth = TRUE))
})
output$test <- renderText({
req(test_text())
paste("Value from test:", test_text())
})
}
# Run the application
shinyApp(ui = ui, server = server)