{
  "filename": "plot_3.png",
  "iteration": 3,
  "description": "GTEx v8: RAC2 haematopoietic restriction versus ubiquitous RAC1, with tau specificity index and bootstrap CI",
  "timestamp": "2026-11-19 04:38:34",
  "code": "\nimport requests, json\nimport pandas as pd, numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.stats import mannwhitneyu\nS=requests.Session(); S.headers.update({\"User-Agent\":\"openscientist/1.0\",\"Accept\":\"application/json\"})\n\nids={\"RAC1\":\"ENSG00000136238.17\",\"RAC2\":\"ENSG00000128340.14\",\"RAC3\":\"ENSG00000169750.8\"}\nframes={}\nfor sym,gid in ids.items():\n    r=S.get(\"https://gtexportal.org/api/v2/expression/medianGeneExpression\",\n            params={\"gencodeId\":gid,\"datasetId\":\"gtex_v8\",\"itemsPerPage\":100}, timeout=90)\n    dd=r.json()[\"data\"]\n    frames[sym]=pd.Series({x[\"tissueSiteDetailId\"]: x[\"median\"] for x in dd})\n    print(f\"{sym}: {len(dd)} tissues\")\nG=pd.DataFrame(frames).dropna()\n\ndef tau(x):\n    x=np.asarray(x,float); return ((1-x/x.max()).sum())/(len(x)-1)\n\nprint(\"\\nTop 10 tissues by RAC2 median TPM (GTEx v8):\")\nprint(G.sort_values(\"RAC2\",ascending=False).head(10).round(2).to_string())\nprint(\"\\nBottom 6 tissues:\")\nprint(G.sort_values(\"RAC2\").head(6).round(2).to_string())\nprint(f\"\\nRAC2 over {len(G)} tissues: max {G.RAC2.max():.1f} TPM ({G.RAC2.idxmax()}), median {G.RAC2.median():.1f}, min {G.RAC2.min():.2f} ({G.RAC2.idxmin()}), max/min {G.RAC2.max()/G.RAC2.min():.0f}x\")\nprint(f\"RAC1 over {len(G)} tissues: max {G.RAC1.max():.1f} ({G.RAC1.idxmax()}), median {G.RAC1.median():.1f}, min {G.RAC1.min():.1f} ({G.RAC1.idxmin()}), max/min {G.RAC1.max()/G.RAC1.min():.1f}x\")\nfor s in ids: print(f\"tau {s} = {tau(G[s]):.3f}\")\n\nrng=np.random.default_rng(0)\nd_obs=tau(G.RAC2)-tau(G.RAC1)\nboot=[tau(G.RAC2.values[i])-tau(G.RAC1.values[i]) for i in (rng.integers(0,len(G),(4000,len(G))))]\nlo,hi=np.percentile(boot,[2.5,97.5])\nprint(f\"\\ntau(RAC2)-tau(RAC1) = {d_obs:.3f}, bootstrap 95% CI [{lo:.3f}, {hi:.3f}]\")\n\nblood=[t for t in G.index if any(k in t.lower() for k in [\"whole_blood\",\"spleen\",\"lymphocyte\",\"ebv\"])]\nnonb=G.drop(index=blood)\nprint(\"\\nhaematopoietic/lymphoid tissues:\", blood)\nprint(G.loc[blood].round(1).to_string())\nu,p=mannwhitneyu(G.loc[blood,\"RAC2\"], nonb[\"RAC2\"], alternative=\"greater\")\nu1,p1=mannwhitneyu(G.loc[blood,\"RAC1\"], nonb[\"RAC1\"], alternative=\"greater\")\nprint(f\"\\nRAC2: blood/lymphoid median {G.loc[blood,'RAC2'].median():.1f} vs other {nonb['RAC2'].median():.1f} TPM; U={u:.0f}, p={p:.2e}; ratio {G.loc[blood,'RAC2'].median()/nonb['RAC2'].median():.1f}x\")\nprint(f\"RAC1: blood/lymphoid median {G.loc[blood,'RAC1'].median():.1f} vs other {nonb['RAC1'].median():.1f} TPM; U={u1:.0f}, p={p1:.3f}; ratio {G.loc[blood,'RAC1'].median()/nonb['RAC1'].median():.2f}x\")\n\nfig,axes=plt.subplots(1,2, figsize=(15.2,6.6), gridspec_kw={\"width_ratios\":[1.4,1]})\nax=axes[0]\ntop=G.sort_values(\"RAC2\",ascending=False).head(20)[::-1]\nyp=np.arange(len(top))\nax.barh(yp+0.2, top.RAC2, height=0.4, color=\"#C03221\", label=\"RAC2\")\nax.barh(yp-0.2, top.RAC1, height=0.4, color=\"#4C78A8\", label=\"RAC1\")\nax.set_yticks(yp); ax.set_yticklabels([t.replace(\"_\",\" \")[:33] for t in top.index], fontsize=8)\nfor i,t in enumerate(top.index):\n    if t in blood: ax.get_yticklabels()[i].set_color(\"#C03221\"); ax.get_yticklabels()[i].set_weight(\"bold\")\nax.set_xlabel(\"GTEx v8 median expression (TPM)\", fontsize=9.5); ax.legend(fontsize=9, frameon=False, loc=\"lower right\")\nax.set_title(f\"A  RAC2 expression is haematopoietic-restricted; RAC1 is ubiquitous\\nRAC2 {G.loc[blood,'RAC2'].median():.0f} TPM in blood/lymphoid vs {nonb['RAC2'].median():.1f} TPM elsewhere (Mann-Whitney p = {p:.1e})\",\n             fontsize=9.8, loc=\"left\", weight=\"bold\")\nfor s in [\"top\",\"right\"]: ax.spines[s].set_visible(False)\n\nax=axes[1]\nax.scatter(G.RAC1, G.RAC2, s=26, c=\"#aaa\", alpha=0.7, label=\"other tissues\")\nax.scatter(G.loc[blood,\"RAC1\"], G.loc[blood,\"RAC2\"], s=95, c=\"#C03221\", zorder=3, label=\"blood / lymphoid\")\nfor t in blood: ax.annotate(t.replace(\"_\",\" \")[:20], (G.loc[t,\"RAC1\"],G.loc[t,\"RAC2\"]), fontsize=7.5, xytext=(6,3), textcoords=\"offset points\", color=\"#C03221\")\nmx=G.RAC1.max()*1.05; ax.plot([0,mx],[0,mx], ls=\"--\", color=\"#ccc\", lw=1)\nax.text(mx*0.62, mx*0.68, \"RAC2 = RAC1\", fontsize=7.5, color=\"#aaa\", rotation=40)\nax.set_xlabel(\"RAC1 median TPM\", fontsize=9.5); ax.set_ylabel(\"RAC2 median TPM\", fontsize=9.5)\nax.set_title(f\"B  Tissue-specificity index tau over {len(G)} GTEx tissues\\nRAC2 = {tau(G.RAC2):.2f}, RAC1 = {tau(G.RAC1):.2f}\\ndifference {d_obs:.2f}, bootstrap 95% CI [{lo:.2f}, {hi:.2f}]\",\n             fontsize=9.8, loc=\"left\", weight=\"bold\")\nax.legend(fontsize=8.5, frameon=False, loc=\"upper left\")\nfor s in [\"top\",\"right\"]: ax.spines[s].set_visible(False)\nplt.tight_layout(); plt.show()\n",
  "plot_number": 3
}