Exercise: two cell types and an uneven density

Goal

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

  • compute a cross-K by hand and read cross-L and the mark connection function,
  • choose between independence and random labelling for a biological question, and say how to simulate each,
  • predict what the window and an uneven density do to the plain K, and what the inhomogeneous K does about it,
  • (math) prove the random-labelling identity, the inhomogeneous-K result for Poisson patterns, and why relocation is valid in a mask where the toroidal shift is not.

Background

Idea What it means
Cross-K \hat K_{01} = \frac{|W|}{n_0n_1}\sum_{i\in 0}\sum_{j\in 1}w_{ij}[d_{ij} \le r]; independence: \pi r^2
Independence null move one type as a whole: relocate in the tissue, or shift (rectangles only)
Random labelling null keep positions, permute types; K_{00} = K_{11} = K_{01} = K
Mark connection p_{01}(r): share of pairs at distance r that are mixed; random labels: 2p_0p_1
Inhomogeneous K pairs weighted by 1/(\hat\rho(\mathbf x_i)\hat\rho(\mathbf x_j)); inhomogeneous Poisson: \pi r^2

By hand

B1. In a 10 × 10 µm window there are 4 immune cells (type 1) and 5 tumour cells (type 0). The distances between immune cells (rows) and tumour cells (columns), in µm:

T1 T2 T3 T4 T5
I1 1.41 6.40 6.71 4.12 9.90
I2 3.61 1.41 3.16 4.00 5.00
I3 5.00 4.47 2.00 7.62 6.08
I4 5.00 3.16 5.83 2.83 6.08
  1. Count the immune–tumour pairs closer than r = 1, 2, 3 µm.
  2. Compute \hat K_{10}(r) = \frac{|W|}{n_1n_0} \cdot (count), compare with \pi r^2, and give L_{10} - r.

B2. For each question, choose independence or random labelling, and say how you would simulate the null: (a) Do T cells sit closer to tumour cells than if the two populations had nothing to do with each other? (b) Among the epithelial cells of a crypt, are the dividing cells (stained for a mitosis marker) randomly placed? (c) Two kinds of receptor spots in a membrane, imaged in two colours: are they associated?

B3. 30 % of the cells are immune cells. (a) Under random labelling, what is p_{01}(r)? (b) Observed p_{01}(10\,\mu m) = 0.20. What does it suggest? (c) Observed p_{01}(10\,\mu m) = 0.55?

B4. Cells placed at random within a tissue that covers half of a 200 × 200 µm image. (a) What is \hat\rho with the image as the window, and with the tissue mask? (b) With the image as the window, roughly what is the plain \hat K(r) at small r, and L(r) - r at r = 10 µm? (Every cell has the neighbours of a random pattern of the true density.)

B5. Cells denser near a vessel, no interaction. Predict the plain L - r (against CSR) and the inhomogeneous L - r with a well-chosen bandwidth. What happens to the inhomogeneous curve if the bandwidth is far too large? Far too small?

B1. (a) r = 1: 0 pairs; r = 2: 3 (I1–T1 1.41, I2–T2 1.41, I3–T3 2.00); r = 3: 4 (adding I4–T4 2.83). (b) |W|/(n_1n_0) = 100/20 = 5: \hat K_{10} = 0, 15, 20 against \pi r^2 = 3.1, 12.6, 28.3; L_{10} - r = −1.00, +0.19, −0.48. Nothing clear, and 9 cells cannot show anything: this is only the arithmetic.

B2. (a) Independence: keep the tumour cells, relocate the T cells at random inside the tissue (or, in a rectangle, shift them as a whole). (b) Random labelling: keep all epithelial cell positions, permute the “dividing” label. (c) Independence: two channels, two separately formed populations; shift one channel (in a rectangle) or relocate its spots inside the cell mask.

B3. (a) 2 \cdot 0.3 \cdot 0.7 = 0.42 at every r. (b) Fewer mixed pairs than chance: cells of the same type are neighbours more often; segregation at 10 µm. (c) More mixed pairs: the types mix more than chance (immune cells next to tumour cells).

B4. (a) Image: n/40\,000; mask: n/20\,000, twice as high. (b) Each cell has about \rho_\text{true}\pi r^2 = 2\hat\rho_\text{image}\pi r^2 neighbours within small r, so \hat K \approx 2\pi r^2 and L = \sqrt2\,r: L - r = 0.41 r, about 4.1 µm at r = 10. A strong “clustering” that is only the window.

B5. Plain: L - r > 0 at every r, rising with r (pairs are concentrated near the vessel). Inhomogeneous with a bandwidth that follows the gradient: about 0. Bandwidth far too large: \hat\rho is almost constant, the inhomogeneous K becomes the plain K: “clustered” again. Far too small: \hat\rho follows the random clumps of individual cells and the curve drops below 0 (over-corrected).

Math questions

B6. (math) Prove K_{11} = K_{00} = K_{01} = K under random labelling (labels independent of positions, type 1 with probability p_1). Show also p_{01}(r) = 2p_0p_1.

B7. (math) Show that the inhomogeneous K with the true ρ and translation weights has expectation \pi r^2 for an inhomogeneous Poisson process. Why does a leave-one-out \hat\rho matter when ρ is estimated from the same points?

B8. (math) (a) Why does relocating type 1 uniformly in the tissue mask give a valid null for “type 1 is a CSR pattern in the tissue, independent of type 0”? (b) Why is the toroidal shift valid in a rectangle for any stationary pair of patterns, but not in an irregular mask?

B6. For a pair of points at distance r, its two labels are independent of the positions and of each other: P(1,1) = p_1^2, P(0,0) = p_0^2, P(0,1) = p_0p_1 for an ordered pair. The pair density of type a around type b is therefore p_ap_b\,\rho^2g(r), and K_{ab}(r) = \frac{1}{p_a\rho\,p_b\rho}\int_0^r p_ap_b\rho^2 g(s)\,2\pi s\,ds = K(r). Of all pairs at distance r, the mixed ones (either order) have share 2p_0p_1, independent of r.

B7. For a Poisson process with intensity ρ(·), E\sum_{i\ne j}h(\mathbf x_i, \mathbf x_j) = \int\!\!\int h(\mathbf u, \mathbf v)\rho(\mathbf u)\rho(\mathbf v)\,d\mathbf u\,d\mathbf v. With h = w(\mathbf v - \mathbf u)[|\mathbf u - \mathbf v| \le r]/(\rho(\mathbf u)\rho(\mathbf v)) the ρ’s cancel, leaving \int\!\!\int_{W\times W} \frac{|W|[|\mathbf v - \mathbf u| \le r]}{|W \cap W_{\mathbf v - \mathbf u}|}d\mathbf u\,d\mathbf v = |W|\,\pi r^2 (the set-covariance argument of unit 3), so E[\hat K_\text{inhom}] = \pi r^2. If \hat\rho(\mathbf x_i) includes the kernel of \mathbf x_i itself, it is inflated at every data point (by k_\sigma(0)/e), most where points are sparse, and \hat K_\text{inhom} is biased down; leaving the point out removes this self-contribution.

B8. (a) Under that null, given n_1, the type-1 points are n_1 independent uniform points in the mask (conditional uniformity of the Poisson process, unit 1), whatever type 0 does; relocation draws exactly that. If type 1 is itself clustered, relocation destroys its structure and the null is wrong; then model type 1 instead. (b) On a torus a stationary pattern shifted by any vector has the same distribution, and the shift keeps type 1’s own structure and its relation to nothing in particular: under independence, the shifted pair has the same distribution as the observed one. An irregular mask is not a torus: shifted points leave the tissue or land in holes, and the wrap-around joins pieces of tissue that are not neighbours.

Explore

A synthetic tissue (mask with a vessel) of tumour cells (orange) and immune cells (blue) from several generators. Tabs: cross-L (immune around tumour), L of all cells, mark connection, inhomogeneous L (the kernel estimate ρ̂ is drawn under the tissue; bandwidth slider). Choose the null below the plot; the window can be the tissue mask or the whole image. Each curve is tested with 39 simulations (pointwise band grey, global MAD band green, the null mean dashed) and the MAD p-value.

E1. Preset attraction: cross-L above. Read L_{01}(10) - 10 and the MAD p. Lower the strength slider to 0: what happens?

E2. Presets independent: inside and segregated domains: below. Read the p-values. For the segregated tissue, switch to the mark connection tab: what is the random-labelling value, and what is observed at 5 and 20 µm?

E3. Preset packed tissue: relocation says repulsion, then packed tissue: labels say random. Explain the two results with the picture of the tissue.

E4. Presets gradient: relocation says attraction and random labelling ignores the gradient. Which one answers “are immune cells attracted to tumour cells?” here, and why?

E5. Presets the image as the window and the tissue mask as the window. Read L - r at 20 µm and p for both.

E6. Preset inhomogeneous L: no interaction, then density gradient: plain L says clustered. Compare. Now move the bandwidth to 20 µm, then to 10 µm (release the slider each time). What happens?

E7. Presets bandwidth too small and large bandwidth (clustered cells). Which conclusion is right? What would you need to decide it?

E1. 148 tumour and 90 immune cells: L_{01}(10) - 10 = 4.1 µm, p = 0.025 (the smallest possible with 39 simulations). At strength 0 the immune cells are placed at random in the tissue: the curve falls into the band.

E2. Independent (tumour nests, independent immune cells): inside, p = 0.65. Segregated: far below, L_{01}(10) - 10 = -5.6, p = 0.025. Mark connection: random labelling gives 0.42; observed 0.09 at 5 µm and 0.19 at 20 µm: neighbours are mostly of the same type.

E3. Packed tissue (202 cells, minimum distance 7.5 µm, types random). Relocation: L_{01} - r = -7 at 7 µm, p = 0.025: “repulsion”, because relocated immune cells may come close to tumour cells while real cells cannot. Permuted labels: the null has the same packing, p = 0.83: the types are random, which is the truth.

E4. Both types are denser near the vessel. Relocation (uniform in the tissue) reports “attraction” (p = 0.025) because both types crowd at the vessel. Random labelling keeps the positions and finds nothing (p = 0.90). Here the types were assigned at random, so random labelling answers the question; relocation would need an inhomogeneous null (relocate according to the density).

E5. Image window: L - r = 10.6 µm at 20 µm, p = 0.025: “clustered”, because 60 % of the window is empty glass. Tissue mask: L - r ≈ 0.1, p = 0.73.

E6. Plain L against CSR: 3.5 µm at 20 µm, p = 0.025. Inhomogeneous L (σ = 40 µm) against inhomogeneous Poisson from ρ̂: p = 0.20, no interaction beyond the density. At σ = 20 µm the curve lies below the band (p = 0.025, slightly over-corrected), at σ = 10 µm inside again (p = 0.83): the verdict depends on the bandwidth, which is why it must be fixed in advance.

E7. σ = 5 µm: p = 0.125 (clusters explained as density); σ = 40 µm: p = 0.025, clustered beyond the density. Both are internally consistent. The data alone cannot decide: you need a reason for the scale of “density” (e.g. a tissue compartment of 50 µm, or the density of another cell type, or a covariate such as the distance to the vessel), chosen before looking.

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 spatialstats/src/marked_patterns/exercise.rs. Points are pairs (x, y) in µm in the window [0, a] \times [0, b], labels are 0 (tumour) and 1 (immune). The pattern generators, the translation weight and Φ are given.

C1. cross_k(type0, type1, a, b, r, translation): the cross-K, type-1 points around type-0 points:

s = 0
for every point i of type 0:
    for every point j of type 1:
        if distance(i, j) ≤ r:  s = s + w_ij        # translation weight, or 1
K01 = |W| / (n0 · n1) · s

The check is B1 (with cells placed so that the close pairs have the distances of the table), and that independent types give K_{01} \approx \pi r^2 even when one type is clustered.

C2. permute_labels(labels, rng): the null model of random labelling, a random order of the same labels (the Fisher–Yates shuffle):

for k = last index down to 1:
    j = a random whole number from 0 to k
    swap labels k and j

The check: the labels are kept, and every order is equally likely.

C3. mark_connection(points, labels, r, h): of the pairs whose distance lies within h of r (each pair once), the share with different labels. The check is B3(a): random labels give 2p_0p_1; two separated halves give almost 0.

C4. k_inhom(points, a, b, r, sigma): the inhomogeneous K:

for every point i:
    ρ_i = sum over the OTHER points j of exp(−d_ij² / 2σ²) / (2πσ²),  divided by e(x_i)
                                         # e: the edge correction of the point-patterns unit
K_inhom = 1/|W| · sum over i ≠ j with d_ij ≤ r of  w_ij / (ρ_i · ρ_j)

The check: a gradient without interaction looks clustered with the plain K, but not with K_\text{inhom} (\sigma = 40 µm).

Then cargo run -p spatialstats -- marked-patterns. It tests cross-L (r = 2 to 40 µm, 39 simulations, MAD test against the mean of the simulations) for four tissues in 200 \times 200 µm, under relocation of the immune cells and under random labelling: attraction (half of the immune cells next to a tumour cell), independent types, packed tissue (cells at least 7.5 µm apart) with random types, and the same packed cells segregated along a wavy line. Then the mark connection function of the packed tissue, and L - r of a gradient with the plain and the inhomogeneous K.

C5. Read the table. For which tissues do the two nulls agree, and where do they disagree? Explain each disagreement. Which null answers which question?

C6. Compare the mark connection values with 2p_0p_1 for both labellings of the packed tissue. Why is the random one a little low at 8 µm?

C7. Compare the plain and the inhomogeneous L - r of the gradient. What does σ = 200 µm do?

The model solution is spatialstats/src/marked_patterns/solution.rs in the download.

C5. Attraction (324 tumour, 90 immune cells): L_{01}(10) - 10 = 4.44, both nulls reject (p = 0.025, the smallest possible). Segregated packed tissue: -8.92, both reject. Independent types: relocation p = 0.675 (no relation, which is true), but random labelling p = 0.025: the tumour cells sit in nests and the immune cells do not, so the labels are not spread at random over the positions, although the two populations ignore each other. Packed tissue with random types: relocation p = 0.025 (L_{01}(10) - 10 = -3.50: “repulsion”, because relocated immune cells may land closer than 7.5 µm to a tumour cell, which real cells cannot), random labelling p = 0.375 (the truth: the types are random). Relocation asks “are two separately formed populations related?”, random labelling “given where the cells are, are the types placed at random?”.

C6. Random types: 2p_0p_1 = 0.41; observed 0.34, 0.39, 0.40, 0.40 at 8, 10, 20, 40 µm, close to it. At 8 µm (the ring from 6 to 10 µm) only about 80 pairs remain, because no pair is closer than 7.5 µm: the share is uncertain by about ±0.05. Segregated: 2p_0p_1 = 0.48 but only 0.01 to 0.06 up to 20 µm, 0.20 at 40 µm: close neighbours are almost always of the same type.

C7. Plain K: L - r = 0.17, 1.15, 2.41, 3.20 at 5, 10, 20, 30 µm: “clustered”, growing with r, although the cells do not interact. Inhomogeneous with \sigma = 20 or 40 µm: between −0.7 and −1.7, slightly below 0 (the leave-one-out estimate makes it a little conservative), and no clustering. With \sigma = 200 µm the estimated density hardly varies over the window, and the curve returns towards the plain one (0.80, 1.72, 2.21 at 10, 20, 30 µm): the bandwidth decides what counts as density.

Where this leads

These are the core tools of spatial statistics for cells. The colocalisation day applies the cross-K to two-colour spots, and tissue-scale analyses of multiplexed images build per-cell neighbourhood counts from the same idea. To find the clusters themselves, see k-means and Gaussian mixtures (day 35). The same null-model logic in time is the mean squared displacement of track analysis (day 26), and the statistics of whole experiments are replicates (day 38).