{
"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\n",
"> - how we can customize 2D visualizations"
]
},
{
"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 BuildAMol molecules have two methods `show` and `draw` that will visualize the structures in 3D. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotting backends \n",
"\n",
"BuildAMol 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, BuildAMol 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.\n",
"\n",
"We can set the default backend for visualization using \n",
"\n",
"```python\n",
"import buildamol as bam\n",
"\n",
"bam.visual.set_backend(\"plotly\") # (default)\n",
"bam.visual.set_backend(\"py3dmol\")\n",
"bam.visual.set_backend(\"nglview\")\n",
"```\n",
"\n",
"The backend will be used by the `Molecule.draw` and `Molecule.show` methods to create visualizations. Specific methods for each backend are also available: `Molecule.plotly`, `Molecule.py3dmol`, and `Molecule.nglview`, which operate at the \"drawing\" level and return an object that can be further manipulated before ultimatelly rendering using their own `show` method. "
]
},
{
"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",
" \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}
__marker_size=%{marker.size}
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}
__marker_size=%{marker.size}
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}
__marker_size=%{marker.size}
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}
__marker_size=%{marker.size}
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}
__marker_size=%{marker.size}
atom_id=%{customdata[0]}
atom_serial=%{customdata[1]}
residue_serial=%{customdata[2]}
residue_name=%{customdata[3]}
chain_id=%{customdata[4]}
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.