{
  "filename": "plot_8.png",
  "iteration": 5,
  "description": "Recompute gnomAD v4 PNLIP constraint and ancestry-stratified Hardy-Weinberg carrier frequency and birth prevalence",
  "timestamp": "2026-11-29 00:14:27",
  "code": "\nimport requests, pandas as pd, numpy as np, matplotlib.pyplot as plt\nGQL=\"https://gnomad.broadinstitute.org/api\"\nq=\"\"\"query($g:String!,$ds:DatasetId!){ gene(gene_symbol:$g, reference_genome:GRCh38){\n gene_id symbol\n gnomad_constraint{ exp_lof obs_lof oe_lof oe_lof_lower oe_lof_upper pLI obs_mis exp_mis oe_mis mis_z }\n variants(dataset:$ds){ variant_id consequence flags\n   exome{ ac an homozygote_count filters populations{id ac an} }\n   genome{ ac an homozygote_count filters populations{id ac an} } } } }\"\"\"\nr=requests.post(GQL,json={\"query\":q,\"variables\":{\"g\":\"PNLIP\",\"ds\":\"gnomad_r4\"}},\n                headers={\"Content-Type\":\"application/json\"},timeout=240).json()\ng=r[\"data\"][\"gene\"]; c=g[\"gnomad_constraint\"]\nprint(\"PNLIP gnomAD v4 constraint:\")\nprint(f\"  obs/exp LoF = {c['obs_lof']}/{c['exp_lof']:.1f} = {c['oe_lof']:.2f}  (90% CI {c['oe_lof_lower']:.2f}-{c['oe_lof_upper']:.2f}, LOEUF={c['oe_lof_upper']:.2f})\")\nprint(f\"  pLI = {c['pLI']:.3f}   missense Z = {c['mis_z']:.2f}  o/e mis = {c['oe_mis']:.2f}\")\n\nLOF={\"frameshift_variant\",\"stop_gained\",\"splice_acceptor_variant\",\"splice_donor_variant\"}\nANC={\"afr\":\"African/African American\",\"amr\":\"Admixed American\",\"asj\":\"Ashkenazi Jewish\",\"eas\":\"East Asian\",\n     \"fin\":\"Finnish\",\"nfe\":\"Non-Finnish European\",\"sas\":\"South Asian\",\"mid\":\"Middle Eastern\",\"remaining\":\"Remaining\"}\ntot_ac=tot_an=0; pop=dict()\nnlof=0\nfor v in g[\"variants\"]:\n    if v[\"consequence\"] not in LOF: continue\n    if v.get(\"flags\") and \"lc_lof\" in v[\"flags\"]: continue\n    src=v.get(\"exome\") or v.get(\"genome\")\n    if not src or (src.get(\"filters\") or []): continue\n    nlof+=1; tot_ac+=src[\"ac\"]; tot_an=max(tot_an,src[\"an\"])\n    for p in src.get(\"populations\") or []:\n        pid=p[\"id\"].split(\"_\")[0].lower()\n        if pid in ANC:\n            a,b=pop.get(pid,(0,0)); pop[pid]=(a+p[\"ac\"], max(b,p[\"an\"]))\nq_all=tot_ac/tot_an\nprint(f\"\\nPASS high-confidence pLoF variants: {nlof}; cumulative AC={tot_ac}, AN={tot_an}\")\nprint(f\"Global pLoF allele frequency q = {q_all:.6g}\")\nprint(f\"  carrier frequency 2q(1-q) = {2*q_all*(1-q_all):.6g}  = 1 in {1/(2*q_all*(1-q_all)):,.0f}\")\nprint(f\"  predicted birth prevalence q^2 = {q_all**2:.3g} = 1 in {1/q_all**2:,.0f} = {q_all**2*1e5:.4f} per 100,000\")\nrows=[]\nfor k,(ac,an) in pop.items():\n    if an<1000: continue\n    qq=ac/an\n    rows.append(dict(ancestry=ANC[k], AC=ac, AN=an, q=qq, carrier_1_in=1/(2*qq*(1-qq)) if qq>0 else np.inf,\n                     prev_1_in=1/qq**2 if qq>0 else np.inf, per_100k=qq**2*1e5))\nep=pd.DataFrame(rows).sort_values(\"q\",ascending=False)\nprint(\"\\nAncestry-stratified predicted epidemiology (Hardy-Weinberg, pLoF only, a floor on true prevalence):\")\nprint(ep.to_string(index=False,float_format=lambda x:f\"{x:,.4g}\"))\n\nfig,ax=plt.subplots(figsize=(9.5,4.6))\ne2=ep.sort_values(\"per_100k\")\nax.barh(e2.ancestry, e2.per_100k, color=\"#4C72B0\")\nax.axvline(q_all**2*1e5, color=\"#C44E52\", ls=\"--\", lw=1.6, label=f\"global {q_all**2*1e5:.4f} per 100,000\")\nax.set_xlabel(\"predicted birth prevalence per 100,000 (q$^2$, pLoF alleles only)\")\nax.set_title(\"Predicted birth prevalence of biallelic PNLIP loss of function by ancestry\\ngnomAD v4, Hardy-Weinberg; a lower bound because missense secretion-null alleles are excluded\")\nfor i,(a,v) in enumerate(zip(e2.ancestry,e2.per_100k)): ax.text(v*1.03,i,f\"{v:.4f}\",va=\"center\",fontsize=8)\nax.legend(fontsize=8); plt.tight_layout(); plt.show()\n",
  "plot_number": 8
}