LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 35 lessons

Lesson 7 of 9

Module 2 · Figures that tell the truth

Omics figures

Omics figures

Omics figures

Heatmaps, volcano plots and MA plots summarise thousands of genes in one picture. Each has a few choices that decide whether it informs or misleads.

⏱ 13 min readModule 2 · Figures that tell the truthFree
Lesson at a glance6 steps · about 13 min
  1. Clustered heatmap
  2. What this does
  3. Reading the output
  4. Volcano and MA plots
  5. What this does
  6. Reading the output

Before you start. This lesson uses the z-scored matrix z from Colour that works for everyone. If you open a new notebook, or Colab has restarted, run this setup cell first. If seaborn prints a DeprecationWarning or PendingDeprecationWarning, it comes from the library, not from your code, and you can ignore it.

Python · setup
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

expr = pd.read_csv("expression_example.csv", index_col="gene")
z = expr.sub(expr.mean(axis=1), axis=0).div(expr.std(axis=1), axis=0)
Files for this lessonSave each file under the name shown here, next to your notebook, or upload it to Colab (folder icon › Upload). Your browser may give the download a different name; rename it before you run the code.
expression_example.csvSimulated log2 expression: 30 genes, 8 samples de_results_example.csvSimulated differential expression results for 3,000 genes

Clustered heatmap

Scale each gene (row) to z-scores, so colour shows each gene relative to its own average; otherwise a few highly expressed genes take all the colour. Clustering then groups similar genes and samples.

A heatmap is a table drawn as colours: one row per gene, one column per sample, and each cell coloured by a value. A z-score says how far a value sits from its row's average, measured in standard deviations: z = (value − gene mean) ÷ gene standard deviation. A z-score of 0 means "average for this gene", +1 means one standard deviation above it, and −1 one below. After this scaling every gene has a mean of 0 and a standard deviation of 1, so a gene measured in thousands and a gene measured in tens use the colour scale in the same way.

Clustering here means hierarchical clustering: the computer repeatedly joins the two most similar rows (or columns), then the next most similar, until everything is joined. The tree it builds, drawn at the edge of the heatmap, is called a dendrogram; the height at which two branches join shows how different they are. The rows and columns are then reordered to follow the tree, so similar genes end up next to each other and blocks of colour appear.

"Most similar" needs two definitions, and the tree depends on both. The distance says how different two rows are; seaborn's clustermap uses Euclidean distance by default, the straight-line distance between two rows treated as points. The linkage says how to measure the distance between two groups once rows have been joined; the default, average linkage, uses the average distance between their members. You can change them with metric= and method=. This is also why the z-scoring comes first: on raw values, Euclidean distance mostly measures how highly two genes are expressed, so genes would cluster by level. On z-scores it measures whether they rise and fall together across samples, which is usually the question.

A real experiment measures far more genes than fit in a readable heatmap, so a heatmap usually shows a chosen subset. A common choice is the few hundred genes that vary most across samples, picked without looking at the group labels. Say in the legend how the genes were chosen, because the choice shapes the picture as much as the palette does.

Omics figures · LSMA free course Two made-up genes in four samples. On raw values, gene H is dark in every sample and gene L is pale in every sample, so neither pattern shows. After z-scoring each gene, both show low values in the two control samples and high values in the two treated samples. 1 · RAW VALUES 2 · Z-SCORE EACH GENE ctrl_1ctrl_2treat_1treat_2 ctrl_1ctrl_2treat_1treat_2 Gene HGene L Gene HGene L 9009501,1001,150 9111921 −1.05−0.630.631.05 −1.02−0.680.681.02 Gene H: mean 1,025, SD 119 Gene L: mean 15, SD 5.9 z = (value − gene mean) ÷ gene SD Both genes now show the same pattern.
Made-up values for two genes. On one shared colour scale (left), gene H is dark everywhere and gene L pale everywhere, hiding the fact that both rise with treatment. Z-scoring each row (right) puts both genes on the same footing. Schematic, not to scale.
Python
print(z.iloc[:2].round(2))
cg = sns.clustermap(z, cmap="RdBu_r", center=0, vmin=-2.5, vmax=2.5, figsize=(5.5, 7),
                    col_colors=["#0072B2"] * 4 + ["#E69F00"] * 4)
cg.savefig("heatmap.png", dpi=300)
ctrl_1 ctrl_2 ctrl_3 ctrl_4 treat_1 treat_2 treat_3 treat_4 gene G01 -1.16 -0.72 -1.03 -0.80 0.86 0.89 1.06 0.89 G02 -0.85 -1.03 -0.96 -0.87 0.71 1.05 1.00 0.96

What this does

  • z is the z-scored expression table built in Colour that works for everyone: genes in rows, eight samples in columns. print(z.iloc[:2].round(2)) prints its first two rows (iloc selects rows by position, and :2 means positions 0 and 1), rounded to two decimals.
  • sns.clustermap(z, ...) clusters the rows and the columns, reorders both, and draws the heatmap with a dendrogram on each side.
  • cmap="RdBu_r" is the red–blue diverging palette, reversed (_r) so that high values are red and low values blue. center=0 puts the palette's neutral middle colour at 0, the gene's own average.
  • vmin=-2.5, vmax=2.5 fixes the ends of the colour scale. Any z-score beyond ±2.5 gets the darkest colour, so one extreme value cannot stretch the scale and wash out everything else.
  • figsize=(5.5, 7) makes the figure taller than it is wide, which suits 30 gene rows and 8 sample columns.
  • col_colors=["#0072B2"] * 4 + ["#E69F00"] * 4 builds a list of eight colours, four blue then four orange, and draws them as a strip above the columns: blue marks the four control samples and orange the four treated ones. The strip moves with its column when the columns are reordered, so you can see at a glance whether the clustering separated the groups.
  • cg.savefig("heatmap.png", dpi=300) saves the whole clustered figure. A clustermap is its own figure-level object, so you save it through cg.

Reading the output

Each printed row is one gene and each column one sample. Gene G01 is below its own average in all four control samples (−1.16 to −0.72) and above it in all four treated samples (0.86 to 1.06): a gene that goes up with treatment. G02 shows the same pattern. You can check the scaling by eye: G01's four negative values add up to −3.71 and its four positive values to 3.70, so its mean is 0, as every z-scored row's must be (the small difference is rounding).

When you read the heatmap itself, start with the colour strip above the columns. If all the blue samples sit on one side of the tree and all the orange on the other, the treatment is the largest source of difference between samples. If one treated sample sits among the controls, do not hide it. It may be a sample that was mislabelled or swapped, a failed treatment, or a sample processed in a different batch, and each of those is worth finding before any further analysis.

Clustered heatmap of 30 genes. Samples split into control and treated groups; two gene clusters go up and down with treatment, and the rest show no pattern.
Drawn by the Python code above. The samples cluster by group, and two blocks of genes change with treatment. Simulated data.

Eisen, Spellman, Brown & Botstein (1998), "Cluster analysis and display of genome-wide expression patterns", PNAS 95(25), 14863–14868, introduced the clustered, colour-coded expression heatmap that became standard in genomics. The adjusted p-values in the next section use the method of Benjamini & Hochberg (1995), Journal of the Royal Statistical Society, Series B 57(1), 289–300. Treat a clustered heatmap as a display, not a test. Hierarchical clustering always produces a tree, and the tree depends on the distance and linkage chosen, so report both. Be careful, too, with genes chosen because they differ between control and treated: they will separate those samples almost by definition, so the split confirms the selection, not a new finding.

Volcano and MA plots

A volcano plot shows effect size against evidence. An MA plot shows effect size against how highly a gene is expressed, which reveals whether low-count genes are driving the results.

Both plots start from a differential expression results table: one row per gene, comparing two groups. Three columns matter here.

  • log2FoldChange: how much the gene changed, on a log base 2 scale. +1 means doubled in the treated group, −1 means halved, 0 means no change.
  • padj: the adjusted p-value. With thousands of genes tested at once, some will look significant by chance alone, so each p-value is adjusted for the number of tests. A common method, Benjamini–Hochberg, controls the false discovery rate: the expected proportion of false positives among the genes you call significant.
  • baseMean: the gene's average expression across all samples, after adjusting for sequencing depth.

The volcano plot puts log2 fold change on the x-axis and −log10(padj) on the y-axis. Taking −log10 turns small p-values into large numbers: padj = 0.1 becomes 1, padj = 0.01 becomes 2, and each step of 1 up the axis is a p-value ten times smaller. So the strongest evidence is at the top, the biggest increases at the right, the biggest decreases at the left, and the cloud of points takes the shape of an erupting volcano. In the MA plot, M is the log ratio (the fold change) and A is the average expression; the name comes from microarray analysis, where the plot was first widely used.

Python
de = pd.read_csv("de_results_example.csv")
de["status"] = "Not significant"
de.loc[(de["padj"] < 0.05) & (de["log2FoldChange"] > 1), "status"] = "Up"
de.loc[(de["padj"] < 0.05) & (de["log2FoldChange"] < -1), "status"] = "Down"
print(de["status"].value_counts())

cols = {"Up": "#D55E00", "Down": "#0072B2", "Not significant": "#BFBFBF"}
fig, axes = plt.subplots(1, 2, figsize=(9, 3.8))
for status, d in de.groupby("status"):
    axes[0].scatter(d["log2FoldChange"], -np.log10(d["padj"]), s=4, alpha=0.7, color=cols[status], label=status)
    axes[1].scatter(np.log10(d["baseMean"]), d["log2FoldChange"], s=4, alpha=0.7, color=cols[status])
axes[0].axvline(-1, ls="--", c="grey"); axes[0].axvline(1, ls="--", c="grey")
axes[0].axhline(-np.log10(0.05), ls="--", c="grey")
axes[1].axhline(0, c="black", lw=0.8)
axes[0].set(xlabel="log2 fold change", ylabel="-log10 adjusted p-value")
axes[1].set(xlabel="log10 mean expression", ylabel="log2 fold change")
axes[0].legend(frameon=False, markerscale=3)
for ax, letter in zip(axes, "AB"):
    ax.text(-0.15, 1.05, letter, transform=ax.transAxes, fontsize=13, fontweight="bold")
sns.despine(); plt.tight_layout()
status Not significant 2841 Down 83 Up 76 Name: count, dtype: int64

What this does

  • pd.read_csv loads the results table into de.
  • de["status"] = "Not significant" adds a new column with the same label in every row. The next two lines overwrite it where a gene passes both tests. de.loc[condition, "status"] = "Up" means "in the rows where the condition is True, set status to Up". The condition joins two tests with & (and): adjusted p-value below 0.05 and log2 fold change above 1. Each test needs its own brackets, because Python evaluates & before comparisons such as <. The "Down" line is the same with a fold change below −1.
  • de["status"].value_counts() counts how many genes carry each label.
  • cols is a dictionary mapping each label to a colour: vermilion for up, blue for down, light grey for not significant, a colour-blind-safe pairing from Colour that works for everyone.
  • for status, d in de.groupby("status") loops over the three groups of genes; d holds the rows of one group. For each group, the first scatter draws the volcano plot in the left panel (fold change against −log10 of padj) and the second draws the MA plot in the right panel (log10 of baseMean against fold change). s=4 makes the points small and alpha=0.7 slightly transparent, which helps when thousands of points overlap.
  • axvline draws vertical dashed lines at −1 and 1, and axhline a horizontal one at −log10(0.05): the two thresholds. In the MA plot, a solid black line at 0 marks "no change".
  • set(...) labels the axes, legend(..., markerscale=3) draws the legend with its dots three times larger than the tiny plotted points so they can be seen, and the final loop adds the panel letters A and B, as in Multi-panel figures.

Reading the output

value_counts() lists the labels from most to least common. Of the genes in the file, 2,841 are not significant, 83 are down and 76 are up; the three add up to 3,000. Name: count says the numbers are counts, and int64 that they are whole numbers.

A: volcano plot with 76 up-regulated genes in orange on the right and 83 down-regulated in blue on the left, above the dashed significance line. B: MA plot of the same genes, with significant genes spread across all expression levels.
Drawn by the Python code above. Simulated data.

In panel A, the significant genes sit in the two upper corners: far enough from zero on the x-axis and high enough on the y-axis to pass both thresholds. A gene high up but near the middle has strong evidence for a small change; a gene far out at the side but low down has a large change that may be noise. In panel B, the significant genes are spread across low, medium and high expression. If they were bunched at the far left, among genes with very few reads, you would suspect that a handful of noisy, low-count genes were driving the result.

The MA plot on real RNA-seq data often has a recognisable shape: a funnel that is wide at the left and narrow at the right. Genes with few reads have very uncertain fold changes, so they scatter widely up and down; genes with many reads are measured precisely and stay close to the line at zero. The x-axis is on a log10 scale for the reason given in Honest axes and scales: mean expression spans several powers of ten. Tools such as DESeq2 can also report shrunken fold changes, which pull the noisy estimates of low-count genes towards zero, and the funnel then narrows. If you plot shrunken values, say so in the legend.

Worked example · what the thresholds mean

vertical lines at ±1 → a doubling or halving (21 = 2)
horizontal line at −log10(0.05) = 1.3 → padj = 0.05
significant: 76 up + 83 down = 159 of 3,000 genes (5.3%)

Step by step. The fold-change lines sit at log2 fold change = ±1. Undoing the log, 21 = 2 and 2−1 = 0.5, so a gene must at least double or at least halve. The significance line sits at −log10(0.05). Since log10(0.05) = −1.301, the line is at 1.301, about 1.3; any gene above it has padj below 0.05. Adding the two groups, 76 + 83 = 159, and 159 ÷ 3,000 = 0.053, or 5.3%.

What the answer means. Both thresholds are choices, not laws of nature. Moving the fold-change cut-off from 1 to 0.5 would admit more genes with smaller changes. State the thresholds in the figure legend, so readers know what "significant" means in your figure.

A common pitfall is to read the grey points as "genes that did not change". Many of them may have changed, but by too little, or with too little evidence in this experiment, to pass the thresholds. Not significant means not shown to change, not shown not to change.

Go deeper: how Benjamini–Hochberg works

The method works on ranks. Sort all m p-values from smallest to largest and give each a rank i, so the smallest has rank 1. Each gene gets its own threshold, i × q ÷ m, where q is the false discovery rate you accept. Find the largest rank whose p-value is at or below its threshold: that gene and every gene ranked above it pass.

With m = 3,000 and q = 0.05, the gene ranked first needs p ≤ 0.05 ÷ 3,000, about 0.0000167, while the gene ranked 159th needs only p ≤ 159 × 0.05 ÷ 3,000 = 0.00265. The bar is lowered as more genes pass. The padj column turns this round: it is the smallest q at which each gene would pass, so padj < 0.05 applies the same rule. Two practical catches before plotting: a padj of exactly 0 makes -np.log10 return infinity, and tools such as DESeq2 leave padj missing for genes they filter out. Count both with (de["padj"] == 0).sum() and de["padj"].isna().sum().

Key point

Label only the genes you discuss, with lines to their points if crowded (the adjustText package does this for matplotlib). Thirty overlapping labels help nobody.

A second example. In a clustered heatmap of 30 genes the row names are readable, but in a heatmap of 500 genes they shrink to an unreadable grey stripe. There, hide the row names and mark only the few genes the text discusses, or show a smaller heatmap of those genes as a separate panel.

Heatmaps, volcano plots and MA plots are the figures that reviewers of omics papers look at first, and they bring together everything in this module so far: a palette matched to the data, a centred diverging scale, log axes, and panels with shared colours and letters. The next lesson, Publication-ready export, turns figures like these into files that a journal or a slide will reproduce faithfully.

Further reading · Module 2Weissgerber et al. 2015: why bar charts hide the data. · All reading for this course

Check your understanding

1. Why z-score genes before a heatmap?

2. What does an MA plot add to a volcano plot?

3. On a volcano plot, a gene sits at y = 3. What is its adjusted p-value?

Up next

Lesson 8

Publication-ready export

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.