API reference

Recover a ligand’s mol2 / frcmod / OFF lib from an Amber parm7 + rst7 pair.

exception unleap.UnleapError[source]

Bases: RuntimeError

Raised for user-facing failures (bad selection, unsupported topology, …).

unleap.extract_ligand(parm7, crd, *, mask=None, resname=None, frame=0, name=None, base='auto', undo_hmr=True, validate=True, workdir=None)[source]

Recover the mol2 / frcmod / OFF lib of one ligand residue in a built Amber system.

Parameters:
  • parm7 (str or path) – Amber topology.

  • crd (str or path) – Coordinates: ASCII rst7/inpcrd, NetCDF restart, or NetCDF trajectory (see frame).

  • mask (str, optional) – Amber mask or residue name selecting the ligand (at most one). If both are None, the single non-standard residue is auto-detected.

  • resname (str, optional) – Amber mask or residue name selecting the ligand (at most one). If both are None, the single non-standard residue is auto-detected.

  • frame (int) – Trajectory frame, 0-based; negative counts from the end.

  • name (str, optional) – Unit/residue name for the outputs (default: the original residue name).

  • base (str) – Base force field for the diff frcmod: “auto”, “gaff”, “gaff2”, “gaff211” or a .dat path.

  • undo_hmr (bool) – Detect and reverse hydrogen mass repartitioning.

  • validate (bool) – Rebuild the ligand with tleap and compare terms and sander energies to the original.

  • workdir (str or path, optional) – Scratch directory for the validation (kept). A temporary one is used if None.

Return type:

LigandParams

Raises:

UnleapError – For any user-facing failure (bad selection, unsupported topology, missing AmberTools).

unleap.extract_ligand_from_structure(struct, *, mask=None, resname=None, name=None, base='auto', undo_hmr=True, validate=True, workdir=None)[source]

Like extract_ligand(), for an already-loaded parmed.amber.AmberParm with coordinates (and box, if periodic). struct is not modified, so it can be reused, e.g. once per resname in a dual-topology system. After editing it through ParmEd’s object model, call struct.remake_parm() first.

class unleap.LigandParams(name: str, resname: str, mask: str, n_atoms: int, net_charge: float, frcmod: str, full_frcmod: str, lib: str, mol2: str, sdf: object, tleap_in: str, base: BaseFF, base_scores: dict, hmr_undone: bool, diff_counts: dict, checks: list = <factory>, validated: bool = False, warnings: list = <factory>, report: str = '', structure: object = None)[source]

Bases: object

Everything unleap recovers for one ligand. File bodies are strings; write() puts them on disk.

name: str
resname: str
mask: str
n_atoms: int
net_charge: float
frcmod: str
full_frcmod: str
lib: str
mol2: str
sdf: object
tleap_in: str
base: BaseFF
base_scores: dict
hmr_undone: bool
diff_counts: dict
checks: list
validated: bool = False
warnings: list
report: str = ''
structure: object = None
property ok

True when every validation check passed (vacuously True when not validated).

files()[source]

{file name: contents} for every text output.

write(outdir)[source]

Write the outputs to outdir (created if needed); returns {kind: Path}.

Kinds: frcmod, full_frcmod, lib, mol2, sdf (if any), tleap_in, and orig_parm7/orig_rst7 (the stripped original ligand) when the result was validated.

to_dict()[source]

JSON-able metadata (no file bodies).

class unleap.CheckResult(name: str, ok: bool, details: list = <factory>)[source]

Bases: object

One rebuild-and-compare check: details lists each mismatch (empty when ok).

name: str
ok: bool
details: list
to_dict()[source]
class unleap.BaseFF(name: str, dat: str, leap_preamble: str)[source]

Bases: object

A base force field the diff frcmod is written against.

name: str
dat: str
leap_preamble: str
load()[source]

Python API internals

Python API: extract a ligand’s parameters from a built system and get them back as data.

class unleap.api.LigandParams(name: str, resname: str, mask: str, n_atoms: int, net_charge: float, frcmod: str, full_frcmod: str, lib: str, mol2: str, sdf: object, tleap_in: str, base: BaseFF, base_scores: dict, hmr_undone: bool, diff_counts: dict, checks: list = <factory>, validated: bool = False, warnings: list = <factory>, report: str = '', structure: object = None)[source]

Bases: object

Everything unleap recovers for one ligand. File bodies are strings; write() puts them on disk.

name: str
resname: str
mask: str
n_atoms: int
net_charge: float
frcmod: str
full_frcmod: str
lib: str
mol2: str
sdf: object
tleap_in: str
base: BaseFF
base_scores: dict
hmr_undone: bool
diff_counts: dict
checks: list
validated: bool = False
warnings: list
report: str = ''
structure: object = None
property ok

True when every validation check passed (vacuously True when not validated).

files()[source]

{file name: contents} for every text output.

write(outdir)[source]

Write the outputs to outdir (created if needed); returns {kind: Path}.

Kinds: frcmod, full_frcmod, lib, mol2, sdf (if any), tleap_in, and orig_parm7/orig_rst7 (the stripped original ligand) when the result was validated.

to_dict()[source]

JSON-able metadata (no file bodies).

unleap.api.pick_base(spec, full)[source]

Return (BaseFF, params, {name: score}).

unleap.api.extract_ligand(parm7, crd, *, mask=None, resname=None, frame=0, name=None, base='auto', undo_hmr=True, validate=True, workdir=None)[source]

Recover the mol2 / frcmod / OFF lib of one ligand residue in a built Amber system.

Parameters:
  • parm7 (str or path) – Amber topology.

  • crd (str or path) – Coordinates: ASCII rst7/inpcrd, NetCDF restart, or NetCDF trajectory (see frame).

  • mask (str, optional) – Amber mask or residue name selecting the ligand (at most one). If both are None, the single non-standard residue is auto-detected.

  • resname (str, optional) – Amber mask or residue name selecting the ligand (at most one). If both are None, the single non-standard residue is auto-detected.

  • frame (int) – Trajectory frame, 0-based; negative counts from the end.

  • name (str, optional) – Unit/residue name for the outputs (default: the original residue name).

  • base (str) – Base force field for the diff frcmod: “auto”, “gaff”, “gaff2”, “gaff211” or a .dat path.

  • undo_hmr (bool) – Detect and reverse hydrogen mass repartitioning.

  • validate (bool) – Rebuild the ligand with tleap and compare terms and sander energies to the original.

  • workdir (str or path, optional) – Scratch directory for the validation (kept). A temporary one is used if None.

Return type:

LigandParams

Raises:

UnleapError – For any user-facing failure (bad selection, unsupported topology, missing AmberTools).

unleap.api.extract_ligand_from_structure(struct, *, mask=None, resname=None, name=None, base='auto', undo_hmr=True, validate=True, workdir=None)[source]

Like extract_ligand(), for an already-loaded parmed.amber.AmberParm with coordinates (and box, if periodic). struct is not modified, so it can be reused, e.g. once per resname in a dual-topology system. After editing it through ParmEd’s object model, call struct.remake_parm() first.

Command line

unleap: strip a ligand out of a parm7/rst7 pair and regenerate its mol2, frcmod and lib.

unleap.cli.build_parser()[source]
unleap.cli.run(args)[source]
unleap.cli.main(argv=None)[source]

Selection

Choose the ligand residue: explicit mask, residue name, or auto-detect.

unleap.selection.ligand_mask(struct, mask=None, resname=None)[source]

Return an Amber mask string selecting the ligand.

Extraction

Load the system, strip out the ligand, and check it can be expressed as mol2/frcmod/lib.

class unleap.extract.Ligand(struct: object, lig: object, mask: str, hmr: bool = False, masses: list = <factory>, warnings: list = <factory>)[source]

Bases: object

struct: object
lig: object
mask: str
hmr: bool = False
masses: list
warnings: list
property resname
unleap.extract.read_coordinates(path, frame=0)[source]

(xyz, box) from an ASCII rst7/inpcrd or one frame of a NetCDF restart/trajectory.

Reads the file directly: ParmEd’s format sniffing rejects rst7 headers that carry a temperature (pmemd/REMD restarts), and loading a whole trajectory just for one frame is wasteful.

unleap.extract.load_parm(parm7)[source]
unleap.extract.load(parm7, crd, frame=0)[source]
unleap.extract.check_parm(struct)[source]
unleap.extract.strip(struct, mask)[source]

Extract the ligand and run the topology sanity checks. struct is not modified.

The ligand is stripped from copy.copy(struct), which AmberParm rebuilds from parm_data; after editing a structure through the object model, call struct.remake_parm() first.

unleap.extract.undo_hmr(ligand, base_params=None)[source]

Detect hydrogen mass repartitioning and restore the original masses in ligand.masses.

Each H gets its type’s mass from the base force field (or 1.008); the mass it carried beyond that is returned to the heavy atom it is bonded to, which reverses HMR exactly.

unleap.extract.check_type_consistency(ligand)[source]

Each atom type must map to one mass/LJ pair for a frcmod to describe it.

frcmod

Build the full (self-contained) and diff-vs-base frcmod parameter sets.

unleap.frcmod.improper_atoms(d)[source]

Improper atoms ordered with the central atom third (frcmod convention).

tleap may store an improper reversed in the prmtop (central atom second); a reversed torsion is the same angle, so reverse it back. The central atom is the one bonded to the other three.

unleap.frcmod.full_params(ligand)[source]

Every parameter the ligand uses, with masses taken from ligand.masses (HMR undone).

unleap.frcmod.lookup_dihedral(base, key)[source]

tleap order: explicit types (either direction), then X-b-c-X.

unleap.frcmod.lookup_improper(base, key)[source]

Central atom is third; outer atoms may be permuted; most specific match wins.

unleap.frcmod.same_dihedral(x, y, check_14=True)[source]
unleap.frcmod.compare_to_base(full, base)[source]

Return {section: [(key, value), …]} of ligand terms that the base does not reproduce.

unleap.frcmod.count_terms(full)[source]
unleap.frcmod.score_base(full, base)[source]

Fraction of the ligand’s bonded terms the base reproduces (masses ignored: HMR may alter them).

unleap.frcmod.diff_params(full, base)[source]

Parameter set holding only the terms missing from, or different in, the base.

unleap.frcmod.render(pset, title)[source]

frcmod text, modeled on ParmEd’s writer but keeping prmtop precision (ParmEd rounds req/theteq to 3 decimals, which breaks GAFF2’s 4-decimal bond lengths) and the prmtop improper order.

unleap.frcmod.write(pset, path, title)[source]
unleap.frcmod.LEAP_DEG_CORRECTION

Convert a string or number to a floating-point number, if possible.

OFF library

Write the ligand as an Amber OFF library unit.

unleap.lib.render(ligand, name)[source]

OFF library text holding one unit, name.

unleap.lib.write(ligand, path, name)[source]

mol2 / SDF

TRIPOS mol2 writer: Amber atom types, exact charges, RDKit-inferred bond orders.

unleap.mol2.bond_orders(lig)[source]

Return ({(i, j): order} with i < j, kekulized, RDKit mol); (None, None) if RDKit cannot assign them.

unleap.mol2.render_sdf(mol, ligand, name)[source]

SDF with bond orders and formal charges: the RDKit/viewer-friendly copy (RDKit’s mol2 reader only understands SYBYL types, not GAFF).

unleap.mol2.write_sdf(mol, ligand, path, name)[source]
unleap.mol2.render(ligand, name)[source]

(mol2 text, SDF text); the SDF is None when RDKit could not assign bond orders, in which case the mol2 bonds are all single.

unleap.mol2.write(ligand, path, name, sdf_path=None)[source]

Write the mol2 (and SDF, if bond orders were assigned); returns False when bond orders fell back to all-single.

Validation

Round-trip check: rebuild the ligand with tleap from the outputs and compare to the original.

class unleap.validate.CheckResult(name: str, ok: bool, details: list = <factory>)[source]

Bases: object

One rebuild-and-compare check: details lists each mismatch (empty when ok).

name: str
ok: bool
details: list
to_dict()[source]
unleap.validate.save_stripped(struct, prefix)[source]

Write the stripped original ligand (non-periodic) for comparison.

unleap.validate.compare_terms(orig, rebuilt, masses)[source]

Term-by-term comparison keyed by atom indices. Returns a list of mismatch strings.

unleap.validate.compare_energies(e_orig, e_new)[source]
unleap.validate.run(ligand, name, preamble, texts, workdir)[source]

Rebuild three ways in workdir and compare each to the stripped original.

texts maps “frcmod”, “full”, “lib” and “mol2” to the rendered file contents. Returns ([CheckResult], original energies).

AmberTools helpers

AmberTools environment helpers: base force fields, tleap and sander runners.

class unleap.amber.BaseFF(name: str, dat: str, leap_preamble: str)[source]

Bases: object

A base force field the diff frcmod is written against.

name: str
dat: str
leap_preamble: str
load()[source]
unleap.amber.amberhome()[source]
unleap.amber.parm_dir()[source]
unleap.amber.preset_bases()[source]

All preset base force fields available in this AMBERHOME.

unleap.amber.resolve_base(spec)[source]

Resolve a –base value (preset name or .dat path); ‘auto’ returns None.

unleap.amber.run_tleap(script, workdir, tag='tleap')[source]

Run a tleap script in workdir; return the log text. Raises on leap errors.

unleap.amber.sander_energy(parm7, rst7, workdir, tag)[source]

Single-point vacuum energy decomposition {term: kcal/mol} via sander.