Exercise: what the microscope does to colocalisation
Before this unit: Objects instead of pixels (previous unit); Day 46: Colour.
See also: Day 4: Image formation; Day 14: Labelling; Day 16: Measurements; Day 23: FFT registration; Day 47: Spectral unmixing.
Goal
By the end of this exercise you should be able to:
- compute what bleed-through does to r, estimate the leakage from a single-stained control and compensate,
- predict the overlap of two spots from the PSF and their distance, and the cost of a chromatic shift,
- say which coefficients a constant offset and a shared background affect,
- put the corrections in order and justify it,
- (math) derive r = s/\sqrt{1 + s^2}, the Gaussian overlap formulas and the shared-background r.
Do the pen-and-paper part first, then the demo.
Background
| Idea | What it means |
|---|---|
| Bleed-through | channel B holds B + sA; s from a single-stained A control: slope through the origin, \hat s = \sum A B / \sum A^2 |
| Compensation | \hat B = B' - \hat s A; wrong \hat s leaves (s - \hat s)A |
| PSF width | \sigma \approx 0.21\,\lambda/\mathrm{NA}; FWHM \approx 0.51\,\lambda/\mathrm{NA} |
| Normalised overlap of two spots | \frac{2\sigma_1\sigma_2}{\sigma_1^2 + \sigma_2^2}\exp\big(-\delta^2/2(\sigma_1^2 + \sigma_2^2)\big); equal widths: e^{-\delta^2/4\sigma^2} |
| Shared background | A = S_A + G, B = S_B + kG |
| Order | background → compensation → registration → region → coefficients → controls |
By hand
B1. Two channels of six pixels, unrelated: A = (0, 10, 2, 8, 0, 4), B = (3, 4, 1, 2, 5, 3), with \sum (A - \bar A)(B - \bar B) = -2, \sum (A - \bar A)^2 = 88, \sum (B - \bar B)^2 = 10. (a) Compute r(A, B). (b) 30 % of A leaks into B: compute B' = B + 0.3A. With \sum (A - \bar A)(B' - \bar B') = 24.4 and \sum (B' - \bar B')^2 = 16.72, compute r(A, B'). (c) A single-stained control (only dye A) gives A_c = (0, 10, 5, 20) and, in channel B, B_c = (0.1, 3.1, 1.4, 6.0). Estimate s by the slope through the origin. (d) Compensate B' with your estimate. What is r now (approximately)? (e) What would \hat s = 0.6 do?
B2. NA 1.4, \lambda = 520 nm. (a) Compute the FWHM (0.51\lambda/\mathrm{NA}) and σ (0.21\lambda/\mathrm{NA}). (b) Compute the normalised overlap of two equal spots 100 nm apart and 300 nm apart. (c) At which distance is it 0.5?
B3. Pixels of 65 nm, spots of σ = 80 nm. What does a chromatic shift of 1 pixel do to the normalised overlap of a colocalised pair? And of 2 pixels?
B4. (a) Which coefficients does a constant offset added to both channels change: r, the overlap coefficient, M1/M2 with fixed thresholds? (b) Which does a background that varies over the field and is shared by both channels change? (c) For each, what is the correction?
B5. Put these corrections in order and justify every position: registration, background subtraction, choosing the region (and excluding saturated pixels), compensation, computing the coefficients with thresholds.
B6. Explain, in one or two sentences each, why each of these makes two unrelated proteins look colocalised: (a) measuring r over the whole image including the dark background; (b) 30 % of dye A leaking into channel B; (c) both proteins being in vesicles smaller than the resolution limit.
Math questions
B7. (math) Show that for independent A and B with equal variance, r(A, B + sA) = s/\sqrt{1 + s^2}. Generalise to \operatorname{var}B = c^2\operatorname{var}A.
B8. (math) For 2D Gaussians g_i(\mathbf x) \propto e^{-|\mathbf x - \mathbf m_i|^2/2\sigma_i^2} with |\mathbf m_1 - \mathbf m_2| = \delta, show that \int g_1 g_2 / \sqrt{\int g_1^2 \int g_2^2} = \frac{2\sigma_1\sigma_2}{\sigma_1^2 + \sigma_2^2}\exp\big(-\delta^2/2(\sigma_1^2 + \sigma_2^2)\big). (Use: the convolution of two Gaussians is a Gaussian whose variance is the sum.)
B9. (math) For A = S_A + G, B = S_B + kG with independent S_A, S_B, G, derive r(A, B). For which k is it largest, for fixed variances?
B1. (a) r = -2/\sqrt{88 \cdot 10} = \mathbf{-0.07}: unrelated. (b) B' = (3, 7, 1.6, 4.4, 5, 4.2); r = 24.4/\sqrt{88 \cdot 16.72} = 24.4/38.4 = \mathbf{0.64}, from leakage alone. (c) \hat s = \sum A_c B_c / \sum A_c^2 = (0 + 31 + 7 + 120)/(0 + 100 + 25 + 400) = 158/525 = \mathbf{0.301}. (d) \hat B = B' - 0.301 A = (3.00, 3.99, 1.00, 1.99, 5.00, 3.00), almost B: r = -0.07 again. (e) \hat B = B' - 0.6A = (3, 1, 0.4, -0.4, 5, 1.8) = B - 0.3A: a negative copy of A in B, r = -0.69: anti-colocalisation made by the correction.
B2. (a) FWHM $= 0.51 /1.4 = $ 189 nm; σ = 78 nm. (b) e^{-100^2/(4 \cdot 78^2)} = e^{-0.41} = \mathbf{0.66}; e^{-300^2/(4 \cdot 78^2)} = e^{-3.70} = \mathbf{0.025}. (c) $= 2 = 1.665 = $ 130 nm, about 0.7 FWHM.
B3. 1 px = 65 nm: e^{-65^2/(4 \cdot 80^2)} = e^{-0.165} = \mathbf{0.85}. 2 px: e^{-0.66} = \mathbf{0.52}. Half the overlap is gone at 2 px.
B4. (a) r: unchanged. Overlap coefficient: changes (towards 1). M1/M2: change unless the thresholds move with the offset (thresholds below the offset make M1, M2 → 1). Correction: subtract the offset, or set thresholds above it. (b) All of them: r gains a shared term, the overlap coefficient both the offset and the shared term, M1/M2 count background as signal where the background is high, ICQ too. Correction: subtract the background (top-hat or a measured background), measure in regions where it varies little.
B5. (1) Background subtraction: the later steps are linear and assume zero means “no dye”. (2) Compensation: the leaked light sits at A’s positions, so it must be removed before B is moved; with background still in, \hat s A would also subtract \hat s times A’s background. (3) Registration: shift B back by the bead-measured shift. (4) Region, saturated pixels excluded: the coefficients are only meaningful over the cells, and a clipped cloud bends them. (5) Coefficients with thresholds (then controls: shift curve, randomisation).
B6. (a) The background pixels are dark in both channels and the cells bright in both, so the scatter plot is a cloud stretched along the diagonal from (dark, dark) to (bright, bright): high r from the cell outline alone (unit 1 demo: 0.51 against −0.08 inside the cells). (b) Wherever A is bright, B gets 0.3 × A on top: B now contains a copy of A’s pattern, so the channels correlate (B1: −0.07 → 0.64; this unit’s demo: 0.42 → 0.64). (c) Two vesicles closer than about 200 nm blur into one spot in both channels: the image cannot separate them, so they overlap in the image whether or not the proteins are in the same vesicle.
B7. \operatorname{cov}(A, B + sA) = \operatorname{cov}(A, B) + s\operatorname{var}A = s\operatorname{var}A (independence); \operatorname{var}(B + sA) = \operatorname{var}B + s^2\operatorname{var}A. With \operatorname{var}B = c^2\operatorname{var}A: r = s\operatorname{var}A/\sqrt{\operatorname{var}A\,(c^2 + s^2)\operatorname{var}A} = s/\sqrt{c^2 + s^2}; c = 1 gives s/\sqrt{1 + s^2}. A weak B channel (c small) is hit hardest: the same leakage dominates it.
B8. \int g_1 g_2 is the value at \boldsymbol\delta of the correlation of two Gaussians, itself a Gaussian with variance \sigma_1^2 + \sigma_2^2. With normalised g_i = \frac{1}{2\pi\sigma_i^2}e^{-|\mathbf x - \mathbf m_i|^2/2\sigma_i^2}: \int g_1 g_2 = \frac{1}{2\pi(\sigma_1^2 + \sigma_2^2)}e^{-\delta^2/2(\sigma_1^2 + \sigma_2^2)}, and \int g_i^2 = \frac{1}{4\pi\sigma_i^2} (the same formula with \sigma_1 = \sigma_2 = \sigma_i, \delta = 0). The ratio: \frac{4\pi\sigma_1\sigma_2}{2\pi(\sigma_1^2 + \sigma_2^2)}e^{-\delta^2/2(\sigma_1^2 + \sigma_2^2)} = \frac{2\sigma_1\sigma_2}{\sigma_1^2 + \sigma_2^2}e^{-\delta^2/2(\sigma_1^2 + \sigma_2^2)}. Equal widths: e^{-\delta^2/4\sigma^2}.
B9. \operatorname{cov}(A, B) = k\operatorname{var}G; \operatorname{var}A = \operatorname{var}S_A + \operatorname{var}G, \operatorname{var}B = \operatorname{var}S_B + k^2\operatorname{var}G; so r = k\operatorname{var}G/\sqrt{(\operatorname{var}S_A + \operatorname{var}G)(\operatorname{var}S_B + k^2\operatorname{var}G)}. As a function of k > 0, k/\sqrt{\operatorname{var}S_B + k^2\operatorname{var}G} increases monotonically towards 1/\sqrt{\operatorname{var}G}: the larger the shared part in B, the larger r, up to \sqrt{\operatorname{var}G/(\operatorname{var}S_A + \operatorname{var}G)} as k \to \infty.
Explore
The demo simulates spots at pixel 65 nm, NA 1.4, A at 520 nm and B at 670 nm, in two cells: half of the A spots have a B partner at the chosen distance, plus independent B spots. Left: the truth (positions; yellow links are pairs), the measured merge, the corrected merge. Sliders: pair distance, share paired, a background shared by both channels, bleed-through, chromatic shift in x and y, photons; the top-hat radius and the estimated leakage \hat s for the corrections. Tick the corrections in pipeline order. The table gives Pearson r, M1/M2 with Costes thresholds, the ICQ and the share of A spots with a B spot within 130 nm (and its chance level), for an ideal microscope (same spots, PSF and noise, no confounds), the measured and the corrected images. The table updates shortly after a slider stops moving. Tab resolution sweep: r and the object fraction against the true pair distance.
E1. Preset clean. Read the table. Why are the three columns equal? What are the coefficients of an ideal microscope for this scene?
E2. Preset all paired at 0 nm: the red PSF is wider. Read r. What does the Gaussian formula predict for spots at the same place with σ = 78 and 100 nm, and why is the simulated r lower?
E3. Preset 100 nm chromatic shift: read the measured column. Then register with beads: read the bead shift estimate and the corrected column. Add a shift of −60 nm in y and keep registration on.
E4. Preset 30 % bleed-through: compensate (background subtracted, then compensated with \hat s = 0.3). Compare measured and corrected. Set \hat s to 0.15 (under) and read again. Then preset over-compensation (unrelated channels, \hat s = 0.6): what are the ideal, measured and corrected r?
E5. Preset uneven background shared by both. Which coefficients does the background change most? Tick subtract background and read again.
E6. Preset everything at once. Tick the corrections one by one in order (1, 2, 3) and write down r, M1/M2 and the object fraction after each step. Why is the result after step 2 worse than before? Then untick 1 and keep 2: what does compensation without background subtraction give?
E7. Tab resolution sweep. Read r and the object fraction at 0, 50, 100, 150 and 200 nm. Which method separates 0 nm from 50 nm? Which separates 100 nm from 150 nm, and why so abruptly?
E1. Without confounds the measured images are the ideal ones (same spots, PSF and noise), and with no correction ticked the corrected column is the measured one. Ideal: r = 0.418, M1/M2 = 0.52/0.40, ICQ 0.250, 50 % of the A spots with a B spot within 130 nm, 10 % by chance. Half of the A spots are paired, and many B spots are unpaired: the coefficients are moderate.
E2. r = 0.908 (M1/M2 = 0.73/0.76, 94 % of the A spots with a B spot within 130 nm). The formula: 2 \cdot 78 \cdot 100.5/(78^2 + 100.5^2) = 0.97 for noise-free spots; noise and the background between the spots lower the measured r. The wider red PSF itself costs only about 3 %.
E3. Shift 100 nm: r = 0.303, M1/M2 = 0.45/0.31, object fraction 43 % (ideal 0.418, 0.52/0.40, 50 %). Registration: the beads give a shift of 100.8 nm (true 100); corrected r = 0.420, M1/M2 = 0.53/0.39, 50 %: back to the ideal. With −60 nm in y: measured r = 0.248; estimate (100.8, −61.8) nm; corrected r = 0.419.
E4. 30 % leakage: measured r = 0.641, M1/M2 = 0.73/0.62, objects 89 % (the leaked copies are detected as B spots: 68 instead of 49); corrected with \hat s = 0.3: r = 0.381, 0.51/0.40, 50 %. Under-compensation (\hat s = 0.15): r = 0.527, 0.72/0.58, 76 %. Over-compensation: ideal r = 0.005, measured 0.455 (the leakage alone), corrected −0.474, 4 % of the A spots with a B spot nearby (fewer than the 10 % expected by chance); Costes’ threshold is undefined for negative r and falls back to fixed thresholds (marked *).
E5. Measured: r = 0.485 (from 0.418), but M1/M2 jump to 0.84/0.88 (from 0.52/0.40): the thresholds now count the bright background as signal, and the ICQ drops to 0.178 (the PDM sign pattern follows the background). The object fraction hardly changes (53 %): spot detection is local. After the top-hat: r = 0.354, M1/M2 = 0.52/0.39: the thresholds are right again; r stays below the ideal 0.418, because subtraction removes the background but not its shot noise, which is independent in the two channels.
E6. Measured: r = 0.595, M1/M2 = 0.93/0.96, 70 %. After 1 (background): 0.496, 0.68/0.52, 72 %. After 2 (compensation): 0.182, 0.41/0.19, 42 %: the leakage is gone, but the channels are still 100 nm apart, so the real pairs no longer overlap well: the shift was hidden under the leakage. After 3 (registration): 0.343, 0.52/0.37, 53 % (ideal 0.418, 0.52/0.40, 50 %). Compensation without background subtraction: r = 0.325, M1/M2 = 0.51/0.47, 43 %: still shifted, and the background left in distorts the thresholds.
E7. r: 0.91 (0 nm), 0.84 (50), 0.67 (100), 0.48 (150), 0.30 (200). Object fraction: 94 %, 96 %, 78 %, 13 %, 7 %. Neither method separates 0 from 50 nm: both are far below the resolution (FWHM ≈ 184 nm in this model). The object fraction separates 100 from 150 nm sharply because it thresholds a measured distance at d_\text{max} = 130 nm (with the localisation error smearing the step); r fades smoothly with the overlap of the PSFs, as the Gaussian formula says (dashed curve).
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_confounds/exercise.rs. Pearson’s r over a mask is given.
C1. leak_slope(a, b, mask) and compensate(b, a, s): the slope through the origin \sum AB/\sum A^2 of a single-stained control, and \hat B = B - \hat s A at every pixel. The checks are B1(c, d, e).
C2. gaussian_overlap(s1, s2, delta): the normalised overlap of two Gaussian spots, \frac{2\sigma_1\sigma_2}{\sigma_1^2 + \sigma_2^2}\exp\big(-\delta^2/2(\sigma_1^2 + \sigma_2^2)\big). The checks are B2 and B3.
C3. subtract_background(img, radius): the white top-hat, image minus its opening with a square of side 2r + 1:
for every pixel: low(x, y) = minimum of img over the square around (x, y)
for every pixel: open(x, y) = maximum of low over the square around (x, y)
result = img − open
The checks: a ramp becomes 0, a constant offset becomes 0, a one-pixel spot keeps its height.
C4. estimate_shift(a, b, max_shift) and shift_image(img, dx, dy): the chromatic shift from bead images, and the shift back:
subtract the mean from A and from B
for every (tx, ty) with |tx|, |ty| ≤ max_shift:
c(tx, ty) = Σ A(x, y) · B(x + tx, y + ty) # zero outside the image
(tx, ty) = the best one; refine tx with a parabola through c(tx−1, ty), c(tx, ty), c(tx+1, ty),
and ty likewise
shift back: B_registered(x, y) = B(x + tx, y + ty), read between pixels by bilinear interpolation
The checks use bead images with known shifts, and small images shifted by whole and half pixels.
Then cargo run -p colocalisation -- coloc-confounds and open the pictures in out/coloc-confounds/. The program makes the synthetic two-cell scene of this day’s programs with half of B’s structures on A’s, once ideal and once measured through a microscope that shifts B by 1.5 px (about 100 nm at 65 nm pixels), leaks 30 % of A into B and adds a haze shared by both channels. It measures the leak on a single-stained control and the shift on beads, then corrects in order (background, compensation, registration), printing r inside the cells after each step; then it tries the wrong order, skips the background, and uses wrong leak factors. Last, the overlap of a 78 nm and a 100 nm spot at several distances.
C5. Write down r for the ideal and the measured images and after each step. Why does r drop below the ideal after compensation, and rise again after registration? Look at 2-compensated.png and 3-registered.png.
C6. Why is “registered before compensating” worse, and why does skipping the background leave r so high? Compare with B5.
C7. From the printed overlaps, at what distance do a green and a red spot overlap by one half? Compare with B2(c).
The model solution is colocalisation/src/coloc_confounds/solution.rs in the download.
C5. Ideal r = 0.403, measured 0.716. The control gives \hat s = 0.291 (true 0.3) and the beads a shift of (1.51, 0.00) px (true (1.5, 0)). After background subtraction 0.533, after compensation 0.289, after registration 0.401, back at the ideal. After compensation the leaked copy is gone, but the shared structures still sit 1.5 px apart: the shift was hidden under the leak (in 2-compensated.png the shared spots show green and magenta halves side by side). After registration they are white again (3-registered.png), as in the ideal image. This is E6.
C6. Registering first moves the leaked copy of A in channel B away from A’s position; compensation then subtracts \hat s A at A’s position, leaving a positive copy beside a negative one: r = 0.328 instead of 0.401. Without background subtraction r = 0.611: compensation subtracts \hat s times A’s haze, but B’s haze is a full copy of the shared haze, so most of it remains and correlates the channels (B5: background first, because the later steps assume that zero means no dye). Wrong factors: \hat s = 0.15 gives 0.494 (leak left over), \hat s = 0.6 gives 0.142 (a negative copy of A in B).
C7. 0.71 at 100 nm and 0.48 at 150 nm, so one half at a little under 150 nm. For two equal spots of 78 nm, B2(c) gives 130 nm; the red spot is wider, which spreads the overlap a little further (and lowers it to 0.97 at 0 nm).
Where this leads
This was the last unit of the colocalisation day. The other fluorescence days of Part VIII build on the same two-channel forward model: FRET (day 49) measures proximity below 10 nm by energy transfer, and needs exactly the bleed-through and cross-excitation corrections of this unit (sensitised emission); FLIM (day 50) measures fluorescence lifetimes. The unmixing of many overlapping dyes is the spectral unmixing day; comparing coefficients between conditions is the statistics of the evaluation day.