A content-addressed (0x00 || sha256(wasm)) WebAssembly extension codec implementing the embedded clinical knowledge-table expansion paradigm for CODEC-ABI v0.1.
Instead of serializing tens of kilobytes of repetitive FHIR Genomics Reporting observations and CPIC drug-gene clinical decision support alerts into a dense QR code, pgx-cpic encodes only the patient's tested pharmacogenes and exact diplotypes (~53–57 bytes; ~88–92 bytes with full SHA-256 content-addressing and 2-byte integrity check, fitting inside a tiny QR Version 5-L symbol) and deterministically expands them inside a sandboxed, zero-import, zero-float, zero-panic #![no_std] Rust WASM module into a complete FHIR R4 Genomics Reporting Bundle with actionable CPIC DetectedIssue clinical drug-safety alerts (~23–33 KB — a 423×–578× expansion over the wire payload, and 19×–40× smaller than raw DEFLATE-9).
Module Properties (pgx-cpic.wasm)
- Size:
44,707bytes (includes the entire 14-gene CPIC diplotype/phenotype/drug-alert knowledge table, strict JSON encoder, bit-packed binary decoder, and FHIR R4 JSON renderer) - SHA-256:
8a0e73db854b448f7b4975d42c1917b1fa563c1055f232333b705349bdf36f3f - ABI Audit (
auditWasm):0violations,0imports (link-time zero-panic guarantee),0floats,0SIMD/threads, declared memory max16384pages (1 GiB) - Exports:
memory,shc_alloc,shc_decode,shc_encode - Safety:
#![no_std]and#![forbid(unsafe_code)]acrosspgx_cpic_core; only the 100-line ABI shim touches linear memory
Embedded 14-Gene CPIC Pharmacogenomics Table
| # | Gene | HGNC ID | Bit Width | Clinical Drugs Covered (RxNorm) |
|---|---|---|---|---|
| 0 | CYP2C19 | HGNC:2621 | 4 bits (15 + esc) | clopidogrel (32968), voriconazole (121243), citalopram (2556), escitalopram (321988), sertraline (36437), omeprazole (7646) |
| 1 | CYP2D6 | HGNC:2625 | 5 bits (31 + esc) | codeine (2670), tramadol (10689), tamoxifen (10324), ondansetron (26225), amitriptyline (704) |
| 2 | CYP2C9 | HGNC:2623 | 4 bits (15 + esc) | warfarin (11289), phenytoin (8183), celecoxib (140587), siponimod (2123111) |
| 3 | VKORC1 | HGNC:23663 | 2 bits (3 + esc) | warfarin (11289) — -1639G>A (G/G, G/A, A/A) |
| 4 | SLCO1B1 | HGNC:10959 | 4 bits (15 + esc) | simvastatin (36567), atorvastatin (83367), rosuvastatin (301542) |
| 5 | DPYD | HGNC:3012 | 4 bits (15 + esc) | fluorouracil (4492), capecitabine (194000) |
| 6 | TPMT | HGNC:12014 | 4 bits (15 + esc) | azathioprine (1256), mercaptopurine (103), thioguanine (10485) |
| 7 | NUDT15 | HGNC:23063 | 3 bits (7 + esc) | azathioprine (1256), mercaptopurine (103) |
| 8 | UGT1A1 | HGNC:12530 | 3 bits (7 + esc) | irinotecan (51499), atazanavir (343047) |
| 9 | CYP3A5 | HGNC:2638 | 3 bits (7 + esc) | tacrolimus (42316) (*3/*3 non-expresser baseline; *1/*1, *1/*3 expressers → 1.5–2× starting dose) |
| 10 | HLA-B*57:01 | HGNC:4932 | 2 bits (2 + esc) | abacavir (190521) — hypersensitivity reaction |
| 11 | HLA-B*58:01 | HGNC:4932 | 2 bits (2 + esc) | allopurinol (519) — SJS/TEN |
| 12 | HLA-B*15:02 | HGNC:4932 | 2 bits (2 + esc) | carbamazepine (2002), oxcarbazepine (32624) — SJS/TEN |
| 13 | G6PD | HGNC:4057 | 2 bits (3 + esc) | rasburicase (283810), primaquine (8686) — acute hemolytic anemia |
Any diplotype not in a gene's built-in table is encoded losslessly using that gene's escape code plus a length-prefixed UTF-8 string (decoded with phenotype Indeterminate and zero speculative drug alerts).
Canonical Wire Format (v0)
Every valid PgxPanel has a single canonical binary encoding:
- Header (1 byte): bits
7..5= format version (0; non-zero returnsUNSUPPORTED), bit4= reserved (0), bit3=has_lab, bit2=has_birth_date, bit1=has_given, bit0=has_family. testDate(2 bytes, u16 LE): days since1900-01-01(1900-01-01through2079-06-06).geneMask(2 bytes, u16 LE): 14-bit presence mask for genes0..13(non-zero; bits14..15must be0).- Bit-packed diplotype indices (LSB-first, zero-padded to byte boundary): for each set bit
iin0..13,GENES[i].bitsbits encoding either a built-in diplotype index orGENES[i].escape_code. Non-zero padding bits are rejected asMALFORMED. - Escape diplotype strings: in ascending gene index order,
len: u8 (1..=64)+ UTF-8 bytes (must not duplicate a built-in table entry for that gene). - Optional fields in flag order:
family(u8 len + UTF-8),given(u8 count (1..=8)×u8 len + UTF-8),birthDate(u16 LEdays since1900-01-01),lab.name(u8 len + UTF-8). Trailing bytes are rejected asMALFORMED.
FHIR R4 Output Structure
shc_decode emits a canonical minified FHIR R4 Bundle (type: "collection") with fullUrl values resource:0, resource:1, ...:
Patient(id: "0",fullUrl: "resource:0")- Optional performing laboratory
Organization(id: "1",fullUrl: "resource:1") DiagnosticReport(id: "pgx-report", LOINC51969-4Genetic analysis report,effectiveDateTime: testDate,result[]referencing every genotype & phenotypeObservation)- Per tested gene (in canonical gene order):
- Genotype / Diplotype
Observation(LOINC84413-4, HGNC gene studied component LOINC48018-6, lossless diplotype invalueCodeableConcept.text) - Predicted Phenotype
Observation(LOINC53040-2, CPIC metabolizer/function phenotype with LOINC Answer code + HL7 interpretation,derivedFromgenotype Observation)
- Genotype / Diplotype
- Deterministically expanded CPIC
DetectedIssuedrug-safety alerts (codeDRG,severityhigh/moderate/low, affected RxNorm medication code, gene + phenotype evidence references, CPIC guideline link, and concise clinical recommendation) — emitted only for actionable non-normal findings at 0 extra QR bytes.
Showcase Benchmarks
Measured with bun test codecs/pgx-cpic/pgx-cpic.test.ts:
| Sample | Genes | Alerts | FHIR JSON | Raw DEFLATE-9 | Wire Bytes | SHA-256 Card (0x00||sha256) | QR (EC-L) | Reg. Card (ID 1) | Reg. QR | Expansion vs Card / Wire |
|---|---|---|---|---|---|---|---|---|---|---|
high-risk-multi | 5 | 16 | 32,965 B | 3,692 B | 57 B | 92 B | V5-L | 60 B | V4-L | 358× / 578× |
cardio-warfarin | 4 | 11 | 23,712 B | 2,839 B | 56 B | 91 B | V5-L | 59 B | V4-L | 261× / 423× |
oncology-immuno | 5 | 10 | 24,358 B | 2,649 B | 53 B | 88 B | V5-L | 56 B | V4-L | 277× / 460× |
all-normal-14 | 14 | 0 | 31,148 B | 1,744 B | 55 B | 90 B | V5-L | 58 B | V4-L | 346× / 566× |
Building & Testing
# Build pgx-cpic.wasm (Rust stable with wasm32-unknown-unknown target)
bash codecs/pgx-cpic/rust/build.sh
# Run full audit, round-trip, clinical expansion, fuzz, and benchmark suite
bun test codecs/pgx-cpic/pgx-cpic.test.ts