This project processes spatial transcriptomics data from a .zarr store and prepares it for downstream clustering and spatial analysis workflows.
The pipeline focuses on:
- Spatial coordinate extraction
- Gene expression preprocessing
- PCA-based dimensionality reduction
- Clustering analysis and validation
- Spatial embedding preparation
- Python
- Scanpy
- SpatialData
- AnnData
- NumPy
- Pandas
- Scikit-learn
- Shapely
- Leidenalg
Spatial transcriptomics data was loaded from a .zarr store using the SpatialData framework.
spatialdata.read_zarr()The dataset contained:
- Cell geometries
- Gene expression counts
- Spatial metadata
Cell geometries were stored as WKB (Well-Known Binary) objects.
Using shapely, the geometries were decoded and converted into centroid coordinates:
xy
This produced a structured spatial representation for each cell.
| File | Description |
|---|---|
exports/cell_coordinates.csv |
Cell IDs with centroid coordinates |
Gene expression counts were extracted from the AnnData table inside the Zarr store.
- Cells: 2,389
- Genes: 268
The sparse matrix was converted into a dense tabular format for downstream analysis.
| File | Description |
|---|---|
exports/count_matrix.csv |
Gene expression matrix |
To prepare the data for clustering, I performed a systematic PCA analysis instead of selecting the number of components arbitrarily.
I evaluated cumulative variance explained across multiple thresholds:
| Variance Threshold | Components Required |
|---|---|
| 50% | 43 |
| 75% | 108 |
| 90% | 171 |
| 95% | 203 |
To determine the optimal PCA dimension, I analyzed:
- Scree plots
- Eigenvalue decay
- Log-scale variance plots
- Cumulative explained variance
- Clustering stability across dimensions
I tested clustering performance across multiple PCA dimensions:
| PCA Components | Variance Captured | Clusters Found |
|---|---|---|
| 5 | 24.2% | 8 |
| 10 | 30.3% | 8 |
| 15 | 34.4% | 8 |
| 30 | 43.6% | 7 |
| 50 | 53.6% | 6 |
I selected:
n_components = 15- Captured meaningful biological variation
- Produced stable cluster separation
- Reduced noise amplification
- Avoided over-clustering
- Improved downstream UMAP and Leiden clustering efficiency
This selection balanced:
- Information retention
- Computational efficiency
- Biological interpretability
rather than relying on arbitrary PCA dimensions.
The processed embeddings were used for:
- UMAP visualization
- Leiden clustering
- Spatial embedding analysis
This project demonstrates:
- Working with large spatial omics datasets
- Spatial transcriptomics preprocessing
- Dimensionality reduction and clustering validation
- Data-driven PCA component selection
- Integration of spatial and gene expression data for downstream ML workflows