Skip to content

Commit dc37a8f

Browse files
committed
support MERSCOPE .parquet
1 parent 516be36 commit dc37a8f

2 files changed

Lines changed: 31 additions & 18 deletions

File tree

sainsc/io/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55

66
from ._io import (
77
ATERA_CTRLS,
8-
VIZGEN_CTRLS,
8+
MERSCOPE_CTRLS,
99
XENIUM_CTRLS,
1010
read_Atera,
1111
read_gem_file,
1212
read_gem_header,
13+
read_MERSCOPE,
1314
read_StereoSeq,
1415
read_StereoSeq_bins,
1516
read_VisiumHD,
16-
read_Vizgen,
1717
read_Xenium,
1818
)
1919

2020
__all__ = [
2121
"ATERA_CTRLS",
22-
"VIZGEN_CTRLS",
22+
"MERSCOPE_CTRLS",
2323
"XENIUM_CTRLS",
2424
"read_Atera",
2525
"read_gem_file",
2626
"read_gem_header",
27+
"read_MERSCOPE",
2728
"read_StereoSeq",
2829
"read_StereoSeq_bins",
2930
"read_VisiumHD",
30-
"read_Vizgen",
3131
"read_Xenium",
3232
]

sainsc/io/_io.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -451,30 +451,30 @@ def read_VisiumHD(
451451
# Vizgen
452452

453453
_VIZGEN_COLUMNS = {"gene": "gene", "global_x": "x", "global_y": "y"}
454-
VIZGEN_CTRLS = ["^Blank"]
455-
"""Patterns for Vizgen controls"""
454+
MERSCOPE_CTRLS = ["^Blank"]
455+
"""Patterns for MERSCOPE controls"""
456456

457457

458458
@validate_threads
459-
def read_Vizgen(
459+
def read_MERSCOPE(
460460
filepath: _PathLike,
461461
*,
462462
binsize: float = 0.5,
463-
remove_genes: Collection[str] = VIZGEN_CTRLS,
463+
remove_genes: Collection[str] = MERSCOPE_CTRLS,
464464
n_threads: int | None = None,
465465
) -> LazyKDE:
466466
"""
467-
Read a Vizgen transcripts file.
467+
Read a MERSCOPE transcripts file.
468468
469469
Parameters
470470
----------
471471
filepath : os.PathLike or str
472-
Path to the Vizgen transcripts file.
472+
Path to the MERSCOPE transcripts file.
473473
binsize : float, optional
474474
Size of each bin in um.
475475
remove_genes : collections.abc.Collection[str], optional
476476
List of regex patterns to filter the 'gene' column,
477-
:py:attr:`sainsc.io.VIZGEN_CTRLS` by default.
477+
:py:attr:`sainsc.io.MERSCOPE_CTRLS` by default.
478478
n_threads : int | None, optional
479479
Number of threads used for reading file and processing. If `None` or 0 this will
480480
default to the number of available CPUs.
@@ -483,15 +483,28 @@ def read_Vizgen(
483483
-------
484484
sainsc.LazyKDE
485485
"""
486+
filepath = Path(filepath)
487+
columns = list(_VIZGEN_COLUMNS.keys())
486488

487-
transcripts = pl.read_csv(
488-
Path(filepath),
489-
columns=list(_VIZGEN_COLUMNS.keys()),
490-
schema_overrides={"gene": pl.Categorical},
491-
n_threads=n_threads,
492-
).rename(_VIZGEN_COLUMNS)
489+
if filepath.suffix == ".csv":
490+
transcripts = pl.read_csv(
491+
filepath,
492+
columns=columns,
493+
schema_overrides={"gene": pl.Categorical},
494+
n_threads=n_threads,
495+
)
496+
elif filepath.suffix == ".parquet":
497+
# what about transcript_score (no documentation?)
498+
transcripts = (
499+
pl.scan_parquet(filepath)
500+
.select(columns)
501+
.with_columns(pl.col("gene").cast(pl.Categorical))
502+
.collect()
503+
)
504+
else:
505+
raise ValueError("Unexpected file format. Only .csv or .parquet are supported.")
493506

494-
transcripts = _filter_genes(transcripts, remove_genes)
507+
transcripts = _filter_genes(transcripts.rename(_VIZGEN_COLUMNS), remove_genes)
495508

496509
return LazyKDE.from_dataframe(
497510
transcripts, binsize=binsize, resolution=1_000, n_threads=n_threads

0 commit comments

Comments
 (0)