{
"cells": [
{
"cell_type": "markdown",
"id": "ddae61fd-fa15-4be9-9160-ba3e195c67dc",
"metadata": {},
"source": [
"# Plotting OpenFE results against experiment using Cinnabar v0.6.0\n",
"\n",
"This notebook shows how one would go about creating a ``cinnabar`` style plot of OpenFE results against known experimental values. For more information on the ``cinnabar`` API and the available plot styles, see the [tutorials](https://cinnabar.openfree.energy/en/latest/tutorials/index.html)."
]
},
{
"cell_type": "markdown",
"id": "6f3d03c2",
"metadata": {},
"source": [
"## Setup for Google Colab\n",
"\n",
"If you are running this example in Google Colab, run the following cells to setup the environment. If you are running this notebook locally, skip down to `Loading experimental data`"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "04aa17bf",
"metadata": {},
"outputs": [],
"source": [
"# NBVAL_SKIP\n",
"# Only run this cell if on Google Colab\n",
"import os\n",
"if \"COLAB_RELEASE_TAG\" in os.environ:\n",
" # fix for colab's torchvision causing issues\n",
" !rm -r /usr/local/lib/python3.12/dist-packages/torchvision\n",
" \n",
" !pip install -q condacolab\n",
" import condacolab\n",
" condacolab.install_from_url(\"https://github.com/OpenFreeEnergy/openfe/releases/download/v1.7.0/OpenFEforge-1.7.0-Linux-x86_64.sh\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6d297968",
"metadata": {},
"outputs": [],
"source": [
"# NBVAL_SKIP\n",
"# Only run this cell if on google colab\n",
"import os\n",
"if \"COLAB_RELEASE_TAG\" in os.environ:\n",
" import condacolab\n",
" import locale\n",
" locale.getpreferredencoding = lambda: \"UTF-8\"\n",
" !mkdir inputs && cd inputs && openfe fetch rbfe-tutorial\n",
" # quick fix for https://github.com/conda-incubator/condacolab/issues/75\n",
" # if deprecation warnings persist, rerun this cell\n",
" import warnings\n",
" warnings.filterwarnings(action=\"ignore\", message=r\"datetime.datetime.utcnow\") \n",
" for _ in range(3):\n",
" # Sometimes we have to re-run the check\n",
" try:\n",
" condacolab.check()\n",
" except:\n",
" pass\n",
" else:\n",
" break"
]
},
{
"cell_type": "markdown",
"id": "22124f1f-5349-4174-830d-65c5bf9f560e",
"metadata": {},
"source": [
"## Loading experimental data\n",
"\n",
"First, we load the experimental data from a tab-separated values (TSV) file.\n",
"\n",
"The format of the TSV file is as follows:\n",
"\n",
"```\n",
"ligand estimate (kcal/mol) uncertainty (kcal/mol)\n",
"```\n",
"\n",
"**Important note:**\n",
"Please note that for now you must have an experimental entry for every ligand involved in your free energy network.\n",
"In future versions of cinnabar this will no longer be needed."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6abcaae7-f5fd-483c-972f-5bb5b6d4908a",
"metadata": {},
"outputs": [],
"source": [
"# First we do a set of imports\n",
"%matplotlib inline\n",
"%config InlineBackend.figure_formats = ['svg']\n",
"import pandas as pd\n",
"from cinnabar import FEMap, plotting\n",
"from openff.units import unit"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8b80f592-f1a3-4c7f-8757-58a88c2e8ee7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
ligand
\n",
"
estimate (kcal/mol)
\n",
"
uncertainty (kcal/mol)
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
lig_ejm_31
\n",
"
-9.57
\n",
"
0.18
\n",
"
\n",
"
\n",
"
1
\n",
"
lig_ejm_42
\n",
"
-9.81
\n",
"
0.18
\n",
"
\n",
"
\n",
"
2
\n",
"
lig_ejm_43
\n",
"
-8.29
\n",
"
0.18
\n",
"
\n",
"
\n",
"
3
\n",
"
lig_ejm_45
\n",
"
-9.59
\n",
"
0.18
\n",
"
\n",
"
\n",
"
4
\n",
"
lig_ejm_46
\n",
"
-11.35
\n",
"
0.17
\n",
"
\n",
"
\n",
"
5
\n",
"
lig_ejm_47
\n",
"
-9.73
\n",
"
0.18
\n",
"
\n",
"
\n",
"
6
\n",
"
lig_ejm_48
\n",
"
-9.03
\n",
"
0.18
\n",
"
\n",
"
\n",
"
7
\n",
"
lig_ejm_50
\n",
"
-9.01
\n",
"
0.18
\n",
"
\n",
"
\n",
"
8
\n",
"
lig_ejm_54
\n",
"
-10.57
\n",
"
0.18
\n",
"
\n",
"
\n",
"
9
\n",
"
lig_ejm_55
\n",
"
-9.24
\n",
"
0.18
\n",
"
\n",
"
\n",
"
10
\n",
"
lig_jmc_23
\n",
"
-11.74
\n",
"
0.18
\n",
"
\n",
"
\n",
"
11
\n",
"
lig_jmc_27
\n",
"
-11.31
\n",
"
0.17
\n",
"
\n",
"
\n",
"
12
\n",
"
lig_jmc_28
\n",
"
-11.01
\n",
"
0.18
\n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ligand estimate (kcal/mol) uncertainty (kcal/mol)\n",
"0 lig_ejm_31 -9.57 0.18\n",
"1 lig_ejm_42 -9.81 0.18\n",
"2 lig_ejm_43 -8.29 0.18\n",
"3 lig_ejm_45 -9.59 0.18\n",
"4 lig_ejm_46 -11.35 0.17\n",
"5 lig_ejm_47 -9.73 0.18\n",
"6 lig_ejm_48 -9.03 0.18\n",
"7 lig_ejm_50 -9.01 0.18\n",
"8 lig_ejm_54 -10.57 0.18\n",
"9 lig_ejm_55 -9.24 0.18\n",
"10 lig_jmc_23 -11.74 0.18\n",
"11 lig_jmc_27 -11.31 0.17\n",
"12 lig_jmc_28 -11.01 0.18"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# read in the experimental data\n",
"experimental_data = pd.read_csv('experimental.tsv', sep=\"\\t\")\n",
"experimental_data"
]
},
{
"cell_type": "markdown",
"id": "aca5ede7-3741-4cc1-97ba-9115d90abd96",
"metadata": {},
"source": [
"## Loading free energy results\n",
"\n",
"Next we load in results from the TSV file created by running `openfe gather --report ddg` on a network of results from [relative binding free energy calculations](https://docs.openfree.energy/en/latest/guide/protocols/relativehybridtopology.html#overview).\n",
"\n",
"Please see the full [RBFE gather command tutorial](https://github.com/OpenFreeEnergy/ExampleNotebooks/blob/main/rbfe_tutorial/cli_tutorial.md) for more information."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "63a920b3-6ef3-4214-b3cc-433319053721",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
ligand_i
\n",
"
ligand_j
\n",
"
DDG(i->j) (kcal/mol)
\n",
"
uncertainty (kcal/mol)
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
lig_ejm_31
\n",
"
lig_ejm_42
\n",
"
0.49
\n",
"
0.09
\n",
"
\n",
"
\n",
"
1
\n",
"
lig_ejm_31
\n",
"
lig_ejm_46
\n",
"
-0.73
\n",
"
0.10
\n",
"
\n",
"
\n",
"
2
\n",
"
lig_ejm_31
\n",
"
lig_ejm_47
\n",
"
0.00
\n",
"
0.20
\n",
"
\n",
"
\n",
"
3
\n",
"
lig_ejm_31
\n",
"
lig_ejm_48
\n",
"
0.50
\n",
"
0.20
\n",
"
\n",
"
\n",
"
4
\n",
"
lig_ejm_31
\n",
"
lig_ejm_50
\n",
"
0.94
\n",
"
0.07
\n",
"
\n",
"
\n",
"
5
\n",
"
lig_ejm_42
\n",
"
lig_ejm_43
\n",
"
1.20
\n",
"
0.10
\n",
"
\n",
"
\n",
"
6
\n",
"
lig_ejm_46
\n",
"
lig_jmc_23
\n",
"
-0.39
\n",
"
0.05
\n",
"
\n",
"
\n",
"
7
\n",
"
lig_ejm_46
\n",
"
lig_jmc_27
\n",
"
-0.60
\n",
"
0.10
\n",
"
\n",
"
\n",
"
8
\n",
"
lig_ejm_46
\n",
"
lig_jmc_28
\n",
"
-0.12
\n",
"
0.04
\n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ligand_i ligand_j DDG(i->j) (kcal/mol) uncertainty (kcal/mol)\n",
"0 lig_ejm_31 lig_ejm_42 0.49 0.09\n",
"1 lig_ejm_31 lig_ejm_46 -0.73 0.10\n",
"2 lig_ejm_31 lig_ejm_47 0.00 0.20\n",
"3 lig_ejm_31 lig_ejm_48 0.50 0.20\n",
"4 lig_ejm_31 lig_ejm_50 0.94 0.07\n",
"5 lig_ejm_42 lig_ejm_43 1.20 0.10\n",
"6 lig_ejm_46 lig_jmc_23 -0.39 0.05\n",
"7 lig_ejm_46 lig_jmc_27 -0.60 0.10\n",
"8 lig_ejm_46 lig_jmc_28 -0.12 0.04"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Read in calculated relative free energy results\n",
"calculated_data = pd.read_csv('final_results.tsv', sep=\"\\t\")\n",
"calculated_data"
]
},
{
"cell_type": "markdown",
"id": "a1daf9cf-5107-4564-a200-b9fe670837cb",
"metadata": {},
"source": [
"## Creating a Cinnabar ``FEMap``\n",
"\n",
"Next we will use the ``cinnabar`` API to gather the experimental and calculated results into a single object which we can use to plot and analyse the network of results.\n",
"\n",
"**Note**: The ``FEMap`` object requires estimates and uncertainties to have units attached to remove ambiguity in the data. When using OpenFE results, the units are always kcal/mol, so we can attach these units using the [OpenFF Units API](https://docs.openforcefield.org/projects/units/en/stable/)."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "16d98240-62b3-469c-a12a-c2b8337caba9",
"metadata": {},
"outputs": [],
"source": [
"fe_map = FEMap()\n",
"\n",
"# add the calculated data\n",
"for _, row in calculated_data.iterrows():\n",
" fe_map.add_relative_calculation(\n",
" # the ligand we are transforming from\n",
" labelA=row['ligand_i'],\n",
" # the ligand we are transforming to\n",
" labelB=row['ligand_j'],\n",
" # the computed relative free energy change for the transformation with units\n",
" value=row[\"DDG(i->j) (kcal/mol)\"] * unit.kilocalories_per_mole,\n",
" # the uncertainty in the computed relative free energy change with units\n",
" uncertainty=row[\"uncertainty (kcal/mol)\"] * unit.kilocalories_per_mole,\n",
" # the source of the data, can be any string but should be consistent across all data points from the same source\n",
" source=\"openfe\",\n",
" )\n",
"\n",
"# add the experimental data\n",
"for _, row in experimental_data.iterrows():\n",
" fe_map.add_experimental_measurement(\n",
" # the ligand for which we have an experimental measurement\n",
" label=row['ligand'],\n",
" # the experimental free energy for that ligand with units\n",
" value=row['estimate (kcal/mol)'] * unit.kilocalories_per_mole,\n",
" # the uncertainty in the experimental free energy with units\n",
" uncertainty=row['uncertainty (kcal/mol)'] * unit.kilocalories_per_mole,\n",
" # the source of the data, can be any string but should be consistent across all data points from the same source\n",
" source=\"Experimental\",\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "7dea03c7-6b6a-4e59-96e7-a94cdc9047e9",
"metadata": {},
"source": [
"## Plotting the results using Cinnabar\n",
"\n",
"Once we've created the ``FEMap`` we can inspect the alchemical network:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "86c004bc-bc58-4a55-92e2-151beb994d65",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fe_map.draw_graph()"
]
},
{
"cell_type": "markdown",
"id": "5caacbf3-089c-460c-bc91-bb22e78b3c7c",
"metadata": {},
"source": [
"### Plotting out the relative free energy results\n",
"\n",
"Next we can go ahead and plot out the relative free energy results."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2edeabb9-9c07-49be-a83f-9622ef742046",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# note you can pass the filename argument to write things out to file\n",
"plotting.plot_DDGs(\n",
" fe_map, \n",
" source=\"openfe\", # the name of the computational source to plot\n",
" figsize=5\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "23c78360-bd2f-40a7-97a6-d9f967f6abb6",
"metadata": {},
"source": [
"### Plotting out the absolute free energy results\n",
"\n",
"Finally let's generate an estimate of the absolute binding free energy for each ligand using the [MLE estimator](https://cinnabar.openfree.energy/en/latest/concepts/estimators.html#maximum-likelihood-estimation-mle) and plot out the derived values:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "f39ef3c0-4d74-4c53-83bc-94d820428d47",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# note you can pass the filename argument to write to file\n",
"fe_map.generate_absolute_values() # Get MLE generated estimates of the absolute values\n",
"plotting.plot_DGs(\n",
" fe_map, \n",
" source=\"MLE\", # the name of the computational source to plot\n",
" figsize=5\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "a905416e-11bd-47cb-8002-5ac923b0e95b",
"metadata": {},
"source": [
"We can also shift our free energies by the average experimental value to have DGs on the same scale as experiment."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "7fed7b2c-076f-4f98-818d-abdbdaf3121b",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"shift = experimental_data[\"estimate (kcal/mol)\"].mean() # shift the computed values by the mean of the experimental values to put them on a similar scale\n",
"\n",
"plotting.plot_DGs(\n",
" fe_map, \n",
" source=\"MLE\", \n",
" figsize=5, \n",
" shift=shift\n",
")"
]
},
{
"cell_type": "markdown",
"id": "0c1b5935-d11b-4c1b-b092-038b007323ce",
"metadata": {},
"source": [
"## Writing out the MLE derived absolute free energies\n",
"\n",
"Finally, we can also extract the MLE derived absolute free energies along with the experimental results into a pandas ``DataFrame``."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "26b09073-f05f-401c-a10f-95c6c957f4da",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"