Skip to main content

image_based spatial RNA sequencing

Project description

spatioloji_s

PyPI version Documentation Status

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

Credits

This package was created with Cookiecutter and the audreyfeldroy/cookiecutter-pypackage project template.

๐Ÿ“ฆ GitHub: gynecoloji/spatioloji_s


Key Features

  • Custom data structure โ€” A spatioloji object 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


Download files

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

Source Distribution

spatioloji_s-0.1.0.tar.gz (138.3 kB view details)

Uploaded Source

Built Distribution

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

spatioloji_s-0.1.0-py3-none-any.whl (145.8 kB view details)

Uploaded Python 3

File details

Details for the file spatioloji_s-0.1.0.tar.gz.

File metadata

  • Download URL: spatioloji_s-0.1.0.tar.gz
  • Upload date:
  • Size: 138.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for spatioloji_s-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f49d8ed73292c7a5f951646ebc0f7c6e7d24ec973e14a203a28f1c8cbca6a6d1
MD5 6d9f13b8de2929eb5195ff8109daabe1
BLAKE2b-256 8ddd577f3ffb53a1f2b51fd0da6e988b427249166c163fe328566f0d65356143

See more details on using hashes here.

File details

Details for the file spatioloji_s-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: spatioloji_s-0.1.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

Hashes for spatioloji_s-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3ae49dff577257b80e3f7d767520365b6f98efa3a073de382b29f7250d774da
MD5 0a75e2e6733c89d2bf6bd497ace9f17d
BLAKE2b-256 b3d0c36299403ba1bd4534ae635e09c78f1c1e7df2e6a82b090e733550ea0c76

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