{
  "filename": "plot_1.png",
  "iteration": 1,
  "description": "ABAT variant spectrum in ClinVar, gnomAD constraint, and ancestry-stratified pLoF allele frequency",
  "timestamp": "2026-11-28 20:22:00",
  "code": "\nimport requests, json, time, re\nimport pandas as pd, numpy as np\nimport matplotlib.pyplot as plt\nS=requests.Session(); S.headers.update({\"User-Agent\":\"research/1.0\",\"Content-Type\":\"application/json\"})\n\n# gnomAD v4 ABAT variants\nq=\"\"\"query { gene(gene_symbol:\"ABAT\", reference_genome:GRCh38){\n gnomad_constraint{oe_lof oe_lof_lower oe_lof_upper pLI mis_z lof_z}\n variants(dataset: gnomad_r4){ variant_id hgvsc hgvsp consequence lof\n   exome{ac an ac_hom populations{id ac an}} genome{ac an ac_hom} } } }\"\"\"\nd=S.post(\"https://gnomad.broadinstitute.org/api\",data=json.dumps({\"query\":q}),timeout=300).json()[\"data\"][\"gene\"]\ncon=d[\"gnomad_constraint\"]\nrows=[]\nPOPS={}\nfor v in d[\"variants\"]:\n    e=v.get(\"exome\") or {}; g=v.get(\"genome\") or {}\n    ac=(e.get(\"ac\") or 0)+(g.get(\"ac\") or 0); an=(e.get(\"an\") or 0)+(g.get(\"an\") or 0)\n    rows.append(dict(csq=v[\"consequence\"],lof=v.get(\"lof\"),ac=ac,an=an,af=ac/an if an else 0,\n                     hgvsp=v.get(\"hgvsp\"),vid=v[\"variant_id\"]))\n    if v.get(\"lof\")==\"HC\" and e.get(\"populations\"):\n        for p in e[\"populations\"]:\n            if \"_\" in p[\"id\"]: continue\n            a=POPS.setdefault(p[\"id\"],[0,0]); a[0]+=p[\"ac\"]; a[1]=max(a[1],p[\"an\"])\ndf=pd.DataFrame(rows); plof=df[df.lof==\"HC\"]\nqlof=plof.af.sum()\npop=pd.DataFrame([(k,v[0],v[1],v[0]/v[1]) for k,v in POPS.items() if v[1]>0],\n                 columns=[\"pop\",\"ac\",\"an\",\"af\"]).sort_values(\"af\",ascending=False)\n\n# ClinVar P/LP consequence spectrum (ABAT-specific, excluding multi-gene CNVs)\nbase=\"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi\"\ndef cnt(term):\n    time.sleep(0.5)\n    return int(S.get(base,params={\"db\":\"clinvar\",\"term\":term,\"retmax\":0,\"retmode\":\"json\"},timeout=90).json()[\"esearchresult\"][\"count\"])\nplp='ABAT[gene] AND (\"clinsig pathogenic\"[Properties] OR \"clinsig likely pathogenic\"[Properties])'\nmcs={\"missense\":\"missense variant\",\"nonsense\":\"nonsense\",\"frameshift\":\"frameshift variant\",\n     \"splice donor\":\"splice donor variant\",\"splice acceptor\":\"splice acceptor variant\"}\ncvc={k:cnt(f'{plp} AND \"{v}\"[molecular consequence]') for k,v in mcs.items()}\nprint(\"ClinVar P/LP consequences:\",cvc)\nprint(\"constraint:\",con, \"| qLoF=%.3e\"%qlof)\n\nfig,ax=plt.subplots(1,3,figsize=(15,4.4))\n# A: ClinVar P/LP spectrum\nk=list(cvc); vals=[cvc[x] for x in k]\nax[0].bar(k,vals,color=\"#4C78A8\")\nax[0].set_title(\"ABAT ClinVar pathogenic / likely pathogenic\\nvariants by molecular consequence\")\nax[0].set_ylabel(\"number of variants\"); ax[0].tick_params(axis=\"x\",rotation=30)\nfor i,v in enumerate(vals): ax[0].text(i,v+0.2,str(v),ha=\"center\")\nax[0].text(0.98,0.95,f\"truncating {sum(vals)-cvc['missense']}/{sum(vals)}\",transform=ax[0].transAxes,ha=\"right\",va=\"top\",fontsize=9)\n\n# B: constraint\nax[1].barh([\"pLI\",\"LOEUF (oe_lof upper)\",\"o/e LoF\",\"missense z\",\"LoF z\"],\n           [con[\"pLI\"],con[\"oe_lof_upper\"],con[\"oe_lof\"],con[\"mis_z\"],con[\"lof_z\"]],color=\"#F58518\")\nax[1].axvline(0.35,ls=\"--\",c=\"grey\"); ax[1].text(0.36,4.2,\"LOEUF 0.35\\nhaploinsufficiency cut-off\",fontsize=8,color=\"grey\")\nax[1].set_title(\"gnomAD v4 constraint for ABAT:\\nheterozygous LoF is tolerated\")\nax[1].set_xlabel(\"value\")\n\n# C: per-ancestry pLoF allele frequency\npp=pop.head(9)\nax[2].bar(pp[\"pop\"],pp[\"af\"]*1e4,color=\"#54A24B\")\nax[2].set_ylabel(\"pLoF allele frequency (per 10,000)\")\nax[2].set_title(\"Summed high-confidence pLoF allele frequency\\nby gnomAD v4 exome ancestry group\")\nax[2].tick_params(axis=\"x\",rotation=45)\nplt.tight_layout(); plt.show()\n\nprint(pop.to_string())\nprint(\"\\nCarrier freq from pLoF alone 2q = %.5f (1 in %.0f)\"%(2*qlof,1/(2*qlof)))\nprint(\"Birth prevalence from pLoF alone q^2 = 1 in %.0f\"%(1/qlof**2))\n",
  "plot_number": 1
}