# Versioned browser data shards
VariantHound's full processed research dataset is too large to ship inside the web application bundle. `pipelines/build_browser_shards.py` converts the verified source snapshots into small, immutable files that can be hosted in the public Hugging Face dataset and fetched on demand.
## Collections
| Collection | Contents | Partition key |
|---|---|---|
| `terms` | Active HPO and MP labels, aliases, synonyms, asserted parents, and information content, plus the UPHENO bridge terms | phenotype ID |
| `phenotype_closure` | Entailed reflexive HP/MP/uPheno ancestor relations | phenotype ID |
| `gene_profiles` | Stable-ID human, mouse, and structured canine phenotype profiles | species and gene key |
| `orthology` | Compact Ensembl 116 dog-to-human and dog-to-mouse relations | dog Ensembl gene ID |
Each collection has 16 buckets selected by the first hexadecimal character of the SHA-256 digest of its partition key. Every filename includes the first 12 characters of its content digest. The stable `manifest.json` records the complete digest, byte size, row count, format, and bucket for every file.
The generated gzip streams use a zero timestamp and no source filename, so identical inputs produce identical bytes. The builder verifies all source snapshot checksums before reading them and re-verifies the complete output package after writing it.
The manifest declares `format_version` `1.1.0`. A `1.0.0` package has no information content and no bridge terms, and the runtime loader rejects it outright rather than ranking against it.
## Information content and the UPHENO bridge
Similarity is Resnik: the score for two phenotypes is the information content of their most informative common ancestor, normalised by the corpus maximum. Two properties of the pinned data make this non-obvious.
**Information content is a property of the corpus, so the builder computes it.** For each term `t`, `IC(t) = -ln(P(t))`, where `P(t)` is the share of annotated gene profiles whose phenotype set entails `t` through the pinned closure. Terms no profile entails receive the corpus ceiling, `-ln(1/(N+1))`, so an unannotated term can never look less specific than an annotated one. Values are rounded to six decimals to keep the build byte-reproducible, and the manifest records `annotated_gene_profiles`, `information_content_terms`, and `maximum_information_content`.
Computing IC in the browser instead was rejected: it would require the entire closure before scoring anything, and the value would then depend on which buckets a session happened to load, so the same query could score differently across sessions.
**HP and MP are disjoint trees.** Their asserted parents never meet, so a human HPO profile and a mouse MP profile share no common ancestor and every cross-species similarity would be exactly zero. The only bridge is the UPHENO ancestors in `phenotype_closure`. The builder therefore emits UPHENO terms into the `terms` collection as well, carrying their information content. They have no label in the pinned PHENIO release and are never displayed; they exist to be a most informative common ancestor. `statistics.active_terms` counts displayable HP and MP terms, `statistics.bridge_terms` counts the UPHENO additions.
Consequently the runtime ontology index is built from the closure, not from `parents`. A partially loaded closure degrades to exact matching rather than silently scoring unrelated terms as similar.
## Runtime loading
`packages/ranker/src/shards/` implements the browser loader. In order: fetch `manifest.json` with `cache: "no-store"`, validate its shape and reject an unsupported `format_version`, then for each shard verify the SHA-256 of the *compressed* bytes against the manifest before decompressing, decompress with `DecompressionStream("gzip")`, and parse JSONL or TSV. Verified compressed bytes are stored in Cache Storage under `varianthound-dataset-v1`, keyed by build ID and content digest, and are re-verified on every read rather than trusted. The previous build's entries are evicted only after the new build has fully verified, so an interrupted upgrade cannot strand an offline user with no dataset.
Shards live under an immutable per-build prefix, `///`, so two builds can never share a path. The application origin's service worker skips dataset requests entirely; the loader's own cache is the only cache in that path.
`terms` and `gene_profiles` are loaded in full. Ranking scores every gene and a phenotype query cannot predict which gene buckets matter, so partial loading there would silently drop candidates. `orthology` and `phenotype_closure` are bucket-addressable and can be narrowed once a candidate set exists.
Candidate genes are keyed on the dog Ensembl gene from the pinned orthology snapshot, with human and mouse evidence attached through their ortholog Ensembl IDs. Canine OMIA profiles are keyed on NCBI Gene, so they are emitted as their own unfused candidates and the canine evidence track stays empty for orthology-keyed genes until the pinned dog NCBI-Ensembl crosswalk lands. Joining them by symbol is prohibited.
Discovery masking is applied when the dataset is constructed, before any profile, feature or score exists. The ranker re-applies it as defence in depth.
## Publishing a build
```bash
npm run build:browser
npm run publish:browser -- --bucket varianthound-data --dry-run
npm run publish:browser -- --bucket varianthound-data
```
The publisher re-verifies the package before uploading anything, writes shards
under `/` with a one-year immutable cache, and publishes
`manifest.json` last with `no-store` so a reader can never see a manifest
pointing at objects that are not there yet.
Shards are deliberately served **without** `Content-Encoding: gzip`. The
manifest digests the compressed bytes and the loader verifies them before
decompressing; if the origin advertised gzip encoding the browser would inflate
the body transparently and every checksum would fail. They are opaque
`application/gzip` payloads that the application decompresses itself.
`infra/r2-cors.json` holds the bucket CORS rules, in the `{"rules": [...]}`
shape the R2 API expects (not the S3-style `AllowedOrigins` array the dashboard
displays):
```bash
npx wrangler r2 bucket cors set varianthound-data --file infra/r2-cors.json
```
## Regenerating the test fixture
The TypeScript loader tests run against a miniature package committed at `packages/ranker/test/fixtures/browser/`, built through the same code path:
```bash
python3 pipelines/build_fixture_shards.py # regenerate
python3 pipelines/build_fixture_shards.py --check # fail if it has drifted
```
Regenerate and commit it whenever the shard format changes; `pipelines/tests/test_build_fixture_shards.py` fails the build if you forget.
## Stable identity and exclusions
Human HPO profiles use Ensembl IDs when the pinned Monarch/HGNC crosswalk supplies one, otherwise NCBI Gene remains the key. Mouse profiles use the direct MGI stable-ID crosswalk and exclude unresolved, conflicting, and ambiguous multi-gene evidence. Canine OMIA profiles retain discovery mask IDs but enter the browser semantic set only when OMIA supplies a structured phenotype identifier.
Dog OMIA records currently use NCBI Gene while the Ensembl orthology snapshot uses dog Ensembl IDs. They are not joined by symbol. Cross-species canine fusion remains blocked until a separately pinned dog NCBIāEnsembl gene crosswalk is added.
## Reproduction
After materializing every pinned source, run:
```bash
npm run build:browser
```
The Hugging Face publication workflow builds the shards after all source ingestors and packages them under `data/browser/`. Raw upstream databases are never copied into the browser package.