mchem
Core
CLI entry point: convert PDB to SQLite DB, solvate PDB, etc.
System of force terms and particles; load/save from SQLite DB.
- class mchem.system.Box(a: float, b: float, c: float, alpha: float, beta: float, gamma: float)[source]
Bases:
objectPeriodic box dimensions from PDB CRYST1: cell lengths a, b, c (Angstroms) and angles alpha, beta, gamma (degrees).
- mchem.system.register_term_class(cls)[source]
Register a custom term dataclass so it can be deserialized from a database file. Call this before loading a
.dbthat contains the class.
- class mchem.system.System(path: PathLike | None = None)[source]
Bases:
objectContainer for force-field terms and metadata, backed by SQLite.
Terms are stored by table name (e.g.
Particle,AmoebaBond). Metadata is stored in a separatemetatable. Useregister_term_class()to register custom term dataclasses before loading a DB that contains them.- __init__(path: PathLike | None = None)[source]
Create an empty system or load from an existing SQLite DB.
- Parameters:
path (os.PathLike, optional) – If given, path to SQLite file to load; otherwise an empty system.
- property data
Mapping of table name to
TermListof terms.
- property meta
Mapping of metadata keys to values (e.g. name, units).
- addTerm(term, name: str | None = None)[source]
Append a single term; use its class name as table name if name is not given.
- addMeta(key: str, value: Any)[source]
Set a metadata key (hyphens in key are replaced with underscores).
Residue templates (atoms and bonds) loaded from XML; used for topology matching.
- class mchem.template.ResidueTemplate(name: str, atoms: Dict[str, Dict[str, Any]], bonds: List[Dict[str, Any]], altNames: List[str] = [])[source]
Bases:
objectTemplate for a residue: atom names (with alternates) and bond list for matching.
- __init__(name: str, atoms: Dict[str, Dict[str, Any]], bonds: List[Dict[str, Any]], altNames: List[str] = [])[source]
- classmethod fromXMLData(data: Element)[source]
Build a
ResidueTemplatefrom an XMLResidueelement (name, Atom, Bond children).
- mchem.template.loadTemplateDefinitions(fname: PathLike)[source]
Load residue templates from an XML file and add them to
TEMPLATES.- Parameters:
fname (os.PathLike) – Path to XML file containing
Residueelements.
Molecular topology: residues, atoms, bonds, and connectivity.
- class mchem.topology.Residue(name: str, number: int, chain: str = 'A', insertionCode: str = '')[source]
Bases:
objectA residue (e.g. amino acid) containing atoms and optional link to a
Topology.- property topology
- property numAtoms
- property atoms
- matchTemplate(template: ResidueTemplate, stdResidueName: bool = True)[source]
Match this residue to a template: align atom names (including alternates) and add template bonds to topology.
- Parameters:
template (ResidueTemplate) – Template to match (atom names and bonds).
stdResidueName (bool, optional) – If True, set residue name to template name.
- Returns:
True if match succeeded.
- Return type:
- class mchem.topology.Atom(name: str, element: str)[source]
Bases:
objectAn atom with element, position, optional residue/topology, bonds, and force-field type/class.
- property polarizationGroup
- property atomType
- property atomClass
- property parametrized
- property residue
- property topology
- property bonds
- property pathsToBondedAtoms
- property bondedAtoms
- class mchem.topology.Bond(atom1: Atom, atom2: Atom, order: float)[source]
Bases:
objectA bond between two atoms with a bond order.
- class mchem.topology.BondedAtoms(atoms: List[Atom])[source]
Bases:
objectOrdered list of bonded atoms, with hash/equality invariant under reversal (for undirected paths).
- class mchem.topology.Topology(name: str, maxConnect: int = 5)[source]
Bases:
objectMolecular topology: list of residues, bonds, and connectivity up to
_maxConnectbonds.- property editable
- property numResidues
- property numAtoms
- property numBonds
- property bonds
- property connTable
- property bondedAtoms
Solvation: add water and ions to a solute in a periodic box.
Uses Packmol as the external placement engine and computes box dimensions
following OpenMM’s openmm.app.Modeller.addSolvent() conventions.
- mchem.solvate.BoxShape
Allowed periodic box shapes.
alias of
Literal[‘cube’, ‘dodecahedron’, ‘octahedron’]
- mchem.solvate.solvate(topology: Topology, positions: ndarray, *, box_shape: Literal['cube', 'dodecahedron', 'octahedron'] = 'cube', buffer: float = 10.0, neutralize: bool = True, ionic_strength: float = 0.0, positive_ion: str = 'Na+', negative_ion: str = 'Cl-') Tuple[Topology, ndarray, Tuple[ndarray, ndarray, ndarray]][source]
Solvate a solute with water and optional ions in a periodic box.
- Parameters:
topology (Topology) – Solute topology (no solvent).
positions (np.ndarray) – Atom positions, shape
(N, 3), in Angstroms.box_shape (BoxShape, optional) – Periodic box shape. Default is
"cube".buffer (float, optional) – Minimum padding in Angstroms between solute and box edge. Default is
10.0.neutralize (bool, optional) – If True, add counterions to neutralize the system. Default is True.
ionic_strength (float, optional) – Target ionic strength in mol/L. Default is
0.0.positive_ion (str, optional) – Positive ion type (e.g.
"Na+"). Default is"Na+".negative_ion (str, optional) – Negative ion type (e.g.
"Cl-"). Default is"Cl-".
- Returns:
(topology, positions, box_vectors)where box_vectors is a tuple of threenp.ndarrayvectors.- Return type:
- Raises:
ValueError – If an unsupported ion type is given.
RuntimeError – If Packmol fails.
Physical constants and unit conversion factors for energy and length.
Periodic table data: element symbol, mass, and name.
Force field
Force field XML loading and term generation (see ForceField, Generator).
Force field definition from XML: atom types, residues, and force generators.
- mchem.forcefield.base.str2bool(string)[source]
Convert string to bool (True/False, case-insensitive).
- mchem.forcefield.base.xmlele2str(xmlele: Element)[source]
Serialize XML element to string (unicode, stripped).
- class mchem.forcefield.base.AtomType(name: str, atomClass: str)[source]
Bases:
objectForce-field atom type with name and class.
- class mchem.forcefield.base.ForceField(*files)[source]
Bases:
objectLoad and apply a force field from one or more XML files.
Parses AtomTypes, Residues, and force elements (e.g. AmoebaBondForce); uses
Parsersto create generators that produce terms for aTopology.- __init__(*files)[source]
Load force field from one or more XML files.
- Parameters:
*files (str or os.PathLike) – XML paths; resolved via
processFileNames()if not found.
- processFileNames(files)[source]
Resolve file paths; search next to this module if not found. Returns list of paths.
- property generators
List of force generators (one per force type from XML).
- getGeneratorWithClass(generatorClass)[source]
Return the first generator that is an instance of generatorClass, or None.
- addGeneratorWithClass(generatorClass)[source]
Get or create a generator of the given class and return it.
- addAtomType(atomTypeElement: Element)[source]
Register an atom type from an XML
Typeelement (name, class).
- loadAtomTypeDefs()[source]
Map residue atom names to atom types from Residues section (templates must exist).
- assignAtomTypes(topology: Topology)[source]
Set atom type and class on each atom from residue templates.
- findAtomTypes(element: Element, numAtoms: int)[source]
Resolve type/class attributes to list of atom type name tuples (supports wildcards).
- createSystem(topology: Topology, **kwargs)[source]
Build a
Systemwith particles and all generator terms for the given topology.
- class mchem.forcefield.base.Generator(ff: ForceField, paramFields: List[str] = [], raiseError: bool = True)[source]
Bases:
objectBase class for a force generator: holds parameters keyed by atom type/SMIRKS and creates terms for a topology.
Force generators: create bonded/nonbonded terms from XML and topology.
- class mchem.forcefield.generators.AmoebaBondGenerator(ff)[source]
Bases:
GeneratorGenerator for AMOEBA bond terms (quartic) from AmoebaBondForce XML.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.AmoebaAngleGenerator(ff)[source]
Bases:
GeneratorGenerator for AMOEBA angle and in-plane angle terms from AmoebaAngleForce XML.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.AmoebaUreyBradleyGenerator(ff)[source]
Bases:
GeneratorGenerator for AMOEBA Urey-Bradley terms from AmoebaUreyBradleyForce XML.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.MultipoleGenerator(ff)[source]
Bases:
GeneratorGenerator for atomic multipoles from AmoebaMultipoleForce / MBUCBMultipoleForce XML.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.IsotropicPolarizationGenerator(ff)[source]
Bases:
GeneratorGenerator for isotropic polarizability terms from Polarize elements.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.AmoebaVdwGenerator(ff)[source]
Bases:
GeneratorGenerator for AMOEBA buffered 14-7 VdW terms from AmoebaVdwForce XML.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.AmoebaStretchBendGenerator(ff)[source]
Bases:
GeneratorGenerator for AMOEBA stretch-bend coupling from AmoebaStretchBendForce XML.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.AmoebaOutOfPlaneBendGenerator(ff)[source]
Bases:
GeneratorGenerator for AMOEBA out-of-plane bend terms from AmoebaOutOfPlaneBendForce XML.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.AmoebaPiTorsionGenerator(ff)[source]
Bases:
GeneratorGenerator for AMOEBA pi-torsion terms from AmoebaPiTorsionForce XML.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.PeriodicTorsionGenerator(ff)[source]
Bases:
GeneratorGenerator for periodic proper torsions from PeriodicTorsionForce XML.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.AmoebaTorsionTorsionGenerator(ff)[source]
Bases:
GeneratorGenerator for AMOEBA torsion-torsion (5-atom) terms and grids from AmoebaTorsionTorsionForce XML.
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.AnisotropicPolarizationGenerator(ff)[source]
Bases:
GeneratorGenerator for anisotropic polarizability from Polarize elements (MBUCB).
- static parseElement(element: Element, ff: ForceField)[source]
- class mchem.forcefield.generators.MBUCBChargePenetrationGenerator(ff)[source]
Bases:
GeneratorGenerator for MBUCB charge-penetration terms from ChargePenetration elements.
- static parseElement(element: Element, ff: ForceField)[source]
Terms
Force term dataclasses and TermList (bonded, nonbonded).
Base container for force-term lists keyed by term class.
- class mchem.terms.base.TermList(cls)[source]
Bases:
listList of force terms restricted to a single dataclass type.
Used by
mchem.system.Systemto store terms per table (e.g.AmoebaBond).- __init__(cls)[source]
- Parameters:
cls (type) – Dataclass type; only instances of this type can be appended.
- property cls
Term dataclass type for this list.
Bonded force terms: bonds, angles, Urey-Bradley, torsions, CMAP, AMOEBA-specific.
- class mchem.terms.bonded.HarmonicBond(p0: int, p1: int, b0: float, kb: float, paramIdx: int = -1)[source]
Bases:
objectClass for a bond with normal harmonic potential
- class mchem.terms.bonded.AmoebaBond(p0: int, p1: int, b0: float, kb: float, cubic: float, quartic: float, paramIdx: int = -1)[source]
Bases:
objectClass for a bond in AMOEBA force field, i.e. up to quartic polynomials
- class mchem.terms.bonded.HarmonicAngle(p0: int, p1: int, p2: int, th0: float, kth: float, paramIdx: int = -1)[source]
Bases:
objectClass for an angle with harmonic potential
- class mchem.terms.bonded.AmoebaAngle(p0: int, p1: int, p2: int, th0: float, kth: float, cubic: float, quartic: float, pentic: float, sextic: float, paramIdx: int = -1)[source]
Bases:
objectClass for an angle in AMOEBA force field (up to sextic polynomials)
- class mchem.terms.bonded.AmoebaAngleInPlane(p0: int, p1: int, p2: int, p3: int, th0: float, kth: float, cubic: float, quartic: float, pentic: float, sextic: float, paramIdx: int = -1)[source]
Bases:
objectClass for an in-plane angle in AMOEBA force field (up to sextic polynomials)
- class mchem.terms.bonded.AmoebaStretchBend(p0: int, p1: int, p2: int, th0: float, b01: float, b02: float, kb1: float, kb2: float, paramIdx: int = -1)[source]
Bases:
objectClass for stretch-bend coupling term in AMOEBA force field
- class mchem.terms.bonded.AmoebaUreyBradley(p0: int, p1: int, p2: int, r0: float, fc: float, paramIdx: int = -1)[source]
Bases:
objectClass for Urey-Bradley term used in AMOEBA force field
- class mchem.terms.bonded.AmoebaOutOfPlaneBend(p0: int, p1: int, p2: int, p3: int, fc: float, paramIdx: int = -1)[source]
Bases:
objectClass for out-of-plane bending term in AMOEBA force field
- class mchem.terms.bonded.PeriodicTorsion(p0: int, p1: int, p2: int, p3: int, phase1: float, phase2: float, phase3: float, phase4: float, phase5: float, phase6: float, k1: float, k2: float, k3: float, k4: float, k5: float, k6: float, paramIdx: int = -1)[source]
Bases:
objectPeriodic torsion (dihedral) term with up to six Fourier components.
- class mchem.terms.bonded.AmoebaStretchTorsion(p0: int, p1: int, p2: int, p3: int, k11: float, k12: float, k13: float, k21: float, k22: float, k23: float, k31: float, k32: float, k33: float, b01: float, b02: float, b03: float, phi01: float, phi02: float, phi03: float)[source]
Bases:
objectClass for AMOEBA stretch-torsion coupling term
- class mchem.terms.bonded.AmoebaAngleTorsion(p0: int, p1: int, p2: int, p3: int, k11: float, k12: float, k13: float, k21: float, k22: float, k23: float, th01: float, th02: float, phi01: float, phi02: float, phi03: float, paramIdx: int = -1)[source]
Bases:
objectClass for AMOEBA angle-torsion coupling term
- class mchem.terms.bonded.AmoebaPiTorsion(p0: int, p1: int, p2: int, p3: int, p4: int, p5: int, k: float, paramIdx: int = -1)[source]
Bases:
objectClass for pi-torsion term in Amoeba
- class mchem.terms.bonded.CMAPTable(phi: float, psi: float, ene: float, paramIdx: int = -1)[source]
Bases:
objectClass for a CMAP tabulated torsion-torsion coupling term
- class mchem.terms.bonded.CMAP(cmap: int, p0: int, p1: int, p2: int, p3: int, p4: int, p5: int, p6: int, p7: int)[source]
Bases:
objectClass for CMAP torsion-torsion coupling term
- class mchem.terms.bonded.AmoebaTorsionTorsionGrid(angle1: float, angle2: float, f: float, gridIdx: int)[source]
Bases:
objectClass for an AMOEBA torsion-torsion 2D energy grid point
- class mchem.terms.bonded.AmoebaTorsionTorsion(p0: int, p1: int, p2: int, p3: int, p4: int, gridIdx: int, nx: int, ny: int)[source]
Bases:
objectClass for an AMOEBA torsion-torsion coupling term (5-atom chain)
Non-bonded terms: particles, VdW, multipoles, polarization, MBUCB.
- class mchem.terms.nonbonded.MultipoleAxisType(value)[source]
Bases:
EnumAxis convention for multipole frame (ZThenX, Bisector, ZOnly, etc.).
- ZThenX = 0
- Bisector = 1
- ZBisect = 2
- ThreeFold = 3
- ZOnly = 4
- NoAxisType = 5
- LastAxisTypeIndex = 6
- class mchem.terms.nonbonded.Particle(idx: int, name: str, element: str, mass: float, resnum: int, resname: str, xx: float, xy: float, xz: float, vx: float = 0.0, vy: float = 0.0, vz: float = 0.0)[source]
Bases:
objectSingle particle: index, name, element, mass, residue info, position, optional velocity.
- class mchem.terms.nonbonded.AmoebaVdw147(idx: int, epsilon: float, sigma: float, parentIdx: int = -1, reduction: float = 1.0, paramIdx: int = -1)[source]
Bases:
objectAMOEBA buffered 14-7 VdW parameters (epsilon, sigma, reduction, optional parent).
- class mchem.terms.nonbonded.Multipole(idx: int, c0: float, dx: float, dy: float, dz: float, qxx: float, qxy: float, qxz: float, qyy: float, qyz: float, qzz: float, axistype: int, kz: int = -1, kx: int = -1, ky: int = -1, paramIdx: int = -1)[source]
Bases:
objectAtomic multipole (charge, dipole, quadrupole) and axis type / frame indices.
- class mchem.terms.nonbonded.IsotropicPolarization(idx: int, alpha: float, thole: float, grp: list, paramIdx: int = -1)[source]
Bases:
objectIsotropic polarizability (alpha, thole) and polarization group indices.
- class mchem.terms.nonbonded.AnisotropicPolarization(idx: int, alphaxx: float, alphaxy: float, alphaxz: float, alphayy: float, alphayz: float, alphazz: float, thole: float, grp: list = <factory>, paramIdx: int = -1)[source]
Bases:
objectAnisotropic polarizability tensor (alpha_xx, alpha_xy, …) and thole/group.
- class mchem.terms.nonbonded.MBUCBChargePenetration(idx: int, z: float, alpha: float, beta: float, paramIdx: int = -1)[source]
Bases:
objectMBUCB charge-penetration correction parameters (z, alpha, beta).
File formats
File format loaders (e.g. PDB) that produce mchem.topology.Topology.
PDB file I/O: read/write Topology from/to ATOM/HETATM records.
- mchem.fileformats.pdb.read_pdb_box(fname: PathLike) Tuple[float, float, float, float, float, float] | None[source]
Read periodic box dimensions from the first CRYST1 record in a PDB file.
- mchem.fileformats.pdb.load_pdb(fname: PathLike) Topology[source]
Load a topology from a PDB file (ATOM/HETATM), match residue templates, and return editable topology.
- Parameters:
fname (os.PathLike) – Path to the PDB file.
- Returns:
Topology with residues and atoms; templates are matched so bonds and standard names are set.
- Return type:
- mchem.fileformats.pdb.write_pdb(fname: PathLike, topology: Topology, positions: ndarray, box_vectors: Tuple[ndarray, ndarray, ndarray] | None = None) None[source]
Write a PDB file from a topology and positions.
- Parameters:
fname (os.PathLike) – Output PDB file path.
topology (Topology) – Molecular topology.
positions (np.ndarray) – Atom positions, shape
(N, 3), in Angstroms.box_vectors (tuple of np.ndarray, optional) – Three box vectors
(v1, v2, v3). If provided, aCRYST1record is written at the top of the file.