geposanui/server.R

224 lines
6.4 KiB
R
Raw Normal View History

2021-06-24 22:38:16 +02:00
library(data.table)
library(DT)
2021-10-07 12:18:47 +02:00
library(gprofiler2)
library(plotly)
2021-10-07 12:59:04 +02:00
library(rclipboard)
2021-06-24 22:38:16 +02:00
library(shiny)
2021-09-18 23:10:52 +02:00
source("init.R")
source("optimize.R")
2021-10-15 13:16:51 +02:00
source("rank_plot.R")
2021-06-24 22:38:16 +02:00
source("scatter_plot.R")
2021-10-01 09:50:04 +02:00
#' Java script function to replace gene IDs with Ensembl gene links.
js_link <- JS("function(row, data) {
let id = data[1];
var name = data[2];
if (!name) name = 'Unknown';
let url = `https://www.ensembl.org/Homo_sapiens/Gene/Summary?g=${id}`;
$('td:eq(1)', row).html(`<a href=\"${url}\" target=\"_blank\">${name}</a>`);
}")
server <- function(input, output, session) {
#' Show the customized slider for setting the required number of species.
output$n_species_slider <- renderUI({
sliderInput(
"n_species",
"Required number of species per gene",
min = 0,
max = if (input$species == "all") {
nrow(species)
} else {
length(species_ids_replicative)
},
step = 1,
value = 10
)
})
observeEvent(input$optimize_button, {
results <- isolate(results())
method_ids <- NULL
for (method in methods) {
if (isolate(input[[method$id]])) {
method_ids <- c(method_ids, method$id)
}
}
reference_gene_ids <- genes[suggested | verified == TRUE, id]
weights <- optimize_weights(results, method_ids, reference_gene_ids)
mapply(function(method_id, weight) {
updateSliderInput(
session,
sprintf("%s_weight", method_id),
value = weight * 100
)
}, method_ids, weights)
})
# Observe each method's enable button.
lapply(methods, function(method) {
observeEvent(input[[method$id]], {
shinyjs::toggleState(sprintf("%s_weight", method$id))
}, ignoreInit = TRUE)
})
2021-10-15 15:03:40 +02:00
#' Rank the results based on the specified weights. Filter out genes with
#' too few species but don't apply the cut-off score.
2021-08-29 13:25:12 +02:00
results <- reactive({
2021-09-18 23:33:37 +02:00
# Select the species preset.
2021-08-29 13:25:12 +02:00
results <- if (input$species == "all") {
results_all
} else {
results_replicative
}
2021-09-18 23:33:37 +02:00
# Compute scoring factors and the weighted score.
2021-10-15 09:26:57 +02:00
total_weight <- 0.0
results[, score := 0.0]
for (method in methods) {
if (input[[method$id]]) {
weight <- input[[sprintf("%s_weight", method$id)]]
total_weight <- total_weight + weight
column <- method$id
weighted <- weight * results[, ..column]
results[, score := score + weighted]
}
2021-10-15 09:26:57 +02:00
}
2021-09-30 13:25:39 +02:00
2021-10-15 09:26:57 +02:00
results[, score := score / total_weight]
2021-09-30 13:25:39 +02:00
2021-10-11 11:14:08 +02:00
# Exclude genes with too few species.
results <- results[n_species >= input$n_species]
2021-09-30 13:25:39 +02:00
2021-10-11 11:14:08 +02:00
# Penalize missing species.
if (input$penalize) {
species_count <- if (input$species == "all") {
nrow(species)
} else {
length(species_ids_replicative)
}
results <- results[, score := score * n_species / species_count]
}
2021-10-15 09:26:57 +02:00
# Order the results based on their score.
2021-09-18 23:33:37 +02:00
2021-09-30 12:54:40 +02:00
setorder(results, -score, na.last = TRUE)
2021-10-15 09:26:57 +02:00
results[, rank := .I]
2021-08-26 11:20:50 +02:00
})
2021-08-25 15:01:18 +02:00
2021-10-15 15:03:40 +02:00
#' Apply the cut-off score to the ranked results.
results_filtered <- reactive({
results()[score >= input$cutoff / 100]
2021-10-15 13:16:51 +02:00
})
2021-06-24 22:38:16 +02:00
output$genes <- renderDT({
2021-10-15 09:26:57 +02:00
method_ids <- sapply(methods, function(method) method$id)
method_names <- sapply(methods, function(method) method$name)
2021-10-15 11:46:15 +02:00
columns <- c("rank", "gene", "name", "chromosome", method_ids, "score")
column_names <- c("", "Gene", "", "Chromosome", method_names, "Score")
2021-10-15 09:26:57 +02:00
2021-09-30 13:25:39 +02:00
dt <- datatable(
2021-10-15 15:03:40 +02:00
results_filtered()[, ..columns],
2021-08-26 12:51:43 +02:00
rownames = FALSE,
2021-10-15 09:26:57 +02:00
colnames = column_names,
2021-10-01 09:50:04 +02:00
style = "bootstrap",
2021-10-15 13:59:00 +02:00
fillContainer = TRUE,
2021-10-15 11:46:15 +02:00
extensions = "Scroller",
2021-10-01 09:50:04 +02:00
options = list(
rowCallback = js_link,
2021-10-15 11:46:15 +02:00
columnDefs = list(list(visible = FALSE, targets = 2)),
deferRender = TRUE,
scrollY = 200,
scroller = TRUE
2021-10-01 09:50:04 +02:00
)
2021-06-24 22:38:16 +02:00
)
2021-09-30 13:25:39 +02:00
2021-10-15 09:26:57 +02:00
formatPercentage(dt, c(method_ids, "score"), digits = 1)
2021-06-24 22:38:16 +02:00
})
2021-10-07 12:59:04 +02:00
output$copy <- renderUI({
2021-10-15 15:03:40 +02:00
results <- results_filtered()
2021-10-07 12:59:04 +02:00
gene_ids <- results[, gene]
names <- results[name != "", name]
genes_text <- paste(gene_ids, collapse = "\n")
names_text <- paste(names, collapse = "\n")
splitLayout(
2021-10-15 13:59:00 +02:00
cellWidths = "auto",
2021-10-07 12:59:04 +02:00
rclipButton(
"copy_ids_button",
"Copy gene IDs",
genes_text,
2021-10-15 13:59:00 +02:00
icon = icon("clipboard")
2021-10-07 12:59:04 +02:00
),
rclipButton(
"copy_names_button",
"Copy gene names",
names_text,
2021-10-15 13:59:00 +02:00
icon = icon("clipboard")
2021-10-07 12:59:04 +02:00
)
)
})
2021-10-15 12:24:28 +02:00
output$scatter <- renderPlotly({
2021-10-15 15:03:40 +02:00
results <- results_filtered()
2021-08-29 13:25:12 +02:00
gene_ids <- results[input$genes_rows_selected, gene]
2021-09-16 00:06:54 +02:00
genes <- genes[id %chin% gene_ids]
2021-08-29 13:25:12 +02:00
species <- if (input$species == "all") {
2021-09-16 00:06:54 +02:00
species
2021-08-29 13:25:12 +02:00
} else {
2021-09-16 00:06:54 +02:00
species[replicative == TRUE]
2021-08-29 13:25:12 +02:00
}
2021-09-16 00:06:54 +02:00
scatter_plot(results, species, genes, distances)
2021-06-24 22:38:16 +02:00
})
2021-10-07 12:18:47 +02:00
2021-10-15 15:03:40 +02:00
output$assessment_synopsis <- renderText({
reference_gene_ids <- genes[suggested | verified == TRUE, id]
reference_count <- results_filtered()[
gene %chin% reference_gene_ids,
.N
]
reference_results <- results()[gene %chin% reference_gene_ids]
sprintf(
"Included reference genes: %i/%i<br> \
Mean rank of reference genes: %.1f<br> \
Maximum rank of reference genes: %i",
reference_count,
length(reference_gene_ids),
reference_results[, mean(rank)],
reference_results[, max(rank)]
)
})
output$rank_plot <- renderPlotly({
rank_plot(
results(),
genes[suggested | verified == TRUE, id],
input$cutoff / 100
)
})
2021-10-07 12:18:47 +02:00
output$gost <- renderPlotly({
2021-10-07 12:42:36 +02:00
if (input$enable_gost) {
2021-10-15 15:03:40 +02:00
result <- gost(results_filtered()[, gene], ordered_query = TRUE)
2021-10-07 12:42:36 +02:00
gostplot(result, capped = FALSE, interactive = TRUE)
} else {
NULL
}
2021-10-07 12:18:47 +02:00
})
2021-06-24 22:38:16 +02:00
}