Convert an AnnData
object to a Seurat
object
Arguments
- adata
The
AnnData
object to convert.- assay_name
Name of the assay to be created in the new
Seurat
object- layers_mapping
A named vector where names are names of
Layers
in the resultingSeurat
object and values are keys oflayers
inadata
. See below for details.- object_metadata_mapping
A named vector where names are cell metadata columns in the resulting
Seurat
object and values are columns ofobs
inadata
. See below for details.- assay_metadata_mapping
A named vector where names are variable metadata columns in the assay of the resulting
Seurat
object and values are columns ofvar
inadata
. See below for details.- reduction_mapping
A named vector where names are names of
Embeddings
in the resultingSeurat
object and values are keys ofobsm
inadata
. Alternatively, a named list where names are names ofEmbeddings
in the resultingSeurat
object and values are vectors with the items"key"
,"embeddings"
and (optionally)"loadings"
. See below for details.- graph_mapping
A named vector where names are names of
Graphs
in the resultingSeurat
object and values are keys ofobsp
inadata
. See below for details.- misc_mapping
A named vector where names are names of
Misc
in the resultingSeurat
object and values are keys ofuns
inadata
. See below for details.
Details
Mapping arguments
All mapping arguments expect a named character vector where names are the
names of the slot in the Seurat
object and values are the keys of the
corresponding slot of adata
. If NULL
, the conversion function will guess
which items to copy as described in the conversion tables conversion table
below. In most cases, the default is to copy all items using the same names
except where the correspondence between objects is unclear. The
reduction_mapping
argument can also accept a more complex list format, see
below for details. To avoid copying anything to a slot, provide an empty
vector. If an unnamed vector is provided, the values will be used as names
Examples:
NULL
will guess which items to copy as described in the conversion tablec(seurat_item = "adata_item")
will copyadata_item
from the slot inadata
toseurat_item
in the corresponding slot of newSeurat
objectc()
will avoid copying anything to the slotc("adata_item")
is equivalent toc(adata_item = "adata_item")
Conversion table
From AnnData | To Seurat | Example mapping argument | Default if NULL |
adata$layers | Layers(seurat) | layers_mapping = c(counts = "counts") | All items are copied by name |
adata$obs | seurat[[]] | object_metadata_mapping = c(n_counts = "n_counts", cell_type = "CellType") | All columns are copied by name |
adata$var | seurat[[assay_name]][[]] | assay_metadata_mapping = c(n_cells = "n_cells", pct_zero = "PctZero") | All columns are copied by name |
adata$obsm | Embeddings(sce) | reduction_mapping = c(pca = "X_pca") OR reduction_mapping = list(pca = c(key = "PC_", obsm = "X_pca", varm = "PCs")) | All items that can be coerced to a numeric matrix are copied by name without loadings except for "X_pca" for which loadings are added from "PCs" |
adata$obsp | Graphs(seurat) | graph_mapping = c(nn = "connectivities") | All items are copied by name |
adata$varp | NA | NA | There is no corresponding slot for varp |
adata$uns | Misc(seurat) | misc_mapping = c(project_metadata = "metadata") | All items are copied by name |
The reduction_mapping
argument
For the simpler named vector format, the names should be the names of
Embeddings
in the resulting Seurat
object, and the values
should be the keys of obsm
in adata
. A key will created from the obsm
key.
For more advanced mapping, use the list format where each item is a vector
with the following names defining arguments to
SeuratObject::CreateDimReducObject()
:
key
: the key of the resultingSeuratObject::DimReduc
object, passed to thekey
argument after processing withSeuratObject::Key()
embeddings
: a key of theobsm
slot inadata
,adata$obsm[[embeddings]]
is passed to theembeddings
argumentloadings
: a key of thevarm
slot inadata
(optional),adata$varm[[loadings]]
is passed to theloadings
argument
See also
Other object converters:
as_AnnData()
,
as_HDF5AnnData
,
as_InMemoryAnnData
,
as_SingleCellExperiment
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)
)
# Default usage
seurat <- ad$as_Seurat(
assay_name = "RNA",
layers_mapping = NULL,
object_metadata_mapping = NULL,
assay_metadata_mapping = NULL,
reduction_mapping = NULL,
graph_mapping = NULL,
misc_mapping = NULL
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.