Skip to content

Commit 8a23696

Browse files
committed
a
1 parent fdf98cb commit 8a23696

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

src/pyXenium/io/partial_xenium_loader.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ def _attach_spatial(adata: AnnData, cells_zarr: Path | str) -> None:
254254
z = _first_available(root, ["cells/centroids/z", "centroids/z", "cells/centroid_z", "z"]) # optional
255255
cell_ids = _first_available(root, ["cells/cell_id", "cells/ids", "cell_ids", "ids", "barcodes"]) # optional
256256
if cell_ids is not None:
257-
cell_ids = cell_ids.astype(str)
257+
if cell_ids.dtype.kind in ("i", "u"):
258+
cell_ids = np.char.add("cell_", cell_ids.astype(str))
259+
else:
260+
cell_ids = cell_ids.astype(str)
258261

259262
if x is None or y is None:
260263
logger.warning("Could not locate centroid x/y in cells.zarr; skipping spatial attach.")
@@ -304,7 +307,10 @@ def _attach_clusters(adata: AnnData, analysis_zarr: Path | str, cluster_key: str
304307
ids = _first_available(root, ["clusters/ids", "clustering/ids", "ids", "cluster_ids"])
305308
cell_ids = _first_available(root, ["cells/cell_id", "cell_ids", "barcodes", "cells/ids"]) # optional
306309
if cell_ids is not None:
307-
cell_ids = cell_ids.astype(str)
310+
if cell_ids.dtype.kind in ("i", "u"):
311+
cell_ids = np.char.add("cell_", cell_ids.astype(str))
312+
else:
313+
cell_ids = cell_ids.astype(str)
308314

309315
if label_arr is None:
310316
logger.warning("No cluster labels found in analysis.zarr; skipping.")
@@ -346,7 +352,10 @@ def _counts_from_transcripts(transcripts_zarr: Path | str, cell_id_index: pd.Ind
346352
if gene is None or cell is None:
347353
raise KeyError("Could not locate transcript gene/cell arrays in transcripts.zarr")
348354

349-
cell = cell.astype(str)
355+
if cell.dtype.kind in ("i", "u"):
356+
cell = np.char.add("cell_", cell.astype(str))
357+
else:
358+
cell = cell.astype(str)
350359

351360
gene_names = _first_available(root, ["genes/name", "genes/names", "gene_names", "gene/name"]) # optional
352361
if gene_names is not None and gene.dtype.kind in ("i", "u"):
@@ -454,13 +463,25 @@ def _resolve_zarr(explicit: Optional[os.PathLike | str], name: str) -> Optional[
454463
continue
455464
arr = _first_available(root, ["cells/cell_id", "cell_ids", "barcodes", "cells/ids", "cell"]) # type: ignore
456465
if arr is not None:
457-
cell_ids = pd.Index(pd.Series(arr).astype(str), name="cell_id")
466+
if arr.dtype.kind in ("i", "u"):
467+
if arr.ndim == 2:
468+
ids_numeric = arr[:, 0]
469+
else:
470+
ids_numeric = arr
471+
cell_ids = pd.Index([f"cell_{int(x)}" for x in ids_numeric], name="cell_id")
472+
else:
473+
cell_ids = pd.Index(pd.Series(arr).astype(str), name="cell_id")
458474
break
459475
if cell_ids is None and transcripts_p is not None:
460476
root = _open_zarr(transcripts_p)
461477
c = _first_available(root, ["transcripts/cell_id", "transcripts/cells", "cell_id", "cell"]) # type: ignore
462478
if c is not None:
463-
cell_ids = pd.Index(pd.unique(pd.Series(c).astype(str)), name="cell_id")
479+
if c.dtype.kind in ("i", "u"):
480+
unique_nums = pd.unique(pd.Series(c))
481+
cell_ids_list = [f"cell_{int(x)}" for x in unique_nums]
482+
cell_ids = pd.Index(cell_ids_list, name="cell_id")
483+
else:
484+
cell_ids = pd.Index(pd.unique(pd.Series(c).astype(str)), name="cell_id")
464485
if cell_ids is None:
465486
raise ValueError("Could not determine cell IDs; provide MEX or any zarr with cell ids.")
466487

0 commit comments

Comments
 (0)