R/result.R

Defines functions .new_score_info .new_nearender_result

#' Renderer results in a uniform format
#'
#' Every renderer returns the same list-based result class. Score matrices have
#' functional gene sets in rows and samples or altered gene sets in columns.
#' Every matrix identified by `score_info$score_id` occurs identically in both
#' `scores` and `statistics`. Additional method-specific outputs occur only in
#' `statistics`; statistical matrices are never duplicated at the top level.
#'
#' @format A list with components:
#' \describe{
#'   \item{scores}{Named list of numeric score matrices.}
#'   \item{score_info}{One metadata row per matrix in `scores`.}
#'   \item{statistics}{The named score matrices plus method-specific
#'     statistics.}
#'   \item{inputs}{Names and sizes of rendered inputs.}
#'   \item{diagnostics}{Mapping and numerical diagnostics.}
#'   \item{parameters}{Renderer parameters.}
#' }
#' @name nearender_result
NULL

# Internal constructor and validator for renderer results.
.new_nearender_result <- function(
    scores,
    score_info,
    statistics = list(),
    inputs = list(),
    diagnostics = list(),
    parameters = list()) {
  if (!is.list(scores) || !length(scores) || is.null(names(scores)) ||
      anyNA(names(scores)) || any(names(scores) == "") ||
      anyDuplicated(names(scores))) {
    stop("scores must be a non-empty, uniquely named list.", call. = FALSE)
  }
  valid_score <- vapply(scores, function(score) {
    is.matrix(score) && is.numeric(score) &&
      !is.null(rownames(score)) && !is.null(colnames(score))
  }, logical(1))
  if (!all(valid_score)) {
    stop("Every score must be a numeric matrix with row and column names.",
      call. = FALSE)
  }
  reference_dimnames <- dimnames(scores[[1L]])
  same_dimensions <- vapply(scores, function(score) {
    identical(dim(score), dim(scores[[1L]])) &&
      identical(dimnames(score), reference_dimnames)
  }, logical(1))
  if (!all(same_dimensions)) {
    stop("All score matrices must have identical dimensions and names.",
      call. = FALSE)
  }
  required_info <- c(
    "score_id", "renderer", "metric", "input_variant", "mode",
    "parameters", "citation"
  )
  if (!is.data.frame(score_info) ||
      !all(required_info %in% names(score_info)) ||
      nrow(score_info) != length(scores) ||
      !identical(as.character(score_info$score_id), names(scores))) {
    stop("score_info must describe every score in score-list order.",
      call. = FALSE)
  }
  if (!is.list(statistics) ||
      (length(statistics) &&
        (is.null(names(statistics)) || anyNA(names(statistics)) ||
          any(names(statistics) == "") || anyDuplicated(names(statistics))))) {
    stop("statistics must be a uniquely named list.", call. = FALSE)
  }
  repeated_scores <- intersect(names(scores), names(statistics))
  same_score <- vapply(repeated_scores, function(score_id) {
    identical(scores[[score_id]], statistics[[score_id]])
  }, logical(1))
  if (!all(same_score)) {
    stop("Score entries in statistics must be identical to scores.",
      call. = FALSE)
  }
  statistics <- c(
    scores,
    statistics[setdiff(names(statistics), names(scores))]
  )
  list_fields <- list(
    statistics = statistics,
    inputs = inputs,
    diagnostics = diagnostics,
    parameters = parameters
  )
  if (!all(vapply(list_fields, is.list, logical(1)))) {
    stop("Result statistics and metadata must be lists.", call. = FALSE)
  }
  structure(list(
    scores = scores,
    score_info = score_info,
    statistics = statistics,
    inputs = inputs,
    diagnostics = diagnostics,
    parameters = parameters
  ), class = c("nearender_result", "list"))
}


.new_score_info <- function(
    score_id,
    renderer,
    metric,
    input_variant = "default",
    mode = "standard",
    parameters = "",
    citation = "") {
  data.frame(
    score_id = score_id,
    renderer = renderer,
    metric = metric,
    input_variant = input_variant,
    mode = mode,
    parameters = parameters,
    citation = citation,
    stringsAsFactors = FALSE
  )
}

Try the NEArender package in your browser

Any scripts or data that you put into this service are public.

NEArender documentation built on Nov. 28, 2026, 5:10 p.m.