papertrail/tests/pharmacovigilance.test.ts
caelum0x 174222bcaa feat: Builder-Track named-user tools, bio + evidence-intelligence platform, Apache-2.0
Builds PaperTrail from the base skeleton into a full evidence-verification platform
for the "Built with Claude: Life Sciences" hackathon (Builder Track).

New named-user features (Build Beyond the Bench):
- Lab Notebook Companion — rough bench notes -> structured, searchable, grounded
  experiment records (lib/labNotebook, /console/lab-notebook, migration 0059)
- Clinical Trial Matcher — de-identified patient notes -> ranked eligible
  ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning
  (lib/trialMatcher, /console/trial-matcher, migrations 0060)

Platform:
- Deterministic verification moat (biostats recompute, exact-span grounding)
- 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar,
  PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier)
- Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise
  governance (RBAC, /api/v1 gateway, metering, audit chain, retention)
- 17 OSS projects assimilated into native TypeScript on our stack (backend/engines
  retained + credited in NOTICE)
- Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site
- Next.js 16 upgrade, benchmark harness (honest smoke-test caveat)

Licensing: Apache-2.0 (LICENSE + NOTICE).
2026-07-10 16:23:13 +03:00

56 lines
2.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, it, expect } from "vitest";
import { disproportionality } from "../lib/bio/pharmacovigilance";
// Oracle test: locks the FAERS disproportionality statistics to hand-computed
// reference values for a fixed drug–event 2x2 (a=25, b=1000, c=200, d=50000).
// PRR = (a/(a+b)) / (c/(c+d)) = (25/1025)/(200/50200) ≈ 6.12
// ROR = (a*d)/(b*c) = (25*50000)/(1000*200) = 6.25 exactly
// IC = log2( a*n / ((a+b)(a+c)) ) = log2(25*51225 / (1025*225)) ≈ 2.47
// chi² (Pearson) = n(ad-bc)² / ((a+b)(c+d)(a+c)(b+d)) ≈ 95.6
// This is the same discipline as tests/biostatsOracle.test.ts — "we wrote the
// pharmacovigilance math ourselves" becomes "our numbers equal the standard ones."
describe("disproportionality — FAERS signal oracle", () => {
const r = disproportionality({ a: 25, b: 1000, c: 200, d: 50000 })!;
it("returns a result for a well-formed 2x2", () => {
expect(r).not.toBeNull();
expect(r.n).toBe(51225);
});
it("PRR ≈ 6.12", () => {
expect(r.prr).toBeCloseTo(6.12, 1);
expect(r.prrCiLower).toBeLessThan(r.prr);
expect(r.prrCiUpper).toBeGreaterThan(r.prr);
});
it("ROR = 6.25 exactly", () => {
expect(r.ror).toBeCloseTo(6.25, 2);
});
it("Information Component ≈ 2.47", () => {
expect(r.informationComponent).toBeCloseTo(2.47, 1);
expect(r.ic025).toBeLessThan(r.informationComponent);
});
it("Pearson chi-square ≈ 95.6, Yates slightly lower, both well above 4", () => {
expect(r.chiSquared).toBeCloseTo(95.6, 0);
expect(r.chiSquaredYates).toBeLessThan(r.chiSquared);
expect(r.chiSquaredYates).toBeGreaterThan(4);
});
it("fires a signal (PRR≥2 AND a≥3 AND Yates χ²≥4)", () => {
expect(r.signal).toBe(true);
});
it("a below-threshold table does NOT fire a signal", () => {
// Roughly proportional reporting → PRR near 1.
const weak = disproportionality({ a: 10, b: 1000, c: 100, d: 10000 })!;
expect(weak.prr).toBeCloseTo(1, 0);
expect(weak.signal).toBe(false);
});
it("returns null for a degenerate (empty) table", () => {
expect(disproportionality({ a: 0, b: 0, c: 0, d: 0 })).toBeNull();
});
});