Skip to content

Commit 2e568c6

Browse files
Merge pull request #391 from stephenwilliams22/stephen/update_10x_readers
feat: add support for non-gex data in visium hd
2 parents acf08dc + bebce8a commit 2e568c6

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/spatialdata_io/readers/visium_hd.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def visium_hd(
6060
imread_kwargs: Mapping[str, Any] = MappingProxyType({}),
6161
image_models_kwargs: Mapping[str, Any] = MappingProxyType({}),
6262
anndata_kwargs: Mapping[str, Any] = MappingProxyType({}),
63+
gex_only: bool = False,
6364
) -> SpatialData:
6465
"""Read *10x Genomics* Visium HD formatted dataset.
6566
@@ -107,6 +108,8 @@ def visium_hd(
107108
Keyword arguments for :class:`spatialdata.models.Image2DModel`.
108109
anndata_kwargs
109110
Keyword arguments for :func:`anndata.io.read_h5ad`.
111+
gex_only
112+
If `True`, only the gene expression (GEX) data will be loaded.
110113
111114
Returns
112115
-------
@@ -253,7 +256,7 @@ def _get_bins(path_bins: Path) -> list[str]:
253256
counts_file = VisiumHDKeys.FILTERED_COUNTS_FILE if filtered_counts_file else VisiumHDKeys.RAW_COUNTS_FILE
254257
adata = sc.read_10x_h5(
255258
path_bin / counts_file,
256-
gex_only=False,
259+
gex_only=gex_only,
257260
**anndata_kwargs,
258261
)
259262

@@ -346,7 +349,7 @@ def _get_bins(path_bins: Path) -> list[str]:
346349
# Integrate the segmentation data (skipped if segmentation files are not found)
347350
if cell_segmentation_files_exist:
348351
print("Found segmentation data. Incorporating cell_segmentations.")
349-
cell_adata_hd = sc.read_10x_h5(COUNT_MATRIX_PATH, gex_only=False)
352+
cell_adata_hd = sc.read_10x_h5(COUNT_MATRIX_PATH, gex_only=gex_only)
350353
cell_adata_hd.var_names_make_unique()
351354

352355
cell_shapes_gdf = _extract_geometries_from_geojson(
@@ -379,6 +382,7 @@ def _get_bins(path_bins: Path) -> list[str]:
379382
nucleus_adata_hd = _make_filtered_nucleus_adata(
380383
filtered_matrix_h5_path=FILTERED_MATRIX_2U_PATH,
381384
barcode_mappings_parquet_path=BARCODE_MAPPINGS_PATH,
385+
gex_only=gex_only,
382386
)
383387
nucleus_shapes_gdf = _extract_geometries_from_geojson(
384388
adata=nucleus_adata_hd, geojson_path=NUCLEUS_GEOJSON_PATH
@@ -756,6 +760,7 @@ def _make_filtered_nucleus_adata(
756760
barcode_mappings_parquet_path: Path,
757761
bin_col_name: str = "square_002um",
758762
aggregate_col_name: str = "cell_id",
763+
gex_only: bool = False,
759764
) -> AnnData:
760765
"""Generate a filtered AnnData object by aggregating 2um binned data based on nucleus segmentation.
761766
@@ -774,6 +779,8 @@ def _make_filtered_nucleus_adata(
774779
Column name in the barcode mappings that specifies the spatial bin (default is 'square_002um').
775780
aggregate_col_name
776781
Column name in the barcode mappings that specifies the aggregate cell ID (default is 'cell_id').
782+
gex_only
783+
If `True`, only the gene expression (GEX) data will be loaded.
777784
778785
Returns:
779786
--------
@@ -782,7 +789,7 @@ def _make_filtered_nucleus_adata(
782789
and the variables correspond to the original features from the input data.
783790
"""
784791
# Read in the necessary files
785-
adata_2um = sc.read_10x_h5(filtered_matrix_h5_path)
792+
adata_2um = sc.read_10x_h5(filtered_matrix_h5_path, gex_only=gex_only)
786793
barcode_mappings = pq.read_table(barcode_mappings_parquet_path)
787794

788795
# Filter to only include valid cell IDs that are in both nucleus and cell

0 commit comments

Comments
 (0)