geposan/R/analyze.R

91 lines
2.4 KiB
R
Raw Permalink Normal View History

#' Analyze genes based on position data.
2021-10-19 13:39:55 +02:00
#'
2021-11-03 14:17:39 +01:00
#' @param preset The preset to use which should be created using [preset()].
2021-10-19 15:03:10 +02:00
#' @param progress A function to be called for progress information. The
#' function should accept a number between 0.0 and 1.0 for the current
#' progress. If no function is provided, a simple text progress bar will be
#' shown.
#' @param include_results Whether to include the detailed results. If this is
#' set to `FALSE`, only the `scores` are available.
2021-10-19 13:39:55 +02:00
#'
2021-11-12 10:16:11 +01:00
#' @returns An object containing the results of the analysis with the following
#' items:
#' \describe{
#' \item{`preset`}{The preset that was used.}
#' \item{`scores`}{Table containing all scores for each gene.}
#' \item{`results`}{Results from the different methods including details.}
2021-11-12 10:16:11 +01:00
#' }
2021-10-19 13:39:55 +02:00
#'
#' @export
analyze <- function(preset, progress = NULL, include_results = TRUE) {
2022-05-26 12:42:19 +02:00
if (!inherits(preset, "geposan_preset")) {
stop("Preset is invalid. Use geposan::preset() to create one.")
}
2021-11-03 14:17:39 +01:00
2022-05-26 12:42:19 +02:00
if (is.null(progress)) {
progress_bar <- progress::progress_bar$new()
progress_bar$update(0.0)
2021-10-19 13:39:55 +02:00
2022-05-26 12:42:19 +02:00
progress <- function(progress_value) {
if (!progress_bar$finished) {
progress_bar$update(progress_value)
if (progress_value >= 1.0) {
progress_bar$terminate()
}
2022-05-26 12:42:19 +02:00
}
}
2022-05-26 12:42:19 +02:00
}
2021-10-19 15:03:10 +02:00
2022-05-26 12:42:19 +02:00
progress_buffer <- 0.0
method_count <- length(preset$methods)
2022-05-26 12:42:19 +02:00
method_progress <- function(progress_value) {
progress(progress_buffer + progress_value / method_count)
}
2021-10-19 13:39:55 +02:00
2022-05-26 12:42:19 +02:00
scores <- data.table(gene = preset$gene_id)
results <- list()
2021-10-19 15:03:10 +02:00
2022-05-26 12:42:19 +02:00
for (method in preset$methods) {
method_results <- method$func(preset, method_progress)
2022-05-26 12:42:19 +02:00
scores <- merge(scores, method_results$scores, by = "gene")
setnames(scores, "score", method$id)
2021-10-19 15:03:10 +02:00
2022-05-26 12:42:19 +02:00
results <- c(results, list(method_results))
2021-11-19 15:07:15 +01:00
2022-05-26 12:42:19 +02:00
progress_buffer <- progress_buffer + 1 / method_count
progress(progress_buffer)
}
2021-11-19 15:07:15 +01:00
2022-05-26 12:42:19 +02:00
structure(
list(
preset = preset,
scores = scores,
results = if (include_results) results else NULL
),
class = "geposan_analysis"
)
}
2021-11-19 15:07:15 +01:00
#' Print an analysis object.
#'
#' @param x The analysis to print.
#' @param ... Other parameters.
#'
#' @seealso [analyze()]
#'
#' @export
print.geposan_analysis <- function(x, ...) {
2022-05-26 12:42:19 +02:00
cat("geposan analysis:\n\n")
print(x$preset)
cat("\n")
2021-11-05 14:33:39 +01:00
2022-05-26 12:42:19 +02:00
for (result in x$results) {
print(result)
cat("\n")
}
2021-11-17 22:57:31 +01:00
2022-05-26 12:42:19 +02:00
invisible(x)
2021-10-19 13:39:55 +02:00
}