R/nacho-class.R

Defines functions validate_nacho duplicate_problem

#' @include qc.R thresholds.R
NULL

#' Schema version of the fields stored in `@provenance`
#'
#' @noRd
nacho_schema_version <- 2L

#' Describe duplicated values in a validity problem
#'
#' Up to three values are named, and an ellipsis shows that more follow.
#'
#' @noRd
duplicate_problem <- function(label, values) {
  duplicates <- duplicated_values(values)
  sprintf(
    "%s must be unique; duplicated: %s%s.",
    label,
    paste(utils::head(duplicates, 3), collapse = ", "),
    if (length(duplicates) > 3L) ", ..." else ""
  )
}

validate_nacho <- function(self) {
  counts <- self@counts
  if (!is.matrix(counts)) {
    return("@counts must be an integer matrix of probes by samples.")
  }
  problems <- character(0)
  if (
    !is.matrix(self@normalised) ||
      !identical(dim(self@normalised), dim(counts)) ||
      !identical(dimnames(self@normalised), dimnames(counts))
  ) {
    problems <- c(
      problems,
      "@normalised must be a matrix with the same dimensions and names as @counts."
    )
  }
  id <- self@settings[["id_colname"]]
  if (!rlang::is_string(id) || !id %in% names(self@samples)) {
    return(c(problems, "@settings$id_colname must name a column of @samples."))
  }
  ids <- as.character(self@samples[[id]])
  if (!identical(names(self@samples)[1], id)) {
    problems <- c(problems, "The id column must come first in @samples.")
  }
  if (anyDuplicated(ids) > 0) {
    problems <- c(problems, duplicate_problem("Sample ids", ids))
  }
  if (!identical(ids, colnames(counts))) {
    problems <- c(
      problems,
      "The sample ids in @samples must match colnames(@counts), in the same order."
    )
  }
  scores <- self@pca[["scores"]]
  if (!is.null(scores) && !identical(rownames(scores), ids)) {
    problems <- c(
      problems,
      "The row names of @pca$scores must match the sample ids, in the same order."
    )
  }
  probe_columns <- c(
    "CodeClass",
    "Name",
    "Accession",
    "is_housekeeping",
    "is_excluded"
  )
  if (!all(probe_columns %in% names(self@probes))) {
    problems <- c(
      problems,
      sprintf(
        "@probes must have the columns %s.",
        paste(probe_columns, collapse = ", ")
      )
    )
  } else if (
    !identical(as.character(self@probes[["Name"]]), rownames(counts))
  ) {
    problems <- c(
      problems,
      "The probe names in @probes must match rownames(@counts), in the same order."
    )
  } else if (anyDuplicated(self@probes[["Name"]]) > 0) {
    problems <- c(
      problems,
      duplicate_problem("Probe names", as.character(self@probes[["Name"]]))
    )
  }
  if (!rlang::is_string(self@rcc_type) || !self@rcc_type %in% c("n1", "n8")) {
    problems <- c(problems, "@rcc_type must be \"n1\" or \"n8\".")
  }
  problems <- c(problems, validate_thresholds(self@thresholds))
  if (!rlang::is_scalar_integer(self@provenance[["schema_version"]])) {
    problems <- c(problems, "@provenance$schema_version must be one integer.")
  }
  if (length(problems) == 0) NULL else problems
}

#' The nacho class
#'
#' NACHO stores RCC data, quality-control metrics and normalised counts in an
#' S7 object. Reach its content through [nacho_counts()], [nacho_samples()],
#' [nacho_probes()] and [nacho_qc()], never through `@`.
#'
#' @noRd
nacho <- S7::new_class(
  name = "nacho",
  package = "NACHO",
  properties = list(
    counts = S7::class_integer,
    normalised = S7::class_double,
    probes = S7::class_data.frame,
    samples = S7::class_data.frame,
    settings = S7::class_list,
    thresholds = S7::class_list,
    pca = S7::class_list,
    rcc_type = S7::class_character,
    provenance = S7::class_list
  ),
  validator = validate_nacho
)

Try the NACHO package in your browser

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

NACHO documentation built on Nov. 2, 2026, 5:09 p.m.