-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from YosefLab/inherited-methods
readwrite
- Loading branch information
Showing
9 changed files
with
371 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from importlib.metadata import version | ||
|
||
from ._core.merge import concat | ||
from ._core.read import read_h5ad, read_zarr | ||
from ._core.treedata import TreeData | ||
|
||
__version__ = version("treedata") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import MutableMapping, Sequence | ||
from pathlib import Path | ||
from typing import ( | ||
Literal, | ||
) | ||
|
||
import anndata as ad | ||
import zarr | ||
from scipy import sparse | ||
|
||
from treedata._core.aligned_mapping import AxisTrees | ||
from treedata._core.treedata import TreeData | ||
from treedata._utils import dict_to_digraph | ||
|
||
|
||
def _tdata_from_adata(tdata) -> TreeData: | ||
"""Create a TreeData object parsing attribute from AnnData uns field.""" | ||
tdata.__class__ = TreeData | ||
if "treedata_attrs" in tdata.uns.keys(): | ||
treedata_attrs = tdata.uns["treedata_attrs"] | ||
tdata._tree_label = treedata_attrs["label"] if "label" in treedata_attrs.keys() else None | ||
tdata._allow_overlap = bool(treedata_attrs["allow_overlap"]) | ||
tdata._obst = AxisTrees(tdata, 0, vals={k: dict_to_digraph(v) for k, v in treedata_attrs["obst"].items()}) | ||
tdata._vart = AxisTrees(tdata, 1, vals={k: dict_to_digraph(v) for k, v in treedata_attrs["vart"].items()}) | ||
del tdata.uns["treedata_attrs"] | ||
else: | ||
tdata._tree_label = None | ||
tdata._allow_overlap = False | ||
tdata._obst = AxisTrees(tdata, 0) | ||
tdata._vart = AxisTrees(tdata, 1) | ||
return tdata | ||
|
||
|
||
def read_h5ad( | ||
filename: str | Path = None, | ||
backed: Literal["r", "r+"] | bool | None = None, | ||
*, | ||
as_sparse: Sequence[str] = (), | ||
as_sparse_fmt: type[sparse.spmatrix] = sparse.csr_matrix, | ||
chunk_size: int = 6000, | ||
) -> TreeData: | ||
"""Read `.h5ad`-formatted hdf5 file. | ||
Parameters | ||
---------- | ||
filename | ||
File name of data file. | ||
backed | ||
If `'r'`, load :class:`~anndata.TreeData` in `backed` mode | ||
instead of fully loading it into memory (`memory` mode). | ||
If you want to modify backed attributes of the TreeData object, | ||
you need to choose `'r+'`. | ||
as_sparse | ||
If an array was saved as dense, passing its name here will read it as | ||
a sparse_matrix, by chunk of size `chunk_size`. | ||
as_sparse_fmt | ||
Sparse format class to read elements from `as_sparse` in as. | ||
chunk_size | ||
Used only when loading sparse dataset that is stored as dense. | ||
Loading iterates through chunks of the dataset of this row size | ||
until it reads the whole dataset. | ||
Higher size means higher memory consumption and higher (to a point) | ||
loading speed. | ||
""" | ||
adata = ad.read_h5ad( | ||
filename, | ||
backed=backed, | ||
as_sparse=as_sparse, | ||
as_sparse_fmt=as_sparse_fmt, | ||
chunk_size=chunk_size, | ||
) | ||
return _tdata_from_adata(adata) | ||
|
||
|
||
def read_zarr(store: str | Path | MutableMapping | zarr.Group) -> TreeData: | ||
"""Read from a hierarchical Zarr array store. | ||
Parameters | ||
---------- | ||
store | ||
The filename, a :class:`~typing.MutableMapping`, or a Zarr storage class. | ||
""" | ||
adata = ad.read_zarr(store) | ||
return _tdata_from_adata(adata) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.