from_Seurat()
converts a Seurat object to an AnnData object.
Only one assay can be converted at a time. Arguments are used to configure the conversion.
If NULL
, the functions .from_Seurat_guess_*
will be used to guess the mapping.
Usage
from_Seurat(
seurat_obj,
output_class = c("InMemoryAnnData", "HDF5AnnData"),
assay_name = NULL,
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,
...
)
Arguments
- seurat_obj
A Seurat object to be converted.
- output_class
Name of the AnnData class. Must be one of
"HDF5AnnData"
or"InMemoryAnnData"
.- assay_name
The name of the assay to be converted. If
NULL
, the default assay will be used (SeuratObject::DefaultAssay()
).- x_mapping
A mapping of a Seurat layer to the AnnData
X
slot. IfNULL
, no data will be copied to theX
slot.- layers_mapping
A named vector mapping layer names in AnnData to the names of the layers in the Seurat object. Each name corresponds to the layer name in AnnData, and each value to the layer name in Seurat. See section "
$layers
mapping" for more details.- obs_mapping
A named vector or list mapping obs names in AnnData to the names of the object-level (cell level) metadata in the Seurat object. Each name corresponds to a column name in AnnData's obs, and each value to a column name in Seurat's cell metadata. See section "
$obs
mapping" for more details.- var_mapping
A named vector or list mapping var names in AnnData to the names of the feature-level metadata in the Seurat object. Each name corresponds to a column name in AnnData's var, and each value to a column name in Seurat's feature metadata. See section "
$var
mapping" for more details.- obsm_mapping
A named vector mapping obsm keys in AnnData to reduction names in the Seurat object. Each name corresponds to a key in AnnData's obsm, and each value to the name of a reduction in Seurat. Example:
obsm_mapping = c(X_pca = "pca", X_umap = "umap")
.- varm_mapping
A named vector mapping varm keys in AnnData to reduction names in the Seurat object. Each name corresponds to a key in AnnData's varm, and each value to the name of a reduction in Seurat. Example:
varm_mapping = c(PCs = "pca")
.- obsp_mapping
A named vector mapping obsp keys in AnnData to graph names in the Seurat object. Each name corresponds to a key in AnnData's obsp, and each value to the name of a graph in Seurat. Example:
obsp_mapping = c(connectivities = "RNA_nn")
.- varp_mapping
A named vector mapping varp keys in AnnData to misc data names in the Seurat object. Each name corresponds to a key in AnnData's varp, and each value to the name of misc data in Seurat. Example:
varp_mapping = c(foo = "foo_data")
.- uns_mapping
A named vector mapping uns keys in AnnData to misc data names in the Seurat object. Each name corresponds to a key in AnnData's uns, and each value to the name of misc data in Seurat. Example:
uns_mapping = c(metadata = "project_meta")
.- ...
Additional arguments passed to the generator function.
Details
For more information on the functionality of an AnnData object, see anndataR-package.
$X
mapping
A mapping of a Seurat layer to the AnnData X
slot. Its value must be NULL
or a character vector of the Seurat
layer name to copy. If NULL
, no data will be copied to the X
slot.
$layers
mapping
A named list to map AnnData layers to Seurat layers. Each item in the list must be a character vector of length 1.
The $X
key maps to the X
slot.
Example: layers_mapping = c(counts = "counts", foo = "bar")
.
If NULL
, the internal function .from_Seurat_guess_layers
will be used to guess the layer mapping as follows:
All Seurat layers are copied to AnnData
layers
by name.This means that the AnnData
X
slot will beNULL
(empty). If you want to copy data to theX
slot, you must define the layer mapping explicitly.
$obs
mapping
A named list or vector to map Seurat object-level metadata to AnnData $obs
. The values of this list or vector
correspond to the names of the metadata in the Seurat object, and the names correspond to the names of the
metadata in the resulting $obs
slot.
Example: obs_mapping = c(cellType = "cell_type")
.
If NULL
, the internal function .from_Seurat_guess_obs
will be used to guess the obs mapping as follows:
All Seurat object-level metadata is copied to AnnData
$obs
by name.
$var
mapping
A named list or vector to map Seurat feature-level metadata to AnnData $var
. The values of this list or
vector correspond to the names of the metadata of the assay in the Seurat object, and the
names correspond to the names of the metadata in the resulting $var
slot.
Example: var_mapping = c(geneInfo = "gene_info")
.
If NULL
, the internal function .from_Seurat_guess_vars
will be used to guess the var mapping as follows:
All Seurat feature-level metadata is copied to AnnData
$var
by name.
$obsm
mapping
A named vector mapping obsm keys in AnnData to reduction names in the Seurat object.
Each name corresponds to a key in AnnData's obsm, and each value to the name of a reduction in Seurat.
Example: obsm_mapping = c(pca = "pca", umap = "umap")
.
If NULL
, the internal function .from_Seurat_guess_obsms
will be used to guess the obsm mapping as follows:
All Seurat reductions are copied to AnnData
$obsm
.
$varm
mapping
A named vector mapping varm keys in AnnData to reduction names in the Seurat object.
Each name corresponds to a key in AnnData's varm, and each value to the name of a reduction in Seurat.
Example: varm_mapping = c(PCs = "pca")
.
If NULL
, the internal function .from_Seurat_guess_varms
will be used to guess the varm mapping as follows:
The name of the PCA loadings is copied by name.
$obsp
mapping
A named vector mapping obsp keys in AnnData to graph names in the Seurat object.
Each name corresponds to a key in AnnData's obsp, and each value to the name of a graph in Seurat.
Example: obsp_mapping = c(connectivities = "RNA_nn")
.
If NULL
, the internal function .from_Seurat_guess_obsps
will be used to guess the obsp mapping as follows:
All Seurat graphs are copied to
$obsp
by name.
$varp
mapping
A named vector mapping varp keys in AnnData to misc data names in the Seurat object.
Each name corresponds to a key in AnnData's varp, and each value to the name of misc data in Seurat.
Example: varp_mapping = c(foo = "foo_data")
.
If NULL
, the internal function .from_Seurat_guess_varps
will be used to guess the varp mapping as follows:
No data is mapped to
$varp
.
$uns
mapping
A named vector mapping uns keys in AnnData to misc data names in the Seurat object.
Each name corresponds to a key in AnnData's uns, and each value to the name of misc data in Seurat.
Example: uns_mapping = c(metadata = "project_meta")
.
If NULL
, the internal function .from_Seurat_guess_uns
will be used to guess the uns mapping as follows:
All Seurat miscellaneous data is copied to
uns
by name.
Mapping details
If an unnamed vector is provided to a mapping argument the values will be used as names
Examples
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
#> 09:33:47 UMAP embedding parameters a = 0.9922 b = 1.112
#> 09:33:47 Read 200 rows and found 10 numeric columns
#> 09:33:47 Using Annoy for neighbor search, n_neighbors = 30
#> 09:33:47 Building Annoy index with metric = cosine, n_trees = 50
#> 0% 10 20 30 40 50 60 70 80 90 100%
#> [----|----|----|----|----|----|----|----|----|----|
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> |
#> 09:33:47 Writing NN index file to temp file /tmp/Rtmp8vIMpj/file1d436609c2d6
#> 09:33:47 Searching Annoy index using 1 thread, search_k = 3000
#> 09:33:47 Annoy recall = 100%
#> 09:33:47 Commencing smooth kNN distance calibration using 1 thread
#> with target n_neighbors = 30
#> 09:33:48 Initializing from normalized Laplacian + noise (using RSpectra)
#> 09:33:48 Commencing optimization for 500 epochs, with 6160 positive edges
#> 09:33:48 Using rng type: pcg
#> 09:33:48 Optimization finished
from_Seurat(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'