GitHub
Back to all tutorials

Spatial analysis

Spatial Analysis - Xenium

This tutorial analyzes a 10x Genomics Xenium dataset in CellPilot: loading the Xenium output folder, overlaying the H&E image, exploring clusters and marker genes on the tissue, imputing genes that are not in the panel, and BANKSY spatial domains. Every command is typed in the chat box of the AI Assistant panel; no code or API key is needed. The screenshots come from CellPilot 2.1.0 running this exact dataset.

Input data

The example is the 10x Genomics Xenium FFPE Human Breast Cancer preview dataset (replicate 1, 313-gene panel). The commands below download the Xenium output bundle, the H&E image of the same section (OME-TIFF) with its alignment matrix, and a breast cancer scRNA-seq dataset used as the reference for gene imputation (about 11.3 GB in total, mostly the output bundle); you can also download them from the 10x Genomics dataset page.

mkdir -p xenium_human_breast
cd xenium_human_breast
BASE=https://cf.10xgenomics.com/samples/xenium/1.0.1/Xenium_FFPE_Human_Breast_Cancer_Rep1
SAMPLE=Xenium_FFPE_Human_Breast_Cancer_Rep1
curl -O ${BASE}/${SAMPLE}_outs.zip
curl -O ${BASE}/${SAMPLE}_he_image.ome.tif
curl -O ${BASE}/${SAMPLE}_he_imagealignment.csv
curl -O https://cf.10xgenomics.com/samples/cell-exp/7.0.1/SC3pv3_GEX_Breast_Cancer_DTC_Aggr/SC3pv3_GEX_Breast_Cancer_DTC_Aggr_count_filtered_feature_bc_matrix.h5

1. Prepare the Xenium folder

unzip ${SAMPLE}_outs.zip

This creates an outs/ folder with the standard Xenium output: experiment.xenium, cells.csv.gz (cell positions), cell_feature_matrix/ and cell_feature_matrix.h5 (gene counts), analysis/ (Xenium's clusters and UMAP) and the morphology images. Keep the structure as it is; your own Xenium output folder needs no changes. The zip file is no longer needed.

2. Load the dataset

  1. Open CellPilot.
  2. In the Data Loader & Model panel, set Data Type to Spatial, Platform to 10x Xenium and Data to Single sample.
  3. Click Browse and select the xenium_human_breast folder (the folder that contains outs/) or outs itself.
  4. Wait for the data to load.

CellPilot shows two views: the UMAP View and the Spatial View, which draws every cell at its position in the tissue, colored by cluster. The clusters and UMAP come from Xenium's analysis/ folder; cells that fail quality control are removed.

What you should see: 166,363 of 167,780 cells after quality control, 313 genes and 20 clusters.
CellPilot after loading the Xenium breast cancer dataset
After loading: UMAP View (left) and Spatial View (right).

3. Add the H&E image

  1. In the Spatial View, click Add Image.
  2. Under Histology Image (TIFF), click Select Image... and choose Xenium_FFPE_Human_Breast_Cancer_Rep1_he_image.ome.tif.
  3. Under Transformation Matrix (CSV), click Select Matrix... and choose Xenium_FFPE_Human_Breast_Cancer_Rep1_he_imagealignment.csv.
  4. Click Load Image.

The matrix (3 × 3, from Xenium's image alignment) places the H&E image on the Xenium coordinates. It was made for this OME-TIFF image: the 10x page also offers ..._he_image.tif, a version of the image with other dimensions, which this matrix does not fit (the cells and the tissue would not line up). CellPilot converts the TIFF into tiles for fast zooming; the first time this takes about a minute (less than a minute on our test computer; the tiles are kept, so loading the image again is quick). The H&E image covers the whole section, the Xenium cells the part that was measured.

The Add Histology Image dialog
The Add Histology Image dialog with the image and its matrix.
Xenium clusters on the H&E image
The clusters on the H&E image.

Your own histology image

An image from another microscope must first be registered to the Xenium coordinates, for example with Xenium Explorer's image alignment; load the exported matrix CSV with the image. See the 10x Genomics image alignment tutorial.

4. Explore clusters and marker genes

Tell me about cluster 6
Find markers for cluster 6
Plot EPCAM
Violin plot EPCAM
Dotplot EPCAM PTPRC POSTN VWF
Change color to lightgray lemonchiffon red

Gene plots color the cells on both the UMAP and the tissue. Xenium measures a panel of 313 genes, so only panel genes can be plotted directly (step 7 imputes others). The dot plot uses one marker per lineage: EPCAM (epithelial and tumor cells), PTPRC (immune cells), POSTN (fibroblasts) and VWF (endothelial cells).

Cluster 6 has 9,726 cells; its top marker genes are KLRD1, GNLY, NKG7, KLRC1, CD8A and CD8B: cytotoxic CD8 T and NK cells.
Marker genes of cluster 6
Find markers for cluster 6: the marker table (left) next to the tissue.
EPCAM on the UMAP and the tissue
Plot EPCAM: highest in the tumor epithelium.
Violin plot of EPCAM
Violin plot EPCAM.
Dot plot of four lineage markers
Dotplot EPCAM PTPRC POSTN VWF, recolored light gray–lemon chiffon–red.

5. Adjust analysis parameters

Rerun the analysis by setting min gene = 20
Rerun umap with min dist = 0.4
Recluster the cells using resolution = 2.0

The settings work as for scRNA-seq data; the spatial positions stay with the cells. With 166,363 cells, re-running the analysis or the UMAP takes several minutes.

With min gene = 20, 160,255 cells remain; at resolution 2.0, how many clusters are there answers 27 clusters. On our test computer, re-running the analysis and the UMAP took about 6 minutes each, re-clustering 2 minutes.

6. Identify cell types

Annotate all clusters
yes

CellPilot compares every cluster with built-in reference atlases on the genes of the panel and names the most similar cell type; for this section it recognizes breast cancer tissue and uses the breast cancer and breast atlases. yes renames the clusters; clusters with the same name are merged. With names, the region questions below answer in cell types.

The organ is recognized as breast cancer and breast; the 27 clusters become tumor-like epithelium (BRCA cycling), luminal hormone-sensing cells (LumHS), cancer-associated fibroblasts (iCAF, myCAF), endothelial cells, macrophages (LAM2), CD8 and CD4 T cells, plasma cells, B cells and dendritic cells; names ending in ? are not clear-cut.
Clusters named with cell types
After yes: cell type names on the UMAP and the tissue.

You can change cluster names at any time, for example if an automatic name looks wrong: Rename cluster 0 to Luminal HS.

7. Impute genes that are not in the panel

Impute COL1A1 expression
Impute KRT19 expression
Impute VIM expression

COL1A1, KRT19 and VIM are not in the Xenium panel. CellPilot predicts them from a scRNA-seq reference with SpaGE: it aligns the reference and the spatial cells on the genes they share and transfers the missing gene from each cell's nearest reference cells. The first request asks for the reference: click Select scRNA-seq Reference and choose SC3pv3_GEX_Breast_Cancer_DTC_Aggr_count_filtered_feature_bc_matrix.h5. Later requests use the same reference; to change it, say impute VIM with another reference. Genes that are in the panel (ERBB2, ESR1, PGR) are measured and need no imputation.

Each gene takes about 2 minutes for these 160,255 cells. KRT19 is predicted in the tumor epithelium, COL1A1 in the stroma, VIM in stromal and immune areas.
Imputed KRT19 on the UMAP and the tissue
Impute KRT19 expression: predicted KRT19 in the tumor epithelium (the reference was selected for COL1A1 and reused).

8. Spatial domains with BANKSY

Run BANKSY region segmentation
Show regions
What cell types are in region 4?
Rename region 4 to tumor

BANKSY finds tissue domains from each cell's own expression and that of its neighbors. The regions can be shown, described by their cell types, renamed, and used for marker analysis.

BANKSY takes about 10 minutes for these 160,255 cells. It finds 8 regions (numbered from 0). Region 4 holds 21,508 cells, 98.9% of them BRCA cycling (tumor-like epithelium): the solid tumor, so it is renamed tumor. Region 2, the largest (40% of the cells), is stroma with immune cells: fibroblasts, T cells and macrophages. The same data and settings give the same regions in every run; with other settings or your own data, check each region's cell types (What cell types are in region N?) before naming it.
BANKSY spatial domains
Show regions: the BANKSY domains on the UMAP and the tissue, with region 4 named tumor.

9. Save and revisit results

CellPilot saves the analysis (clusters and names) as cellpilot_results.json in the data folder. When you load the same folder again, CellPilot asks whether to load the previous results.