domains / science / spatial-transcriptomics-image-analysis / image-analysis
The two skills cover the same domain but diverge on scope and method. The first copy is a procedural recipe for Squidpy-based feature extraction and basic segmentation on Visium/H&E data. The second copy is a strategic guide for imaging spatial transcriptomics (Xenium/MERFISH/CosMx) that prioritizes segmentation strategy selection (Cellpose, StarDist, Baysor, proseg) and includes critical warnings about segmentation artifacts and spillover. The first copy lacks the strategic decision-making and artifact warnings, while the second copy lacks the specific Visium feature extraction code and H&E deconvolution steps found in the first. Combining them provides both the strategic framework for choosing a segmentation method and the concrete code for feature extraction.
| Repository | Path |
|---|---|
| FreedomIntelligence/OpenClaw-Medical-Skills | skills/spatial-transcriptomics-analysis/bioSkills/image-analysis/SKILL.md |
| GPTomics/bioSkills | spatial-transcriptomics/image-analysis/SKILL.md |
| Role | Repository | Path |
|---|---|---|
| peer | FreedomIntelligence/OpenClaw-Medical-Skills | skills/spatial-transcriptomics-analysis/bioSkills/image-analysis/SKILL.md |
| peer | GPTomics/bioSkills | spatial-transcriptomics/image-analysis/SKILL.md |
# Image Analysis for Spatial Transcriptomics
## 1. Strategic Segmentation Decision
Before processing, determine the available signal (nuclear stain, membrane stain, or transcript points) to choose the appropriate segmentation method.
- **Nuclear stain only (DAPI):** Use StarDist for round nuclei or Cellpose (nuclei mode). Note that whole-cell segmentation is inference.
- **Nuclear + Membrane stain:** Use Cellpose (2-channel) or Mesmer/DeepCell for true whole-cell segmentation.
- **Transcript points (Xenium/MERFISH):** Use Baysor or proseg for transcript-aware segmentation.
- **Visium/H&E:** Use Squidpy watershed or Cellpose for approximate boundaries.
**Warning:** Segmentation is a hypothesis, not an observation. Over/under-segmentation and spillover can fabricate false co-expression and cell-cell communication signals. Treat the resulting cell-by-gene matrix as provisional and run contamination QC.
## 2. Access Tissue Images
```python
import squidpy as sq
import scanpy as sc
import numpy as np
import matplotlib.pyplot as plt
from skimage import io, filters, segmentation
# For Visium data
library_id = list(adata.uns['spatial'].keys())[0]
img_dict = adata.uns['spatial'][library_id]['images']
hires = img_dict['hires']
lowres = img_dict['lowres']
scalef = adata.uns['spatial'][library_id]['scalefactors']
# Create ImageContainer
img = sq.im.ImageContainer(hires)
```
## 3. Segmentation
### Option A: Squidpy Watershed (Baseline)
```python
sq.im.segment(img, layer='image', method='watershed', channel=0, thresh=0.5)
seg_mask = img['segmented_watershed'].values
```
### Option B: Cellpose (Generalist)
```python
from cellpose import models
model = models.Cellpose(model_type='nuclei') # or 'cyto' for whole cell
image = img['image'].values[:, :, 0]
masks, flows, styles, diams = model.eval(image, diameter=30, channels=[0, 0])
img.add_img(masks, layer='cellpose_masks')
```
### Option C: Transcript-based (Baysor/proseg)
*Use CLI tools on the molecule table for imaging platforms like Xenium/MERFISH.*
## 4. Extract Image Features (Visium/H&E)
```python
sq.im.calculate_image_features(
adata,
img,
features=['summary', 'histogram', 'texture'],
key_added='img_features',
spot_scale=1.0,
n_jobs=4,
)
```
## 5. H&E Color Deconvolution
```python
from skimage.color import rgb2hed
hed = rgb2hed(hires)
hematoxylin = hed[:, :, 0]
eosin = hed[:, :, 1]
```
## 6. Morphological Features
```python
from skimage.measure import regionprops_table
props = regionprops_table(seg_mask, intensity_image=hires[:, :, 0], properties=['label', 'area', 'eccentricity', 'solidity', 'mean_intensity'])
```
## 7. Downstream Analysis
Combine expression and image features for clustering, but validate results against segmentation QC metrics to avoid artifacts.