Convert an AnnData
to a SingleCellExperiment
Source: R/as_SingleCellExperiment.R
as_SingleCellExperiment.Rd
Convert an AnnData
object to a SingleCellExperiment
object
Usage
as_SingleCellExperiment(
adata,
x_mapping = NULL,
assays_mapping = TRUE,
colData_mapping = TRUE,
rowData_mapping = TRUE,
reducedDims_mapping = TRUE,
colPairs_mapping = TRUE,
rowPairs_mapping = TRUE,
metadata_mapping = TRUE
)
Arguments
- adata
The
AnnData
object to convert- x_mapping
A string specifying the name of the assay in the resulting
SingleCellExperiment
where the data in theX
slot ofadata
will be mapped to- 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 TRUE
, the conversion function
will guess which items to copy as described in the conversion tables 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, set the mapping
argument to FALSE
. Empty 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(sce_item = "adata_item")
will copyadata_item
from the slot inadata
tosce_item
in the corresponding slot of the newSingleCellExperiment
objectFALSE
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$X | assays(sce) | x_mapping = "counts" | The data in adata$X is copied to the assay named X |
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
The x_mapping
and assays_mapping
arguments
In order to specify where the data in adata$X
will be stored in the
assays(sce)
slot of the resulting object, you can use either the x_mapping
argument or the assays_mapping
argument.
If you use x_mapping
, it should be a string specifying the name of the layer
in assays(sce)
where the data in adata$X
will be stored.
If you use assays_mapping
, it should be a named vector where names are names
of assays(sce)
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 assays_mapping
.
You must provide an assay named counts
or data
in either x_mapping
or
assays_mapping
.
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 = TRUE,
colData_mapping = TRUE,
rowData_mapping = TRUE,
reducedDims_mapping = TRUE,
colPairs_mapping = TRUE,
rowPairs_mapping = TRUE,
metadata_mapping = TRUE
)