Welcome to NGLView’s documentation!

NGLViewer - demo

Please wait ...

Contents:

class nglview.NGLWidget(structure=None, representations=None, parameters=None, **kwargs)[source]

Bases: ipywidgets.widgets.domwidget.DOMWidget

add_component(filename, **kwargs)[source]

add component from file/trajectory/struture

filename : str or Trajectory or Structure or their derived class or url **kwargs : additional arguments, optional

>>> view = nglview.Widget()
>>> view
>>> view.add_component(filename)

If you want to load binary file such as density data, mmtf format, it is faster to load file from current or subfolder.

add_pdbid(pdbid)[source]

add new Structure view by fetching pdb id from rcsb

>>> view = nglview.NGLWidget()
>>> view.add_pdbid('1tsu')
>>> # which is equal to 
>>> # view.add_component('rcsb://1tsu.pdb')
add_representation(repr_type, selection='all', **kwargs)[source]

Add structure representation (cartoon, licorice, ...) for given atom selection.

repr_type
: str
type of representation. Please see http://arose.github.io/ngl/api/ for further info.
selection
: str or 1D array (atom indices) or any iterator that returns integer, default ‘all’
atom selection

**kwargs: additional arguments for representation

>>> import nglview as nv
>>> import pytraj as pt
>>> t = (pt.datafiles.load_dpdp()[:].superpose('@CA'))
>>> w = nv.show_pytraj(t)
>>> w.add_representation('cartoon', selection='protein', color='blue')
>>> w.add_representation('licorice', selection=[3, 8, 9, 11], color='red')
>>> w

User can also use shortcut

>>> w.add_cartoon(selection) # w.add_representation('cartoon', selection)
add_structure(structure, **kwargs)[source]

add structure to view

structure : nglview.Structure object

>>> view.add_trajectory(traj0)
>>> view.add_trajectory(traj1)
>>> # then add Structure
>>> view.add_structure(...)

nglview.NGLWidget.add_component

add_trajectory(trajectory, **kwargs)[source]

add new trajectory to view

trajectory: nglview.Trajectory or its derived class or
pytraj.Trajectory-like, mdtraj.Trajectory or MDAnalysis objects

nglview.NGLWidget.add_component

>>> import nglview as nv, pytraj as pt
>>> traj = pt.load(nv.datafiles.TRR, nv.datafiles.PDB)
>>> view = nv.show_pytraj(view)
>>> # show view first
>>> view
>>> # add new Trajectory
>>> traj2 = pt.datafiles.load_tz2()
>>> view.add_trajectory(traj2)
background

A trait for unicode strings.

camera
center(*args, **kwargs)[source]

alias of center_view

center_view(zoom=True, selection='*', component=0)[source]

center view for given atom selection

view.center_view(selection=‘1-4’)

clear(*args, **kwargs)[source]

shortcut of clear_representations

clear_representations(component=0)[source]

clear all representations for given component

component
: int, default 0 (first model)
You need to keep track how many components you added.
color_by(color_scheme, component=0)[source]

update color for all representations of given component

Unstable feature

color_scheme : str component : int, default 0

component index
>>> # component 0
>>> view.color_by('atomindex')
>>> # component 1
>>> view.color_by('atomindex', component=1)
coordinates_dict
out
: dict of numpy 3D-array, dtype=’f4’
coordinates of trajectories at current frame
count

An int trait.

detach(split=False)[source]

detach player from its original container.

split
: bool, default False
if True, resize notebook then move it to the right of its container
display(gui=False, use_box=False)[source]
download_image(filename='screenshot.png', factor=4, antialias=True, trim=False, transparent=False)[source]

render and download scence at current frame

filename : str, default ‘screenshot.png’ factor : int, default 4

quality of the image, higher is better

antialias : bool, default True trim : bool, default False transparent : bool, default False

frame

An int trait.

hide(indices)[source]

set invisibility for given component/struture/trajectory (by their indices)

loaded

A boolean (True, False) trait.

n_components

An int trait.

on_frame_changed
on_loaded
on_update_dragged_file
orientation

An instance of a Python list.

parameters
picked

An instance of a Python dict.

remove_component(component_id)[source]

remove component by its uuid

>>> view.add_trajectory(traj0)
>>> view.add_trajectory(traj1)
>>> view.add_struture(structure)
>>> # remove last component
>>> view.remove_component(view._ngl_component_ids[-1])
render_image(frame=None, factor=4, antialias=True, trim=False, transparent=False)[source]

render and get image as ipywidgets.widget_image.Image

frame
: int or None, default None
if None, use current frame if specified, use this number.
factor
: int, default 4
quality of the image, higher is better

antialias : bool, default True trim : bool, default False transparent : bool, default False

# tell NGL to render send image data to notebook. view.render_image()

# make sure to call get_image method view.get_image()

You need to call render_image and get_image in different notebook’s Cells

representations
selector

A trait for unicode strings.

set_representations(representations, component=0)[source]

representations : list of dict

show(**kwargs)[source]

shortcut of show_only

show_only(indices='all')[source]

set visibility for given components (by their indices)

indices : {‘all’, array-like}, component index, default ‘all’

sync_view()[source]

call this if you want to sync multiple views of a single viewer

Note: unstable feature

update_representation(component=0, repr_index=0, **parameters)[source]
component
: int, default 0
component index
repr_index
: int, default 0
representation index for given component

parameters : dict

class nglview.NGLWidget(structure=None, representations=None, parameters=None, **kwargs)[source]

Bases: ipywidgets.widgets.domwidget.DOMWidget

add_component(filename, **kwargs)[source]

add component from file/trajectory/struture

filename : str or Trajectory or Structure or their derived class or url **kwargs : additional arguments, optional

>>> view = nglview.Widget()
>>> view
>>> view.add_component(filename)

If you want to load binary file such as density data, mmtf format, it is faster to load file from current or subfolder.

add_pdbid(pdbid)[source]

add new Structure view by fetching pdb id from rcsb

>>> view = nglview.NGLWidget()
>>> view.add_pdbid('1tsu')
>>> # which is equal to 
>>> # view.add_component('rcsb://1tsu.pdb')
add_representation(repr_type, selection='all', **kwargs)[source]

Add structure representation (cartoon, licorice, ...) for given atom selection.

repr_type
: str
type of representation. Please see http://arose.github.io/ngl/api/ for further info.
selection
: str or 1D array (atom indices) or any iterator that returns integer, default ‘all’
atom selection

**kwargs: additional arguments for representation

>>> import nglview as nv
>>> import pytraj as pt
>>> t = (pt.datafiles.load_dpdp()[:].superpose('@CA'))
>>> w = nv.show_pytraj(t)
>>> w.add_representation('cartoon', selection='protein', color='blue')
>>> w.add_representation('licorice', selection=[3, 8, 9, 11], color='red')
>>> w

User can also use shortcut

>>> w.add_cartoon(selection) # w.add_representation('cartoon', selection)
add_structure(structure, **kwargs)[source]

add structure to view

structure : nglview.Structure object

>>> view.add_trajectory(traj0)
>>> view.add_trajectory(traj1)
>>> # then add Structure
>>> view.add_structure(...)

nglview.NGLWidget.add_component

add_trajectory(trajectory, **kwargs)[source]

add new trajectory to view

trajectory: nglview.Trajectory or its derived class or
pytraj.Trajectory-like, mdtraj.Trajectory or MDAnalysis objects

nglview.NGLWidget.add_component

>>> import nglview as nv, pytraj as pt
>>> traj = pt.load(nv.datafiles.TRR, nv.datafiles.PDB)
>>> view = nv.show_pytraj(view)
>>> # show view first
>>> view
>>> # add new Trajectory
>>> traj2 = pt.datafiles.load_tz2()
>>> view.add_trajectory(traj2)
background

A trait for unicode strings.

camera
center(*args, **kwargs)[source]

alias of center_view

center_view(zoom=True, selection='*', component=0)[source]

center view for given atom selection

view.center_view(selection=‘1-4’)

clear(*args, **kwargs)[source]

shortcut of clear_representations

clear_representations(component=0)[source]

clear all representations for given component

component
: int, default 0 (first model)
You need to keep track how many components you added.
color_by(color_scheme, component=0)[source]

update color for all representations of given component

Unstable feature

color_scheme : str component : int, default 0

component index
>>> # component 0
>>> view.color_by('atomindex')
>>> # component 1
>>> view.color_by('atomindex', component=1)
coordinates_dict
out
: dict of numpy 3D-array, dtype=’f4’
coordinates of trajectories at current frame
count

An int trait.

detach(split=False)[source]

detach player from its original container.

split
: bool, default False
if True, resize notebook then move it to the right of its container
display(gui=False, use_box=False)[source]
download_image(filename='screenshot.png', factor=4, antialias=True, trim=False, transparent=False)[source]

render and download scence at current frame

filename : str, default ‘screenshot.png’ factor : int, default 4

quality of the image, higher is better

antialias : bool, default True trim : bool, default False transparent : bool, default False

frame

An int trait.

hide(indices)[source]

set invisibility for given component/struture/trajectory (by their indices)

loaded

A boolean (True, False) trait.

n_components

An int trait.

on_frame_changed
on_loaded
on_update_dragged_file
orientation

An instance of a Python list.

parameters
picked

An instance of a Python dict.

remove_component(component_id)[source]

remove component by its uuid

>>> view.add_trajectory(traj0)
>>> view.add_trajectory(traj1)
>>> view.add_struture(structure)
>>> # remove last component
>>> view.remove_component(view._ngl_component_ids[-1])
render_image(frame=None, factor=4, antialias=True, trim=False, transparent=False)[source]

render and get image as ipywidgets.widget_image.Image

frame
: int or None, default None
if None, use current frame if specified, use this number.
factor
: int, default 4
quality of the image, higher is better

antialias : bool, default True trim : bool, default False transparent : bool, default False

# tell NGL to render send image data to notebook. view.render_image()

# make sure to call get_image method view.get_image()

You need to call render_image and get_image in different notebook’s Cells

representations
selector

A trait for unicode strings.

set_representations(representations, component=0)[source]

representations : list of dict

show(**kwargs)[source]

shortcut of show_only

show_only(indices='all')[source]

set visibility for given components (by their indices)

indices : {‘all’, array-like}, component index, default ‘all’

sync_view()[source]

call this if you want to sync multiple views of a single viewer

Note: unstable feature

update_representation(component=0, repr_index=0, **parameters)[source]
component
: int, default 0
component index
repr_index
: int, default 0
representation index for given component

parameters : dict

class nglview.FileStructure(path)[source]

Bases: nglview.base_adaptor.Structure

get_structure_string()[source]
class nglview.TextStructure(text, ext='pdb', params={})[source]

Bases: nglview.base_adaptor.Structure

get_structure_string()[source]
class nglview.RdkitStructure(rdkit_mol, ext='pdb')[source]

Bases: nglview.base_adaptor.Structure

get_structure_string()[source]
class nglview.PdbIdStructure(pdbid)[source]

Bases: nglview.base_adaptor.Structure

get_structure_string()[source]
class nglview.ASEStructure(ase_atoms, ext='pdb', params={})[source]

Bases: nglview.base_adaptor.Structure

get_structure_string()[source]
class nglview.SimpletrajTrajectory(path, structure_path)[source]

Bases: nglview.base_adaptor.Trajectory, nglview.base_adaptor.Structure

simpletraj adaptor.

>>> import nglview as nv
>>> t = nv.SimpletrajTrajectory(nv.datafiles.XTC, nv.datafiles.GRO)
>>> w = nv.NGLWidget(t)
>>> w
get_coordinates(index)[source]
get_structure_string()[source]
n_frames
class nglview.MDTrajTrajectory(trajectory)[source]

Bases: nglview.base_adaptor.Trajectory, nglview.base_adaptor.Structure

mdtraj adaptor.

>>> import nglview as nv
>>> import mdtraj as md
>>> traj = md.load(nv.datafiles.XTC, nv.datafiles.GRO)
>>> t = MDTrajTrajectory(traj)
>>> w = nv.NGLWidget(t)
>>> w
get_coordinates(index)[source]
get_structure_string()[source]
n_frames
class nglview.PyTrajTrajectory(trajectory)[source]

Bases: nglview.base_adaptor.Trajectory, nglview.base_adaptor.Structure

PyTraj adaptor.

>>> import nglview as nv
>>> import pytraj as pt
>>> traj = pt.load(nv.datafiles.TRR, nv.datafiles.PDB)
>>> t = nv.PyTrajTrajectory(traj)
>>> w = nv.NGLWidget(t)
>>> w
get_coordinates(index)[source]
get_structure_string()[source]
n_frames
class nglview.ParmEdTrajectory(trajectory)[source]

Bases: nglview.base_adaptor.Trajectory, nglview.base_adaptor.Structure

ParmEd adaptor.

get_coordinates(index)[source]
get_structure_string()[source]
n_frames
class nglview.MDAnalysisTrajectory(atomgroup)[source]

Bases: nglview.base_adaptor.Trajectory, nglview.base_adaptor.Structure

MDAnalysis adaptor.

Can take either a Universe or AtomGroup.

>>> import nglview as nv
>>> import MDAnalysis as mda
>>> u = mda.Universe(nv.datafiles.GRO, nv.datafiles.XTC)
>>> prot = u.select_atoms('protein')
>>> t = nv.MDAnalysisTrajectory(prot)
>>> w = nv.NGLWidget(t)
>>> w
get_coordinates(index)[source]
get_structure_string()[source]
n_frames
class nglview.HTMDTrajectory(mol)[source]

Bases: nglview.base_adaptor.Trajectory

HTMD adaptor.

Takes a Molecule object.

>>> import nglview as nv
>>> from htmd import Molecule
>>> mol = Molecule(nv.datafiles.PDB)
>>> t = nv.HTMDTrajectory(mol)
>>> w = nv.NGLWidget(t)
>>> w
get_coordinates(index)[source]
get_structure_string()[source]
n_frames
class nglview.register_backend(package_name)[source]

Bases: object

nglview.demo(*args, **kwargs)[source]
nglview.show_pdbid(pdbid, **kwargs)[source]

Show PDB entry.

>>> import nglview as nv
>>> w = nv.show_pdbid("3pqr")
>>> w
nglview.show_url(url, **kwargs)[source]
nglview.show_text(text, **kwargs)[source]

for development

nglview.show_ase(ase_atoms, **kwargs)[source]
>>> import nglview as nv
>>> from ase import Atom, Atoms
>>> dimer = Atoms([Atom('X', (0, 0, 0)),
...                Atom('X', (0, 0, 1))])
>>> dimer.set_positions([(1, 2, 3), (4, 5, 6.2)])
>>> nv.show_ase(dimer)
nglview.show_simpletraj(traj, **kwargs)[source]

Show simpletraj trajectory and structure file.

>>> import nglview as nv
>>> w = nv.show_simpletraj(nv.datafiles.GRO, nv.datafiles.XTC)
>>> w
nglview.show_mdtraj(mdtraj_trajectory, **kwargs)[source]

Show mdtraj trajectory.

>>> import nglview as nv
>>> import mdtraj as md
>>> t = md.load(nv.datafiles.XTC, top=nv.datafiles.GRO)
>>> w = nv.show_mdtraj(t)
>>> w
nglview.show_pytraj(pytraj_trajectory, **kwargs)[source]

Show pytraj trajectory.

>>> import nglview as nv
>>> import pytraj as pt
>>> t = pt.load(nv.datafiles.TRR, nv.datafiles.PDB)
>>> w = nv.show_pytraj(t)
>>> w
nglview.show_mdanalysis(atomgroup, **kwargs)[source]

Show NGL widget with MDAnalysis AtomGroup.

Can take either a Universe or AtomGroup as its data input.

>>> import nglview as nv
>>> import MDAnalysis as mda
>>> u = mda.Universe(nv.datafiles.GRO, nv.datafiles.XTC)
>>> prot = u.select_atoms('protein')
>>> w = nv.show_mdanalysis(prot)
>>> w
nglview.show_parmed(parmed_structure, **kwargs)[source]

Show pytraj trajectory.

>>> import nglview as nv
>>> import parmed as pmd
>>> t = pt.load_file(nv.datafiles.PDB)
>>> w = nv.show_parmed(t)
>>> w
nglview.show_rdkit(rdkit_mol, **kwargs)[source]

Show rdkit’s Mol.

rdkit_mol : rdkit.Chem.rdchem.Mol kwargs : additional keyword argument

>>> import nglview as nv
>>> from rdkit import Chem
>>> from rdkit.Chem import AllChem
>>> m = Chem.AddHs(Chem.MolFromSmiles('COc1ccc2[C@H](O)[C@@H](COc2c1)N3CCC(O)(CC3)c4ccc(F)cc4'))
>>> AllChem.EmbedMultipleConfs(m, useExpTorsionAnglePrefs=True, useBasicKnowledge=True)
>>> view = nv.show_rdkit(m)
>>> view
>>> # add component m2
>>> # create file-like object
>>> fh = StringIO(Chem.MolToPDBBlock(m2))
>>> view.add_component(fh, ext='pdb')
>>> # load as trajectory, need to have ParmEd
>>> view = nv.show_rdkit(m, parmed=True)
nglview.show_structure_file(path, **kwargs)[source]

Show structure file. Allowed are text-based structure file formats that are by supported by NGL, including pdb, gro, mol2, sdf.

>>> import nglview as nv
>>> w = nv.show_structure_file(nv.datafiles.GRO)
>>> w
nglview.show_htmd(mol, **kwargs)[source]

Show NGL widget with HTMD Molecule.

Takes a Molecule object as its data input.

>>> import nglview as nv
>>> from htmd import Molecule
>>> mol = Molecule(nv.datafiles.PDB)
>>> mol.filter('protein')
>>> w = nv.show_htmd(mol)
>>> w

Indices and tables