Skip to content

Commit 2f3aff1

Browse files
author
wjm41
committed
minor fix
1 parent c8cce63 commit 2f3aff1

2 files changed

Lines changed: 56 additions & 6 deletions

File tree

examples/pose_centroids_molplotly.ipynb

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,30 @@
614614
20.56523380975767
615615
],
616616
"yaxis": "y"
617+
},
618+
{
619+
"marker": {
620+
"color": "red",
621+
"size": 12,
622+
"symbol": "x"
623+
},
624+
"mode": "markers",
625+
"showlegend": false,
626+
"type": "scatter",
627+
"x": [
628+
20.710113587648642,
629+
4.312267729434556,
630+
18.935473315914678,
631+
17.68965247025577,
632+
20.61053154671133
633+
],
634+
"y": [
635+
20.431212351814835,
636+
4.189062767055896,
637+
18.95856209889753,
638+
17.674840683395807,
639+
20.55944905349298
640+
]
617641
}
618642
],
619643
"layout": {
@@ -1473,7 +1497,7 @@
14731497
}
14741498
],
14751499
"source": [
1476-
"import plotly.express as px\n",
1500+
"import plotly.graph_objects as go\n",
14771501
"\n",
14781502
"rmsd_df['pose_index'] = range(1, len(renamed_poses)+1)\n",
14791503
"rmsd_df['cluster'] = kmeans.labels_\n",
@@ -1490,14 +1514,25 @@
14901514
" labels={'1':'RMSD to pose 1',\n",
14911515
" '2':'RMSD to pose 2',}\n",
14921516
" )\n",
1517+
"scatter_fig.add_trace(\n",
1518+
" go.Scatter(\n",
1519+
" x = kmeans.cluster_centers_[:,0],\n",
1520+
" y = kmeans.cluster_centers_[:,1],\n",
1521+
" mode='markers',\n",
1522+
" marker=dict(color=\"red\", \n",
1523+
" symbol='x',\n",
1524+
" size=10),\n",
1525+
" showlegend=False,))\n",
14931526
"scatter_fig.update_traces(marker=dict(size=12))\n"
14941527
]
14951528
},
14961529
{
14971530
"cell_type": "markdown",
14981531
"metadata": {},
14991532
"source": [
1500-
"Adding `molplotly` and specifying `mol_col` allows the 3D coordinates of the pose to be shown on mouseover!"
1533+
"Adding `molplotly` and specifying `mol_col` allows the 3D coordinates of the pose to be shown on mouseover!\n",
1534+
"\n",
1535+
"Unfortunately there are issues with handling additional non-molecular traces in `molplotly` so we'll have to re-create the scatter plot without the centroids."
15011536
]
15021537
},
15031538
{
@@ -1520,7 +1555,7 @@
15201555
" "
15211556
],
15221557
"text/plain": [
1523-
"<IPython.lib.display.IFrame at 0x13733e8c0>"
1558+
"<IPython.lib.display.IFrame at 0x139627d30>"
15241559
]
15251560
},
15261561
"metadata": {},
@@ -1532,6 +1567,20 @@
15321567
"\n",
15331568
"rmsd_df['mol'] = renamed_poses \n",
15341569
"\n",
1570+
"scatter_fig = px.scatter(rmsd_df, \n",
1571+
" x=\"1\", \n",
1572+
" y=\"2\",\n",
1573+
" color='cluster',\n",
1574+
" hover_name='pose_index',\n",
1575+
" width=1000,\n",
1576+
" height=800,\n",
1577+
" title='Clustering of ligand poses',\n",
1578+
" labels={'1':'RMSD to pose 1',\n",
1579+
" '2':'RMSD to pose 2',}\n",
1580+
" )\n",
1581+
"\n",
1582+
"scatter_fig.update_traces(marker=dict(size=12))\n",
1583+
"\n",
15351584
"app_clusters = molplotly.add_molecules(\n",
15361585
" fig=scatter_fig,\n",
15371586
" df=rmsd_df,\n",
@@ -1553,7 +1602,7 @@
15531602
},
15541603
{
15551604
"cell_type": "code",
1556-
"execution_count": 7,
1605+
"execution_count": 6,
15571606
"metadata": {},
15581607
"outputs": [
15591608
{
@@ -1571,7 +1620,7 @@
15711620
" "
15721621
],
15731622
"text/plain": [
1574-
"<IPython.lib.display.IFrame at 0x13733f820>"
1623+
"<IPython.lib.display.IFrame at 0x13969be50>"
15751624
]
15761625
},
15771626
"metadata": {},

molplotly/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ def find_grouping(
5555

5656
if fig.data[0].hovertemplate is not None:
5757
col_names = re.findall(r"(.*?)=(?!%).*?<.*?>", fig.data[0].hovertemplate)
58+
col_names = [re.sub(r"(.*)>", "", col_name) for col_name in col_names]
5859
if set(col_names) != set(cols):
5960
raise ValueError(
60-
"marker_col/color_col/facet_col is misspecified because the dataframe grouping names don't match the names in the plotly figure.",
61+
f"marker_col/color_col/facet_col is misspecified because the specified dataframe grouping names {cols} don't match the names in the plotly figure {col_names}.",
6162
)
6263

6364
df_grouped = df_data.groupby(col_names)

0 commit comments

Comments
 (0)