Conformer
API¶
rdworks.conf.Conf
¶
Container for 3D conformers.
Source code in src/rdworks/conf.py
class Conf:
"""Container for 3D conformers."""
_rdworks_kind = "conf" # dispatch key for rdworks.io.writers
def __init__(
self,
molecule: str | Chem.Mol | None = None,
name: str = "",
compressed: bool = False,
) -> None:
"""Initialize.
Args:
molecule (Chem.Mol | MolBlock string): Molecule for 3D conformer.
name (str): Name prefix of the generated conformers. Defaults to ''.
compressed (bool): whether the MolBlock string is compressed or not.
Defaults to False.
Raises:
ValueError: if `molecule` is an invalid MolBlock or does not have
exactly one conformer.
"""
assert isinstance(molecule, str | Chem.Mol) or molecule is None
self.name = name
self.rdmol = None # must contain one and only one rdkit conformer
self.natoms = 0
self.charge = 0 # molecular formal charge
self.spin = 1 # molecular spin multiplicity for ASE
# Molecular spin multiplicity describes the number of possible orientations of
# spin angular momentum for a given molecule, essentially indicating the total
# number of unpaired electrons.
# Spin Angular Momentum (S): up (+1/2) or down (-1/2)
# Spin Multiplicity (2S + 1)
# 0 unpaired electron has S = 0, 2S + 1 = 0, called a singlet.
# 1 unpaired electron has S = 1/2, 2S + 1 = 2, called a doublet (radical).
# 2 unpaired electrons has S = 1, 2S + 1 = 3, called a triplet.
self.smiles = ""
self.symbols = []
self.numbers = []
self.props = {}
# `positions` and `molblock` are properties computed from `rdmol`, so they
# always reflect the current coordinates (after optimize, sync, torsion changes)
if molecule is None:
return
if isinstance(molecule, str): # 3-D MolBLock string
if compressed:
molecule = rdworks.utils.decompress_string(molecule)
try:
self.rdmol = Chem.MolFromMolBlock(
molecule, sanitize=False, removeHs=False, strictParsing=True
)
except (TypeError, ValueError, RuntimeError) as e:
raise ValueError("Conf() Error: invalid MolBlock string") from e
if self.rdmol is None:
raise ValueError("Conf() Error: invalid MolBlock string")
elif isinstance(molecule, Chem.Mol): # 3-D
# copy, so later coordinate changes do not modify the caller's molecule
self.rdmol = Chem.Mol(molecule)
if self.rdmol.GetNumConformers() != 1:
raise ValueError(
"Conf() Error: expects exactly one conformer, "
f"got {self.rdmol.GetNumConformers()}"
)
if name:
self.rdmol.SetProp("_Name", name)
self._update()
def _update(self) -> None:
self.smiles = Chem.MolToSmiles(self.rdmol)
self.symbols = [a.GetSymbol() for a in self.rdmol.GetAtoms()]
self.numbers = [a.GetAtomicNum() for a in self.rdmol.GetAtoms()]
# check hydrogens
num_atoms = self.rdmol.GetNumAtoms()
tot_atoms = self.rdmol.GetNumAtoms(onlyExplicit=False)
assert num_atoms == tot_atoms, "Conf() Error: missing hydrogens"
self.natoms = num_atoms
self.charge = rdmolops.GetFormalCharge(self.rdmol)
self.props.update(
{
"atoms": self.natoms,
"charge": self.charge,
}
)
assert self.rdmol.GetConformer().Is3D(), "Conf() Error: not 3D"
def __str__(self) -> str:
"""Returns a string representation.
Returns:
str: string representation.
"""
return f"<rdworks.Conf({self.smiles} name={self.name} atoms={self.natoms})>"
##################################################
### Properties
##################################################
@property
def positions(self) -> np.ndarray:
"""Returns current atomic coordinates (natoms, 3) in Angstrom."""
if self.rdmol is None:
return np.array([])
return self.rdmol.GetConformer().GetPositions()
@property
def molblock(self) -> str:
"""Returns the MolBlock of the current coordinates."""
if self.rdmol is None:
return ""
return Chem.MolToMolBlock(self.rdmol)
@property
def COG(self) -> np.array:
"""Returns the center of geometry (COG).
Returns:
np.array: the center of geometry.
"""
return np.mean(self.positions, axis=0)
@property
def Rg(self) -> float:
"""Returns the radius of gyration (Rg).
Returns:
float: the radius of gyration.
"""
xyz = self.positions
cog = np.mean(xyz, axis=0)
a = xyz - cog
b = np.einsum("ij,ij->i", a, a)
return np.sqrt(np.mean(b))
@property
def SASA(self) -> dict:
"""Calculate Solvent Accessible Surface Area (total, polar, apolar).
Returns:
tuple[float, float, float]: (total, polar, apolar)
"""
radii = [ptable.GetRvdw(atom.GetAtomicNum()) for atom in self.rdmol.GetAtoms()]
# CalcSASA signature: CalcSASA(mol, radii, confId=conf_id) in modern RDKit
total_sasa = rdFreeSASA.CalcSASA(self.rdmol, radii=radii, confIdx=-1)
# try to read per-atom SASA values set by CalcSASA into atoms
atom_sasas = [atom.GetDoubleProp("SASA") for atom in self.rdmol.GetAtoms()]
# Now compute polar/apolar sums: polar = N,O and Hs attached to them
polar_idx = set()
for atom in self.rdmol.GetAtoms():
if atom.GetAtomicNum() in (7, 8): # N or O
polar_idx.add(atom.GetIdx())
# include attached hydrogens
for nbr in atom.GetNeighbors():
if nbr.GetAtomicNum() == 1:
polar_idx.add(nbr.GetIdx())
# default numeric 0 for None entries (defensive)
numeric_atom_sasas = [0.0 if v is None else float(v) for v in atom_sasas]
sasa_polar = sum(numeric_atom_sasas[i] for i in polar_idx)
sasa_total = sum(numeric_atom_sasas)
sasa_apolar = sasa_total - sasa_polar
return rdworks.utils.recursive_round(
{
"sasa_total": sasa_total,
"sasa_polar": sasa_polar,
"sasa_apolar": sasa_apolar,
},
decimals=2,
)
def copy(self) -> Self:
"""Returns a copy of self.
Returns:
Self: `rdworks.Conf` object.
"""
return copy.deepcopy(self)
def rename(self, name: str) -> Self:
"""Rename and returns self.
Args:
name (str): a new name for conformers.
Raises:
ValueError: if `name` is not given.
Returns:
Self: `rdworks.Conf` object.
"""
if not name:
raise ValueError("rdworks.Conf.rename() expects a name")
self.name = name
self.rdmol.SetProp("_Name", name)
return self
def sync(self, coord: np.ndarray | list) -> Self:
"""Synchronize the conformer coordinates with the provided `coord`.
Args:
coord (np.array): 3D coordinates.
Raises:
ValueError: if `coord` does not have the correct shape (natoms, 3).
Returns:
Self: `rdworks.Conf` object.
"""
if isinstance(coord, np.ndarray) and coord.shape != (self.natoms, 3):
raise ValueError(f"`coord.shape` should be ({self.natoms},3)")
elif isinstance(coord, list) and len(coord) != self.natoms:
raise ValueError(f"`coord` should be length of {self.natoms}")
for i, a in enumerate(self.rdmol.GetAtoms()):
self.rdmol.GetConformer().SetAtomPosition(a.GetIdx(), coord[i])
return self
def get_torsion_angle_atoms(self, strict: bool = True) -> list[tuple]:
"""Determine torsion/dihedral angle atoms (i-j-k-l) and rotating group for each rotatable bond (j-k).
Args:
strict (bool): whether to exclude amide/imide/ester/acid bonds.
Returns:
[(i, j, k, l), ...]
"""
return [d[:4] for d in get_torsion_angle_atom_indices(self.rdmol, strict)]
def get_torsion_angle(self, i: int, j: int, k: int, l: int) -> float:
"""Get dihedral angle (i-j-k-l) in degrees.
Args:
i (int): atom index
j (int): atom index
k (int): atom index
l (int): atom index
Returns:
float: dihedral angle in degrees.
"""
degree = rdMolTransforms.GetDihedralDeg(self.rdmol.GetConformer(), i, j, k, l)
return degree
def set_torsion_angle(self, i: int, j: int, k: int, l: int, degree: float) -> Self:
"""Set dihedral angle (i-j-k-l) in degrees.
Args:
i (int): atom index
j (int): atom index
k (int): atom index
l (int): atom index
degree (float): dihedral angle in degrees
Returns:
Self: modified Conf object
"""
rdMolTransforms.SetDihedralDeg(self.rdmol.GetConformer(), i, j, k, l, degree)
return self
def protonate(self, atom_indices: list[int]) -> Self:
"""Protonate given non-hydrogen atoms.
Args:
atom_indices (list[int]): atom indices of non-hydrogen atoms to protonate.
Returns:
Self: self.
"""
for idx in atom_indices:
atom = self.rdmol.GetAtomWithIdx(idx)
h = atom.GetNumExplicitHs()
c = atom.GetFormalCharge()
atom.SetNumExplicitHs(h + 1)
atom.SetFormalCharge(c + 1)
Chem.SanitizeMol(self.rdmol)
self.rdmol = Chem.AddHs(self.rdmol, addCoords=True)
# The Chem.AddHs function in RDKit returns a new Mol object with hydrogens added to the molecule.
# It modifies the input molecule by adding hydrogens,
# but the original molecule remains unchanged.
self._update()
return self
def deprotonate(self, atom_indices: list[int]) -> Self:
"""Deprotonate given non-hydrogen atoms.
Args:
atom_indices (list[int]): atom indices of non-hydrogen atoms to deprotonate.
Returns:
Self: self.
"""
for idx in atom_indices:
bonded_H_idx = None
atom = self.rdmol.GetAtomWithIdx(idx)
h = atom.GetNumExplicitHs()
if h - 1 >= 0:
atom.SetNumExplicitHs(h - 1) # (h-1) must be unsigned int
c = atom.GetFormalCharge()
atom.SetFormalCharge(c - 1)
neighbors = atom.GetNeighbors()
for neighbor in neighbors:
if neighbor.GetAtomicNum() == 1:
bonded_H_idx = neighbor.GetIdx()
break
if bonded_H_idx is not None:
edit_mol = Chem.EditableMol(self.rdmol)
edit_mol.RemoveAtom(bonded_H_idx)
self.rdmol = edit_mol.GetMol()
Chem.SanitizeMol(self.rdmol)
self._update()
return self
##################################################
### Endpoint methods
##################################################
def has_acceptable_bond_lengths(self, tolerance: float = 0.25) -> bool:
"""Check bond length.
The expected length of each bond is the sum of the covalent radii of the
bonded atoms, shortened by 0.18 A per extra bond order (aromatic = 1.5).
(Van der Waals radii are non-bonded contact distances, about twice as long.)
Args:
tolerance (float, optional): allowed deviation from the expected
bond length. Defaults to 0.25 (A).
Returns:
bool: True if all bond lengths are accceptable.
"""
pt = Chem.GetPeriodicTable()
for bond in self.rdmol.GetBonds():
idx1 = bond.GetBeginAtomIdx()
idx2 = bond.GetEndAtomIdx()
nuc1 = self.rdmol.GetAtomWithIdx(idx1).GetAtomicNum()
nuc2 = self.rdmol.GetAtomWithIdx(idx2).GetAtomicNum()
expected = (
pt.GetRcovalent(nuc1)
+ pt.GetRcovalent(nuc2)
- 0.18 * (bond.GetBondTypeAsDouble() - 1.0)
) # (A)
bond_length = rdMolTransforms.GetBondLength(
self.rdmol.GetConformer(), idx1, idx2
)
if abs(bond_length - expected) > tolerance:
return False
return True
def singlepoint(
self, calculator: str | Callable = "MMFF94", water: str | None = None
) -> Self:
"""Get potential energy and set `E_tot(kcal/mol)` in the self.props.
Args:
calculator (str | Callable): MMFF94 (= MMFF), MMFF94s, UFF, xTB, or ASE calculator.
`MMFF94` or `MMFF` - Intended for general use, including organic molecules and proteins,
and primarily relies on data from quantum mechanical calculations.
It's often used in molecular dynamics simulations.
`MMFF94s` - A "static" variant of MMFF94, with adjusted parameters for out-of-plane
bending and dihedral torsions to favor planar geometries for specific nitrogen atoms.
This makes it better suited for geometry optimization studies where a static,
time-averaged structure is desired. The "s" stands for "static".
`UFF` - UFF refers to the "Universal Force Field," a force field model used for
molecular mechanics calculations. It's a tool for geometry optimization,
energy minimization, and exploring molecular conformations in 3D space.
UFF is often used to refine conformers generated by other methods,
such as random conformer generation, to produce more physically plausible
and stable structures.
'xTB' - GFN2-xTB
`GAFF`, `GAFF2`, `Parsley`, `Sage`, `Rosemary` - Amber GAFF/GAFF2 and OpenFF
force fields with OpenMM in the gas phase (NAGL AM1-BCC charges), see
`rdworks.openff.singlepoint()`. Needs `pip install rdworks[openff]`
(GAFF/GAFF2 also need conda-forge `openmmforcefields` and `ambertools`).
`AIMNet2`, `AIMNet2-B973c`, ... - AIMNet2 neural network potential models in the
gas phase, see `rdworks.aimnet2.singlepoint()`. Needs
`pip install rdworks[aimnet2]`.
`UMA` (= `uma-s-1p2p1-ft-d4rev`, task 'omol') or another fairchem UMA model,
see `rdworks.uma.singlepoint()`. Needs `pip install rdworks[uma]`
and Hugging Face access to the gated facebook/UMA checkpoints.
Args for xTB:
water (str, optional): water solvation model (choose 'gbsa' or 'alpb')
alpb: ALPB solvation model (Analytical Linearized Poisson-Boltzmann).
gbsa: generalized Born (GB) model with Surface Area contributions.
Defaults to None.
Raises:
ValueError: if `water` is given for a GAFF/OpenFF force field, an AIMNet2 model,
or a UMA model.
Returns:
float | None: potential energy in kcal/mol or None.
"""
for backend in (openforcefield, aimnet2, uma):
if backend.is_supported(calculator):
if water is not None:
raise ValueError(
f"{calculator} calculations are in the gas phase; water={water!r} is not supported"
)
# sets E_tot(kcal/mol)
return backend.singlepoint(self, calculator)
if isinstance(calculator, str):
if calculator.lower() == "xTB".lower():
results = GFN2xTB(self.rdmol).singlepoint(water=water)
# results = SimpleNamespace(
# PE = datadict['total energy'] * hartree2kcalpermol,
# Gsolv = Gsolv,
# charges = datadict['partial charges'],
# wbo = Wiberg_bond_orders,
# )
PE = results.PE
elif (
calculator.lower() == "MMFF94".lower()
or calculator.lower() == "MMFF".lower()
):
mp = Chem.rdForceFieldHelpers.MMFFGetMoleculeProperties(
self.rdmol, mmffVariant="MMFF94"
)
ff = Chem.rdForceFieldHelpers.MMFFGetMoleculeForceField(self.rdmol, mp)
elif calculator.lower() == "MMFF94s".lower():
mp = Chem.rdForceFieldHelpers.MMFFGetMoleculeProperties(
self.rdmol, mmffVariant="MMFF94s"
)
ff = Chem.rdForceFieldHelpers.MMFFGetMoleculeForceField(self.rdmol, mp)
elif calculator.lower() == "UFF".lower():
ff = Chem.rdForceFieldHelpers.UFFGetMoleculeForceField(self.rdmol)
else:
raise ValueError("Unsupported calculator")
if calculator.lower() != "xTB".lower():
if ff is None:
raise ValueError(
f"{calculator} force field could not be set up for {self.name!r} "
"(missing parameters); try calculator='UFF'"
)
PE = ff.CalcEnergy()
self.props.update({"E_tot(kcal/mol)": PE})
return self
else:
try:
ase_atoms = ase.Atoms(symbols=self.symbols, positions=self.positions)
ase_atoms.info["charge"] = self.charge
ase_atoms.info["spin"] = self.spin
ase_atoms.calc = calculator
PE = ase_atoms.get_potential_energy() # np.array
if isinstance(PE, float):
PE = rdworks.units.ev2kcalpermol * PE
elif isinstance(PE, np.ndarray | list):
PE = rdworks.units.ev2kcalpermol * float(
PE[0]
) # np.float64 to float
self.props.update({"E_tot(kcal/mol)": PE})
return self
except Exception as e:
# third-party ASE calculators may raise any exception type;
# wrap it but keep the original cause for debugging
raise RuntimeError("ASE calculator error") from e
def optimize(
self,
calculator: str | Callable = "MMFF94",
fmax: float = 0.05,
max_iter: int = 1000,
water: str | None = None,
) -> Self:
"""Optimize 3D geometry.
Args:
calculator (str | Callable): MMFF94 (= MMFF), MMFF94s, UFF, xTB or ASE calculator.
`MMFF94` or `MMFF` - Intended for general use, including organic molecules and proteins,
and primarily relies on data from quantum mechanical calculations.
It's often used in molecular dynamics simulations.
`MMFF94s` - A "static" variant of MMFF94, with adjusted parameters for out-of-plane
bending and dihedral torsions to favor planar geometries for specific nitrogen atoms.
This makes it better suited for geometry optimization studies where a static,
time-averaged structure is desired. The "s" stands for "static".
`UFF` - UFF refers to the "Universal Force Field," a force field model used for
molecular mechanics calculations. It's a tool for geometry optimization,
energy minimization, and exploring molecular conformations in 3D space.
UFF is often used to refine conformers generated by other methods,
such as random conformer generation, to produce more physically plausible
and stable structures.
`GAFF`, `GAFF2`, `Parsley`, `Sage`, `Rosemary` - Amber GAFF/GAFF2 and OpenFF
force fields with OpenMM in the gas phase (NAGL AM1-BCC charges), see
`rdworks.openff.optimize()`. Needs `pip install rdworks[openff]`
(GAFF/GAFF2 also need conda-forge `openmmforcefields` and `ambertools`).
`AIMNet2`, `AIMNet2-B973c`, ... - AIMNet2 neural network potential models in the
gas phase, optimized with TorchSim FIRE, see `rdworks.aimnet2.optimize()`.
Needs `pip install rdworks[aimnet2]`.
`UMA` (= `uma-s-1p2p1-ft-d4rev`, task 'omol') or another fairchem UMA model,
optimized with ASE FIRE, see `rdworks.uma.optimize()`. Needs
`pip install rdworks[uma]` and Hugging Face access to facebook/UMA.
fmax (float, optional): fmax for the calculator convergence. Defaults to 0.05.
max_iter (int, optional): max iterations for the calculator. Defaults to 1000.
Raises:
ValueError: if `water` is given for a GAFF/OpenFF force field, an AIMNet2 model,
or a UMA model.
Args for xTB calculator:
water (str, optional): water solvation model (choose 'gbsa' or 'alpb')
alpb: ALPB solvation model (Analytical Linearized Poisson-Boltzmann).
gbsa: generalized Born (GB) model with Surface Area contributions.
Defaults to None.
Returns:
Self: self
"""
for backend in (openforcefield, aimnet2, uma):
if backend.is_supported(calculator):
if water is not None:
raise ValueError(
f"{calculator} calculations are in the gas phase; water={water!r} is not supported"
)
# sets E_tot_init(kcal/mol), E_tot(kcal/mol), Converged and updates coordinates
return backend.optimize(self, calculator, fmax=fmax, max_iter=max_iter)
if isinstance(calculator, str):
PE_start = self.singlepoint(calculator).props.get("E_tot(kcal/mol)")
PE_final = None
if calculator.lower() == "xTB".lower():
results = GFN2xTB(self.rdmol).optimize(water=water)
# results = SimpleNamespace(
# PE = datadict['total energy'] * hartree2kcalpermol,
# charges = datadict['partial charges'],
# wbo = Wiberg_bond_orders,
# geometry = rdmol_opt,
# )
try:
self.rdmol = results.geometry
PE_final = results.PE
retcode = 0
except AttributeError:
retcode = 1
elif (
calculator.lower() == "MMFF94".lower()
or calculator.lower() == "MMFF".lower()
):
retcode = Chem.rdForceFieldHelpers.MMFFOptimizeMolecule(
self.rdmol, mmffVariant="MMFF94", maxIters=max_iter
)
# returns 0 if the optimization converged
elif calculator.lower() == "MMFF94s".lower():
retcode = Chem.rdForceFieldHelpers.MMFFOptimizeMolecule(
self.rdmol, mmffVariant="MMFF94s", maxIters=max_iter
)
# returns 0 if the optimization converged
elif calculator.lower() == "UFF".lower():
retcode = Chem.rdForceFieldHelpers.UFFOptimizeMolecule(
self.rdmol, maxIters=max_iter
)
# returns 0 if the optimization converged
if PE_final is None:
PE_final = self.singlepoint(calculator).props.get("E_tot(kcal/mol)")
self.props.update(
{
"E_tot_init(kcal/mol)": PE_start, # energy before optimization
"E_tot(kcal/mol)": PE_final, # energy after optimization
"Converged": retcode == 0, # True or False
}
)
return self
else:
# assuming ASE calculator
with io.StringIO() as logfile:
ase_atoms = ase.Atoms(symbols=self.symbols, positions=self.positions)
ase_atoms.info["charge"] = self.charge
ase_atoms.info["spin"] = self.spin
ase_atoms.calc = calculator
FIRE(ase_atoms, logfile=logfile).run(fmax=fmax)
lines = [
l.strip().split()[1:]
for l in logfile.getvalue().split("\n")
if l.startswith("FIRE")
]
data = [(float(e), float(f)) for (_, _, e, f) in lines]
self.props.update(
{
"E_tot_init(kcal/mol)": data[0][0]
* rdworks.units.ev2kcalpermol, # energy before optimization
"E_tot(kcal/mol)": data[-1][0]
* rdworks.units.ev2kcalpermol, # energy after optimization
"Converged": data[-1][1] < fmax, # True or False
}
)
# update atomic coordinates
return self.sync(ase_atoms.get_positions())
def torsion_scan(
self,
calculator: str | Callable,
torsion_angle_idx: int | None = None,
simplify: bool = True,
interval: float = 20.0,
fmax: float = 0.05,
water: str | None = None,
use_converged_only: bool = True,
) -> Self:
"""Calculates potential energy profiles for each torsion angle (see `rdworks.torsion.torsion_scan()`).
Args:
calculator (str | Callable): 'MMFF', 'UFF', 'xTB', or ASE calculator; or 'GAFF', 'GAFF2',
'Parsley', 'Sage', 'Rosemary', 'AIMNet2', or 'UMA' (see `Conf.optimize()`).
torsion_angle_idx (int | None): key to the torsion indices to calculate. Defaults to None (all).
simplify (bool, optional): whether to use fragment surrogate. Defaults to True.
fmax (float, optional): fmax of ASE optimizer. Defaults to 0.05.
interval (float, optional): interval of torsion angles in degree. Defaults to 20.0.
use_converged_only (bool, optional): whether to use only converged data. Defaults to True.
Args for xTB calculator:
water (str, optional): water solvation model (choose 'gbsa' or 'alpb')
alpb: ALPB solvation model (Analytical Linearized Poisson-Boltzmann).
gbsa: generalized Born (GB) model with Surface Area contributions.
Defaults to None.
Returns:
Self: modified self.
"""
return rdworks.torsion.torsion_scan(
self,
calculator=calculator,
torsion_angle_idx=torsion_angle_idx,
simplify=simplify,
interval=interval,
fmax=fmax,
water=water,
use_converged_only=use_converged_only,
)
def dumps(self, key: str = "") -> str:
"""Returns JSON dumps of the `props`.
Args:
key (str): a key for the `props` dictionary. Defaults to '' (all).
Returns:
str: JSON dumps.
"""
if key:
return json.dumps({key: self.props[key]})
else:
return json.dumps(self.props)
def serialize(self, decimals: int = 3) -> str:
"""Serialize information necessary to rebuild a Conf object.
Args:
decimals (int, optional): number of decimal places for float data type. Defaults to 3.
Examples:
```python
serialized = conf1.serialize()
conf2 = Conf().deserialize(serialized)
assert conf1 == conf2
```
Returns:
str: serialized string for json.loads()
"""
serialized = rdworks.utils.serialize(
{
"name": self.name,
"natoms": self.natoms,
"charge": self.charge,
"spin": self.spin,
"props": rdworks.utils.recursive_round(self.props, decimals),
"molblock": self.molblock,
}
)
return serialized
def deserialize(self, serialized: str) -> Self:
"""De-serialize information and rebuild a Conf object.
Examples:
```python
serialized = conf1.serialize()
conf2 = Conf().deserialize(serialized)
assert conf1 == conf2
```
Args:
serialized (str): serialized string.
Returns:
Self: modified self.
"""
data = rdworks.utils.deserialize(serialized)
self.name = data["name"]
self.natoms = int(data["natoms"])
self.charge = int(data["charge"])
self.spin = int(data["spin"])
self.props = data["props"]
self.rdmol = Chem.MolFromMolBlock(
data["molblock"], sanitize=False, removeHs=False
)
self._update()
return self
def to_geometry(self) -> str:
"""Returns geometry input for psi4 or other quantum chemistry software.
Each line has atom symbol and its X, Y, and Z coordinates.
Unit is Angstrom.
O 0.0 0.0 0.0
H 0.757 0.586 0.0
H -0.757 0.586 0.0
Example:
import psi4
geometry = conf.to_geometry()
mol = psi4.geometry(geometry)
Returns:
str: geometry input for psi4 or other quantum chemistry software.
"""
return rwio.to_geometry(self)
def to_xyz(self, path: str | Path | None = None) -> str:
"""Returns XYZ formatted strings (the comment line is the conformer name).
Args:
path (str | Path | None, optional): also write to this file. Defaults to None.
Returns:
str: XYZ formatted strings.
"""
return rwio.to_xyz(self, path=path)
def to_turbomole(self, bohr: bool = False) -> str:
"""Returns TURBOMOLE coord file formatted strings.
Turbomole coord file format:
- It starts with the keyword `$coord`.
- Each line after the $coord line specifies an atom, consisting of:
- Three real numbers representing the Cartesian coordinates (x, y, z).
- A string for the element name.
- Optional: an "f" label at the end to indicate that the atom's coordinates are frozen during optimization.
- Coordinates can be given in Bohr (default), Ångström (`$coord angs`), or fractional coordinates (`$coord frac`).
- Optional data groups like periodicity (`$periodic`), lattice parameters (`$lattice`), and cell parameters (`$cell`) can also be included.
- Regarding precision:
The precision of the coordinates is crucial for accurate calculations, especially geometry optimizations.
Tools like the TURBOMOLEOptimizer might check for differences in atomic positions with a tolerance of 1e-13.
Args:
bohr (bool): whether to use Bohr units of the coordinates. Defaults to False.
Otherwise, Angstrom units will be used.
Returns:
str: TURBOMOLE coord formatted file.
"""
return rwio.to_turbomole(self, bohr=bohr)
def to_sdf(self, props: bool = True, path: str | Path | None = None) -> str | None:
"""Returns the SDF-formatted strings with 3D coordinates.
Args:
props (bool, optional): include `props` as SDF properties. Defaults to True.
path (str | Path | None, optional): write to this .sdf (.sdf.gz) file instead
of returning the strings. Defaults to None.
Returns:
str | None: strings in the SDF format if `path` is None.
"""
return rwio.to_sdf(self, path=path, props=props)
def to_png(
self,
width: int = 300,
height: int = 300,
legend: str = "",
atom_index: bool = False,
highlight_atoms: list[int] | None = None,
highlight_bonds: list[int] | None = None,
redraw: bool = False,
coordgen: bool = False,
trim: bool = True,
path: str | Path | None = None,
) -> Image.Image:
"""Draw 2D molecule in PNG format.
Args:
width (int, optional): width. Defaults to 300.
height (int, optional): height. Defaults to 300.
legend (str, optional): legend. Defaults to ''.
atom_index (bool, optional): whether to show atom index. Defaults to False.
highlight_atoms (list[int] | None, optional): atom(s) to highlight. Defaults to None.
highlight_bonds (list[int] | None, optional): bond(s) to highlight. Defaults to None.
redraw (bool, optional): whether to redraw. Defaults to False.
coordgen (bool, optional): whether to use coordgen. Defaults to False.
trim (bool, optional): whether to trim white margins. Default to True.
Returns:
Image.Image: output PIL Image object.
"""
return rwio.to_png(
self,
path=path,
width=width,
height=height,
legend=legend,
atom_index=atom_index,
highlight_atoms=highlight_atoms,
highlight_bonds=highlight_bonds,
redraw=redraw,
coordgen=coordgen,
trim=trim,
)
def to_svg(
self,
width: int = 300,
height: int = 300,
legend: str = "",
atom_index: bool = False,
highlight_atoms: list[int] | None = None,
highlight_bonds: list[int] | None = None,
redraw: bool = False,
coordgen: bool = False,
optimize: bool = True,
path: str | Path | None = None,
) -> str:
"""Draw 2D molecule in SVG format.
Examples:
For Jupyternotebook, wrap the output with SVG:
>>> from IPython.display import SVG
>>> SVG(libr[0].to_svg())
Args:
width (int, optional): width. Defaults to 300.
height (int, optional): height. Defaults to 300.
legend (str, optional): legend. Defaults to ''.
atom_index (bool, optional): whether to show atom index. Defaults to False.
highlight_atoms (list[int] | None, optional): atom(s) to highlight. Defaults to None.
highlight_bonds (list[int] | None, optional): bond(s) to highlight. Defaults to None.
redraw (bool, optional): whether to redraw. Defaults to False.
coordgen (bool, optional): whether to use coordgen. Defaults to False.
optimize (bool, optional): whether to optimize SVG string. Defaults to True.
Returns:
str: SVG string
"""
return rwio.to_svg(
self,
path=path,
width=width,
height=height,
legend=legend,
atom_index=atom_index,
highlight_atoms=highlight_atoms,
highlight_bonds=highlight_bonds,
redraw=redraw,
coordgen=coordgen,
optimize=optimize,
)
def to_df(self, name: str = "name", smiles: str = "smiles") -> "pd.DataFrame":
"""Returns a one-row pandas DataFrame of name, SMILES, and conformer properties.
Args:
name (str, optional): column name for name. Defaults to 'name'.
smiles (str, optional): column name for SMILES. Defaults to 'smiles'.
Returns:
pd.DataFrame: pandas DataFrame.
"""
return rwio.to_df(self, name=name, smiles=smiles)
def to_csv(self, path: str | Path, decimals: int = 3) -> None:
"""Writes name, SMILES, and conformer properties to a .csv file.
Args:
path (str | Path): output filename or path.
decimals (int, optional): decimal places for float numbers. Defaults to 3.
"""
rwio.to_csv(self, path, decimals=decimals)
Attributes¶
COG
property
¶
Returns the center of geometry (COG).
Returns:
-
array–np.array: the center of geometry.
Rg
property
¶
Returns the radius of gyration (Rg).
Returns:
-
float(float) –the radius of gyration.
SASA
property
¶
Calculate Solvent Accessible Surface Area (total, polar, apolar).
Returns:
-
dict–tuple[float, float, float]: (total, polar, apolar)
charge = 0
instance-attribute
¶
molblock
property
¶
Returns the MolBlock of the current coordinates.
name = name
instance-attribute
¶
natoms = 0
instance-attribute
¶
numbers = []
instance-attribute
¶
positions
property
¶
Returns current atomic coordinates (natoms, 3) in Angstrom.
props = {}
instance-attribute
¶
rdmol = Chem.MolFromMolBlock(molecule, sanitize=False, removeHs=False, strictParsing=True)
instance-attribute
¶
smiles = ''
instance-attribute
¶
spin = 1
instance-attribute
¶
symbols = []
instance-attribute
¶
Methods:¶
copy()
¶
deprotonate(atom_indices)
¶
Deprotonate given non-hydrogen atoms.
Parameters:
-
atom_indices(list[int]) –atom indices of non-hydrogen atoms to deprotonate.
Returns:
-
Self(Self) –self.
Source code in src/rdworks/conf.py
def deprotonate(self, atom_indices: list[int]) -> Self:
"""Deprotonate given non-hydrogen atoms.
Args:
atom_indices (list[int]): atom indices of non-hydrogen atoms to deprotonate.
Returns:
Self: self.
"""
for idx in atom_indices:
bonded_H_idx = None
atom = self.rdmol.GetAtomWithIdx(idx)
h = atom.GetNumExplicitHs()
if h - 1 >= 0:
atom.SetNumExplicitHs(h - 1) # (h-1) must be unsigned int
c = atom.GetFormalCharge()
atom.SetFormalCharge(c - 1)
neighbors = atom.GetNeighbors()
for neighbor in neighbors:
if neighbor.GetAtomicNum() == 1:
bonded_H_idx = neighbor.GetIdx()
break
if bonded_H_idx is not None:
edit_mol = Chem.EditableMol(self.rdmol)
edit_mol.RemoveAtom(bonded_H_idx)
self.rdmol = edit_mol.GetMol()
Chem.SanitizeMol(self.rdmol)
self._update()
return self
deserialize(serialized)
¶
De-serialize information and rebuild a Conf object.
Examples:
Args: serialized (str): serialized string.
Returns:
-
Self(Self) –modified self.
Source code in src/rdworks/conf.py
def deserialize(self, serialized: str) -> Self:
"""De-serialize information and rebuild a Conf object.
Examples:
```python
serialized = conf1.serialize()
conf2 = Conf().deserialize(serialized)
assert conf1 == conf2
```
Args:
serialized (str): serialized string.
Returns:
Self: modified self.
"""
data = rdworks.utils.deserialize(serialized)
self.name = data["name"]
self.natoms = int(data["natoms"])
self.charge = int(data["charge"])
self.spin = int(data["spin"])
self.props = data["props"]
self.rdmol = Chem.MolFromMolBlock(
data["molblock"], sanitize=False, removeHs=False
)
self._update()
return self
dumps(key='')
¶
Returns JSON dumps of the props.
Parameters:
-
key(str, default:'') –a key for the
propsdictionary. Defaults to '' (all).
Returns:
-
str(str) –JSON dumps.
Source code in src/rdworks/conf.py
get_torsion_angle(i, j, k, l)
¶
Get dihedral angle (i-j-k-l) in degrees.
Parameters:
-
i(int) –atom index
-
j(int) –atom index
-
k(int) –atom index
-
l(int) –atom index
Returns:
-
float(float) –dihedral angle in degrees.
Source code in src/rdworks/conf.py
def get_torsion_angle(self, i: int, j: int, k: int, l: int) -> float:
"""Get dihedral angle (i-j-k-l) in degrees.
Args:
i (int): atom index
j (int): atom index
k (int): atom index
l (int): atom index
Returns:
float: dihedral angle in degrees.
"""
degree = rdMolTransforms.GetDihedralDeg(self.rdmol.GetConformer(), i, j, k, l)
return degree
get_torsion_angle_atoms(strict=True)
¶
Determine torsion/dihedral angle atoms (i-j-k-l) and rotating group for each rotatable bond (j-k).
Parameters:
-
strict(bool, default:True) –whether to exclude amide/imide/ester/acid bonds.
Returns:
-
list[tuple]–[(i, j, k, l), ...]
Source code in src/rdworks/conf.py
def get_torsion_angle_atoms(self, strict: bool = True) -> list[tuple]:
"""Determine torsion/dihedral angle atoms (i-j-k-l) and rotating group for each rotatable bond (j-k).
Args:
strict (bool): whether to exclude amide/imide/ester/acid bonds.
Returns:
[(i, j, k, l), ...]
"""
return [d[:4] for d in get_torsion_angle_atom_indices(self.rdmol, strict)]
has_acceptable_bond_lengths(tolerance=0.25)
¶
Check bond length.
The expected length of each bond is the sum of the covalent radii of the bonded atoms, shortened by 0.18 A per extra bond order (aromatic = 1.5). (Van der Waals radii are non-bonded contact distances, about twice as long.)
Parameters:
-
tolerance(float, default:0.25) –allowed deviation from the expected bond length. Defaults to 0.25 (A).
Returns:
-
bool(bool) –True if all bond lengths are accceptable.
Source code in src/rdworks/conf.py
def has_acceptable_bond_lengths(self, tolerance: float = 0.25) -> bool:
"""Check bond length.
The expected length of each bond is the sum of the covalent radii of the
bonded atoms, shortened by 0.18 A per extra bond order (aromatic = 1.5).
(Van der Waals radii are non-bonded contact distances, about twice as long.)
Args:
tolerance (float, optional): allowed deviation from the expected
bond length. Defaults to 0.25 (A).
Returns:
bool: True if all bond lengths are accceptable.
"""
pt = Chem.GetPeriodicTable()
for bond in self.rdmol.GetBonds():
idx1 = bond.GetBeginAtomIdx()
idx2 = bond.GetEndAtomIdx()
nuc1 = self.rdmol.GetAtomWithIdx(idx1).GetAtomicNum()
nuc2 = self.rdmol.GetAtomWithIdx(idx2).GetAtomicNum()
expected = (
pt.GetRcovalent(nuc1)
+ pt.GetRcovalent(nuc2)
- 0.18 * (bond.GetBondTypeAsDouble() - 1.0)
) # (A)
bond_length = rdMolTransforms.GetBondLength(
self.rdmol.GetConformer(), idx1, idx2
)
if abs(bond_length - expected) > tolerance:
return False
return True
optimize(calculator='MMFF94', fmax=0.05, max_iter=1000, water=None)
¶
Optimize 3D geometry.
Parameters:
-
calculator(str | Callable, default:'MMFF94') –MMFF94 (= MMFF), MMFF94s, UFF, xTB or ASE calculator.
MMFF94orMMFF- Intended for general use, including organic molecules and proteins, and primarily relies on data from quantum mechanical calculations. It's often used in molecular dynamics simulations.MMFF94s- A "static" variant of MMFF94, with adjusted parameters for out-of-plane bending and dihedral torsions to favor planar geometries for specific nitrogen atoms. This makes it better suited for geometry optimization studies where a static, time-averaged structure is desired. The "s" stands for "static".UFF- UFF refers to the "Universal Force Field," a force field model used for molecular mechanics calculations. It's a tool for geometry optimization, energy minimization, and exploring molecular conformations in 3D space. UFF is often used to refine conformers generated by other methods, such as random conformer generation, to produce more physically plausible and stable structures.GAFF,GAFF2,Parsley,Sage,Rosemary- Amber GAFF/GAFF2 and OpenFF force fields with OpenMM in the gas phase (NAGL AM1-BCC charges), seerdworks.openff.optimize(). Needspip install rdworks[openff](GAFF/GAFF2 also need conda-forgeopenmmforcefieldsandambertools).AIMNet2,AIMNet2-B973c, ... - AIMNet2 neural network potential models in the gas phase, optimized with TorchSim FIRE, seerdworks.aimnet2.optimize(). Needspip install rdworks[aimnet2].UMA(=uma-s-1p2p1-ft-d4rev, task 'omol') or another fairchem UMA model, optimized with ASE FIRE, seerdworks.uma.optimize(). Needspip install rdworks[uma]and Hugging Face access to facebook/UMA. -
fmax(float, default:0.05) –fmax for the calculator convergence. Defaults to 0.05.
-
max_iter(int, default:1000) –max iterations for the calculator. Defaults to 1000.
Raises:
-
ValueError–if
wateris given for a GAFF/OpenFF force field, an AIMNet2 model, or a UMA model.
Args for xTB calculator
water (str, optional): water solvation model (choose 'gbsa' or 'alpb') alpb: ALPB solvation model (Analytical Linearized Poisson-Boltzmann). gbsa: generalized Born (GB) model with Surface Area contributions. Defaults to None.
Returns:
-
Self(Self) –self
Source code in src/rdworks/conf.py
def optimize(
self,
calculator: str | Callable = "MMFF94",
fmax: float = 0.05,
max_iter: int = 1000,
water: str | None = None,
) -> Self:
"""Optimize 3D geometry.
Args:
calculator (str | Callable): MMFF94 (= MMFF), MMFF94s, UFF, xTB or ASE calculator.
`MMFF94` or `MMFF` - Intended for general use, including organic molecules and proteins,
and primarily relies on data from quantum mechanical calculations.
It's often used in molecular dynamics simulations.
`MMFF94s` - A "static" variant of MMFF94, with adjusted parameters for out-of-plane
bending and dihedral torsions to favor planar geometries for specific nitrogen atoms.
This makes it better suited for geometry optimization studies where a static,
time-averaged structure is desired. The "s" stands for "static".
`UFF` - UFF refers to the "Universal Force Field," a force field model used for
molecular mechanics calculations. It's a tool for geometry optimization,
energy minimization, and exploring molecular conformations in 3D space.
UFF is often used to refine conformers generated by other methods,
such as random conformer generation, to produce more physically plausible
and stable structures.
`GAFF`, `GAFF2`, `Parsley`, `Sage`, `Rosemary` - Amber GAFF/GAFF2 and OpenFF
force fields with OpenMM in the gas phase (NAGL AM1-BCC charges), see
`rdworks.openff.optimize()`. Needs `pip install rdworks[openff]`
(GAFF/GAFF2 also need conda-forge `openmmforcefields` and `ambertools`).
`AIMNet2`, `AIMNet2-B973c`, ... - AIMNet2 neural network potential models in the
gas phase, optimized with TorchSim FIRE, see `rdworks.aimnet2.optimize()`.
Needs `pip install rdworks[aimnet2]`.
`UMA` (= `uma-s-1p2p1-ft-d4rev`, task 'omol') or another fairchem UMA model,
optimized with ASE FIRE, see `rdworks.uma.optimize()`. Needs
`pip install rdworks[uma]` and Hugging Face access to facebook/UMA.
fmax (float, optional): fmax for the calculator convergence. Defaults to 0.05.
max_iter (int, optional): max iterations for the calculator. Defaults to 1000.
Raises:
ValueError: if `water` is given for a GAFF/OpenFF force field, an AIMNet2 model,
or a UMA model.
Args for xTB calculator:
water (str, optional): water solvation model (choose 'gbsa' or 'alpb')
alpb: ALPB solvation model (Analytical Linearized Poisson-Boltzmann).
gbsa: generalized Born (GB) model with Surface Area contributions.
Defaults to None.
Returns:
Self: self
"""
for backend in (openforcefield, aimnet2, uma):
if backend.is_supported(calculator):
if water is not None:
raise ValueError(
f"{calculator} calculations are in the gas phase; water={water!r} is not supported"
)
# sets E_tot_init(kcal/mol), E_tot(kcal/mol), Converged and updates coordinates
return backend.optimize(self, calculator, fmax=fmax, max_iter=max_iter)
if isinstance(calculator, str):
PE_start = self.singlepoint(calculator).props.get("E_tot(kcal/mol)")
PE_final = None
if calculator.lower() == "xTB".lower():
results = GFN2xTB(self.rdmol).optimize(water=water)
# results = SimpleNamespace(
# PE = datadict['total energy'] * hartree2kcalpermol,
# charges = datadict['partial charges'],
# wbo = Wiberg_bond_orders,
# geometry = rdmol_opt,
# )
try:
self.rdmol = results.geometry
PE_final = results.PE
retcode = 0
except AttributeError:
retcode = 1
elif (
calculator.lower() == "MMFF94".lower()
or calculator.lower() == "MMFF".lower()
):
retcode = Chem.rdForceFieldHelpers.MMFFOptimizeMolecule(
self.rdmol, mmffVariant="MMFF94", maxIters=max_iter
)
# returns 0 if the optimization converged
elif calculator.lower() == "MMFF94s".lower():
retcode = Chem.rdForceFieldHelpers.MMFFOptimizeMolecule(
self.rdmol, mmffVariant="MMFF94s", maxIters=max_iter
)
# returns 0 if the optimization converged
elif calculator.lower() == "UFF".lower():
retcode = Chem.rdForceFieldHelpers.UFFOptimizeMolecule(
self.rdmol, maxIters=max_iter
)
# returns 0 if the optimization converged
if PE_final is None:
PE_final = self.singlepoint(calculator).props.get("E_tot(kcal/mol)")
self.props.update(
{
"E_tot_init(kcal/mol)": PE_start, # energy before optimization
"E_tot(kcal/mol)": PE_final, # energy after optimization
"Converged": retcode == 0, # True or False
}
)
return self
else:
# assuming ASE calculator
with io.StringIO() as logfile:
ase_atoms = ase.Atoms(symbols=self.symbols, positions=self.positions)
ase_atoms.info["charge"] = self.charge
ase_atoms.info["spin"] = self.spin
ase_atoms.calc = calculator
FIRE(ase_atoms, logfile=logfile).run(fmax=fmax)
lines = [
l.strip().split()[1:]
for l in logfile.getvalue().split("\n")
if l.startswith("FIRE")
]
data = [(float(e), float(f)) for (_, _, e, f) in lines]
self.props.update(
{
"E_tot_init(kcal/mol)": data[0][0]
* rdworks.units.ev2kcalpermol, # energy before optimization
"E_tot(kcal/mol)": data[-1][0]
* rdworks.units.ev2kcalpermol, # energy after optimization
"Converged": data[-1][1] < fmax, # True or False
}
)
# update atomic coordinates
return self.sync(ase_atoms.get_positions())
protonate(atom_indices)
¶
Protonate given non-hydrogen atoms.
Parameters:
-
atom_indices(list[int]) –atom indices of non-hydrogen atoms to protonate.
Returns:
-
Self(Self) –self.
Source code in src/rdworks/conf.py
def protonate(self, atom_indices: list[int]) -> Self:
"""Protonate given non-hydrogen atoms.
Args:
atom_indices (list[int]): atom indices of non-hydrogen atoms to protonate.
Returns:
Self: self.
"""
for idx in atom_indices:
atom = self.rdmol.GetAtomWithIdx(idx)
h = atom.GetNumExplicitHs()
c = atom.GetFormalCharge()
atom.SetNumExplicitHs(h + 1)
atom.SetFormalCharge(c + 1)
Chem.SanitizeMol(self.rdmol)
self.rdmol = Chem.AddHs(self.rdmol, addCoords=True)
# The Chem.AddHs function in RDKit returns a new Mol object with hydrogens added to the molecule.
# It modifies the input molecule by adding hydrogens,
# but the original molecule remains unchanged.
self._update()
return self
rename(name)
¶
Rename and returns self.
Parameters:
-
name(str) –a new name for conformers.
Raises:
-
ValueError–if
nameis not given.
Returns:
-
Self(Self) –rdworks.Confobject.
Source code in src/rdworks/conf.py
def rename(self, name: str) -> Self:
"""Rename and returns self.
Args:
name (str): a new name for conformers.
Raises:
ValueError: if `name` is not given.
Returns:
Self: `rdworks.Conf` object.
"""
if not name:
raise ValueError("rdworks.Conf.rename() expects a name")
self.name = name
self.rdmol.SetProp("_Name", name)
return self
serialize(decimals=3)
¶
Serialize information necessary to rebuild a Conf object.
Parameters:
-
decimals(int, default:3) –number of decimal places for float data type. Defaults to 3.
Examples:
Returns:
-
str(str) –serialized string for json.loads()
Source code in src/rdworks/conf.py
def serialize(self, decimals: int = 3) -> str:
"""Serialize information necessary to rebuild a Conf object.
Args:
decimals (int, optional): number of decimal places for float data type. Defaults to 3.
Examples:
```python
serialized = conf1.serialize()
conf2 = Conf().deserialize(serialized)
assert conf1 == conf2
```
Returns:
str: serialized string for json.loads()
"""
serialized = rdworks.utils.serialize(
{
"name": self.name,
"natoms": self.natoms,
"charge": self.charge,
"spin": self.spin,
"props": rdworks.utils.recursive_round(self.props, decimals),
"molblock": self.molblock,
}
)
return serialized
set_torsion_angle(i, j, k, l, degree)
¶
Set dihedral angle (i-j-k-l) in degrees.
Parameters:
-
i(int) –atom index
-
j(int) –atom index
-
k(int) –atom index
-
l(int) –atom index
-
degree(float) –dihedral angle in degrees
Returns:
-
Self(Self) –modified Conf object
Source code in src/rdworks/conf.py
def set_torsion_angle(self, i: int, j: int, k: int, l: int, degree: float) -> Self:
"""Set dihedral angle (i-j-k-l) in degrees.
Args:
i (int): atom index
j (int): atom index
k (int): atom index
l (int): atom index
degree (float): dihedral angle in degrees
Returns:
Self: modified Conf object
"""
rdMolTransforms.SetDihedralDeg(self.rdmol.GetConformer(), i, j, k, l, degree)
return self
singlepoint(calculator='MMFF94', water=None)
¶
Get potential energy and set E_tot(kcal/mol) in the self.props.
Parameters:
-
calculator(str | Callable, default:'MMFF94') –MMFF94 (= MMFF), MMFF94s, UFF, xTB, or ASE calculator.
MMFF94orMMFF- Intended for general use, including organic molecules and proteins, and primarily relies on data from quantum mechanical calculations. It's often used in molecular dynamics simulations.MMFF94s- A "static" variant of MMFF94, with adjusted parameters for out-of-plane bending and dihedral torsions to favor planar geometries for specific nitrogen atoms. This makes it better suited for geometry optimization studies where a static, time-averaged structure is desired. The "s" stands for "static".UFF- UFF refers to the "Universal Force Field," a force field model used for molecular mechanics calculations. It's a tool for geometry optimization, energy minimization, and exploring molecular conformations in 3D space. UFF is often used to refine conformers generated by other methods, such as random conformer generation, to produce more physically plausible and stable structures. 'xTB' - GFN2-xTBGAFF,GAFF2,Parsley,Sage,Rosemary- Amber GAFF/GAFF2 and OpenFF force fields with OpenMM in the gas phase (NAGL AM1-BCC charges), seerdworks.openff.singlepoint(). Needspip install rdworks[openff](GAFF/GAFF2 also need conda-forgeopenmmforcefieldsandambertools).AIMNet2,AIMNet2-B973c, ... - AIMNet2 neural network potential models in the gas phase, seerdworks.aimnet2.singlepoint(). Needspip install rdworks[aimnet2].UMA(=uma-s-1p2p1-ft-d4rev, task 'omol') or another fairchem UMA model, seerdworks.uma.singlepoint(). Needspip install rdworks[uma]and Hugging Face access to the gated facebook/UMA checkpoints.
Args for xTB
water (str, optional): water solvation model (choose 'gbsa' or 'alpb') alpb: ALPB solvation model (Analytical Linearized Poisson-Boltzmann). gbsa: generalized Born (GB) model with Surface Area contributions. Defaults to None.
Raises:
-
ValueError–if
wateris given for a GAFF/OpenFF force field, an AIMNet2 model, or a UMA model.
Returns:
-
Self–float | None: potential energy in kcal/mol or None.
Source code in src/rdworks/conf.py
def singlepoint(
self, calculator: str | Callable = "MMFF94", water: str | None = None
) -> Self:
"""Get potential energy and set `E_tot(kcal/mol)` in the self.props.
Args:
calculator (str | Callable): MMFF94 (= MMFF), MMFF94s, UFF, xTB, or ASE calculator.
`MMFF94` or `MMFF` - Intended for general use, including organic molecules and proteins,
and primarily relies on data from quantum mechanical calculations.
It's often used in molecular dynamics simulations.
`MMFF94s` - A "static" variant of MMFF94, with adjusted parameters for out-of-plane
bending and dihedral torsions to favor planar geometries for specific nitrogen atoms.
This makes it better suited for geometry optimization studies where a static,
time-averaged structure is desired. The "s" stands for "static".
`UFF` - UFF refers to the "Universal Force Field," a force field model used for
molecular mechanics calculations. It's a tool for geometry optimization,
energy minimization, and exploring molecular conformations in 3D space.
UFF is often used to refine conformers generated by other methods,
such as random conformer generation, to produce more physically plausible
and stable structures.
'xTB' - GFN2-xTB
`GAFF`, `GAFF2`, `Parsley`, `Sage`, `Rosemary` - Amber GAFF/GAFF2 and OpenFF
force fields with OpenMM in the gas phase (NAGL AM1-BCC charges), see
`rdworks.openff.singlepoint()`. Needs `pip install rdworks[openff]`
(GAFF/GAFF2 also need conda-forge `openmmforcefields` and `ambertools`).
`AIMNet2`, `AIMNet2-B973c`, ... - AIMNet2 neural network potential models in the
gas phase, see `rdworks.aimnet2.singlepoint()`. Needs
`pip install rdworks[aimnet2]`.
`UMA` (= `uma-s-1p2p1-ft-d4rev`, task 'omol') or another fairchem UMA model,
see `rdworks.uma.singlepoint()`. Needs `pip install rdworks[uma]`
and Hugging Face access to the gated facebook/UMA checkpoints.
Args for xTB:
water (str, optional): water solvation model (choose 'gbsa' or 'alpb')
alpb: ALPB solvation model (Analytical Linearized Poisson-Boltzmann).
gbsa: generalized Born (GB) model with Surface Area contributions.
Defaults to None.
Raises:
ValueError: if `water` is given for a GAFF/OpenFF force field, an AIMNet2 model,
or a UMA model.
Returns:
float | None: potential energy in kcal/mol or None.
"""
for backend in (openforcefield, aimnet2, uma):
if backend.is_supported(calculator):
if water is not None:
raise ValueError(
f"{calculator} calculations are in the gas phase; water={water!r} is not supported"
)
# sets E_tot(kcal/mol)
return backend.singlepoint(self, calculator)
if isinstance(calculator, str):
if calculator.lower() == "xTB".lower():
results = GFN2xTB(self.rdmol).singlepoint(water=water)
# results = SimpleNamespace(
# PE = datadict['total energy'] * hartree2kcalpermol,
# Gsolv = Gsolv,
# charges = datadict['partial charges'],
# wbo = Wiberg_bond_orders,
# )
PE = results.PE
elif (
calculator.lower() == "MMFF94".lower()
or calculator.lower() == "MMFF".lower()
):
mp = Chem.rdForceFieldHelpers.MMFFGetMoleculeProperties(
self.rdmol, mmffVariant="MMFF94"
)
ff = Chem.rdForceFieldHelpers.MMFFGetMoleculeForceField(self.rdmol, mp)
elif calculator.lower() == "MMFF94s".lower():
mp = Chem.rdForceFieldHelpers.MMFFGetMoleculeProperties(
self.rdmol, mmffVariant="MMFF94s"
)
ff = Chem.rdForceFieldHelpers.MMFFGetMoleculeForceField(self.rdmol, mp)
elif calculator.lower() == "UFF".lower():
ff = Chem.rdForceFieldHelpers.UFFGetMoleculeForceField(self.rdmol)
else:
raise ValueError("Unsupported calculator")
if calculator.lower() != "xTB".lower():
if ff is None:
raise ValueError(
f"{calculator} force field could not be set up for {self.name!r} "
"(missing parameters); try calculator='UFF'"
)
PE = ff.CalcEnergy()
self.props.update({"E_tot(kcal/mol)": PE})
return self
else:
try:
ase_atoms = ase.Atoms(symbols=self.symbols, positions=self.positions)
ase_atoms.info["charge"] = self.charge
ase_atoms.info["spin"] = self.spin
ase_atoms.calc = calculator
PE = ase_atoms.get_potential_energy() # np.array
if isinstance(PE, float):
PE = rdworks.units.ev2kcalpermol * PE
elif isinstance(PE, np.ndarray | list):
PE = rdworks.units.ev2kcalpermol * float(
PE[0]
) # np.float64 to float
self.props.update({"E_tot(kcal/mol)": PE})
return self
except Exception as e:
# third-party ASE calculators may raise any exception type;
# wrap it but keep the original cause for debugging
raise RuntimeError("ASE calculator error") from e
sync(coord)
¶
Synchronize the conformer coordinates with the provided coord.
Parameters:
-
coord(array) –3D coordinates.
Raises:
-
ValueError–if
coorddoes not have the correct shape (natoms, 3).
Returns:
-
Self(Self) –rdworks.Confobject.
Source code in src/rdworks/conf.py
def sync(self, coord: np.ndarray | list) -> Self:
"""Synchronize the conformer coordinates with the provided `coord`.
Args:
coord (np.array): 3D coordinates.
Raises:
ValueError: if `coord` does not have the correct shape (natoms, 3).
Returns:
Self: `rdworks.Conf` object.
"""
if isinstance(coord, np.ndarray) and coord.shape != (self.natoms, 3):
raise ValueError(f"`coord.shape` should be ({self.natoms},3)")
elif isinstance(coord, list) and len(coord) != self.natoms:
raise ValueError(f"`coord` should be length of {self.natoms}")
for i, a in enumerate(self.rdmol.GetAtoms()):
self.rdmol.GetConformer().SetAtomPosition(a.GetIdx(), coord[i])
return self
to_csv(path, decimals=3)
¶
Writes name, SMILES, and conformer properties to a .csv file.
Parameters:
-
path(str | Path) –output filename or path.
-
decimals(int, default:3) –decimal places for float numbers. Defaults to 3.
Source code in src/rdworks/conf.py
to_df(name='name', smiles='smiles')
¶
Returns a one-row pandas DataFrame of name, SMILES, and conformer properties.
Parameters:
-
name(str, default:'name') –column name for name. Defaults to 'name'.
-
smiles(str, default:'smiles') –column name for SMILES. Defaults to 'smiles'.
Returns:
-
DataFrame–pd.DataFrame: pandas DataFrame.
Source code in src/rdworks/conf.py
def to_df(self, name: str = "name", smiles: str = "smiles") -> "pd.DataFrame":
"""Returns a one-row pandas DataFrame of name, SMILES, and conformer properties.
Args:
name (str, optional): column name for name. Defaults to 'name'.
smiles (str, optional): column name for SMILES. Defaults to 'smiles'.
Returns:
pd.DataFrame: pandas DataFrame.
"""
return rwio.to_df(self, name=name, smiles=smiles)
to_geometry()
¶
Returns geometry input for psi4 or other quantum chemistry software.
Each line has atom symbol and its X, Y, and Z coordinates. Unit is Angstrom.
O 0.0 0.0 0.0 H 0.757 0.586 0.0 H -0.757 0.586 0.0
Example
import psi4 geometry = conf.to_geometry() mol = psi4.geometry(geometry)
Returns:
-
str(str) –geometry input for psi4 or other quantum chemistry software.
Source code in src/rdworks/conf.py
def to_geometry(self) -> str:
"""Returns geometry input for psi4 or other quantum chemistry software.
Each line has atom symbol and its X, Y, and Z coordinates.
Unit is Angstrom.
O 0.0 0.0 0.0
H 0.757 0.586 0.0
H -0.757 0.586 0.0
Example:
import psi4
geometry = conf.to_geometry()
mol = psi4.geometry(geometry)
Returns:
str: geometry input for psi4 or other quantum chemistry software.
"""
return rwio.to_geometry(self)
to_png(width=300, height=300, legend='', atom_index=False, highlight_atoms=None, highlight_bonds=None, redraw=False, coordgen=False, trim=True, path=None)
¶
Draw 2D molecule in PNG format.
Parameters:
-
width(int, default:300) –width. Defaults to 300.
-
height(int, default:300) –height. Defaults to 300.
-
legend(str, default:'') –legend. Defaults to ''.
-
atom_index(bool, default:False) –whether to show atom index. Defaults to False.
-
highlight_atoms(list[int] | None, default:None) –atom(s) to highlight. Defaults to None.
-
highlight_bonds(list[int] | None, default:None) –bond(s) to highlight. Defaults to None.
-
redraw(bool, default:False) –whether to redraw. Defaults to False.
-
coordgen(bool, default:False) –whether to use coordgen. Defaults to False.
-
trim(bool, default:True) –whether to trim white margins. Default to True.
Returns:
-
Image–Image.Image: output PIL Image object.
Source code in src/rdworks/conf.py
def to_png(
self,
width: int = 300,
height: int = 300,
legend: str = "",
atom_index: bool = False,
highlight_atoms: list[int] | None = None,
highlight_bonds: list[int] | None = None,
redraw: bool = False,
coordgen: bool = False,
trim: bool = True,
path: str | Path | None = None,
) -> Image.Image:
"""Draw 2D molecule in PNG format.
Args:
width (int, optional): width. Defaults to 300.
height (int, optional): height. Defaults to 300.
legend (str, optional): legend. Defaults to ''.
atom_index (bool, optional): whether to show atom index. Defaults to False.
highlight_atoms (list[int] | None, optional): atom(s) to highlight. Defaults to None.
highlight_bonds (list[int] | None, optional): bond(s) to highlight. Defaults to None.
redraw (bool, optional): whether to redraw. Defaults to False.
coordgen (bool, optional): whether to use coordgen. Defaults to False.
trim (bool, optional): whether to trim white margins. Default to True.
Returns:
Image.Image: output PIL Image object.
"""
return rwio.to_png(
self,
path=path,
width=width,
height=height,
legend=legend,
atom_index=atom_index,
highlight_atoms=highlight_atoms,
highlight_bonds=highlight_bonds,
redraw=redraw,
coordgen=coordgen,
trim=trim,
)
to_sdf(props=True, path=None)
¶
Returns the SDF-formatted strings with 3D coordinates.
Parameters:
-
props(bool, default:True) –include
propsas SDF properties. Defaults to True. -
path(str | Path | None, default:None) –write to this .sdf (.sdf.gz) file instead of returning the strings. Defaults to None.
Returns:
-
str | None–str | None: strings in the SDF format if
pathis None.
Source code in src/rdworks/conf.py
def to_sdf(self, props: bool = True, path: str | Path | None = None) -> str | None:
"""Returns the SDF-formatted strings with 3D coordinates.
Args:
props (bool, optional): include `props` as SDF properties. Defaults to True.
path (str | Path | None, optional): write to this .sdf (.sdf.gz) file instead
of returning the strings. Defaults to None.
Returns:
str | None: strings in the SDF format if `path` is None.
"""
return rwio.to_sdf(self, path=path, props=props)
to_svg(width=300, height=300, legend='', atom_index=False, highlight_atoms=None, highlight_bonds=None, redraw=False, coordgen=False, optimize=True, path=None)
¶
Draw 2D molecule in SVG format.
Examples:
For Jupyternotebook, wrap the output with SVG:
Parameters:
-
width(int, default:300) –width. Defaults to 300.
-
height(int, default:300) –height. Defaults to 300.
-
legend(str, default:'') –legend. Defaults to ''.
-
atom_index(bool, default:False) –whether to show atom index. Defaults to False.
-
highlight_atoms(list[int] | None, default:None) –atom(s) to highlight. Defaults to None.
-
highlight_bonds(list[int] | None, default:None) –bond(s) to highlight. Defaults to None.
-
redraw(bool, default:False) –whether to redraw. Defaults to False.
-
coordgen(bool, default:False) –whether to use coordgen. Defaults to False.
-
optimize(bool, default:True) –whether to optimize SVG string. Defaults to True.
Returns:
-
str(str) –SVG string
Source code in src/rdworks/conf.py
def to_svg(
self,
width: int = 300,
height: int = 300,
legend: str = "",
atom_index: bool = False,
highlight_atoms: list[int] | None = None,
highlight_bonds: list[int] | None = None,
redraw: bool = False,
coordgen: bool = False,
optimize: bool = True,
path: str | Path | None = None,
) -> str:
"""Draw 2D molecule in SVG format.
Examples:
For Jupyternotebook, wrap the output with SVG:
>>> from IPython.display import SVG
>>> SVG(libr[0].to_svg())
Args:
width (int, optional): width. Defaults to 300.
height (int, optional): height. Defaults to 300.
legend (str, optional): legend. Defaults to ''.
atom_index (bool, optional): whether to show atom index. Defaults to False.
highlight_atoms (list[int] | None, optional): atom(s) to highlight. Defaults to None.
highlight_bonds (list[int] | None, optional): bond(s) to highlight. Defaults to None.
redraw (bool, optional): whether to redraw. Defaults to False.
coordgen (bool, optional): whether to use coordgen. Defaults to False.
optimize (bool, optional): whether to optimize SVG string. Defaults to True.
Returns:
str: SVG string
"""
return rwio.to_svg(
self,
path=path,
width=width,
height=height,
legend=legend,
atom_index=atom_index,
highlight_atoms=highlight_atoms,
highlight_bonds=highlight_bonds,
redraw=redraw,
coordgen=coordgen,
optimize=optimize,
)
to_turbomole(bohr=False)
¶
Returns TURBOMOLE coord file formatted strings.
Turbomole coord file format:
- It starts with the keyword `$coord`.
- Each line after the $coord line specifies an atom, consisting of:
- Three real numbers representing the Cartesian coordinates (x, y, z).
- A string for the element name.
- Optional: an "f" label at the end to indicate that the atom's coordinates are frozen during optimization.
- Coordinates can be given in Bohr (default), Ångström (`$coord angs`), or fractional coordinates (`$coord frac`).
- Optional data groups like periodicity (`$periodic`), lattice parameters (`$lattice`), and cell parameters (`$cell`) can also be included.
- Regarding precision:
The precision of the coordinates is crucial for accurate calculations, especially geometry optimizations.
Tools like the TURBOMOLEOptimizer might check for differences in atomic positions with a tolerance of 1e-13.
Parameters:
-
bohr(bool, default:False) –whether to use Bohr units of the coordinates. Defaults to False. Otherwise, Angstrom units will be used.
Returns:
-
str(str) –TURBOMOLE coord formatted file.
Source code in src/rdworks/conf.py
def to_turbomole(self, bohr: bool = False) -> str:
"""Returns TURBOMOLE coord file formatted strings.
Turbomole coord file format:
- It starts with the keyword `$coord`.
- Each line after the $coord line specifies an atom, consisting of:
- Three real numbers representing the Cartesian coordinates (x, y, z).
- A string for the element name.
- Optional: an "f" label at the end to indicate that the atom's coordinates are frozen during optimization.
- Coordinates can be given in Bohr (default), Ångström (`$coord angs`), or fractional coordinates (`$coord frac`).
- Optional data groups like periodicity (`$periodic`), lattice parameters (`$lattice`), and cell parameters (`$cell`) can also be included.
- Regarding precision:
The precision of the coordinates is crucial for accurate calculations, especially geometry optimizations.
Tools like the TURBOMOLEOptimizer might check for differences in atomic positions with a tolerance of 1e-13.
Args:
bohr (bool): whether to use Bohr units of the coordinates. Defaults to False.
Otherwise, Angstrom units will be used.
Returns:
str: TURBOMOLE coord formatted file.
"""
return rwio.to_turbomole(self, bohr=bohr)
to_xyz(path=None)
¶
Returns XYZ formatted strings (the comment line is the conformer name).
Parameters:
-
path(str | Path | None, default:None) –also write to this file. Defaults to None.
Returns:
-
str(str) –XYZ formatted strings.
Source code in src/rdworks/conf.py
torsion_scan(calculator, torsion_angle_idx=None, simplify=True, interval=20.0, fmax=0.05, water=None, use_converged_only=True)
¶
Calculates potential energy profiles for each torsion angle (see rdworks.torsion.torsion_scan()).
Parameters:
-
calculator(str | Callable) –'MMFF', 'UFF', 'xTB', or ASE calculator; or 'GAFF', 'GAFF2', 'Parsley', 'Sage', 'Rosemary', 'AIMNet2', or 'UMA' (see
Conf.optimize()). -
torsion_angle_idx(int | None, default:None) –key to the torsion indices to calculate. Defaults to None (all).
-
simplify(bool, default:True) –whether to use fragment surrogate. Defaults to True.
-
fmax(float, default:0.05) –fmax of ASE optimizer. Defaults to 0.05.
-
interval(float, default:20.0) –interval of torsion angles in degree. Defaults to 20.0.
-
use_converged_only(bool, default:True) –whether to use only converged data. Defaults to True.
Args for xTB calculator
water (str, optional): water solvation model (choose 'gbsa' or 'alpb') alpb: ALPB solvation model (Analytical Linearized Poisson-Boltzmann). gbsa: generalized Born (GB) model with Surface Area contributions. Defaults to None.
Returns:
-
Self(Self) –modified self.
Source code in src/rdworks/conf.py
def torsion_scan(
self,
calculator: str | Callable,
torsion_angle_idx: int | None = None,
simplify: bool = True,
interval: float = 20.0,
fmax: float = 0.05,
water: str | None = None,
use_converged_only: bool = True,
) -> Self:
"""Calculates potential energy profiles for each torsion angle (see `rdworks.torsion.torsion_scan()`).
Args:
calculator (str | Callable): 'MMFF', 'UFF', 'xTB', or ASE calculator; or 'GAFF', 'GAFF2',
'Parsley', 'Sage', 'Rosemary', 'AIMNet2', or 'UMA' (see `Conf.optimize()`).
torsion_angle_idx (int | None): key to the torsion indices to calculate. Defaults to None (all).
simplify (bool, optional): whether to use fragment surrogate. Defaults to True.
fmax (float, optional): fmax of ASE optimizer. Defaults to 0.05.
interval (float, optional): interval of torsion angles in degree. Defaults to 20.0.
use_converged_only (bool, optional): whether to use only converged data. Defaults to True.
Args for xTB calculator:
water (str, optional): water solvation model (choose 'gbsa' or 'alpb')
alpb: ALPB solvation model (Analytical Linearized Poisson-Boltzmann).
gbsa: generalized Born (GB) model with Surface Area contributions.
Defaults to None.
Returns:
Self: modified self.
"""
return rdworks.torsion.torsion_scan(
self,
calculator=calculator,
torsion_angle_idx=torsion_angle_idx,
simplify=simplify,
interval=interval,
fmax=fmax,
water=water,
use_converged_only=use_converged_only,
)