rdmc.utils#

This module provides methods that can directly apply to RDKit Mol/RWMol.

rdmc.utils.determine_smallest_atom_index_in_torsion(atom1: rdkit.Chem.rdchem.Atom, atom2: rdkit.Chem.rdchem.Atom) int#

Determine the smallest atom index in mol connected to atom1 which is not atom2. Returns a heavy atom if available, otherwise a hydrogen atom. Useful for deterministically determining the indices of four atom in a torsion. This function assumes there ARE additional atoms connected to atom1, and that atom2 is not a hydrogen atom.

Parameters:
  • atom1 (Atom) – The atom who’s neighbors will be searched.

  • atom2 (Atom) – An atom connected to atom1 to exclude (a pivotal atom).

Returns:

int – The smallest atom index (1-indexed) connected to atom1 which is not atom2.

rdmc.utils.find_internal_torsions(mol: Mol | RWMol, exclude_methyl: bool = False) list#

Find the internal torsions from RDkit molecule.

Parameters:
  • mol (Union[Mol, RWMol]) – RDKit molecule.

  • exclude_methyl (bool) – Whether exclude the torsions with methyl groups.

Returns:

list – A list of internal torsions.

rdmc.utils.find_ring_torsions(mol: Mol | RWMol) list#

Find the ring from RDkit molecule.

Parameters:

mol (Union[Mol, RWMol]) – RDKit molecule.

Returns:

list – A list of ring torsions.

rdmc.utils.get_atom_masses(atom_nums: Iterable)#

Get the atom masses for a given atom index list.

Parameters:

atom_nums (Iterable) – A list of elemental numbers.

Returns:

list – A list of element symbols.

rdmc.utils.get_closed_shell_by_add_hs(mol: RWMol) RWMol#

Get the closed shell molecule of a radical molecule by explicitly adding hydrogen atoms to the molecule.

rdmc.utils.get_closed_shell_cheap(mol: RWMol) RWMol#

Get the closed shell molecule of a radical molecule. This is a cheap version where no new atom is actually added to the molecule and all operation is inplace.

Parameters:

mol (RWMol) – The radical molecule.

Returns:

RWMol – The closed shell molecule.

rdmc.utils.get_element_symbols(atom_nums: Iterable)#

Get the element symbols for a given atom index list.

Parameters:

atom_nums (Iterable) – A list of elemental numbers.

Returns:

list – A list of element symbols.

rdmc.utils.get_internal_coords(obmol, nonredundant: bool = True) list#

Generate a non_redundant_internal coordinate.

Parameters:
  • obmol (OBMol) – Openbabel mol.

  • nonredundant (bool) – whether non-redundant. Defaults to True.

rdmc.utils.get_obmol_coords(obmol: OBMol)#

Get the atom coordinates from an openbabel molecule. If all coordinates are zero, None will be returned.

Parameters:

obmol (OBMol) – The openbabel OBMol to get coordinates from.

Returns:

np.array – The coordinates.

rdmc.utils.get_substruct_match_and_recover_recipe(mol1: RWMol, mol2: RWMol) Tuple[tuple, dict]#

Get the substructure match between two molecules and the recipe to recover mol2 to mol1. If swapping the atom indices in mol2 according to the recipe, mol2 should be the same as mol1.

Parameters:
  • mol1 (RWMol) – The first molecule.

  • mol2 (RWMol) – The second molecule.

Returns:
  • tuple – The substructure match.

  • dict – A truncated atom mapping of mol2 to mol1.

rdmc.utils.openbabel_mol_to_rdkit_mol(obmol: openbabel.OBMol, remove_hs: bool = False, sanitize: bool = True, embed: bool = True) RWMol#

Convert a OpenBabel molecular structure to a Chem.rdchem.RWMol object. :param obmol: An OpenBabel Molecule object for the conversion. :type obmol: Molecule :param remove_hs: Whether to remove hydrogen atoms from the molecule, Defaults to False. :type remove_hs: bool, optional :param sanitize: Whether to sanitize the RDKit molecule. Defaults to True. :type sanitize: bool, optional :param embed: Whether to embeb 3D conformer from OBMol. Defaults to True. :type embed: bool, optional

Returns:

RWMol – A writable RDKit RWMol instance.

rdmc.utils.parse_xyz_by_jensen(xyz: str, charge: int = 0, allow_charged_fragments: bool = False, use_huckel: bool = False, embed_chiral: bool = True, use_atom_maps: bool = False, force_rdmc: bool = False, **kwargs) Mol#

Perceive a xyz str using xyz2mol by Jensen et al. and generate the corresponding RDKit Mol. The implementation refers the following blog: https://greglandrum.github.io/rdkit-blog/posts/2022-12-18-introducing-rdDetermineBonds.html

Parameters:
  • charge – The charge of the species. Defaults to 0.

  • allow_charged_fragmentsTrue for charged fragment, False for radical. Defaults to False.

  • use_huckelTrue to use extended Huckel bond orders to locate bonds. Defaults to False.

  • embed_chiralTrue to embed chiral information. Defaults to True.

  • use_atom_maps (bool, optional) – True to set atom map numbers to the molecule. Defaults to False.

  • force_rdmc (bool, optional) – Defaults to False. In rare case, we may hope to use a tailored version of the Jensen XYZ parser, other than the one available in RDKit. Set this argument to True to force use RDMC’s implementation, which user’s may have some flexibility to modify.

Returns:

Mol – A RDKit Mol corresponding to the xyz.

rdmc.utils.parse_xyz_by_openbabel(xyz: str)#

Perceive a xyz str using openbabel and generate the corresponding OBMol.

Parameters:

xyz (str) – A str in xyz format containing atom positions.

Returns:

ob.OBMol – An openbabel molecule from the xyz

rdmc.utils.rdkit_mol_to_openbabel_mol(rdmol: Mol | RWMol, embed: bool = True) openbabel.OBMol#

Convert a Mol/RWMol to a Openbabel mol. This a temporary replace of rdkit_mol_to_openbabel_mol_manual.

Parameters:
  • rdmol (Mol) – The RDKit Mol/RWMol object to be converted.

  • embed (bool, optional) – Whether to embed conformer into the OBMol. Defaults to True.

Returns:

OBMol – An openbabel OBMol instance.

rdmc.utils.rdkit_mol_to_openbabel_mol_manual(rdmol: Mol | RWMol, embed: bool = True) openbabel.OBMol#

Convert a Mol/RWMol to a Openbabel mol. This function has a problem converting aromatic molecules. Example: ‘c1nc[nH]n1’. Currently use a workaround, converting an RDKit Mol to sdf string and read by openbabel.

Parameters:
  • rdmol (Mol) – The RDKit Mol/RWMol object to be converted.

  • embed (bool, optional) – Whether to embed conformer into the OBMol. Defaults to True.

Returns:

OBMol – An openbabel OBMol instance.

rdmc.utils.reverse_map(map: Iterable, as_list: bool = True)#

Inverse-transform the index and value relationship in a mapping. E.g., when doing a subgraph match, RDKit will returns a list that the indexes correspond to the reference molecule and the values correspond to the probing molecule. One by renumber the atoms in the probing molecule according to the reverse_map, the atom numbering between the two molecules should be consistent

Parameters:
  • map (Iterable) – An atom mapping.

  • as_list (bool, optional) – Output result as a list object. Otherwise, the output is a np.ndarray.

Returns:

An inverted atom map from the given ``match`` atom map

rdmc.utils.rmg_mol_to_rdkit_mol(rmgmol: rmgpy.molecule.Molecule, remove_hs: bool = False, sanitize: bool = True) RWMol#

Convert a RMG molecular structure to an RDKit Mol object. Uses RDKit to perform the conversion. Perceives aromaticity. Adopted from rmgpy/molecule/converter.py

Parameters:
  • rmgmol (Molecule) – An RMG Molecule object for the conversion.

  • remove_hs (bool, optional) – Whether to remove hydrogen atoms from the molecule, True to remove.

  • sanitize (bool, optional) – Whether to sanitize the RDKit molecule, True to sanitize.

Returns:

RWMol – An RWMol molecule object corresponding to the input RMG Molecule object.

rdmc.utils.set_obmol_coords(obmol: OBMol, coords: array)#

Get the atom coordinates from an openbabel molecule. If all coordinates are zero, It will return None

Parameters:
  • obmol (OBMol) – The openbabel OBMol to get coordinates from.

  • coords (np.array) – The coordinates to set.

rdmc.utils.set_rdconf_coordinates(conf: Conformer | RDKitConf, coords: tuple | list | ndarray)#

Set the Positions of atoms of the conformer.

Parameters:
  • conf (Union[Conformer, 'RDKitConf']) – The conformer to be set.

  • coords (Union[tuple, list, np.ndarray]) – The coordinates to be set.

Raises:

ValueError – Not a valid coords input, when giving something else.