# alzh-toolkit data contract

Version 0.1, 27 Nov 2026. Each source module owns the tables listed under it and writes them to
`build/parts/<module>.sqlite` through `alzh.common.Part`. `alzh build` merges every part
into `build/alzmap.sqlite`. A module must not create tables owned by another module.

## Rules for every module

- **Open data only.** No login, no application, no data-use agreement. Never use controlled-access, private or lab data.
- **Licence first.** Register each source with `Part.source(...)`, giving its licence and one of:
  - `open`: CC0, public domain or CC BY. We republish values with attribution.
  - `share-alike`: CC BY-SA. We republish with attribution, in tables kept separate.
  - `link-only`: no licence or a restrictive one. Store only identifiers and a link, never values.
  Non-commercial (NC) or no-redistribution sources are `link-only`.
- **Every data table has a `source_id` column**; `Part.close()` refuses tables without one.
- **Fetch through `Part.get` / `Part.post`** so each response is cached and logged in `_fetches`
  with its SHA-256. Be polite: pause between calls when a service asks for it; retry on 429/5xx.
- **JSON lists** go in TEXT columns as compact JSON (`Part.insert` does this for list/dict values).
- **Module entry point:** `alzh fetch <module>` runs it end to end, idempotently.
  Add a `--limit N` flag for quick tests.
- **Size:** keep a part under ~500 MB. Prefer aggregated APIs over bulk downloads.

## Identifiers

| Thing | Key | Example |
|---|---|---|
| Gene | Ensembl gene ID, unversioned | `ENSG00000130203` |
| Variant | GRCh38 `CHR_POS_REF_ALT`, no `chr` prefix | `19_44908684_T_C` |
| rsID | dbSNP | `rs429358` |
| GWAS study | GWAS Catalog accession or Open Targets study ID | `GCST90027158`, `FINNGEN_R12_G6_AD_WIDE` |
| Drug | ChEMBL ID | `CHEMBL4594217` |
| Trial | NCT ID | `NCT04437511` |
| Cell type | Cell Ontology ID | `CL:0000129` |
| Disease | MONDO / EFO with underscore as in Open Targets | `MONDO_0004975` |
| Dates | ISO 8601 text | `2026-11-27` |

`build/gene_universe.tsv` (written first by `opentargets`) lists the genes in scope with columns
`gene_id, symbol, ot_score, tier`. Tier 1 = the top 2,000 by Open Targets score plus every gene with
genetic, drug or locus-to-gene evidence; tier 2 = all other associated genes.

## Tables by module

### opentargets (Open Targets Platform GraphQL, CC0)
- `genes(gene_id PK, symbol, name, biotype, chrom, start, end, strand, uniprot_ids JSON, function_text, source_id)`
- `gene_disease(gene_id, disease_id, score, genetic_association, somatic_mutation, known_drug, affected_pathway, rna_expression, literature, animal_model, source_id)`
- `gwas_studies(study_id PK, trait, disease_ids JSON, first_author, pub_date, pmid, journal, n_cases, n_controls, n_samples, ancestry JSON, has_sumstats, n_credible_sets, source_id)`
- `credible_sets(cs_id PK, study_id, lead_variant_id, chrom, pos, pvalue_mantissa, pvalue_exponent, beta, se, finemapping_method, confidence, n_variants, source_id)`
- `cs_variants(cs_id, variant_id, pip, source_id)`
- `variants(variant_id PK, rsids JSON, chrom, pos, ref, alt, consequence, source_id)`
- `l2g(cs_id, gene_id, score, source_id)`
- `coloc(cs_id, right_study_id, right_study_type, right_gene_id, biosample, project, h4, clpp, method, source_id)` — molecular-QTL colocalisations only

### trials (ClinicalTrials.gov API v2; Open Targets clinical candidates)
- `trials(trial_id PK, title, acronym, phase, status, why_stopped, stop_category, start_date, completion_date, enrollment, sponsor, sponsor_class, has_results, study_type, conditions JSON, url, source_id)`
- `trial_interventions(trial_id, name, type, drug_id, source_id)` — `drug_id` NULL when unmatched
- `drugs(drug_id PK, name, drug_type, max_phase_ad, mechanism_summary, source_id)`
- `drug_targets(drug_id, gene_id, action_type, mechanism, source_id)`
- `drug_indications(drug_id, disease_id, phase, status, trial_ids JSON, source_id)`

### cells (CZ CELLxGENE)
- `cell_expression(gene_id, tissue_id, tissue, cell_type_id, cell_type, disease_id, mean_expr, frac_expressing, n_cells, source_id)` — brain only; `disease_id` is `MONDO:0004975` (Alzheimer's) or `PATO:0000461` (normal)
- `cell_types(cell_type_id PK, label, n_cells_ad, n_cells_normal, source_id)`
- `sc_datasets(dataset_id PK, collection_id, collection, title, n_cells, n_donors, assays JSON, diseases JSON, tissues JSON, licence, url, source_id)`
- optional `cell_de(gene_id, cell_type_id, log_fc, effect_size, adj_p, n_ad, n_normal, method, source_id)`

### proteins (UniProt; PDB cross-references)
- `proteins(uniprot_id PK, gene_symbol, protein_name, length, function_text, subcellular JSON, pdb_ids JSON, n_pdb, reviewed, source_id)`

### gwas_catalog (NHGRI-EBI GWAS Catalog REST v2; CC0 unless the study says otherwise)
- `gc_studies(study_id PK, trait, efo_traits JSON, category, first_author, pub_date, pmid, journal, initial_sample, ancestry JSON, n_total, has_full_sumstats, sumstats_url, licence_note, source_id)` — `category` is `risk`, `proxy` (family history) or `endophenotype`
- `gc_associations(assoc_id PK, study_id, rsid, variant_id, chrom, pos, risk_allele, risk_allele_freq, pvalue, pvalue_mlog, or_beta, ci, mapped_genes JSON, reported_genes JSON, source_id)`

### literature (Europe PMC REST)
- `papers(pmid PK, title, journal, year, pub_date, first_author, doi, is_open_access, cited_by, source_id)`
- `gene_papers(gene_id, pmid, rank, source_id)`
- `gene_paper_counts(gene_id, n_ad_papers, n_ad_papers_5y, query, source_id)`

## Research evidence and public time

Each data row has a conservative `known_from` date. `date_basis` (or the source’s date-basis field) explains whether the date represents a published observation, source revision, or a toolkit observation. The public event index stores separate, narrower historical claims.

| Tables | What a row means |
|---|---|
| `evidence_events`, `research_resources` | A dated public event, or a curator-written source and its access conditions |
| `exome_burden`, `brain_eqtl` | A published burden mask per gene, or a cell/fine-feature lead eQTL in its source’s units |
| `csf_assays`, `csf_assay_targets` | One aptamer study accession, or an unambiguous reviewed protein-name match |
| `risk_window_stats`, `csf_protein_qtl`, `molecular_directions` | Measured harmonised GWAS rows, measured CSF rows, or an allele-matched risk and expression association |
| `encode_experiments`, `regulatory_overlaps` | Released Rush experiments, or a credible-set SNV inside a GRCh38 predicted enhancer linked to a gene |
| `trial_result_records`, `trial_result_measures` | One submitted result revision, or an outcome with original groups, units and time frame |
| `trial_result_values`, `trial_result_analyses` | One reported stratum–arm measurement, or a statistical comparison in an outcome |
| `trial_safety_groups`, `trial_safety_events` | Adverse-event arm denominators, or an event–arm record |
| `gene_constraint`, `drug_label_safety` | One selected gnomAD transcript per gene, or an identified US product label |
| `nih_awards`, `nih_gene_mentions` | One fiscal-year award application, or one explicit symbol matched in its title |

Missing, zero and unavailable counts have distinct meanings. A locus may contain multiple candidate genes. The workbench’s target-gap table uses the single highest-ranked one. A grant title match is a discovery aid. It does not allocate spending to a gene or establish what a project studied. Use schema inspection or `PRAGMA table_info(table_name)` for every column, type and key in the actual database.

`regional_colocalisation` contains one row per regional model setting with the five hypothesis probabilities, priors, source coverage, diagnostics and date. `atlas_collections` and `atlas_study_coverage` retain disease-scoped catalogue records and accession-match basis. `paper_tables` retains the original attributed table markup. `paper_table_rows` keeps headers, original cell positions, spans, inherited labels and footnotes through its parent table.

The versioned evidence release is available independently of SQLite below `research/data/validation/`. `case-index.json` lists its three questions, results, sources and downloads. Trial labels, registry screening, RNA library manifests and canonical counts retain their own units and denominators. [Input and result definitions](docs/evidence-releases.md)
