Skip to content

Commit

Permalink
Merge pull request #200 from scverse/fix/visium_old_data_with_header
Browse files Browse the repository at this point in the history
Fixes header bug Visium data #146
  • Loading branch information
LucaMarconato authored Aug 16, 2024
2 parents 248c7f0 + 3980b21 commit c15dca9
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/spatialdata_io/readers/visium.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import re
from collections.abc import Mapping
from functools import partial
from pathlib import Path
from types import MappingProxyType
from typing import Any
Expand Down Expand Up @@ -138,7 +137,6 @@ def visium(
if (path / "spatial" / VisiumKeys.SPOTS_FILE_1).exists() or (
tissue_positions_file is not None and str(VisiumKeys.SPOTS_FILE_1) in str(tissue_positions_file)
):
read_coords = partial(pd.read_csv, header=None, index_col=0)
tissue_positions_file = (
path / "spatial" / VisiumKeys.SPOTS_FILE_1
if tissue_positions_file is None
Expand All @@ -147,19 +145,25 @@ def visium(
elif (path / "spatial" / VisiumKeys.SPOTS_FILE_2).exists() or (
tissue_positions_file is not None and str(VisiumKeys.SPOTS_FILE_2) in str(tissue_positions_file)
):
read_coords = partial(pd.read_csv, header=0, index_col=0)
tissue_positions_file = (
path / "spatial" / VisiumKeys.SPOTS_FILE_2
if tissue_positions_file is None
else path / tissue_positions_file
)
else:
raise ValueError(f"Cannot find `tissue_positions` file in `{path}`.")
coords = read_coords(tissue_positions_file)
coords = pd.read_csv(tissue_positions_file, header=None, index_col=0)

# to handle spaceranger_version < 2.0.0, where no column names are provided
if "in_tissue" not in coords.columns:
coords.columns = ["in_tissue", "array_row", "array_col", "pxl_row_in_fullres", "pxl_col_in_fullres"]
if tissue_positions_file.name == VisiumKeys.SPOTS_FILE_1:
# spaceranger_version < 2.0.0 but header detected: https://github.com/scverse/spatialdata-io/issues/146
if "in_tissue" in coords.iloc[0].tolist():
coords = pd.read_csv(tissue_positions_file, header=0, index_col=0)
else:
assert "in_tissue" not in coords.columns
coords.columns = ["in_tissue", "array_row", "array_col", "pxl_row_in_fullres", "pxl_col_in_fullres"]
else:
assert tissue_positions_file.name == VisiumKeys.SPOTS_FILE_2
coords = pd.read_csv(tissue_positions_file, header=0, index_col=0)

adata.obs = pd.merge(adata.obs, coords, how="left", left_index=True, right_index=True)
coords = adata.obs[[VisiumKeys.SPOTS_X, VisiumKeys.SPOTS_Y]].values
Expand Down

0 comments on commit c15dca9

Please sign in to comment.