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 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 309x 309x 309x 309x 309x 309x 3489x 3489x 12x 12x 12x 3489x 12x 12x 12x 3477x 3489x 574x 574x 574x 2903x 2866x 3489x 3489x 3489x 672x 672x 672x 3359x 3489x 309x 309x 2x 81x 81x 81x 81x 81x 2x 68x 68x 2x 49x 49x 2x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 2x 41x 41x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 41x 41x 2x 78x 78x 222x 222x 222x 222x 222x 222x 179x 179x 179x 222x 222x 222x 222x 222x 222x 222x 222x 222x 222x 222x 222x 78x 78x 212x 212x 206x 206x 2x 49x 49x 49x 49x 203x 203x 203x 267x 18x 18x 18x 249x 64x 64x 203x 203x 49x 49x 2x 44x 44x 1077x 1077x 10x 10x 1077x 44x 44x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 27x 27x 14x 14x 27x 10x 10x 23x 1x 1x 2x 177x 177x 26x 26x 26x 2x 39x 39x 39x 39x 39x 39x 39x 49x 49x 49x 49x 203x 171x 203x 22x 22x 22x 22x 22x 149x 149x 49x 49x 49x 49x 49x 49x 49x 49x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 47x 49x 149x 149x 149x 149x 149x 93x 56x 149x 149x 149x 149x 149x 149x 149x 149x 149x 49x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 49x 49x 49x 49x 49x 49x 23x 23x 23x 23x 23x 23x 49x 26x 49x 24x 24x 24x 24x 24x 24x 24x 24x 149x 149x 149x 301x 301x 301x 149x 149x 9x 9x 431x 431x 431x 251x 251x 431x 2x 2x 431x 2x 2x 38x 38x 38x 38x 38x 38x 38x 38x 38x 68x 68x 68x 68x 38x 13x 13x 13x 13x 13x 13x 13x 38x 38x 38x 38x 38x 34x 34x 34x 34x 34x 25x 25x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 171x 171x 171x 43x 43x 9x 9x 43x 43x 2x 14x 14x 14x 64x 64x 16x 16x 16x 16x 16x 16x 64x 34x 34x 34x 34x 64x 64x 64x 64x 14x 14x 50x 50x 50x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 13x 13x 13x 13x 13x 13x 39x 39x | /**
* UCSC **track hubs**, read as stores (§24.10).
*
* ## Why a hub is a store and not a new idea
*
* A store is somebody's shelf of data written down once, with enough about each
* row to choose without opening it (`state/store.ts`). A track hub is the same
* object, declared in a format that predates this one by fifteen years and is
* what most of the world's published genomics data is actually indexed by: a
* `hub.txt` naming a `genomes.txt` naming a `trackDb.txt` per assembly, each
* track a stanza with a `bigDataUrl` and a `type`.
*
* So this module is a **translation**, not a second catalogue system. It reads
* the three files and produces a `StoreFile`, and from that point on a hub *is*
* a store: the same table, the same facets, the same two verbs on every row, the
* same `?store=` in the address bar, the same *remember this store*, the same
* download. Nothing downstream of {@link hubStore} knows what a hub is, which is
* the whole of what makes them first-class rather than bolted on.
*
* ## What a reader has to do
*
* Paste the `hub.txt` URL into the same box every other store URL goes into.
* `fetchStore` recognises a hub and comes here, so a hub URL works in `?store=`,
* in the Connect dialog, as a remembered tile and in a shared link — every road
* a store URL already travels.
*
* ## The genome row, and why `.2bit` had to come first
*
* A hub's genome is one of two things, and the split runs through everything
* here.
*
* An **assembly hub** carries its own sequence, as `twoBitPath` — always, and
* only, a `.2bit`. That is why `annotations/twobit.ts` is part of this release
* rather than a separate nicety: without it, every assembly hub in the world
* opens as a set of tracks over coordinates this viewer cannot read. With it,
* the genome row *is* the sequence, at two bits a base, read by range.
*
* A **db hub** names an assembly UCSC already hosts (`genome hg38`) and carries
* no sequence at all. There is then nothing to open as the genome — so the row
* falls back, in order, to the `chromSizes` the hub declares, and failing that
* to the **first drawable bbi file in its own trackDb** — any `big*` type, all
* of which are bigBeds or bigWigs underneath — whose header carries every
* sequence and its exact length (§5.8). That last is not a
* guess: it is the same door a dropped bigWig opens a browser through, and it
* keeps the whole arrangement inside files the hub itself names. What this
* deliberately does *not* do is reach for `hgdownload.soe.ucsc.edu` to fetch a
* `chrom.sizes` nobody asked for: C1 says nothing leaves the machine that the
* reader did not ask for, and a third-party request invented by a translation
* layer is exactly that.
*
* ## What the hub asks to be on
*
* A published hub is not a short list. EPFL's Breast Cancer Epigenomics hub
* declares some 440 bigWigs across nine composites and asks for twenty of them,
* so the genome row's shelf — the one *open with its tracks* loads — is what
* the hub asks for and not everything it names: `parent <name> off` and
* `visibility hide` are its own answer, and a hidden composite hides its
* children whatever their own word says. Every track keeps a row of its own,
* because off by default is not absent.
*
* ## What is refused, and where it is said
*
* A track whose `type` this viewer cannot draw gets **no row**, and is counted
* in the store's description rather than being silently absent — `docs/stores.md`'s
* rule for the IGV generator, applied here at read time. `cram` and `halSnake`
* are the two that matter: the first is refused on its own grounds (GAPS.md, and
* `browse/route.ts` says so where a reader meets one), and the second is a
* server-side alignment format with no file behind it at all.
*/
import { canonicalUrl } from '../sources/rewrite'
import { STORE_KIND, STORE_VERSION, type StoreColumn, type StoreEntry, type StoreFile } from './store'
/** The columns a hub's rows are narrowed by. */
export const HUB_COLUMNS: readonly StoreColumn[] = [
{ key: 'assembly', label: 'Assembly' },
{ key: 'genome', label: 'Genome' },
{ key: 'role', label: 'Role' },
{ key: 'format', label: 'Format' },
// The composite, super-track or view a subtrack hangs under. A hub's own
// grouping, which is the thing its author spent their time on, and the one
// column a reader of somebody else's hub will actually narrow by.
{ key: 'group', label: 'Group' },
]
// ---------------------------------------------------------------------------
// The stanza format
// ---------------------------------------------------------------------------
/** One stanza: `key value` lines, in the order they were written. */
export type Stanza = Map<string, string>
/**
* Split a hub file into stanzas.
*
* The format is UCSC's `ra`: `key value` a line, a blank line between records,
* `#` for a comment, and a trailing `\` continuing a line. Three details are
* worth stating because real hubs exercise all of them.
*
* **Leading whitespace is meaningless.** Subtracks are conventionally indented
* under their composite, and that indentation is presentation: what nests a
* track is its `parent` line and nothing else. Stripping it is therefore not a
* convenience, it is the format.
*
* **A repeated `startKey` begins a new record even with no blank line between.**
* The specification asks for the blank line; hubs in the wild do not always have
* one, particularly around indented subtracks, and a parser that insisted would
* merge a composite and its first subtrack into one stanza whose `bigDataUrl`
* belongs to neither. Keying on the record's *first* key is what the file means
* either way.
*
* **A repeated key inside one stanza keeps the first.** `ra` is order-sensitive
* and the first wins; the case that matters is a hub that sets `type` twice.
*/
export function parseStanzas(text: string, startKey?: string | readonly string[]): Stanza[] {
const starts =
startKey === undefined ? null : new Set(typeof startKey === 'string' ? [startKey] : startKey)
const out: Stanza[] = []
let current: Stanza | null = null
let pending = ''
for (const raw of text.split('\n')) {
let line = raw.endsWith('\r') ? raw.slice(0, -1) : raw
// A continuation joins before anything else looks at the line, so a stanza
// break cannot fall in the middle of one.
if (pending !== '') {
line = `${pending} ${line.trim()}`
pending = ''
}
if (line.endsWith('\\')) {
pending = line.slice(0, -1).trimEnd()
continue
}
const trimmed = line.trim()
if (trimmed === '') {
current = null
continue
}
if (trimmed.startsWith('#')) continue
const at = trimmed.search(/\s/)
const key = at < 0 ? trimmed : trimmed.slice(0, at)
const value = at < 0 ? '' : trimmed.slice(at + 1).trim()
if (current === null || (starts !== null && starts.has(key) && current.size > 0)) {
current = new Map()
out.push(current)
}
if (!current.has(key)) current.set(key, value)
}
return out
}
// ---------------------------------------------------------------------------
// hub.txt, genomes.txt, trackDb.txt
// ---------------------------------------------------------------------------
export interface HubHeader {
/** `hub` — the hub's short identifier. */
name: string
shortLabel: string
longLabel: string
/** Relative to the `hub.txt`, or absent on a one-file hub. */
genomesFile: string
/** `useOneFile on`: the genome and track stanzas are in this same file. */
oneFile: boolean
descriptionUrl: string
email: string
}
/**
* Whether these bytes are a `hub.txt`.
*
* The `hub` key, and a `shortLabel` beside it. One key alone would match a
* `trackDb.txt` whose first track is called `hub`, and neither appears in a
* JSON store — which is the discrimination this is really for, since the two
* arrive through the same box.
*/
export function looksLikeHub(text: string): boolean {
const stanzas = parseStanzas(text)
const first = stanzas[0]
if (first === undefined) return false
return first.has('hub') && (first.has('shortLabel') || first.has('genomesFile'))
}
/** `…/hub.txt` — the name the convention fixes, which is what a reader pastes. */
export function looksLikeHubUrl(url: string): boolean {
return /(^|\/)[^/?#]*hub[^/?#]*\.txt(\?|#|$)/i.test(url)
}
/**
* Whether this text carries the genomes as well as the hub line.
*
* What tells a whole hub from its first file — and so what decides whether a
* pasted text can be read at all, since pasting leaves nothing to fetch the
* rest from.
*/
export function hasGenomeStanzas(text: string): boolean {
return parseStanzas(text, ['hub', 'genome', 'track']).some((stanza) => stanza.has('genome'))
}
export function parseHubHeader(text: string): HubHeader | null {
const stanza = parseStanzas(text)[0]
if (stanza === undefined || !stanza.has('hub')) return null
return {
name: stanza.get('hub') ?? '',
shortLabel: stanza.get('shortLabel') ?? '',
longLabel: stanza.get('longLabel') ?? '',
genomesFile: stanza.get('genomesFile') ?? '',
oneFile: /^on$/i.test(stanza.get('useOneFile') ?? ''),
descriptionUrl: stanza.get('descriptionUrl') ?? '',
email: stanza.get('email') ?? '',
}
}
export interface HubGenome {
/** The assembly name: `hg38`, or an assembly hub's own identifier. */
assembly: string
/** A human name where the hub gives one — `organism`, else `description`. */
label: string
/** Relative to `genomes.txt`. Absent on a one-file hub. */
trackDb: string
/** An assembly hub's own sequence. The reason `.2bit` is in this release. */
twoBitPath: string
chromSizes: string
description: string
defaultPos: string
}
export function parseHubGenomes(text: string): HubGenome[] {
const out: HubGenome[] = []
for (const stanza of parseStanzas(text, 'genome')) {
const assembly = stanza.get('genome') ?? ''
if (assembly === '') continue
const organism = stanza.get('organism') ?? ''
const description = stanza.get('description') ?? ''
out.push({
assembly,
label: organism !== '' ? organism : description !== '' ? description : assembly,
trackDb: stanza.get('trackDb') ?? '',
twoBitPath: stanza.get('twoBitPath') ?? '',
chromSizes: stanza.get('chromSizes') ?? '',
description,
defaultPos: stanza.get('defaultPos') ?? stanza.get('defaultPosition') ?? '',
})
}
return out
}
/** One stanza of a `trackDb.txt` that names a file. */
export interface HubTrack {
track: string
shortLabel: string
longLabel: string
/** The `type` word, lower-cased: `bigwig`, `bigbed`, `vcftabix`, `hic`, … */
type: string
/** Relative to the `trackDb.txt`. */
bigDataUrl: string
/** A sidecar the hub names outright — a `.tbi` or `.bai` at an odd address. */
bigDataIndex: string
/** The `parent` or `superTrack` this hangs under, by track name. */
parent: string
/** `true` for a composite, super-track or view: a heading, not a file. */
container: boolean
/**
* What the hub asks for when a reader connects: `parent <name> on` or a
* `visibility` that is not `hide`. This is the stanza's own word only —
* {@link hubStore} cascades it through the containers.
*/
shown: boolean
}
export function parseTrackDb(text: string): HubTrack[] {
const out: HubTrack[] = []
for (const stanza of parseStanzas(text, 'track')) {
const track = stanza.get('track') ?? ''
if (track === '') continue
// `parent <name> [on|off]` — the visibility word is not part of the name,
// but it is the hub's answer to whether this one is on, so keep it.
const hangs = (stanza.get('parent') ?? stanza.get('superTrack') ?? '').split(/\s+/)
const parent = hangs[0] ?? ''
const container =
/^on$/i.test(stanza.get('compositeTrack') ?? '') ||
/^on$/i.test(stanza.get('superTrack') ?? '') ||
stanza.has('container') ||
(stanza.get('view') ?? '') !== ''
out.push({
track,
shortLabel: stanza.get('shortLabel') ?? '',
longLabel: stanza.get('longLabel') ?? '',
// `type bigBed 9 +` — the format is the first word and the rest is its
// arity, which nothing here needs: this viewer reads a bigBed's real
// column count out of the file.
type: (stanza.get('type') ?? '').split(/\s+/)[0].toLowerCase(),
bigDataUrl: stanza.get('bigDataUrl') ?? '',
bigDataIndex: stanza.get('bigDataIndex') ?? '',
parent: parent === track ? '' : parent,
container,
shown: shownHere(stanza.get('visibility') ?? '', hangs[1] ?? ''),
})
}
return out
}
/**
* Whether one stanza asks to be on, by its own two words.
*
* A subtrack's checkbox is the word after its `parent`; a top-level track's is
* its `visibility`. A subtrack usually carries both — `parent x off` beside
* `visibility dense` — and they are not in conflict: the second is the density
* to draw it at *if* it is on, which the first says it is not.
*/
function shownHere(visibility: string, hangsAs: string): boolean {
if (/^off$/i.test(hangsAs)) return false
return !/^(hide|none)$/i.test(visibility.trim())
}
/** How far up a `parent` chain to walk before calling it a cycle. */
const PARENT_DEPTH = 16
/**
* The tracks a hub asks to be **on**, cascaded through their containers.
*
* A hub says twice what a reader should see on connecting: once per track, and
* once per composite over all of them. A hidden composite hides its children
* whatever their own word says, which is UCSC's rule and the only reading that
* makes the two words agree.
*
* Honouring it is the difference between a shelf and a stampede. A published
* hub is not a short list: EPFL's breast-cancer hub declares some 440 bigWigs
* across nine composites and asks for twenty of them, and *open with its
* tracks* on that genome has to mean the twenty. Every one of the 440 still has
* a row of its own, which is where a reader goes for the other 420.
*/
function shownTracks(tracks: readonly HubTrack[]): Set<string> {
const byName = new Map(tracks.map((track) => [track.track, track]))
const out = new Set<string>()
for (const track of tracks) {
let held: HubTrack | undefined = track
let on = true
for (let hop = 0; held !== undefined && hop < PARENT_DEPTH; hop++) {
if (!held.shown) {
on = false
break
}
if (held.parent === '') break
held = byName.get(held.parent)
}
if (on) out.add(track.track)
}
return out
}
/** `include other.txt` lines, which a trackDb may use to split itself up. */
export function includesOf(text: string): string[] {
const out: string[] = []
for (const raw of text.split('\n')) {
const line = raw.trim()
if (!line.startsWith('include ')) continue
const named = line.slice('include '.length).trim()
if (named !== '') out.push(named)
}
return out
}
// ---------------------------------------------------------------------------
// What this viewer can draw
// ---------------------------------------------------------------------------
/**
* A hub `type` word → what this viewer calls the format, or `null` to refuse.
*
* The list is UCSC's own set of remote data types, and every entry in it is a
* decision this release either already made or has just made. The five new ones
* are the reason a hub is worth reading at all: before them, `bigInteract`,
* `longTabix` and `bigMethyl` tracks would each have got a row that drew the
* wrong picture when it was pressed, which is worse than no row.
*/
const HUB_TYPES: Record<string, string> = {
bigwig: 'bigWig',
bigbed: 'bigBed',
bignarrowpeak: 'bigNarrowPeak',
biggenepred: 'bigGenePred',
bigpsl: 'bigPsl',
bigchain: 'bigChain',
bigmaf: 'bigMaf',
biglolly: 'bigLolly',
bigdbsnp: 'bigDbSnp',
bigrmsk: 'bigRmsk',
bigbarchart: 'bigBarChart',
barchart: 'barChart',
biginteract: 'bigInteract',
interact: 'interact',
bigmethyl: 'bigMethyl',
bedmethyl: 'bedMethyl',
longtabix: 'longrange',
vcftabix: 'VCF',
bam: 'BAM',
hic: 'hic',
}
/**
* Why a track is not offered, or `null` when it is.
*
* Named rather than counted, because the two refusals here are the two a reader
* is most likely to go looking for — and because "your hub has 40 tracks and
* this shows 38" with no reason is the kind of silence this project spends its
* comments avoiding.
*/
export function hubRefusal(type: string): string | null {
if (HUB_TYPES[type] !== undefined) return null
if (type === 'cram') {
return 'CRAM needs the exact reference its records were compressed against, which this viewer will not fetch'
}
if (type === 'halsnake' || type === 'hal') {
return 'HAL alignments are drawn by a server, and there is no file behind one to read'
}
if (type === '') return 'the stanza names no type'
return `no reader for type ${type}`
}
/** The format label for a drawable type. */
export function hubFormat(type: string): string | null {
return HUB_TYPES[type] ?? null
}
/**
* `true` for a type whose own header carries the assembly (§5.8).
*
* Every `big*` type in {@link HUB_TYPES} is a bbi file — a bigBarChart, a
* bigMethyl and a bigInteract are each a bigBed with a particular autoSql and
* nothing else (`annotations/barchart.ts`) — so each one's header holds the
* whole chrom B-tree, every sequence with its exact length. That is what a db
* hub's genome row falls back to, and excluding any of them means a hub whose
* tracks happen to all be one kind gets no genome row at all: the miRNAome hub
* is two bigBarCharts on hg38 and nothing else.
*
* The types that do *not* qualify are the ones with no bbi header to read:
* `bam`, `vcfTabix`, `longTabix`, `hic` and the three text formats. A BAM does
* declare its sequences, in `@SQ` lines, but the genome row has to be a file the
* 1D browser opens a genome *on*, and that is not one.
*/
function declaresAssembly(type: string): boolean {
return type.startsWith('big')
}
// ---------------------------------------------------------------------------
// Hub → store
// ---------------------------------------------------------------------------
/** One genome's trackDb, as read. */
export interface HubGenomeTracks {
genome: HubGenome
tracks: readonly HubTrack[]
/** The URL its `trackDb` was read from, which its `bigDataUrl`s resolve against. */
trackDbUrl: string
}
export interface HubStoreOptions {
header: HubHeader
/** The URL the `hub.txt` was read from. */
hubUrl: string
genomes: readonly HubGenomeTracks[]
/** The URL `genomes.txt` was read from; an assembly's own files resolve against it. */
genomesUrl: string
}
/**
* A hub, as the store it is.
*
* Two rows per drawable track is tempting and wrong: one row is the file, and
* what a reader wants of a *genome* is the sequence with its tracks on it. So
* the genome gets one row carrying `tracks`, which the shell turns into the
* shelf offered after it opens (`App.noteShelf`), and every track gets a row of
* its own for the reader who wants one file over their own map. That is exactly
* the arrangement the IGV store arrived at (`docs/stores.md`), reached
* independently from the same two questions.
*/
export function hubStore(options: HubStoreOptions): StoreFile {
const { header, hubUrl, genomes, genomesUrl } = options
const entries: StoreEntry[] = []
let refused = 0
let drawn = 0
let offered = 0
const reasons = new Map<string, number>()
for (const held of genomes) {
const { genome, tracks, trackDbUrl } = held
const shown = shownTracks(tracks)
const drawable: Array<{ track: HubTrack; url: string; format: string }> = []
for (const track of tracks) {
if (track.bigDataUrl === '') continue // a composite, a view, a heading
const format = hubFormat(track.type)
if (format === null) {
refused++
const why = hubRefusal(track.type) ?? 'unreadable'
reasons.set(why, (reasons.get(why) ?? 0) + 1)
continue
}
drawable.push({ track, url: withIndex(resolve(track.bigDataUrl, trackDbUrl), track, trackDbUrl), format })
}
const label = genome.label === genome.assembly ? genome.assembly : `${genome.label} (${genome.assembly})`
// The shelf is **annotations**, and a contact map is not one. A `type hic`
// track has a row of its own that opens through the map door, and putting
// it here as well would hand it to the annotation loader, which reads it as
// text, sniffs it as a BED and reports that all 32,918 of its lines failed
// the contig/start/end test — a true sentence about the wrong question.
// What a reader wants from a hub's map is to open it as a map, and that row
// already does.
// …and not what the hub has switched off. `visibility hide` on a composite
// and `parent <name> off` on a subtrack are how a hub says which of its
// tracks a reader should arrive to, and a shelf that ignored them would
// open four hundred bigWigs at a press.
const shelf = drawable
.filter((held_) => !isContactMap(held_.track) && shown.has(held_.track.track))
.map((held_) => ({ label: labelOf(held_.track), url: held_.url }))
drawn += drawable.length
offered += shelf.length
const sequence = sequenceFor(genome, genomesUrl, drawable)
if (sequence !== null) {
entries.push({
label,
uri: sequence.uri,
kind: 'genome',
note: sequence.note,
fields: {
assembly: genome.assembly,
genome: genome.label,
role: 'sequence',
format: sequence.format,
...(genome.description === '' ? {} : { group: genome.description }),
},
...(shelf.length === 0 ? {} : { tracks: shelf }),
})
}
for (const { track, url, format } of drawable) {
// A `.hic` is a contact map and goes through the map door; everything else
// annotates a genome. `kindOf` would work this out from the name, and is
// not asked: a hub states the type outright, which is better evidence than
// any suffix, and plenty of `bigDataUrl`s end in `?token=…`.
entries.push({
label: labelOf(track),
uri: url,
kind: track.type === 'hic' ? 'map' : 'genome',
...(track.longLabel === '' || track.longLabel === track.shortLabel
? {}
: { note: track.longLabel }),
fields: {
assembly: genome.assembly,
genome: genome.label,
role: 'annotation',
format,
...(track.parent === '' ? {} : { group: track.parent }),
},
})
}
}
return {
kind: STORE_KIND,
version: STORE_VERSION,
name: header.shortLabel !== '' ? header.shortLabel : header.name !== '' ? header.name : hubUrl,
description: describe(header, genomes.length, refused, reasons, drawn, offered),
source: hubUrl,
columns: [...HUB_COLUMNS],
entries,
}
}
/**
* What the genome row opens, and what to call it.
*
* The ladder the module header describes. `null` where a db hub carries neither
* a `chromSizes` nor a track whose header declares an assembly — a hub of
* nothing but BAMs and VCFs against `hg38`, which is legal and which this cannot
* open a genome from. The tracks still get their rows; what is missing is the
* one row that would have been the assembly, and inventing a `hgdownload` URL
* for it is the third-party request this refuses to make.
*/
function sequenceFor(
genome: HubGenome,
genomesUrl: string,
drawable: ReadonlyArray<{ track: HubTrack; url: string; format: string }>,
): { uri: string; note: string; format: string } | null {
if (genome.twoBitPath !== '') {
return {
uri: resolve(genome.twoBitPath, genomesUrl),
note: `The hub’s own sequence for ${genome.assembly}, two bits a base.`,
format: '2bit',
}
}
if (genome.chromSizes !== '') {
return {
uri: resolve(genome.chromSizes, genomesUrl),
note: `${genome.assembly}: the sequence names and lengths this hub declares.`,
format: 'chrom.sizes',
}
}
const declaring = drawable.find((held) => declaresAssembly(held.track.type))
if (declaring === undefined) return null
return {
uri: declaring.url,
note:
`${genome.assembly}: this hub names no sequence of its own, so the browser opens on the ` +
`lengths in ${labelOf(declaring.track)}’s header.`,
format: declaring.format,
}
}
/**
* Whether this track is a contact map rather than something drawn along an axis.
*
* One type, and it is the app's *primary* object rather than an annotation of
* one — which is why it takes the map door in `hubStore` and is kept off the
* track shelf.
*/
function isContactMap(track: HubTrack): boolean {
return track.type === 'hic'
}
/** A track's display name: its `shortLabel`, or the identifier it was declared under. */
function labelOf(track: HubTrack): string {
return track.shortLabel !== '' ? track.shortLabel : track.track
}
/**
* `#index=` for a hub that names its sidecar somewhere unguessable.
*
* `bigDataIndex` exists precisely for the BAM or tabix file whose index is not
* `<url>.bai` — which is the one case `annotations/sidecar.ts`'s guess cannot
* cover — and the fragment is how this app carries that fact alongside the
* address (§8.13). Where the hub is silent the guess still applies, exactly as
* it does for a pasted URL.
*/
function withIndex(url: string, track: HubTrack, base: string): string {
if (track.bigDataIndex === '') return url
return `${url}#index=${resolve(track.bigDataIndex, base)}`
}
/**
* A hub-relative path against the file that named it.
*
* `canonicalUrl` first, so a hub pointing at `s3://` or a Drive share link
* resolves to the address that will actually be fetched (§24.5) — hubs do carry
* those, and a bucket URL run through `new URL(…, base)` would come out as a
* path under the hub.
*/
function resolve(path: string, base: string): string {
const canonical = canonicalUrl(path)
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(canonical)) return canonical
try {
return new URL(canonical, base).href
} catch {
return canonical
}
}
// ---------------------------------------------------------------------------
// Reading one
// ---------------------------------------------------------------------------
/**
* How this module asks for a file.
*
* `null` for anything that did not come back — a 404 on one genome's trackDb is
* a genome without tracks, not the end of a hub, and this returns what it read
* rather than a diagnosis. `state/store.ts` owns the fetching, the signing and
* the error wording, and hands the bytes here; the same separation
* `annotations/fai.ts` keeps, and it is what lets every case below be pinned by
* a test with no network in it.
*/
export type ReadHubFile = (url: string) => Promise<string | null>
/** How many `include` hops a trackDb may take before this stops following. */
const INCLUDE_DEPTH = 4
/** How many files one hub may read in total, so a cycle cannot become a crawl. */
const FILE_BUDGET = 64
/**
* Read a hub, from its `hub.txt` text, into the store it stands for.
*
* Three shapes, and the first is increasingly the common one.
*
* **One file.** `useOneFile on` puts the hub, genome and track stanzas in the
* single file already read, which is what UCSC now recommends for a hub of a
* handful of tracks. Nothing further is fetched.
*
* **hub.txt → genomes.txt → trackDb.txt**, the original arrangement, and the one
* every large hub uses.
*
* **A hub whose genomes cannot be read** — a `genomesFile` that 404s — comes
* back as a store with no entries rather than as an error, because the hub
* header is still worth showing and the reason is in the description. What is
* refused outright is text that is not a hub at all, which `fetchStore` has
* already decided before calling this.
*/
export async function readHub(
hubUrl: string,
text: string,
read: ReadHubFile,
): Promise<StoreFile | null> {
const header = parseHubHeader(text)
if (header === null) return null
let budget = FILE_BUDGET
const fetchOne = async (url: string): Promise<string | null> => {
if (budget <= 0) return null
budget--
return read(url)
}
// `useOneFile on` — **or** a text that simply has genome stanzas in it, which
// is the same file by another route and is what a **pasted** hub looks like:
// somebody's `hub.txt`, `genomes.txt` and `trackDb.txt` one after another in a
// box (§24.10). Concatenating them *is* a one-file hub in every respect that
// matters here, because the only keys it loses are the two that say where the
// next file is, and the next file is already in hand. A real three-file
// `hub.txt` has no `genome` stanza, so this can never fire on one by mistake.
if (header.oneFile || hasGenomeStanzas(text)) {
return hubStore({
header,
hubUrl,
genomes: oneFileGenomes(text, hubUrl),
genomesUrl: hubUrl,
})
}
const genomesUrl = header.genomesFile === '' ? hubUrl : resolve(header.genomesFile, hubUrl)
const genomesText = header.genomesFile === '' ? null : await fetchOne(genomesUrl)
const genomes = genomesText === null ? [] : parseHubGenomes(genomesText)
const held: HubGenomeTracks[] = []
for (const genome of genomes) {
if (genome.trackDb === '') {
held.push({ genome, tracks: [], trackDbUrl: genomesUrl })
continue
}
const trackDbUrl = resolve(genome.trackDb, genomesUrl)
const tracks = await readTrackDb(trackDbUrl, fetchOne, INCLUDE_DEPTH)
held.push({ genome, tracks, trackDbUrl })
}
return hubStore({ header, hubUrl, genomes: held, genomesUrl })
}
/**
* A `trackDb.txt` and whatever it `include`s.
*
* An included file's `bigDataUrl`s resolve against **its own** address, not the
* including one, which is why the tracks come back already resolved rather than
* as stanzas to be resolved later by a caller holding the wrong base. Depth is
* bounded because `include` cycles exist and a hub is somebody else's file.
*/
async function readTrackDb(
url: string,
read: ReadHubFile,
depth: number,
seen = new Set<string>(),
): Promise<HubTrack[]> {
if (depth <= 0 || seen.has(url)) return []
seen.add(url)
const text = await read(url)
if (text === null) return []
const out = parseTrackDb(text).map((track) => ({
...track,
// Resolved here, against the file that named it, and marked absolute so
// `hubStore` leaves it alone.
bigDataUrl: track.bigDataUrl === '' ? '' : resolve(track.bigDataUrl, url),
bigDataIndex: track.bigDataIndex === '' ? '' : resolve(track.bigDataIndex, url),
}))
for (const named of includesOf(text)) {
out.push(...(await readTrackDb(resolve(named, url), read, depth - 1, seen)))
}
return out
}
/**
* A `useOneFile on` hub's genomes, with each one's tracks under it.
*
* The file is stanzas in order — the hub, then a genome, then its tracks, then
* perhaps another genome — so the tracks belong to whichever `genome` stanza
* they follow. Ordering *is* the nesting here, which is the one place in this
* format where that is true, and it is why this cannot go through
* {@link parseTrackDb} over the whole text: that would give one flat list with
* no way back to which assembly each track was under.
*
* A track before any genome stanza is dropped. There is no assembly to draw it
* against, and a one-file hub that begins with tracks is malformed rather than
* ambiguous.
*/
export function oneFileGenomes(text: string, url: string): HubGenomeTracks[] {
const out: HubGenomeTracks[] = []
let current: { genome: HubGenome; tracks: HubTrack[] } | null = null
for (const stanza of parseStanzas(text, ['hub', 'genome', 'track'])) {
if (stanza.has('hub')) continue
if (stanza.has('genome')) {
const parsed = parseHubGenomes(stanzaText(stanza))[0]
if (parsed === undefined) continue
current = { genome: parsed, tracks: [] }
out.push({ genome: parsed, tracks: current.tracks, trackDbUrl: url })
continue
}
if (!stanza.has('track') || current === null) continue
const track = parseTrackDb(stanzaText(stanza))[0]
if (track === undefined) continue
current.tracks.push({
...track,
bigDataUrl: track.bigDataUrl === '' ? '' : resolve(track.bigDataUrl, url),
bigDataIndex: track.bigDataIndex === '' ? '' : resolve(track.bigDataIndex, url),
})
}
return out
}
/**
* A stanza back as text, so one parser reads it however it arrived.
*
* The alternative is a second reader for each record type — one over a `Map`
* and one over lines — and two readers of one format is how the one-file hub
* and the three-file hub come to disagree about what `superTrack` means.
*/
function stanzaText(stanza: Stanza): string {
return [...stanza].map(([key, value]) => `${key} ${value}`).join('\n')
}
/** The line under the store's name: what the hub says it is, and what was dropped. */
function describe(
header: HubHeader,
genomes: number,
refused: number,
reasons: ReadonlyMap<string, number>,
drawn: number,
offered: number,
): string {
const parts: string[] = []
if (header.longLabel !== '' && header.longLabel !== header.shortLabel) parts.push(header.longLabel)
parts.push(`${genomes} ${genomes === 1 ? 'assembly' : 'assemblies'}`)
// Said only when the hub switched some off, because otherwise the number is
// the row count and the table is already showing it.
if (drawn > offered) parts.push(`${drawn} tracks, ${offered} on by default`)
if (refused > 0) {
const why = [...reasons.entries()]
.sort((a, b) => b[1] - a[1])
.map(([reason, count]) => `${count} — ${reason}`)
.join('; ')
parts.push(`${refused} ${refused === 1 ? 'track' : 'tracks'} not offered (${why})`)
}
return parts.join(' · ')
}
|