Skip to contents

For a TileMatrix or other count assay in a MarchRDataset or MarchRProject, simulate synthetic doublets and score each observed cell by its proximity to synthetic doublets. These scores can then be used by filter_doublets().

Usage

add_doublet_scores(
  x,
  n_doublets = 1000,
  n_trials = NULL,
  sample_ratio = 0.5,
  n_dimensions = 30L,
  n_iterations = 2,
  lsi_method = 1,
  binarize = TRUE,
  corr_cutoff = 0.75,
  scale_dims = FALSE,
  umap_params = list(),
  lsi_params = list(),
  scoring_method = c("UMAP", "LSI"),
  knn_method = knn_hnsw(k = 10L, metric = "euclidean", ef = 200L),
  force = FALSE,
  seed = 1,
  batch = TRUE,
  threads = NULL,
  verbose = getOption("MarchR.verbose", FALSE),
  ...
)

# S3 method for class 'MarchRDataset'
add_doublet_scores(
  x,
  assay_name = character(0),
  n_doublets = 1000,
  n_trials = NULL,
  sample_ratio = 0.5,
  n_dimensions = 30L,
  n_iterations = 2,
  lsi_method = 1,
  binarize = TRUE,
  corr_cutoff = 0.75,
  scale_dims = FALSE,
  umap_params = list(),
  lsi_params = list(),
  scoring_method = c("UMAP", "LSI"),
  knn_method = knn_hnsw(k = 10L, metric = "euclidean", ef = 200L),
  force = FALSE,
  seed = 1,
  batch = TRUE,
  threads = NULL,
  verbose = getOption("MarchR.verbose", FALSE),
  ...
)

# S3 method for class 'MarchRProject'
add_doublet_scores(
  x,
  assay_name = character(0),
  dataset_names = character(0),
  n_doublets = 1000,
  n_trials = NULL,
  sample_ratio = 0.5,
  n_dimensions = 30L,
  n_iterations = 2,
  lsi_method = 1,
  binarize = TRUE,
  corr_cutoff = 0.75,
  scale_dims = FALSE,
  umap_params = list(),
  lsi_params = list(),
  scoring_method = c("UMAP", "LSI"),
  knn_method = knn_hnsw(k = 10L, metric = "euclidean", ef = 200L),
  combine = FALSE,
  force = FALSE,
  seed = 1,
  batch = TRUE,
  threads = NULL,
  verbose = getOption("MarchR.verbose", FALSE),
  ...
)

Arguments

x

A MarchRDataset or MarchRProject.

n_doublets

(numeric) Number of doublets to simulate from the total cells in the sample.

n_trials

(numeric or NULL) Number of simulation trials (each producing n_doublets synthetic doublets). If NULL (default), 5 * max(floor(n_cells / n_doublets), 1).

sample_ratio

(numeric) Downsample of non-zero input matrix entries to run simulation on.

n_dimensions

(numeric) Number of dimensions in LSI DimReduction model.

n_iterations

(numeric) Number of LSI iterations to run.

lsi_method

(integer or character) Method for TF-IDF normalization. Default is 1 (tf-logidf) to match ArchR doublet inference. See IterativeLSI() for details.

binarize

(logical) Binarize input matrix prior to LSI (ArchR default is TRUE)

corr_cutoff

(numeric) Numeric filter for the correlation of a PC to the sequencing depth. If the PC has a correlation that is greater or equal to the corr_cutoff, it will be excluded from the final matrix.

scale_dims

(logical) Whether to apply row-wise z-score scaling to the LSI embedding during doublet inference. Default is FALSE, matching ArchR's addDoubletScores(scaleDims = FALSE). Passed through to IterativeLSI().

umap_params

(list) List of parameters to pass to the UMAP function if "UMAP" is designated to scoring_method.

lsi_params

(list) Named overrides for the internal IterativeLSI() fit, e.g. list(preset = "archr"). Defaults set here take precedence only where unspecified. See the function umap in the uwot package.

scoring_method

(character) The name of the dimensionality reduction method to be used for k-nearest neighbors calculation. This affects results in output columns: doublet_enrichment and doublet_score. Possible values are "UMAP" or "LSI". Default is "UMAP".

knn_method

(function) KNN method. Default knn_hnsw(k = 10L, metric = "euclidean", ef = 200L). Put KNN-specific arguments such as k or metric inside the partial function.

force

(logical) Force through UMAP projection to scoring even if R^2 < 0.9 for re-projection of training data. This typically indicates a very homogenous population of cells, and is not recommended to continue. Default is FALSE.

seed

(integer) Random seed used for doublet simulation and UMAP.

batch

(logical) Use uwot's deterministic batch UMAP optimization. Default TRUE,

threads

(integer) Per-call override of set_threads(). Default NULL uses get_threads() for the LSI SVD and UMAP, and single-threads IterativeLSI clustering KNN. Note that an explicit value also threads clustering, which makes doublet scores non-deterministic.

verbose

(logical) Print progress messages. Default getOption("MarchR.verbose", FALSE).

...

Additional arguments passed to methods.

assay_name

(character) The name of the matrix/assay to perform operation on. If not provided, will use smart selection when only one assay is present.

dataset_names

(character) For MarchRProject only. The name(s) of the dataset(s) containing input tile matrix. If not provided, will use smart selection when only one dataset is present.

combine

(logical) MarchRProject only. If TRUE, combine matrices from dataset_names as replicates of one experiment and store the scored result as a new dataset whose name joins the source dataset names. Matrices must have the same assay_name.

Value

The modified MarchRDataset or MarchRProject with a $doublet_data slot added to each processed dataset. $doublet_data is a named list containing:

scores

data.frame (rownames = cell IDs) with columns doublet_score and doublet_enrichment

cell_umap

data.frame (rownames = cell IDs) with columns UMAP1, UMAP2; NULL if UMAP projection failed

sim_umap

data.frame with columns UMAP1, UMAP2 for simulated doublets; NULL if UMAP projection failed

lsi_cells

Dense matrix (cells x dims) of the LSI cell embedding for downstream iteration

All doublet data is removed in a single operation by filter_doublets(clear_doublet_data = TRUE), or manually via proj$doublet_data <- NULL.

Details

The function performs the following steps:

  1. Simulate n_doublets for each of n_trials from the input ATAC (features x cells) matrix.

  2. Fit IterativeLSI to produce real-cell embeddings with shape (cells x retained dimensions).

  3. Project the simulated doublets into the fitted LSI space and, when requested, into the fitted UMAP space.

  4. Calculate KNN-based doublet enrichment and adjusted-binomial scores for every observed cell.