{
  "filename": "plot_5.png",
  "iteration": 5,
  "description": "gnomAD v4 RAC2 pLoF allele spectrum, carrier frequency and Hardy-Weinberg projection of IMD73C birth prevalence under random mating and consanguinity",
  "timestamp": "2026-11-19 04:48:58",
  "code": "\nimport requests, json\nimport numpy as np, pandas as pd\nimport matplotlib.pyplot as plt\nS=requests.Session(); S.headers.update({\"User-Agent\":\"openscientist/1.0\",\"Content-Type\":\"application/json\"})\n\nq=\"\"\"{ gene(gene_symbol:\"RAC2\", reference_genome: GRCh38){\n  gnomad_constraint{ exp_lof obs_lof oe_lof oe_lof_lower oe_lof_upper pLI mis_z lof_z }\n  variants(dataset: gnomad_r4){ variant_id hgvsp consequence flags\n    genome{ ac an homozygote_count } exome{ ac an homozygote_count } } } }\"\"\"\nr=S.post(\"https://gnomad.broadinstitute.org/api\", json={\"query\":q}, timeout=120)\ng=r.json()[\"data\"][\"gene\"]; C=g[\"gnomad_constraint\"]\nprint(\"gnomAD v4 constraint:\", {k:(round(v,4) if isinstance(v,float) else v) for k,v in C.items()})\n\nLOF={\"stop_gained\",\"frameshift_variant\",\"splice_acceptor_variant\",\"splice_donor_variant\"}\nrows=[]\nfor v in g[\"variants\"]:\n    if v[\"consequence\"] in LOF:\n        ac=(v[\"exome\"][\"ac\"] if v[\"exome\"] else 0)+(v[\"genome\"][\"ac\"] if v[\"genome\"] else 0)\n        an=max((v[\"exome\"][\"an\"] if v[\"exome\"] else 0),(v[\"genome\"][\"an\"] if v[\"genome\"] else 0))\n        hom=(v[\"exome\"][\"homozygote_count\"] if v[\"exome\"] else 0)+(v[\"genome\"][\"homozygote_count\"] if v[\"genome\"] else 0)\n        if ac>0: rows.append({\"variant\":v[\"variant_id\"],\"hgvsp\":v[\"hgvsp\"],\"csq\":v[\"consequence\"],\"ac\":ac,\"an\":an,\"hom\":hom,\"af\":ac/an})\nL=pd.DataFrame(rows).sort_values(\"af\",ascending=False)\nprint(f\"\\npLoF variants observed in gnomAD v4: {len(L)}; total minor alleles {L.ac.sum()}; homozygotes {L.hom.sum()}\")\nprint(L.head(12).round(8).to_string(index=False))\nqLoF=L.ac.sum()/L.an.max()\nprint(f\"\\ncumulative pLoF allele frequency q = {qLoF:.3e}  (carrier frequency 2q = 1 in {1/(2*qLoF):,.0f})\")\n\n# Hardy-Weinberg birth prevalence, random mating and with inbreeding coefficient F\nfor F,lab in [(0,\"random mating\"),(0.0625,\"first-cousin union, F=1/16\"),(0.03125,\"second-cousin, F=1/32\")]:\n    p_hom=qLoF**2*(1-F)+qLoF*F\n    print(f\"  {lab:34s} expected biallelic pLoF birth prevalence = {p_hom:.3e}  (1 in {1/p_hom:,.0f})\")\np_rand=qLoF**2\nprint(f\"\\nRandom-mating projection corresponds to {p_rand*1e5:.2e} cases per 100,000 births, i.e. roughly {p_rand*1.3e8:.2f} affected births per year worldwide at 130 million annual births.\")\nprint(\"Note: gnomAD homozygote count for pLoF RAC2 alleles is\", int(L.hom.sum()), \"so no biallelic LoF individual is present in 807,162 gnomAD v4 exomes/genomes.\")\n\n# orthologues for sections 14-15\nmg=S.get(\"https://mygene.info/v3/query\", params={\"q\":\"symbol:RAC2\",\"species\":\"human,mouse,rat,zebrafish,fruitfly,nematode\",\"fields\":\"symbol,taxid,entrezgene,name\",\"size\":20}, timeout=60).json()\nprint(\"\\nRAC2 orthologue-name search:\")\nfor h in mg.get(\"hits\",[]): print(\" \", h.get(\"taxid\"), h.get(\"symbol\"), h.get(\"entrezgene\"), h.get(\"name\"))\n\nfig,axes=plt.subplots(1,2, figsize=(14.6,5.6))\nax=axes[0]\nlabels=[\"observed pLoF\\nalleles\",\"expected pLoF\\nalleles\"]\nax.bar(labels,[C[\"obs_lof\"],C[\"exp_lof\"]], color=[\"#C03221\",\"#9aa5b1\"], width=0.55)\nfor i,v in enumerate([C[\"obs_lof\"],C[\"exp_lof\"]]): ax.text(i,v+0.4,f\"{v:.1f}\", ha=\"center\", fontsize=10, weight=\"bold\")\nax.set_ylabel(\"allele count in gnomAD v4\", fontsize=9.5)\nax.set_title(f\"A  RAC2 tolerates almost no loss of function\\no/e = {C['oe_lof']:.3f} (90% CI {C['oe_lof_lower']:.2f}-{C['oe_lof_upper']:.2f}), pLI = {C['pLI']:.3f}, LOEUF = {C['oe_lof_upper']:.2f}\",\n             fontsize=10, loc=\"left\", weight=\"bold\")\nfor s in [\"top\",\"right\"]: ax.spines[s].set_visible(False)\n\nax=axes[1]\nF=np.linspace(0,0.0625,200)\nprev=qLoF**2*(1-F)+qLoF*F\nax.plot(F, prev, color=\"#C03221\", lw=2.2)\nfor f,lab in [(0,\"random\\nmating\"),(0.03125,\"second\\ncousins\"),(0.0625,\"first\\ncousins\")]:\n    v=qLoF**2*(1-f)+qLoF*f\n    ax.scatter([f],[v], s=55, color=\"#C03221\", zorder=3)\n    ax.annotate(f\"{lab}\\n1 in {1/v:,.0f}\", (f,v), xytext=(6,-6 if f==0 else 4), textcoords=\"offset points\", fontsize=8.2)\nax.set_yscale(\"log\"); ax.set_xlabel(\"inbreeding coefficient F\", fontsize=9.5)\nax.set_ylabel(\"expected birth prevalence of biallelic RAC2 pLoF\", fontsize=9.5)\nax.set_title(f\"B  Consanguinity raises expected IMD73C birth prevalence by {((qLoF**2*(1-0.0625)+qLoF*0.0625)/qLoF**2):,.0f}-fold\\ncumulative pLoF allele frequency q = {qLoF:.1e}, carrier frequency 1 in {1/(2*qLoF):,.0f}\",\n             fontsize=10, loc=\"left\", weight=\"bold\")\nfor s in [\"top\",\"right\"]: ax.spines[s].set_visible(False)\nplt.tight_layout(); plt.show()\n",
  "plot_number": 5
}