"""Build a bounded, evidence-preserving acid-adaptation knowledge graph. Inputs must be local source tables. The script never queries an undocumented live API and never expands beyond configured hops/neighbour caps. Expected edge columns: source,target,edge_type,database,evidence_id,score. """ from __future__ import annotations import argparse,csv,pathlib def build(seed_file:pathlib.Path,edge_files:list[pathlib.Path],outdir:pathlib.Path,max_hops=1,max_neighbors=20,min_score=.7): seeds={r['gene'] for r in csv.DictReader(seed_file.open(encoding='utf-8')) if float(r.get('evidence_score',0))>=1} all_edges=[] for path in edge_files: with path.open(encoding='utf-8') as f: for r in csv.DictReader(f): if float(r.get('score',1))>=min_score: all_edges.append(r) selected=[];front=set(seeds);seen=set(seeds) for _ in range(max_hops): degree={x:0 for x in front};next_front=set() for r in sorted(all_edges,key=lambda x:float(x.get('score',1)),reverse=True): a,b=r['source'],r['target']; anchor=a if a in front else b if b in front else None if anchor and degree[anchor]