This repository accompanies the study:
Cell segmentation method governs transcript recovery and biological interpretation in sequencing-based spatial transcriptomics
James Lubkowitz, Boyi Guo, Wei Zhang, Beatrice Knudsen, Kendell Clement
The project benchmarks how cell segmentation strategy changes transcript assignment, cell definitions, and downstream biological interpretation in high-resolution sequencing-based spatial transcriptomics, with a focus on 10x Genomics Visium HD data. The code compares eleven segmentation methods spanning nucleus-based, whole-cell, and transcript-aware approaches across multiple tissues.
The tracked repository is centered on a Snakemake workflow plus manuscript figure notebooks:
analysis/pipeline.smkis the main benchmarking pipeline.analysis/bin2cell_nuclei.pyruns the Bin2Cell-based nucleus and expanded-cell workflow.analysis/STP.pyruns STP-based cytoplasmic segmentation.analysis/envs/contains conda environment definitions used by the workflow.analysis/figures/contains notebooks and exported figures used for the manuscript.data/patholog_rankings/contains expert ranking spreadsheets used in the analysis.
This is an analysis repository, not a packaged Python library. The expected usage model is: prepare a Visium HD dataset, choose a region of interest, run the Snakemake workflow in a scratch/output directory, then use the notebooks to assemble manuscript figures.
The main pipeline produces or combines the following segmentation outputs:
- Nucleus-only outputs:
stardist,cellpose,bin2cell - Cytoplasm / whole-cell outputs:
cellpose,bin2cell,STP_tuned,STP_untuned,naive_8um - Expansion outputs:
stardist_voronoi,stardist_proximity,cellpose_voronoi,cellpose_proximity
The manuscript frames the benchmark as eleven segmentation methods. In the code, some methods generate both nucleus-level and expanded-cell outputs, so the workflow materializes more than eleven analysis objects even when they derive from the same underlying method family.
The workflow expects inputs derived from a 10x Genomics Visium HD run, specifically:
- Full-resolution tissue image (
.btfor.tif) - Filtered feature-barcode matrix (
filtered_feature_bc_matrix.h5) - Tissue positions parquet (
tissue_positions.parquet) square_002umbin directoryspatial/directory containingscalefactors_json.json
The repository does not ship the Visium HD datasets. The helper script data/get.sh shows the original download locations used during manuscript development for several public 10x datasets:
- Human colon cancer
- Human lung cancer
- Mouse brain
- Mouse embryo
- Mouse small intestine
- Mouse kidney
Those paths are examples from the original HPC environment and should be adapted to your system.
This workflow was built for a Linux/HPC environment and assumes:
- Conda or Mamba
- Snakemake
- Python 3.10
- NVIDIA GPU access for most segmentation methods
- Enough memory for full-image preprocessing and segmentation
The main environment file is analysis/envs/snakemake_env.yaml. STP uses a separate environment in analysis/envs/STP_env.yaml. An explicit archival environment export is also present as analysis/envs/STP_env_explicit.txt.
conda env create -f analysis/envs/snakemake_env.yaml
conda activate snakemake_envconda env create -f analysis/envs/STP_env.yaml
conda activate STP_envBefore running the full benchmark, there are a few non-obvious constraints in the tracked code:
analysis/STP.pyimportsSTP_utilsfromanalysis/dev_files/stp/STP, so STP depends on that local vendored path rather than an installed package.
At a high level, the workflow does the following:
- Preprocess the Visium HD image and bin-level expression matrix.
- Convert transcript bin coordinates into a GeoDataFrame.
- Run multiple segmentation methods.
- Assign bins/transcripts to polygons.
- Aggregate counts per segmented cell.
- Normalize, log-transform, run PCA/neighborhood graph construction, and Leiden clustering.
- Generate per-method summary plots and cross-method benchmarking tables.
The pipeline internally computes:
- Cell counts
- UMI per cell
- Unique genes per cell
- Area per cell
- UMI density per cell area
- ROI transcript capture
- Pairwise adjusted Rand index (ARI) summaries across segmentation methods
- Resource usage summaries for runtime and memory
The workflow is configured entirely through --config arguments. The required keys are:
region_x: ROI x-range as a two-element listregion_y: ROI y-range as a two-element listimage_path: full-resolution tissue imagesquare_002um: path to thesquare_002umdirectoryspatial: path to the matchingspatialdirectorymatrix_path: path tofiltered_feature_bc_matrix.h5positions_path: path totissue_positions.parquet
snakemake -s /path/to/repo/analysis/pipeline.smk \
--use-conda \
--conda-frontend mamba \
--cores 8 \
--resources gpu=1 \
--config \
region_x="[9000, 11000]" \
region_y="[9000, 11000]" \
image_path="/path/to/Visium_HD_sample_tissue_image.btf" \
square_002um="/path/to/binned_outputs/square_002um" \
spatial="/path/to/binned_outputs/square_002um/spatial" \
matrix_path="/path/to/binned_outputs/square_002um/filtered_feature_bc_matrix.h5" \
positions_path="/path/to/binned_outputs/square_002um/spatial/tissue_positions.parquet"The pipeline produces method-level segmentation objects, clustered cell-by-gene matrices, resource-use summaries, and benchmarking plots.
normalized_img.btfgdf_coordinates.gpkgadata_processed.h5ad
*_nuclei.gpkg*_cyto.gpkgnuc_filtered_*_adata.h5adcyto_filtered_*_adata.h5ad
resource_usage/*.csvplots/overview/total_cell_calls.csvplots/overview/total_UMI_per_cell.csvplots/overview/total_area_per_cell.csvplots/overview/total_UMI_per_area_per_cell.csvplots/overview/roi_transcripts_summary.csvplots/overview/ARI_segmentation.csv
Per-method plotting rules generate files such as:
plots/nuc_<method>_cluster.pngplots/nuc_<method>_umi.pngplots/nuc_<method>_spatial_visualization.pngplots/nuc_<method>_area.pngplots/cyto_<method>_cluster.pngplots/cyto_<method>_umi.pngplots/cyto_<method>_spatial_visualization.pngplots/cyto_<method>_area.png
The manuscript figures are assembled from notebooks in analysis/figures/, including:
analysis/figures/benchmark_fig.ipynbanalysis/figures/resource_usage.ipynbanalysis/figures/spatial_visuals.ipynbanalysis/figures/ARI_fig.ipynbanalysis/figures/cell_type_fig.ipynbanalysis/figures/pathologist_ranking.ipynbanalysis/figures/goblet_nuc_localization.ipynb
- The benchmark is tuned for Visium HD-style bin-level inputs and is not presented as a general segmentation framework for arbitrary spatial platforms.
- Several methods rely upon GPU availability
- STP integration relies on a local source path instead of a separately versioned installable package.
For questions about the benchmark, manuscript, or repository, use the authorship information in the manuscript or the repository issue tracker at: