Skip to content

From counts to clusters

Beginner 20 minutes Data: bundled toy_spatial.h5ad (1,000 cells, 76 genes, raw counts)

This tutorial takes a dataset from raw counts to annotated clusters: normalize, reduce, build a neighbor graph, embed, cluster, then find what makes each cluster different. It is the standard scanpy workflow (Wolf et al. 2018), run from menus, and every step lands in the analysis record so you can export it as code later.

The dataset is the toy tissue that loads when Skopia starts without a file. It is synthetic: 1,000 cells on a 2D section with two cell types, Mesen and Primor, and gene names that say which region expresses them (MesenAnt_3 is a gene of the anterior mesenchyme). You can therefore check whether the clusters are right.

Before you start

Launch Skopia with pixi run backend and pixi run dev (Install) and open http://localhost:5173. The header should read toy_spatial.h5ad, 1,000 cells, 76 genes. If another file is loaded, open File → Load… and pick backend/skopia/data/toy_spatial.h5ad from the Skopia folder.

1. Look at the data first

The plot shows X_spatial, the cells' positions in the section. Click Color next to cell_type in the Cells panel: the Primor cells fill a disc in the middle and Mesen surrounds it.

The View box at the bottom of the plot says what .X holds: raw counts here. Skopia detects the scale of every matrix and says so wherever it matters (Expression scale detection), because most of the steps below give wrong answers on the wrong scale.

2. Normalize and log-transform

Open Analyze. The modal has a tab per kind of operation; you will use Preprocess and Cells.

  1. In Preprocess, pick Normalize Total and click Run. Each cell's counts are scaled to the median total count across cells.
  2. Pick Log1p and click Run.

The View box now reads log-normalized.

Quality control

On real data, start with Filter Cells and Filter Genes in the same tab. Both draw histograms of the per-cell or per-gene QC metric with draggable cutoffs, so you can see what you remove before removing it (Preprocess). The toy data has no low-quality cells, so this tutorial skips filtering.

Filter Cells with its QC histograms

3. Highly variable genes and PCA

  1. In Preprocess, pick Highly Variable Genes and click Run. Skopia writes a boolean .var['highly_variable'] column, and the Genes panel lists it under .var columns. On this toy dataset the cell_ranger flavor keeps all 76 genes; on a whole-transcriptome dataset you would expect it to keep around 2,000.
  2. Switch to the Cells tab, pick PCA and click Run. PCA uses the highly variable genes and writes obsm['X_pca']. With 76 genes it reports PCA to 50 components over 76 genes.

PCA leaves the genes unscaled by default, following current single-cell practice (Heumos et al. 2023). The PCA page explains when to turn scaling on.

4. Neighbors, UMAP and Leiden

Still in Cells:

  1. Neighbors, Run. This builds a k-nearest-neighbor graph on the PCA (k = 15 by default) and stores it as obsp['connectivities'].
  2. UMAP, Run. UMAP (McInnes et al. 2018) lays the graph out in two dimensions as obsm['X_umap'].
  3. Leiden, Run with Resolution 0.5. Leiden (Traag et al. 2019) partitions the same graph. The modal reports Found 5 clusters, written to .obs['leiden'].

Close the modal. Switch the Embedding picker in the View box to X_umap and click Color next to leiden.

UMAP colored by Leiden cluster

Five Leiden clusters (resolution 0.5) on the toy dataset's UMAP. Cluster sizes in this run: 334, 266, 176, 134 and 90 cells.

Now switch the Embedding picker back to X_spatial. Each expression cluster occupies its own region of the section: clustering found the tissue's layout from expression alone, which is what the toy data was built to show.

The section colored by Leiden cluster

The same clusters on the tissue coordinates.

Resolution is a choice, not a fact

Re-run Leiden at 1.0 and you get more, smaller clusters. Neither answer is wrong. Pick the resolution at which the clusters mean something you can test, and say which one you used.

5. What makes each cluster different

  1. Open Analyze → Cells → DEG. In Select .obs column choose leiden, and tick all five groups.
  2. Click Run Marker Genes (5). The Find Marker Genes window opens with the five groups ticked.
  3. Leave Genes on highly_variable (76) and Top N genes per group at 5, and click Run.

Find Marker Genes after a run

Each group lists its top genes with the log2 fold change, the share of its cells expressing the gene (% in) and the share of all other cells (% out). Cluster 0's markers are MesenAnt_1 to MesenAnt_5: it is the anterior mesenchyme. The full ranking is stored in the dataset as markers_leiden, so changing Top N or the filters later re-selects from it without running the test again (Differential expression and markers).

Click Plot…. The Expression tab opens with the 25 markers as heatmap rows and the cells grouped by cluster.

Heatmap of the top five markers per cluster

Top 5 markers per Leiden cluster, cells binned within each cluster. Each block of genes is high in its own cluster only.

Save as figure in the heatmap's toolbar keeps this plot, with its inputs and settings, in the dataset (The Figures tab).

6. Name the clusters and save

With the markers in hand, give the clusters names: expand leiden in the Cells panel and double-click a cluster's label to rename it: 0 becomes Mesen anterior, and so on (Rename a label).

Then save: File → Export…, choose .h5ad, pick a folder and click Save. The file holds everything this session wrote, including the marker ranking, the saved figure and the analysis record (Save and export).

Where next