Answers.Body: <p>Since both Seurat and Scanpy are evolving rapidly, the safest way is to
  <a href="https://github.com/brianpenghe/Seurat3-plus/blob/master/Seurat_to_MM10X.R" rel="nofollow noreferrer">save your Seurat object into MatrixMarket format + metadata csv</a>, and
  <a href="https://scanpy.readthedocs.io/en/stable/generated/scanpy.read_10x_mtx.html" rel="nofollow noreferrer">import them into Scanpy</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-20T10:37:24.413Z
Answers.LastActivityDate: 2021-10-20T10:37:24.413Z
Answers.UserDisplayName: Code42
Answers.Body: <p>The accepted solution is probably the best for older objects of type <code>seurat</code> created with Seurat package v2. For newer <code>Seurat Objects</code>, there is a new tool designed specifically for this purpose, called <a href="https://mojaveazure.github.io/seurat-disk/articles/convert-anndata.html" rel="nofollow noreferrer"><code>SeuratDisk</code></a>.</p>
  <p>There is a nicely documented vignette about the <a href="https://mojaveazure.github.io/seurat-disk/articles/convert-anndata.html" rel="nofollow noreferrer">Seurat &lt;-&gt; AnnData conversion</a>. In short:</p>
  <ol>
  <li>In R, save the Seurat object as an h5Seurat file:</li>
  </ol>
  <pre><code>library(Seurat)
  library(SeuratDisk)
  
  SaveH5Seurat(SeuratObject, filename = &quot;file.h5Seurat&quot;)
  Convert(&quot;file.h5Seurat&quot;, dest = &quot;h5ad&quot;)
  </code></pre>
  <ol start="2">
  <li>In python, simply load the file:</li>
  </ol>
  <pre class="lang-py prettyprint-override"><code>import scanpy
  
  adata = scanpy.read_h5ad(&quot;file.h5Seurat&quot;)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-04T22:37:10.183Z
Answers.LastActivityDate: 2021-08-04T22:37:10.183Z
Answers.UserDisplayName: Brunox13
Answers.Body: <p>A simple solution for converting Seurat objects into AnnData, <a href="https://satijalab.org/seurat/conversion_vignette.html" rel="nofollow noreferrer">as described in this vignette</a>:</p>
  
  <pre><code>library(reticulate)
  seuratobject_ad &lt;- Convert(from=seuratobject, to="anndata", filename="seuratobject.h5ad")
  </code></pre>
  
  <hr>
  
  <p>Alternatively, one can use <a href="http://loompy.org/" rel="nofollow noreferrer">Loom</a>, <em>"a file format designed to store and work with single-cell RNA-seq data".</em></p>
  
  <p>In R, install <a href="http://satijalab.org/loomR/loomR_tutorial.html" rel="nofollow noreferrer">LoomR</a>:</p>
  
  <pre><code>devtools::install_github(repo = "mojaveazure/loomR")
  </code></pre>
  
  <p>Convert from Seurat object to loom:</p>
  
  <pre><code>pfile &lt;- Convert(from = pbmc_small, to = "loom", filename = "pbmc_small.loom")
  pfile$close() # close loom objects when finished using them.
  </code></pre>
  
  <p>Then import loom object in Python using <a href="http://linnarssonlab.org/loompy/index.html" rel="nofollow noreferrer">loompy</a>, or directly as AnnData:</p>
  
  <p><a href="http://scanpy.readthedocs.io/en/latest/api/scanpy.api.read_loom.html" rel="nofollow noreferrer"><code>scanpy.api.read_loom</code></a></p>
  
  <hr>
  
  <p>Alternatively, see <a href="https://github.com/wesm/feather" rel="nofollow noreferrer"><code>feather</code></a>.</p>
  
  <hr>
  
  <p><a href="https://stackoverflow.com/questions/46390461/the-best-way-to-import-r-object-into-python">Or export</a> as some text format (csv, json) then import into Python.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-22T10:38:28.320Z
Answers.Text: I would recommend to follow the KISS rule. If you can export the data in .csv or json better, but that depends on what is the original data format
Answers.UserDisplayName: llrs
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-03-21T17:45:49.547Z
Answers.LastActivityDate: 2018-08-29T16:58:47.240Z
Answers.LastEditDate: 2018-08-29T16:58:47.240Z
Answers.UserDisplayName: Peter
Body: <p>I have done some work in R and would like to try a Python tool.</p>
  
  <p>What is a good way to <strong>import</strong> the data (and its annotations etc) <strong>as a Python object?</strong></p>
  
  <p>I am particularly interested in converting a <code>Seurat</code> object into an <a href="https://scanpy.readthedocs.io/en/latest/api/scanpy.api.AnnData.html#scanpy.api.AnnData" rel="noreferrer">AnnData</a> object. (Either directly or as a Python object which can be converted into an <code>AnnData</code>.)</p>
  
Tags: <rna-seq><r><python>
Title: Convert R RNA-seq data object to a Python object
ThreadText: # Convert R RNA-seq data object to a Python object
  
  (Asked by: username_0 on 2018-03-21)
  
  I have done some work in R and would like to try a Python tool.
  
  What is a good way to **import** the data (and its annotations etc) **as a Python object?**
  
  I am particularly interested in converting a `Seurat` object into an [AnnData](https://scanpy.readthedocs.io/en/latest/api/scanpy.api.AnnData.html#scanpy.api.AnnData) object. (Either directly or as a Python object which can be converted into an `AnnData`.)
  
  **Tags:** rna-seq, r, python
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2018-03-21. Score: 10 | Accepted answer
  
  A simple solution for converting Seurat objects into AnnData, [as described in this vignette](https://satijalab.org/seurat/conversion_vignette.html):
  
  ```
  library(reticulate)
  seuratobject_ad <- Convert(from=seuratobject, to="anndata", filename="seuratobject.h5ad")
  
  ```
  
  ---
  
  Alternatively, one can use [Loom](http://loompy.org/), *"a file format designed to store and work with single-cell RNA-seq data".*
  
  In R, install [LoomR](http://satijalab.org/loomR/loomR_tutorial.html):
  
  ```
  devtools::install_github(repo = "mojaveazure/loomR")
  
  ```
  
  Convert from Seurat object to loom:
  
  ```
  pfile <- Convert(from = pbmc_small, to = "loom", filename = "pbmc_small.loom")
  pfile$close() # close loom objects when finished using them.
  
  ```
  
  Then import loom object in Python using [loompy](http://linnarssonlab.org/loompy/index.html), or directly as AnnData:
  
  [`scanpy.api.read_loom`](http://scanpy.readthedocs.io/en/latest/api/scanpy.api.read_loom.html)
  
  ---
  
  Alternatively, see [`feather`](https://github.com/wesm/feather).
  
  ---
  
  [Or export](https://stackoverflow.com/questions/46390461/the-best-way-to-import-r-object-into-python) as some text format (csv, json) then import into Python.
  
  > **Comments:**
  > **username_3**: I would recommend to follow the KISS rule. If you can export the data in .csv or json better, but that depends on what is the original data format  
  
  ---
  
  ### Answer by username_1 on 2021-10-20. Score: 0
  
  Since both Seurat and Scanpy are evolving rapidly, the safest way is to
  [save your Seurat object into MatrixMarket format + metadata csv](https://github.com/brianpenghe/Seurat3-plus/blob/master/Seurat_to_MM10X.R), and
  [import them into Scanpy](https://scanpy.readthedocs.io/en/stable/generated/scanpy.read_10x_mtx.html).
  
  
  ---
  
  ### Answer by username_2 on 2021-08-04. Score: 0
  
  The accepted solution is probably the best for older objects of type `seurat` created with Seurat package v2. For newer `Seurat Objects`, there is a new tool designed specifically for this purpose, called [`SeuratDisk`](https://mojaveazure.github.io/seurat-disk/articles/convert-anndata.html).
  
  There is a nicely documented vignette about the [Seurat <-> AnnData conversion](https://mojaveazure.github.io/seurat-disk/articles/convert-anndata.html). In short:
  
  1. In R, save the Seurat object as an h5Seurat file:
  
  ```
  library(Seurat)
  library(SeuratDisk)
  
  SaveH5Seurat(SeuratObject, filename = "file.h5Seurat")
  Convert("file.h5Seurat", dest = "h5ad")
  
  ```
  
  2. In python, simply load the file:
  
  ```py
  import scanpy
  
  adata = scanpy.read_h5ad("file.h5Seurat")
  
  ```
  
  
  ---
  
  