Skip to content

Commit

Permalink
Convert to and from h5ad.
Browse files Browse the repository at this point in the history
  • Loading branch information
orenbenkiki committed Mar 23, 2024
1 parent 10273a5 commit 7a584a7
Show file tree
Hide file tree
Showing 31 changed files with 1,478 additions and 81 deletions.
2 changes: 2 additions & 0 deletions daf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
# pylint: disable=wildcard-import,unused-wildcard-import

from .julia_import import * # isort: skip
from .generic import * # isort: skip
from .storage_types import * # isort: skip
from .operations import * # isort: skip
from .queries import * # isort: skip
Expand All @@ -60,3 +61,4 @@
from .copies import * # isort: skip
from .adapters import * # isort: skip
from .concat import * # isort: skip
from .anndata_format import * # isort: skip
56 changes: 56 additions & 0 deletions daf/anndata_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Import/export ``Daf`` data from/to ``AnnData``. See the Julia
`documentation <https://tanaylab.github.io/Daf.jl/v0.1.0/anndata_format.html>`__ for details.
"""

__all__ = ["h5ad_as_daf", "daf_as_h5ad"]

from typing import Optional

from .data import DafReader
from .formats import MemoryDaf
from .generic import JL_ABNORMAL_HANDLER
from .generic import AbnormalHandler
from .julia_import import jl


def h5ad_as_daf(
h5ad: str,
*,
name: Optional[str] = None,
obs_is: Optional[str] = None,
var_is: Optional[str] = None,
X_is: Optional[str] = None,
unsupported_handler: AbnormalHandler = "WarnHandler",
) -> MemoryDaf:
"""
View ``AnnData`` as a ``Daf`` data set, specifically using a ``MemoryDaf``. See the Julia
`documentation <https://tanaylab.github.io/Daf.jl/v0.1.0/anndata_format.html#anndata_as_daf>`__ for details.
Note that you only pass an ``h5ad`` path, since the Julia ``AnnData`` object comes from the ``Muon.jl`` package and
is not compatible with the Python ``anndata`` object.
"""
return MemoryDaf(
jl.Daf.anndata_as_daf(
h5ad,
name=name,
obs_is=obs_is,
var_is=var_is,
X_is=X_is,
unsupported_handler=JL_ABNORMAL_HANDLER[unsupported_handler],
)
)


def daf_as_h5ad(
daf: DafReader, *, obs_is: Optional[str] = None, var_is: Optional[str] = None, X_is: Optional[str] = None, h5ad: str
) -> None:
"""
View the ``Daf`` data set as ``AnnData``. See the Julia
`documentation <https://tanaylab.github.io/Daf.jl/v0.1.0/anndata_format.html#Daf.AnnDataFormat.daf_as_anndata>`__
for details.
Note this just creates the ``h5ad`` file. We do not return the ``AnnData`` object, because it is a Julia
(``Muon.jl``) ``AnnData`` object, which is **not** a Python ``anndata`` ``AnnData`` object.
"""
jl.daf_as_anndata(daf, obs_is=obs_is, var_is=var_is, X_is=X_is, h5ad=h5ad)
6 changes: 4 additions & 2 deletions daf/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ class MemoryDaf(DafWriter):
`documentation <https://tanaylab.github.io/Daf.jl/v0.1.0/memory_format.html>`__ for details.
"""

def __init__(self, *, name: str = "memory") -> None:
super().__init__(jl.Daf.MemoryDaf(name=name))
def __init__(self, jl_obj: Optional[jl.MemoryDaf] = None, *, name: str = "memory") -> None:
if jl_obj is None:
jl_obj = jl.Daf.MemoryDaf(name=name)
super().__init__(jl_obj)


class FilesDaf(DafWriter):
Expand Down
21 changes: 21 additions & 0 deletions daf/generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Types that arguably should belong in a more general-purpose package. See the Julia
`documentation <https://tanaylab.github.io/Daf.jl/v0.1.0/generic.html>`__ for details.
"""

from typing import Literal

from .julia_import import jl

__all__ = ["AbnormalHandler"]


#: The action to take when encountering an "abnormal" (but recoverable) operation. See the Julia
#: `documentation <https://tanaylab.github.io/Daf.jl/v0.1.0/generic.html#Daf.Generic.AbnormalHandler>`__ for details.
AbnormalHandler = Literal["IgnoreHandler"] | Literal["WarnHandler"] | Literal["ErrorHandler"]

JL_ABNORMAL_HANDLER = {
"IgnoreHandler": jl.Daf.Generic.IgnoreHandler,
"WarnHandler": jl.Daf.Generic.WarnHandler,
"ErrorHandler": jl.Daf.Generic.ErrorHandler,
}
3 changes: 2 additions & 1 deletion daf/julia_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@
jl.seval("import DataFrames")
jl.seval("import HDF5")
jl.seval("import LinearAlgebra")
jl.seval("import Muon")
jl.seval("import NamedArrays")
jl.seval("import SparseArrays")
jl.seval("import PythonCall")
jl.seval("import SparseArrays")


class UndefInitializer:
Expand Down
4 changes: 3 additions & 1 deletion docs/API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ API

julia_import
data
storage_types
queries
operations
formats
views
copies
adapters
concat
storage_types
anndata_format
generic
5 changes: 5 additions & 0 deletions docs/anndata_format.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
anndata_format
==============

.. automodule:: daf.anndata_format
:Members:
44 changes: 38 additions & 6 deletions docs/v0.1.0/html/API.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
</a>
</li>

<li class="toctree-l2">
<a class="reference internal" href="storage_types.html">storage_types
</a>
</li>

<li class="toctree-l2">
<a class="reference internal" href="queries.html">queries
</a>
Expand Down Expand Up @@ -161,7 +166,7 @@
</li>

<li class="toctree-l2">
<a class="reference internal" href="storage_types.html">storage_types
<a class="reference internal" href="anndata_format.html">anndata_format
</a>
</li>

Expand Down Expand Up @@ -898,6 +903,33 @@ <h1>API

</li>

<li class="toctree-l1">
<a class="reference internal" href="storage_types.html">storage_types
</a>
<ul>

<li class="toctree-l2">
<a class="reference internal" href="storage_types.html#daf.storage_types.StorageNumber">
<code class="docutils literal notranslate">
<span class="pre">StorageNumber
</span>
</code>
</a>
</li>

<li class="toctree-l2">
<a class="reference internal" href="storage_types.html#daf.storage_types.StorageScalar">
<code class="docutils literal notranslate">
<span class="pre">StorageScalar
</span>
</code>
</a>
</li>

</ul>

</li>

<li class="toctree-l1">
<a class="reference internal" href="queries.html">queries
</a>
Expand Down Expand Up @@ -1565,23 +1597,23 @@ <h1>API
</li>

<li class="toctree-l1">
<a class="reference internal" href="storage_types.html">storage_types
<a class="reference internal" href="anndata_format.html">anndata_format
</a>
<ul>

<li class="toctree-l2">
<a class="reference internal" href="storage_types.html#daf.storage_types.StorageNumber">
<a class="reference internal" href="anndata_format.html#daf.anndata_format.h5ad_as_daf">
<code class="docutils literal notranslate">
<span class="pre">StorageNumber
<span class="pre">h5ad_as_daf()
</span>
</code>
</a>
</li>

<li class="toctree-l2">
<a class="reference internal" href="storage_types.html#daf.storage_types.StorageScalar">
<a class="reference internal" href="anndata_format.html#daf.anndata_format.daf_as_h5ad">
<code class="docutils literal notranslate">
<span class="pre">StorageScalar
<span class="pre">daf_as_h5ad()
</span>
</code>
</a>
Expand Down
171 changes: 171 additions & 0 deletions docs/v0.1.0/html/_modules/daf/anndata_format.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>daf.anndata_format &mdash; Daf 0.1.0 documentation</title>
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="../../_static/css/theme.css?v=19f00094" />


<!--[if lt IE 9]>
<script src="../../_static/js/html5shiv.min.js"></script>
<![endif]-->

<script src="../../_static/jquery.js?v=5d32c60e"></script>
<script src="../../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="../../_static/documentation_options.js?v=01f34227"></script>
<script src="../../_static/doctools.js?v=888ff710"></script>
<script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
</head>

<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >



<a href="../../index.html" class="icon icon-home">
Daf
<img src="../../_static/logo.svg" class="logo" alt="Logo"/>
</a>
<div class="version">
0.1.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../readme.html">README</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../API.html">API</a></li>
</ul>

</div>
</div>
</nav>

<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../../index.html">Daf</a>
</nav>

<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item"><a href="../index.html">Module code</a></li>
<li class="breadcrumb-item active">daf.anndata_format</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">

<h1>Source code for daf.anndata_format</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Import/export ``Daf`` data from/to ``AnnData``. See the Julia</span>
<span class="sd">`documentation &lt;https://tanaylab.github.io/Daf.jl/v0.1.0/anndata_format.html&gt;`__ for details.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;h5ad_as_daf&quot;</span><span class="p">,</span> <span class="s2">&quot;daf_as_h5ad&quot;</span><span class="p">]</span>

<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>

<span class="kn">from</span> <span class="nn">.data</span> <span class="kn">import</span> <span class="n">DafReader</span>
<span class="kn">from</span> <span class="nn">.formats</span> <span class="kn">import</span> <span class="n">MemoryDaf</span>
<span class="kn">from</span> <span class="nn">.generic</span> <span class="kn">import</span> <span class="n">JL_ABNORMAL_HANDLER</span>
<span class="kn">from</span> <span class="nn">.generic</span> <span class="kn">import</span> <span class="n">AbnormalHandler</span>
<span class="kn">from</span> <span class="nn">.julia_import</span> <span class="kn">import</span> <span class="n">jl</span>


<div class="viewcode-block" id="h5ad_as_daf">
<a class="viewcode-back" href="../../anndata_format.html#daf.anndata_format.h5ad_as_daf">[docs]</a>
<span class="k">def</span> <span class="nf">h5ad_as_daf</span><span class="p">(</span>
<span class="n">h5ad</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
<span class="o">*</span><span class="p">,</span>
<span class="n">name</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">obs_is</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">var_is</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">X_is</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">unsupported_handler</span><span class="p">:</span> <span class="n">AbnormalHandler</span> <span class="o">=</span> <span class="s2">&quot;WarnHandler&quot;</span><span class="p">,</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">MemoryDaf</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> View ``AnnData`` as a ``Daf`` data set, specifically using a ``MemoryDaf``. See the Julia</span>
<span class="sd"> `documentation &lt;https://tanaylab.github.io/Daf.jl/v0.1.0/anndata_format.html#anndata_as_daf&gt;`__ for details.</span>

<span class="sd"> Note that you only pass an ``h5ad`` path, since the Julia ``AnnData`` object comes from the ``Muon.jl`` package and</span>
<span class="sd"> is not compatible with the Python ``anndata`` object.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">MemoryDaf</span><span class="p">(</span>
<span class="n">jl</span><span class="o">.</span><span class="n">Daf</span><span class="o">.</span><span class="n">anndata_as_daf</span><span class="p">(</span>
<span class="n">h5ad</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span>
<span class="n">obs_is</span><span class="o">=</span><span class="n">obs_is</span><span class="p">,</span>
<span class="n">var_is</span><span class="o">=</span><span class="n">var_is</span><span class="p">,</span>
<span class="n">X_is</span><span class="o">=</span><span class="n">X_is</span><span class="p">,</span>
<span class="n">unsupported_handler</span><span class="o">=</span><span class="n">JL_ABNORMAL_HANDLER</span><span class="p">[</span><span class="n">unsupported_handler</span><span class="p">],</span>
<span class="p">)</span>
<span class="p">)</span></div>



<div class="viewcode-block" id="daf_as_h5ad">
<a class="viewcode-back" href="../../anndata_format.html#daf.anndata_format.daf_as_h5ad">[docs]</a>
<span class="k">def</span> <span class="nf">daf_as_h5ad</span><span class="p">(</span>
<span class="n">daf</span><span class="p">:</span> <span class="n">DafReader</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">obs_is</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">var_is</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">X_is</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">h5ad</span><span class="p">:</span> <span class="nb">str</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> View the ``Daf`` data set as ``AnnData``. See the Julia</span>
<span class="sd"> `documentation &lt;https://tanaylab.github.io/Daf.jl/v0.1.0/anndata_format.html#Daf.AnnDataFormat.daf_as_anndata&gt;`__</span>
<span class="sd"> for details.</span>

<span class="sd"> Note this just creates the ``h5ad`` file. We do not return the ``AnnData`` object, because it is a Julia</span>
<span class="sd"> (``Muon.jl``) ``AnnData`` object, which is **not** a Python ``anndata`` ``AnnData`` object.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">jl</span><span class="o">.</span><span class="n">daf_as_anndata</span><span class="p">(</span><span class="n">daf</span><span class="p">,</span> <span class="n">obs_is</span><span class="o">=</span><span class="n">obs_is</span><span class="p">,</span> <span class="n">var_is</span><span class="o">=</span><span class="n">var_is</span><span class="p">,</span> <span class="n">X_is</span><span class="o">=</span><span class="n">X_is</span><span class="p">,</span> <span class="n">h5ad</span><span class="o">=</span><span class="n">h5ad</span><span class="p">)</span></div>

</pre></div>

</div>
</div>
<footer>

<hr/>

<div role="contentinfo">
<p>&#169; Copyright 2023-2024 Weizmann Institute of Science.</p>
</div>

Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.


</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>

</body>
</html>
Loading

0 comments on commit 7a584a7

Please sign in to comment.