NGL Viewer
repository·master·Indexed 20 days ago
https://github.com/nglviewer/nglA high-performance WebGL-based molecular visualization tool for web applications. NGL allows developers to render complex molecular structures, density volumes, and coordinate trajectories in a browser. It supports a wide range of formats including mmCIF, PDB, and MMTF, and provides a JavaScript API for programmatic control, as well as integrations for Python (NGLView) and R (NGLViewR).
What's inside NGL
- NGL is an online viewer for proteins and other molecular structures. It provides a high-performance graphical interface for common visualization tasks and is built on WebGL technology, meaning it requires only a modern web browser to run. It is designed for both end-users who want a GUI and developers who want to integrate molecular visualization into their own web applications.
Overview of NGL Viewer features
masterNGL Viewer is a WebGL-powered molecular visualization application. Key capabilities include:
- Molecular Structures: Supports mmCIF, PDB, PQR, GRO, SDF, MOL2, and MMTF formats.
- Density Volumes: Supports MRC/MAP/CCP4, DX/DXBIN, CUBE, BRIX/DSN6, and XPLOR/CNS.
- User Interaction: Includes mouse picking, a selection language, animations, and image export.
- Coordinate Trajectories: Supports DCD & PSF, NCTRAJ & PRMTOP, and TRR/XTC & TOP. Remote access is available via MDSrv.
- Integration: Embeddable via a single file or API.
Overview of available Molecular Representations
masterNGL supports a wide variety of representations to visualize molecular structures. These can be combined to create complex views.
Common Types
spacefill: Atoms as space-filling spheres.ball+stick: Atoms as spheres and bonds as cylinders.licorice: A variant ofball+stickwhereaspectRatiois fixed to1.0.cartoon: Smooth trace representing secondary structure.backbone: Cylinders connecting successive residues via main backbone atoms (.CAfor proteins,.C4'/.C3'for RNA/DNA) and spheres for the atoms themselves.line: Simple lines for bonds.ribbon: A thin ribbon along the backbone.tube: Similar tocartoonbut withaspectRatiofixed at1.0.surface: Molecular surfaces (vws, sas, ms, ses, av).base: Simplified RNA/DNA nucleotide display (best used withcartoon).rope: A tube following local axes, useful for coarse-grained structures.point: Atoms as textured points.trace: A flat, unshaded line along the backbone.distance: Displays distances between atom pairs.label: Displays text labels near atoms.unitcell: Draws crystallographic unitcell edges and corners.validation: Draws clashes from wwPDB validation reports.
How NGL Stage, Components, and Representations work together
masterThe NGL viewer follows a hierarchical model:
Stage: The main entry point and container for the viewer. It manages the viewport and the lifecycle of loaded data.Component: Created viastage.loadFile(). A component contains the actual structural or volume data (e.g., a PDB structure).Representation: Added to aComponentto define how the data is visually rendered (e.g., "cartoon", "surface", or "ball+stick").
Example workflow:
- Create a
Stage. - Load a file to get a
Component(returned as a Promise). - Add a
Representationto that component. - Call
component.autoView()to center the camera on the data.
// create a `stage` object var stage = new NGL.Stage("viewport"); // load a PDB structure and consume the returned `Promise` stage.loadFile("rcsb://1CRN").then(function (component) { // add a "cartoon" representation to the structure component component.addRepresentation("cartoon"); // provide a "good" view of the structure component.autoView(); });Access molecular hierarchy via Structure objects
masterMolecular data in NGL is organized into a hierarchical model: Structure $\rightarrow$ Model $\rightarrow$ Chain $\rightarrow$ Residue $\rightarrow$ Atom. These hierarchies are represented by instances of theStructureclass. You can navigate this hierarchy to access specific components of a molecular system.Load Topology and Trajectory files
masterTopologies
Topology files contain connectivity information but no coordinate data. Supported formats:
- PRMTOP/PARM7 (
.prmtop,.parm7): Amber - PSF (
.psf): Charmm - TOP (
.top): Gromacs
Trajectories
Trajectory files can be loaded alongside structure files. If a structure file (mmCIF, PDB, GRO, SDF, MOL2, or MMTF) is loaded with the
asTrajectoryflag, it can be combined with these binary trajectory formats:- DCD (
.dcd): Charmm - TRR (
.trr): Gromacs - NCTRAJ (
.nctraj,.ncdf,.nc): Amber - XTC (
.xtc): Gromacs
- PRMTOP/PARM7 (
Release a new version of NGL
masterWhen releasing a new version, follow semantic versioning and ensure
CHANGELOG.mdis updated.- Update version and build: Use
npm version [level](e.g.,patch,minor,major,prerelease) to update the version number and create a dist build. This also tags and pushes to GitHub. - Publish to npm:
- For standard releases:
npm publish - For prereleases:
npm publish --tag next(this prevents prereleases from being installed by default by users).
- For standard releases:
- Finalize: For non-prerelease versions, manually update
README.mdandCHANGELOG.md, and create a GitHub release containing the relevant changelog information.
# Update version and tag (e.g., patch) npm version patch # Publish standard release npm publish # Publish prerelease npm publish --tag next- Update version and build: Use
Embed and control NGL via JavaScript API
masterDevelopers can embed the NGL viewer within other web pages and control the visualization using a JavaScript API. For detailed instructions on how to integrate the viewer into your project, refer to the embedding guide.Render large-scale geometries using SphereBuffer
masterFor high-performance or large-scale rendering (e.g., thousands of spheres), use the low-level
Bufferapproach. This involves creating aNGL.SphereBufferobject with typed arrays for positions, colors, and radii, then adding that buffer to anNGL.Shapeobject.This method is more scalable than the convenience
addSpheremethod because it uses contiguous memory viaFloat32Arrayto define many objects at once.var shape = new NGL.Shape( "shape" ); var sphereBuffer = new NGL.SphereBuffer( { position: new Float32Array( [ 0, 0, 0, 4, 0, 0 ] ), color: new Float32Array( [ 1, 0, 0, 1, 1, 0 ] ), radius: new Float32Array( [ 1, 1.2 ] ) } ); shape.addBuffer( sphereBuffer ); var shapeComp = stage.addComponentFromObject( shape ); shapeComp.addRepresentation( "buffer" ); shapeComp.autoView();Use Surface representation for volumetric or mesh data
masterTheSurfaceRepresentationallows you to show a triangulation of volume data at a specific isolevel or to directly display a surface mesh. This is useful for visualizing the boundary of a volume or rendering existing mesh geometries.Build the NGL Viewer for development or distribution
masterThe project uses
npmfor build management. You can generate different builds depending on your needs:- Development build: Creates a non-minified version at
build/js/ngl.dev.js. - Distribution build: Creates a minified version at
dist/ngl.js. - Watch mode: Automatically rebuilds when source files change.
- Documentation: Builds the project documentation.
Ensure you have run
npm installto install necessary dependencies before building.# Install dependencies npm install # Build non-minified development version npm run-script build # Build minified distribution version npm run-script build-min # Watch for changes and rebuild automatically npm run-script watch # Build documentation npm run-script doc- Development build: Creates a non-minified version at
Serve examples using a webserver
masterThe files in the
examples/directory must be served via a webserver to function correctly. You can use any of the following simple development servers:- Python 3:
python -m http.server - Python 2:
python -m SimpleHTTPServer - Node.js:
http-server(requires installation via npm)
# Python 3 python -m http.server # Python 2 python -m SimpleHTTPServer- Python 3: