Skip to contents

This function implements the fast mutual nearest neighbour (MNN) from Haghverdi, et al. This version works on the PCA embedding and generates an embedding only and not a fully corrected count matrix. The function will iterate through the batches, identify the MNN and generate correction vectors and generate a corrected embedding which is added to the function.

Usage

fast_mnn_sc(
  object,
  batch_column,
  batch_hvg_genes,
  fastmnn_params = params_sc_fastmnn(),
  use_precomputed_pca = FALSE,
  seed = 42L,
  .verbose = TRUE
)

Arguments

object

SingleCells or SingleCellsSubset class.

batch_column

String. The column with the batch information in the obs data of the class.

batch_hvg_genes

Integer vector. These are the highly variable genes, identified by a batch-aware method. Please refer to find_hvg_batch_aware_sc() for more details. These genes have to be 0-indexed!

fastmnn_params

A list, please see params_sc_fastmnn(). The list has the following parameters:

  • sigma - Numeric. Bandwidth of the Gaussian smoothing kernel (as proportion of space radius).

  • cos_norm - Logical. Apply cosine normalisation before computing distances.

  • var_adj - Logical. Apply variance adjustment to avoid kissing effects.

  • no_pcs - Integer. Number of PCs to use for MNN calculations.

  • knn - List of kNN parameters. See params_knn_defaults() for available parameters and their defaults.

  • pca - List of PCA parameters, see params_sc_pca() for available parameters and their defaults.

use_precomputed_pca

Boolean. Should the PCA in the object be used if found. If you decide to do this, make sure that you have run the PCA on the batch-aware HVG ideally.

seed

Integer. Random seed.

.verbose

Boolean or integer. Controls verbosity and returns run times. FALSE -> quiet, TRUE or 1L -> normal verbosity, 2L -> detailed verbosity.

Value

The object with the added fastMNN embeddings to the object.

References

Haghverdi, et al., Nat Biotechnol, 2018

Examples

# fastMNN over batch aware highly variable genes
sc <- demo_single_cells(
  syn_data_params = params_sc_synthetic_data(
    n_cells = 600L, n_genes = 50L, n_batches = 3L
  )
)
hvg <- find_hvg_batch_aware_sc(
  sc, hvg_no = 20L, batch_column = "batch_index", .verbose = FALSE
)
sc <- fast_mnn_sc(
  sc,
  batch_column = "batch_index",
  batch_hvg_genes = hvg$hvg_gene_idx,
  fastmnn_params = params_sc_fastmnn(
    no_pcs = 10L,
    knn = list(k = 5L)
  ),
  .verbose = FALSE
)
dim(get_embedding(sc, "mnn"))
#> [1] 600  10

unlink(sc@dir_data, recursive = TRUE, force = TRUE)