Detailed look into the mapping between AnnData and Seurat objects

{anndataR} provides a way to convert between AnnData and Seurat objects. This vignette will provide a detailed look into the mapping between the two objects. The conversion can be done with or without extra user input as to which fields and slots of the respective objects should be converted and put into the other object. Please take into account that lossless conversion is not always possible between AnnData and Seurat objects due to differences in the object structure. Have a look at our known issues section for more information and please inspect the object before and after conversion to make sure that the conversion was successful.

suppressMessages({
  library(anndataR)
  library(Seurat)
})

We will first generate a sample dataset to work with:

ad <- generate_dataset(
  n_obs = 10L,
  n_var = 20L,
  x_type = "numeric_matrix",
  layer_types = c("integer_matrix", "numeric_rsparse"),
  obs_types = c("integer", "numeric", "factor"),
  var_types = c("character", "numeric", "logical"),
  obsm_types = c("numeric_matrix", "numeric_csparse"),
  varm_types = c("numeric_matrix", "numeric_csparse"),
  obsp_types = c("numeric_matrix", "numeric_csparse"),
  varp_types = c("integer_matrix", "numeric_matrix"),
  uns_types = c("vec_integer", "vec_character", "df_integer"),
  format = "AnnData"
)

# add PCA reduction
ad$obsm[["X_pca"]] <- matrix(1:50, 10, 5)
ad$varm[["PCs"]] <- matrix(1:100, 20, 5)

ad$obsm[["X_umap"]] <- matrix(1:20, 10, 2)

ad$obsp[["connectivities"]] <- ad$obsp[["numeric_matrix"]]
ad$obsp[["connectivities_sparse"]] <- ad$obsp[["numeric_csparse"]]

Convert AnnData objects to Seurat objects

By default, anndataR will try to guess a reasonable mapping. If you do not want this to happen, and you want nothing of that specific slot to be converted, you can pass an empty list.

We will first detail how anndataR guesses a reasonable mapping. In the section after that, we will detail how you can provide your own mappings.

Implicit mapping

Here, we showcase what happens if you do not provide any mapping information for the conversion.

seurat_obj <- to_Seurat(ad)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: No columnames present in cell embeddings, setting to 'umap_1:2'
seurat_obj
#> An object of class Seurat 
#> 20 features across 10 samples within 1 assay 
#> Active assay: RNA (20 features, 0 variable features)
#>  3 layers present: counts, integer_matrix, numeric_rsparse
#>  2 dimensional reductions calculated: pca, umap

In the following subsections, we detail how each of these implicit conversions work. If we explicitly don’t want to convert a certain slot, we can pass an empty list.

assay_name

An anndata object can only have one assay, while a Seurat object can have multiple assays. If you want to work with multiple assays, you can use (https://mudata.readthedocs.io/en/latest/)[MuData] objects.

The assay_name parameter will only determine what the name of the assay in the Seurat object will be. By default, this will be “RNA”.

to_Seurat(
  adata = ad,
  assay_name = "Other_assay",
  object_metadata_mapping = list(),
  layers_mapping = list(counts = NULL),
  assay_metadata_mapping = list(),
  reduction_mapping = list(),
  graph_mapping = list(),
  misc_mapping = list()
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: [[<- defined for objects of type "S4" only for subclasses of
#> environment
#> An object of class Seurat 
#> 20 features across 10 samples within 1 assay 
#> Active assay: Other_assay (20 features, 0 variable features)
#>  1 layer present: counts

object_metadata_mapping

Seurat allows for metadata to be stored on the object level, corresponding to cell (or observation) level metadata. This corresponds with the obs slot in the AnnData object.

If no information is provided, anndataR will try to convert the whole obs slot. You can see that Seurat adds some columns by default (orig.ident, nCount_RNA, nFeature_RNA).

seurat_object <- to_Seurat(
  adata = ad,
  assay_name = "RNA",
  layers_mapping = list(counts = NULL),
  assay_metadata_mapping = list(),
  reduction_mapping = list(),
  graph_mapping = list(),
  misc_mapping = list()
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
seurat_object[[]]
#>           orig.ident nCount_RNA nFeature_RNA integer numeric factor
#> cell1  SeuratProject        200           20       0     0.5 Value1
#> cell2  SeuratProject        600           20       1     1.5 Value2
#> cell3  SeuratProject       1000           20       2     2.5 Value1
#> cell4  SeuratProject       1400           20       3     3.5 Value2
#> cell5  SeuratProject       1800           20       4     4.5 Value1
#> cell6  SeuratProject       2200           20       5     5.5 Value2
#> cell7  SeuratProject       2600           20       6     6.5 Value1
#> cell8  SeuratProject       3000           20       7     7.5 Value2
#> cell9  SeuratProject       3400           20       8     8.5 Value1
#> cell10 SeuratProject       3800           20       9     9.5 Value2

layers_mapping

Seurat allows for multiple layers to be stored in the object. This corresponds with the layers slot in the AnnData object. If no mapping is provided, we will try to guess which layer the X anndata slot corresponds to by checking whether another layer called counts or data exists in the AnnData object. The other layers of the AnnData object will be copied by name.

seurat_object <- to_Seurat(
  adata = ad,
  assay_name = "RNA",
  object_metadata_mapping = list(),
  assay_metadata_mapping = list(),
  reduction_mapping = list(),
  graph_mapping = list(),
  misc_mapping = list()
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: [[<- defined for objects of type "S4" only for subclasses of
#> environment
Layers(seurat_object[["RNA"]])
#> [1] "counts"          "integer_matrix"  "numeric_rsparse"

assay_metadata_mapping

Besides metadata on the object level, Seurat also stores metadata on the assay level. This corresponds to gene (or variable or feature) level metadata. This corresponds with the var slot in the AnnData object.

If no information is provided, anndataR will try to convert the whole var slot.

seurat_object <- to_Seurat(
  adata = ad,
  assay_name = "RNA",
  object_metadata_mapping = list(),
  layers_mapping = list(counts = NULL),
  reduction_mapping = list(),
  graph_mapping = list(),
  misc_mapping = list()
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: [[<- defined for objects of type "S4" only for subclasses of
#> environment
seurat_object[["RNA"]][[]]
#>        character numeric logical
#> gene1    value_0     0.5   FALSE
#> gene2    value_1     1.5    TRUE
#> gene3    value_2     2.5    TRUE
#> gene4    value_3     3.5   FALSE
#> gene5    value_4     4.5   FALSE
#> gene6    value_5     5.5   FALSE
#> gene7    value_6     6.5    TRUE
#> gene8    value_7     7.5   FALSE
#> gene9    value_8     8.5    TRUE
#> gene10   value_9     9.5    TRUE
#> gene11  value_10    10.5   FALSE
#> gene12  value_11    11.5    TRUE
#> gene13  value_12    12.5    TRUE
#> gene14  value_13    13.5   FALSE
#> gene15  value_14    14.5   FALSE
#> gene16  value_15    15.5   FALSE
#> gene17  value_16    16.5    TRUE
#> gene18  value_17    17.5   FALSE
#> gene19  value_18    18.5   FALSE
#> gene20  value_19    19.5    TRUE

reduction_mapping

Reductions are a bit more complicated. Some reductions have a standard way of being saved in an AnnData objecct, some don’t. We assume that a PCA will always be stored as X_pca in the obsm slot, with the loadings as PCs in the varm slot. For other reductions, we will assume that they will start with X_ in the obsm slot, with the characters after the X_ being the name of the reduction. Elements in the obsm slot not starting with X_ will not be converted.

seurat_object <- to_Seurat(
  adata = ad,
  assay_name = "RNA",
  object_metadata_mapping = list(),
  layers_mapping = list(counts = NULL),
  assay_metadata_mapping = list(),
  graph_mapping = list(),
  misc_mapping = list()
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: [[<- defined for objects of type "S4" only for subclasses of
#> environment
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: No columnames present in cell embeddings, setting to 'umap_1:2'
Reductions(seurat_object)
#> [1] "pca"  "umap"

graph_mapping

Graphs of cells are stored in the obsp slot in the AnnData object. If a graph is stored with the name connectivities, we will store it as nn in the Seurat object. Graphs starting with connectivities_{name} will be stored as {name} in the Seurat object. Other graphs will not be converted.

seurat_object <- to_Seurat(
  adata = ad,
  assay_name = "RNA",
  object_metadata_mapping = list(),
  layers_mapping = list(counts = NULL),
  assay_metadata_mapping = list(),
  reduction_mapping = list(),
  misc_mapping = list()
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: [[<- defined for objects of type "S4" only for subclasses of
#> environment

Graphs(seurat_object)
#> [1] "RNA_nn"     "RNA_sparse"

misc_mapping

The misc slot in the Seurat object is used to store any other information that does not fit in the other slots. By default, it will copy the uns slot from the AnnData object.

seurat_object <- to_Seurat(
  adata = ad,
  assay_name = "RNA",
  object_metadata_mapping = list(),
  layers_mapping = list(counts = NULL),
  assay_metadata_mapping = list(),
  reduction_mapping = list(),
  graph_mapping = list()
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: [[<- defined for objects of type "S4" only for subclasses of
#> environment
Misc(seurat_object)
#> $vec_integer
#>  [1] 0 1 2 3 4 5 6 7 8 9
#> 
#> $vec_character
#>  [1] "value_0" "value_1" "value_2" "value_3" "value_4" "value_5" "value_6"
#>  [8] "value_7" "value_8" "value_9"
#> 
#> $df_integer
#> $df_integer$integer
#>  [1] 0 1 2 3 4 5 6 7 8 9

Explicit mapping

assay_name

As stated in the section on implicit mapping, an anndata object can only have one assay, while a Seurat object can have multiple assays. The name you pass here will only determine what the assay is called in the Seurat object. By default, it is “RNA”.

seurat_obj <- to_Seurat(
  adata = ad,
  assay_name = "Other_assay"
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: No columnames present in cell embeddings, setting to 'umap_1:2'
Assays(seurat_obj)
#> [1] "Other_assay"

layers_mapping

We can explicitly provide a layers mapping. The names of the named list refer to what the layer will be called in the Seurat object. The values refer to what the layers are called in the anndata object.

You must provide at least a layer called counts or data in the mapping.

seurat_obj <- to_Seurat(
  adata = ad,
  layers_mapping = list(counts = "integer_matrix", other_layer2 = "numeric_rsparse")
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: No columnames present in cell embeddings, setting to 'umap_1:2'
Layers(seurat_obj, assay = "RNA")
#> [1] "counts"       "other_layer2"

object_metadata_mapping

You can provide a mapping for object-level metadata in Seurat, which corresponds to cell-level metadata, and thus with the obs slot in the AnnData object.

The names of the named list refer to what the metadata will be called in the Seurat object. The values refer to what the columns are called in the obs slot of the anndata object.

seurat_obj <- to_Seurat(
  adata = ad,
  object_metadata_mapping = list(metadata1 = "integer", metadata2 = "numeric")
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: No columnames present in cell embeddings, setting to 'umap_1:2'
seurat_obj[[]]
#>           orig.ident nCount_RNA nFeature_RNA metadata1 metadata2
#> cell1  SeuratProject        200           20         0       0.5
#> cell2  SeuratProject        600           20         1       1.5
#> cell3  SeuratProject       1000           20         2       2.5
#> cell4  SeuratProject       1400           20         3       3.5
#> cell5  SeuratProject       1800           20         4       4.5
#> cell6  SeuratProject       2200           20         5       5.5
#> cell7  SeuratProject       2600           20         6       6.5
#> cell8  SeuratProject       3000           20         7       7.5
#> cell9  SeuratProject       3400           20         8       8.5
#> cell10 SeuratProject       3800           20         9       9.5

assay_metadata_mapping

You can provide a mapping for assay-level metadata, which corresponds to gene-level metadata, and with the var slot in the AnnData object.

The names of the named list refer to what the metadata will be called in the Seurat object. The values refer to what the metadata is called in the var slot of the anndata object.

seurat_obj <- to_Seurat(
  adata = ad,
  assay_metadata_mapping = list(metadata1 = "numeric", metadata2 = "logical")
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: No columnames present in cell embeddings, setting to 'umap_1:2'
seurat_obj[["RNA"]][[]]
#>        metadata1 metadata2
#> gene1        0.5     FALSE
#> gene2        1.5      TRUE
#> gene3        2.5      TRUE
#> gene4        3.5     FALSE
#> gene5        4.5     FALSE
#> gene6        5.5     FALSE
#> gene7        6.5      TRUE
#> gene8        7.5     FALSE
#> gene9        8.5      TRUE
#> gene10       9.5      TRUE
#> gene11      10.5     FALSE
#> gene12      11.5      TRUE
#> gene13      12.5      TRUE
#> gene14      13.5     FALSE
#> gene15      14.5     FALSE
#> gene16      15.5     FALSE
#> gene17      16.5      TRUE
#> gene18      17.5     FALSE
#> gene19      18.5     FALSE
#> gene20      19.5      TRUE

reduction_mapping

You can provide a mapping for the reductions slot. The names of the named list refer to what the reduction will be called. The values must be a named list, containing the keys: key, obsm and maybe varm, if appropriate for the reduction.

seurat_obj <- to_Seurat(
  adata = ad,
  reduction_mapping = list(
    pca = list(key = "PC", obsm = "X_pca", varm = "PCs"),
    umap = list(key = "UMAP", obsm = "X_umap")
  )
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: Keys should be one or more alphanumeric characters followed by an
#> underscore, setting key from PC to PC_
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: Keys should be one or more alphanumeric characters followed by an
#> underscore, setting key from UMAP to UMAP_
#> Warning: No columnames present in cell embeddings, setting to 'UMAP_1:2'
Reductions(seurat_obj)
#> [1] "pca"  "umap"

graph_mapping

You can provide a mapping for the graph slot. The names of the named list refer to what the graph will be called in the Seurat object. The values refer to names in the obsp slot of the anndata object. stored in the obsp slot.

seurat_obj <- to_Seurat(
  adata = ad,
  graph_mapping = list(connectivities = "numeric_matrix", connectivities_sparse = "numeric_csparse")
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: No columnames present in cell embeddings, setting to 'umap_1:2'
Graphs(seurat_obj)
#> [1] "RNA_connectivities"        "RNA_connectivities_sparse"

misc_mapping

You must provide a named list.

Names of the list correspond to the name the item will be assigned to in the misc of the Seurat object). Items can be a vector with two elements. The first is the name of a slot in the AnnData object (e.g. X, layers, obs, var, obsm, varm, obsp, varp, uns) and the second is the name of the item to transfer from that slot.

seurat_obj <- to_Seurat(
  adata = ad,
  misc_mapping = list(varp_neighbors = c("varp", "integer_matrix"), uns_vec = c("uns", "vec_integer"))
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: No columnames present in cell embeddings, setting to 'umap_1:2'
Misc(seurat_obj)
#> $varp_neighbors
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]    0    1    2    3    4    5    6    7    8     9    10    11    12
#>  [2,]   20   21   22   23   24   25   26   27   28    29    30    31    32
#>  [3,]   40   41   42   43   44   45   46   47   48    49    50    51    52
#>  [4,]   60   61   62   63   64   65   66   67   68    69    70    71    72
#>  [5,]   80   81   82   83   84   85   86   87   88    89    90    91    92
#>  [6,]  100  101  102  103  104  105  106  107  108   109   110   111   112
#>  [7,]  120  121  122  123  124  125  126  127  128   129   130   131   132
#>  [8,]  140  141  142  143  144  145  146  147  148   149   150   151   152
#>  [9,]  160  161  162  163  164  165  166  167  168   169   170   171   172
#> [10,]  180  181  182  183  184  185  186  187  188   189   190   191   192
#> [11,]  200  201  202  203  204  205  206  207  208   209   210   211   212
#> [12,]  220  221  222  223  224  225  226  227  228   229   230   231   232
#> [13,]  240  241  242  243  244  245  246  247  248   249   250   251   252
#> [14,]  260  261  262  263  264  265  266  267  268   269   270   271   272
#> [15,]  280  281  282  283  284  285  286  287  288   289   290   291   292
#> [16,]  300  301  302  303  304  305  306  307  308   309   310   311   312
#> [17,]  320  321  322  323  324  325  326  327  328   329   330   331   332
#> [18,]  340  341  342  343  344  345  346  347  348   349   350   351   352
#> [19,]  360  361  362  363  364  365  366  367  368   369   370   371   372
#> [20,]  380  381  382  383  384  385  386  387  388   389   390   391   392
#>       [,14] [,15] [,16] [,17] [,18] [,19] [,20]
#>  [1,]    13    14    15    16    17    18    19
#>  [2,]    33    34    35    36    37    38    39
#>  [3,]    53    54    55    56    57    58    59
#>  [4,]    73    74    75    76    77    78    79
#>  [5,]    93    94    95    96    97    98    99
#>  [6,]   113   114   115   116   117   118   119
#>  [7,]   133   134   135   136   137   138   139
#>  [8,]   153   154   155   156   157   158   159
#>  [9,]   173   174   175   176   177   178   179
#> [10,]   193   194   195   196   197   198   199
#> [11,]   213   214   215   216   217   218   219
#> [12,]   233   234   235   236   237   238   239
#> [13,]   253   254   255   256   257   258   259
#> [14,]   273   274   275   276   277   278   279
#> [15,]   293   294   295   296   297   298   299
#> [16,]   313   314   315   316   317   318   319
#> [17,]   333   334   335   336   337   338   339
#> [18,]   353   354   355   356   357   358   359
#> [19,]   373   374   375   376   377   378   379
#> [20,]   393   394   395   396   397   398   399
#> 
#> $uns_vec
#>  [1] 0 1 2 3 4 5 6 7 8 9

Giving an item with one element can be used to copy the whole AnnData slot to misc.

seurat_obj <- to_Seurat(
  adata = ad,
  misc_mapping = list(metadata_from_anndata = "uns", varp = "varp")
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: No columnames present in cell embeddings, setting to 'umap_1:2'
Misc(seurat_obj)
#> $metadata_from_anndata
#> $metadata_from_anndata$vec_integer
#>  [1] 0 1 2 3 4 5 6 7 8 9
#> 
#> $metadata_from_anndata$vec_character
#>  [1] "value_0" "value_1" "value_2" "value_3" "value_4" "value_5" "value_6"
#>  [8] "value_7" "value_8" "value_9"
#> 
#> $metadata_from_anndata$df_integer
#>    integer
#> 1        0
#> 2        1
#> 3        2
#> 4        3
#> 5        4
#> 6        5
#> 7        6
#> 8        7
#> 9        8
#> 10       9
#> 
#> 
#> $varp
#> $varp$integer_matrix
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]    0    1    2    3    4    5    6    7    8     9    10    11    12
#>  [2,]   20   21   22   23   24   25   26   27   28    29    30    31    32
#>  [3,]   40   41   42   43   44   45   46   47   48    49    50    51    52
#>  [4,]   60   61   62   63   64   65   66   67   68    69    70    71    72
#>  [5,]   80   81   82   83   84   85   86   87   88    89    90    91    92
#>  [6,]  100  101  102  103  104  105  106  107  108   109   110   111   112
#>  [7,]  120  121  122  123  124  125  126  127  128   129   130   131   132
#>  [8,]  140  141  142  143  144  145  146  147  148   149   150   151   152
#>  [9,]  160  161  162  163  164  165  166  167  168   169   170   171   172
#> [10,]  180  181  182  183  184  185  186  187  188   189   190   191   192
#> [11,]  200  201  202  203  204  205  206  207  208   209   210   211   212
#> [12,]  220  221  222  223  224  225  226  227  228   229   230   231   232
#> [13,]  240  241  242  243  244  245  246  247  248   249   250   251   252
#> [14,]  260  261  262  263  264  265  266  267  268   269   270   271   272
#> [15,]  280  281  282  283  284  285  286  287  288   289   290   291   292
#> [16,]  300  301  302  303  304  305  306  307  308   309   310   311   312
#> [17,]  320  321  322  323  324  325  326  327  328   329   330   331   332
#> [18,]  340  341  342  343  344  345  346  347  348   349   350   351   352
#> [19,]  360  361  362  363  364  365  366  367  368   369   370   371   372
#> [20,]  380  381  382  383  384  385  386  387  388   389   390   391   392
#>       [,14] [,15] [,16] [,17] [,18] [,19] [,20]
#>  [1,]    13    14    15    16    17    18    19
#>  [2,]    33    34    35    36    37    38    39
#>  [3,]    53    54    55    56    57    58    59
#>  [4,]    73    74    75    76    77    78    79
#>  [5,]    93    94    95    96    97    98    99
#>  [6,]   113   114   115   116   117   118   119
#>  [7,]   133   134   135   136   137   138   139
#>  [8,]   153   154   155   156   157   158   159
#>  [9,]   173   174   175   176   177   178   179
#> [10,]   193   194   195   196   197   198   199
#> [11,]   213   214   215   216   217   218   219
#> [12,]   233   234   235   236   237   238   239
#> [13,]   253   254   255   256   257   258   259
#> [14,]   273   274   275   276   277   278   279
#> [15,]   293   294   295   296   297   298   299
#> [16,]   313   314   315   316   317   318   319
#> [17,]   333   334   335   336   337   338   339
#> [18,]   353   354   355   356   357   358   359
#> [19,]   373   374   375   376   377   378   379
#> [20,]   393   394   395   396   397   398   399
#> 
#> $varp$numeric_matrix
#>        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11] [,12]
#>  [1,]   0.5   1.5   2.5   3.5   4.5   5.5   6.5   7.5   8.5   9.5  10.5  11.5
#>  [2,]  20.5  21.5  22.5  23.5  24.5  25.5  26.5  27.5  28.5  29.5  30.5  31.5
#>  [3,]  40.5  41.5  42.5  43.5  44.5  45.5  46.5  47.5  48.5  49.5  50.5  51.5
#>  [4,]  60.5  61.5  62.5  63.5  64.5  65.5  66.5  67.5  68.5  69.5  70.5  71.5
#>  [5,]  80.5  81.5  82.5  83.5  84.5  85.5  86.5  87.5  88.5  89.5  90.5  91.5
#>  [6,] 100.5 101.5 102.5 103.5 104.5 105.5 106.5 107.5 108.5 109.5 110.5 111.5
#>  [7,] 120.5 121.5 122.5 123.5 124.5 125.5 126.5 127.5 128.5 129.5 130.5 131.5
#>  [8,] 140.5 141.5 142.5 143.5 144.5 145.5 146.5 147.5 148.5 149.5 150.5 151.5
#>  [9,] 160.5 161.5 162.5 163.5 164.5 165.5 166.5 167.5 168.5 169.5 170.5 171.5
#> [10,] 180.5 181.5 182.5 183.5 184.5 185.5 186.5 187.5 188.5 189.5 190.5 191.5
#> [11,] 200.5 201.5 202.5 203.5 204.5 205.5 206.5 207.5 208.5 209.5 210.5 211.5
#> [12,] 220.5 221.5 222.5 223.5 224.5 225.5 226.5 227.5 228.5 229.5 230.5 231.5
#> [13,] 240.5 241.5 242.5 243.5 244.5 245.5 246.5 247.5 248.5 249.5 250.5 251.5
#> [14,] 260.5 261.5 262.5 263.5 264.5 265.5 266.5 267.5 268.5 269.5 270.5 271.5
#> [15,] 280.5 281.5 282.5 283.5 284.5 285.5 286.5 287.5 288.5 289.5 290.5 291.5
#> [16,] 300.5 301.5 302.5 303.5 304.5 305.5 306.5 307.5 308.5 309.5 310.5 311.5
#> [17,] 320.5 321.5 322.5 323.5 324.5 325.5 326.5 327.5 328.5 329.5 330.5 331.5
#> [18,] 340.5 341.5 342.5 343.5 344.5 345.5 346.5 347.5 348.5 349.5 350.5 351.5
#> [19,] 360.5 361.5 362.5 363.5 364.5 365.5 366.5 367.5 368.5 369.5 370.5 371.5
#> [20,] 380.5 381.5 382.5 383.5 384.5 385.5 386.5 387.5 388.5 389.5 390.5 391.5
#>       [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
#>  [1,]  12.5  13.5  14.5  15.5  16.5  17.5  18.5  19.5
#>  [2,]  32.5  33.5  34.5  35.5  36.5  37.5  38.5  39.5
#>  [3,]  52.5  53.5  54.5  55.5  56.5  57.5  58.5  59.5
#>  [4,]  72.5  73.5  74.5  75.5  76.5  77.5  78.5  79.5
#>  [5,]  92.5  93.5  94.5  95.5  96.5  97.5  98.5  99.5
#>  [6,] 112.5 113.5 114.5 115.5 116.5 117.5 118.5 119.5
#>  [7,] 132.5 133.5 134.5 135.5 136.5 137.5 138.5 139.5
#>  [8,] 152.5 153.5 154.5 155.5 156.5 157.5 158.5 159.5
#>  [9,] 172.5 173.5 174.5 175.5 176.5 177.5 178.5 179.5
#> [10,] 192.5 193.5 194.5 195.5 196.5 197.5 198.5 199.5
#> [11,] 212.5 213.5 214.5 215.5 216.5 217.5 218.5 219.5
#> [12,] 232.5 233.5 234.5 235.5 236.5 237.5 238.5 239.5
#> [13,] 252.5 253.5 254.5 255.5 256.5 257.5 258.5 259.5
#> [14,] 272.5 273.5 274.5 275.5 276.5 277.5 278.5 279.5
#> [15,] 292.5 293.5 294.5 295.5 296.5 297.5 298.5 299.5
#> [16,] 312.5 313.5 314.5 315.5 316.5 317.5 318.5 319.5
#> [17,] 332.5 333.5 334.5 335.5 336.5 337.5 338.5 339.5
#> [18,] 352.5 353.5 354.5 355.5 356.5 357.5 358.5 359.5
#> [19,] 372.5 373.5 374.5 375.5 376.5 377.5 378.5 379.5
#> [20,] 392.5 393.5 394.5 395.5 396.5 397.5 398.5 399.5

Of course you can also mix these approaches in a single mapping.

seurat_obj <- to_Seurat(
  adata = ad,
  misc_mapping = list(metadata_from_anndata = "uns", obs_num = c("obs", "numeric"))
)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
#> Warning: No columnames present in cell embeddings, setting to 'PC_1:5'
#> Warning: No columnames present in cell embeddings, setting to 'umap_1:2'
Misc(seurat_obj)
#> $metadata_from_anndata
#> $metadata_from_anndata$vec_integer
#>  [1] 0 1 2 3 4 5 6 7 8 9
#> 
#> $metadata_from_anndata$vec_character
#>  [1] "value_0" "value_1" "value_2" "value_3" "value_4" "value_5" "value_6"
#>  [8] "value_7" "value_8" "value_9"
#> 
#> $metadata_from_anndata$df_integer
#>    integer
#> 1        0
#> 2        1
#> 3        2
#> 4        3
#> 5        4
#> 6        5
#> 7        6
#> 8        7
#> 9        8
#> 10       9
#> 
#> 
#> $obs_num
#>  [1] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5

Convert Seurat objects to AnnData objects

Implicit mapping

The reverse, converting Seurat objects to AnnData objects, works in a similar way. There is an implicit conversion, where we attempt a standard conversion, but the user can always provide an explicit mapping.

suppressWarnings({
  counts <- matrix(rbinom(600, 20000, .001), nrow = 20)
  obj <- CreateSeuratObject(counts = counts) |>
    NormalizeData() |>
    FindVariableFeatures() |>
    ScaleData() |>
    RunPCA(npcs = 10L) |>
    FindNeighbors() |>
    RunUMAP(dims = 1:10)
  obj@misc[["data"]] <- "some data"
})
#> Normalizing layer: counts
#> Finding variable features for layer counts
#> Centering and scaling data matrix
#> PC_ 1 
#> Positive:  Feature4, Feature5, Feature13, Feature11, Feature9, Feature17, Feature10, Feature7, Feature1, Feature15 
#> Negative:  Feature2, Feature16, Feature3, Feature8, Feature6, Feature14, Feature19, Feature18, Feature12, Feature20 
#> PC_ 2 
#> Positive:  Feature18, Feature17, Feature3, Feature1, Feature5, Feature19, Feature10, Feature13, Feature2, Feature15 
#> Negative:  Feature20, Feature4, Feature14, Feature9, Feature6, Feature11, Feature12, Feature8, Feature16, Feature7 
#> PC_ 3 
#> Positive:  Feature15, Feature16, Feature11, Feature2, Feature13, Feature10, Feature3, Feature20, Feature7, Feature18 
#> Negative:  Feature12, Feature6, Feature5, Feature14, Feature8, Feature1, Feature9, Feature19, Feature4, Feature17 
#> PC_ 4 
#> Positive:  Feature8, Feature7, Feature19, Feature5, Feature15, Feature3, Feature9, Feature2, Feature17, Feature11 
#> Negative:  Feature10, Feature18, Feature14, Feature1, Feature20, Feature16, Feature6, Feature12, Feature4, Feature13 
#> PC_ 5 
#> Positive:  Feature19, Feature14, Feature20, Feature11, Feature1, Feature17, Feature3, Feature13, Feature15, Feature12 
#> Negative:  Feature16, Feature6, Feature10, Feature2, Feature5, Feature4, Feature8, Feature7, Feature9, Feature18
#> Computing nearest neighbor graph
#> Computing SNN
#> 20:20:44 UMAP embedding parameters a = 0.9922 b = 1.112
#> 20:20:44 Read 30 rows and found 10 numeric columns
#> 20:20:44 Using Annoy for neighbor search, n_neighbors = 30
#> 20:20:44 Building Annoy index with metric = cosine, n_trees = 50
#> 20:20:44 Writing NN index file to temp file /tmp/RtmppHIQ3F/file2268229800d8
#> 20:20:44 Searching Annoy index using 1 thread, search_k = 3000
#> 20:20:44 Annoy recall = 100%
#> 20:20:44 Commencing smooth kNN distance calibration using 1 thread with target n_neighbors = 30
#> 20:20:46 Initializing from normalized Laplacian + noise (using RSpectra)
#> 20:20:46 Commencing optimization for 500 epochs, with 868 positive edges
#> 20:20:46 Using rng type: pcg
#> 20:20:46 Optimization finished
ad <- from_Seurat(obj)
ad
#> AnnData object with n_obs × n_vars = 30 × 20
#>     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'
#>     uns: 'data'
#>     obsm: 'X_pca', 'X_umap'
#>     varm: 'pca'
#>     layers: 'counts', 'data', 'scale.data'
#>     obsp: 'connectivities', 'snn'

assay_name

The assay_name argument determines which assay in the Seurat object will be converted to the AnnData object. By default it’s the DefaultAssay.

ad <- from_Seurat(
  obj,
  assay_name = NULL,
  layers_mapping = list(),
  obs_mapping = list(),
  var_mapping = list(),
  obsm_mapping = list(),
  varm_mapping = list(),
  obsp_mapping = list(),
  varp_mapping = list(),
  uns_mapping = list()
)
ad
#> AnnData object with n_obs × n_vars = 30 × 20

x_mapping

By default, no data will be put into the X slot of the AnnData object. If you want to put the data from a Seurat layer, such as counts or data into the X slot, you need to provide a mapping.

ad <- from_Seurat(
  obj,
  assay_name = "RNA",
  layers_mapping = list(),
  obs_mapping = list(),
  var_mapping = list(),
  obsm_mapping = list(),
  varm_mapping = list(),
  obsp_mapping = list(),
  varp_mapping = list(),
  uns_mapping = list()
)
ad$X
#> NULL

As you can see, by default, no information was copied to the X slot of the AnnData object.

layers_mapping

By default, all layers in the Seurat object will be copied to the AnnData object. This means that the X slot will be NULL (empty).

ad <- from_Seurat(
  obj,
  obs_mapping = list(),
  var_mapping = list(),
  obsm_mapping = list(),
  varm_mapping = list(),
  obsp_mapping = list(),
  varp_mapping = list(),
  uns_mapping = list()
)

names(ad$layers)
#> [1] "counts"     "data"       "scale.data"

obs_mapping

The obs slot is used to store observation-level annotations, such as cell-level metadata. By default, all information in the meta.data slot of the Seurat object is copied to the obs slot of the AnnData object.

ad <- from_Seurat(
  obj,
  layers_mapping = list(),
  var_mapping = list(),
  obsm_mapping = list(),
  varm_mapping = list(),
  obsp_mapping = list(),
  varp_mapping = list(),
  uns_mapping = list()
)
names(ad$obs)
#> [1] "orig.ident"   "nCount_RNA"   "nFeature_RNA"

var_mapping

The var slot is used to store feature-level annotations, such as gene-level metadata. By default, all information in the meta.data slot of the specified assay of the Seurat object is copied to the var slot of the AnnData object.

ad <- from_Seurat(
  obj,
  layers_mapping = list(),
  obs_mapping = list(),
  obsm_mapping = list(),
  varm_mapping = list(),
  obsp_mapping = list(),
  varp_mapping = list(),
  uns_mapping = list()
)
names(ad$var)
#> [1] "vf_vst_counts_mean"                  "vf_vst_counts_variance"             
#> [3] "vf_vst_counts_variance.expected"     "vf_vst_counts_variance.standardized"
#> [5] "vf_vst_counts_variable"              "vf_vst_counts_rank"                 
#> [7] "var.features"                        "var.features.rank"

obsm_mapping

The obsm slot is used to store multidimensional observation-level annotations, such as dimensionality reductions. By default, it will prefix the name of all Seurat reductions with X_.

ad <- from_Seurat(
  obj,
  layers_mapping = list(),
  obs_mapping = list(),
  var_mapping = list(),
  varm_mapping = list(),
  obsp_mapping = list(),
  varp_mapping = list(),
  uns_mapping = list()
)

ad
#> AnnData object with n_obs × n_vars = 30 × 20
#>     obsm: 'X_pca', 'X_umap'

varm_mapping

The varm slot is used to store multidimensional feature-level annotations. This is mainly used to store the loadings of dimensionality reductions. By default, all loadings of dimensionality reductions will be copied to the varm slot of the AnnData object.

ad <- from_Seurat(
  obj,
  layers_mapping = list(),
  obs_mapping = list(),
  var_mapping = list(),
  obsm_mapping = list(),
  obsp_mapping = list(),
  varp_mapping = list(),
  uns_mapping = list()
)

ad$varm
#> $pca
#>                  PC_1         PC_2        PC_3        PC_4        PC_5
#> Feature15 -0.05227803 -0.019399528  0.44161454  0.17822059  0.00454775
#> Feature19 -0.14200272  0.109667902 -0.08084922  0.32899284  0.39557855
#> Feature18 -0.10853639  0.444856613 -0.01597420 -0.27248080 -0.10989492
#> Feature14 -0.16780483 -0.234371948 -0.26340375 -0.27007196  0.34575002
#> Feature20 -0.10466175 -0.408314234  0.06766743 -0.13611932  0.27485105
#> Feature2  -0.32888682  0.008053757  0.19019450  0.08708914 -0.25012356
#> Feature11  0.29155184 -0.135122174  0.27334345 -0.02449776  0.27050415
#> Feature3  -0.21866990  0.329951521  0.07030886  0.14777898  0.09934857
#> Feature10  0.07770765  0.056000235  0.12047402 -0.48691646 -0.26572618
#> Feature13  0.31453401  0.014187705  0.17602993 -0.02516386  0.08204985
#> Feature1   0.01025817  0.280384894 -0.16894614 -0.23295415  0.11140541
#> Feature8  -0.21551470 -0.114209145 -0.18596036  0.41137421 -0.20191520
#> Feature6  -0.20855351 -0.165113942 -0.31730329 -0.08811215 -0.27789268
#> Feature9   0.28907181 -0.208023318 -0.09566863  0.10998505 -0.12665892
#> Feature4   0.41637607 -0.236279583 -0.05134871 -0.04202887 -0.23732401
#> Feature7   0.07228113 -0.081042914  0.03772760  0.34687974 -0.17728012
#> Feature5   0.32805964  0.225219063 -0.26948572  0.18972561 -0.24091818
#> Feature16 -0.25345187 -0.089589761  0.38523914 -0.12748709 -0.32617697
#> Feature17  0.21135775  0.372896983 -0.04365471  0.05268677  0.10353638
#> Feature12 -0.10732343 -0.122989640 -0.40365146 -0.07791276 -0.06352493
#>                   PC_6        PC_7          PC_8         PC_9       PC_10
#> Feature15  0.058330816 -0.28243025 -0.2602051448  0.252149257 -0.37375909
#> Feature19  0.339325239 -0.13669655 -0.0922632382 -0.355056725 -0.07995740
#> Feature18 -0.002811198 -0.02418511 -0.1161793764  0.228539521  0.07908915
#> Feature14  0.052670710 -0.30795715  0.1792500357 -0.044573226  0.11272173
#> Feature20 -0.226555069  0.28338369  0.0008902168  0.006061468  0.24571242
#> Feature2  -0.175644502  0.36508442  0.1425692879 -0.127325755 -0.09548892
#> Feature11 -0.261534492  0.00877141  0.0837554413  0.319799164  0.06949476
#> Feature3  -0.309672084  0.13502749 -0.3090791217 -0.274177324  0.02202451
#> Feature10  0.245067338 -0.05239387  0.0021916893 -0.397294839  0.23597740
#> Feature13  0.273550272  0.16953876  0.3648636645 -0.173097179 -0.49197774
#> Feature1   0.338285721  0.10220275 -0.1876144319  0.421753186  0.02352705
#> Feature8   0.056557715 -0.39301041  0.1420912248  0.180764850  0.21371306
#> Feature6  -0.234055891 -0.01068926 -0.0330187750 -0.101166079 -0.33285283
#> Feature9   0.068968640  0.13766587 -0.6419921851 -0.049344107  0.10098511
#> Feature4  -0.150873604 -0.25141936 -0.1407421736 -0.088944801 -0.06798775
#> Feature7   0.323526239  0.40973321  0.1746664646  0.147503352  0.36277798
#> Feature5  -0.081251122 -0.07984045  0.2186186045 -0.060516328  0.04825786
#> Feature16  0.078394750 -0.21419613  0.1064222052  0.140577090  0.05955059
#> Feature17 -0.423382529 -0.06208010  0.2097279863  0.064179525  0.09293918
#> Feature12  0.026525521  0.26751394  0.0097441106  0.306930932 -0.38007387

obsp_mapping

The obsp slot is used to store pairwise relations between observations. This is mainly used to store the connectivities of a graph. By default, all Seurat graphs are copied to the obsp slot of the AnnData object.

ad <- from_Seurat(
  obj,
  layers_mapping = list(),
  obs_mapping = list(),
  var_mapping = list(),
  obsm_mapping = list(),
  varm_mapping = list(),
  varp_mapping = list(),
  uns_mapping = list()
)
ad$obsp
#> $connectivities
#> 30 x 30 sparse Matrix of class "dgCMatrix"
#>   [[ suppressing 30 column names 'Cell_1', 'Cell_2', 'Cell_3' ... ]]
#>                                                                    
#> Cell_1  1 . . . . . 1 1 1 1 1 1 . 1 1 1 1 1 . . 1 . 1 1 1 . 1 1 1 1
#> Cell_2  . 1 1 . 1 . 1 1 . 1 1 . 1 1 1 1 1 1 . . . . 1 1 1 1 1 1 1 .
#> Cell_3  . 1 1 . . . 1 1 1 1 . . 1 1 1 1 . 1 1 1 1 . 1 . 1 . 1 1 1 1
#> Cell_4  . 1 1 1 1 1 1 1 . 1 . . 1 1 1 1 . 1 1 1 1 . 1 . 1 . 1 1 . .
#> Cell_5  . 1 . 1 1 . 1 1 1 1 . . 1 . 1 1 . . 1 1 1 1 1 1 . 1 1 1 . 1
#> Cell_6  . 1 1 1 1 1 1 . 1 1 . 1 1 . 1 . . 1 1 1 1 . 1 . 1 . 1 1 . 1
#> Cell_7  . 1 1 . 1 1 1 . . 1 . 1 1 1 1 . . 1 1 . 1 1 1 1 1 . 1 1 . 1
#> Cell_8  . 1 1 . 1 . . 1 . . 1 1 1 1 1 1 1 1 1 1 . . 1 1 1 . 1 1 1 .
#> Cell_9  . . 1 . 1 . 1 . 1 1 1 1 1 . 1 1 1 . . . 1 1 1 1 1 1 1 . 1 1
#> Cell_10 . 1 1 . 1 1 1 . 1 1 1 1 1 1 1 1 . 1 . 1 1 . 1 . . . . 1 1 1
#> Cell_11 . 1 1 . . . 1 1 1 1 1 1 1 1 1 . 1 1 . . 1 . 1 1 1 . 1 1 1 .
#> Cell_12 . . 1 . . . 1 1 1 1 1 1 1 1 1 . . 1 1 1 . . 1 1 1 . 1 1 1 1
#> Cell_13 . 1 1 . 1 . 1 1 . 1 1 1 1 1 1 1 . 1 1 . . . 1 1 . 1 1 1 1 .
#> Cell_14 . . 1 1 . . 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 . 1 . . . 1 1 1 .
#> Cell_15 . . . . 1 . 1 1 1 1 1 1 1 . 1 1 . 1 1 1 . . 1 1 . 1 1 1 1 1
#> Cell_16 . 1 1 1 1 . 1 1 1 1 . . 1 . 1 1 . 1 1 1 1 . . 1 . 1 1 . 1 1
#> Cell_17 . 1 1 . 1 . 1 1 1 . 1 . 1 . 1 . 1 . . . 1 1 1 1 1 1 1 1 1 1
#> Cell_18 . 1 1 . . . 1 1 . 1 1 1 1 1 1 1 . 1 1 1 . . 1 . 1 . 1 1 1 1
#> Cell_19 . . 1 1 1 . 1 1 1 . . 1 1 1 1 1 . 1 1 1 . . 1 1 1 . 1 1 1 .
#> Cell_20 . . 1 1 1 . 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 . 1 . 1 . . 1 . .
#> Cell_21 . . 1 . 1 . 1 . 1 1 1 . 1 1 1 1 . . . 1 1 1 1 1 1 . 1 1 1 1
#> Cell_22 . 1 . . 1 . 1 . 1 1 1 1 1 . 1 1 1 . . . 1 1 1 1 1 . 1 1 1 1
#> Cell_23 . 1 1 . . 1 1 . 1 1 1 1 1 1 1 . 1 1 . . 1 . 1 1 1 . 1 1 1 .
#> Cell_24 . . . . 1 . 1 . 1 . 1 1 1 . 1 1 1 . 1 . 1 1 1 1 1 1 1 1 1 1
#> Cell_25 . . 1 . . . 1 1 1 . 1 1 1 1 1 . . 1 1 . 1 1 1 1 1 . 1 1 1 1
#> Cell_26 . 1 1 . 1 . 1 1 1 1 1 . 1 . 1 1 1 1 . . . . 1 1 . 1 1 1 1 1
#> Cell_27 . 1 . . 1 . 1 1 1 . 1 1 1 1 1 1 . 1 1 . . . 1 1 1 1 1 1 1 .
#> Cell_28 . 1 1 . 1 . 1 1 . 1 . 1 1 1 1 . 1 1 1 1 . 1 1 1 . . 1 1 . 1
#> Cell_29 1 1 1 . . . 1 1 1 1 1 1 1 1 1 1 . 1 . . 1 . 1 1 1 . 1 . 1 .
#> Cell_30 . . 1 . 1 . 1 . 1 1 . 1 1 . 1 1 1 1 . . 1 1 1 1 1 1 1 1 . 1
#> 
#> $snn
#> 30 x 30 sparse Matrix of class "dgCMatrix"
#>   [[ suppressing 30 column names 'Cell_1', 'Cell_2', 'Cell_3' ... ]]
#>                                                                              
#> Cell_1  1.0000000 0.6000000 0.6000000 0.4285714 0.4285714 0.4285714 0.4814815
#> Cell_2  0.6000000 1.0000000 0.6000000 0.6000000 0.4814815 0.4285714 0.5384615
#> Cell_3  0.6000000 0.6000000 1.0000000 0.7391304 0.6000000 0.6666667 0.6000000
#> Cell_4  0.4285714 0.6000000 0.7391304 1.0000000 0.6000000 0.7391304 0.6666667
#> Cell_5  0.4285714 0.4814815 0.6000000 0.6000000 1.0000000 0.6000000 0.5384615
#> Cell_6  0.4285714 0.4285714 0.6666667 0.7391304 0.6000000 1.0000000 0.7391304
#> Cell_7  0.4814815 0.5384615 0.6000000 0.6666667 0.5384615 0.7391304 1.0000000
#> Cell_8  0.5384615 0.7391304 0.6000000 0.6000000 0.4285714 0.4814815 0.5384615
#> Cell_9  0.6000000 0.6000000 0.4814815 0.3793103 0.5384615 0.4814815 0.5384615
#> Cell_10 0.5384615 0.5384615 0.6666667 0.6000000 0.4814815 0.6666667 0.6000000
#> Cell_11 0.7391304 0.7391304 0.6666667 0.5384615 0.4285714 0.5384615 0.6000000
#> Cell_12 0.6666667 0.6000000 0.7391304 0.5384615 0.4814815 0.6000000 0.6000000
#> Cell_13 0.5384615 0.8181818 0.6000000 0.6000000 0.5384615 0.4814815 0.6000000
#> Cell_14 0.6000000 0.5384615 0.7391304 0.6666667 0.5384615 0.6000000 0.4814815
#> Cell_15 0.6000000 0.6000000 0.6000000 0.4814815 0.6666667 0.5384615 0.4814815
#> Cell_16 0.4285714 0.5384615 0.6666667 0.6000000 0.7391304 0.6000000 0.4814815
#> Cell_17 0.5384615 0.6666667 0.5384615 0.4285714 0.6000000 0.4814815 0.5384615
#> Cell_18 0.6000000 0.6666667 0.8181818 0.6666667 0.4814815 0.6000000 0.6000000
#> Cell_19 0.5384615 0.6000000 0.6666667 0.6666667 0.5384615 0.6000000 0.5384615
#> Cell_20 0.5384615 0.5384615 0.6666667 0.7391304 0.5384615 0.6666667 0.5384615
#> Cell_21 0.6000000 0.6000000 0.6666667 0.5384615 0.6000000 0.5384615 0.6000000
#> Cell_22 0.6666667 0.6000000 0.5384615 0.4285714 0.6000000 0.5384615 0.6000000
#> Cell_23 0.6666667 0.6666667 0.6000000 0.5384615 0.3793103 0.6000000 0.6666667
#> Cell_24 0.6000000 0.5384615 0.4814815 0.3793103 0.6000000 0.4814815 0.5384615
#> Cell_25 0.6666667 0.5384615 0.6666667 0.4814815 0.4814815 0.5384615 0.6666667
#> Cell_26 0.6000000 0.8181818 0.6000000 0.4814815 0.6000000 0.4814815 0.4814815
#> Cell_27 0.6000000 0.7391304 0.6000000 0.5384615 0.5384615 0.4814815 0.5384615
#> Cell_28 0.4814815 0.6000000 0.6000000 0.6000000 0.6000000 0.6000000 0.7391304
#> Cell_29 0.7391304 0.6666667 0.6666667 0.5384615 0.4285714 0.4814815 0.5384615
#> Cell_30 0.6000000 0.6000000 0.5384615 0.4814815 0.6000000 0.6000000 0.6666667
#>                                                                              
#> Cell_1  0.5384615 0.6000000 0.5384615 0.7391304 0.6666667 0.5384615 0.6000000
#> Cell_2  0.7391304 0.6000000 0.5384615 0.7391304 0.6000000 0.8181818 0.5384615
#> Cell_3  0.6000000 0.4814815 0.6666667 0.6666667 0.7391304 0.6000000 0.7391304
#> Cell_4  0.6000000 0.3793103 0.6000000 0.5384615 0.5384615 0.6000000 0.6666667
#> Cell_5  0.4285714 0.5384615 0.4814815 0.4285714 0.4814815 0.5384615 0.5384615
#> Cell_6  0.4814815 0.4814815 0.6666667 0.5384615 0.6000000 0.4814815 0.6000000
#> Cell_7  0.5384615 0.5384615 0.6000000 0.6000000 0.6000000 0.6000000 0.4814815
#> Cell_8  1.0000000 0.4814815 0.5384615 0.6666667 0.6666667 0.7391304 0.6000000
#> Cell_9  0.4814815 1.0000000 0.5384615 0.6000000 0.5384615 0.5384615 0.4814815
#> Cell_10 0.5384615 0.5384615 1.0000000 0.6000000 0.6000000 0.6000000 0.6666667
#> Cell_11 0.6666667 0.6000000 0.6000000 1.0000000 0.7391304 0.6666667 0.6666667
#> Cell_12 0.6666667 0.5384615 0.6000000 0.7391304 1.0000000 0.6666667 0.7391304
#> Cell_13 0.7391304 0.5384615 0.6000000 0.6666667 0.6666667 1.0000000 0.6666667
#> Cell_14 0.6000000 0.4814815 0.6666667 0.6666667 0.7391304 0.6666667 1.0000000
#> Cell_15 0.6000000 0.6000000 0.6000000 0.5384615 0.7391304 0.7391304 0.6666667
#> Cell_16 0.4814815 0.5384615 0.5384615 0.4814815 0.5384615 0.6000000 0.6000000
#> Cell_17 0.5384615 0.7391304 0.4814815 0.6666667 0.5384615 0.5384615 0.4285714
#> Cell_18 0.7391304 0.4814815 0.6666667 0.6666667 0.8181818 0.7391304 0.7391304
#> Cell_19 0.7391304 0.4814815 0.5384615 0.6000000 0.7391304 0.6666667 0.7391304
#> Cell_20 0.6000000 0.4814815 0.6666667 0.6000000 0.6666667 0.6000000 0.8181818
#> Cell_21 0.5384615 0.7391304 0.6666667 0.6000000 0.6666667 0.5384615 0.6000000
#> Cell_22 0.5384615 0.8181818 0.6000000 0.6666667 0.5384615 0.5384615 0.4814815
#> Cell_23 0.6000000 0.6000000 0.6666667 0.9047619 0.6666667 0.6000000 0.6000000
#> Cell_24 0.5384615 0.8181818 0.4814815 0.5384615 0.5384615 0.5384615 0.4814815
#> Cell_25 0.6000000 0.6000000 0.5384615 0.7391304 0.8181818 0.6000000 0.6666667
#> Cell_26 0.6000000 0.6666667 0.6000000 0.6666667 0.6000000 0.7391304 0.5384615
#> Cell_27 0.7391304 0.5384615 0.5384615 0.6666667 0.6666667 0.8181818 0.6000000
#> Cell_28 0.6666667 0.4814815 0.5384615 0.6000000 0.6666667 0.6666667 0.5384615
#> Cell_29 0.6000000 0.6000000 0.6000000 0.8181818 0.6666667 0.6666667 0.6666667
#> Cell_30 0.4814815 0.8181818 0.5384615 0.6000000 0.5384615 0.5384615 0.4814815
#>                                                                              
#> Cell_1  0.6000000 0.4285714 0.5384615 0.6000000 0.5384615 0.5384615 0.6000000
#> Cell_2  0.6000000 0.5384615 0.6666667 0.6666667 0.6000000 0.5384615 0.6000000
#> Cell_3  0.6000000 0.6666667 0.5384615 0.8181818 0.6666667 0.6666667 0.6666667
#> Cell_4  0.4814815 0.6000000 0.4285714 0.6666667 0.6666667 0.7391304 0.5384615
#> Cell_5  0.6666667 0.7391304 0.6000000 0.4814815 0.5384615 0.5384615 0.6000000
#> Cell_6  0.5384615 0.6000000 0.4814815 0.6000000 0.6000000 0.6666667 0.5384615
#> Cell_7  0.4814815 0.4814815 0.5384615 0.6000000 0.5384615 0.5384615 0.6000000
#> Cell_8  0.6000000 0.4814815 0.5384615 0.7391304 0.7391304 0.6000000 0.5384615
#> Cell_9  0.6000000 0.5384615 0.7391304 0.4814815 0.4814815 0.4814815 0.7391304
#> Cell_10 0.6000000 0.5384615 0.4814815 0.6666667 0.5384615 0.6666667 0.6666667
#> Cell_11 0.5384615 0.4814815 0.6666667 0.6666667 0.6000000 0.6000000 0.6000000
#> Cell_12 0.7391304 0.5384615 0.5384615 0.8181818 0.7391304 0.6666667 0.6666667
#> Cell_13 0.7391304 0.6000000 0.5384615 0.7391304 0.6666667 0.6000000 0.5384615
#> Cell_14 0.6666667 0.6000000 0.4285714 0.7391304 0.7391304 0.8181818 0.6000000
#> Cell_15 1.0000000 0.6666667 0.5384615 0.6666667 0.6666667 0.6000000 0.6000000
#> Cell_16 0.6666667 1.0000000 0.5384615 0.5384615 0.6000000 0.5384615 0.5384615
#> Cell_17 0.5384615 0.5384615 1.0000000 0.4814815 0.4814815 0.4285714 0.6666667
#> Cell_18 0.6666667 0.5384615 0.4814815 1.0000000 0.6666667 0.6666667 0.6000000
#> Cell_19 0.6666667 0.6000000 0.4814815 0.6666667 1.0000000 0.7391304 0.6000000
#> Cell_20 0.6000000 0.5384615 0.4285714 0.6666667 0.7391304 1.0000000 0.6000000
#> Cell_21 0.6000000 0.5384615 0.6666667 0.6000000 0.6000000 0.6000000 1.0000000
#> Cell_22 0.6000000 0.4814815 0.7391304 0.5384615 0.4814815 0.4814815 0.7391304
#> Cell_23 0.4814815 0.4285714 0.6000000 0.6000000 0.5384615 0.5384615 0.6000000
#> Cell_24 0.6666667 0.4814815 0.7391304 0.4814815 0.5384615 0.4814815 0.6666667
#> Cell_25 0.6000000 0.4814815 0.6666667 0.6666667 0.6666667 0.6000000 0.6666667
#> Cell_26 0.7391304 0.6666667 0.7391304 0.6000000 0.5384615 0.4814815 0.6000000
#> Cell_27 0.7391304 0.5384615 0.6000000 0.6666667 0.7391304 0.6000000 0.5384615
#> Cell_28 0.6000000 0.5384615 0.5384615 0.6666667 0.6000000 0.5384615 0.5384615
#> Cell_29 0.5384615 0.5384615 0.5384615 0.6666667 0.6000000 0.6000000 0.6000000
#> Cell_30 0.6000000 0.5384615 0.6666667 0.4814815 0.5384615 0.5384615 0.6666667
#>                                                                              
#> Cell_1  0.6666667 0.6666667 0.6000000 0.6666667 0.6000000 0.6000000 0.4814815
#> Cell_2  0.6000000 0.6666667 0.5384615 0.5384615 0.8181818 0.7391304 0.6000000
#> Cell_3  0.5384615 0.6000000 0.4814815 0.6666667 0.6000000 0.6000000 0.6000000
#> Cell_4  0.4285714 0.5384615 0.3793103 0.4814815 0.4814815 0.5384615 0.6000000
#> Cell_5  0.6000000 0.3793103 0.6000000 0.4814815 0.6000000 0.5384615 0.6000000
#> Cell_6  0.5384615 0.6000000 0.4814815 0.5384615 0.4814815 0.4814815 0.6000000
#> Cell_7  0.6000000 0.6666667 0.5384615 0.6666667 0.4814815 0.5384615 0.7391304
#> Cell_8  0.5384615 0.6000000 0.5384615 0.6000000 0.6000000 0.7391304 0.6666667
#> Cell_9  0.8181818 0.6000000 0.8181818 0.6000000 0.6666667 0.5384615 0.4814815
#> Cell_10 0.6000000 0.6666667 0.4814815 0.5384615 0.6000000 0.5384615 0.5384615
#> Cell_11 0.6666667 0.9047619 0.5384615 0.7391304 0.6666667 0.6666667 0.6000000
#> Cell_12 0.5384615 0.6666667 0.5384615 0.8181818 0.6000000 0.6666667 0.6666667
#> Cell_13 0.5384615 0.6000000 0.5384615 0.6000000 0.7391304 0.8181818 0.6666667
#> Cell_14 0.4814815 0.6000000 0.4814815 0.6666667 0.5384615 0.6000000 0.5384615
#> Cell_15 0.6000000 0.4814815 0.6666667 0.6000000 0.7391304 0.7391304 0.6000000
#> Cell_16 0.4814815 0.4285714 0.4814815 0.4814815 0.6666667 0.5384615 0.5384615
#> Cell_17 0.7391304 0.6000000 0.7391304 0.6666667 0.7391304 0.6000000 0.5384615
#> Cell_18 0.5384615 0.6000000 0.4814815 0.6666667 0.6000000 0.6666667 0.6666667
#> Cell_19 0.4814815 0.5384615 0.5384615 0.6666667 0.5384615 0.7391304 0.6000000
#> Cell_20 0.4814815 0.5384615 0.4814815 0.6000000 0.4814815 0.6000000 0.5384615
#> Cell_21 0.7391304 0.6000000 0.6666667 0.6666667 0.6000000 0.5384615 0.5384615
#> Cell_22 1.0000000 0.6666667 0.8181818 0.6000000 0.6666667 0.6000000 0.5384615
#> Cell_23 0.6666667 1.0000000 0.5384615 0.6666667 0.6000000 0.6000000 0.5384615
#> Cell_24 0.8181818 0.5384615 1.0000000 0.6666667 0.6000000 0.6666667 0.4814815
#> Cell_25 0.6000000 0.6666667 0.6666667 1.0000000 0.5384615 0.6666667 0.6000000
#> Cell_26 0.6666667 0.6000000 0.6000000 0.5384615 1.0000000 0.6666667 0.6000000
#> Cell_27 0.6000000 0.6000000 0.6666667 0.6666667 0.6666667 1.0000000 0.5384615
#> Cell_28 0.5384615 0.5384615 0.4814815 0.6000000 0.6000000 0.5384615 1.0000000
#> Cell_29 0.6000000 0.7391304 0.4814815 0.6666667 0.6000000 0.6666667 0.4814815
#> Cell_30 0.7391304 0.6000000 0.7391304 0.6000000 0.6666667 0.5384615 0.6000000
#>                            
#> Cell_1  0.7391304 0.6000000
#> Cell_2  0.6666667 0.6000000
#> Cell_3  0.6666667 0.5384615
#> Cell_4  0.5384615 0.4814815
#> Cell_5  0.4285714 0.6000000
#> Cell_6  0.4814815 0.6000000
#> Cell_7  0.5384615 0.6666667
#> Cell_8  0.6000000 0.4814815
#> Cell_9  0.6000000 0.8181818
#> Cell_10 0.6000000 0.5384615
#> Cell_11 0.8181818 0.6000000
#> Cell_12 0.6666667 0.5384615
#> Cell_13 0.6666667 0.5384615
#> Cell_14 0.6666667 0.4814815
#> Cell_15 0.5384615 0.6000000
#> Cell_16 0.5384615 0.5384615
#> Cell_17 0.5384615 0.6666667
#> Cell_18 0.6666667 0.4814815
#> Cell_19 0.6000000 0.5384615
#> Cell_20 0.6000000 0.5384615
#> Cell_21 0.6000000 0.6666667
#> Cell_22 0.6000000 0.7391304
#> Cell_23 0.7391304 0.6000000
#> Cell_24 0.4814815 0.7391304
#> Cell_25 0.6666667 0.6000000
#> Cell_26 0.6000000 0.6666667
#> Cell_27 0.6666667 0.5384615
#> Cell_28 0.4814815 0.6000000
#> Cell_29 1.0000000 0.5384615
#> Cell_30 0.5384615 1.0000000

varp_mapping

The varp slot is used to store pairwise relations between features. By default, no information is copied to the varp slot of the AnnData object.

ad <- from_Seurat(
  obj,
  layers_mapping = list(),
  obs_mapping = list(),
  var_mapping = list(),
  obsm_mapping = list(),
  varm_mapping = list(),
  obsp_mapping = list(),
  uns_mapping = list()
)
ad$varp
#> NULL

uns_mapping

The uns slot is used for any non-structured miscellaneous data. By default all information in the misc slot of the Seurat object is copied to the uns slot of the AnnData object.

ad <- from_Seurat(
  obj,
  layers_mapping = list(),
  obs_mapping = list(),
  var_mapping = list(),
  obsm_mapping = list(),
  varm_mapping = list(),
  obsp_mapping = list(),
  varp_mapping = list(),
)
ad$uns
#> $data
#> [1] "some data"

Explicit mapping

It’s also possible to provide an explicit mapping for the conversion from Seurat to AnnData objects. For layers_mapping, obs_mapping and var_mapping, you can provide a named list with the names of the items in the AnnData object as names and the names of the items in the Seurat object as values.

ad <- from_Seurat(
  obj,
  layers_mapping = list(counts = "counts", data = "data"),
  obs_mapping = list(metadata1 = "orig.ident", metadata2 = "nCount_RNA"),
  var_mapping = list(metadata_mean = "vf_vst_counts_mean", metadata_variance = "vf_vst_counts_variance"),
  obsm_mapping = list(),
  varm_mapping = list(),
  obsp_mapping = list(),
  varp_mapping = list(),
  uns_mapping = list()
)
ad
#> AnnData object with n_obs × n_vars = 30 × 20
#>     obs: 'metadata1', 'metadata2'
#>     var: 'metadata_mean', 'metadata_variance'
#>     layers: 'counts', 'data'

obsm_mapping, varm_mapping, obsp_mapping, varp_mapping, uns_mapping

The mappings for obsm, varm and obsp all work in the same way.
You need to provide a named list, where the name of the list refers to the name of the item in that slot of the AnnData object.
The values of the list must themselves be vectors of length 2 (for obsm) or 2 or 3 (for varm, obsp, varp and uns).
The first element of the vector refers to a slot in the the Seurat object and the second element refers to the name of the item in that slot.
The third element (if provided) refers to the name of a column in that item.

For obsm, the first element must be reductions or misc. For varm, the first element must be reductions or misc. For obsp, the first element must be graphs or misc.

For varp, the first element must be misc. For uns, the first element must be misc.

ad <- from_Seurat(
  obj,
  layers_mapping = list(),
  obs_mapping = list(),
  var_mapping = list(),
  obsm_mapping = list(X_pca = c("reductions", "pca"), X_umap = c("reductions", "umap")),
  varm_mapping = list(PCs = c("reductions", "pca")),
  obsp_mapping = list(connectivities = c("graphs", "RNA_nn")),
  varp_mapping = list(),
  uns_mapping = list(extra_data = c("misc", "data"))
)
ad
#> AnnData object with n_obs × n_vars = 30 × 20
#>     uns: 'extra_data'
#>     obsm: 'X_pca', 'X_umap'
#>     varm: 'PCs'
#>     obsp: 'connectivities'