geposan/R/method_clustering.R

117 lines
3.3 KiB
R
Raw Normal View History

#' Perform a cluster analysis.
#'
#' This function will cluster the data using [stats::hclust()] and
#' [stats::cutree()]. Every cluster with at least two members qualifies for
#' further analysis. Clusters are then ranked based on their size in relation
#' to the total number of values. The return value is a final score between
#' 0.0 and 1.0. Lower ranking clusters contribute less to this score.
#'
#' @param data The values that should be scored.
#' @param span The maximum span of values considered to be in one cluster.
#' @param weight The weight that will be given to the next largest cluster in
#' relation to the previous one. For example, if `weight` is 0.7 (the
#' default), the first cluster will weigh 1.0, the second 0.7, the third 0.49
#' etc.
#' @param n_clusters Maximum number of clusters that should be taken into
#' account. By default, all clusters will be regarded.
2022-06-22 11:05:51 +02:00
#' @param relation Number of items that the cluster size should be based on.
#' This should always at least the length of the data. By default, the length
#' of the data is used.
#'
#' @return A score between 0.0 and 1.0 summarizing how much the data clusters.
#'
#' @export
2022-06-22 11:05:51 +02:00
clusteriness <- function(data,
span = 100000,
weight = 0.7,
n_clusters = NULL,
relation = NULL) {
2022-05-26 12:42:19 +02:00
n <- length(data)
2022-05-26 12:42:19 +02:00
# Return a score of 0.0 if there is just one or no value at all.
if (n < 2) {
return(0.0)
}
2022-06-22 11:05:51 +02:00
if (is.null(relation)) {
relation <- n
}
2022-05-26 12:42:19 +02:00
# Cluster the data and compute the cluster sizes.
2022-05-26 12:42:19 +02:00
tree <- stats::hclust(stats::dist(data))
clusters <- stats::cutree(tree, h = span)
cluster_sizes <- sort(tabulate(clusters), decreasing = TRUE)
2022-05-26 12:42:19 +02:00
# Compute the "clusteriness" score.
2022-05-26 12:42:19 +02:00
score <- 0.0
2022-05-26 12:42:19 +02:00
for (i in seq_along(cluster_sizes)) {
if (!is.null(n_clusters)) {
if (i > n_clusters) {
break
}
}
2022-05-26 12:42:19 +02:00
cluster_size <- cluster_sizes[i]
2022-05-26 12:42:19 +02:00
if (cluster_size >= 2) {
2022-06-22 11:05:51 +02:00
cluster_score <- cluster_size / relation
2022-05-26 12:42:19 +02:00
score <- score + weight^(i - 1) * cluster_score
}
2022-05-26 12:42:19 +02:00
}
2022-05-26 12:42:19 +02:00
score
}
#' Process genes clustering their distance to telomeres.
#'
#' The result will be cached and can be reused for different presets, because
#' it is independent of the reference genes in use.
#'
#' @return An object of class `geposan_method`.
#'
#' @seealso [clusteriness()]
#'
#' @export
clustering <- function() {
2022-05-26 12:42:19 +02:00
method(
id = "clustering",
name = "Clustering",
description = "Clustering of genes",
function(preset, progress) {
species_ids <- preset$species_ids
gene_ids <- preset$gene_ids
2022-05-26 12:42:19 +02:00
cached("clustering", c(species_ids, gene_ids), {
scores <- data.table(gene = gene_ids)
2022-05-26 12:42:19 +02:00
# Prefilter the input data by species.
distances <- geposan::distances[species %chin% species_ids]
2022-05-26 12:42:19 +02:00
genes_done <- 0
genes_total <- length(gene_ids)
2022-05-26 12:42:19 +02:00
# Perform the cluster analysis for one gene.
compute <- function(gene_id) {
data <- distances[gene == gene_id, distance]
score <- clusteriness(data)
2022-05-26 12:42:19 +02:00
genes_done <<- genes_done + 1
progress(genes_done / genes_total)
2022-05-26 12:42:19 +02:00
score
}
2022-05-26 12:42:19 +02:00
scores[, score := compute(gene), by = gene]
2022-05-26 12:42:19 +02:00
result(
method = "clustering",
scores = scores
)
})
}
)
}