Exercise: objects instead of pixels

Goal

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

  • compute nearest-neighbour distances between two channels, the fraction within d_\text{max}, and the difference between nearest-neighbour and one-to-one pairing,
  • compute the chance level 1 - e^{-\rho_B\pi d^2} and the corrected fraction,
  • apply three overlap definitions to labelled objects,
  • predict the bias of a measured distance from the localisation precision,
  • explain why random relocation must stay inside the cells,
  • (math) derive the chance level, the distance bias and K_{AB}(r) = \pi r^2.

Do the pen-and-paper part first, then the demo.

Background

Idea What it means
Nearest-neighbour distance for each A spot, the distance to the closest B spot
One-to-one pairing each B spot used at most once; most pairs within d_\text{max}, then the smallest total distance
Chance level random B spots of density \rho_B: P(\text{a B spot within } d) = 1 - e^{-\rho_B\pi d^2}
Corrected fraction (f_\text{obs} - f_\text{chance}) / (1 - f_\text{chance})
Overlap definitions any pixel in B; more than 50 % of the pixels in B; centroid in B
Cross-K, L K(r) = \frac{|W|}{n_An_B}\#\{(i,j): d_{ij} \le r\}; L(r) - r = \sqrt{K/\pi} - r; 0 for independence
Distance bias E[d_\text{meas}^2] = d^2 + 2(\sigma_A^2 + \sigma_B^2); for d = 0 the mean is \sqrt{\sigma_A^2 + \sigma_B^2}\,\sqrt{\pi/2}

By hand

B1. Five A spots and six B spots (pixel coordinates (x, y)):

A (2, 3) (5, 8) (7, 8) (12, 4) (15, 15)
B (3, 3) (6, 9) (9, 12) (12, 7) (18, 15) (1, 14)
  1. For each A spot, find the nearest B spot and the distance. (b) What fraction of A spots has a B spot within d_\text{max} = 2 px? (c) Two A spots share the same nearest B spot. With one-to-one pairing (within 2 px), what fraction is paired? (d) Which of the two numbers would you report as “A spots with a B partner”, and why?

B2. Chance level at d = 2 px. (a) Compute 1 - e^{-\rho_B\pi d^2} for \rho_B = 0.002 px⁻² and \rho_B = 0.02 px⁻². (b) In both cases 30 % of the A spots have a B spot within 2 px. Compute the corrected fraction. (c) A cell of 6000 pixels: how many B spots are these two densities?

B3. Two small label images (10 × 7 pixels; A objects labelled 1–4, B objects shown as 1):

\text{A} = \begin{bmatrix} 0&0&0&0&0&0&0&0&0&0\\ 1&1&1&0&2&2&2&0&3&0\\ 0&0&0&0&2&2&2&0&0&0\\ 0&0&0&0&2&2&2&0&0&0\\ 0&4&4&4&0&0&0&0&0&0\\ 0&4&4&4&0&0&0&0&0&0\\ 0&4&4&4&0&0&0&0&0&0 \end{bmatrix}, \qquad \text{B} = \begin{bmatrix} 0&0&1&1&0&0&0&0&0&0\\ 0&0&1&1&0&1&1&1&0&1\\ 0&0&0&0&0&1&1&1&0&1\\ 0&0&0&0&0&1&1&1&0&0\\ 0&0&0&0&0&0&0&0&0&0\\ 0&0&1&0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0&0&0&0 \end{bmatrix}.

For each A object compute its area, the number of its pixels in B, and its centroid (column, row). Then give the share of A objects colocalised by (i) any overlap, (ii) more than 50 % overlap, (iii) centroid in B.

B4. Two-colour spots of the same molecule (true distance 0) are localised with a precision of 20 nm per axis in each channel. (a) Predict the average measured distance, using \sqrt{\pi/2} \approx 1.25. (b) What is the root-mean-square measured distance? (c) The true distance is 60 nm instead: what is the rms measured distance?

B5. B spots are relocated at random to build a null distribution. Explain in two or three sentences why the relocation must stay inside the cell mask, and what goes wrong if the whole image is used.

Math questions

B6. (math) B spots form a Poisson process of density \rho_B. Derive P(D \le d) = 1 - e^{-\rho_B\pi d^2} for the distance D from a given point to the nearest B spot, its density, and its mean 1/(2\sqrt{\rho_B}).

B7. (math) The true separation vector is \mathbf d; the two centroids have independent errors with mean 0 and variance \sigma_A^2, \sigma_B^2 per axis. Show that E[|\mathbf d_\text{meas}|^2] = d^2 + 2(\sigma_A^2 + \sigma_B^2). Why is E[|\mathbf d_\text{meas}|] \ne d?

B8. (math) Show that for B spots independent of the A spots and spread uniformly with density \rho_B = n_B/|W| (ignoring edges), E[\hat K_{AB}(r)] = \pi r^2, and hence L(r) = r.

B1. (a) A1 (2, 3) → B1 (3, 3): 1.00. A2 (5, 8) → B2 (6, 9): \sqrt 2 = 1.41. A3 (7, 8) → B2 (6, 9): 1.41. A4 (12, 4) → B4 (12, 7): 3.00. A5 (15, 15) → B5 (18, 15): 3.00. (b) A1, A2, A3 are within 2 px: 3/5 = 60 %. (c) B2 can serve only one of A2 and A3; the other’s next B spot, B3 (9, 12), is 4.47 px away, beyond 2 px. One-to-one: 2/5 = 40 %. (d) If each B spot can partner only one A spot (one complex per pair), one-to-one counts real pairs; nearest neighbour counts how often an A spot is near some B, which over-counts when B spots are shared.

B2. (a) \rho_B\pi d^2 = 0.002 \cdot \pi \cdot 4 = 0.0251: chance 2.5 %; 0.02 \cdot \pi \cdot 4 = 0.251: chance 22.2 %. (b) Sparse: (0.30 - 0.025)/(1 - 0.025) = \mathbf{0.28}; dense: (0.30 - 0.222)/(1 - 0.222) = \mathbf{0.10}. The same 30 % is strong colocalisation in one cell and almost chance in the other. (c) 12 and 120 B spots.

B3. A1: area 3, 1 pixel in B (at column 2, row 1), centroid (1, 1), not in B. A2: area 9, 6 pixels in B (columns 5–6), centroid (5, 2), in B. A3: area 1, 0 pixels in B (B’s object at column 9 only touches it), centroid (8, 1), not in B. A4: area 9, 1 pixel in B (its centre, (2, 5)), centroid (2, 5), in B. (i) Any overlap: A1, A2, A4: 75 %. (ii) More than 50 %: only A2 (67 %): 25 %. (iii) Centroid in B: A2 and A4: 50 %. Three definitions, three answers.

B4. (a) \sqrt{20^2 + 20^2} = 28.3 nm, times 1.25: 35 nm (35.4). (b) \sqrt{2(\sigma_A^2 + \sigma_B^2)} = \sqrt{1600} = 40 nm. (c) \sqrt{60^2 + 1600} = \sqrt{5200} = 72 nm (the mean is 67 nm).

B5. B spots can only be where B can be: inside the cells (perhaps only in the cytoplasm). Relocations over the whole image put many spots outside the cells, far from all A spots, so random arrangements look less colocalised than real unrelated spots would; the observed data then look “attracted” although they are not. Relocating inside the mask keeps the density and the geometry of the cells.

B6. The disc of radius d around the point contains a Poisson number of B spots with mean \rho_B\pi d^2; D > d means it contains none: P(D > d) = e^{-\rho_B\pi d^2}, so P(D \le d) = 1 - e^{-\rho_B\pi d^2}. Density: \frac{d}{dd}P(D \le d) = 2\pi\rho_B d\,e^{-\rho_B\pi d^2}. Mean: E[D] = \int_0^\infty P(D > d)\,dd = \int_0^\infty e^{-\rho_B\pi d^2}\,dd = \frac12\sqrt{\pi/(\rho_B\pi)} = 1/(2\sqrt{\rho_B}).

B7. \mathbf d_\text{meas} = \mathbf d + \boldsymbol\varepsilon_B - \boldsymbol\varepsilon_A. E|\mathbf d_\text{meas}|^2 = d^2 + 2\,\mathbf d\cdot E[\boldsymbol\varepsilon_B - \boldsymbol\varepsilon_A] + E|\boldsymbol\varepsilon_B - \boldsymbol\varepsilon_A|^2 = d^2 + 0 + 2(\sigma_A^2 + \sigma_B^2) (two axes, independent errors). The length is a nonlinear (convex) function of the vector: errors in every direction lengthen the vector on average more than they shorten it, and for d = 0 every error lengthens it. So E|\mathbf d_\text{meas}| > d, most for d \lesssim \sqrt{\sigma_A^2 + \sigma_B^2}.

B8. E\big[\#\{j : d_{ij} \le r\}\big] = \rho_B\pi r^2 for each A spot i (the expected number of uniformly spread B spots in a disc of area \pi r^2, independent of where A is). Summing over the n_A spots and multiplying by |W|/(n_An_B): E[\hat K] = \frac{|W|}{n_A n_B} n_A \frac{n_B}{|W|}\pi r^2 = \pi r^2. Then L(r) = \sqrt{K/\pi} = r.

Explore

The demo simulates two channels at physical scale: pixel 65 nm, NA 1.4, A at 520 nm, B at 670 nm, inside two cells (25 µm²). n_A spots in A, a share of them with a B partner at a true distance (random direction), extra independent B spots, photons per spot, a chromatic shift of B. Spots are detected in both channels (circles). Tabs: distances (histogram of A → B distances with the chance density, d_\text{max} and readouts; nearest neighbour or one-to-one), cross-L (L(r) - r with the envelope of 39 relocations of B inside the cells), objects (larger discs, three overlap definitions), below the resolution (measured distances of the truly paired spots against the true distance and the predicted mean). All random numbers have fixed seeds.

E1. Preset pairs at 0 nm. How many spots are detected in each channel (out of how many)? Read the fraction within 130 nm, the chance fraction and the corrected fraction, and the pixel r. Switch to one-to-one; then set d_\text{max} = 260 nm and compare nearest neighbour with one-to-one.

E2. Presets independent, sparse and independent, dense. Compare the observed and the chance fractions. Why does the dense preset detect only about two thirds of its B spots, and what does that do to the chance level? Set d_\text{max} = 260 nm in the dense preset.

E3. Preset chromatic shift 100 nm. What happens to the histogram and the fraction within 130 nm? What is the fraction within 200 nm? What should be done instead of widening d_\text{max}?

E4. Preset few photons: missed detections (all A spots paired at 0 nm, 80 photons per spot). Read the detections and the fraction within 130 nm. Then raise the photons to 2000. Why does the fraction change although the biology did not?

E5. Tab cross-L: presets attraction and independent. Where is the observed curve relative to the envelope? In the attraction preset, set the true distance to 300 nm: where does the curve leave the envelope? Go to the distances tab: what do the fraction within 130 nm and the pixel r say now?

E6. Tab objects: presets big objects: overlap or centroid? and small A inside big B. Read the three shares for each, and explain the difference with B3.

E7. Tab below the resolution: presets pairs at 60 nm and few photons: the distance bias. For each read the precision of A and B, the measured and the predicted mean distance, and the pixel r. In the first preset set the true distance to 0: what is the measured mean now?

E1. A: 37 of 40 detected, B: 41 of 48 (B spots that happen to lie close together merge into one). Within 130 nm: 64.9 %, chance 8.3 %, corrected 61.7 % (the true share of paired spots is 60 %). Pixel r = 0.58. One-to-one at 130 nm: also 64.9 %. At 260 nm: nearest neighbour 81.1 %, one-to-one 67.6 %, chance 29.2 %: with a larger d_\text{max}, single B spots start to serve two A spots, and the chance level rises fast.

E2. Sparse (12 B spots, 14 detected: two false detections): observed 2.7 %, chance 2.9 %: nothing beyond chance. Dense (120 B spots): 80 detected, because spots closer than about the PSF width merge into one; observed 21.6 %, chance 15.5 %, computed from the detected density, which underestimates the true density (with 120 spots it would be 22 %). With d_\text{max} = 260 nm: 51.4 % observed against 49.0 % by chance: a large fraction that means nothing.

E3. The whole histogram moves out to about 100 nm; the fraction within 130 nm drops from 64.9 % to 54.1 % (pixel r from 0.58 to 0.46). Within 200 nm it is 75.7 %, but a larger d_\text{max} also raises the chance level. Measure the shift on two-colour beads and correct it (register the channels, unit 4); then measure.

E4. 80 photons: all 40 A spots but only 32 of 40 B spots detected; 62.5 % within 130 nm. At 2000 photons: 37 of 40 A (spots too close together merge), 32 of 40 B, 94.6 %. With few photons the dim B spots are missed or badly located (precision about 30 nm), so true pairs are lost. The fraction measures the detection as much as the biology.

E5. Attraction: far above the envelope at every radius (L - r ≈ 250 nm at 100 nm). Independent: inside the envelope everywhere. True distance 300 nm: the curve is inside the envelope up to 300 nm and above it from 325 nm on; the fraction within 130 nm is 5.4 % against 8.8 % by chance, and pixel r = 0.05. Only the cross-L function shows that the B spots sit at a preferred distance of about 300 nm from the A spots.

E6. Big A (radius 6 px) next to small B (radius 4 px, 5 px away): any overlap 78.6 %, more than 50 % 0 %, centroid 0 %. Small A (4 px) next to big B (6 px): 78.6 % for all three. As in B3, the definitions agree only when the A objects are small compared with the B objects they touch.

E7. 60 nm, 2000 photons: precision 9 nm (A) and 14 nm (B), measured mean 64 nm, predicted 62 nm; pixel r = 0.86, so pixels call these pairs colocalised, while the centroids recover the 60 nm. True distance 0 (2000 photons): measured mean 22 nm. Few photons (150, true 0 nm): precision 24 and 33 nm, measured mean 43 nm, predicted 52 nm. The bias grows with the localisation error; a “distance” of 40 nm can be a true 0.

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_objects/exercise.rs. Spots are lists of positions (x, y); the distance between two points is given.

C1. nearest(a, b) and fraction_within(a, b, d_max): for every A spot the nearest B spot and its distance, then the share of A spots with that distance \le d_\text{max}:

for every A spot p:
    best = infinity
    for every B spot q:  if dist(p, q) < best: best = dist(p, q), remember q
fraction = (number of A spots with best ≤ d_max) / (number of A spots)

The check is B1(a, b).

C2. one_to_one(a, b, d_max): the number of pairs when every spot may be used once. Take the closest pairs first (a greedy approximation of the optimal assignment of the lecture):

pairs = all (A spot, B spot) with distance ≤ d_max, sorted by distance
for each pair, closest first:
    if neither spot is used yet: count it, mark both as used

The check is B1(c): 2 pairs, not 3.

C3. chance_level(rho, d) = 1 - e^{-\rho\pi d^2} and corrected(f_obs, f_chance) = (f_\text{obs} - f_\text{chance})/(1 - f_\text{chance}). The check is B2.

C4. overlap_shares(a, b): the share of labelled A objects that overlap the B mask by any pixel, by more than half of their pixels, and with their centroid:

for every pixel with label l > 0:
    area[l] += 1;  sum_x[l] += x;  sum_y[l] += y
    if B is on there: in_B[l] += 1
for every label l:
    any       if in_B[l] > 0
    majority  if in_B[l] > area[l] / 2
    centroid  if B is on at (round(sum_x[l] / area[l]), round(sum_y[l] / area[l]))

The check is B3: 75 %, 25 % and 50 %.

C5. cross_k(a, b, area, r) and relocate(n, mask, rng): the cross-K function (count the pairs within r, times |W|/(n_A n_B)), and n random positions inside the mask (draw anywhere in the image, keep the point only if it falls in the mask). The checks: a small example by hand, L(r) \approx r for independent spots (B8), and relocated points all inside the cells.

Then cargo run -p colocalisation -- coloc-objects and open the pictures in out/coloc-objects/ (A spots green, B spots magenta rings, yellow links to a B spot within d_\text{max}). The program simulates spot positions in two cells (pixel 65 nm, 33.8 µm², localisation precision 20 nm per axis, d_\text{max} = 130 nm): 40 A spots with 60 % paired at 0 nm plus 8 extra B spots (scene1), independent sparse (12 B spots) and dense (120), and all A spots paired at 300 nm (scene4). It prints the observed, chance and corrected fractions and the one-to-one share; then L(r) - r for r = 50 … 500 nm against an envelope of 39 relocations of B, inside the cells and over the whole image; and the mean measured distance of 2000 pairs.

C6. In which scenes is the observed fraction more than chance? What happens with 120 independent B spots, at 130 and at 260 nm?

C7. Compare the two envelopes for the independent spots. Which one is honest, and why does the other call independent spots “attracted”? Where does the curve of the pairs at 300 nm leave the envelope, and what did the fraction within 130 nm say about the same scene?

C8. Predict the mean measured distance for true distances 0, 60 and 200 nm from B4 and the Rayleigh formula, then compare with the printed values.

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

C6. Pairs at 0 nm: 65.0 % observed against 4.9 % by chance, corrected 63.2 % (the true paired share is 60 %, and one-to-one gives exactly 60.0 %: a few unpaired A spots find a B spot by chance). Independent and sparse: 2.5 % against 1.9 %. Independent and dense: 12.5 % against 17.2 % (corrected −5.7 %): nothing beyond chance, and 12.5 % alone would have looked like some colocalisation. At 260 nm: 50.0 % observed, 53.0 % by chance: a large fraction that means nothing. Pairs at 300 nm: 7.5 % against 6.1 %: at 130 nm these pairs are invisible.

C7. For the independent spots the curve stays inside the envelope from relocations inside the cells at every radius, but lies above the whole-image envelope from 350 nm on. Relocations over the whole image put B spots outside the cells, far from every A spot, so random spots look less close than real unrelated spots inside the cells can be, and the data look “attracted”. The pairs at 300 nm are inside the envelope up to 250 nm and above it from 300 nm on: the spots keep a distance of about 300 nm, which the fraction within 130 nm (7.5 % against 6.1 %) cannot see.

C8. B4: true 0 nm gives \sqrt{20^2 + 20^2}\cdot\sqrt{\pi/2} = 35.4 nm; the program measures 35.6 nm. 60 nm: rms \sqrt{60^2 + 1600} = 72 nm, mean about 67 nm (the lecture); measured 67.3 nm. 200 nm: 202.0 nm, almost no bias, because the distance is much larger than the localisation error (28 nm for the difference vector per axis).

Where this leads

Objects and distances make the question precise, but every channel still carries what the microscope added: the next unit, what the microscope does to colocalisation, puts background, bleed-through, chromatic shift and the resolution limit into one forward model and corrects them in order. Below about 10 nm even centroids are not enough: FRET, the next day (day 49), measures proximity by energy transfer. For localisation microscopy, where the data are lists of coordinates rather than images, the same distance statistics are applied to the coordinates themselves. The full toolbox for point patterns (the G, F and J functions, edge corrections, two cell types and an uneven density) is the spatial statistics day (day 19); its cross-type functions are the cross-K of this unit.