{
  "filename": "plot_3.png",
  "iteration": 1,
  "description": "Independent test: are NMD-escaping RAD51D truncations tolerated in the general population (gnomAD) relative to ClinVar pathogenic calls?",
  "timestamp": "2026-11-19 03:29:54",
  "code": "\nimport requests, re, numpy as np, matplotlib.pyplot as plt\nfrom scipy import stats\nH={\"User-Agent\":\"openscientist/1.0 (research)\",\"Content-Type\":\"application/json\"}\nq=\"\"\"query { gene(gene_symbol:\"RAD51D\", reference_genome: GRCh38){\n variants(dataset: gnomad_r4){ variant_id hgvsc hgvsp consequence flags exome{ac an} genome{ac an} }}}\"\"\"\nvs=requests.post(\"https://gnomad.broadinstitute.org/api\",json={\"query\":q},headers=H,timeout=90).json()[\"data\"][\"gene\"][\"variants\"]\nCDS=984; last_junction=902; nmd_escape=last_junction-50\nLOF={\"frameshift_variant\",\"stop_gained\",\"splice_donor_variant\",\"splice_acceptor_variant\",\"start_lost\"}\n\nesc=[]; tgt=[]\nfor v in vs:\n    if v[\"consequence\"] not in LOF: continue\n    h=v.get(\"hgvsc\") or \"\"\n    m=re.search(r\"c\\.(\\d+)\",h)\n    if not m: continue\n    p=int(m.group(1))\n    if p>CDS: continue\n    e=v.get(\"exome\") or {}\n    ac=e.get(\"ac\") or 0; an=e.get(\"an\") or 0\n    if an<100000: continue\n    af=ac/an\n    (esc if p>=nmd_escape else tgt).append((h,p,ac,an,af))\n\nprint(f\"gnomAD v4 pLoF alleles in CDS: NMD-escape zone n={len(esc)}, NMD-target zone n={len(tgt)}\")\nac_e=sum(x[2] for x in esc); ac_t=sum(x[2] for x in tgt)\nan=max([x[3] for x in esc+tgt])\nfrac=(CDS-nmd_escape+1)/CDS\nprint(f\"escape-zone length fraction of CDS = {frac*100:.1f}%\")\nprint(f\"allele count: escape {ac_e}, target {ac_t}; escape share of alleles = {ac_e/(ac_e+ac_t)*100:.1f}%\")\nprint(f\"distinct-site share in escape zone = {len(esc)/(len(esc)+len(tgt))*100:.1f}%\")\nbt=stats.binomtest(len(esc),len(esc)+len(tgt),frac)\nprint(f\"binomial test, distinct pLoF sites in escape zone vs CDS length: p={bt.pvalue:.3f}\")\nmw=stats.mannwhitneyu([x[4] for x in esc],[x[4] for x in tgt],alternative='greater') if esc and tgt else None\nif mw: print(f\"Mann-Whitney, escape-zone AF > target-zone AF: U={mw.statistic:.0f}, p={mw.pvalue:.3f}\")\nprint(\"\\nEscape-zone pLoF alleles (population-tolerated candidates):\")\nfor h,p,ac,an_,af in sorted(esc,key=lambda x:-x[4])[:10]:\n    print(f\"  {h:20s} c.{p:4d}  AC={ac:4d}  AF={af:.2e}\")\n\n# ClinVar vs gnomAD comparison figure\nfig,ax=plt.subplots(figsize=(9,4.6))\ncats=[\"ClinVar P/LP\\ntruncating alleles\",\"gnomAD v4\\npLoF alleles\"]\nobs_share=[11/160*100, len(esc)/(len(esc)+len(tgt))*100]\nax.bar([0,1],obs_share,0.5,color=[\"#2b6cb0\",\"#2f855a\"])\nax.axhline(frac*100,ls=\"--\",c=\"k\",lw=1.2)\nax.text(1.35,frac*100+0.6,f\"expected from length ({frac*100:.1f}%)\",fontsize=9,ha=\"right\")\nfor i,v in enumerate(obs_share): ax.text(i,v+0.4,f\"{v:.1f}%\",ha=\"center\",fontsize=10)\nax.set_xticks([0,1]); ax.set_xticklabels(cats)\nax.set_ylabel(\"Share of alleles in the NMD-escape zone (%)\")\nax.set_title(\"Truncating RAD51D alleles that escape nonsense-mediated decay\\nare depleted among pathogenic calls but not among population pLoF variants\",fontsize=11)\nax.set_ylim(0,22); ax.spines[[\"top\",\"right\"]].set_visible(False)\nplt.tight_layout(); plt.show()\n",
  "plot_number": 3
}