Skip to content

Commit 167a78d

Browse files
authored
Read AnnData object directly from .h5ad (#128)
* Read AnnData object directly from .h5ad * Only import anndata if read_h5ad is called * Bump version
1 parent f9d80d8 commit 167a78d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

cirro/sdk/file.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
import pandas as pd
66

7+
from typing import TYPE_CHECKING
8+
if TYPE_CHECKING:
9+
import anndata
10+
711
from cirro.cirro_client import CirroApi
812
from cirro.models.file import File
913
from cirro.sdk.asset import DataPortalAssets, DataPortalAsset
@@ -124,6 +128,19 @@ def read_csv(self, compression='infer', encoding='utf-8', **kwargs) -> pd.DataFr
124128
handle.close()
125129
return df
126130

131+
def read_h5ad(self) -> 'anndata.AnnData':
132+
"""Read an AnnData object from a file."""
133+
# Import the anndata library, and raise an error if it is not available
134+
try:
135+
import anndata as ad # noqa
136+
except ImportError:
137+
raise ImportError("The anndata library is required to read AnnData files. "
138+
"Please install it using 'pip install anndata'.")
139+
140+
# Download the file to a temporary file handle and parse the contents
141+
with BytesIO(self._get()) as handle:
142+
return ad.read_h5ad(handle)
143+
127144
def readlines(self, encoding='utf-8', compression=None) -> List[str]:
128145
"""Read the file contents as a list of lines."""
129146

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "cirro"
3-
version = "1.2.7"
3+
version = "1.2.8"
44
description = "CLI tool and SDK for interacting with the Cirro platform"
55
authors = ["Cirro Bio <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)