geposanui/server.R

170 lines
4.7 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")
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>`);
}")
2021-06-24 22:38:16 +02:00
server <- function(input, output) {
#' 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
)
})
2021-09-18 23:33:37 +02:00
#' This reactive expression applies all user defined filters as well as the
#' desired ranking weights to the results.
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) {
weight <- input[[method$id]]
total_weight <- total_weight + weight
column <- method$id
weighted <- weight * results[, ..column]
results[, score := score + weighted]
}
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]
}
# Apply the cut-off score.
results <- results[score >= input$cutoff / 100]
2021-09-18 23:33:37 +02:00
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-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)
columns <- c("rank", "gene", "name", method_ids, "score")
column_names <- c("", "Gene", "", method_names, "Score")
2021-09-30 13:25:39 +02:00
dt <- datatable(
2021-10-15 09:26:57 +02:00
results()[, ..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",
options = list(
rowCallback = js_link,
columnDefs = list(list(visible = FALSE, targets = 2))
)
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-08-29 15:29:34 +02:00
output$synposis <- renderText({
results <- results()
sprintf(
"Found %i candidates including %i/%i verified and %i/%i suggested \
TPE-OLD genes.",
results[, .N],
results[verified == TRUE, .N],
2021-09-16 00:06:54 +02:00
genes[verified == TRUE, .N],
2021-08-29 15:29:34 +02:00
results[suggested == TRUE, .N],
2021-09-16 00:06:54 +02:00
genes[suggested == TRUE, .N]
2021-08-29 15:29:34 +02:00
)
})
2021-10-07 12:59:04 +02:00
output$copy <- renderUI({
results <- results()
gene_ids <- results[, gene]
names <- results[name != "", name]
genes_text <- paste(gene_ids, collapse = "\n")
names_text <- paste(names, collapse = "\n")
splitLayout(
rclipButton(
"copy_ids_button",
"Copy gene IDs",
genes_text,
icon = icon("clipboard"),
width = "100%"
),
rclipButton(
"copy_names_button",
"Copy gene names",
names_text,
icon = icon("clipboard"),
width = "100%"
)
)
})
2021-06-24 22:38:16 +02:00
output$scatter <- renderPlot({
2021-08-29 13:25:12 +02:00
results <- results()
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
output$gost <- renderPlotly({
2021-10-07 12:42:36 +02:00
if (input$enable_gost) {
result <- gost(results()[, gene], ordered_query = TRUE)
gostplot(result, capped = FALSE, interactive = TRUE)
} else {
NULL
}
2021-10-07 12:18:47 +02:00
})
2021-06-24 22:38:16 +02:00
}