{
  "filename": "plot_3.png",
  "iteration": 2,
  "description": "Merged HPO phenotype frequency table with Wilson confidence intervals for MSH3-related AFAP",
  "timestamp": "2026-11-26 19:53:35",
  "code": "\nimport requests, pandas as pd, numpy as np, matplotlib.pyplot as plt\nfrom statsmodels.stats.proportion import proportion_confint\n\nr=requests.get(\"https://www.ebi.ac.uk/ols4/api/ontologies/mondo/terms\",\n               params={\"iri\":\"http://purl.obolibrary.org/obo/MONDO_0044300\"},timeout=60)\nt=r.json()[\"_embedded\"][\"terms\"][0]\ntry:\n    par=requests.get(t[\"_links\"][\"parents\"][\"href\"],timeout=30).json()[\"_embedded\"][\"terms\"]\n    print(\"Parent terms:\", [(p[\"obo_id\"],p[\"label\"]) for p in par])\nexcept Exception as e: print(\"parents err:\",e)\n\npheno = pd.DataFrame([\n (\"HP:0005227\",\"Adenomatous colonic polyposis\",15,15,\"full census (6 families)\"),\n (\"HP:0003003\",\"Colon cancer\",4,15,\"full census\"),\n (\"HP:0004783\",\"Duodenal polyposis\",4,15,\"full census\"),\n (\"HP:0003002\",\"Breast carcinoma\",3,5,\"female patients only\"),\n (\"HP:0000131\",\"Uterine leiomyoma\",2,4,\"HPO / Adam cohort\"),\n (\"HP:6000102\",\"Breast intraductal papilloma\",2,3,\"HPO / Adam cohort\"),\n (\"HP:0000138\",\"Ovarian cyst\",1,4,\"HPO / Adam cohort\"),\n (\"HP:0000107\",\"Renal cyst\",2,15,\"HPO + Gavric\"),\n (\"HP:0001399\",\"Hepatic cysts\",4,15,\"Aelvoet + Gavric\"),\n (\"HP:0000854\",\"Thyroid adenoma\",1,4,\"HPO / Adam cohort\"),\n (\"HP:0033770\",\"Gastric adenocarcinoma\",1,15,\"full census\"),\n (\"HP:0009592\",\"Astrocytoma\",1,15,\"full census\"),\n (\"HP:0002671\",\"Basal cell carcinoma\",1,15,\"full census\"),\n], columns=[\"hpo\",\"label\",\"k\",\"n\",\"source\"])\npheno[\"freq\"]=pheno.k/pheno.n\npheno[[\"lo\",\"hi\"]]=pd.DataFrame([proportion_confint(k,n,method=\"wilson\") for k,n in zip(pheno.k,pheno.n)],index=pheno.index)\npheno[\"HPO_freq_class\"]=pd.cut(pheno.freq,[-.01,.04,.29,.79,.99,1.0],\n    labels=[\"Very rare HP:0040284\",\"Occasional HP:0040283\",\"Frequent HP:0040282\",\n            \"Very frequent HP:0040281\",\"Obligate HP:0040280\"])\nprint(\"\\nPhenotype frequency table:\")\nprint(pheno.sort_values(\"freq\",ascending=False).round(3).to_string(index=False))\n\np=pheno.sort_values(\"freq\")\nfig,ax=plt.subplots(figsize=(10,6))\nax.errorbar(p.freq, range(len(p)), xerr=[p.freq-p.lo, p.hi-p.freq], fmt=\"o\",\n            color=\"#4C72B0\", ecolor=\"#A0B4D4\", capsize=3)\nax.set_yticks(range(len(p))); ax.set_yticklabels([f\"{l}  ({h})\" for l,h in zip(p.label,p.hpo)], fontsize=9)\nax.set_xlabel(\"Frequency among reported biallelic MSH3 patients (Wilson 95% CI)\")\nax.set_title(\"Phenotype spectrum of MSH3-related attenuated FAP\\nColonic adenomatous polyposis is obligate, every other feature is uncommon with wide intervals\")\nax.set_xlim(0,1.05); ax.grid(axis=\"x\", alpha=.3)\nplt.tight_layout(); plt.show()\n",
  "plot_number": 3
}