Skip to contents

This function performs single-cell reference mapping using the K-nearest neighbor (KNN) method. It takes two single-cell datasets as input: srt_query and srt_ref. The function maps cells from the srt_query dataset to the srt_ref dataset based on their similarity or distance.

Usage

RunKNNMap(
  srt_query,
  srt_ref,
  query_assay = NULL,
  ref_assay = NULL,
  ref_umap = NULL,
  ref_group = NULL,
  features = NULL,
  nfeatures = 2000,
  query_reduction = NULL,
  ref_reduction = NULL,
  query_dims = 1:30,
  ref_dims = 1:30,
  projection_method = c("model", "knn"),
  nn_method = NULL,
  k = 30,
  distance_metric = "cosine",
  vote_fun = "mean",
  verbose = TRUE
)

Arguments

srt_query

An object of class Seurat to be annotated with cell types.

srt_ref

A Seurat object or count matrix representing the reference object. If provided, the similarities will be calculated between cells from the query and reference objects. If not provided, the similarities will be calculated within the query object.

query_assay

The assay to use for the query object. If not provided, the default assay of the query object will be used.

ref_assay

The assay to use for the reference object. If not provided, the default assay of the reference object will be used.

ref_umap

A character string specifying the name of the UMAP reduction in the reference object. If not provided, the first UMAP reduction found in the reference object will be used.

ref_group

The grouping variable in the reference object. This variable will be used to group cells in the heatmap columns. If not provided, all cells will be treated as one group.

features

A vector of feature names to include in the heatmap. If not provided, highly variable features (HVF) will be used.

nfeatures

The maximum number of features to include in the heatmap. Default is 2000.

query_reduction

A character string specifying the name of a dimensionality reduction in the query object to use for calculating the distance metric.

ref_reduction

A character string specifying the name of a dimensionality reduction in the reference object to use for calculating the distance metric.

query_dims

A numeric vector specifying the dimension indices from the query reduction to be used for calculating the distance metric.

ref_dims

A numeric vector specifying the dimension indices from the reference reduction to be used for calculating the distance metric.

projection_method

A character string specifying the projection method to use. Options are "model" and "knn". If "model" is selected, the function will try to use a pre-trained UMAP model in the reference object for projection. If "knn" is selected, the function will directly find the nearest neighbors using the distance metric.

nn_method

A character string specifying the nearest neighbor search method to use. Options are "raw", "annoy", "rann", and "cpp". If "raw" is selected, the function will use the brute-force method to find the nearest neighbors. If "annoy" is selected, the function will use the Annoy library for approximate nearest neighbor search. If "rann" is selected, the function will use the RANN library for approximate nearest neighbor search. If "cpp" is selected, the function will use the compiled exact top-k search. If not provided, the function will use "cpp" for Euclidean or cosine distance, otherwise it will choose the search method based on the size of the query and reference datasets.

k

A number of nearest neighbors to find for each cell in the query object.

distance_metric

The distance metric to use for calculating the pairwise distances between cells. Options include: "pearson", "spearman", "cosine", "correlation", "jaccard", "ejaccard", "dice", "edice", "hamman", "simple matching", and "faith". Additional distance metrics can also be used, such as "euclidean", "manhattan", "hamming", etc.

vote_fun

A character string specifying the function to be used for aggregating the nearest neighbors in the reference object. Options are "mean", "median", "sum", "min", "max", "sd", "var", etc. If not provided, the default is "mean".

verbose

Whether to print the message. Default is TRUE.

Value

A Seurat object with the projection results stored in the "ref.embeddings" reduction. If ref_group is provided, the function will also add a new metadata column called "predicted_ref_group" to the query object.

Examples

data(panc8_sub)
panc8_sub <- standard_scop(panc8_sub)
srt_ref <- panc8_sub[, panc8_sub$tech != "fluidigmc1"]
srt_query <- panc8_sub[, panc8_sub$tech == "fluidigmc1"]
srt_ref <- integration_scop(
  srt_ref,
  batch = "tech",
  integration_method = "Uncorrected"
)
CellDimPlot(
  srt_ref,
  group.by = c("celltype", "tech")
)

# Set the number of threads for RcppParallel
# details see: ?RcppParallel::setThreadOptions
# if (requireNamespace("RcppParallel", quietly = TRUE)) {
#   RcppParallel::setThreadOptions()
# }
# Projection
srt_query <- RunKNNMap(
  srt_query = srt_query,
  srt_ref = srt_ref,
  ref_umap = "UncorrectedUMAP2D"
)
ProjectionPlot(
  srt_query = srt_query,
  srt_ref = srt_ref,
  query_group = "celltype",
  ref_group = "celltype"
)