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).
251 lines
9.8 KiB
TypeScript
251 lines
9.8 KiB
TypeScript
// NATIVE graph LINK PREDICTION over the persisted evidence knowledge graph.
|
||
//
|
||
// Ported from PyKEEN (backend/engines/pykeen) — but NOT its trained-embedding models
|
||
// (TransE/DistMult/ComplEx need a torch training loop we deliberately don't run here).
|
||
// We port PyKEEN's OWN non-parametric BASELINE family (`models/baseline/*.py`): the
|
||
// idea that you can rank candidate links from GRAPH TOPOLOGY ALONE, no learned
|
||
// parameters, no GPU. PyKEEN's baselines build a normalized sparse adjacency and score
|
||
// candidates by shared structure; we implement the four classic topology link-
|
||
// predictors from the same lineage (used across the link-prediction literature
|
||
// PyKEEN's baselines draw on):
|
||
//
|
||
// common-neighbors (CN) : |Γ(u) ∩ Γ(v)|
|
||
// Adamic-Adar (AA) : Σ_{w ∈ Γ(u)∩Γ(v)} 1 / log|Γ(w)|
|
||
// resource-allocation (RA) : Σ_{w ∈ Γ(u)∩Γ(v)} 1 / |Γ(w)|
|
||
// preferential-attachment(PA): |Γ(u)| · |Γ(v)|
|
||
//
|
||
// where Γ(x) is the neighbor set of x. A high score on (u, v) means u and v are
|
||
// structurally close in the evidence graph WITHOUT a direct edge — i.e. a NOVEL
|
||
// repurposing / association HYPOTHESIS worth surfacing to a researcher.
|
||
//
|
||
// The graph is read ONLY through lib/kg/repository (neighbors), so this stays offline-
|
||
// testable against the same in-memory pool fake the rest of the KG uses. All math is
|
||
// pure, deterministic, and immutable — there is NO LLM anywhere in a score.
|
||
|
||
import { neighbors, type KgNeighbor, type KgPool } from "./repository";
|
||
import type { KgNode, KgPredicate } from "./schemas";
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Local undirected subgraph.
|
||
//
|
||
// The repository primitive `neighbors(nodeId)` returns OUTBOUND edges only. To score
|
||
// topology we need each node's neighbor SET; we treat every discovered edge as
|
||
// UNDIRECTED (both endpoints gain each other as a neighbor), which is the standard
|
||
// convention for these structural predictors. We BFS-expand from a seed set up to a
|
||
// bounded radius so scoring works over the neighborhood of interest without loading the
|
||
// whole table — mirroring PyKEEN baselines operating on a fixed adjacency, but built
|
||
// lazily and immutably from the repository.
|
||
// ---------------------------------------------------------------------------
|
||
|
||
export interface Subgraph {
|
||
// nodeId -> the KgNode (identity + Biolink-typeable metadata)
|
||
readonly nodes: ReadonlyMap<string, KgNode>;
|
||
// nodeId -> undirected neighbor ids (a Set, so membership + degree are O(1))
|
||
readonly adjacency: ReadonlyMap<string, ReadonlySet<string>>;
|
||
// "subjectId->objectId" of every DIRECTED edge seen, so we never propose a link that
|
||
// already exists, and can carry the predicate a proposed link would take.
|
||
readonly directedEdges: ReadonlySet<string>;
|
||
}
|
||
|
||
const DEFAULT_RADIUS = 2;
|
||
const MAX_RADIUS = 4;
|
||
const MAX_NODES = 5_000; // defence against a runaway expansion on a dense hub
|
||
|
||
function directedKey(subjectId: string, objectId: string): string {
|
||
return `${subjectId} |