Exercise: colocalisation

Goal

By the end of this exercise you should be able to:

  • compute Pearson’s r, Spearman’s rank coefficient, the overlap coefficient with k1, k2, and Manders’ M1 and M2 for two small channels,
  • predict which of them change when a channel gets a background offset, a gain or one bright outlier,
  • explain why thresholds decide M1 and M2, and how Costes’ method sets them,
  • say why the region matters, and (math) derive the invariances of the coefficients.

Do the pen-and-paper part first, then the two demos. Controls and significance (unit 2), objects (unit 3) and the confounds of the microscope (unit 4) come later in the day.

Background

Idea What it means
Scatter plot one point (A_i, B_i) per pixel of the region
Pearson r \sum (A_i - \bar A)(B_i - \bar B) / \sqrt{\sum (A_i - \bar A)^2 \sum (B_i - \bar B)^2}
Spearman Pearson’s r of the ranks (1 = darkest; ties get the average of their ranks)
Overlap coefficient \sum A_i B_i / \sqrt{\sum A_i^2 \sum B_i^2}
k1, k2 \sum A_iB_i / \sum A_i^2 and \sum A_iB_i / \sum B_i^2
M1 \sum A_i over pixels with A_i > t_A and B_i > t_B, divided by \sum A_i over pixels with A_i > t_A
M2 the same with A and B (and t_A, t_B) swapped
Costes’ threshold lower t_A (with t_B on the regression line) until the pixels below the thresholds have r \le 0

By hand

Two 3 \times 3 channels (the region is all 9 pixels):

A = \begin{bmatrix} 0 & 2 & 8 \\ 1 & 6 & 9 \\ 0 & 1 & 3 \end{bmatrix}, \qquad B = \begin{bmatrix} 1 & 1 & 6 \\ 0 & 5 & 8 \\ 1 & 2 & 0 \end{bmatrix}.

B1. (a) Compute the means \bar A and \bar B. (b) Compute \sum (A_i - \bar A)(B_i - \bar B), \sum (A_i - \bar A)^2 and \sum (B_i - \bar B)^2, and Pearson’s r. (c) Compute \sum A_i B_i, \sum A_i^2, \sum B_i^2 and the overlap coefficient.

B2. Thresholds t_A = t_B = 2 (“above” means > 2). (a) Which pixels are above in A, in B, in both? (b) Compute M1 and M2. Interpret them in words. (c) Compute M1 and M2 with t_A = t_B = -1 (no thresholds). Why are they both 1?

B3. The B channel gets a background offset: B' = B + 10. (a) Which of r and the overlap coefficient change? Compute the new overlap coefficient (\sum A_i B'_i = 454, \sum B_i'^2 = 1512). (b) Keep t_B = 2: what are M1 and M2 now, and why are they wrong? (c) Which t_B gives back the M1 and M2 of B2? (d) What would a gain, B'' = 2B with t_B = 4, change?

B4. A small structure inside a large one: A = \begin{bmatrix} 0 & 0 & 0 \\ 0 & 9 & 0 \\ 0 & 6 & 0 \end{bmatrix}, \qquad B = \begin{bmatrix} 0 & 5 & 5 \\ 0 & 5 & 5 \\ 0 & 5 & 5 \end{bmatrix}, \qquad t_A = t_B = 0. Compute M1 and M2. Then compute r (it is 0.37) and say which of the three numbers best describes the situation.

B5. Spearman’s coefficient of the B1 channels. With ties averaged, the ranks are (row by row) R_A = \begin{bmatrix} 1.5 & 5 & 8 \\ 3.5 & 7 & 9 \\ 1.5 & 3.5 & 6 \end{bmatrix}, \qquad R_B = \begin{bmatrix} 4 & 4 & 8 \\ 1.5 & 7 & 9 \\ 4 & 6 & 1.5 \end{bmatrix}. (a) Check two of the ranks. (b) Compute Spearman’s coefficient (the mean rank is 5; \sum (R_A - 5)(R_B - 5) = 36.25, \sum (R_A - 5)^2 = 59, \sum (R_B - 5)^2 = 57.5). (c) The 9 in A is changed into 90. Which of Pearson’s r and Spearman’s coefficient changes? (Pearson becomes 0.742.)

B6. Manders’ k1 and k2 for the B1 channels, from the sums of B1(c). Multiply them and compare with the overlap coefficient. What happens to k1 and k2 when B is doubled (B'' = 2B)?

Math questions

B7. (math) Show that r is unchanged when A is replaced by gA + c with g > 0, and that it changes sign for g < 0. Show that the overlap coefficient is unchanged by g > 0 but not by c.

B8. (math) Show that for B = aA (a > 0, no noise) and any t_A \ge 0 with t_B = a\,t_A, M_1 = M_2 = 1. What does this mean for the thresholds on the Costes line?

B9. (math) Show that the direction of the orthogonal regression line is the eigenvector of the 2 \times 2 covariance matrix of (A, B) with the largest eigenvalue, and derive the slope formula \big(s_{BB} - s_{AA} + \sqrt{(s_{BB} - s_{AA})^2 + 4 s_{AB}^2}\big)/(2 s_{AB}).

B10. (math) Show that \text{overlap}^2 = k_1 k_2, and that k_1 \mapsto k_1 g, k_2 \mapsto k_2 / g when B is multiplied by g > 0.

B1. (a) \bar A = 30/9 = 3.33, \bar B = 24/9 = 2.67. (b) \sum (A - \bar A)(B - \bar B) = \sum AB - 9\bar A\bar B = 154 - 80 = 74; \sum (A - \bar A)^2 = 196 - 100 = 96; \sum (B - \bar B)^2 = 132 - 64 = 68. r = 74/\sqrt{96 \cdot 68} = 74/80.8 = \mathbf{0.916}. (c) \sum AB = 154, \sum A^2 = 196, \sum B^2 = 132: overlap = 154/\sqrt{196 \cdot 132} = 154/160.9 = \mathbf{0.957}.

B2. (a) A above 2: the pixels with values 8, 6, 9, 3. B above 2: 6, 5, 8 (at the positions of A’s 8, 6, 9). Both: the positions of A’s 8, 6 and 9. (b) M_1 = (8 + 6 + 9)/(8 + 6 + 9 + 3) = 23/26 = \mathbf{0.885}: 88 % of A’s signal lies where B is present (the 3 in the bottom-right corner has no B). M_2 = (6 + 5 + 8)/(6 + 5 + 8) = \mathbf{1}: all of B’s signal lies where A is present. (c) With t = -1 every pixel is above in both channels, so numerator and denominator are the same sums: M_1 = M_2 = 1. Without a threshold M1 and M2 say nothing.

B3. (a) r stays 0.916 (it subtracts the means). Overlap: 454/\sqrt{196 \cdot 1512} = 454/544.4 = \mathbf{0.834} (was 0.957): not offset-invariant. (b) Now every B' is above 2: M_1 = 26/26 = 1; M_2 = \sum B' over A-above pixels / \sum B' over all = (16 + 15 + 18 + 10)/114 = 59/114 = 0.518. Both are wrong: the offset made every pixel “positive” in B, so all of A counts as colocalised, and B’s background (now 10 per pixel) is counted as B signal. (c) Moving the threshold with the background: t_B = 12 gives back M_1 = 0.885, M_2 = 1. (d) A gain changes nothing if the threshold scales with it: r, overlap, M1 and M2 are the same (M2’s sums double in numerator and denominator).

B4. A above 0: the 9 and the 6, both where B = 5: M_1 = 15/15 = \mathbf 1. B above 0: six pixels with sum 30, of which two (sum 10) have A: M_2 = 10/30 = \mathbf{0.33}. All of A is inside B, but only a third of B contains A. r = 0.37 hides this asymmetry; M1 and M2 together describe it.

B5. (a) A’s values sorted: 0, 0, 1, 1, 2, 3, 6, 8, 9. The two zeros share ranks 1 and 2, so each gets 1.5; the two ones share 3 and 4: 3.5. In B the three ones share ranks 3, 4, 5: each gets 4. (b) \rho = 36.25 / \sqrt{59 \cdot 57.5} = 36.25 / 58.25 = \mathbf{0.622}, lower than r = 0.916: the ranks give the dim pixels as much weight as the bright ones, and the dim pixels are hardly related. (c) 90 is still the largest value of A, so its rank stays 9: Spearman stays 0.622. Pearson drops from 0.916 to 0.742: the one pixel now has a large deviation from the mean in A but only an ordinary one in B, so the products no longer line up.

B6. k_1 = 154/196 = \mathbf{0.786}, k_2 = 154/132 = \mathbf{1.167}; k_1 k_2 = 0.917 = 0.957^2, the square of the overlap coefficient. With B'' = 2B: \sum AB'' = 308, \sum B''^2 = 528, so k_1 = 1.571 (doubled) and k_2 = 0.583 (halved): each depends on the gain, only the product does not.

B7. gA + c has mean g\bar A + c, so (gA_i + c) - (g\bar A + c) = g(A_i - \bar A). Numerator scales by g, denominator by \sqrt{g^2} = |g|: r \mapsto \operatorname{sign}(g)\,r. Overlap: \sum (gA_i) B_i / \sqrt{g^2 \sum A_i^2 \sum B_i^2} is unchanged for g > 0; with c: \sum (A_i + c) B_i / \sqrt{\sum (A_i + c)^2 \sum B_i^2}, which differs in general (B3: 0.957 → 0.834) and tends to \sum B_i / \sqrt{n \sum B_i^2} as c \to \infty.

B8. With B_i = aA_i and t_B = a t_A: B_i > t_B \iff A_i > t_A. So the set of pixels above in A equals the set above in B, numerator and denominator of M_1 are the same sums, and likewise for M_2. On the Costes line the thresholds are “matched”: for perfectly colocalised channels M1 = M2 = 1 at every step, and any value below 1 comes from pixels off the line.

B9. Centre the data; a unit direction \mathbf u captures the variance \mathbf u^\top S \mathbf u, and the sum of squared perpendicular distances is \operatorname{tr} S - \mathbf u^\top S \mathbf u (times n). Minimising it maximises \mathbf u^\top S \mathbf u: the top eigenvector of S = \begin{bmatrix} s_{AA} & s_{AB} \\ s_{AB} & s_{BB} \end{bmatrix} (the PCA unit). Its eigenvalue \lambda = \tfrac12\big(s_{AA} + s_{BB} + \sqrt{(s_{BB} - s_{AA})^2 + 4 s_{AB}^2}\big); the eigenvector satisfies the first row of (S - \lambda I)\mathbf u = 0, (s_{AA} - \lambda) u_A + s_{AB} u_B = 0, so the slope u_B/u_A = (\lambda - s_{AA})/s_{AB} = \big(s_{BB} - s_{AA} + \sqrt{(s_{BB} - s_{AA})^2 + 4 s_{AB}^2}\big)/(2 s_{AB}).

B10. k_1 k_2 = \dfrac{\sum AB}{\sum A^2}\cdot\dfrac{\sum AB}{\sum B^2} = \Big(\dfrac{\sum AB}{\sqrt{\sum A^2 \sum B^2}}\Big)^2 = \text{overlap}^2. With B \mapsto gB: k_1 = g\sum AB / \sum A^2 is multiplied by g, and k_2 = g \sum AB / (g^2 \sum B^2) is divided by g; their product is unchanged, as the overlap coefficient is (B7).

Explore: coefficients on a grid

Two small channels you can edit: click the upper half of a cell to add 1, the lower half to subtract 1. The scatter plot shows one point per pixel and the two thresholds; cells above their own threshold are tinted, cells above both (the ones M1 and M2 count) are outlined.

D1. Preset exercise B1: check your answers to B1, B2, B5 and B6. Then raise the 9 in A to 20, one click at a time. Which numbers change, and which do not? Explain with B5(c).

D2. Preset one bright outlier carries r. Read Pearson’s r and Spearman’s coefficient. Lower the two 20s (same cell in A and B) to 10, then 6, then 4. How does each coefficient respond?

D3. Preset no thresholds: M1 = M2 = 1. Read r and M1, M2. Then set both thresholds to 4 and to 5. What do M1 and M2 measure at each setting?

D1. At 20: r 0.888, Spearman 0.622, overlap 0.928, k1 0.470, k2 1.833, M1 0.919, M2 1. Spearman does not move (the pixel stays the brightest of A, rank 9), M2 does not move (B’s pixels above 2 are still all where A is above 2). Pearson, the overlap, k1 and k2 change because they use the values themselves; M1 rises a little because the colocalised pixel carries more of A’s intensity.

D2. Outlier 20: r = 0.849, Spearman −0.400. At 10: 0.483 and −0.400; at 6: 0.026 and −0.400; at 4: −0.283 and −0.400. Spearman does not care how bright the brightest pixel is; Pearson goes from strongly positive to negative because the rest of the grid is anti-correlated (r = -0.51 without the outlier). One pixel decided the sign of r.

D3. r = -0.51 (the channels are anti-correlated), yet M1 = M2 = 1: with thresholds 0 every pixel is “above” in both channels. At 4: M1 = 0.722, M2 = 0.695 (the offset is 4, so “above” means “any signal at all”, which still covers most pixels). At 5: M1 = 0.118, M2 = 0.110: only pixels with real signal count, and almost none has it in both channels.

Explore

The demo makes two synthetic channels inside two cells: structures in A; in B a fraction shared f of the same structures and the rest independent ones (or, with A inside B, large B structures that contain A’s small spots); a diffuse signal in the cells in both channels; bleed-through of A into B; a sideways shift of B; a background offset and noise. Left: the merge (A green, B magenta, white where both are bright) with the region (yellow: inside the cells). Middle: the 2D histogram over the region (logarithmic colour), the orthogonal regression line (dashed), and the thresholds t_A (green, vertical) and t_B (red, horizontal): drag them. Right: the readouts. Costes animates the threshold search; a toggle switches Manders’ definition (with or without the own-channel threshold in the denominator). (The randomise button is the subject of unit 2.)

E1. Preset perfect colocalisation. Describe the scatter plot. Read r, Spearman, the overlap coefficient, M1 and M2, and the Costes thresholds. Press Costes and watch the thresholds move: where do they stop, relative to the background blob? Why is Spearman so much lower than r?

E2. Preset independent: r ≈ 0. Describe the scatter plot. Why is Costes’ method not defined here, and what are M1 and M2 with the fallback thresholds?

E3. Preset background inflates r: diffuse signal in both channels, region = whole image. Read r. Switch the region to inside cells. Explain the difference with the scatter plot.

E4. Preset M1 ≈ M2 ≈ 1 without thresholds (independent channels, t_A = t_B = 0). Read M1 and M2. Then press Costes. Compare with B2(c) and B3(b).

E5. Preset A inside B. Read M1, M2 and r. Which number tells you that A sits inside B? Switch the Manders definition to without the own threshold: what changes, and why? Set the thresholds to zero: what happens to M2?

E6. Back to the default (reset): shared f = 0.5. Set f to 0.25 and 0.75. Is r the fraction of shared structures?

E1. A thin cloud along a line through the background blob with slope 1.00; r = 0.988, Spearman 0.808, overlap 0.993, M1 = 0.940, M2 = 0.939. Costes’ thresholds stop at t_A = 13.9, t_B = 13.8, just above the background blob (offset 10, noise σ = 4): below them the remaining pixels are noise with no correlation. Spearman is lower because about half of the pixels in the cells are background (2733 of 5978 are above both thresholds): their ranks are set by independent noise in the two channels, and every rank counts equally, while for Pearson the few bright pixels with their large deviations dominate.

E2. An L shape: bright A with dark B along the A axis, bright B with dark A along the B axis. r = -0.08 (structures that exclude each other slightly), so the regression line falls and there is no rising line to walk down. With the 75th-percentile thresholds, M1 = 0.20 and M2 = 0.18: the chance level, not zero.

E3. Whole image: r = 0.51. Inside the cells: r = -0.08. Over the whole image the scatter plot has two clusters: the background at (10, 10) and the cells at about (40, 40) (diffuse signal 30 in both channels). The line through the two clusters gives a high r although the structures inside the cells are independent.

E4. M1 = 0.999, M2 = 0.997: with a background offset and noise, almost every pixel is above 0 in both channels, so “where the other channel is present” means everywhere. Costes is not defined for these anticorrelated channels; the fallback thresholds give 0.20 and 0.18. As in B2(c) and B3(b): without the right threshold, M1 and M2 measure nothing.

E5. M1 = 0.79, M2 = 0.32, r = 0.37. M1 high and M2 low: most of A lies where B is, but most of B has no A. r alone cannot tell this. Without the own threshold M1 = 0.48 and M2 = 0.24: the denominators now contain all of A’s (B’s) intensity in the cells, including the offset and noise of the dim pixels, which mostly lie outside the other channel. With zero thresholds M2 becomes 0.995 (and M1 1.000): every pixel counts as A-positive, so all of B is “where A is”.

E6. r = 0.26 at f = 0.25, 0.69 at 0.5, 0.94 at 0.75. It grows with f but is not equal to it: r measures how well a straight line explains B from A, which mixes the shared fraction with the intensities and the noise.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in colocalisation/src/coloc/exercise.rs. Every function takes the two channels and a mask: only the pixels of the region count.

C1. pearson(a, b, mask): Pearson’s r over the pixels of the mask:

mean_A, mean_B = means over the region
s_AB = s_AA = s_BB = 0
for every pixel i in the region:
    dA = A_i − mean_A,  dB = B_i − mean_B
    s_AB += dA · dB,  s_AA += dA²,  s_BB += dB²
r = s_AB / sqrt(s_AA · s_BB)

The checks are B1(b) (r = 0.916), B4 and B5(c), and B7: a gain and an offset of A leave r unchanged.

C2. overlap(a, b, mask): the same loop without subtracting the means gives \sum AB, \sum A^2, \sum B^2, and from them the overlap coefficient, k1 and k2. Checks: B1(c), B3(a) and B6.

C3. manders(a, b, mask, ta, tb):

for every pixel i in the region:
    if A_i > tA:  sumA += A_i;  if B_i > tB: bothA += A_i
    if B_i > tB:  sumB += B_i;  if A_i > tA: bothB += B_i
M1 = bothA / sumA,  M2 = bothB / sumB

Checks: B2, B3(b, c) and B4.

C4. ranks(values) and spearman(a, b, mask): rank the pixel values of each channel (ties share the average of their ranks), then Pearson’s r of the ranks.

sort the positions by value
for each group of equal values, taking sorted places k+1 … m:
    every member gets the rank (k+1 + m) / 2

Checks: the ranks of B5(a) and Spearman’s coefficient of B5(b, c).

C5. orthogonal_line(a, b, mask) and costes(a, b, mask, steps): the slope of the orthogonal regression line from the slide “The math: the regression line”, then the walk down the line:

if r(region) ≤ 0: no Costes threshold
slope, intercept = orthogonal regression line
for k = 1, 2, …, steps:
    tA = max A − k · (max A − min A) / steps
    tB = slope · tA + intercept
    below = pixels of the region with A ≤ tA or B ≤ tB
    if r(below) ≤ 0: stop
report tA, tB

The check runs it on a synthetic colocalised image with background 0.04: the thresholds must stop just above the background, with r \le 0 below them.

Then cargo run -p colocalisation -- coloc and open the pictures in out/coloc/. The program makes five synthetic two-cell images (grey values 0 to 1, background 0.04, noise 0.015): perfect (B has A’s structures), independent, leak (independent, plus 40 % of A in B), a-inside-b (small A spots inside large B structures) and diffuse (independent, plus a diffuse signal in both cells). For each it prints r inside the cells and over the whole image, the overlap coefficient, Spearman, the Costes thresholds and M1, M2, and it saves the merge and the scatter plot with the thresholds (A to the right, B upwards).

C6. Compare r over the cells and over the whole image for diffuse, and look at diffuse-scatter.png and diffuse-merge.png. Which slide of the lecture is this?

C7. For perfect, Spearman is far below r. Why? For leak, nothing is colocalised: which numbers say otherwise, and what in leak-scatter.png gives the leak away?

C8. For a-inside-b, which numbers describe the situation, and which does not?

The model solution is colocalisation/src/coloc/solution.rs in the download.

C6. Inside the cells r = -0.045, over the whole image r = 0.656. The structures are independent; the whole image adds a dense blob of background pixels at (0.04, 0.04) and moves the cell pixels to about (0.19, 0.19), so the scatter plot is stretched along the diagonal between the two clusters. This is the slide “Pearson’s correlation” (the region matters), as in E3.

C7. perfect: r = 0.932 but Spearman 0.453. Most pixels in the cells are background, whose ranks are set by independent noise in the two channels; every rank counts equally, while in r the few bright pixels with large deviations dominate. leak: r = 0.325 (independent: -0.045), Spearman 0.282, and with the Costes thresholds M1 = 0.777 (independent: 0.194): the leaked copy of A in channel B looks like colocalisation. In leak-scatter.png the leak is a second, straight arm of slope about 0.4 rising from the background blob, next to the L shape of the independent structures.

C8. M1 = 0.916 and M2 = 0.493: almost all of A lies where B is, about half of B has no A. Together they describe “A inside B”. r = 0.593 alone does not say which channel lies inside the other.

Where this leads

Every number of this unit needs a reference for “no colocalisation”: the next unit, is it more than chance?, builds one with shift curves and block randomisation. Colocalisation coefficients are per image or per cell; to compare conditions they go through the same aggregation and statistics as every other measurement (the evaluation day: replicates). FRET (day 49) and fluorescence lifetimes (day 50), the other days of this part, measure molecular proximity below the resolution limit.