Convert an AnnData
object to a Seurat
object
Usage
as_Seurat(
adata,
assay_name = "RNA",
x_mapping = NULL,
layers_mapping = TRUE,
object_metadata_mapping = TRUE,
assay_metadata_mapping = TRUE,
reduction_mapping = TRUE,
graph_mapping = TRUE,
misc_mapping = TRUE
)
Arguments
- adata
The
AnnData
object to convert.- assay_name
Name of the assay to be created in the new
Seurat
object- x_mapping
A string specifying the name of the layer in the resulting
Seurat
object where the data in theX
slot ofadata
will be mapped to- 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 TRUE
, the conversion function will guess
which items to copy as described in the 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, set the mapping argument to FALSE
. Empt
mapping arguments (NULL
, c()
, list()
) will be treated as FALSE
with
a warning. If an unnamed vector is provided, the values will be used as
names.
Examples:
TRUE
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 the newSeurat
objectFALSE
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$X | Layers(seurat) | x_mapping = "counts" OR layers_mapping = c(counts = NA) | The data in adata$X is copied to a layer named X |
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
The x_mapping
and layers_mapping
arguments
In order to specify where the data in adata$X
will be stored in the
Layers(seurat)
slot of the resulting object, you can use either the x_mapping
argument or the layers_mapping
argument.
If you use x_mapping
, it should be a string specifying the name of the layer
in Layers(seurat)
where the data in adata$X
will be stored.
If you use layers_mapping
, it should be a named vector where names are names
of Layers(seurat)
and values are keys of layers
in adata
.
In order to indicate the adata$X
slot, you use NA
as the value in the vector.
The name you provide for x_mapping
may not be a name in layers_mapping
.
You must provide a layer named counts
or data
in either x_mapping
or
layers_mapping
.
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",
x_mapping = "counts",
layers_mapping = TRUE,
object_metadata_mapping = TRUE,
assay_metadata_mapping = TRUE,
reduction_mapping = TRUE,
graph_mapping = TRUE,
misc_mapping = TRUE
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.