Skip to contents

to_Seurat() converts an AnnData object to a Seurat object. Only one assay can be converted at a time. Arguments are used to configure the conversion. If NULL, the functions to_Seurat_guess_* will be used to guess the mapping.

Usage

to_Seurat(
  adata,
  assay_name = "RNA",
  layers_mapping = NULL,
  object_metadata_mapping = NULL,
  assay_metadata_mapping = NULL,
  reduction_mapping = NULL,
  graph_mapping = NULL,
  misc_mapping = NULL
)

Arguments

adata

An AnnData object to be converted

assay_name

Name of the assay to be created (default: "RNA").

layers_mapping

A named vector to map AnnData layers to Seurat layers. See section "Layer mapping" for more details.

object_metadata_mapping

A named vector mapping observation-level metadata to object-level metadata in the Seurat object. See section "Metadata mapping" for more details.

assay_metadata_mapping

A named vector mapping variable-level metadata to assay-level metadata in the Seurat object. See section "Metadata mapping" for more details.

reduction_mapping

A named vector or list to map AnnData reductions to Seurat reductions. The named vector format is simpler to use for common cases. The list format allows more advanced mapping with explicit keys. See section "Reduction mapping" for more details.

graph_mapping

A named vector mapping graph names to the names of the graphs in the AnnData object. See section "Graph mapping" for more details.

misc_mapping

A named vector mapping miscellaneous data to the names of the data in the Seurat object. See section "Miscellaneous mapping" for more details.

Value

A Seurat object

Layer mapping

A named vector to map AnnData layers to Seurat layers. Each name corresponds to the layer name in the Seurat object, and each value corresponds to the layer name in the AnnData object. A value of NA corresponds to the AnnData X slot.

Example: layers_mapping = c(counts = "counts", data = NA, foo = "bar")

If NULL, the internal function .to_Seurat_guess_layers will be used to guess the layer mapping as follows:

  • All AnnData layers are copied to Seurat layers by name

  • If there is layer named "counts", adata$X is stored a "data" otherwise it is stored as "counts"

Metadata mapping

A named vector to map observation-level and feature-level metadata to object-level and assay-level metadata in the Seurat object.

Each value in the object_metadata_mapping named vector corresponds to the names of the obs slot in the AnnData object, and each of the names correspond to the names of the metadata in the resulting Seurat object.

Example: object_metadata_mapping = c(cellType = "cell_type").

Each value in the assay_metadata_mapping named vector corresponds to the names of the var slot in the AnnData object, and the names correspond to the names of the metadata in the specified assay of the resulting Seurat object.

Example: assay_metadata_mapping = c(geneInfo = "gene_info").

By default, all metadata in the obs and var slots will be copied to the Seurat object.

Reduction mapping

A named vector or list to map AnnData $obsm and $varm to Seurat reductions.

When using a named vector, each name corresponds to the reduction name in the Seurat object, and each value corresponds to the key in AnnData's $obsm slot. This is a simpler format for common use cases.

Example: reduction_mapping = c(pca = "X_pca", umap = "X_umap")

For more advanced mapping with explicit keys and varm entries, use the list format. Each item in the list must be a named vector with keys 'key', 'obsm', and can contain the key 'varm'.

Example: reduction_mapping = list(pca = c(key = "PC_", obsm = "X_pca", varm = "PCs")).

If 'varm' is provided, the matching matrix is given to the loadings argument of SeuratObject::CreateDimReducObject().

If NULL, the internal function .to_Seurat_guess_reductions will be used to guess the reduction mapping as follows:

  • All $obsm items that can be coerced to a numeric matrix are copied by name

  • If there is an $obsm item named "PCA" and a $varm item named "PCs" then 'varm' will be set to "PCs"

All keys are processed by SeuratObject::Key() to match requirements

Graph mapping

A named vector mapping graph names to the names of the graphs in the AnnData object. Each name corresponds to the graph name in the resulting Seurat object, and each value corresponds to the name of the graph in the AnnData object.

Example: graph_mapping = c(nn = "connectivities").

If NULL, the internal function .to_Seurat_guess_graphs will be used to guess the graph mapping as follows:

  • All $obsp items are copied by name

Miscellaneous mapping

A named vector mapping miscellaneous data to the names of the data in the AnnData object. Each name corresponds to the name in the resulting Seurat object, and each value corresponds to the name of the data in the AnnData object's uns slot.

Example: misc_mapping = c(parameters = "params", cell_types = "annotations")

If NULL, the internal function .to_Seurat_guess_misc will be used to guess the miscellaneous mapping as follows:

  • If $uns is defined, all values in $uns are copied to the Seurat misc

Mapping details

If an unnamed vector is provided to a mapping argument the values will be used as names

Examples

ad <- AnnData(
  X = matrix(1:5, 3L, 5L),
  obs = data.frame(row.names = LETTERS[1:3], cell = 1:3),
  var = data.frame(row.names = letters[1:5], gene = 1:5)
)
to_Seurat(ad)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> An object of class Seurat 
#> 5 features across 3 samples within 1 assay 
#> Active assay: RNA (5 features, 0 variable features)
#>  1 layer present: counts