Convert an AnnData
object to a SingleCellExperiment
object
Arguments
- adata
The
AnnData
object to convert- assays_mapping
A named vector where names are names of
assays
in the resultingSingleCellExperiment
object and values are keys oflayers
inadata
. See below for details.- colData_mapping
A named vector where names are columns of
colData
in the resultingSingleCellExperiment
object and values are columns ofobs
inadata
. See below for details.- rowData_mapping
A named vector where names are columns of
rowData
in the resultingSingleCellExperiment
object and values are columns ofvar
inadata
. See below for details.- reducedDims_mapping
A named vector where names are names of
reducedDims
in the resultingSingleCellExperiment
object and values are keys ofobsm
inadata
. Alternatively, a named list where names are names ofreducedDims
in the resultingSingleCellExperiment
object and values are vectors with the items"sampleFactors"
and"featureLoadings"
and/or"metadata"
. See below for details.- colPairs_mapping
A named vector where names are names of
colPairs
in the resultingSingleCellExperiment
object and values are keys ofobsp
inadata
. See below for details.- rowPairs_mapping
A named vector where names are names of
rowPairs
in the resultingSingleCellExperiment
object and values are keys ofvarp
inadata
. See below for details.- metadata_mapping
A named vector where names are names of
metadata
in the resultingSingleCellExperiment
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 SingleCellExperiment
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 reducedDims_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(sce_item = "adata_item")
will copyadata_item
from the slot inadata
tosce_item
in the corresponding slot of newSingleCellExperiment
objectc()
will avoid copying anything to the slotc("adata_item")
is equivalent toc(adata_item = "adata_item")
Conversion table
From AnnData | To SingleCellExperiment | Example mapping argument | Default if NULL |
adata$layers | assays(sce) | assays_mapping = c(counts = "counts") | All items are copied by name |
adata$obs | colData(sce) | colData_mapping = c(n_counts = "n_counts", cell_type = "CellType") | All columns are copied by name |
adata$var | rowData(sce) | rowData_mapping = c(n_cells = "n_cells", pct_zero = "PctZero") | All columns are copied by name |
adata$obsm | reducedDims(sce) | reducedDims_mapping = c(pca = "X_pca") OR reducedDims_mapping = list(pca = c(obsm = "X_pca", varm = "PCs", uns = "pca_metadata")) | All items are copied by name without loadings except for "X_pca" for which loadings are added from "PCs" |
adata$obsp | colPairs(sce) | colPairs_mapping = c(nn = "connectivities") | All items are copied by name |
adata$varp | rowPairs(sce) | rowPairs_mapping = c(gene_overlaps = "similarities") | All items are copied by name |
adata$uns | metadata(sce) | uns_mapping = c(project_metadata = "metadata") | All items are copied by name |
The reducedDims_mapping
argument
For the simpler named vector format, the names should be the names of
reducedDims
in the resulting SingleCellExperiment
object, and the values
should be the keys of obsm
in adata
.
For more advanced mapping, use the list format where each item is a vector
with the following names used to create a
SingleCellExperiment::LinearEmbeddingMatrix
(if featureLoadings
or
metadata
is provided):
sampleFactors
: a key of theobsm
slot inadata
,adata$obsm[[sampleFactors]]
is passed to thesampleFactors
argumentfeatureLoadings
: a key of thevarm
slot inadata
(optional),adata$varm[[featureLoadings]]
is passed to thefeatureLoadings
argumentmetadata
: a key of theuns
slot inadata
(optional),adata$uns[[metadata]]
is passed to themetadata
argument
See also
Other object converters:
as_AnnData()
,
as_HDF5AnnData
,
as_InMemoryAnnData
,
as_Seurat
Examples
ad <- AnnData(
X = matrix(1:5, 3L, 5L),
layers = list(A = matrix(5:1, 3L, 5L), B = matrix(letters[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
sce <- ad$as_SingleCellExperiment(
assays_mapping = NULL,
colData_mapping = NULL,
rowData_mapping = NULL,
reducedDims_mapping = NULL,
colPairs_mapping = NULL,
rowPairs_mapping = NULL,
metadata_mapping = NULL
)