Convert other objects to an AnnData
object. See the sections below for
details on how slots are mapped between objects. For more information on the
functionality of an AnnData
object, see AnnData-usage.
Usage
as_AnnData(
x,
x_mapping = NULL,
layers_mapping = NULL,
obs_mapping = NULL,
var_mapping = NULL,
obsm_mapping = NULL,
varm_mapping = NULL,
obsp_mapping = NULL,
varp_mapping = NULL,
uns_mapping = NULL,
assay_name = NULL,
output_class = c("InMemory", "HDF5AnnData"),
...
)
# S3 method for class 'SingleCellExperiment'
as_AnnData(
x,
x_mapping = NULL,
layers_mapping = NULL,
obs_mapping = NULL,
var_mapping = NULL,
obsm_mapping = NULL,
varm_mapping = NULL,
obsp_mapping = NULL,
varp_mapping = NULL,
uns_mapping = NULL,
assay_name = NULL,
output_class = c("InMemory", "HDF5AnnData"),
...
)
# S3 method for class 'Seurat'
as_AnnData(
x,
x_mapping = NULL,
layers_mapping = NULL,
obs_mapping = NULL,
var_mapping = NULL,
obsm_mapping = NULL,
varm_mapping = NULL,
obsp_mapping = NULL,
varp_mapping = NULL,
uns_mapping = NULL,
assay_name = NULL,
output_class = c("InMemory", "HDF5AnnData"),
...
)
Arguments
- x
The object to convert
- x_mapping
A string specifying the data to map to the
X
slot. IfNULL
, no data will be copied to theX
slot.- layers_mapping
A named character vector where the names are keys of
layers
in the newAnnData
object and values are the names of items in the corresponding slot ofx
. See below for default ifNULL
depending on the class ofx
.- obs_mapping
A named character vector where the names are names of
obs
columns in the newAnnData
object and values are the names of columns in the corresponding slot ofx
. See below for default ifNULL
depending on the class ofx
.- var_mapping
A named character vector where the names are names of
var
columns in the newAnnData
object and values are the names of columns in the corresponding slot ofx
. See below for default ifNULL
depending on the class ofx
.- obsm_mapping
A named character vector where the names are keys of
obsm
in the newAnnData
object and values are the names of items in the corresponding slot ofx
. See below for default ifNULL
depending on the class ofx
.- varm_mapping
A named character vector where the names are keys of
varm
in the newAnnData
object and values are the names of items in the corresponding slot ofx
. See below for default ifNULL
depending on the class ofx
.- obsp_mapping
A named character vector where the names are keys of
obsp
in the newAnnData
object and values are the names of items in the corresponding slot ofx
. See below for default ifNULL
depending on the class ofx
.- varp_mapping
A named character vector where the names are keys of
varp
in the newAnnData
object and values are the names of items in the corresponding slot ofx
. See below for default ifNULL
depending on the class ofx
.- uns_mapping
A named character vector where the names are keys of
uns
in the newAnnData
object and values are the names of items in the corresponding slot ofx
. See below for default ifNULL
depending on the class ofx
.- assay_name
For
SeuratObject::Seurat
objects, the name of the assay to be converted. IfNULL
, the default assay will be used (SeuratObject::DefaultAssay()
). This is ignored for other objects.- output_class
The
AnnData
class to convert to. Must be one of"HDF5AnnData"
or"InMemoryAnnData"
.- ...
Additional arguments passed to the generator function for
output_class
Value
An AnnData
object of the class requested by output_class
containing the data specified in the mapping arguments.
Details of mapping arguments
All mapping arguments except for x_mapping
expect a named character
vector where names are the keys of the slot in the AnnData
object and
values are the names of items in the corresponding slot of x
. If NULL
,
the conversion function will guess which items to copy as described in the
conversion tables for each object type. In most cases, the default is to
copy all items using the same names except where the correspondence between
objects is unclear. 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 tables for each object typec(adata_item = "x_item")
will copyx_item
from the slot inx
toadata_item
in the corresponding slot of newAnnData
objectc()
will avoid copying anything to the slotc("x_item")
is equivalent toc(x_item = "x_item")
Converting from a SingleCellExperiment
object
This table describes how slots in a
SingleCellExperiment::SingleCellExperiment
object to the new AnnData
object.
From SingleCellExperiment | To AnnData | Example mapping argument | Default if NULL |
assays(x) | adata$X | x_mapping = "counts" | Nothing is copied to X |
assays(x) | adata$layers | layers_mapping = c(counts = "counts") | All items are copied by name |
colData(x) | adata$obs | obs_mapping = c(n_counts = "n_counts", cell_type = "CellType") | All columns are copied by name |
rowData(x) | adata$var | var_mapping = c(n_cells = "n_cells", pct_zero = "PctZero") | All columns are copied by name |
reducedDims(x) | adata$obsm | obsm_mapping = c(X_pca = "pca") | All items are copied by name |
featureLoadings(reducedDims(x)) | adata$varm | varm_mapping = c(PCs = "pca") | Feature loadings from all SingleCellExperiment::LinearEmbeddingMatrix objects in reducedDims(x) |
colPairs(x) | adata$obsp | obsp_mapping = c(connectivities = "RNA_nn") | All items are copied by name |
rowPairs(x) | adata$varp | varp_mapping = c(similarities = "gene_overlaps") | All items are copied by name |
metadata(x) | adata$uns | uns_mapping = c(metadata = "project_metadata") | All items are copied by name |
Converting from a Seurat
object
Only one assay can be converted from a SeuratObject::Seurat
object to
an AnnData
object at a time. This can be controlled using the
assay_name
argument. By default, the current default assay will be used.
This table describes how slots in a SeuratObject::Seurat
object to the
new AnnData
object.
From Seurat | To AnnData | Example mapping argument | Default if NULL |
Layers(x) | adata$X | x_mapping = "counts" | Nothing is copied to X |
Layers(x) | adata$layers | layers_mapping = c(counts = "counts") | All items are copied by name |
x[[]] | adata$obs | obs_mapping = c(n_counts = "n_counts", cell_type = "CellType") | All columns are copied by name |
x[[assay_name]][[]] | adata$var | var_mapping = c(n_cells = "n_cells", pct_zero = "PctZero") | All columns are copied by name |
Embeddings(x) | adata$obsm | obsm_mapping = c(X_pca = "pca") | All embeddings matching assay_name are copied by name |
Loadings(x) | adata$varm | varm_mapping = c(PCs = "pca") | All valid loadings are copied by name |
Graphs(x) | adata$obsp | obsp_mapping = c(connectivities = "RNA_nn") | All graphs matching assay_name are copied by name |
Misc(x) | adata$varp | varp_mapping = c(similarities = "gene_overlaps") | No data is copied to varp |
Misc(x) | adata$uns | uns_mapping = c(metadata = "project_metadata") | All items are copied by name |
See also
Other AnnData creators:
AnnData()
,
read_h5ad()
Other object converters:
as_HDF5AnnData
,
as_InMemoryAnnData
,
as_Seurat
,
as_SingleCellExperiment
Examples
# Convert a Seurat object to an AnnData object
library(Seurat)
#> Loading required package: SeuratObject
#> Loading required package: sp
#>
#> Attaching package: ‘SeuratObject’
#> The following objects are masked from ‘package:base’:
#>
#> intersect, t
counts <- matrix(rbinom(20000, 1000, .001), nrow = 100)
obj <- CreateSeuratObject(counts = counts)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
obj <- NormalizeData(obj)
#> Normalizing layer: counts
obj <- FindVariableFeatures(obj)
#> Finding variable features for layer counts
obj <- ScaleData(obj)
#> Centering and scaling data matrix
obj <- RunPCA(obj, npcs = 10L)
#> PC_ 1
#> Positive: Feature72, Feature61, Feature35, Feature78, Feature52, Feature50, Feature66, Feature8, Feature14, Feature32
#> Feature70, Feature1, Feature56, Feature87, Feature19, Feature97, Feature7, Feature17, Feature86, Feature48
#> Feature81, Feature88, Feature43, Feature67, Feature36, Feature3, Feature26, Feature54, Feature40, Feature33
#> Negative: Feature9, Feature29, Feature4, Feature39, Feature15, Feature27, Feature34, Feature68, Feature62, Feature18
#> Feature22, Feature46, Feature10, Feature74, Feature49, Feature37, Feature12, Feature23, Feature95, Feature100
#> Feature64, Feature30, Feature90, Feature55, Feature47, Feature96, Feature5, Feature31, Feature60, Feature92
#> PC_ 2
#> Positive: Feature41, Feature89, Feature45, Feature98, Feature33, Feature44, Feature68, Feature91, Feature18, Feature59
#> Feature61, Feature86, Feature32, Feature4, Feature51, Feature42, Feature54, Feature67, Feature56, Feature21
#> Feature53, Feature70, Feature8, Feature31, Feature22, Feature14, Feature12, Feature76, Feature62, Feature58
#> Negative: Feature97, Feature13, Feature48, Feature69, Feature49, Feature82, Feature7, Feature5, Feature79, Feature93
#> Feature94, Feature84, Feature23, Feature78, Feature27, Feature40, Feature26, Feature50, Feature81, Feature46
#> Feature29, Feature10, Feature71, Feature72, Feature52, Feature88, Feature87, Feature9, Feature95, Feature16
#> PC_ 3
#> Positive: Feature3, Feature93, Feature2, Feature5, Feature11, Feature79, Feature66, Feature74, Feature50, Feature36
#> Feature78, Feature12, Feature45, Feature98, Feature51, Feature16, Feature83, Feature28, Feature27, Feature9
#> Feature8, Feature95, Feature33, Feature63, Feature21, Feature87, Feature40, Feature53, Feature85, Feature57
#> Negative: Feature58, Feature90, Feature46, Feature81, Feature44, Feature56, Feature69, Feature86, Feature37, Feature97
#> Feature92, Feature26, Feature88, Feature30, Feature96, Feature42, Feature14, Feature25, Feature71, Feature34
#> Feature70, Feature82, Feature23, Feature35, Feature13, Feature19, Feature80, Feature4, Feature29, Feature67
#> PC_ 4
#> Positive: Feature96, Feature11, Feature78, Feature59, Feature31, Feature57, Feature19, Feature80, Feature41, Feature4
#> Feature55, Feature44, Feature60, Feature90, Feature32, Feature50, Feature1, Feature14, Feature9, Feature77
#> Feature85, Feature28, Feature82, Feature99, Feature25, Feature8, Feature27, Feature62, Feature64, Feature29
#> Negative: Feature20, Feature18, Feature81, Feature100, Feature93, Feature10, Feature15, Feature74, Feature83, Feature33
#> Feature58, Feature6, Feature72, Feature89, Feature7, Feature35, Feature53, Feature37, Feature42, Feature43
#> Feature76, Feature46, Feature98, Feature73, Feature52, Feature88, Feature63, Feature49, Feature17, Feature22
#> PC_ 5
#> Positive: Feature77, Feature40, Feature9, Feature21, Feature38, Feature65, Feature80, Feature22, Feature72, Feature18
#> Feature50, Feature71, Feature1, Feature83, Feature82, Feature53, Feature67, Feature63, Feature90, Feature13
#> Feature4, Feature61, Feature56, Feature84, Feature43, Feature54, Feature19, Feature94, Feature37, Feature33
#> Negative: Feature30, Feature51, Feature86, Feature69, Feature45, Feature14, Feature3, Feature26, Feature57, Feature23
#> Feature99, Feature60, Feature66, Feature36, Feature15, Feature68, Feature85, Feature29, Feature78, Feature24
#> Feature16, Feature70, Feature42, Feature41, Feature48, Feature79, Feature10, Feature2, Feature49, Feature97
obj <- FindNeighbors(obj)
#> Computing nearest neighbor graph
#> Computing SNN
obj <- RunUMAP(obj, dims = 1:10)
#> Warning: The default method for RunUMAP has changed from calling Python UMAP via reticulate to the R-native UWOT using the cosine metric
#> To use Python UMAP via reticulate, set umap.method to 'umap-learn' and metric to 'correlation'
#> This message will be shown once per session
#> 13:58:23 UMAP embedding parameters a = 0.9922 b = 1.112
#> 13:58:23 Read 200 rows and found 10 numeric columns
#> 13:58:23 Using Annoy for neighbor search, n_neighbors = 30
#> 13:58:23 Building Annoy index with metric = cosine, n_trees = 50
#> 0% 10 20 30 40 50 60 70 80 90 100%
#> [----|----|----|----|----|----|----|----|----|----|
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> |
#> 13:58:23 Writing NN index file to temp file /tmp/RtmpnbQ4Iy/file1e642b165dde
#> 13:58:23 Searching Annoy index using 1 thread, search_k = 3000
#> 13:58:23 Annoy recall = 100%
#> 13:58:24 Commencing smooth kNN distance calibration using 1 thread
#> with target n_neighbors = 30
#> 13:58:25 Initializing from normalized Laplacian + noise (using RSpectra)
#> 13:58:25 Commencing optimization for 500 epochs, with 6160 positive edges
#> 13:58:25 Using rng type: pcg
#> 13:58:26 Optimization finished
as_AnnData(obj)
#> AnnData object with n_obs × n_vars = 200 × 100
#> obs: 'orig.ident', 'nCount_RNA', 'nFeature_RNA'
#> var: 'vf_vst_counts_mean', 'vf_vst_counts_variance', 'vf_vst_counts_variance.expected', 'vf_vst_counts_variance.standardized', 'vf_vst_counts_variable', 'vf_vst_counts_rank', 'var.features', 'var.features.rank'
#> obsm: 'pca', 'umap'
#> varm: 'pca'
#> layers: 'counts', 'data', 'scale.data'
#> obsp: 'nn', 'snn'
# Convert a SingleCellExperiment object to an AnnData object
library(SingleCellExperiment)
#> Loading required package: SummarizedExperiment
#> Loading required package: MatrixGenerics
#> Loading required package: matrixStats
#>
#> Attaching package: ‘MatrixGenerics’
#> The following objects are masked from ‘package:matrixStats’:
#>
#> colAlls, colAnyNAs, colAnys, colAvgsPerRowSet, colCollapse,
#> colCounts, colCummaxs, colCummins, colCumprods, colCumsums,
#> colDiffs, colIQRDiffs, colIQRs, colLogSumExps, colMadDiffs,
#> colMads, colMaxs, colMeans2, colMedians, colMins, colOrderStats,
#> colProds, colQuantiles, colRanges, colRanks, colSdDiffs, colSds,
#> colSums2, colTabulates, colVarDiffs, colVars, colWeightedMads,
#> colWeightedMeans, colWeightedMedians, colWeightedSds,
#> colWeightedVars, rowAlls, rowAnyNAs, rowAnys, rowAvgsPerColSet,
#> rowCollapse, rowCounts, rowCummaxs, rowCummins, rowCumprods,
#> rowCumsums, rowDiffs, rowIQRDiffs, rowIQRs, rowLogSumExps,
#> rowMadDiffs, rowMads, rowMaxs, rowMeans2, rowMedians, rowMins,
#> rowOrderStats, rowProds, rowQuantiles, rowRanges, rowRanks,
#> rowSdDiffs, rowSds, rowSums2, rowTabulates, rowVarDiffs, rowVars,
#> rowWeightedMads, rowWeightedMeans, rowWeightedMedians,
#> rowWeightedSds, rowWeightedVars
#> Loading required package: GenomicRanges
#> Loading required package: stats4
#> Loading required package: BiocGenerics
#> Loading required package: generics
#>
#> Attaching package: ‘generics’
#> The following objects are masked from ‘package:base’:
#>
#> as.difftime, as.factor, as.ordered, intersect, is.element, setdiff,
#> setequal, union
#>
#> Attaching package: ‘BiocGenerics’
#> The following objects are masked from ‘package:stats’:
#>
#> IQR, mad, sd, var, xtabs
#> The following objects are masked from ‘package:base’:
#>
#> Filter, Find, Map, Position, Reduce, anyDuplicated, aperm, append,
#> as.data.frame, basename, cbind, colnames, dirname, do.call,
#> duplicated, eval, evalq, get, grep, grepl, is.unsorted, lapply,
#> mapply, match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
#> rank, rbind, rownames, sapply, saveRDS, table, tapply, unique,
#> unsplit, which.max, which.min
#> Loading required package: S4Vectors
#>
#> Attaching package: ‘S4Vectors’
#> The following object is masked from ‘package:utils’:
#>
#> findMatches
#> The following objects are masked from ‘package:base’:
#>
#> I, expand.grid, unname
#> Loading required package: IRanges
#>
#> Attaching package: ‘IRanges’
#> The following object is masked from ‘package:sp’:
#>
#> %over%
#> Loading required package: GenomeInfoDb
#> Loading required package: Biobase
#> Welcome to Bioconductor
#>
#> Vignettes contain introductory material; view with
#> 'browseVignettes()'. To cite Bioconductor, see
#> 'citation("Biobase")', and for packages 'citation("pkgname")'.
#>
#> Attaching package: ‘Biobase’
#> The following object is masked from ‘package:MatrixGenerics’:
#>
#> rowMedians
#> The following objects are masked from ‘package:matrixStats’:
#>
#> anyMissing, rowMedians
#>
#> Attaching package: ‘SummarizedExperiment’
#> The following object is masked from ‘package:Seurat’:
#>
#> Assays
#> The following object is masked from ‘package:SeuratObject’:
#>
#> Assays
sce <- SingleCellExperiment(
assays = list(counts = matrix(1:5, 5L, 3L)),
colData = DataFrame(cell = 1:3, row.names = paste0("Cell", 1:3)),
rowData = DataFrame(gene = 1:5, row.names = paste0("Gene", 1:5))
)
as_AnnData(sce)
#> AnnData object with n_obs × n_vars = 3 × 5
#> obs: 'cell'
#> var: 'gene'
#> layers: 'counts'