xyzrender

repository·main·Indexed 20 days ago

https://github.com/aligfellow/xyzrender

A high-fidelity molecular graphics engine for producing publication-quality vector (SVG) and raster (PNG, PDF, GIF) images. It supports a wide range of chemical file formats including XYZ, PDB, SMILES, CIF, and quantum chemistry outputs from Gaussian, ORCA, and NWChem. Key features include automatic detection of bond orders and aromaticity, rendering of molecular orbitals and electron density, structural overlays, and animations for rotation and trajectories.

Tokens
57.4K
Snippets
185
Records
236
Agent score
68%

What's inside xyzrender

  1. Overview of xyzrender capabilities

    main
    xyzrender is a tool for generating publication-quality molecular graphics from various chemical data formats, including XYZ, cube, and QM output files. It supports multiple output formats such as SVG, PNG, PDF, and animated GIF. The tool can be used via a Command Line Interface (CLI) or a Python API to render complex molecular features like NCI surfaces, vdW spheres, and transition state (TS) bonds.
  2. Overview of xyzrender

    main

    xyzrender is a tool for generating publication-quality molecular graphics in SVG, PNG, PDF, and animated GIF formats. It supports a wide range of input formats including XYZ, mol/SDF, MOL2, PDB, SMILES, CIF, SHELXL, cube files, and various quantum chemistry input/output files (Gaussian, ORCA, NWChem, etc.).

    Key features include:

    • Automatic Detection: Bond orders, aromaticity, transition state (TS) bonds, stereochemistry labels, and non-covalent interactions (e.g., hydrogen bonds).
    • Advanced Rendering: Molecular orbitals, electron density, vdW spheres, convex hulls, and pore detection.
    • Animations: Rotation, TS vibration, trajectory, and depth-of-field animations.
    • Structural Overlays: Overlaying two structures with automatic alignment or manual atom alignment.
    • Presets: Built-in styles like default, flat, paton, pmol, skeletal, bubble, vdw, tube, mtube, btube, wire, and graph.
  3. Explore xyzrender feature capabilities

    main

    xyzrender supports a wide range of publication-quality molecular graphics, including:

    • Presets: Various rendering styles like Default, Flat, Paton (PyMOL-like), Pmol, Skeletal, Bubble, Tube, BTube, Wire, Graph, MTube, vdW, and haptic.
    • Style Regions: Defining specific regions (e.g., Tube + ball-stick) and combining them with NCI or vdW.
    • Display Options: Controlling hydrogen visibility (All H, Some H, No H), aromaticity, and Kekule structures.
    • vdW Spheres: Rendering Van der Waals spheres in All atoms, Partial, or Paton-style.
    • Convex Hull: Generating hulls for rings (Benzene, Anthracene) or automatic ring detection.
    • Hull Faces & Pore Detection: Visualizing faces and pores for structures like Buckyballs or MOFs.
    • Highlight & Color: Custom molecule coloring, multi-group highlighting, and per-atom scaling (radius/opacity).
    • Depth of Field & Glow: Adding cinematic effects like DoF and atom-specific glows.
    • Structural Overlays & Ensembles: Overlaying molecules with custom colors or rendering ensembles using spectral, CPK, or viridis color maps.
    • Transition States (TS) & NCI: Automatic or manual TS detection, Non-Covalent Interaction (NCI) surfaces, and QM output integration.
    • Annotations: Distances, angles, dihedrals, and custom labels.
    • Stereochemistry: Labels for R/S, E/Z, and planar configurations.
    • Atom Property Colormaps: Mapping properties like Mulliken charges to colors.
    • Surfaces: Rendering Molecular Orbitals (HOMO, LUMO), Electron Density, ESP (Electrostatic Potential), and NCI surfaces from cube files.
    • Crystal/Periodic Structures: Unit cells, unwrapped structures, and VASP support.
    • Animations: Generating GIFs for rotation, bouncing, trajectories, and transitions.

    For specific commands and Python snippets for any of these features, refer to the official examples documentation. For a full list of CLI flags, see the CLI reference.

  4. Distinguish between load-time and render-time options

    main

    In the Python API, options are strictly categorized based on when they are applied. Mixing them up will result in errors or ignored parameters.

    Load-time options (load())

    These options control how the input file is parsed and interpreted. They must be passed to the load() function. Examples include:

    • smiles, charge, multiplicity, kekule, rebuild
    • mol_frame, bohr, quick
    • ts_detect (--ts), ts_frame
    • nci_detect (--nci-detect)
    • cell (--cell)
    • All ensemble* options (e.g., --ensemble, --ensemble-color, --align-atoms, --max-frames)

    Render-time options (render() / render_gif())

    These options control the visual appearance of the molecule. They must be passed to the render() or render_gif() functions. Examples include:

    • Styling (colors, backgrounds)
    • Bond display rules
    • Surfaces and overlays
    • Hulls and annotations
    • Output format/path

    Note: If you use the shorthand render("path.xyz", ...) you are implicitly calling load() with default settings. If you need custom parsing (like charge=1), you must call load() explicitly first.

    # Correct way to use both types of options:
    # Use load() for parsing logic
    mol = load("ts.out", charge=1, ts_detect=True) 
    
    # Use render() for visual logic
    render(mol, mo=True, background="black")
  5. Understand xyzrender key dependencies

    main

    xyzrender relies on several core libraries for molecular analysis and rendering:

    • xyzgraph: Handles bond connectivity, bond orders, aromaticity detection, and non-covalent interactions.
    • graphRC: Used for reaction coordinate analysis and TS bond detection.
    • cclib: Parses quantum chemistry output files (e.g., ORCA, Gaussian, Q-Chem).
    • resvg-py: Performs SVG to PNG conversion while preserving SVG effects.
    • CairoSVG: Used as a fallback for SVG to PNG/PDF conversion. If resvg-py is unavailable, xyzrender falls back to CairoSVG automatically, though filters may be silently ignored in the resulting image (the SVG output itself will still contain the filters).
    • Pillow: Used for GIF frame assembly.
  6. Align multiple molecules using a reference orientation

    main

    To ensure consistent orientation across a batch of different molecules (e.g., different orbitals or states), use the ref= parameter in render(). This uses Kabsch-alignment to align the current molecule to a saved reference.

    1. Save a reference: Call render() with ref="path/to/ref.xyz". This saves the current orientation to that file.
    2. Apply reference: Subsequent calls to render() using the same ref path will automatically align the molecule to that orientation.

    Note: When a reference file is provided via ref=, the orient=True parameter is ignored.

    from xyzrender import load, render
    
    # First call: saves the orientation to 'reference.xyz'
    mol1 = load("homo.cube")
    render(mol1, mo=True, ref="reference.xyz")
    
    # Subsequent calls: aligns mol2 to the orientation in 'reference.xyz'
    mol2 = load("lumo.cube")
    render(mol2, mo=True, ref="reference.xyz")
  7. Configure atom regions in presets

    main

    The regions key in a JSON preset allows you to define per-atom-group style overrides. You can target atoms using:

    • Categories: M (metals), sbm (s-block metals), het (heteroatoms), hal (halogens), pnic (pnictogens), chal (chalcogens), noble (noble gases), triel (group 13), tetrel (group 14).
    • Element symbols: e.g., "Pt", "Fe".
    • Numeric indices: e.g., "1-5".
    • Combinations: e.g., "hal,chal".

    Values can be a preset name or an inline dictionary of overrides.

    "regions": {
      "M": "flat",
      "het": { "atom_scale": 3.0, "gradient": true }
    }
  8. Expand periodic structures into supercells

    main

    You can expand a unit cell into a larger supercell by specifying the number of repetitions along the lattice vectors (a, b, c).

    • CLI: Use --supercell M N L where M, N, and L are integers.
    • Python: Use the supercell=(M, N, L) keyword argument in render().

    Ghost atoms represent the periodic images of the supercell, while the cell-box overlay always represents the original unit cell. The --hy / --no-hy flags can be used to control hydrogen ghost atoms.

    # Expand to a 2x2x1 supercell
    xyzrender caffeine_cell.xyz --supercell 2 2 1 -o caffeine_cell_supercell_221.svg
  9. Unwrap molecules across periodic boundaries

    main

    In molecular crystals, molecules are often split across the unit cell boundaries. The unwrap feature reassembles these fragments into single, contiguous molecules by shifting atoms by integer lattice translations.

    • Behavior: It anchors every molecule at the image where most of its atoms reside.
    • Side effects: Ghost atoms are automatically disabled when unwrapping because the molecules are now contiguous. Fully-connected frameworks (ionic or covalent networks) are left unchanged.

    Python Usage: You can use the unwrap keyword in render() or apply the unwrap_molecules() transform directly to the molecule's graph.

    from xyzrender import load, render
    from xyzrender.crystal import unwrap_molecules
    
    mol = load("caffeine_cell.xyz", cell=True)
    
    # Option 1: Unwrap during rendering
    render(mol, unwrap=True, output="caffeine_cell_unwrap.svg")
    
    # Option 2: Apply transform directly to the graph
    unwrap_molecules(mol.graph, mol.cell_data)
  10. Skip Kabsch alignment in ensembles

    main

    If you want to render each frame of a trajectory at its native, raw coordinates without any rotation or translation, use the --no-align flag. This is useful when the absolute geometry and orientation of the trajectory frames are important for your visualization.

    CLI Example

    xyzrender triphenylbenzol.xyz --ensemble --no-align -o ensemble_raw.svg