All files / src/analysis apa.ts

100% Statements 301/301
95.2% Branches 119/125
100% Functions 16/16
100% Lines 301/301

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536                                                                                  2x 2x 2x   2x   2x 157x 157x 157x 157x 157x 157x                   2x 2047x 2047x 841688x 841688x 663140x 663140x 663140x 663140x 2047x 2047x     2x 99x 99x 21920x 21920x 99x 99x 2x                         2x 1396x 1396x 1396x 1396x 1396x 1396x 1396x 1396x 1393x 1393x 1393x 3x 3x 3x   4x 5x 5x 5x 9x 9x 8x 8x 9x 9x 9x 9x 11x 11x 11x 11x 11x 11x 9x 8x 5x 4x 6x 6x 3x 3x                                       2x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 968x 968x 968x 968x 968x 38730x 38730x 38730x 38730x 38730x 38730x 38730x 381155x 381155x 381155x 381155x 6000957x 6000957x 5971866x 5971866x 5971866x 5971866x 5971866x 5971866x 381155x 38730x 38730x 968x 28x 28x                                                       2x 12x 12x     12x     12x 12x 12x 647x 647x 647x 647x 1883x 1883x 1882x 1882x 1882x 1883x 1882x 1883x 1883x 647x 1x 1x 1x 1x 646x 647x 646x 646x 646x     12x 12x 12x 1501x 61971x 61971x 61971x 61971x 61971x 483251x 483251x 93697x 483251x 61971x 61971x 61971x 1501x 12x 12x 12x 647x 647x 647x 107143x 107143x 107143x 261567x 261567x 261567x 107143x 107143x 647x 12x 12x                     2x 11x 11x 11x 11x 11x   11x 11x 11x 11x 11x 11x 11x     2x 29x 29x 29x 29x 29x 29x                                         2x 69x 69x     69x 69x 69x 69x                   2x 27x 27x 27x 27x 27x 27x 27x 250x 250x 27x 535x 535x 27x 250x 250x 27x 27x                                               2x 38x 38x 37x 37x   37x 111x 111x 111x 537x 2631x 2631x 2478x 2478x 2478x 537x 111x 111x   37x 37x   37x 38x   37x 38x 38x 36x 38x 38x                             2x 1357x 1357x 1357x 1357x   1357x 2714x 2714x 2714x 11209x 49681x 49681x 35658x 35658x 35658x 11209x 2714x 2714x   1357x 1357x 1357x 1043x 1043x     2x 3x 3x 3x 3x 3x 1723x 1723x       1723x 1723x 3x 3x                                               2x 12x 12x 12x 12x 12x 12x 156x 156x 156x 156x 156x 156x 108x 108x 12x 12x  
/**
 * Aggregate peak analysis: many small squares of the matrix, averaged.
 *
 * The kernel. What makes it worth its own module is not the averaging — that is
 * a running sum — but the three decisions around it, each of which changes what
 * the picture means.
 *
 * **Reads are per window, at the file's own resolution, never a bounding box.**
 * This is a deliberate reversal of the obvious optimisation and the reason is
 * resolution: a whole-genome or bounding-box read is pooled at `POOLED_CELL_CAP`
 * (§7.2), so a pileup taken from one silently inherits a coarser bin than the
 * reader asked for, on any genome large enough for it to matter — and nothing in
 * the picture would say so. `k` windows inherit nothing. Where windows happen to
 * sit near each other they are read together (see `analysis/run/apa.ts`), which
 * is an optimisation of the *reads* and not of the resolution.
 *
 * **The control is distance-matched by default, not observed/expected.** Both
 * are offered and both are honest, but they control for different things. O/E
 * divides by the expected at each diagonal offset, which removes the distance
 * decay and nothing else. A distance-matched control is a pileup of *shifted*
 * anchor pairs at the same separations, which removes the distance decay **and**
 * whatever local coverage, mappability and compartment background the real
 * anchors sit in. On a real loop set the difference is large and in the
 * direction that matters: O/E over-reports, because loops sit in active regions
 * that are hot for reasons that have nothing to do with the loop.
 *
 * **The enrichment is a corner ratio, and it is reported.** A pileup always
 * looks like something. P2LL — the centre against the lower-left corner — is the
 * number that says whether it looks like a dot, and a pileup published without
 * one is a picture doing the work a statistic should.
 *
 * **A domain pileup is a different picture and takes a different corner.** The
 * scaled mode piles up *intervals* — TADs between two called boundaries, say —
 * with a flank each side and every body resampled onto the same columns
 * ({@link scaledLayout}, {@link scaledEdges}), so what comes out is a square with
 * the domain in the middle of it rather than a dot in the middle of a window.
 * P2LL on that measures the distance decay and nothing else, which is why
 * {@link domainScore} exists and why it is the number reported there.
 */
 
/** A running mean over many equally-shaped windows. */
export class PileupAccumulator {
  private readonly sum: Float64Array
  private readonly count: Float64Array
  /** Windows that contributed at least one cell. */
  private windows = 0
 
  constructor(
    readonly rows: number,
    readonly cols: number,
  ) {
    this.sum = new Float64Array(rows * cols)
    this.count = new Float64Array(rows * cols)
  }
 
  /**
   * Add one window, already extracted and in this pileup's shape.
   *
   * `NaN` cells are skipped per cell rather than per window, so a window with a
   * balancer-dropped row still contributes everything else it has. The count is
   * therefore per cell too — which is what makes the mean of a pileup over
   * ragged windows the mean of what was actually seen.
   */
  add(values: ArrayLike<number>): void {
    let any = false
    for (let i = 0; i < this.sum.length; i++) {
      const value = values[i]
      if (!Number.isFinite(value)) continue
      this.sum[i] += value
      this.count[i] += 1
      any = true
    }
    if (any) this.windows++
  }
 
  /** The pileup, with `NaN` where no window had a value. */
  finish(): { values: Float32Array; n: number } {
    const values = new Float32Array(this.sum.length)
    for (let i = 0; i < values.length; i++) {
      values[i] = this.count[i] > 0 ? this.sum[i] / this.count[i] : Number.NaN
    }
    return { values, n: this.windows }
  }
}
 
/**
 * One window of a square, resampled to a fixed shape.
 *
 * `fixed` mode is a straight copy: the window is already the right size, because
 * it was asked for that way. `scaled` mode is the one that needs arithmetic —
 * every domain is a different number of bins and they all have to land on the
 * same grid — and it is area-weighted rather than nearest-neighbour, for the
 * reason `render/tracks.ts` gives about pixels: taking whichever input cell an
 * output cell starts in makes the picture jump as the input size changes, which
 * across a set of differently sized domains is a blur that looks like biology.
 */
export function resampleWindow(
  source: ArrayLike<number>,
  sourceRows: number,
  sourceCols: number,
  outRows: number,
  outCols: number,
): Float64Array {
  const out = new Float64Array(outRows * outCols)
  if (sourceRows === outRows && sourceCols === outCols) {
    for (let i = 0; i < out.length; i++) out[i] = source[i]
    return out
  }
  const weights = new Float64Array(outRows * outCols)
  const rowScale = outRows / sourceRows
  const colScale = outCols / sourceCols
 
  for (let r = 0; r < sourceRows; r++) {
    const rLo = r * rowScale
    const rHi = (r + 1) * rowScale
    for (let c = 0; c < sourceCols; c++) {
      const value = source[r * sourceCols + c]
      if (!Number.isFinite(value)) continue
      const cLo = c * colScale
      const cHi = (c + 1) * colScale
      for (let orow = Math.floor(rLo); orow < Math.min(outRows, Math.ceil(rHi)); orow++) {
        const rOverlap = Math.min(rHi, orow + 1) - Math.max(rLo, orow)
        if (!(rOverlap > 0)) continue
        for (let ocol = Math.floor(cLo); ocol < Math.min(outCols, Math.ceil(cHi)); ocol++) {
          const cOverlap = Math.min(cHi, ocol + 1) - Math.max(cLo, ocol)
          if (!(cOverlap > 0)) continue
          const weight = rOverlap * cOverlap
          out[orow * outCols + ocol] += value * weight
          weights[orow * outCols + ocol] += weight
        }
      }
    }
  }
  for (let i = 0; i < out.length; i++) {
    out[i] = weights[i] > 0 ? out[i] / weights[i] : Number.NaN
  }
  return out
}
 
/**
 * One window resampled onto a grid whose **cell edges are given**, rather than
 * spread evenly across it.
 *
 * The difference matters for exactly one picture, and it is the one this exists
 * for: a *domain* pileup, where every window is a flank, a body and a flank and
 * the bodies are all different lengths. Resampled evenly — the whole window
 * stretched onto the grid — a 20 kb domain's left boundary lands on one column
 * and a 200 kb domain's lands on another, because `round(L * flank)` is not the
 * same fraction of `L(1 + 2f)` for the two of them. Averaging those together
 * smears both boundaries over a few columns, and a smeared boundary in an
 * aggregate looks exactly like a real one that is soft.
 *
 * Edges are in **source cells** and may be fractional, so `scaledEdges` can put
 * the body's two ends on the same two grid lines for every window whatever its
 * length. Area-weighted, `NaN`-skipping and gap-preserving, like
 * {@link resampleWindow}, whose uniform case this generalises.
 */
export function resampleWindowTo(
  source: ArrayLike<number>,
  sourceRows: number,
  sourceCols: number,
  rowEdges: ArrayLike<number>,
  colEdges: ArrayLike<number>,
): Float64Array {
  const outRows = rowEdges.length - 1
  const outCols = colEdges.length - 1
  const out = new Float64Array(outRows * outCols)
  for (let orow = 0; orow < outRows; orow++) {
    const rLo = rowEdges[orow]
    const rHi = rowEdges[orow + 1]
    const rFrom = Math.max(0, Math.floor(rLo))
    const rTo = Math.min(sourceRows, Math.ceil(rHi))
    for (let ocol = 0; ocol < outCols; ocol++) {
      const cLo = colEdges[ocol]
      const cHi = colEdges[ocol + 1]
      const cFrom = Math.max(0, Math.floor(cLo))
      const cTo = Math.min(sourceCols, Math.ceil(cHi))
      let total = 0
      let weight = 0
      for (let r = rFrom; r < rTo; r++) {
        const rOverlap = Math.min(rHi, r + 1) - Math.max(rLo, r)
        if (!(rOverlap > 0)) continue
        const at = r * sourceCols
        for (let c = cFrom; c < cTo; c++) {
          const value = source[at + c]
          if (!Number.isFinite(value)) continue
          const cOverlap = Math.min(cHi, c + 1) - Math.max(cLo, c)
          if (!(cOverlap > 0)) continue
          const share = rOverlap * cOverlap
          total += value * share
          weight += share
        }
      }
      out[orow * outCols + ocol] = weight > 0 ? total / weight : Number.NaN
    }
  }
  return out
}
 
/**
 * One square window stretched evenly onto a `side`-cell grid by **linear
 * interpolation**, every grid cell a missing value reaches left a gap.
 *
 * The resampling a domain pileup over the expected goes through, because that
 * is coolpuppy's (`rescale=True`), and with no controls drawn at random it is
 * the one scaled pileup two tools can agree on cell for cell. It is cooltools'
 * `zoom_array` over scipy's `ndimage.zoom` at order 1, step for step:
 *
 * - the window is blown up to `side · m` cells a side, `m` the smallest whole
 *   number that is at least `n / side` (1 where the window is smaller than the
 *   grid), by linear interpolation at `o · (n − 1) / (side · m − 1)` for output
 *   cell `o` — and a coordinate that rounds past the last cell is scipy's
 *   constant edge, 0, not the last cell's value;
 * - then averaged back down in blocks of `m × m`;
 * - with the gaps set to 0 first and interpolated the same way as a 0/1 mask,
 *   a grid cell any gap reaches with any weight is a gap.
 *
 * Unlike {@link resampleWindowTo} this is not area-weighted and it does not
 * average round a gap — it is smoothing, and it drops every cell a dropped bin
 * or a near diagonal touches. Both are coolpuppy's choices, kept because the
 * point of this path is to be its answer, not a better one.
 *
 * Done as `A · X · Aᵀ / m²`, `A` the interpolation weights already summed over
 * each block, so the blown-up square — `n²` cells and more — is never held.
 */
export function zoomWindow(source: ArrayLike<number>, n: number, side: number): Float64Array {
  const blocks = side < n ? Math.ceil(n / side) : 1
  const fine = side * blocks
  // scipy's zoom, `grid_mode=False`: the two end cells of the input land on the
  // two end cells of the output.
  const step = fine > 1 ? (n - 1) / (fine - 1) : 1
  // Each grid cell's weights over the input, summed over its block: at most a
  // block and one cells wide, kept as a start and a dense run.
  const firsts = new Int32Array(side)
  const runs: Float64Array[] = []
  for (let a = 0; a < side; a++) {
    let lo = Number.POSITIVE_INFINITY
    let hi = -1
    const pairs: Array<[number, number]> = []
    for (let i = a * blocks; i < (a + 1) * blocks; i++) {
      const at = i * step
      if (at > n - 1) continue
      const from = Math.floor(at)
      const t = at - from
      pairs.push([from, 1 - t])
      if (from + 1 < n) pairs.push([from + 1, t])
      lo = Math.min(lo, from)
      hi = Math.max(hi, from + 1 < n ? from + 1 : from)
    }
    if (hi < 0) {
      firsts[a] = 0
      runs.push(new Float64Array(0))
      continue
    }
    const run = new Float64Array(hi - lo + 1)
    for (const [index, weight] of pairs) run[index - lo] += weight
    firsts[a] = lo
    runs.push(run)
  }
  // Rows of the input against the grid's columns, then the grid's rows against
  // those: the data with its gaps as 0, and the gaps as a 0/1 mask beside it.
  const half = new Float64Array(n * side)
  const halfGaps = new Float64Array(n * side)
  for (let p = 0; p < n; p++) {
    for (let b = 0; b < side; b++) {
      const run = runs[b]
      const from = firsts[b]
      let total = 0
      let gaps = 0
      for (let k = 0; k < run.length; k++) {
        const value = source[p * n + from + k]
        if (Number.isFinite(value)) total += run[k] * value
        else gaps += run[k]
      }
      half[p * side + b] = total
      halfGaps[p * side + b] = gaps
    }
  }
  const out = new Float64Array(side * side)
  const scale = 1 / (blocks * blocks)
  for (let a = 0; a < side; a++) {
    const run = runs[a]
    const from = firsts[a]
    for (let b = 0; b < side; b++) {
      let total = 0
      let gaps = 0
      for (let k = 0; k < run.length; k++) {
        total += run[k] * half[(from + k) * side + b]
        gaps += run[k] * halfGaps[(from + k) * side + b]
      }
      out[a * side + b] = gaps > 0 ? Number.NaN : total * scale
    }
  }
  return out
}
 
/**
 * The bins a domain's window covers when it is stretched evenly: the domain
 * scaled by `1 + 2f` about its middle, half-open, relative to its sequence's
 * first bin.
 *
 * bioframe's `expand(scale=1 + 2f)`, which is how coolpuppy makes the window:
 * each end moved by `f` times the length and **rounded half to even** (numpy's
 * `round`), then snapped outwards to whole bins.
 */
export function evenWindow(
  start: number,
  end: number,
  flankFraction: number,
  binsize: number,
): { from: number; to: number } {
  // In bioframe's own order of operations, so a flank like 0.3 rounds as it does.
  const scale = 2 * flankFraction + 1
  const pad = 0.5 * (scale - 1) * (end - start)
  return {
    from: Math.floor(roundHalfEven(start - pad) / binsize),
    to: Math.ceil(roundHalfEven(end + pad) / binsize),
  }
}
 
/** numpy's `round`: to the nearest whole number, and a half to the even one. */
export function roundHalfEven(value: number): number {
  const floor = Math.floor(value)
  const diff = value - floor
  if (diff > 0.5) return floor + 1
  if (diff < 0.5) return floor
  return floor % 2 === 0 ? floor : floor + 1
}
 
/**
 * How a scaled window's grid divides into **flank, body, flank**.
 *
 * One layout for the whole run, so every window lands its body on the same
 * columns. `body` is what is left after the two flanks, which is why the flank
 * is rounded first: a reader asks for a flank as a fraction of the feature and
 * the body takes the remainder, rather than the other way about, so a run with
 * no flank is a grid that is all body and never a grid with one cell missing.
 */
export interface ScaledLayout {
  /** Cells a side; `2 * flank + body`. */
  side: number
  /** Cells each flank occupies. `0` where no flank was asked for. */
  flank: number
  /** Cells the body occupies — the grid rows and columns `[flank, flank + body)`. */
  body: number
}
 
/** The grid's division, for a flank given as a fraction of the body. */
export function scaledLayout(side: number, flankFraction: number): ScaledLayout {
  const grid = Math.max(3, Math.round(side))
  const fraction = Math.max(0, flankFraction)
  // The body keeps at least one cell however wide the flank was asked for, so a
  // flank of 20x is a thin body rather than a picture with no domain in it.
  const room = Math.floor((grid - 1) / 2)
  const flank = Math.min(room, Math.round((grid * fraction) / (1 + 2 * fraction)))
  return { side: grid, flank, body: grid - 2 * flank }
}
 
/**
 * One window's cell edges, in source cells, for {@link resampleWindowTo}.
 *
 * `bodyFrom`/`bodyTo` and `pad` are relative to the window's own first cell.
 * The body's two ends land exactly on grid lines `flank` and `flank + body`,
 * by construction and for every window — which is the whole point of taking
 * edges rather than a shape.
 */
export function scaledEdges(
  layout: ScaledLayout,
  bodyFrom: number,
  bodyTo: number,
  pad: number,
): Float64Array {
  const edges = new Float64Array(layout.side + 1)
  for (let i = 0; i < layout.flank; i++) {
    edges[i] = bodyFrom - pad + (pad * i) / layout.flank
  }
  for (let j = 0; j <= layout.body; j++) {
    edges[layout.flank + j] = bodyFrom + ((bodyTo - bodyFrom) * j) / layout.body
  }
  for (let k = 1; k <= layout.flank; k++) {
    edges[layout.flank + layout.body + k] = bodyTo + (pad * k) / layout.flank
  }
  return edges
}
 
/**
 * A domain pileup's enrichment: the domain's own corner against the same block
 * across its boundary.
 *
 * P2LL is the wrong statistic here and wrong in a way that flatters: the
 * lower-left corner of a *domain* window is the upstream flank against the
 * downstream flank — two neighbours of the domain, further apart than anything
 * inside it — so a ratio against it is mostly the distance decay and reads well
 * above 1 for a set with no domains in it at all.
 *
 * The corner is the block just inside the domain at its widest separation —
 * rows at the domain's start, columns at its end — and the denominator is the
 * two blocks immediately outside it: the same columns one boundary up, and the
 * same rows one boundary down. Those sit at very nearly the same separation as
 * the numerator by construction, so what is left when you divide is whether
 * contact **stops at the boundary**. Above 1 is a domain; 1 is a set of
 * intervals with no boundaries at their edges.
 *
 * `NaN` where there is no flank to compare against, which is a run that asked
 * for no padding: there is then no outside in the picture, and a ratio against
 * a block that was never read is not a large number, it is no number.
 */
export function domainScore(values: ArrayLike<number>, layout: ScaledLayout): number {
  const { side, flank, body } = layout
  if (flank === 0 || body === 0) return Number.NaN
  const q = Math.max(1, Math.round(body / 4))
  const p = Math.min(flank, q)
 
  const mean = (r0: number, r1: number, c0: number, c1: number): number => {
    let total = 0
    let seen = 0
    for (let r = Math.max(0, r0); r < Math.min(side, r1); r++) {
      for (let c = Math.max(0, c0); c < Math.min(side, c1); c++) {
        const value = values[r * side + c]
        if (!Number.isFinite(value)) continue
        total += value
        seen++
      }
    }
    return seen === 0 ? Number.NaN : total / seen
  }
 
  const inside = mean(flank, flank + q, flank + body - q, flank + body)
  const outside: number[] = []
  // Across the upstream boundary: the same columns, rows just above the domain.
  const above = mean(flank - p, flank, flank + body - q, flank + body)
  if (Number.isFinite(above)) outside.push(above)
  // Across the downstream boundary: the same rows, columns just past the domain.
  const beyond = mean(flank, flank + q, flank + body, flank + body + p)
  if (Number.isFinite(beyond)) outside.push(beyond)
  if (!Number.isFinite(inside) || outside.length === 0) return Number.NaN
  const background = outside.reduce((sum, value) => sum + value, 0) / outside.length
  return background === 0 ? Number.NaN : inside / background
}
 
/**
 * Peak to lower-left: the centre of a pileup against its lower-left corner.
 *
 * The conventional APA statistic, and the corner is not arbitrary. On an
 * off-diagonal pileup the lower-left quadrant is the region *further* from the
 * diagonal on one anchor and *nearer* on the other — background at a comparable
 * separation, with none of the loop in it. Taking the whole border instead would
 * include the two quadrants that a stripe runs through, and a stripe would then
 * read as a dot.
 *
 * `NaN` where either part has nothing in it: a ratio against an empty corner is
 * not a large number, it is no number.
 */
export function p2ll(values: ArrayLike<number>, rows: number, cols: number): number {
  const quadrant = Math.max(1, Math.floor(Math.min(rows, cols) / 5))
  const centreR = Math.floor(rows / 2)
  const centreC = Math.floor(cols / 2)
  const half = Math.max(0, Math.floor(quadrant / 2))
 
  const mean = (r0: number, r1: number, c0: number, c1: number): number => {
    let total = 0
    let seen = 0
    for (let r = Math.max(0, r0); r < Math.min(rows, r1); r++) {
      for (let c = Math.max(0, c0); c < Math.min(cols, c1); c++) {
        const value = values[r * cols + c]
        if (!Number.isFinite(value)) continue
        total += value
        seen++
      }
    }
    return seen === 0 ? Number.NaN : total / seen
  }
 
  const peak = mean(centreR - half, centreR + half + 1, centreC - half, centreC + half + 1)
  const corner = mean(rows - quadrant, rows, 0, quadrant)
  if (!Number.isFinite(peak) || !Number.isFinite(corner) || corner === 0) return Number.NaN
  return peak / corner
}
 
/** Divide one pileup by another, cell by cell. */
export function ratioOf(
  numerator: ArrayLike<number>,
  denominator: ArrayLike<number>,
): Float32Array {
  const out = new Float32Array(numerator.length)
  for (let i = 0; i < out.length; i++) {
    const a = numerator[i]
    const b = denominator[i]
    // A zero denominator gives no ratio rather than an infinity: an infinity
    // cannot be drawn on a diverging scale, cannot be written to a bigWig, and
    // would take the colour range of the whole picture with it (§18.7).
    out[i] = Number.isFinite(a) && Number.isFinite(b) && b !== 0 ? a / b : Number.NaN
  }
  return out
}
 
/** One pair of anchors, in absolute bins. */
export interface AnchorPair {
  row: number
  col: number
  /** Bins between them; `col - row` for an ordered pair. */
  separation: number
}
 
/**
 * Controls at the same separations, drawn by shifting both anchors together.
 *
 * The shift preserves the separation exactly, which is the whole point: a
 * control at a different distance would be controlling for the wrong thing, and
 * the distance decay would come back as signal. The offset is drawn from a
 * seeded generator (§18.4) and is bounded so a control stays on the same
 * sequence as the pair it came from — a "control" that lands in trans is
 * measuring a different quantity entirely.
 *
 * Controls that cannot be placed are dropped rather than clamped: clamping would
 * pile several controls onto the same cells and give the denominator a spurious
 * structure of its own.
 */
export function shiftedControls(
  pair: AnchorPair,
  bounds: { from: number; to: number },
  options: { perPair: number; minShift: number; maxShift: number; rng(): number },
): AnchorPair[] {
  const out: AnchorPair[] = []
  for (let i = 0; i < options.perPair; i++) {
    const magnitude =
      options.minShift + Math.floor(options.rng() * (options.maxShift - options.minShift + 1))
    const sign = options.rng() < 0.5 ? -1 : 1
    const row = pair.row + sign * magnitude
    const col = pair.col + sign * magnitude
    if (row < bounds.from || col >= bounds.to) continue
    out.push({ row, col, separation: pair.separation })
  }
  return out
}