geposanui/server.R

197 lines
5.5 KiB
R
Raw Normal View History

2021-06-24 22:38:16 +02:00
library(data.table)
library(DT)
2021-10-19 14:15:28 +02:00
library(geposan)
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-10-19 16:44:29 +02:00
source("methods.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-19 14:15:28 +02:00
source("utils.R")
2021-06-24 22:38:16 +02:00
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
)
})
2021-10-19 16:44:29 +02:00
#' Compute the results according to the preset.
analysis <- reactive({
2021-10-19 14:15:28 +02:00
# Select the preset.
preset <- if (input$species == "all") {
preset_all_species
2021-08-29 13:25:12 +02:00
} else {
2021-10-19 14:15:28 +02:00
preset_replicative_species
2021-08-29 13:25:12 +02:00
}
2021-10-19 14:15:28 +02:00
# Perform the analysis cached based on the preset's hash.
2021-10-19 15:05:01 +02:00
results <- withProgress(
message = "Analyzing genes",
value = 0.0, {
run_cached(
rlang::hash(preset),
geposan::analyze,
preset,
function(progress) {
setProgress(progress)
}
)
}
)
2021-10-19 14:15:28 +02:00
# Add all gene information to the results.
results <- merge(
results,
genes,
by.x = "gene",
by.y = "id"
)
2021-10-20 11:20:20 +02:00
# Count included species from the preset per gene.
genes_n_species <- distances[
species %chin% preset$species_ids,
.(n_species = .N),
by = "gene"
]
setkey(genes_n_species, gene)
2021-10-19 14:15:28 +02:00
# Exclude genes with too few species.
2021-10-20 11:20:20 +02:00
results[genes_n_species[gene, n_species] >= input$n_species]
2021-08-26 11:20:50 +02:00
})
2021-08-25 15:01:18 +02:00
2021-10-19 16:44:29 +02:00
# Rank the results.
results <- methods_server("methods", analysis)
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-10-19 14:15:28 +02:00
}