Convert a SingleCellExperiment to a Python AnnData object
sce_to_anndata.RdCreates a Python anndata.AnnData object from a
SingleCellExperiment::SingleCellExperiment using a
scipy.sparse.csc_matrix built with sce_assay_to_scipy_csc().
Copy semantics
X (the main count matrix): the dgCMatrix is first converted to a
scipy.sparse.csc_matrix(shapegenes × cells) viasce_assay_to_scipy_csc(), then transposed toscipy.sparse.csr_matrix(shapecells × genes) by.T. The transpose shares all data buffers with the original CSC (flags.owndata = FALSE), so no values are copied.obs / var metadata: a pandas
DataFrameis constructed from the Rdata.frame; reticulate performs one copy here, which is unavoidable for columnar R data frames → row-oriented Python objects.layers / obsm / varm: passed through as-is; the caller is responsible for any copy semantics of those objects.
Arguments
- x
- assay
A single string: the assay to place in
X(default"counts").- obs
NULLor adata.frameof cell-level metadata. Defaults toas.data.frame(colData(x))withrownamesset tocolnames(x).- var
NULLor adata.frameof gene-level metadata. Defaults toas.data.frame(rowData(x))withrownamesset torownames(x).- layers
A named list of additional assay matrices to place in
AnnData$layers.- obsm
A named list of matrices added to
AnnData$obsm.- varm
A named list of matrices added to
AnnData$varm.
Examples
if (FALSE) { # \dontrun{
sce <- SingleCellExperiment::SingleCellExperiment(
assays = list(counts = Matrix::rsparsematrix(200, 50, 0.1, repr = "C"))
)
adata <- sce_to_anndata(sce)
cat("obs x var:", reticulate::py_str(adata$shape), "\n")
} # }