Molecular visualization in the Jupyter Notebook with nglview

nglview is a Python package that makes it easy to visualize molecular systems, including trajectories, directly in the Jupyter Notebook. The recent 0.4.0 release of nglview brings a convenient interface for visualizing MDAnalysis Universe and AtomGroup objects directly:

import MDAnalysis as mda
import nglview as nv
from nglview.datafiles import PDB, XTC

u = mda.Universe(PDB, XTC)

protein = u.select_atoms('protein')

Although we could use the full Universe, selecting a subset of the atoms as an AtomGroup will give better performance when viewing the trajectory interactively. We can feed this directly to the nglview.show_mdanalysis function to produce the Jupyter widget:

w = nv.show_mdanalysis(protein)

This will give something like this in the notebook itself:

The notebook widget (you’ll have to try it yourself to see — what you see above is a limited view but you can still rotate and zoom the molecule) also lets you select atoms by clicking on the molecule, add custom representations to atom selections, and interactively view different frames of the trajectory, both programmatically and with a graphical slider. Be sure to double-click the notebook viewer widget to have a look at everything in a lovely full-screen view, too!

nglview is just a pip install away:

pip install nglview

Give it a try, and be sure to thank Alexander Rose (@arose) and Hai Nguyen (@hainm) for pushing this project forward!