Skip to content

Commit dab8c22

Browse files
Merge pull request #292 from cgeyskens/issue288
Use scanpy for reading matrixes
2 parents 86e789f + 8c5ba82 commit dab8c22

3 files changed

Lines changed: 13 additions & 152 deletions

File tree

src/spatialdata_io/readers/_utils/_read_10x_h5.py

Lines changed: 0 additions & 144 deletions
This file was deleted.

src/spatialdata_io/readers/_utils/_utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import os
44
from pathlib import Path
5-
from typing import TYPE_CHECKING, Any, Union
5+
from typing import TYPE_CHECKING, Any
66

7-
from anndata import AnnData
7+
import scanpy as sc
88
from anndata.io import read_text
99
from h5py import File
1010
from spatialdata._logging import logger
1111

12-
from spatialdata_io.readers._utils._read_10x_h5 import _read_10x_h5
13-
1412
if TYPE_CHECKING:
1513
from collections.abc import Mapping
1614

@@ -29,7 +27,7 @@ def _read_counts(
2927
) -> tuple[AnnData, str]:
3028
path = Path(path)
3129
if counts_file.endswith(".h5"):
32-
adata: AnnData = _read_10x_h5(path / counts_file, **kwargs)
30+
adata: AnnData = sc.read_10x_h5(path / counts_file, **kwargs)
3331
with File(path / counts_file, mode="r") as f:
3432
attrs = dict(f.attrs)
3533
if library_id is None:

src/spatialdata_io/readers/xenium.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
from typing import TYPE_CHECKING, Any
1212

1313
import dask.array as da
14+
import h5py
1415
import numpy as np
1516
import packaging.version
1617
import pandas as pd
1718
import pyarrow.compute as pc
1819
import pyarrow.parquet as pq
20+
import scanpy as sc
1921
import tifffile
2022
import zarr
2123
from dask.dataframe import read_parquet
@@ -35,8 +37,7 @@
3537

3638
from spatialdata_io._constants._constants import XeniumKeys
3739
from spatialdata_io._docs import inject_docs
38-
from spatialdata_io._utils import deprecation_alias, zarr_open
39-
from spatialdata_io.readers._utils._read_10x_h5 import _read_10x_h5
40+
from spatialdata_io._utils import deprecation_alias
4041
from spatialdata_io.readers._utils._utils import _initialize_raster_models_kwargs, _set_reader_metadata
4142

4243
if TYPE_CHECKING:
@@ -676,7 +677,13 @@ def _get_tables_and_circles(
676677
gex_only: bool,
677678
cells_zarr_ctx: _XeniumCells,
678679
) -> tuple[AnnData, GeoDataFrame]:
679-
adata = _read_10x_h5(path / XeniumKeys.CELL_FEATURE_MATRIX_FILE, gex_only=gex_only)
680+
adata = sc.read_10x_h5(path / XeniumKeys.CELL_FEATURE_MATRIX_FILE, gex_only=gex_only)
681+
# Undo fixed-point scaling factor applied to Xenium Protein data stored in HDF5.
682+
with h5py.File(path / XeniumKeys.CELL_FEATURE_MATRIX_FILE, "r") as f:
683+
if "protein_scaling_factor" in f.attrs:
684+
protein_feats = np.flatnonzero(adata.var["feature_types"] == "Protein Expression")
685+
if len(protein_feats) > 0:
686+
adata.X[:, protein_feats] /= f.attrs["protein_scaling_factor"]
680687
# get_cell_metadata decodes cell_id and cross-checks it against cells.zarr.zip
681688
metadata = cells_zarr_ctx.get_cell_metadata(path)
682689
_assert_arrays_equal_sampled(metadata[XeniumKeys.CELL_ID].astype(str).values, adata.obs_names.values)

0 commit comments

Comments
 (0)