Handle caching

This commit is contained in:
Elias Projahn 2021-10-21 17:25:44 +02:00
parent b8365e0efb
commit df6e23d219
7 changed files with 247 additions and 191 deletions

View file

@ -3,23 +3,28 @@
# A score will be given to each gene such that 0.0 corresponds to the maximal
# mean distance across all genes and 1.0 corresponds to a distance of 0.
proximity <- function(preset, progress = NULL) {
# Prefilter distances by species and gene.
distances <- geposan::distances[
species %chin% preset$species_ids & gene %chin% preset$gene_ids
]
species_ids <- preset$species_ids
gene_ids <- preset$gene_ids
# Compute the score as described above.
cached("proximity", c(species_ids, gene_ids), {
# Prefilter distances by species and gene.
distances <- geposan::distances[
species %chin% preset$species_ids & gene %chin% preset$gene_ids
]
distances <- distances[, .(mean_distance = mean(distance)), by = "gene"]
max_distance <- distances[, max(mean_distance)]
distances[, score := 1 - mean_distance / max_distance]
# Compute the score as described above.
if (!is.null(progress)) {
# We do everything in one go, so it's not possible to report detailed
# progress information. As the method is relatively quick, this should
# not be a problem.
progress(1.0)
}
distances <- distances[, .(mean_distance = mean(distance)), by = "gene"]
max_distance <- distances[, max(mean_distance)]
distances[, score := 1 - mean_distance / max_distance]
distances[, .(gene, score)]
if (!is.null(progress)) {
# We do everything in one go, so it's not possible to report
# detailed progress information. As the method is relatively quick,
# this should not be a problem.
progress(1.0)
}
distances[, .(gene, score)]
})
}