diff --git a/R/constants.R b/R/constants.R index 3733e07..2fdfb47 100644 --- a/R/constants.R +++ b/R/constants.R @@ -36,7 +36,6 @@ NAMESPACE_STATMODEL = list( visualization_fold_change_checkbox = "visualization_fold_change_checkbox", visualization_fold_change_input = "visualization_fold_change_input", visualization_which_comparison = "visualization_which_comparison", - visualization_volcano_display_protein_name = "visualization_volcano_display_protein_name", visualization_volcano_significance_cutoff = "visualization_volcano_significance_cutoff", visualization_heatmap_number_proteins = "visualization_heatmap_number_proteins", visualization_heatmap_cluster_option = "visualization_heatmap_cluster_option", diff --git a/R/module-statmodel-server.R b/R/module-statmodel-server.R index 5b06cee..bac7a12 100644 --- a/R/module-statmodel-server.R +++ b/R/module-statmodel-server.R @@ -130,6 +130,21 @@ statmodelServer = function(id, parent_session, loadpage_input, qc_input, output[[NAMESPACE_STATMODEL$modeling_section_header]] <- renderUI({ get_modeling_section_header(input[[NAMESPACE_STATMODEL$comparison_mode]], app_template()) }) + + # Auto-generate unique default name for non-pairwise comparisons + observe({ + req(input[[NAMESPACE_STATMODEL$comparison_mode]] == + CONSTANTS_STATMODEL$comparison_mode_custom_nonpairwise) + existing_names <- if (!is.null(contrast$matrix)) rownames(contrast$matrix) else character(0) + # Find the next available "custom comparison N" + n <- 1 + while (paste0("custom comparison ", n) %in% existing_names) { + n <- n + 1 + } + updateTextInput(session, + NAMESPACE_STATMODEL$comparisons_custom_nonpairwise_name, + value = paste0("custom comparison ", n)) + }) # Reset on configuration change observeEvent(c(input[[NAMESPACE_STATMODEL$comparison_mode]], loadpage_input()$proceed1), { diff --git a/R/statmodel-server-visualization.R b/R/statmodel-server-visualization.R index 618efb3..a2aefbc 100644 --- a/R/statmodel-server-visualization.R +++ b/R/statmodel-server-visualization.R @@ -6,9 +6,36 @@ render_group_comparison_plot_inputs = function(output, session, rownames, get_da ns = session$ns output[[NAMESPACE_STATMODEL$visualization_which_comparison]] = renderUI({ + comparison_names <- rownames() + req(length(comparison_names) > 0) selectInput(ns(NAMESPACE_STATMODEL$visualization_which_comparison), label = h5("Select comparison to plot"), - c("all", rownames()), selected = "all") + comparison_names, selected = comparison_names[1]) + }) + + # Dynamically filter plot type choices based on number of comparisons. + # Skips response curve mode (handled separately in module-statmodel-server.R). + observe({ + req(input[[NAMESPACE_STATMODEL$comparison_mode]]) + mode <- input[[NAMESPACE_STATMODEL$comparison_mode]] + if (mode == CONSTANTS_STATMODEL$comparison_mode_response_curve) return() + + comparison_names <- rownames() + n_comparisons <- length(comparison_names) + + choices <- c( + "Volcano Plot" = CONSTANTS_STATMODEL$plot_type_volcano_plot, + "Comparison Plot" = CONSTANTS_STATMODEL$plot_type_comparison_plot + ) + if (n_comparisons >= 2) { + choices <- c( + "Volcano Plot" = CONSTANTS_STATMODEL$plot_type_volcano_plot, + "Heatmap" = CONSTANTS_STATMODEL$plot_type_heatmap, + "Comparison Plot" = CONSTANTS_STATMODEL$plot_type_comparison_plot + ) + } + updateSelectInput(session, NAMESPACE_STATMODEL$visualization_plot_type, + choices = choices) }) output[[NAMESPACE_STATMODEL$visualization_which_protein]] = renderUI({ @@ -21,9 +48,7 @@ render_group_comparison_plot_inputs = function(output, session, rownames, get_da plot_type = input[[NAMESPACE_STATMODEL$visualization_plot_type]] if (plot_type == CONSTANTS_STATMODEL$plot_type_volcano_plot) { - show_protein_name = !is.null(loadpage_input()$DDA_DIA) && - loadpage_input()$DDA_DIA != "TMT" - create_volcano_plot_options(ns, show_protein_name) + create_volcano_plot_options(ns) } else if (plot_type == CONSTANTS_STATMODEL$plot_type_comparison_plot) { create_comparison_plot_options(ns) } else if (plot_type == CONSTANTS_STATMODEL$plot_type_heatmap) { @@ -78,10 +103,6 @@ create_group_comparison_plot = function(input, loadpage_input, data_comparison) fold_change_cutoff = ifelse(!is.null(input[[NAMESPACE_STATMODEL$visualization_fold_change_input]]), input[[NAMESPACE_STATMODEL$visualization_fold_change_input]], FALSE) tryCatch({ - if (input[[NAMESPACE_STATMODEL$visualization_plot_type]] == CONSTANTS_STATMODEL$plot_type_volcano_plot && input[[NAMESPACE_STATMODEL$visualization_which_comparison]] == "all") { - remove_modal_spinner() - stop('** Cannot generate multiple plots in a screen. Please refine selection or save to a pdf. **') - } if (loadpage_input$BIO == "PTM") { result = groupComparisonPlotsPTM( data_comparison, @@ -90,7 +111,9 @@ create_group_comparison_plot = function(input, loadpage_input, data_comparison) FCcutoff = fold_change_cutoff, logBase.pvalue = as.integer(input[[NAMESPACE_STATMODEL$visualization_logp_base]]), ProteinName = TRUE, - which.Comparison = input[[NAMESPACE_STATMODEL$visualization_which_comparison]], + which.Comparison = if (input[[NAMESPACE_STATMODEL$visualization_plot_type]] == + CONSTANTS_STATMODEL$plot_type_heatmap) "all" + else input[[NAMESPACE_STATMODEL$visualization_which_comparison]], address = FALSE, isPlotly = TRUE ) @@ -108,7 +131,9 @@ create_group_comparison_plot = function(input, loadpage_input, data_comparison) ProteinName = TRUE, numProtein = input[[NAMESPACE_STATMODEL$visualization_heatmap_number_proteins]], clustering = input[[NAMESPACE_STATMODEL$visualization_heatmap_cluster_option]], - which.Comparison = input[[NAMESPACE_STATMODEL$visualization_which_comparison]], + which.Comparison = if (input[[NAMESPACE_STATMODEL$visualization_plot_type]] == + CONSTANTS_STATMODEL$plot_type_heatmap) "all" + else input[[NAMESPACE_STATMODEL$visualization_which_comparison]], which.Protein = input[[NAMESPACE_STATMODEL$visualization_which_protein]], height = input[[NAMESPACE_STATMODEL$visualization_plot_height_slider]], address = "Ex_", @@ -121,10 +146,12 @@ create_group_comparison_plot = function(input, loadpage_input, data_comparison) sig = input[[NAMESPACE_STATMODEL$visualization_volcano_significance_cutoff]], FCcutoff = fold_change_cutoff, logBase.pvalue = as.numeric(input[[NAMESPACE_STATMODEL$visualization_logp_base]]), - ProteinName = input[[NAMESPACE_STATMODEL$visualization_volcano_display_protein_name]], + ProteinName = TRUE, numProtein = input[[NAMESPACE_STATMODEL$visualization_heatmap_number_proteins]], clustering = input[[NAMESPACE_STATMODEL$visualization_heatmap_cluster_option]], - which.Comparison = input[[NAMESPACE_STATMODEL$visualization_which_comparison]], + which.Comparison = if (input[[NAMESPACE_STATMODEL$visualization_plot_type]] == + CONSTANTS_STATMODEL$plot_type_heatmap) "all" + else input[[NAMESPACE_STATMODEL$visualization_which_comparison]], which.Protein = input[[NAMESPACE_STATMODEL$visualization_which_protein]], height = input[[NAMESPACE_STATMODEL$visualization_plot_height_slider]], address = "Ex_", @@ -272,8 +299,10 @@ create_download_plot_handler <- function(output, input, contrast, preprocess_dat sig = input[[NAMESPACE_STATMODEL$visualization_volcano_significance_cutoff]], FCcutoff = fold_change_cutoff, logBase.pvalue = as.integer(input[[NAMESPACE_STATMODEL$visualization_logp_base]]), - ProteinName = input[[NAMESPACE_STATMODEL$visualization_volcano_display_protein_name]], - which.Comparison = input[[NAMESPACE_STATMODEL$visualization_which_comparison]], + ProteinName = TRUE, + which.Comparison = if (plot_type == + CONSTANTS_STATMODEL$plot_type_heatmap) "all" + else input[[NAMESPACE_STATMODEL$visualization_which_comparison]], address = address_prefix ) } else { @@ -283,10 +312,12 @@ create_download_plot_handler <- function(output, input, contrast, preprocess_dat sig = input[[NAMESPACE_STATMODEL$visualization_volcano_significance_cutoff]], FCcutoff = fold_change_cutoff, logBase.pvalue = as.numeric(input[[NAMESPACE_STATMODEL$visualization_logp_base]]), - ProteinName = input[[NAMESPACE_STATMODEL$visualization_volcano_display_protein_name]], + ProteinName = TRUE, numProtein = input[[NAMESPACE_STATMODEL$visualization_heatmap_number_proteins]], clustering = input[[NAMESPACE_STATMODEL$visualization_heatmap_cluster_option]], - which.Comparison = input[[NAMESPACE_STATMODEL$visualization_which_comparison]], + which.Comparison = if (plot_type == + CONSTANTS_STATMODEL$plot_type_heatmap) "all" + else input[[NAMESPACE_STATMODEL$visualization_which_comparison]], which.Protein = input[[NAMESPACE_STATMODEL$visualization_which_protein]], height = input[[NAMESPACE_STATMODEL$visualization_plot_height_slider]], address = address_prefix, diff --git a/R/statmodel-ui-options-visualization.R b/R/statmodel-ui-options-visualization.R index 24cb684..3d8cdc4 100644 --- a/R/statmodel-ui-options-visualization.R +++ b/R/statmodel-ui-options-visualization.R @@ -32,12 +32,9 @@ create_plot_type_selector <- function(ns) { } #' Create volcano plot specific options #' @noRd -create_volcano_plot_options <- function(ns, show_protein_name = TRUE) { +create_volcano_plot_options <- function(ns) { tagList( uiOutput(ns(NAMESPACE_STATMODEL$visualization_which_comparison)), - if (show_protein_name) { - checkboxInput(ns(NAMESPACE_STATMODEL$visualization_volcano_display_protein_name), label = p("display protein name")) - }, selectInput( ns(NAMESPACE_STATMODEL$visualization_logp_base), label = h5("Log transformation of adjusted p-value"), @@ -71,7 +68,7 @@ create_heatmap_options <- function(ns) { tagList( h4("Note: Only one page will be shown in browser. To view all proteins please view this plot as a pdf. Heatmaps require at least two comparisons."), selectInput( - ns(NAMESPACE_STATMODEL$visualization_logp_base), + ns(NAMESPACE_STATMODEL$visualization_logp_base), label = h5("Log transformation of adjusted p-value"), c("base 2" = "2", "base 10" = "10"), selected = "10" diff --git a/tests/testthat/test-statmodel-ui-options-visualization.R b/tests/testthat/test-statmodel-ui-options-visualization.R index 3133bdc..b5b3955 100644 --- a/tests/testthat/test-statmodel-ui-options-visualization.R +++ b/tests/testthat/test-statmodel-ui-options-visualization.R @@ -39,13 +39,10 @@ test_that("Correct elements are present in create_volcano_plot_options", { ui_html <- htmltools::renderTags(result)$html expect_true(grepl(NAMESPACE_STATMODEL$visualization_which_comparison, ui_html), info = "Which comparison namespace should be present") - expect_true(grepl(NAMESPACE_STATMODEL$visualization_volcano_display_protein_name, ui_html), - info = "Display protein name checkbox namespace should be present") expect_true(grepl(NAMESPACE_STATMODEL$visualization_logp_base, ui_html), info = "Log p-value base namespace should be present") expect_true(grepl(NAMESPACE_STATMODEL$visualization_volcano_significance_cutoff, ui_html), info = "Significance cutoff namespace should be present") - }) test_that("All possible options in create_plot_type_selector", { diff --git a/tests/testthat/test-utils-statmodel-server.R b/tests/testthat/test-utils-statmodel-server.R index f069bc4..aa42ee1 100644 --- a/tests/testthat/test-utils-statmodel-server.R +++ b/tests/testthat/test-utils-statmodel-server.R @@ -288,6 +288,8 @@ test_that("extract_significant_proteins filters PTM data correctly", { result <- extract_significant_proteins(data_comp, loadpage_input, 0.05) expect_equal(nrow(result$ADJUSTED.Model), 1) + expect_equal(nrow(result$PTM.Model), 2) + expect_equal(nrow(result$PROTEIN.Model), 1) }) test_that("extract_significant_proteins filters TMT data correctly", {