Skip to content

Commit 74eb55d

Browse files
authored
Update zarr io (#66)
* Update `write_zarr` argument names and type hints to accommodate MuData and TreeData. * Refactor `read_zarr` to support multiple backends and update docstrings
1 parent 4d3c2b6 commit 74eb55d

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

src/fancypackage/io/_zarr.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1+
from __future__ import annotations
2+
13
import warnings
24
from pathlib import Path
5+
from typing import Literal, TYPE_CHECKING
36

47
import zarr
58

69
import anndata as ad
710
from anndata import AnnData
811

12+
if TYPE_CHECKING:
13+
from mudata import MuData
14+
from treedata import TreeData
15+
916

10-
def write_zarr(adata: AnnData, path: Path) -> None:
11-
"""Write AnnData to hierarchical Zarr array zip store.
17+
def write_zarr(data: AnnData | MuData | TreeData, path: Path) -> None:
18+
"""Write AnnData-like object to hierarchical Zarr array zip store.
1219
1320
Parameters
1421
----------
1522
adata
16-
AnnData to save.
23+
AnnData-like object to save; supports AnnData, MuData and TreeData.
1724
path
1825
Filename of Zarr store.
1926
@@ -24,24 +31,36 @@ def write_zarr(adata: AnnData, path: Path) -> None:
2431
with warnings.catch_warnings():
2532
warnings.simplefilter("ignore", UserWarning)
2633
store = zarr.storage.ZipStore(path, mode="w")
27-
adata.write_zarr(store=store)
34+
data.write_zarr(store=store)
2835
store.close()
2936

3037

31-
def read_zarr(path: Path) -> AnnData:
32-
"""Read hierarchical Zarr array zip store to AnnData.
38+
def read_zarr(path: Path, backend: Literal["anndata", "mudata", "treedata"] = "anndata") -> AnnData | MuData | TreeData:
39+
"""Read hierarchical Zarr array zip store to AnnData-like object.
3340
3441
Parameters
3542
----------
3643
path
3744
Filename of Zarr store.
45+
backend
46+
Backend to use for reading the Zarr store. Supported backends are "anndata" (default), "mudata" and "treedata".
3847
3948
Returns
4049
-------
41-
Read AnnData object.
50+
The read AnnData-like object (Anndata, MuData, or TreeData).
4251
"""
4352
store = zarr.storage.ZipStore(path, mode="r")
44-
adata = ad.io.read_zarr(store)
53+
54+
if backend == "anndata":
55+
data = ad.read_zarr(store)
56+
elif backend == "mudata":
57+
import mudata as md
58+
59+
data = md.read_zarr(store)
60+
elif backend == "treedata":
61+
import treedata as td
62+
63+
data = td.read_zarr(store)
4564
store.close()
4665

47-
return adata
66+
return data

0 commit comments

Comments
 (0)