Skip to contents

first we must ensure that all vignette dependencies are installed and loaded:

library(found)

if (!(requireNamespace("pak", quietly = TRUE))) {
  install.packages("pak")
}
deps <- list(
  "Seurat" = "Seurat",
  "SeuratData" = "satijalab/seurat-data",
  "irlba" = "irlba"
)
mapply(function(name, pkg) {
  if (!(requireNamespace(name, quietly = TRUE))) {
    pak::pak(pkg)
  }
}, names(deps), deps) |> invisible()

suppressPackageStartupMessages({
  library(irlba)
  library(SeuratObject)
  library(Seurat)
  library(SeuratData)
})

for this example vignette, we will use the ifnb dataset provided by the SeuratData package, which we can load as follows:

if (!("ifnb" %in% InstalledData()[["Dataset"]])) {
  InstallData("ifnb")
}
# ignore the various messages generated by LoadData
so <- LoadData("ifnb") |>
  suppressWarnings() |>
  suppressMessages()

we can then utilize existing found pipelines to run label refinement:

out <- HiDDEN(
  so, "stim", "CTRL",
  adapters$Pipeline(methods$run_pca, methods$reg_logit, methods$bin_kmeans),
  # ⚠️: X must be observation by feature format, so we transpose the output of LayerData!
  X = t(LayerData(so, "counts")), k = 15L
)

however, using the to_step wrapper, we can also inject (almost) any R function into various steps of the pipeline! for example, below we switch to using the pearson residual based transform implemented by sctransform as a VST and irlba for dimensionality reduction. given that our dimensionality reduction function is now written in R, we can also change the pipeline dependencies to take the entire seurat object as input, instead of having to extract the expression matrix.

so <- SCTransform(so, verbose = FALSE) |> suppressWarnings()
sct_pca <- function(so, k) {
  prcomp_irlba(
    t(LayerData(so, assay = "SCT", layer = "scale.data")),
    n = k
  )[["x"]]
}
out <- HiDDEN(
  so, "stim", "CTRL",
  adapters$Pipeline(to_step(sct_pca), methods$reg_logit, methods$bin_kmeans),
  so = so, k = 15L
)

we can also try utilizing the automatic hyperparameter tuning implemented in found:

outs <- HiDDENt(
  so, "stim", "CTRL",
  tune$FixPointTuner(3, 8, 0.02),
  adapters$Pipeline(to_step(sct_pca), methods$reg_logit, methods$bin_kmeans),
  so = so
)
# we can see what the automatically selected hyperparameter `k` is:
outs[["chosen"]]