Skip to main content

Transferable spatial omics deconvolution

Project description

SpaRank

Transferable spatial omics deconvolution.

SpaRank turns expression profiles into rank-ordered token sequences and runs them through a Transformer encoder to predict cell-type composition at each spatial spot. Trained once on a multi-context single-cell reference, a single model deploys directly to spatial sections spanning contexts without per-slice retraining. The same pipeline handles unimodal (RNA), multimodal (RNA + ADT, …), and context-conditioned setups.

PyPI Docs License

Installation

Requires Python >= 3.10

Install torch

# Install PyTorch matching your CUDA driver.
# Example
pip install torch==2.9.1+cu128 --index-url https://download.pytorch.org/whl/cu128

From PyPI

pip install sparank

From source

git clone https://github.com/XiHuYan/SpaRank.git
cd SpaRank
pip install -e .

Quick start

Unimodal RNA

from sparank import SpaRank, ExpConfig, ModalityConfig, SimulationConfig

cfg = ExpConfig(
    modalities=[ModalityConfig(name="rna", top_k=500)],
    simulation=SimulationConfig(       # controls pseudo-spot generation for training
        total_samples=100_000,
        cells_mean=10,                 # expected cells per spot — match your section resolution
        cells_std=5,
        cell_sample_method="gaussian",
        batch_key="Tissue",            # group cells by Tissue; simulate pseudo-spots within each, then pool (can differ from context_key)
    ),
    celltype_key="cell_type",
    batch_key="batch",                # group cells by batch; detect marker genes within each group
    context_key="Tissue",             # label cells by Tissue; embed the label into the prediction head
    epochs=20,
)

sr = SpaRank(cfg, save_dir="./output")
sr.register_modality("rna", adata_sc_rna)
sr.prepare()                           
# or pass markers directly:
# sr.prepare(marker_features={"rna": prior_gene_list})
sr.fit()
sr.save()

# Deploy to a spatial section — no retraining needed
df = sr.predict({"rna": adata_sp_rna})

Multimodal RNA + ADT

cfg = ExpConfig(
    modalities=[
        ModalityConfig(name="rna", top_k=500),
        ModalityConfig(name="adt", top_k=200),
    ],
    simulation=SimulationConfig(       # controls pseudo-spot generation for training
        total_samples=100_000,
        cells_mean=10,                 # expected cells per spot — match your section resolution
        cells_std=5,
        cell_sample_method="gaussian",
        batch_key="Tissue",            # group cells by Tissue; simulate pseudo-spots within each, then pool (can differ from context_key)
    ),
    celltype_key="cell_type",
    batch_key="batch",           
    context_key="Tissue",          
    fusion_type="sigmoid_gate",    # "concat" | "gate" | "joint_gate" | "sigmoid_gate" | "attention"
    epochs=20,
)

sr = SpaRank(cfg, save_dir="./output")
sr.register_modality("rna", adata_sc_rna)
sr.register_modality("adt", adata_sc_adt)
sr.prepare()
sr.fit()

# Predict proportions
proportions_df = sr.predict(
    {"rna": adata_sp_rna, "adt": adata_sp_adt},
)

Documentation

Full documentation, API reference, and tutorials: https://sparank.readthedocs.io/en/latest.html.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sparank-0.1.7.tar.gz (36.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sparank-0.1.7-py3-none-any.whl (40.2 kB view details)

Uploaded Python 3

File details

Details for the file sparank-0.1.7.tar.gz.

File metadata

  • Download URL: sparank-0.1.7.tar.gz
  • Upload date:
  • Size: 36.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for sparank-0.1.7.tar.gz
Algorithm Hash digest
SHA256 ffbd6a238df6873c2b06225426367a4c22f92e2bd16ea47e960b4733e0287e84
MD5 aada3ad75592d0447f356133f5201298
BLAKE2b-256 d681796127cc736f2a52fd5035f7d4ddc869cfcec24e41d27ace6a4e7cec9a21

See more details on using hashes here.

File details

Details for the file sparank-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: sparank-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 40.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for sparank-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 ef939e66f0a911829cdd97d2f9cdf6bdd6e57376427efe75d8be1690dbc73756
MD5 49ea5d3dd137277ac3a1d1c883a21e4a
BLAKE2b-256 3e3ff9a9c01275f06b904132db2b39100892727eef18659f75fa240e9ef2715c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page