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 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | 2x 2x 2x 2x 2x 28x 28x 28x 27x 27x 28x 2x 162x 162x 162x 162x 162x 162x 11x 10x 10x 2x 10x 10x 10x 10x 10x 10x 150x 150x 150x 150x 150x 150x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 2x 2x 2x 20x 20x 20x 20x 20x 20x 2x 20522x 20522x 19145x 20522x 20522x 2x 6334x 6334x 6188x 6334x 5825x 6334x 2x 2x 6186x 6334x 2x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 23x 23x 23x 23x 23x 23x 23x 3510x 3510x 3510x 1310506x 1310506x 3510x 23x 20x 4x 50x 850x 850x 450x 450x 50x 4x 20x 2x 2x 2x 2x 80x 80x 80x 80x 2x 1x 4x 4x 16x 16x 10x 10x 4x 1x 2x 2x 18x 18x 20x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 20x 4x 4x 4x 4x 4x 4x 4x 4x 20x 18x 18x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 68x 20037x 20037x 20037x 5350350x 5350350x 5241143x 5241143x 5241143x 20037x 68x 14x 14x 14x 14x 14x 14x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 14x 14x 14x 14x 14x 14x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 51x 4x 4x 4x 4x 7x 7x 7x 4x 4x 4x 4x 5x 5x 7406x 7406x 7406x 4016x 7406x 4016x 5966x 7406x 5x 4x 4x 4x 4x 2x 68x 68x 68x 146x 146x 146x 68x 68x 2x 74x 74x 74x 74x 14x 14x 2x 82x 82x 82x 82x 82x 82x 82x 2x 2x 2x 2x 2x 3x 3x 3x 82x 82x 2x 170x 170x 170x 2x 76x 4x 56x 4x 4x | /**
* Getting a square, from wherever the cheapest square is.
*
* Almost every task on the map needs the same thing: a dense on-diagonal block
* for one sequence, at the file's own resolution, balanced the way the run asked
* for. And almost every one of them can get it two ways — out of the block the
* view is already holding, or by reading it — with the same three conditions
* deciding which (§18.3, §18.6).
*
* Those conditions were written once, for P(s), and are here because insulation
* needed exactly them and a second copy is how the two would come to disagree
* about what "usable" means. Each is load-bearing:
*
* - the block has to **cover** the span, or the part it does not cover would be
* silently absent from a result that claims to describe the whole sequence;
* - it has to be at the file's **own resolution**, since a pooled block is
* coarser than the file and `AnalysisFraming.binsPerCell` exists precisely so a
* task can decline to pretend otherwise;
* - it has to have been read with the **balancing this run wants**, because the
* block is whatever the map happens to be drawn from and the reader may have
* asked for the other one.
*
* A task that skips any of the three gets an answer that is plausible, cheap and
* wrong, which is the combination this whole subtree is written against.
*/
import { ViewerError } from '../../errors'
import type { MetaCooler } from '../../metacooler/MetaCooler'
import { DEFAULT_WEIGHT_COLUMN } from '../../metacooler/matrix'
import { contigIdsFor, type JobContext } from '../job'
import type { BlockInput } from '../worker/protocol'
import type { SeparationBand } from '../band'
import { diagonalTiles, tileRows } from '../tiles'
/** A half-open run of absolute bins. */
export interface Span {
from: number
to: number
}
/** Whether a block contains the whole of `span` on both axes. */
export function blockCovers(block: BlockInput, span: Span): boolean {
return (
block.rowExtent[0] <= span.from &&
block.rowExtent[1] >= span.to &&
block.colExtent[0] <= span.from &&
block.colExtent[1] >= span.to
)
}
/**
* The view's block, if it can honestly answer for every span.
*
* `null` rather than a partial answer: the caller then reads, which is slower
* and right, and there is no third state in which some spans come from the block
* and some from the file at a different resolution.
*/
export function usableBlock(
job: JobContext,
spans: readonly Span[],
balance: boolean,
): BlockInput | null {
const block = job.inputs.block
if (block === undefined || block.binsPerCell !== 1 || job.framing.balance !== balance) return null
for (const span of spans) if (!blockCovers(block, span)) return null
return block
}
/**
* One span's square, cut out of a larger block.
*
* The copy is skipped where the block *is* the span, which is the usual case —
* the reader is looking at one contig and asks about that contig — and it
* matters because the alternative allocates a second copy of a block that can be
* nine megabytes for no reason at all.
*/
export function squareOf(block: BlockInput, span: Span): BlockInput {
const exact =
block.rowExtent[0] === span.from &&
block.rowExtent[1] === span.to &&
block.colExtent[0] === span.from &&
block.colExtent[1] === span.to
if (exact) return block
const n = span.to - span.from
const values = new Float32Array(n * n)
const rowSkip = span.from - block.rowExtent[0]
const colSkip = span.from - block.colExtent[0]
for (let r = 0; r < n; r++) {
const at = (rowSkip + r) * block.ncols + colSkip
values.set(block.values.subarray(at, at + n), r * n)
}
return {
nrows: n,
ncols: n,
values,
rowExtent: [span.from, span.to],
colExtent: [span.from, span.to],
binsPerCell: 1,
}
}
/**
* A span's square, from the block or from the file.
*
* The one call a task on the diagonal should make. `block` is whatever
* {@link usableBlock} returned for the whole run, so the decision is made once
* per run rather than once per sequence — which is what stops a metagenome from
* checking the same three conditions four thousand times.
*/
export async function squareFor(
job: JobContext,
span: Span,
options: { block: BlockInput | null; balance: boolean; value: string },
): Promise<BlockInput> {
if (options.block !== null) return squareOf(options.block, span)
const dense = await job.mc.matrix({
rows: [span.from, span.to],
cols: [span.from, span.to],
balance: options.balance,
value: options.value,
})
return {
nrows: dense.nrows,
ncols: dense.ncols,
values: dense.values,
rowExtent: [dense.rowExtent[0], dense.rowExtent[1]],
colExtent: [dense.colExtent[0], dense.colExtent[1]],
binsPerCell: 1,
}
}
// ---------------------------------------------------------------------------
// Reading a diagonal, without the square around it
// ---------------------------------------------------------------------------
/**
* A run of diagonals of one sequence, read without the square around it.
*
* **The reason this exists is that everything downstream of it was already
* banded and the read was not.** `analysis/chromosight.ts` keeps its detrended
* map and its correlation map as {@link DiagonalBand}s precisely because holding
* them as `n²` costs gigabytes on a mammalian chromosome — and then the runner
* handed them an `n²` dense square to build from, which put a cap on the
* sequence length that had nothing to do with the arithmetic.
*
* Read row-band by row-band: the cells with `0 ≤ col − row ≤ hi` for rows
* `[a, b)` all lie in columns `[a, b + hi)`, so one rectangle per tile covers
* them with no overlap between tiles. What is held is `(hi + 1) × n` cells
* whatever the sequence is, and what is read at a time is one tile.
*
* `Float32` rather than `Float64`, matching what `mc.matrix` returns: this is
* the file's own numbers, not a derived quantity, and doubling them would double
* the one array that does scale with the sequence.
*/
export class ContigBand implements SeparationBand {
readonly values: Float32Array
constructor(
readonly n: number,
/** Highest offset `col - row` stored, inclusive. */
readonly hi: number,
/**
* Whether the sequence joins its own end to its own beginning (§8.15).
*
* What it changes is only what {@link forward} answers past either end — the
* storage is identical, because a circular band's wrapped cells live at the
* same `(offset, row)` as any other: the cell `offset` along from row `r` is
* at `(r + offset) mod n`, and {@link bandFor} reads the corner of the
* square that holds those and fills them in.
*/
readonly circular: boolean = false,
) {
this.values = new Float32Array((hi + 1) * n).fill(Number.NaN)
}
/** `(row, col)` where the cell is stored; `0` outside the band. */
at(row: number, col: number): number {
const offset = col - row
if (offset < 0 || offset > this.hi || row < 0 || row >= this.n) return 0
const value = this.values[offset * this.n + row]
// `0` rather than `NaN` for a cell that was never read, which is what this
// accessor has always answered and what `analysis/chromosight.ts` is written
// against. {@link forward} is the one that tells *nothing here* apart from
// *nothing read*, because the kernels that use it need to.
return Number.isNaN(value) ? 0 : value
}
/**
* The cell `offset` along the diagonal from `row` — the accessor the banded
* kernels are written against. See `analysis/band.ts`.
*/
forward(row: number, offset: number): number {
if (offset < 0) return this.forward(row + offset, -offset)
if (offset > this.hi) return Number.NaN
let r = row
if (this.circular) {
r = ((row % this.n) + this.n) % this.n
} else if (r < 0 || r + offset >= this.n) {
return Number.NaN
}
return this.values[offset * this.n + r]
}
}
/**
* One sequence's band, from the block on screen or from the file.
*
* The block path is the same three conditions {@link usableBlock} already
* decided, so a reader who pressed Run while looking at the sequence pays
* nothing; the read path is tiled and bounded. Either way what comes back is the
* same object, so nothing downstream knows which it got.
*
* ## The one extra read a circular sequence costs
*
* On a circular sequence the cell `d` bins after the last bin is a cell `d`
* bins into the *first*, and in the contig's own square that cell is not near
* the diagonal at all — it is in the **top-right corner**, rows `[0, hi)` against
* columns `[n − hi, n)`. A tiled band walk never goes near it, which is exactly
* why a band read that simply kept the same tiles and changed the accessor would
* answer `NaN` for every bin within `hi` of the junction: a circular chromosome
* with a linear-looking seam at the one place the flag was set to fix.
*
* So a circular band reads one more rectangle. It is `hi × hi` cells whatever
* the sequence is, it is one request, and it is the whole of the difference.
*/
export async function bandFor(
job: JobContext,
span: Span,
options: {
/** Widest diagonal to keep, in bins. */
hi: number
block: BlockInput | null
balance: boolean
value: string
/**
* Read the sequence as a circle: the band wraps past the last bin (§8.15).
*
* `hi` is then capped at half the sequence rather than at its length, since
* past half the circle the short way round is the other way and a band wider
* than that would hold every cell twice.
*/
circular?: boolean
/** Called between tiles, so a chromosome-long read can be stopped. */
onTile?: (done: number, total: number) => Promise<void> | void
},
): Promise<ContigBand> {
const n = span.to - span.from
const circular = options.circular === true
const hi = Math.max(
0,
Math.min(options.hi, circular ? Math.floor(n / 2) : Math.max(0, n - 1)),
)
const band = new ContigBand(n, hi, circular)
const fill = (
values: ArrayLike<number>,
ncols: number,
rowFrom: number,
rowTo: number,
colFrom: number,
): void => {
for (let r = rowFrom; r < rowTo; r++) {
const base = (r - rowFrom) * ncols - colFrom
const last = Math.min(n - 1, r + hi)
for (let c = r; c <= last; c++) {
band.values[(c - r) * n + r] = values[base + c]
}
}
}
/**
* The wrapped cells, from the corner of the contig's own square.
*
* `corner` is rows `[0, hi)` by columns `[n − hi, n)` of that square, row-major
* and `hi` wide. The cell it holds at `(c, r)` — a *low* row against a *high*
* column — is the contact between bin `r` near the end and bin `c` near the
* start, which is the cell `c + n − r` bins along the circle from `r`. Only
* the separations at or below `hi` belong in the band; the rest of the corner
* is the long way round and is already stored at its short-way offset.
*/
const fillWrap = (corner: ArrayLike<number>, ncols: number): void => {
for (let c = 0; c < hi; c++) {
for (let r = n - hi; r < n; r++) {
const offset = c + n - r
if (offset < 1 || offset > hi) continue
band.values[offset * n + r] = corner[c * ncols + (r - (n - hi))]
}
}
}
if (options.block !== null) {
const block = options.block
const rowSkip = span.from - block.rowExtent[0]
const colSkip = span.from - block.colExtent[0]
for (let r = 0; r < n; r++) {
const at = (rowSkip + r) * block.ncols + colSkip
const last = Math.min(n - 1, r + hi)
for (let c = r; c <= last; c++) band.values[(c - r) * n + r] = block.values[at + c]
}
if (circular && hi > 0) {
// The corner is inside a block that covers the whole sequence, so there is
// nothing to read — only to index. Same arithmetic as {@link fillWrap},
// against the block's own stride.
for (let c = 0; c < hi; c++) {
const at = (rowSkip + c) * block.ncols + colSkip
for (let r = n - hi; r < n; r++) {
const offset = c + n - r
if (offset < 1 || offset > hi) continue
band.values[offset * n + r] = block.values[at + r]
}
}
}
return band
}
const tile = tileRows(hi, hi)
const tiles = Math.max(1, Math.ceil(n / tile))
for (let t = 0; t < tiles; t++) {
await options.onTile?.(t, tiles)
const rowFrom = t * tile
const rowTo = Math.min(n, rowFrom + tile)
if (rowTo <= rowFrom) break
const colTo = Math.min(n, rowTo + hi)
const dense = await job.mc.matrix({
rows: [span.from + rowFrom, span.from + rowTo],
cols: [span.from + rowFrom, span.from + colTo],
balance: options.balance,
value: options.value,
})
fill(dense.values, dense.ncols, rowFrom, rowTo, rowFrom)
}
if (circular && hi > 0) {
const corner = await job.mc.matrix({
rows: [span.from, span.from + hi],
cols: [span.to - hi, span.to],
balance: options.balance,
value: options.value,
})
fillWrap(corner.values, corner.ncols)
}
await options.onTile?.(tiles, tiles)
return band
}
/**
* The mean of each diagonal of one sequence, `0 … hi`, read as a band and never
* held.
*
* cooltools' `expected_cis` for one sequence, which is the expected coolpuppy
* divides a window by: a diagonal's contacts over the bin pairs on it whose two
* bins both survived the balancer. A filtered bin reads `NaN` and is skipped and
* an empty pair reads `0` and is counted, which is that function's
* `balanced.sum / n_valid` exactly, and `count.avg` on a map read raw.
*
* {@link bandFor}'s walk, tile for tile, but each tile is folded into a sum and
* a count per diagonal and let go. What a pileup divides by is a few hundred
* numbers per sequence; the band they come from is a gigabyte on a 1 kb
* mammalian chromosome read two megabases deep.
*/
export async function diagonalMeans(
job: JobContext,
span: Span,
options: {
/** Widest diagonal wanted, in bins. */
hi: number
block: BlockInput | null
balance: boolean
value: string
/** Called between tiles, so a chromosome-long read can be stopped. */
onTile?: (done: number, total: number) => Promise<void> | void
},
): Promise<Float64Array> {
const n = span.to - span.from
const hi = Math.max(0, Math.min(options.hi, n - 1))
const sum = new Float64Array(hi + 1)
const count = new Float64Array(hi + 1)
// Rows `[rowFrom, rowTo)` of the sequence, out of a block whose cell `(r, c)`
// of the sequence is at `base + r * stride + c`.
const fold = (values: ArrayLike<number>, stride: number, base: number, rowFrom: number, rowTo: number): void => {
for (let r = rowFrom; r < rowTo; r++) {
const at = base + r * stride
const last = Math.min(n - 1, r + hi)
for (let c = r; c <= last; c++) {
const value = values[at + c]
if (Number.isNaN(value)) continue
sum[c - r] += value
count[c - r] += 1
}
}
}
const block = options.block
if (block !== null && blockCovers(block, span)) {
const rowSkip = span.from - block.rowExtent[0]
const colSkip = span.from - block.colExtent[0]
fold(block.values, block.ncols, rowSkip * block.ncols + colSkip, 0, n)
} else {
const tile = tileRows(hi, hi)
const tiles = Math.max(1, Math.ceil(n / tile))
for (let t = 0; t < tiles; t++) {
await options.onTile?.(t, tiles)
const rowFrom = t * tile
const rowTo = Math.min(n, rowFrom + tile)
if (rowTo <= rowFrom) break
const dense = await job.mc.matrix({
rows: [span.from + rowFrom, span.from + rowTo],
cols: [span.from + rowFrom, span.from + Math.min(n, rowTo + hi)],
balance: options.balance,
value: options.value,
})
// The tile's cell `(r, c)` of the sequence sits at
// `(r − rowFrom) * ncols + (c − rowFrom)`.
fold(dense.values, dense.ncols, -rowFrom * dense.ncols - rowFrom, rowFrom, rowTo)
}
await options.onTile?.(tiles, tiles)
}
const mean = new Float64Array(hi + 1)
for (let k = 0; k <= hi; k++) mean[k] = count[k] > 0 ? sum[k] / count[k] : Number.NaN
return mean
}
/**
* One sequence walked as overlapping squares along its diagonal.
*
* For a kernel that wants a *square* — the insulation diamond does, and it is
* goldened against cooltools cell for cell, so it is not being rewritten to take
* a band. Each tile is a square of `body + 2 × halo` bins and the caller is told
* which of its bins are fully inside it: those are the ones whose whole window
* the tile holds, and the ones at its seams belong to the neighbouring tile.
*
* At the two ends of the sequence the halo is clipped rather than padded, which
* is what keeps the first and last `halo` bins answering exactly as they do from
* a whole square — with no score at all, because their window runs off the
* sequence and not because a tile boundary fell there.
*/
export async function forEachDiagonalTile(
job: JobContext,
span: Span,
options: {
/** Bins of margin each side; the kernel's own reach. */
halo: number
block: BlockInput | null
balance: boolean
value: string
},
onTile: (tile: {
/** The square, `size × size`, in the sequence's own bins. */
values: Float32Array
size: number
/** Where the square starts, local to the sequence. */
from: number
/** Local bins whose scores this tile owns, half-open. */
ownFrom: number
ownTo: number
index: number
total: number
}) => Promise<void> | void,
): Promise<void> {
const n = span.to - span.from
const halo = Math.max(0, options.halo)
// Through `diagonalTiles`, so the panel's estimate counts the reads this
// loop actually makes (§18.6).
const body = tileRows(halo * 2, halo * 2)
const tiles = diagonalTiles(n, halo)
for (let t = 0; t < tiles; t++) {
const ownFrom = t * body
const ownTo = Math.min(n, ownFrom + body)
if (ownTo <= ownFrom) break
const from = Math.max(0, ownFrom - halo)
const to = Math.min(n, ownTo + halo)
const square = await squareFor(
job,
{ from: span.from + from, to: span.from + to },
{ block: options.block, balance: options.balance, value: options.value },
)
await onTile({
values: square.values,
size: to - from,
from,
ownFrom,
ownTo,
index: t,
total: tiles,
})
}
}
/**
* Non-zero **cis** pixels per bin, over the whole file, in one pass.
*
* The detectability cut needs a count over a whole *row* — every separation, not
* just the scanned band — which is the one quantity in this task that a banded
* read genuinely cannot produce. It used to fall out of the dense square for
* free, and now that the square is gone it has to be counted from the pixel
* table instead.
*
* The count is exactly what the mirrored square gave: a stored pixel `(i, j)`
* adds one to both ends and a diagonal pixel adds one, so an upper-triangular
* sparse table and a mirrored dense block agree per bin — the identity
* `detectableFromCounts` already relied on. **Cis only**, because the square it
* replaces was one contig's: a trans pixel was never in it.
*
* Once per run rather than once per sequence, and only where the file carries no
* weights — with them `detectableFromWeights` reads a column and no matrix at
* all, which is every balanced cooler and so the common case.
*/
export async function cisMarginal(job: JobContext): Promise<Float64Array> {
const mc = job.mc
// Bin → contig, so a pixel spanning two sequences can be left out without a
// binary search per pixel. One `Int32Array` of `nBins` against a pass over
// every pixel in the file is the same trade `run/coverage.ts` makes.
const contigOf = new Int32Array(mc.nBins).fill(-1)
for (let cid = 0; cid < mc.nContigs; cid++) {
const [from, to] = mc.contigExtent(cid)
for (let bin = from; bin < to; bin++) contigOf[bin] = cid
}
const sums = new Float64Array(mc.nBins)
job.progress(0, 1, 'counting coverage')
await mc.scanPixels({
onChunk: (chunk) => {
job.throwIfCancelled()
for (let i = 0; i < chunk.length; i++) {
const bin1 = chunk.bin1[i]
const bin2 = chunk.bin2[i]
if (contigOf[bin1] !== contigOf[bin2]) continue
const count = chunk.count[i]
if (!Number.isFinite(count) || count === 0) continue
sums[bin1] += 1
if (bin2 !== bin1) sums[bin2] += 1
}
},
onProgress: (at, of) => job.progress(at, of, 'counting coverage'),
})
return sums
}
/**
* The contigs a result may be produced for.
*
* `__unbinned__`'s contigs are left out, which is §7.4's rule applied one level
* down exactly as the overview scan applies it — and through `displayGenomes()`
* rather than by matching the reserved name a second time, because §7.4 is
* emphatic that the name is matched in exactly one place.
*/
export function displayableContigs(mc: MetaCooler): number[] {
const out: number[] = []
const shown = mc.hasGenomes ? new Set(mc.displayGenomes()) : null
for (let cid = 0; cid < mc.nContigs; cid++) {
if (shown !== null && !shown.has(mc.contigGenomeIds[cid])) continue
out.push(cid)
}
return out
}
/**
* The contigs a run was ticked for, in the file's own order, and what was said.
*
* The read side of the one shape for *which sequences to run on* (§18.2). Five
* tasks used to take a region string naming one sequence, with the empty string
* meaning every one; they take a list of ticked names now, and this is the
* conversion the five would otherwise each hold a copy of.
*
* Three answers, and each needs its own sentence at the call site rather than a
* shared one, because they are three different situations: **every displayable
* contig** where nothing was answered, which is the genome-wide run; **the
* ticked ones this file has**, with the rest named in `missing` — a deck holds
* files of different assemblies on purpose and a batch asks every panel for one
* reader's list (§5.9), so a name that found nothing is a fact about the pair of
* files rather than an error; and **nothing at all**, where the reader pressed
* *None*.
*
* Resolved through {@link contigIdFor}, never `mc.contigId`, for §5.9's reason:
* the names were ticked against whichever map the Analysis column was pointed
* at, and the `.hic` beside the `.cool` writes `1` where it writes `chr1`.
* Ascending, so a caller walking them walks the file's own layout.
*/
export function tickedContigIds(
mc: MetaCooler,
names: readonly string[] | null,
): { ids: number[]; missing: string[] } {
if (names === null) return { ids: displayableContigs(mc), missing: [] }
return contigIdsFor(mc, names)
}
/**
* What to say about ticked sequences this file has not got, or `null`.
*
* One sentence, in one place, because the alternative is five tasks phrasing the
* same fact five ways in the same drawer. `null` where every name landed, which
* is the ordinary case and wants no line at all.
*/
export function missingSequencesNote(
mc: MetaCooler,
missing: readonly string[],
found: number,
): string | null {
if (missing.length === 0) return null
const shown = `${missing.slice(0, 6).join(', ')}${missing.length > 6 ? ', …' : ''}`
if (found > 0) {
return (
`${missing.length.toLocaleString()} ticked ` +
`${missing.length === 1 ? 'sequence is' : 'sequences are'} not in this file and ` +
`${missing.length === 1 ? 'is' : 'are'} left out: ${shown}.`
)
}
return (
`This file has no sequence called ${shown}, under that spelling or any this viewer knows ` +
`of. It has ${mc.chromnames.slice(0, 6).join(', ')}` +
`${mc.chromnames.length > 6 ? ', …' : ''}.`
)
}
/** One contig's bin span, or `null` where it has no bins. */
export function spanOfContig(mc: MetaCooler, cid: number): Span | null {
const [from, to] = mc.contigExtent(cid)
return to > from ? { from, to } : null
}
/**
* Bins whose weight survived the balancer, or `null` when nothing is balanced.
*
* Read once for the whole file rather than per span. It is one `Float64Array` of
* `nBins` against a pass that touches every pixel in the file, which is the same
* trade `worker/groups.ts` makes for the overview's bin → unit array.
*/
export function validBins(mc: MetaCooler, balance: boolean): Float64Array | null {
if (!balance) return null
const path = `bins/${DEFAULT_WEIGHT_COLUMN}`
if (!mc.handle.has(path)) {
throw new ViewerError(
'unsupported',
`This file has no /${path} column, so there are no weights to balance with. ` +
'Run `cooler balance` on it, or turn balancing off and read the raw counts.',
{ column: DEFAULT_WEIGHT_COLUMN },
)
}
return mc.handle.readNumeric(path, 0, mc.nBins)
}
|