{ "cells": [ { "cell_type": "markdown", "id": "2f333c50", "metadata": {}, "source": [ "# FBN1 Cohort Analysis: Correlation\n", "\n", "In this tutorial, we analyze a cohort of phenopackets associated with the gene **FBN1**. The example data is retrieved from the [phenopacket store](https://github.com/monarch-initiative/phenopacket-store).\n", "\n", "Using this dataset, we demonstrate a complete workflow with **phenocor**, including:\n", "\n", "- dataset construction\n", "- correlation analysis\n", "\n", "We recommend running this tutorial in a **Jupyter notebook** for interactive exploration and visualization, although it can also be executed as a standard Python script.\n", "\n", "\n", "## 1. Load phenopackets\n", "\n", "We start by loading phenopackets for the **FBN1** cohort:" ] }, { "cell_type": "markdown", "id": "b806eb63", "metadata": {}, "source": [ "### Requirements\n", "\n", "Install phenocor and the toolkit used to retrieve the example data:\n", "\n", "```bash\n", "pip install phenocor phenopacket-store-toolkit\n", "```\n", "\n", "The first data download requires an internet connection." ] }, { "cell_type": "code", "execution_count": 13, "id": "deec03c5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded 143 phenopackets\n" ] } ], "source": [ "from ppktstore.registry import configure_phenopacket_registry\n", "registry = configure_phenopacket_registry()\n", "with registry.open_phenopacket_store() as ps:\n", " phenopackets = [\n", " p for p in ps.iter_cohort_phenopackets(\"FBN1\")\n", " ]\n", "\n", "print(f\"Loaded {len(phenopackets)} phenopackets\")" ] }, { "cell_type": "markdown", "id": "3a47822d", "metadata": {}, "source": [ "The registry provides access to Phenopacket Store cohorts through iter_cohort_phenopackets().\n", "\n", "> **Note:** \n", "> You can also use your own phenopacket data. As long as your data follows the [GA4GH Phenopacket schema](https://phenopacket-schema.readthedocs.io/en/latest/index.html), it can be directly used in the workflow.\n", "\n", "## 2. Build the dataset\n", "\n", "Next, we convert phenopackets into a structured dataset suitable for downstream statistical analysis. " ] }, { "cell_type": "code", "execution_count": 14, "id": "01d4d77a", "metadata": {}, "outputs": [], "source": [ "from phenocor import PhenotypeDatasetBuilder\n", "\n", "dataset = PhenotypeDatasetBuilder(phenopackets).build()" ] }, { "cell_type": "markdown", "id": "f5029d84", "metadata": {}, "source": [ "The resulting `dataset` is now ready for correlation.\n", "\n", "\n", "## 3. Correlation analysis\n", "\n", "Next, we compute pairwise correlations between HPO features across individuals in the cohort." ] }, { "cell_type": "code", "execution_count": 15, "id": "69656de4", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Calculating pairwise correlations: 100%|██████████| 728/728 [00:01<00:00, 486.47it/s]\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
HPO_AHPO_Bcorrelationp_valueadj_p_valuen(A:E/B:E)n(A:E/B:O)n(A:O/B:E)n(A:O/B:O)n_individualsHPO_A_labelHPO_B_labeln_pmidspmids
0HP:0004322HP:0004942-0.8271312.049015e-149.773799e-1273433074Short statureAortic aneurysm510756346;11175294;12203992;20375004;21683322
1HP:0000098HP:00049420.7966371.940118e-134.627183e-1145712679Tall statureAortic aneurysm1310756346;11175294;12203992;20375004;20979188;2...
2HP:0000098HP:0004322-0.6856815.424742e-128.625340e-10163337086Tall statureShort stature510756346;11175294;12203992;20375004;21683322
3HP:0000098HP:00026160.6469031.766457e-112.106500e-097071026113Tall statureAortic root aneurysm1810756346;11175294;12203992;20375004;20979188;2...
4HP:0002616HP:0004322-0.6346152.474657e-102.360823e-08193333085Aortic root aneurysmShort stature510756346;11175294;12203992;20375004;21683322
\n", "
" ], "text/plain": [ " HPO_A HPO_B correlation p_value adj_p_value \\\n", "0 HP:0004322 HP:0004942 -0.827131 2.049015e-14 9.773799e-12 \n", "1 HP:0000098 HP:0004942 0.796637 1.940118e-13 4.627183e-11 \n", "2 HP:0000098 HP:0004322 -0.685681 5.424742e-12 8.625340e-10 \n", "3 HP:0000098 HP:0002616 0.646903 1.766457e-11 2.106500e-09 \n", "4 HP:0002616 HP:0004322 -0.634615 2.474657e-10 2.360823e-08 \n", "\n", " n(A:E/B:E) n(A:E/B:O) n(A:O/B:E) n(A:O/B:O) n_individuals \\\n", "0 7 34 33 0 74 \n", "1 45 7 1 26 79 \n", "2 16 33 37 0 86 \n", "3 70 7 10 26 113 \n", "4 19 33 33 0 85 \n", "\n", " HPO_A_label HPO_B_label n_pmids \\\n", "0 Short stature Aortic aneurysm 5 \n", "1 Tall stature Aortic aneurysm 13 \n", "2 Tall stature Short stature 5 \n", "3 Tall stature Aortic root aneurysm 18 \n", "4 Aortic root aneurysm Short stature 5 \n", "\n", " pmids \n", "0 10756346;11175294;12203992;20375004;21683322 \n", "1 10756346;11175294;12203992;20375004;20979188;2... \n", "2 10756346;11175294;12203992;20375004;21683322 \n", "3 10756346;11175294;12203992;20375004;20979188;2... \n", "4 10756346;11175294;12203992;20375004;21683322 " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from phenocor import HPOCorrelationAnalyzer\n", "\n", "analyzer = HPOCorrelationAnalyzer(dataset=dataset)\n", "\n", "results = analyzer.compute_correlation_matrix()\n", "\n", "results.correlation_results.head()" ] }, { "cell_type": "markdown", "id": "7d9459e6", "metadata": {}, "source": [ "This step identifies positive and negative associations between HPO terms.\n", "\n", "In the result table:\n", "\n", "- `HPO_A` and `HPO_B` are the two HPO terms being compared \n", "- `correlation` indicates the strength and direction of association \n", "- `adj_p_value` is the Benjamini–Hochberg-adjusted p-value. \n", "\n", "Interpretation:\n", "\n", "- **positive correlation** → the two phenotypes tend to appear together \n", "- **negative correlation** → the phenotypes tend to occur in different individuals \n", "- **values near zero** → little or no association \n", "\n", "Detailed descriptions of parameters and output columns are provided in the **Usage** section.\n", "\n", "## 4. Visualize correlation results\n", "\n", "To better interpret these relationships, we visualize them as a heatmap." ] }, { "cell_type": "code", "execution_count": 16, "id": "935e51e3", "metadata": { "nbsphinx": { "hide-input": true }, "tags": [ "hide_input" ] }, "outputs": [], "source": [ "import os\n", "from IPython.display import HTML\n", "\n", "STATIC_DIR = \"_static\" \n", "os.makedirs(STATIC_DIR, exist_ok=True)\n", "\n", "def plotly_html_link(fig, filename, link_text=\"View interactive heatmap\"):\n", " \"\"\"\n", " Generate Plotly HTML file and return an RTD-friendly link.\n", " \"\"\"\n", " filepath = os.path.join(STATIC_DIR, filename)\n", " fig.write_html(filepath, include_plotlyjs=\"cdn\", full_html=True)\n", " return HTML(f'

To view the interactive plot, click the link below:

'\n", " f'{link_text}')" ] }, { "cell_type": "code", "execution_count": 17, "id": "66c2e84d", "metadata": {}, "outputs": [], "source": [ "fig1 = results.plot_correlation_heatmap_with_significance(\n", " title=\"Cohort FBN1\",\n", ")" ] }, { "cell_type": "code", "execution_count": 18, "id": "11a16cf9", "metadata": { "nbsphinx": { "hide-input": true }, "tags": [ "hide_input" ] }, "outputs": [ { "data": { "text/html": [ "

To view the interactive plot, click the link below:

Click here to view interactive correlation heatmap" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotly_html_link(fig1, \"correlation_heatmap.html\", link_text=\"Click here to view interactive correlation heatmap\")" ] }, { "cell_type": "markdown", "id": "5954dd69", "metadata": {}, "source": [ "This visualization highlights the strongest and most statistically significant associations between phenotypic features.\n", "\n", "- Strong positive correlations indicate phenotypes that frequently co-occur\n", "- Strong negative correlations indicate phenotypes that tend to occur in different individuals\n", "\n", "> **Note:** \n", "> Hover over the heatmap to see detailed information for each phenotype pair. \n", "> Increasing the adjusted p-value threshold includes more pairs. Increasing the minimum absolute Phi coefficient retains stronger associations.. " ] }, { "cell_type": "markdown", "id": "95a50303", "metadata": {}, "source": [ "## Summary\n", "\n", "In this tutorial, we:\n", "\n", "- Loaded phenopacket data \n", "- Constructed a structured dataset \n", "- Explored phenotype–phenotype relationships using correlation \n", "\n", "Together, these steps demonstrate phenotype correlation analysis within the FBN1 cohort.\n", "\n", "For additional usage patterns and parameter options, see the **Usage** section." ] } ], "metadata": { "kernelspec": { "display_name": "ppkt2synergy", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.20" } }, "nbformat": 4, "nbformat_minor": 5 }