Convert an AnnData to a SingleCellExperiment
      Source: R/as_SingleCellExperiment.R
      as_SingleCellExperiment.RdConvert 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
AnnDataobject to convert- x_mapping
 A string specifying the name of the assay in the resulting
SingleCellExperimentwhere the data in theXslot ofadatawill be mapped to- assays_mapping
 A named vector where names are names of
assaysin the resultingSingleCellExperimentobject and values are keys oflayersinadata. See below for details.- colData_mapping
 A named vector where names are columns of
colDatain the resultingSingleCellExperimentobject and values are columns ofobsinadata. See below for details.- rowData_mapping
 A named vector where names are columns of
rowDatain the resultingSingleCellExperimentobject and values are columns ofvarinadata. See below for details.- reducedDims_mapping
 A named vector where names are names of
reducedDimsin the resultingSingleCellExperimentobject and values are keys ofobsminadata. Alternatively, a named list where names are names ofreducedDimsin the resultingSingleCellExperimentobject 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
colPairsin the resultingSingleCellExperimentobject and values are keys ofobspinadata. See below for details.- rowPairs_mapping
 A named vector where names are names of
rowPairsin the resultingSingleCellExperimentobject and values are keys ofvarpinadata. See below for details.- metadata_mapping
 A named vector where names are names of
metadatain the resultingSingleCellExperimentobject and values are keys ofunsinadata. 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:
TRUEwill guess which items to copy as described in the conversion tablec(sce_item = "adata_item")will copyadata_itemfrom the slot inadatatosce_itemin the corresponding slot of the newSingleCellExperimentobjectFALSEwill 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 | 
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(sampleFactors = "X_pca", featureLoadings = "PCs", metadata = "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 theobsmslot inadata,adata$obsm[[sampleFactors]]is passed to thesampleFactorsargumentfeatureLoadings: a key of thevarmslot inadata(optional),adata$varm[[featureLoadings]]is passed to thefeatureLoadingsargumentmetadata: a key of theunsslot inadata(optional),adata$uns[[metadata]]is passed to themetadataargument
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.
See also
Other object converters:
as_AnnData(),
as_HDF5AnnData(),
as_InMemoryAnnData(),
as_ReticulateAnnData(),
as_Seurat(),
reticulate-helpers
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
 )