Computes the k.param
nearest neighbors for a given dataset. Can also
optionally (via compute.SNN
), construct a shared nearest neighbor
graph by calculating the neighborhood overlap (Jaccard index) between every
cell and its k.param
nearest neighbors.
FindNeighbors(object, ...)
# S3 method for default
FindNeighbors(
object,
query = NULL,
distance.matrix = FALSE,
k.param = 20,
return.neighbor = FALSE,
compute.SNN = !return.neighbor,
prune.SNN = 1/15,
nn.method = "annoy",
n.trees = 50,
annoy.metric = "euclidean",
nn.eps = 0,
verbose = TRUE,
l2.norm = FALSE,
cache.index = FALSE,
index = NULL,
...
)
# S3 method for Assay
FindNeighbors(
object,
features = NULL,
k.param = 20,
return.neighbor = FALSE,
compute.SNN = !return.neighbor,
prune.SNN = 1/15,
nn.method = "annoy",
n.trees = 50,
annoy.metric = "euclidean",
nn.eps = 0,
verbose = TRUE,
l2.norm = FALSE,
cache.index = FALSE,
...
)
# S3 method for dist
FindNeighbors(
object,
k.param = 20,
return.neighbor = FALSE,
compute.SNN = !return.neighbor,
prune.SNN = 1/15,
nn.method = "annoy",
n.trees = 50,
annoy.metric = "euclidean",
nn.eps = 0,
verbose = TRUE,
l2.norm = FALSE,
cache.index = FALSE,
...
)
# S3 method for Seurat
FindNeighbors(
object,
reduction = "pca",
dims = 1:10,
assay = NULL,
features = NULL,
k.param = 20,
return.neighbor = FALSE,
compute.SNN = !return.neighbor,
prune.SNN = 1/15,
nn.method = "annoy",
n.trees = 50,
annoy.metric = "euclidean",
nn.eps = 0,
verbose = TRUE,
do.plot = FALSE,
graph.name = NULL,
l2.norm = FALSE,
cache.index = FALSE,
...
)
An object
Arguments passed to other methods
Matrix of data to query against object. If missing, defaults to object.
Boolean value of whether the provided matrix is a
distance matrix; note, for objects of class dist
, this parameter will
be set automatically
Defines k for the k-nearest neighbor algorithm
Return result as Neighbor
object. Not
used with distance matrix input.
also compute the shared nearest neighbor graph
Sets the cutoff for acceptable Jaccard index when computing the neighborhood overlap for the SNN construction. Any edges with values less than or equal to this will be set to 0 and removed from the SNN graph. Essentially sets the stringency of pruning (0 --- no pruning, 1 --- prune everything).
Method for nearest neighbor finding. Options include: rann, annoy
More trees gives higher precision when using annoy approximate nearest neighbor search
Distance metric for annoy. Options include: euclidean, cosine, manhattan, and hamming
Error bound when performing nearest neighbor seach using RANN; default of 0.0 implies exact nearest neighbor search
Whether or not to print output to the console
Take L2Norm of the data
Include cached index in returned Neighbor object (only relevant if return.neighbor = TRUE)
Precomputed index. Useful if querying new data against existing index to avoid recomputing.
Features to use as input for building the (S)NN; used only when
dims
is NULL
Reduction to use as input for building the (S)NN
Dimensions of reduction to use as input
Assay to use in construction of (S)NN; used only when dims
is NULL
Plot SNN graph on tSNE coordinates
Optional naming parameter for stored (S)NN graph
(or Neighbor object, if return.neighbor = TRUE). Default is assay.name_(s)nn.
To store both the neighbor graph and the shared nearest neighbor (SNN) graph,
you must supply a vector containing two names to the graph.name
parameter. The first element in the vector will be used to store the nearest
neighbor (NN) graph, and the second element used to store the SNN graph. If
only one name is supplied, only the NN graph is stored.
This function can either return a Neighbor
object
with the KNN information or a list of Graph
objects with
the KNN and SNN depending on the settings of return.neighbor
and
compute.SNN
. When running on a Seurat
object, this
returns the Seurat
object with the Graphs or Neighbor objects
stored in their respective slots. Names of the Graph or Neighbor object can
be found with Graphs
or Neighbors
.
data("pbmc_small")
pbmc_small
#> An object of class Seurat
#> 230 features across 80 samples within 1 assay
#> Active assay: RNA (230 features, 20 variable features)
#> 3 layers present: counts, data, scale.data
#> 2 dimensional reductions calculated: pca, tsne
# Compute an SNN on the gene expression level
pbmc_small <- FindNeighbors(pbmc_small, features = VariableFeatures(object = pbmc_small))
#> Computing nearest neighbor graph
#> Computing SNN
# More commonly, we build the SNN on a dimensionally reduced form of the data
# such as the first 10 principle components.
pbmc_small <- FindNeighbors(pbmc_small, reduction = "pca", dims = 1:10)
#> Computing nearest neighbor graph
#> Computing SNN