{
  "filename": "plot_7.png",
  "iteration": 3,
  "description": "Comparative gnomAD constraint across the four enzymes of histidine degradation",
  "timestamp": "2026-11-20 14:34:55",
  "code": "\nimport requests, pandas as pd, numpy as np, matplotlib.pyplot as plt\nH={\"Content-Type\":\"application/json\",\"User-Agent\":\"Mozilla/5.0\"}\nGENES={\"HAL\":\"histidine ammonia-lyase (step 1)\",\"UROC1\":\"urocanate hydratase (step 2)\",\n       \"AMDHD1\":\"imidazolonepropionase (step 3)\",\"FTCD\":\"formimidoyltransferase-cyclodeaminase (step 4)\"}\nLOF={\"frameshift_variant\",\"stop_gained\",\"splice_acceptor_variant\",\"splice_donor_variant\",\"start_lost\",\"stop_lost\"}\nQ=\"\"\"query V($g:String!){ gene(gene_symbol:$g, reference_genome:GRCh38){\n gnomad_constraint{ exp_lof obs_lof oe_lof oe_lof_upper pLI }\n variants(dataset: gnomad_r4){ consequence lof exome{ac an homozygote_count} genome{ac an homozygote_count} } } }\"\"\"\nres=[]\nfor g,desc in GENES.items():\n    d=requests.post(\"https://gnomad.broadinstitute.org/api\",json={\"query\":Q,\"variables\":{\"g\":g}},timeout=180,headers=H).json()\n    gd=d[\"data\"][\"gene\"]; c=gd[\"gnomad_constraint\"] or {}\n    rows=[]\n    for v in gd[\"variants\"]:\n        ac=an=hom=0\n        for s in (v.get(\"exome\"),v.get(\"genome\")):\n            if s: ac+=s.get(\"ac\") or 0; an=max(an,s.get(\"an\") or 0); hom+=s.get(\"homozygote_count\") or 0\n        rows.append((v[\"consequence\"],v.get(\"lof\"),ac,an,hom))\n    df=pd.DataFrame(rows,columns=[\"csq\",\"lof\",\"ac\",\"an\",\"hom\"]); df[\"af\"]=df.ac/df.an.replace(0,np.nan)\n    pl=df[df.csq.isin(LOF)]\n    res.append(dict(gene=g, step=desc, oe_lof=c.get(\"oe_lof\"), oe_up=c.get(\"oe_lof_upper\"), pLI=c.get(\"pLI\"),\n                    obs_lof=c.get(\"obs_lof\"), exp_lof=round(c.get(\"exp_lof\") or 0,1),\n                    n_plof=len(pl), q_plof=float(np.nansum(pl.af)), plof_hom=int(pl.hom.sum()),\n                    max_plof_af=float(np.nanmax(pl.af)) if len(pl) else np.nan))\nt=pd.DataFrame(res)\nt[\"carrier_1_in\"]=(1/(2*t.q_plof)).round(0)\nt[\"biallelic_1_in\"]=(1/t.q_plof**2).round(0)\npd.set_option(\"display.width\",220)\nprint(t[[\"gene\",\"step\",\"obs_lof\",\"exp_lof\",\"oe_lof\",\"oe_up\",\"pLI\",\"n_plof\",\"q_plof\",\"plof_hom\",\"carrier_1_in\",\"biallelic_1_in\"]].to_string(index=False))\n\nfig,axes=plt.subplots(1,3,figsize=(15,4.6))\nax=axes[0]\nax.bar(t.gene, t.oe_lof, yerr=[np.zeros(len(t)), t.oe_up-t.oe_lof], color=\"#5b6b7c\", capsize=4, width=0.55)\nax.axhline(1.0, color=\"#b4553a\", ls=\"--\", lw=1.4); ax.text(3.35,1.02,\"no constraint\",color=\"#b4553a\",fontsize=8,ha=\"right\")\nax.set_ylabel(\"observed / expected pLoF\"); ax.set_title(\"None of the four histidine-degradation\\ngenes is LoF constrained\")\nax=axes[1]\nax.bar(t.gene, t.plof_hom, color=\"#b4553a\", width=0.55)\nfor i,v in enumerate(t.plof_hom): ax.text(i,v+0.4,str(v),ha=\"center\")\nax.set_ylabel(\"pLoF homozygotes in gnomAD v4\"); ax.set_title(\"Biallelic loss-of-function individuals\\nare present for every step\")\nax=axes[2]\nax.bar(t.gene, 2*t.q_plof*100, color=\"#5b6b7c\", width=0.55)\nfor i,v in enumerate(2*t.q_plof*100): ax.text(i,v*1.02,f\"{v:.2f}%\",ha=\"center\",fontsize=9)\nax.set_ylabel(\"predicted pLoF carrier frequency (%)\"); ax.set_title(\"Carrier frequencies, gnomAD v4\")\nplt.tight_layout(); plt.show()\n",
  "plot_number": 7
}