image_based spatial RNA sequencing
Project description
spatioloji_s
spatioloji_s is a Python package for spatial transcriptomics analysis, purpose-built for image-based single-cell RNA sequencing data (e.g., CosMx, MERFISH, Xenium). It provides an integrated, computationally efficient workflow โ from raw data loading through quality control, processing, and spatial analysis โ all within a consistent, easy-to-use data structure.
image_based spatial RNA sequencing
- PyPI package: https://pypi.org/project/spatioloji_s/
- Free software: MIT License
- Documentation: https://spatioloji_s.readthedocs.io.
Credits
This package was created with Cookiecutter and the audreyfeldroy/cookiecutter-pypackage project template.
๐ฆ GitHub: gynecoloji/spatioloji_s
Key Features
- Custom data structure โ A
spatiolojiobject that unifies expression matrices, cell metadata, spatial coordinates, cell polygons, and FOV images under a single master cell index, ensuring automatic alignment across all components. - Efficient memory handling โ Automatic sparse/dense matrix switching (
ExpressionMatrix), and lazy-loading of FOV images with LRU caching (ImageHandler). - Quality control โ Comprehensive QC metrics for cells, genes, and FOVs with diagnostic plots.
- Processing pipeline โ Normalization, feature selection, dimensionality reduction (PCA, UMAP, t-SNE), clustering (Leiden, Louvain, K-Means), batch correction (ComBat, Harmony), and imputation (MAGIC).
- Spatial analysis โ Point-based and polygon-based spatial analysis including neighborhood enrichment, spatial statistics, Ripley's functions, and pattern analysis.
- Visualization โ Static and interactive spatial plots supporting both scatter (dot) and polygon (cell boundary) rendering, with flexible color customization.
Installation
pip install spatioloji-s
For optional dependencies (e.g., MAGIC imputation):
conda create -n spatioloji_magic python=3.10
pip install magic-impute
Quick Start
import spatioloji_s as sj
# Load from files
sp = sj.spatioloji.from_files(
polygons_path = "polygons.csv",
cell_meta_path = "cell_metadata.csv",
expression_path = "expression.npz",
fov_positions_path = "fov_positions.csv",
images_folder = "images/"
)
# Or load from a saved object
sp = sj.spatioloji.from_pickle("my_data.pkl")
# Quick summary
sj.data.utils.quick_summary(sp)
Module Overview
spatioloji/
โโโ data/ # Core data structure, QC, and utilities
โ โโโ spatioloji # Main object class
โ โโโ spatioloji_qc # Quality control pipeline
โ โโโ ExpressionMatrix / ImageHandler / SpatialData
โ
โโโ processing/ # Analysis pipeline
โ โโโ normalization # Library size, log-normalization
โ โโโ feature_selection # Highly variable genes
โ โโโ dimension_reduction # PCA, UMAP, t-SNE
โ โโโ clustering # Leiden, Louvain, K-Means
โ โโโ batch_correction # ComBat, Harmony
โ โโโ imputation # MAGIC
โ
โโโ spatial/ # Spatial analysis
โ โโโ point/ # Centroid-based analysis
โ โ โโโ graph # Spatial neighbor graphs (kNN, radius)
โ โ โโโ neighborhoods # Cell-type neighborhood enrichment
โ โ โโโ statistics # Spatial autocorrelation (Moran's I)
โ โ โโโ ripley # Ripley's K/L/G functions
โ โ โโโ patterns # Spatial pattern detection
โ โโโ polygon/ # Cell boundary-based analysis
โ โโโ graph # Contact graph from polygon intersections
โ โโโ boundaries # Boundary detection
โ โโโ morphology # Cell shape metrics
โ โโโ neighborhoods # Contact-based neighborhoods
โ โโโ statistics # Polygon spatial statistics
โ โโโ patterns # Polygon pattern analysis
โ
โโโ visualization/ # Plotting
โโโ basic_plots # QC and expression plots
โโโ spatial_plots # FOV scatter and polygon plots
โโโ interactive_plots # Interactive (Plotly-based) views
Usage Pattern
import spatioloji_s as sj
# --- QC ---
qc = sj.spatioloji_qc(sp)
qc.filter_cells()
qc.filter_genes(method='percentile')
qc.run_all(output_dir="my_qc_output/")
# --- Processing ---
sj.processing.normalize(sp)
sj.processing.select_hvg(sp)
sj.processing.pca(sp)
sj.processing.umap(sp)
sj.processing.leiden(sp)
# --- Spatial ---
sj.spatial.point.graph.build_knn_graph(sp, k=10)
sj.spatial.point.neighborhoods.neighborhood_enrichment(sp)
# --- Visualization ---
sj.visualization.spatial_plots.plot_fov(sp, fov=1, color_by='leiden')
sj.visualization.spatial_plots.plot_polygons(sp, fov=1, color_by='cell_type')
Data Structure
The spatioloji object stores all data aligned to a master cell index:
| Component | Description |
|---|---|
sp.expression |
ExpressionMatrix โ sparse/dense gene ร cell matrix |
sp.cell_meta |
pd.DataFrame โ per-cell metadata and annotations |
sp.gene_meta |
pd.DataFrame โ per-gene metadata (incl. NegProbe flags) |
sp.spatial |
SpatialData โ global x/y coordinates per cell |
sp.polygons |
GeoDataFrame โ cell boundary polygons |
sp.images |
ImageHandler โ lazy-loaded FOV images with LRU cache |
sp.fov_positions |
pd.DataFrame โ FOV global offsets |
sp.embeddings |
dict โ PCA, UMAP, t-SNE coordinates |
How spatioloji Compares
The table below benchmarks spatioloji against the most widely-used image-based spatial transcriptomics packages.
| Feature | spatioloji | Squidpy | Giotto | SpatialData (scverse) |
|---|---|---|---|---|
| Primary language | Python | Python | Python / R | Python |
| Data structure | Custom (spatioloji) |
AnnData | GiottoObject | SpatialData |
| AnnData dependency | โ None (optional import) | โ Required | โ Required | โ Required |
| Image-based ST focus | โ First-class | โ ๏ธ Partial | โ ๏ธ Partial | โ Yes |
| FOV image handling | โ Lazy load + LRU cache | โ No | โ No | โ Yes |
| Master index consistency | โ Auto-enforced | โ ๏ธ Manual | โ ๏ธ Manual | โ ๏ธ Manual |
| Auto sparse/dense matrix | โ Yes | โ No | โ No | โ No |
| Cell polygon analysis | โ Full polygon module | โ ๏ธ Limited | โ ๏ธ Limited | โ Partial |
| Contact-based neighborhoods | โ Polygon graph | โ No | โ No | โ No |
| Ripley's K/L/G | โ Yes | โ Yes | โ ๏ธ Partial | โ No |
| Batch correction | โ ComBat + Harmony | โ ๏ธ Via scanpy | โ ๏ธ Limited | โ ๏ธ Via scanpy |
| Imputation (MAGIC) | โ Yes | โ No | โ No | โ No |
| NegProbe-aware QC | โ Built-in | โ No | โ No | โ No |
| Interactive visualization | โ Yes | โ Yes | โ Yes | โ Yes |
| Polygon visualization | โ Yes | โ ๏ธ Partial | โ ๏ธ Partial | โ Partial |
โ Fully supported ยท โ ๏ธ Partial/indirect support ยท โ Not supported
Key Design Advantages
1. No AnnData lock-in. AnnData is a general-purpose format not designed around the multi-FOV, multi-image structure of image-based ST data. spatioloji's custom object natively represents FOV images, global/local coordinates, and polygon boundaries without workarounds.
2. Master index as single source of truth. All data components (expression, metadata, coordinates, polygons) are automatically aligned and validated against one master cell index. Misalignment bugs โ a common pain point in AnnData-based workflows โ are caught immediately at load time.
3. Auto sparse/dense switching. ExpressionMatrix automatically selects sparse or dense representation based on sparsity, with no user configuration needed. This directly reduces memory footprint for high-gene-count platforms like CosMx (1000+ genes).
4. Polygon-native spatial analysis. Unlike centroid-only approaches, spatioloji builds spatial graphs from actual cell boundary contacts (via GeoDataFrame + polygon intersection), enabling physically meaningful neighborhood and interaction analysis.
5. Platform-aware QC. Built-in handling of negative control probes (NegProbe) for gene filtering โ a CosMx/MERFISH-specific requirement that generic tools ignore.
License
MIT License โ see LICENSE for details.
Contributing
Contributions are welcome! See CONTRIBUTING.md for guidelines.
TODO
In Progress
- Add exclusive supportive visualization methods for spatial analysis
- Add interactive plot for simple visualization
Planned
- Add AI-supported integrated analysis for histology and gene expression
- Support Xenium native file format loader
Done โ
- Core spatioloji data structure
- QC pipeline with diagnostic plots
- Batch correction (ComBat, Harmony)
- Add spatially variable gene detection
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spatioloji_s-0.2.0.tar.gz.
File metadata
- Download URL: spatioloji_s-0.2.0.tar.gz
- Upload date:
- Size: 138.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec94438ac78ce9dc5b8a835d2159f901c7539f90ce450b1e105cf3a8bd4d4b69
|
|
| MD5 |
53398c56202dc131a7dc0ebc35fe29c4
|
|
| BLAKE2b-256 |
5190dfbed060ae347adf5c14714b9057db62ee5419783b2360123888872ccb8c
|
File details
Details for the file spatioloji_s-0.2.0-py3-none-any.whl.
File metadata
- Download URL: spatioloji_s-0.2.0-py3-none-any.whl
- Upload date:
- Size: 145.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d88c159c6b1c262b8cc95b3b59344548e351b0b44ebb94a2659686ac73fa24ba
|
|
| MD5 |
7bd5d89a10f5e29a9a585391738291b2
|
|
| BLAKE2b-256 |
080feb7e1979d3cf915c119dfff3d7984399c89fcebc39927e57efdca067ba46
|