Batch integration of single-cell atlases
Remove batch effects from six single-cell atlases while keeping cell-type variation, scored on 13 metrics against control methods.
| 1 | """What batch integration asks, as a small computed toy (not benchmark data, so the label says schematic). |
| 2 | |
| 3 | Three cell types are sampled in two batches; the second batch is shifted, as a technical batch effect would shift it. |
| 4 | Before: each cell type splits into two islands, one per batch. After: a per-batch location correction (each batch's |
| 5 | mean moved to the common mean, the simplest form of what ComBat does) mixes the batches and keeps the types apart. |
| 6 | Checks: of each cell's 5 nearest neighbours, the share from the other batch (batch mixing) and from the same cell type |
| 7 | (identity kept), before and after. |
| 8 | """ |
| 9 | import math |
| 10 | import random |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import kit |
| 14 | |
| 15 | rnd = random.Random(7) |
| 16 | TYPES = [(-1.3, 0.8, 0.25, 0.16, 0.5), (1.3, 0.9, 0.23, 0.17, -0.4), (-0.05, -1.0, 0.27, 0.15, 0.1)] # centre, spread, tilt |
| 17 | SHIFT = (0.8, -0.6) |
| 18 | PER = 7 # cells per type per batch |
| 19 | |
| 20 | cells = [] # (x, y, type, batch) |
| 21 | for t, (cx, cy, sx, sy, a) in enumerate(TYPES): |
| 22 | for b in (0, 1): |
| 23 | for _ in range(PER): |
| 24 | u, v = rnd.gauss(0, sx), rnd.gauss(0, sy) |
| 25 | x, y = cx + u * math.cos(a) - v * math.sin(a), cy + u * math.sin(a) + v * math.cos(a) |
| 26 | cells.append((x + b * SHIFT[0], y + b * SHIFT[1], t, b)) |
| 27 | |
| 28 | mean = lambda pts: (sum(p[0] for p in pts) / len(pts), sum(p[1] for p in pts) / len(pts)) |
| 29 | gx, gy = mean(cells) |
| 30 | bm = [mean([c for c in cells if c[3] == b]) for b in (0, 1)] |
| 31 | after = [(x - bm[b][0] + gx, y - bm[b][1] + gy, t, b) for x, y, t, b in cells] |
| 32 | |
| 33 | |
| 34 | def knn_shares(pts, k=5): |
| 35 | mix = keep = 0 |
| 36 | for p in pts: |
| 37 | nn = sorted((q for q in pts if q is not p), key=lambda q: math.dist(p[:2], q[:2]))[:k] |
| 38 | mix += sum(q[3] != p[3] for q in nn) / k |
| 39 | keep += sum(q[2] == p[2] for q in nn) / k |
| 40 | return mix / len(pts), keep / len(pts) |
| 41 | |
| 42 | |
| 43 | m0, k0 = knn_shares(cells) |
| 44 | m1, k1 = knn_shares(after) |
| 45 | assert m0 < 0.1 and m1 > 0.35 and k1 > 0.95, (m0, k0, m1, k1) |
| 46 | |
| 47 | # two panels side by side, at one scale |
| 48 | span = lambda pts, k: max(p[k] for p in pts) - min(p[k] for p in pts) |
| 49 | scale = min(min(250 / span(pts, 0), 228 / span(pts, 1)) for pts in (cells, after)) |
| 50 | |
| 51 | |
| 52 | def place(pts, left): |
| 53 | xs, ys = [p[0] for p in pts], [p[1] for p in pts] |
| 54 | ox = left + 140 - scale * (max(xs) + min(xs)) / 2 |
| 55 | oy = 128 + scale * (max(ys) + min(ys)) / 2 |
| 56 | return [(ox + scale * x, oy - scale * y, t, b) for x, y, t, b in pts] |
| 57 | |
| 58 | |
| 59 | P0, P1 = place(cells, 18), place(after, 342) |
| 60 | def hull(pts): |
| 61 | pts, out = sorted(set(pts)), [] |
| 62 | for seq in (pts, pts[::-1]): |
| 63 | h = [] |
| 64 | for p in seq: |
| 65 | while len(h) >= 2 and (h[-1][0] - h[-2][0]) * (p[1] - h[-2][1]) - (h[-1][1] - h[-2][1]) * (p[0] - h[-2][0]) <= 0: |
| 66 | h.pop() |
| 67 | h.append(p) |
| 68 | out += h[:-1] |
| 69 | return out |
| 70 | |
| 71 | |
| 72 | body = [kit.line(306, 128, 334, 128, 'faint-s', 2.5), kit.polygon([(342, 128), (330, 121), (330, 135)], 'faint')] |
| 73 | for P in (P0, P1): # each cell type as one soft region: before it holds two islands, after one |
| 74 | body.append('<g opacity=".16">' + ''.join( |
| 75 | kit.polygon(hull([(x, y) for x, y, t_, b in P if t_ == t]), 'f', ' stroke="var(--f)" stroke-width="30" stroke-linejoin="round"') |
| 76 | for t in range(len(TYPES))) + '</g>') |
| 77 | for P in (P0, P1): |
| 78 | for x, y, t, b in sorted(P, key=lambda p: (p[3], p[1])): |
| 79 | body.append(kit.dot(x, y, 7, 'sand' if b else 'ink')) |
| 80 | kit.write(__file__, 'life-health', ''.join(body), label='3 types × 2 batches, before → after · schematic', |
| 81 | title='Toy: three cell types from two batches, split by batch before integration and mixed after') |
| 82 | print(f'batch mixing {m0:.2f} → {m1:.2f}, same type among neighbours {k0:.2f} → {k1:.2f}') |