<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Conversions: h5Seurat and AnnData • SeuratDisk</title>
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.4.0/flatly/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css">
<script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script><meta property="og:title" content="Conversions: h5Seurat and AnnData">
<meta property="og:description" content="SeuratDisk">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#toc">
    <div class="container template-article">
      <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">SeuratDisk</a>
        <span class="version label label-danger" data-toggle="tooltip" data-placement="bottom" title="Unreleased version">0.0.0.9011</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
<li>
  <a href="../index.html">
    <span class="fas fa fas fa-home fa-lg"></span>
     
  </a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
    Articles
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
<li>
      <a href="../articles/convert-anndata.html">Conversions: h5Seurat and AnnData</a>
    </li>
    <li>
      <a href="../articles/h5Seurat-load.html">Saving and Loading Data from an h5Seurat File</a>
    </li>
    <li>
      <a href="../articles/h5Seurat-spec.html">h5Seurat File Format Specification</a>
    </li>
  </ul>
</li>
      </ul>
<ul class="nav navbar-nav navbar-right">
<li>
  <a href="https://github.com/mojaveazure/seurat-disk/">
    <span class="fab fa fab fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
</div>
<!--/.nav-collapse -->
  </div>
<!--/.container -->
</div>
<!--/.navbar -->

      

      </header><div class="row">
  <div class="col-md-9 contents">
    <div class="page-header toc-ignore">
      <h1 data-toc-skip>Conversions: h5Seurat and AnnData</h1>
            
      
      <small class="dont-index">Source: <a href="https://github.com/mojaveazure/seurat-disk/blob/master/vignettes/convert-anndata.Rmd"><code>vignettes/convert-anndata.Rmd</code></a></small>
      <div class="hidden name"><code>convert-anndata.Rmd</code></div>

    </div>

    
    
<p>This vignette showcases how to convert from <code>Seurat</code> object to AnnData files via an intermediate step thorugh h5Seurat files. This allows interoperability between Seurat and <a href="https://scanpy.readthedocs.io/">Scanpy</a></p>
<div class="sourceCode"><html><body><pre class="r"><span class="fu"><a href="https://rdrr.io/r/base/library.html">library</a></span>(<span class="no">Seurat</span>)
<span class="fu"><a href="https://rdrr.io/r/base/library.html">library</a></span>(<span class="no">SeuratData</span>)
<span class="fu"><a href="https://rdrr.io/r/base/library.html">library</a></span>(<span class="no">SeuratDisk</span>)</pre></body></html></div>
<div id="converting-from-seurat-to-anndata-via-h5seurat" class="section level2">
<h2 class="hasAnchor">
<a href="#converting-from-seurat-to-anndata-via-h5seurat" class="anchor"></a>Converting from Seurat to AnnData via h5Seurat</h2>
<p>To showcase going from a <code>Seurat</code> object to an AnnData file, we'll use the processed version of the PBMC 3k dataset, available on <a href="https://github.com/satijalab/seurat-data">SeuratData</a>; this dataset was created following <a href="https://satijalab.org/seurat/v3.1/pbmc3k_tutorial.html">Seurat's PBMC 3k tutorial</a></p>
<div class="sourceCode"><html><body><pre class="r"><span class="fu"><a href="https://rdrr.io/pkg/SeuratData/man/InstallData.html">InstallData</a></span>(<span class="st">"pbmc3k"</span>)
<span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span>(<span class="st">"pbmc3k.final"</span>)
<span class="no">pbmc3k.final</span>
<span class="co">#&gt; An object of class Seurat </span>
<span class="co">#&gt; 13714 features across 2638 samples within 1 assay </span>
<span class="co">#&gt; Active assay: RNA (13714 features, 2000 variable features)</span>
<span class="co">#&gt;  2 dimensional reductions calculated: pca, umap</span></pre></body></html></div>
<p>To see how this dataset was generated, please run <code>?pbmc3k.final</code></p>
<p>Converting the <code>Seurat</code> object to an AnnData file is a two-step process. First, we <a href="https://mojaveazure.github.io/seurat-disk/reference/SaveH5Seurat.html">save the <code>Seurat</code> object</a> as an h5Seurat file. For more details about saving <code>Seurat</code> objects to h5Seurat files, please see <a href="https://mojaveazure.github.io/seurat-disk/articles/h5Seurat-load.html">this vignette</a>; after the file is saved, we can <a href="https://mojaveazure.github.io/seurat-disk/reference/Convert.html">convert</a> it to an AnnData file for use in Scanpy. Full details about the conversion processes are listed in the <a href="https://mojaveazure.github.io/seurat-disk/reference/Convert.html">manual page</a> for the <code>Convert</code> function</p>
<div class="sourceCode"><html><body><pre class="r"><span class="fu"><a href="../reference/SaveH5Seurat.html">SaveH5Seurat</a></span>(<span class="no">pbmc3k.final</span>, <span class="kw">filename</span> <span class="kw">=</span> <span class="st">"pbmc3k.h5Seurat"</span>)
<span class="fu"><a href="../reference/Convert.html">Convert</a></span>(<span class="st">"pbmc3k.h5Seurat"</span>, <span class="kw">dest</span> <span class="kw">=</span> <span class="st">"h5ad"</span>)</pre></body></html></div>
<p>We can view the AnnData file in Scanpy by using the <code>read_h5ad</code> function</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> scanpy
<span class="co">#&gt; /home/paul/.local/lib/python3.6/site-packages/anndata/_core/anndata.py:21: FutureWarning: pandas.core.index is deprecated and will be removed in a future version.  The public classes are available in the top-level namespace.</span>
<span class="co">#&gt;   from pandas.core.index import RangeIndex</span>
<span class="co">#&gt; /home/paul/.local/lib/python3.6/site-packages/numba/core/errors.py:144: UserWarning: Insufficiently recent colorama version found. Numba requires colorama &gt;= 0.3.9</span>
<span class="co">#&gt;   warnings.warn(msg)</span>
adata <span class="op">=</span> scanpy.read_h5ad(<span class="st">"pbmc3k.h5ad"</span>)
adata
<span class="co">#&gt; AnnData object with n_obs × n_vars = 2638 × 13714 </span>
<span class="co">#&gt;     obs: 'orig.ident', 'nCount_RNA', 'nFeature_RNA', 'seurat_annotations', 'percent.mt', 'RNA_snn_res.0.5', 'seurat_clusters'</span>
<span class="co">#&gt;     var: 'vst.mean', 'vst.variance', 'vst.variance.expected', 'vst.variance.standardized', 'vst.variable'</span>
<span class="co">#&gt;     uns: 'neighbors'</span>
<span class="co">#&gt;     obsm: 'X_pca', 'X_umap'</span>
<span class="co">#&gt;     varm: 'PCs'</span></code></pre></div>
</div>
<div id="converting-from-anndata-to-seurat-via-h5seurat" class="section level2">
<h2 class="hasAnchor">
<a href="#converting-from-anndata-to-seurat-via-h5seurat" class="anchor"></a>Converting from AnnData to Seurat via h5Seurat</h2>
<p>To shocwcase going from an AnnData file to a <code>Seurat</code> object, we'll use a processed version of the PBMC 3k dataset; this dataset was processed using Scanpy following <a href="https://scanpy-tutorials.readthedocs.io/en/latest/pbmc3k.html">Scanpy's PBMC 3k tutorial</a></p>
<div class="sourceCode"><html><body><pre class="r"><span class="no">url</span> <span class="kw">&lt;-</span> <span class="st">"https://seurat.nygenome.org/pbmc3k_final.h5ad"</span>
<span class="kw pkg">curl</span><span class="kw ns">::</span><span class="fu"><a href="https://jeroen.cran.dev/curl/reference/curl_download.html">curl_download</a></span>(<span class="no">url</span>, <span class="fu"><a href="https://rdrr.io/r/base/basename.html">basename</a></span>(<span class="no">url</span>))</pre></body></html></div>
<p>To see how this dataset was created, please see <a href="https://gist.github.com/mojaveazure/922aa904b9ac212e627f522c2b816a52">this script</a></p>
<p>Converting the AnnData file to a <code>Seurat</code> object is a two-step process. First, convert the AnnData file to an h5Seurat file using the <a href="https://mojaveazure.github.io/seurat-disk/reference/Convert.html"><code>Convert</code> function</a>; full details about the conversion process are listed in the <a href="https://mojaveazure.github.io/seurat-disk/reference/Convert.html">manual page</a>. Then, we <a href="https://mojaveazure.github.io/seurat-disk/reference/LoadH5Seurat.html">load the h5Seurat file</a> into a <code>Seurat</code> object; for more details about loading <code>Seurat</code> objects from h5Seurat files, please see <a href="https://mojaveazure.github.io/seurat-disk/articles/h5Seurat-load.html">this vignette</a></p>
<div class="sourceCode"><html><body><pre class="r"><span class="fu"><a href="../reference/Convert.html">Convert</a></span>(<span class="st">"pbmc3k_final.h5ad"</span>, <span class="kw">dest</span> <span class="kw">=</span> <span class="st">"h5seurat"</span>, <span class="kw">overwrite</span> <span class="kw">=</span> <span class="fl">TRUE</span>)
<span class="no">pbmc3k</span> <span class="kw">&lt;-</span> <span class="fu"><a href="../reference/LoadH5Seurat.html">LoadH5Seurat</a></span>(<span class="st">"pbmc3k_final.h5seurat"</span>)
<span class="no">pbmc3k</span>
<span class="co">#&gt; An object of class Seurat </span>
<span class="co">#&gt; 13714 features across 2638 samples within 1 assay </span>
<span class="co">#&gt; Active assay: RNA (13714 features, 0 variable features)</span>
<span class="co">#&gt;  2 dimensional reductions calculated: pca, umap</span></pre></body></html></div>
</div>
  </div>

  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">

        <nav id="toc" data-toggle="toc"><h2 data-toc-skip>Contents</h2>
    </nav>
</div>

</div>



      <footer><div class="copyright">
  <p>Developed by Paul Hoffman.</p>
</div>

<div class="pkgdown">
  <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.5.1.</p>
</div>

      </footer>
</div>

  


  </body>
</html>
