R/qc-table.R

Defines functions flagged_samples qc_table failure_reason

#' @include qc.R
NULL

#' Metrics assessed against thresholds, in display order
#'
#' @noRd
qc_metrics <- c(
  "BD",
  "FoV",
  "PCL",
  "LoD",
  "Positive_factor",
  "House_factor",
  "Housekeeping_detected",
  "Ligation_order",
  "Ligation_R2",
  "Ligation_NEG",
  "Haemolysis"
)

#' Metrics shared by the eight samples of a PlexSet lane
#'
#' @noRd
lane_metrics <- c("BD", "FoV")

#' Metrics not assessed for PlexSet files
#'
#' @noRd
plexset_unassessed <- c("PCL", "LoD")

#' Describe why a value fails its limits
#'
#' @noRd
failure_reason <- function(metric, value, limits) {
  shown <- function(v) format(signif(v, 3))
  if (length(limits) == 2 && value > max(limits)) {
    paste(metric, shown(value), "above", shown(max(limits)))
  } else {
    paste(metric, shown(value), "below", shown(min(limits)))
  }
}

#' Build the quality-control table
#'
#' @noRd
qc_table <- function(samples, thresholds, rcc_type, id_colname) {
  metrics <- qc_metrics[
    qc_metrics %in% names(samples) & qc_metrics %in% names(thresholds)
  ]
  column <- function(name, missing = NA) {
    if (name %in% names(samples)) {
      samples[[name]]
    } else {
      rep(missing, nrow(samples))
    }
  }
  out <- data.frame(
    column(id_colname),
    lane = column("ID", NA_character_),
    CartridgeID = column("CartridgeID", NA_character_)
  )
  names(out)[1] <- id_colname
  fails <- matrix(
    FALSE,
    nrow(samples),
    length(metrics),
    dimnames = list(NULL, metrics)
  )
  for (metric in metrics) {
    values <- samples[[metric]]
    status <- ifelse(
      metric_fails(values, thresholds[[metric]]),
      "fail",
      ifelse(is.na(values), NA_character_, "pass")
    )
    if (rcc_type == "n8" && metric %in% plexset_unassessed) {
      status <- rep(NA_character_, nrow(samples))
    }
    out[[metric]] <- values
    out[[paste0(metric, "_status")]] <- status
    fails[, metric] <- status %in% "fail"
  }
  for (name in c("MC", "MedC", "Negative_factor", "Background")) {
    out[[name]] <- column(name, NA_real_)
  }
  statuses <- out[paste0(metrics, "_status")]
  assessed <- rowSums(!is.na(statuses)) > 0
  failed <- rowSums(fails) > 0
  inherited <- matrix(
    FALSE,
    nrow(samples),
    length(metrics),
    dimnames = list(NULL, metrics)
  )
  if (rcc_type == "n8") {
    lane_key <- paste(out[["CartridgeID"]], out[["lane"]], sep = "\r")
    shared <- intersect(lane_metrics, metrics)
    for (metric in shared) {
      inherited[, metric] <- stats::ave(fails[, metric], lane_key, FUN = any) &
        !fails[, metric]
    }
    lane_failed <- stats::ave(
      rowSums(fails[, shared, drop = FALSE]) > 0,
      lane_key,
      FUN = any
    )
    lane_assessed <- stats::ave(
      rowSums(!is.na(statuses[paste0(shared, "_status")])) > 0,
      lane_key,
      FUN = any
    )
    out[["lane_status"]] <- ifelse(
      lane_failed,
      "fail",
      ifelse(lane_assessed, "pass", NA_character_)
    )
    failed <- failed | lane_failed
  }
  out[["n_flags"]] <- as.integer(rowSums(fails))
  out[["status"]] <- ifelse(
    failed,
    "fail",
    ifelse(assessed, "pass", NA_character_)
  )
  out[["reason"]] <- vapply(
    seq_len(nrow(samples)),
    function(k) {
      own <- vapply(
        metrics[fails[k, ]],
        function(m) failure_reason(m, samples[[m]][k], thresholds[[m]]),
        character(1)
      )
      lane <- if (any(inherited[k, ])) {
        paste("lane fails", paste(metrics[inherited[k, ]], collapse = ", "))
      }
      reasons <- c(unname(own), lane)
      if (length(reasons) == 0) {
        return(NA_character_)
      }
      paste(reasons, collapse = "; ")
    },
    character(1)
  )
  out
}

#' Samples whose quality-control status is fail
#'
#' @noRd
flagged_samples <- function(x) {
  qc <- qc_table(
    x@samples,
    x@thresholds,
    x@rcc_type,
    x@settings[["id_colname"]]
  )
  qc[["status"]] %in% "fail"
}

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.