Skip to contents

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. If NULL, no data will be copied to the X slot.

layers_mapping

A named list mapping layer names to the names of the layers in the Seurat object. Each item in the list must be a character vector of length 1. See section "$layers mapping" for more details.

obs_mapping

A named list mapping obs names to the names of the object-level (cell level) metadata in the Seurat object. Each item in the list must be a character vector of length 1. See section "$obs mapping" for more details.

var_mapping

A named list mapping var names to the names of the feature-level metadata in the Seurat object. Each item in the list must be a character vector of length 1. See section "$var mapping" for more details.

obsm_mapping

A named list mapping reductions to the names of the reductions in the Seurat object. Each item in the list must be a vector of length 2. See section "$obsm mapping" for more details.

varm_mapping

A named list mapping PCA loadings to the names of the PCA loadings in the Seurat object. Each item in the list must be a character vector of length 1. See section "$varm mapping" for more details.

obsp_mapping

A named list mapping graph names to the names of the graphs in the Seurat object. Each item in the list must be a character vector of length 1. See section "$obsp mapping" for more details.

varp_mapping

A named list mapping miscellaneous data to the names of the data in the Seurat object. Each item in the list must be a named list with one or two elements. See section "$varp mapping" for more details.

uns_mapping

A named list mapping miscellaneous data to the names of the data in the Seurat object. Each item in the list must be a named list with one or two elements. See section "$uns mapping" for more details.

...

Additional arguments passed to the generator function.

Value

An AnnData object

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 = list(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 be NULL (empty). If you want to copy data to the X 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 = list(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 = list(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 list to map Seurat reductions to AnnData $obsm.

Each item in the list must be a vector of length 2, where the name corresponds to the name of the resulting $obsm slot, and the values correspond to the the location of the data in the Seurat object.

Example: obsm_mapping = list(pca = c("reductions", "pca"), umap = c("reductions", "umap")).

If NULL, the internal function .from_Seurat_guess_obsms will be used to guess the obsm mapping as follows:

  • All Seurat reductions are prefixed with X_ and copied to AnnData $obsm.

$varm mapping

A named list to map Seurat reduction loadings to AnnData $varm.

Each item in the list must be a character vector of length 2, where the name corresponds to the name of the resulting $varm slot, and the value corresponds to the location of the data in the Seurat object.

Example: varm_mapping = list(PCs = c("reductions", "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 list to map Seurat graphs to AnnData $obsp.

Each name in the list corresponds to the name of the resulting $obsp slot. Each value must be a character vector of length 2, where the first element of this vector must be graphs or misc, and the second element is the name of the data in the corresponding graphs or misc slot in the Seurat object.

Example: obsp_mapping = list(connectivities = c("graphs", "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 list to map Seurat miscellaneous data to AnnData $varp. The name of each item corresponds to the resulting $varp slot, while the value of each item must be a fector which corresponds to the location of the data in the Seurat object.

Example: varp_mapping = list(foo = c("misc", "foo")).

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 list to map Seurat miscellaneous data to AnnData uns. Each item in the list must be a character of length 2. The first element must be "misc". The second element is the name of the data in the corresponding slot.

Example: uns_mapping = list(foo = c("misc", "foo")).

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.

Examples

library(Seurat)
#> Loading required package: SeuratObject
#> Loading required package: sp
#> ‘SeuratObject’ was built under R 4.4.0 but the current version is
#> 4.4.3; it is recomended that you reinstall ‘SeuratObject’ as the ABI
#> for R may have changed
#> ‘SeuratObject’ was built with package ‘Matrix’ 1.7.1 but the current
#> version is 1.7.2; it is recomended that you reinstall ‘SeuratObject’ as
#> the ABI for ‘Matrix’ may have changed
#> 
#> 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:49:19 UMAP embedding parameters a = 0.9922 b = 1.112
#> 09:49:19 Read 200 rows and found 10 numeric columns
#> 09:49:19 Using Annoy for neighbor search, n_neighbors = 30
#> 09:49:19 Building Annoy index with metric = cosine, n_trees = 50
#> 0%   10   20   30   40   50   60   70   80   90   100%
#> [----|----|----|----|----|----|----|----|----|----|
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> *
#> |
#> 09:49:19 Writing NN index file to temp file /tmp/RtmpZyxz4i/file1ce9342e19ad
#> 09:49:19 Searching Annoy index using 1 thread, search_k = 3000
#> 09:49:19 Annoy recall = 100%
#> 09:49:20 Commencing smooth kNN distance calibration using 1 thread
#>  with target n_neighbors = 30
#> 09:49:20 Initializing from normalized Laplacian + noise (using RSpectra)
#> 09:49:20 Commencing optimization for 500 epochs, with 6160 positive edges
#> 09:49:20 Using rng type: pcg
#> 09:49:21 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: 'X_pca', 'X_umap'
#>     varm: 'pca'
#>     layers: 'counts', 'data', 'scale.data'
#>     obsp: 'connectivities', 'snn'