papertrail/lib/bio/variant.schemas.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

116 lines
5.0 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 { z } from "zod";
// Zod schemas for the VARIANT PATHOGENICITY verification feature. Two roles:
// 1. Validate the PUBLIC request body at the API boundary (never trust raw JSON).
// 2. Give the deterministic verdict engine typed, normalized records so the numeric
// logic never has to defend against a malformed ClinVar esummary payload.
//
// No LLM is involved anywhere in this module — these are ordinary structural schemas.
// Data source: NCBI ClinVar (public domain — https://www.ncbi.nlm.nih.gov/clinvar/).
// --- Public request ------------------------------------------------------------
// A pathogenicity query is keyed by AT LEAST ONE variant identifier: an rsID, an
// HGVS expression, or a gene symbol (optionally narrowed by condition). We enforce
// "at least one identifier" with a refine so an empty probe can't fan out into an
// unbounded catalog scan.
export const VariantPathogenicityRequestSchema = z
.object({
rsId: z.string().trim().min(1).max(64).optional(),
hgvs: z.string().trim().min(1).max(256).optional(),
gene: z.string().trim().min(1).max(64).optional(),
condition: z.string().trim().min(1).max(200).optional(),
// The significance a claim asserts (e.g. a paper stating a variant is
// "pathogenic"), verified against what ClinVar actually reports. Optional:
// absent it, the engine reports the ClinVar consensus without a claim to test.
claimedSignificance: z.string().trim().min(1).max(64).optional(),
})
.refine((v) => Boolean(v.rsId) || Boolean(v.hgvs) || Boolean(v.gene), {
message: "Provide at least one of `rsId`, `hgvs`, or `gene`.",
path: ["rsId"],
});
export type VariantPathogenicityRequest = z.infer<
typeof VariantPathogenicityRequestSchema
>;
// --- Field-standard classification vocabulary ---------------------------------
// The ACMG/ClinVar germline classification tiers we normalize onto. "Conflicting"
// is ClinVar's own outcome when submitters disagree ("Conflicting classifications
// of pathogenicity"); "VUS" is the variant of uncertain significance bucket. Any
// significance string we can't confidently map stays null (honest unknown), never
// forced into a tier.
export const CLINICAL_SIGNIFICANCES = [
"Pathogenic",
"Likely pathogenic",
"VUS",
"Likely benign",
"Benign",
"Conflicting",
] as const;
export type ClinicalSignificance = (typeof CLINICAL_SIGNIFICANCES)[number];
export const ClinicalSignificanceSchema = z.enum(CLINICAL_SIGNIFICANCES);
// --- Normalized upstream record (what the fetcher returns) ---------------------
// A single ClinVar record, normalized from an esummary result. `starRating` is the
// 0–4 ClinVar review-confidence star mapped deterministically from `reviewStatus`
// (see STAR_BY_REVIEW_STATUS). `significance` is the normalized tier, or null when
// the raw string didn't map to a known tier.
export const ClinVarVariantRecordSchema = z.object({
variant: z.string().nullable(),
clinicalSignificance: ClinicalSignificanceSchema.nullable(),
rawSignificance: z.string().nullable(),
condition: z.string().nullable(),
reviewStatus: z.string().nullable(),
starRating: z.number().int().min(0).max(4),
});
export type ClinVarVariantRecord = z.infer<typeof ClinVarVariantRecordSchema>;
// --- Verdict -------------------------------------------------------------------
// The deterministic verdict vocabulary for a pathogenicity CLAIM check.
// confirmed — ClinVar supports the claimed significance at a
// confident (>=1 star) review level.
// overstated_certainty — claim asserts (Likely) Pathogenic but the strongest
// ClinVar record is VUS/benign, or is pathogenic only at
// 0 stars (no assertion criteria). The certainty of the
// claim exceeds what ClinVar supports.
// conflicting — ClinVar itself reports conflicting classifications.
// not_found — no ClinVar record matched (honest empty result).
export const PATHOGENICITY_VERDICTS = [
"confirmed",
"overstated_certainty",
"conflicting",
"not_found",
] as const;
export type PathogenicityVerdict = (typeof PATHOGENICITY_VERDICTS)[number];
export const PathogenicityVerdictSchema = z.enum(PATHOGENICITY_VERDICTS);
// The full result the engine returns and the route serializes. `records` holds ONLY
// records ClinVar actually returned (never fabricated); `bestRecord` is the highest
// -star supporting record that drove the verdict, for auditability.
export const PathogenicityVerificationSchema = z.object({
verdict: PathogenicityVerdictSchema,
query: z.object({
rsId: z.string().nullable(),
hgvs: z.string().nullable(),
gene: z.string().nullable(),
condition: z.string().nullable(),
claimedSignificance: z.string().nullable(),
}),
bestRecord: ClinVarVariantRecordSchema.nullable(),
records: z.array(ClinVarVariantRecordSchema),
rationale: z.string(),
});
export type PathogenicityVerification = z.infer<
typeof PathogenicityVerificationSchema
>;