geposanui/R/methods.R

167 lines
4.7 KiB
R
Raw Permalink Normal View History

2021-10-20 15:34:52 +02:00
# Construct UI for the methods editor.
2022-08-18 09:21:48 +02:00
methods_ui <- function(id, options) {
2022-05-26 12:44:09 +02:00
verticalLayout(
2022-08-18 12:21:00 +02:00
h5("Methods"),
2024-02-18 14:29:23 +01:00
popover(
title = "Optimization target",
help = paste0(
"These genes will be used as the target for optimization. This means ",
"that the method weights will be automatically adjusted to maximize ",
"the scores of this gene set. Select \"Reference genes\", if you want ",
"to compare your genes of interest with the reference gene set."
),
div(class = "label", "Genes to optimize for")
),
2022-05-26 12:44:09 +02:00
selectInput(
NS(id, "optimization_genes"),
2024-02-18 14:29:23 +01:00
label = NULL,
2022-05-26 12:44:09 +02:00
choices = list(
"Reference genes" = "reference",
"Comparison genes" = "comparison"
)
),
2024-02-18 14:29:23 +01:00
popover(
title = "Optimization target",
help = paste0(
"This determines how the genes, that were selected for optimization, ",
"are combined. For example, \"Mean rank\" optimizes the method ",
"weights based on the highest possible mean score across all selected ",
"genes and \"First rank\" would always focus on the best performing ",
"gene."
),
div(class = "label", "Optimization target")
),
2022-05-26 12:44:09 +02:00
selectInput(
NS(id, "optimization_target"),
2024-02-18 14:29:23 +01:00
label = NULL,
2022-05-26 12:44:09 +02:00
choices = list(
"Mean rank" = "mean",
"Median rank" = "median",
"First rank" = "min",
"Last rank" = "max",
"Customize weights" = "custom"
)
),
2022-08-18 09:21:48 +02:00
lapply(options$methods, function(method) {
2022-05-26 12:44:09 +02:00
verticalLayout(
2024-02-18 14:29:23 +01:00
popover(
title = method$name,
help = method$help,
2024-01-31 12:20:07 +01:00
checkboxInput(
NS(id, method$id),
span(
method$description,
class = "control-label"
),
value = TRUE
)
),
2022-05-26 12:44:09 +02:00
sliderInput(
NS(id, sprintf("%s_weight", method$id)),
NULL,
min = -1.0,
max = 1.0,
step = 0.01,
value = 1.0
)
)
})
)
2021-10-19 16:44:29 +02:00
}
2022-08-18 09:21:48 +02:00
#' Construct server for the methods editor.
#'
#' @param options Global options for the application.
#' @param analysis The reactive containing the results to be weighted.
#' @param comparison_gene_ids The comparison gene IDs.
#'
#' @return A reactive containing the weighted results.
#' @noRd
methods_server <- function(id, options, analysis, comparison_gene_ids) {
2022-05-26 12:44:09 +02:00
moduleServer(id, function(input, output, session) {
# Observe each method's enable button and synchronise the slider state.
2022-08-18 09:21:48 +02:00
lapply(options$methods, function(method) {
2022-05-26 12:44:09 +02:00
observeEvent(input[[method$id]], {
shinyjs::toggleState(
sprintf("%s_weight", method$id),
condition = input[[method$id]]
)
})
2022-05-26 12:44:09 +02:00
shinyjs::onclick(sprintf("%s_weight", method$id), {
updateSelectInput(
session,
"optimization_target",
selected = "custom"
)
})
})
2021-10-19 16:44:29 +02:00
2022-05-26 12:44:09 +02:00
# This reactive will always contain the currently selected optimization
# gene IDs in a normalized form.
optimization_gene_ids <- reactive({
gene_ids <- if (input$optimization_genes == "comparison") {
comparison_gene_ids()
} else {
analysis()$preset$reference_gene_ids
}
2022-05-26 12:44:09 +02:00
sort(unique(gene_ids))
})
2022-05-26 12:44:09 +02:00
# This reactive will always contain the optimal weights according to
# the selected parameters.
optimal_weights <- reactive({
withProgress(message = "Optimizing weights", {
setProgress(0.2)
2022-05-26 12:44:09 +02:00
included_methods <- NULL
2022-08-18 09:21:48 +02:00
for (method in options$methods) {
2022-05-26 12:44:09 +02:00
if (input[[method$id]]) {
included_methods <- c(included_methods, method$id)
}
}
2021-12-16 13:52:14 +01:00
2022-05-26 12:44:09 +02:00
geposan::optimal_weights(
analysis(),
included_methods,
optimization_gene_ids(),
target = input$optimization_target
)
2022-05-26 12:44:09 +02:00
})
}) |> bindCache(
analysis(),
optimization_gene_ids(),
2022-08-18 09:21:48 +02:00
sapply(options$methods, function(method) input[[method$id]]),
2022-05-26 12:44:09 +02:00
input$optimization_target
)
2022-05-26 12:44:09 +02:00
reactive({
weights <- NULL
2021-12-16 13:52:14 +01:00
2022-05-26 12:44:09 +02:00
if (length(optimization_gene_ids()) < 1 |
input$optimization_target == "custom") {
2022-08-18 09:21:48 +02:00
for (method in options$methods) {
2022-05-26 12:44:09 +02:00
if (input[[method$id]]) {
weight <- input[[sprintf("%s_weight", method$id)]]
weights[[method$id]] <- weight
}
}
} else {
weights <- optimal_weights()
2021-12-16 13:52:14 +01:00
2022-05-26 12:44:09 +02:00
for (method_id in names(weights)) {
updateSliderInput(
session,
sprintf("%s_weight", method_id),
value = weights[[method_id]]
)
}
}
2021-10-19 16:44:29 +02:00
2022-05-26 12:44:09 +02:00
geposan::ranking(analysis(), weights)
2021-10-19 16:44:29 +02:00
})
2022-05-26 12:44:09 +02:00
})
2021-10-19 16:44:29 +02:00
}