Convert an AnnData object to a SingleCellExperiment object
Source:R/SingleCellExperiment.R
to_SingleCellExperiment.Rd
to_SingleCellExperiment()
converts an AnnData object
to a SingleCellExperiment object.
Usage
to_SingleCellExperiment(
adata,
assays_mapping = NULL,
colData_mapping = NULL,
rowData_mapping = NULL,
reduction_mapping = NULL,
colPairs_mapping = NULL,
rowPairs_mapping = NULL,
metadata_mapping = NULL
)
Arguments
- adata
an AnnData object, e.g., InMemoryAnnData
- assays_mapping
A named vector mapping
layers
inadata
toassay
names in the created SingleCellExperiment object. The names of the vector should be the names of theassays
in the resulting SingleCellExperiment object, and the values should be the names of thelayers
inadata
, and can include theX
matrix as well. IfX
is not in the list, it will be added ascounts
ordata
.- colData_mapping
A named vector mapping
obs
inadata
tocolData
in the created SingleCellExperiment object. The names of the vector should be the names of thecolData
columns in the resulting SingleCellExperiment object. The values should be the names of theobs
columns inadata
.- rowData_mapping
A named vector mapping
var
names inadata
torowData
in the created SingleCellExperiment object. The names of the vector should be the names of therowData
columns in the resulting SingleCellExperiment object. The values should be the names of thevar
columns inadata
.- reduction_mapping
A named vector or list mapping reduction names in
adata
to reduction names in the created SingleCellExperiment object. For the simpler named vector format, the names should be the names of thereducedDims
in the resulting SingleCellExperiment object, and the values should be the names of theobsm
inadata
.For more advanced mapping, use the list format where each item has the following keys:
obsm
: the name of theobsm
slot inadata
varm
: the name of thevarm
slot inadata
(optional)uns
: the name of theuns
slot inadata
(optional)
If
'varm'
or'uns'
is given, a SingleCellExperiment::LinearEmbeddingMatrix will be created for that item withadata$varm[[varm]]
passed to thefeatureLoadings
argument andadata$uns[[uns]]
passed asmetadata
- colPairs_mapping
A named vector mapping obsp names in
adata
to colPairs names in the created SingleCellExperiment object. The names of the vector should be the names of thecolPairs
in the resulting SingleCellExperiment object. The values should be the names of theobsp
inadata
.- rowPairs_mapping
A named vector mapping varp names in
adata
to rowPairs names in the created SingleCellExperiment object. The names of the vector should be the names of therowPairs
in the resulting SingleCellExperiment object. The values should be the names of thevarp
inadata
.- metadata_mapping
A named vector mapping uns names in
adata
to metadata names in the created SingleCellExperiment object. The names of the vector should be the names of themetadata
in the resulting SingleCellExperiment object. The values should be the names of theuns
inadata
.
Examples
if (interactive()) {
## useful when interacting with the SingleCellExperiment !
library(SingleCellExperiment)
}
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)
)
## construct a SingleCellExperiment from an AnnData object
sce <- to_SingleCellExperiment(ad)
sce
#> class: SingleCellExperiment
#> dim: 5 3
#> metadata(0):
#> assays(3): counts A B
#> rownames(5): a b c d e
#> rowData names(1): gene
#> colnames(3): A B C
#> colData names(1): cell
#> reducedDimNames(0):
#> mainExpName: NULL
#> altExpNames(0):