2021-10-19 13:39:55 +02:00
|
|
|
#' Rank the results by computing a score.
|
|
|
|
|
#'
|
2021-11-05 14:47:33 +01:00
|
|
|
#' This function takes the result of [analyze()] and creates a score by
|
2021-10-19 13:39:55 +02:00
|
|
|
#' computing a weighted mean across the different methods' results.
|
|
|
|
|
#'
|
2021-11-05 14:47:33 +01:00
|
|
|
#' @param analysis Analysis object resulting from [analyze()].
|
|
|
|
|
#' @param weights Named list pairing method names with weighting factors. Only
|
|
|
|
|
#' methods that are contained within this list will be included.
|
2021-10-19 13:39:55 +02:00
|
|
|
#'
|
2021-11-12 10:16:11 +01:00
|
|
|
#' @returns A ranking object. The object extends the analysis result with
|
|
|
|
|
#' additional columns containing the `score` and the `rank` of each gene. It
|
|
|
|
|
#' will be ordered by rank.
|
2021-10-19 13:39:55 +02:00
|
|
|
#'
|
|
|
|
|
#' @export
|
2021-11-17 22:57:31 +01:00
|
|
|
ranking <- function(analysis, weights) {
|
2021-11-05 14:47:33 +01:00
|
|
|
if (!"geposan_analysis" %chin% class(analysis)) {
|
|
|
|
|
stop("Invalid analyis. Use geposan::analyze().")
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 22:57:31 +01:00
|
|
|
ranking <- copy(analysis$results)
|
2021-11-12 10:33:33 +01:00
|
|
|
ranking[, score := 0.0]
|
|
|
|
|
|
2021-10-19 13:39:55 +02:00
|
|
|
for (method in names(weights)) {
|
2021-11-05 14:47:33 +01:00
|
|
|
weighted <- weights[[method]] * ranking[, ..method]
|
|
|
|
|
ranking[, score := score + weighted]
|
2021-10-19 13:39:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Normalize scores to be between 0.0 and 1.0.
|
2021-11-16 16:22:36 +01:00
|
|
|
min_score <- ranking[, min(score)]
|
|
|
|
|
max_score <- ranking[, max(score)]
|
|
|
|
|
score_range <- max_score - min_score
|
|
|
|
|
ranking[, score := (score - min_score) / score_range]
|
2021-11-05 14:47:33 +01:00
|
|
|
|
|
|
|
|
setorder(ranking, -score)
|
|
|
|
|
ranking[, rank := .I]
|
2021-10-19 13:39:55 +02:00
|
|
|
|
2021-11-05 14:47:33 +01:00
|
|
|
structure(
|
|
|
|
|
ranking,
|
|
|
|
|
class = c("geposan_ranking", "geposan_analysis", class(ranking))
|
|
|
|
|
)
|
2021-10-19 13:39:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#' Find the best weights to rank the results.
|
|
|
|
|
#'
|
|
|
|
|
#' This function finds the optimal parameters to [ranking()] that result in the
|
|
|
|
|
#' reference genes ranking particulary high.
|
|
|
|
|
#'
|
2021-11-05 14:47:33 +01:00
|
|
|
#' @param analysis Results from [analyze()] or [ranking()].
|
2021-10-19 13:39:55 +02:00
|
|
|
#' @param methods Methods to include in the score.
|
|
|
|
|
#' @param reference_gene_ids IDs of the reference genes.
|
2021-10-21 11:42:44 +02:00
|
|
|
#' @param target The optimization target. It may be one of "mean", "min" or
|
|
|
|
|
#' "max" and results in the respective rank being optimized.
|
2021-10-19 13:39:55 +02:00
|
|
|
#'
|
2021-11-05 14:47:33 +01:00
|
|
|
#' @returns Named list pairing method names with their optimal weights. This
|
|
|
|
|
#' can be used as an argument to [ranking()].
|
2021-10-19 13:39:55 +02:00
|
|
|
#'
|
|
|
|
|
#' @export
|
2021-11-05 14:47:33 +01:00
|
|
|
optimal_weights <- function(analysis, methods, reference_gene_ids,
|
2021-11-17 22:57:31 +01:00
|
|
|
target = "mean") {
|
2021-11-05 14:47:33 +01:00
|
|
|
if (!"geposan_analysis" %chin% class(analysis)) {
|
|
|
|
|
stop("Invalid analyis. Use geposan::analyze().")
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 13:39:55 +02:00
|
|
|
# Create the named list from the factors vector.
|
|
|
|
|
weights <- function(factors) {
|
|
|
|
|
result <- NULL
|
|
|
|
|
|
|
|
|
|
mapply(function(method, factor) {
|
|
|
|
|
result[[method]] <<- factor
|
|
|
|
|
}, methods, factors)
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 11:42:44 +02:00
|
|
|
# Compute the target rank of the reference genes when applying the weights.
|
|
|
|
|
target_rank <- function(factors) {
|
2021-11-17 22:57:31 +01:00
|
|
|
data <- ranking(analysis, weights(factors))
|
2021-10-21 11:42:44 +02:00
|
|
|
|
2021-11-16 16:22:36 +01:00
|
|
|
result <- data[gene %chin% reference_gene_ids, if (target == "min") {
|
2021-10-21 11:42:44 +02:00
|
|
|
min(rank)
|
|
|
|
|
} else if (target == "max") {
|
|
|
|
|
max(rank)
|
|
|
|
|
} else {
|
|
|
|
|
mean(rank)
|
|
|
|
|
}]
|
2021-11-16 16:22:36 +01:00
|
|
|
|
|
|
|
|
if (result > 0) {
|
|
|
|
|
result
|
|
|
|
|
} else {
|
|
|
|
|
Inf
|
|
|
|
|
}
|
2021-10-19 13:39:55 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-16 16:22:36 +01:00
|
|
|
factors <- stats::optim(
|
|
|
|
|
rep(0.0, length(methods)),
|
|
|
|
|
target_rank
|
|
|
|
|
)$par
|
2021-10-19 13:39:55 +02:00
|
|
|
|
2021-11-16 16:22:36 +01:00
|
|
|
weights(factors / max(abs(factors)))
|
2021-10-19 13:39:55 +02:00
|
|
|
}
|