xyzrender
repository·main·Indexed 20 days ago
https://github.com/aligfellow/xyzrenderA 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.
What's inside xyzrender
- 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.
Overview of xyzrender
mainxyzrender 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, andgraph.
Explore xyzrender feature capabilities
mainxyzrender 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, andhaptic. - 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, orPaton-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.
- Presets: Various rendering styles like
Distinguish between load-time and render-time options
mainIn 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,rebuildmol_frame,bohr,quickts_detect(--ts),ts_framenci_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()orrender_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 callingload()with default settings. If you need custom parsing (likecharge=1), you must callload()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")Understand xyzrender key dependencies
mainxyzrenderrelies 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-pyis unavailable,xyzrenderfalls back toCairoSVGautomatically, 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.
Align multiple molecules using a reference orientation
mainTo ensure consistent orientation across a batch of different molecules (e.g., different orbitals or states), use the
ref=parameter inrender(). This uses Kabsch-alignment to align the current molecule to a saved reference.- Save a reference: Call
render()withref="path/to/ref.xyz". This saves the current orientation to that file. - Apply reference: Subsequent calls to
render()using the samerefpath will automatically align the molecule to that orientation.
Note: When a reference file is provided via
ref=, theorient=Trueparameter 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")- Save a reference: Call
Configure atom regions in presets
mainThe
regionskey 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 } }- Categories:
Auto-detection of periodic input formats
mainThe
xyzrendertool automatically detects periodic input formats based on file extensions and content:- VASP:
.vasp,POSCAR,CONTCAR - Quantum ESPRESSO (QE):
.in - ABINIT:
.in,.abi - SIESTA:
.fdf - CP2K:
.inp - extXYZ: Detects
Lattice=header in the comment line.
- VASP:
Expand periodic structures into supercells
mainYou 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 Lwhere M, N, and L are integers. - Python: Use the
supercell=(M, N, L)keyword argument inrender().
Ghost atoms represent the periodic images of the supercell, while the cell-box overlay always represents the original unit cell. The
--hy/--no-hyflags 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- CLI: Use
Configure bond orders (Aromatic vs Kekulé)
mainControl how bond orders are represented:
- Aromatic (Default): Uses aromatic notation.
- Kekulé: Use the
--bo -kflag to force Kekulé bond orders.
xyzrender benzene.xyz --hy # aromatic notation (default) xyzrender caffeine.xyz --bo -k # Kekulé bond ordersUnwrap molecules across periodic boundaries
mainIn molecular crystals, molecules are often split across the unit cell boundaries. The
unwrapfeature 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
unwrapkeyword inrender()or apply theunwrap_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)Skip Kabsch alignment in ensembles
mainIf you want to render each frame of a trajectory at its native, raw coordinates without any rotation or translation, use the
--no-alignflag. 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