Skip to content

Commit

Permalink
use delayed arrays when loading 10x h5 files
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche committed Feb 29, 2024
1 parent c7c28d9 commit e18ece7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ optional =
h5py
mudata
delayedarray
hdf5array

# Add here test requirements (semicolon/line-separated)
testing =
Expand All @@ -74,6 +75,7 @@ testing =
h5py
mudata
delayedarray
hdf5array

[options.entry_points]
# Add here console scripts like:
Expand Down
15 changes: 6 additions & 9 deletions src/singlecellexperiment/io/tenx.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def read_tenx_h5(path: str) -> SingleCellExperiment:
"""

import h5py
from scipy.sparse import csc_matrix, csr_matrix
from hdf5array import Hdf5CompressedSparseMatrix

h5 = h5py.File(path, mode="r")

Expand All @@ -68,16 +68,11 @@ def read_tenx_h5(path: str) -> SingleCellExperiment:
groups = h5["matrix"].keys()

# read the matrix
data = h5["matrix"]["data"][:]
indices = h5["matrix"]["indices"][:]
indptr = h5["matrix"]["indptr"][:]
shape = tuple(h5["matrix"]["shape"][:])

counts = None
if len(indptr) == shape[1] + 1:
counts = csc_matrix((data, indices, indptr), shape=shape)
else:
counts = csr_matrix((data, indices, indptr), shape=shape)
counts = Hdf5CompressedSparseMatrix(
path=path, group_name="matrix", by_column=True, shape=shape
)

# read features
features = None
Expand Down Expand Up @@ -106,6 +101,8 @@ def read_tenx_h5(path: str) -> SingleCellExperiment:
barcodes["barcodes"] = [x.decode("ascii") for x in h5["matrix"]["barcodes"]]
barcodes = BiocFrame(barcodes, number_of_rows=counts.shape[1])

h5.close()

return SingleCellExperiment(
assays={"counts": counts}, row_data=features, column_data=barcodes
)

0 comments on commit e18ece7

Please sign in to comment.