Skip to contents

Wrapper around manifoldsR::forceatlas2() and manifoldsR::forceatlas2_from_graph() for the SingleCells and MetaCells classes. ForceAtlas2 is a force-directed graph layout: every edge pulls, every pair of cells pushes, and gravity keeps disconnected components from drifting off. It is what scanpy's draw_graph does.

graph picks what gets laid out:

  • "knn" - The kNN graph (cached if use_knn = TRUE, otherwise built from the chosen embedding), turned into the UMAP fuzzy union graph first. This matches scanpy.

  • "connectivities" - The neighbour graph from find_neighbours_sc(), i.e. the same graph the Leiden/Louvain clustering runs on. use_knn, embd_to_use, no_embd_to_use, k, knn_method and nn_params do not apply here, and neither do the graph and initialisation knobs in fa2_params. Pass init_embd to start from an existing 2D embedding, otherwise the layout starts from random positions.

Usage

forceatlas2_sc(
  object,
  graph = c("knn", "connectivities"),
  use_knn = TRUE,
  embd_to_use = "pca",
  slot_name = "fa2",
  no_embd_to_use = NULL,
  init_embd = NULL,
  modality = c("rna", "adt", "atac", "wnn"),
  k = 15L,
  knn_method = c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"),
  nn_params = manifoldsR::params_nn(),
  fa2_params = manifoldsR::params_fa2(),
  seed = 42L,
  .verbose = TRUE
)

Arguments

object

SingleCells, MetaCells class.

graph

String. Which graph to lay out. One of c("knn", "connectivities"). Defaults to "knn".

use_knn

Boolean. Use the kNN graph found in the object. Defaults to TRUE. If not available, will default to the embedding. "knn" only.

embd_to_use

String. The embedding to build the kNN graph from. Must be available in the object. "knn" only.

slot_name

String. The name of this embedding within the object. Defaults to "fa2".

no_embd_to_use

Optional integer. Number of embedding dimensions to use. If NULL all will be used. "knn" only.

init_embd

Optional string. Name of a stored 2D embedding, e.g. "umap", to initialise the layout with. "connectivities" only. Defaults to NULL.

modality

String. On which modality to run ForceAtlas2. One of c("rna", "adt", "atac", "wnn"). The two latter options are only available for multi-modal versions with the added data.

k

Integer. Number of nearest neighbours. Defaults to 15L. "knn" only.

knn_method

String. Approximate nearest neighbour algorithm. One of c("kmknn", "hnsw", "annoy", "nndescent", "balltree", "ivf", "exhaustive"). Defaults to "kmknn". Only used when neighbours are computed from the embedding. "knn" only.

nn_params

Named list. See manifoldsR::params_nn(). "knn" only.

fa2_params

Named list. See manifoldsR::params_fa2().

seed

Integer. For reproducibility.

.verbose

Boolean. Controls verbosity.

Value

The object with a "fa2" embedding added.

References

Jacomy, et al., PLoS ONE, 2014

Examples

# ForceAtlas2 on the cached kNN graph, then on the neighbour graph
sc <- demo_single_cells()
sc <- forceatlas2_sc(sc, .verbose = FALSE)
sc <- forceatlas2_sc(
  sc,
  graph = "connectivities",
  slot_name = "fa2_graph",
  .verbose = FALSE
)
dim(get_embedding(sc, "fa2_graph"))
#> [1] 500   2

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