NGL Viewer

repository·master·Indexed 20 days ago

https://github.com/nglviewer/ngl

A 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).

Tokens
13.4K
Snippets
40
Records
72
Agent score
69%

What's inside NGL

  1. Overview of NGL Viewer

    master
    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.
  2. Overview of NGL Viewer features

    master

    NGL 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.
  3. Overview of available Molecular Representations

    master

    NGL 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 of ball+stick where aspectRatio is fixed to 1.0.
    • cartoon: Smooth trace representing secondary structure.
    • backbone: Cylinders connecting successive residues via main backbone atoms (.CA for 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 to cartoon but with aspectRatio fixed at 1.0.
    • surface: Molecular surfaces (vws, sas, ms, ses, av).
    • base: Simplified RNA/DNA nucleotide display (best used with cartoon).
    • 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.
  4. How NGL Stage, Components, and Representations work together

    master

    The NGL viewer follows a hierarchical model:

    1. Stage: The main entry point and container for the viewer. It manages the viewport and the lifecycle of loaded data.
    2. Component: Created via stage.loadFile(). A component contains the actual structural or volume data (e.g., a PDB structure).
    3. Representation: Added to a Component to define how the data is visually rendered (e.g., "cartoon", "surface", or "ball+stick").

    Example workflow:

    1. Create a Stage.
    2. Load a file to get a Component (returned as a Promise).
    3. Add a Representation to that component.
    4. 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();
    });
  5. Access molecular hierarchy via Structure objects

    master
    Molecular 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 the Structure class. You can navigate this hierarchy to access specific components of a molecular system.
  6. Load Topology and Trajectory files

    master

    Topologies

    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 asTrajectory flag, it can be combined with these binary trajectory formats:

    • DCD (.dcd): Charmm
    • TRR (.trr): Gromacs
    • NCTRAJ (.nctraj, .ncdf, .nc): Amber
    • XTC (.xtc): Gromacs
  7. Release a new version of NGL

    master

    When releasing a new version, follow semantic versioning and ensure CHANGELOG.md is updated.

    1. 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.
    2. Publish to npm:
      • For standard releases: npm publish
      • For prereleases: npm publish --tag next (this prevents prereleases from being installed by default by users).
    3. Finalize: For non-prerelease versions, manually update README.md and CHANGELOG.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
  8. Embed and control NGL via JavaScript API

    master
    Developers 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.
  9. Render large-scale geometries using SphereBuffer

    master

    For high-performance or large-scale rendering (e.g., thousands of spheres), use the low-level Buffer approach. This involves creating a NGL.SphereBuffer object with typed arrays for positions, colors, and radii, then adding that buffer to an NGL.Shape object.

    This method is more scalable than the convenience addSphere method because it uses contiguous memory via Float32Array to 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();
  10. Build the NGL Viewer for development or distribution

    master

    The project uses npm for 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 install to 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
  11. Serve examples using a webserver

    master

    The 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