Working with NGLView#
Creating multi-layered graphical representations, styling binding sites, and rendering surfaces in NGLView.
Effective scientific communication requires clear and expressive molecular figures. MolSysMT provides a direct bridge to NGLView, allowing you to combine diverse representations (cartoon backbones, licorice side-chains, transparent surfaces, contact cylinders, and vector arrows) with spatial precision.
In this recipe, we build an expressive multi-component visualization of T4 Lysozyme with its bound Benzene ligand, highlighting active pocket interactions and exporting the scene.
Added in version 1.0.0.
Loading Complex#
We load the high-resolution T4 Lysozyme L99A system from the MolSysMT bundled dataset:
import molsysmt as msm
# Load T4 Lysozyme with bound Benzene ligand
molsys = msm.convert(msm.systems['T4 lysozyme L99A']['181l.h5msm'], to_form='molsysmt.MolSys')
msm.info(molsys)
| form | n_atoms | n_groups | n_components | n_chains | n_molecules | n_entities | n_waters | n_ions | n_small_molecules | n_proteins | n_structures |
|---|---|---|---|---|---|---|---|---|---|---|---|
| molsysmt.MolSys | 1441 | 302 | 141 | 6 | 141 | 5 | 136 | 2 | 2 | 1 | 1 |
Base Representation#
We convert the system to an interactive nglview.NGLWidget and standardize its camera viewport, background, and lighting:
# Convert to NGLWidget
view = msm.convert(molsys, to_form='nglview.NGLWidget')
# Apply standardized orientation and optimal lighting
msm.third_party.nglview.standardize_view(view)
msm.third_party.nglview.show_as_cartoon(view, selection='molecule_type=="protein"', color='silver')
view
Pocket Representation#
Next, we highlight the hydrophobic cavity residues surrounding the Benzene ligand using spatial selection syntax:
# Highlight binding pocket residues in licorice representation
pocket_sel = 'molecule_type=="protein" within 0.5 nm of group_name=="BNZ"'
msm.third_party.nglview.show_as_licorice(view, selection=pocket_sel)
# Highlight ligand in ball-and-stick representation
msm.third_party.nglview.show_as_balls_and_sticks(view, selection='group_name=="BNZ"')
# Add transparent solvent-accessible surface around the pocket
msm.third_party.nglview.show_as_surface(view, selection=pocket_sel, opacity=0.4)
view
Annotation Arrows#
We can overlay 3D vector arrows on the active view to denote principal axes, pocket orientation, or binding vectors:
# Draw a vector arrow indicating active pocket axis
msm.third_party.nglview.add_arrows(
view,
origin='atom_index==0',
end='atom_index==20',
color='#E74C3C',
radius='0.4 angstroms'
)
view
Exporting HTML Scene#
Finally, we serialize the complete 3D scene into a standalone, interactive HTML file for web publishing or sharing with collaborators:
import os
# Export the interactive scene to HTML
html_file = 't4_lysozyme_scene.html'
msm.third_party.nglview.write_html(view, html_file)
print(f"HTML scene exported: {os.path.exists(html_file)} (size: {os.path.getsize(html_file)} bytes)")
if os.path.exists(html_file):
os.remove(html_file)
HTML scene exported: True (size: 189326 bytes)
See also
View: Interactive visualization gateway with
molsysmt.basic.view().molsysmt.third_party.nglview.show_as_cartoon(): Styling protein backbones with cartoon ribbons in NGLView.molsysmt.third_party.nglview.show_as_surface(): Rendering molecular surfaces in NGLView.molsysmt.third_party.nglview.add_arrows(): Overlaying 3D vector arrows in NGLView.