{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualizing Molecules\n",
"\n",
"> ### In this tutorial we will cover:\n",
"> - how we can visualize our structures in 3D\n",
"> - how we can highlight specific parts of molecules\n",
"> - how we can export visualizations\n",
"> - how we can visualize structures in 2D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When building a molecule we need to know which parts to connect to each other. This involves defining _linkages_ where we specify which atoms to connect to each other. How do we know what the atoms are called or what serial numbers they have? Unless we are working with pre-defined linkages and compounds that we trust, we usually don't! The solution is to look at the structure and get the info for ourselves. Also, what if we find the built structure to be a little \"crammed\" or have a clash somewhere. We can optimize the conformation and the best way to check that the optimization was successful is again to look at the result directly. There are many reasons for us to visually inspect structures. This is why biobuild molecules have two methods `show` and `draw` that will visualize the structures in 3D. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plotting backends \n",
"\n",
"Biobuild uses `plotly` as backend to visualize structures. Plotly can handle large plots pretty well even in 3D but it may get overwhelmed if you overlay too many layers! Especially when overlaying different conformations plots can get really large really quickly - 50 conformations of a 200 atom molecule on top of each other is getting difficult to handle! As an alternative, biobuild also integrates `nglview` and `py3dmol` to visualize a molecular structure. In this case no further highlighting functionality is offered, however. There is a correspondignly named method in the Molecule class to open an nglview window instead of a plotly figure."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `show` vs `draw`\n",
"\n",
"The difference between the two is simple. They are identical, except that `show` calls `draw` and immediately opens the preview window without returning anything. On the other hand, `draw` returns a `MoleculeViewer3D` object that can be further used to modify the visualization before in turn calling its own `show` method to open the preview. In summary, when you only want to directly look at a molecule without highlighting anything, use `show` otherwise use `draw` and then do your highlighting."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us visualize and highlight parts in a larger molecule."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import plotly\n",
"plotly.offline.init_notebook_mode()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"customdata": [
[
"O7",
1,
1,
"PRP",
"A"
],
[
"O",
35,
2,
"SER",
"A"
],
[
"OG",
37,
2,
"SER",
"A"
],
[
"OXT",
38,
2,
"SER",
"A"
],
[
"O3",
43,
3,
"TBA",
"A"
],
[
"O1",
44,
3,
"TBA",
"A"
],
[
"O2",
77,
5,
"HAC",
"A"
],
[
"O1",
78,
5,
"HAC",
"A"
],
[
"O7",
84,
6,
"PRP",
"A"
],
[
"O",
118,
7,
"SER",
"A"
],
[
"OG",
120,
7,
"SER",
"A"
],
[
"OXT",
121,
7,
"SER",
"A"
],
[
"O3",
126,
8,
"TBA",
"A"
],
[
"O1",
127,
8,
"TBA",
"A"
]
],
"hovertemplate": "atom_element=O
x=%{x}
y=%{y}
z=%{z}
atom_id=%{customdata[0]}
atom_serial=%{customdata[1]}
residue_serial=%{customdata[2]}
residue_name=%{customdata[3]}
chain_id=%{customdata[4]}
x=%{x}
y=%{y}
z=%{z}
atom_id=%{customdata[0]}
atom_serial=%{customdata[1]}
residue_serial=%{customdata[2]}
residue_name=%{customdata[3]}
chain_id=%{customdata[4]}
x=%{x}
y=%{y}
z=%{z}
atom_id=%{customdata[0]}
atom_serial=%{customdata[1]}
residue_serial=%{customdata[2]}
residue_name=%{customdata[3]}
chain_id=%{customdata[4]}
x=%{x}
y=%{y}
z=%{z}
atom_id=%{customdata[0]}
atom_serial=%{customdata[1]}
residue_serial=%{customdata[2]}
residue_name=%{customdata[3]}
chain_id=%{customdata[4]}
x=%{x}
y=%{y}
z=%{z}
atom_id=%{customdata[0]}
atom_serial=%{customdata[1]}
residue_serial=%{customdata[2]}
residue_name=%{customdata[3]}
chain_id=%{customdata[4]}
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n jupyter labextension install jupyterlab_3dmol
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
\n",
" jupyter labextension install jupyterlab_3dmol