{
  "filename": "plot_8.png",
  "iteration": 3,
  "description": "Pool ICL phenotype frequencies across three cohorts with exact binomial CIs and test between-cohort heterogeneity",
  "timestamp": "2026-11-17 23:00:24",
  "code": "\nimport numpy as np, pandas as pd\nfrom scipy import stats\nimport matplotlib.pyplot as plt\n\n# Phenotype counts extracted verbatim from three ICL cohorts.\n# NIH 2023  PMID 37133586  n=91 (after exclusion of genetic/acquired causes), 374 person-years\n# French    PMID 24646462  n=40\n# NIH 2008  PMID 18456875  n=39 enrolled, 36 followed (median 49.5 months)\nrec = [\n # phenotype, HPO, cohort, pmid, k, n\n (\"HPV-related disease\",            \"HP:0032152\", \"NIH 2023\",   37133586, 26, 91),\n (\"HPV-related disease\",            \"HP:0032152\", \"French 2014\",24646462, 12, 40),\n (\"Cryptococcosis\",                 \"HP:0032249\", \"NIH 2023\",   37133586, 22, 91),\n (\"Molluscum contagiosum\",          \"HP:0032252\", \"NIH 2023\",   37133586,  8, 91),\n (\"Nontuberculous mycobacterial disease\",\"HP:0032257\",\"NIH 2023\",37133586, 5, 91),\n (\"Any opportunistic infection\",    \"HP:0002719\", \"French 2014\",24646462, 25, 40),\n (\"Any opportunistic infection\",    \"HP:0002719\", \"NIH 2008\",   18456875, 15, 36),\n (\"Autoimmune manifestation\",       \"HP:0002960\", \"French 2014\",24646462, 14, 40),\n (\"Autoimmune manifestation\",       \"HP:0002960\", \"NIH 2008\",   18456875,  4, 36),\n (\"Malignancy\",                     \"HP:0002664\", \"French 2014\",24646462,  5, 40),\n (\"Asymptomatic or mild only\",      \"-\",          \"French 2014\",24646462,  8, 40),\n (\"Asymptomatic or mild only\",      \"-\",          \"NIH 2008\",   18456875,  7, 39),\n (\"Death (any cause, in follow-up)\",\"-\",          \"NIH 2008\",   18456875,  7, 39),\n (\"Death attributed to ICL\",        \"-\",          \"NIH 2008\",   18456875,  4, 39),\n]\ndf = pd.DataFrame(rec, columns=[\"phenotype\",\"hpo\",\"cohort\",\"pmid\",\"k\",\"n\"])\ndf[\"p\"] = df.k/df.n\nlo, hi = stats.beta.ppf(0.025, df.k, df.n-df.k+1), stats.beta.ppf(0.975, df.k+1, df.n-df.k)\ndf[\"lo\"], df[\"hi\"] = np.nan_to_num(lo, nan=0.0), np.nan_to_num(hi, nan=1.0)\n\n# Pool phenotypes measured in more than one cohort, and test between-cohort heterogeneity\npool = []\nfor ph, g in df.groupby(\"phenotype\", sort=False):\n    K, N = g.k.sum(), g.n.sum()\n    l = stats.beta.ppf(0.025, K, N-K+1); h = stats.beta.ppf(0.975, K+1, N-K)\n    if len(g) > 1:\n        tab = [[int(r.k), int(r.n-r.k)] for r in g.itertuples()]\n        chi2, pv, dof, _ = stats.chi2_contingency(np.array(tab))\n    else:\n        pv = np.nan\n    pool.append((ph, g.hpo.iloc[0], K, N, K/N, l, h, pv, len(g)))\nP = pd.DataFrame(pool, columns=[\"phenotype\",\"hpo\",\"k\",\"n\",\"p\",\"lo\",\"hi\",\"p_het\",\"n_cohorts\"])\nprint(P.to_string(index=False, float_format=lambda x: f\"{x:.3f}\"))\n\n# Mortality rate from the two cohorts that report follow-up time\npy_2023 = 374.0           # person-years, 91 patients (PMID 37133586)\npy_2008 = 36*49.5/12.0    # 36 patients followed a median 49.5 months (PMID 18456875)\nd_2008  = 7\nrate = d_2008/py_2008\nrl, rh = stats.chi2.ppf(0.025, 2*d_2008)/2/py_2008, stats.chi2.ppf(0.975, 2*(d_2008+1))/2/py_2008\nprint(f\"\\nNIH 2008 all-cause mortality: {d_2008} deaths / {py_2008:.0f} person-years \"\n      f\"= {rate*100:.1f} per 100 person-years (95% CI {rl*100:.1f}-{rh*100:.1f})\")\nprint(f\"ICL-attributable: 4/{py_2008:.0f} py = {4/py_2008*100:.1f} per 100 py\")\n\n# CD4 depth differs markedly between cohorts\nprint(\"\\nCD4 at diagnosis: NIH 2023 median 80/mm3 (n=91); French 2014 mean 127/mm3, range 4-294 (n=40)\")\n\nfig, axes = plt.subplots(1, 2, figsize=(15.5, 6.2))\n\n# Panel A: forest plot of pooled phenotype frequencies\nax = axes[0]\nQ = P.sort_values(\"p\")\ny = np.arange(len(Q))\ncols = [\"#6a51a3\" if c>1 else \"#9e9ac8\" for c in Q.n_cohorts]\nax.errorbar(Q.p, y, xerr=[Q.p-Q.lo, Q.hi-Q.p], fmt=\"o\", color=\"#4a1486\",\n            ecolor=\"#9e9ac8\", elinewidth=2.2, capsize=3, ms=7)\nfor i,r in enumerate(Q.itertuples()):\n    ax.text(1.02, i, f\"{r.k}/{r.n}\", va=\"center\", fontsize=8.5, transform=ax.get_yaxis_transform())\nax.set_yticks(y)\nax.set_yticklabels([f\"{r.phenotype}\" + (f\"  [{r.hpo}]\" if r.hpo!=\"-\" else \"\") for r in Q.itertuples()], fontsize=8.5)\nax.set_xlim(0, 1.0); ax.set_xlabel(\"frequency among ICL patients (exact 95% CI)\")\nax.set_title(\"A  Pooled phenotype frequencies across three ICL cohorts\\n(PMIDs 37133586, 24646462, 18456875)\", fontsize=10.5, loc=\"left\")\nax.grid(axis=\"x\", alpha=0.25)\n\n# Panel B: between-cohort heterogeneity for phenotypes measured more than once\nax = axes[1]\nH = df[df.phenotype.isin(P[P.n_cohorts>1].phenotype)].copy()\norder = [\"Any opportunistic infection\",\"Autoimmune manifestation\",\"Asymptomatic or mild only\",\"HPV-related disease\"]\nH[\"ord\"] = H.phenotype.map({p:i for i,p in enumerate(order)})\nH = H.sort_values([\"ord\",\"cohort\"])\ncmap = {\"NIH 2023\":\"#08519c\",\"French 2014\":\"#d94801\",\"NIH 2008\":\"#238b45\"}\nyp, labels, seen = [], [], 0\nfor i,r in enumerate(H.itertuples()):\n    ax.barh(i, r.p, color=cmap[r.cohort], height=0.62)\n    ax.errorbar(r.p, i, xerr=[[r.p-r.lo],[r.hi-r.p]], fmt=\"none\", ecolor=\"#444444\", capsize=3, elinewidth=1.3)\n    labels.append(f\"{r.phenotype}\\n{r.cohort} ({r.k}/{r.n})\")\nax.set_yticks(np.arange(len(H))); ax.set_yticklabels(labels, fontsize=7.6)\nax.set_xlim(0,1.0); ax.set_xlabel(\"frequency\")\nhet = P[P.n_cohorts>1].set_index(\"phenotype\").p_het\ntxt = \"  |  \".join(f\"{p.split()[0]} p_het = {het[p]:.3f}\" for p in order if p in het.index)\nax.set_title(\"B  Cohort-to-cohort heterogeneity in phenotype frequency\\n\" + txt, fontsize=9.5, loc=\"left\")\nax.grid(axis=\"x\", alpha=0.25)\nplt.tight_layout()\nplt.show()\n",
  "plot_number": 8
}