A clustered heatmap of the top 40 genes: patterns in the response
Turn the 40 strongest steroid responders into a clustered heatmap in RBAi and read it like a researcher: which samples group together, which genes move together, and which donor responds differently. In our run the samples split cleanly by treatment and the genes into 31 that rise and 9 that fall.
A list of 4,030 genes is hard to see. A heatmap turns a table of numbers into a grid of colours, with one row per gene and one column per sample, so patterns jump out: genes that rise together, samples that look alike, a donor that behaves differently. Add clustering, which sorts the rows and columns so that similar ones sit next to each other, and the grid starts to tell a story. Clustered heatmaps have been the signature figure of gene-expression papers since the late 1990s.
In this lesson you will draw one for the 40 strongest responders from Lesson 3 and read it the way a researcher does. Carry on in the same chat as Lessons 2 and 3. Every prompt goes into the RBAi main chat.
What you will make

The 40 most significant dexamethasone-responsive genes in eight samples. Each cell's colour is the gene's variance-stabilised expression in that sample, as a z-score: how far above (orange) or below (teal) that gene's own average across the eight samples, in standard deviations. Trees join the most similar samples (top) and genes (left) first. The first split of the samples is by treatment; the donor (blue bar) matters less. The genes split into 31 that rise with the steroid, from STOM to PDPN, and 9 that fall, from WNT2 to ADAM12. GEO GSE52778, drawn by RBAi.
1. Draw the heatmap Main chat
Make a clustered heatmap of the 40 genes with the smallest adjusted p-values. Use DESeq2's variance-stabilised values and scale each gene's row to a z-score, so the colour shows how far each sample sits above or below that gene's own average. Cluster both the genes and the samples with Euclidean distance and complete linkage, and draw the trees. Colour the cells teal below the average, white at the average and orange above it. Add a colour bar over the samples for treatment (untreated grey, dexamethasone orange) and one for donor in four shades of blue, put the gene symbols on the rows, use a white background, make it about 16 cm wide and draw it in Python so it can be edited later. Then tell me how the samples group, how many genes fall in each of the two main gene clusters, and which of the 40 genes go down with dexamethasone.
A z-score rescales a row so that its average is 0 and its spread (the standard deviation) is 1. That makes a gene with 3,000 reads and a gene with 300 comparable: each is coloured by how it varies across the samples, not by how busy it is. Hierarchical clustering builds a tree by joining the two most similar rows (or columns), then the next two, and so on. The Euclidean distance is the straight-line distance between two rows' values, and complete linkage measures the distance between two groups by their two most different members. The tree drawn beside a heatmap is a dendrogram: the lower two branches join, the more alike they are.
What you should see: a heatmap like the one above and RBAi's reading of it. The samples split into an untreated group and a treated group. The genes form two main clusters, of 31 and 9 genes: the 31 are higher with dexamethasone and the 9 lower. The nine that go down are VCAM1, KCTD12, ADAM12, CXCL12, PRSS35, DNM1, CCKAR, SOX4 and WNT2. RBAi computed the variance-stabilised values from the paired DESeq2 model of Lesson 3 and also saved a bundle with the script and its input files, so the figure can be rebuilt exactly. It took about three minutes in our run.
In our run, the sample names at the bottom ran into the two legends.
2. Fix the layout Main chat
Here the fix has to go through the main chat. RBAi drew this heatmap with a separate Python script and then showed a picture of the saved file, so the code behind the figure in the chat only displays that picture. Edit with AI rewrites the code behind a figure, so here it would only have had the code that shows the picture.
The sample names at the bottom overlap the two legends. Move the treatment and donor legends to the right of the heatmap, below the colour bar, so that nothing overlaps, and label the colour bar "Row z-score". Run the plotting code itself in the chat, rather than showing a picture of a saved file, so that Edit with AI can change the figure later.
What you should see: the figure at the top of this lesson, with the legends on the right under a colour bar labelled Row z-score. RBAi now draws the figure in the chat itself, from a script called airway_heatmap.py that it keeps among the chat's files. For changes to the heatmap itself, such as its colours or the number of genes, ask in the main chat, which can edit that script. It took about a minute.
3. Find the exceptions Main chat
A heatmap is most useful for what does not fit the pattern. Look along the rows: INHBB, for example, is far more orange in one treated sample than in the other three.
Which of these 40 genes respond least evenly across the four donors? For the three most uneven genes, give each donor's change with dexamethasone (treated minus untreated, in variance-stabilised units) and say which donor stands out.
What you should see: RBAi measures unevenness as the spread (standard deviation) of the four donors' changes.
| Gene | N61311 | N052611 | N080611 | N061011 | Spread |
|---|---|---|---|---|---|
| KLF15 | +2.86 | +2.64 | +1.58 | +3.11 | 0.67 |
| INHBB | +1.51 | +1.60 | +2.88 | +1.65 | 0.65 |
| WNT2 | −1.82 | −2.19 | −0.71 | −1.56 | 0.63 |
One donor, N080611, stands out for all three: a weaker rise in KLF15, a stronger rise in INHBB and a weaker fall in WNT2. Variance-stabilised units behave roughly like log2 units, so N080611's INHBB rise of 2.88 is roughly a seven-fold increase, against about three-fold in the other donors. Remember Lesson 2: N080611's two samples also sat apart from the other donors, at the top of the PCA.
The ideas behind the picture
- Colours are relative within each gene. After z-scoring, deep orange means "high for this gene", not "highly expressed". A deep orange cell for SPARCL1 and one for KLF15 can hide very different numbers of reads.
- Clustering always makes a tree. Give it random numbers and it still joins the closest pair first. A tree is only interesting when its branches line up with something you know, such as treatment or donor, or point to something you can test.
- Watch for circular reasoning. The samples split by treatment here partly because we chose the 40 genes that differ most by treatment. The PCA in Lesson 2, made from the most variable genes without telling the method which sample is which, is the fair check that treatment drives the experiment.
- The order of leaves is partly arbitrary. Swapping the two halves of any branch gives an equally valid tree. Read which items join and how high, not who sits next to whom across a branch.
- Replicates show biology, not only noise. Four donors respond in the same direction to every one of these genes, but not by the same amount. Differences between people are the reason experiments need more than one donor.
What the result shows, and what it doesn't
- What it shows: the 40 strongest responders move consistently in all four donors: 31 rise and 9 fall with dexamethasone. The size of the response differs between donors, and N080611 differs most for KLF15, INHBB and WNT2.
- What it doesn't show: why N080611 differs. It could be genetics, age, a different airway or how its cells grew, and four donors cannot tell these apart. The heatmap also tests nothing new; every gene in it was chosen by the test in Lesson 3.
- 40 is not 4,030. These are the most significant genes, so they are the clearest, not necessarily the most important. A heatmap of the largest fold changes, or of one pathway's genes, can tell a different story.
Your turn
Choose genes by the size of their change instead of the strength of the evidence. Before you ask, predict: will the samples still split by treatment, and will more of the genes go down?
Make the same heatmap for the 40 genes with the largest absolute shrunken log2 fold changes among those with FDR below 0.05, with the same styling, and tell me how many of them are also in the first heatmap.
Write three sentences: whether the samples still split by treatment, how many genes the two lists share, and which heatmap you would put in a talk about this experiment, and why. Bonus: does N080611 still stand out?
Try it
Open RBAi and continue your airway chat with the first prompt. Keep the chat: Lesson 5 asks what all these genes have in common.
Carry on in the same chat as Lessons 2 and 3, then paste the lesson’s first prompt:
Make a clustered heatmap of the 40 genes with the smallest adjusted p-values. Use DESeq2's variance-stabilised values and scale each gene's row to a z-score, so the colour shows how far each sample sits above or below that gene's own average. Cluster both the genes and the samples with Euclidean distance and complete linkage, and draw the trees. Colour the cells teal below the average, white at the average and orange above it. Add a colour bar over the samples for treatment (untreated grey, dexamethasone orange) and one for donor in four shades of blue, put the gene symbols on the rows, use a white background, make it about 16 cm wide and draw it in Python so it can be edited later. Then tell me how the samples group, how many genes fall in each of the two main gene clusters, and which of the 40 genes go down with dexamethasone.