ubigen/R/server.R

97 lines
2.3 KiB
R
Raw Normal View History

2022-05-07 17:54:23 +02:00
#' Server implementing the main user interface.
#' @noRd
server <- function(input, output) {
2022-05-30 19:32:19 +02:00
ranked_data <- reactive({
2022-05-30 20:22:42 +02:00
total_weight <- abs(input$cross_sample_weight) +
2022-05-30 19:32:19 +02:00
abs(input$mean_expression) +
abs(input$sd_expression)
2022-05-30 19:32:19 +02:00
data <- data.table::copy(ubigen::genes)
2022-05-30 19:32:19 +02:00
data[, score :=
2022-05-30 20:22:42 +02:00
(input$cross_sample_weight * get(input$cross_sample_metric) +
2022-05-30 19:32:19 +02:00
input$mean_expression * mean_expression_normalized +
input$sd_expression * sd_expression_normalized) /
total_weight]
2022-05-30 19:32:19 +02:00
data.table::setorder(data, -score)
data[, rank := .I]
data[, percentile := 1 - rank / max(rank)]
2022-05-30 19:32:19 +02:00
data
})
2022-05-30 20:08:54 +02:00
output$overview_plot <- plotly::renderPlotly(overview_plot(ranked_data()))
2022-05-30 19:32:19 +02:00
output$scores_plot <- plotly::renderPlotly(scores_plot(ranked_data()))
2022-05-30 19:32:19 +02:00
output$selected_genes <- DT::renderDataTable({
selected_points <- plotly::event_data("plotly_selected")
2022-05-30 19:32:19 +02:00
data <- if (is.null(selected_points)) {
ranked_data()
} else {
ranked_data()[rank %in% selected_points$x]
}
2022-05-30 19:32:19 +02:00
genes_table(data)
})
}
2022-05-07 17:54:23 +02:00
#' Create a displayable data table from the gene results data.
#' @noRd
genes_table <- function(data) {
2022-05-30 19:32:19 +02:00
data <- data[, .(
"Gene" = glue::glue_data(
data,
"<a href=\"https://www.ensembl.org/Homo_sapiens/Gene/Summary",
"?db=core;g={gene}\" target=\"_blank\">{hgnc_name}</a>"
),
"Rank" = rank,
"Percentile" = percentile,
"Score" = score,
"Median" = median_expression,
"Mean" = mean_expression,
"Standard deviation" = sd_expression,
"Expressed" = above_zero,
"Above median" = above_median,
"Above 95%" = above_95
)]
2022-05-30 19:32:19 +02:00
DT::datatable(
data,
options = list(
buttons = list(
list(
extend = "copy",
text = "Copy to clipboard"
2022-05-12 13:24:27 +02:00
),
2022-05-30 19:32:19 +02:00
list(
extend = "csv",
text = "Download CSV"
)
),
dom = "fBrtip",
pageLength = 100
),
rownames = FALSE,
escape = FALSE,
selection = "none",
extensions = "Buttons"
) |>
DT::formatPercentage(
c(
"Percentile",
"Score",
"Expressed",
"Above median",
"Above 95%"
),
digits = 2,
) |>
2022-05-30 19:32:19 +02:00
DT::formatRound(c(
"Median",
"Mean",
"Standard deviation"
))
2022-05-07 17:54:23 +02:00
}