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:

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

Return type:

int

rdmc.utils.find_internal_torsions(mol: Union[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:

A list of internal torsions.

Return type:

list

rdmc.utils.find_ring_torsions(mol: Union[Mol, RWMol]) list

Find the ring from RDkit molecule.

Parameters:

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

Returns:

A list of ring torsions.

Return type:

list

rdmc.utils.fix_CO_openbabel(obmol: Openbabel.OBMol, correct_CO: bool = True)

Fix the CO perception issue for openbabel molecule.

Parameters:
  • obmol (Openbabel.OBMol) – The Openbabel molecule instance.

  • correct_CO (bool, optional) – Whether to fix this issue. Defaults to True.

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:

A list of element symbols.

Return type:

list

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:

A list of element symbols.

Return type:

list

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:

The coordinates.

Return type:

np.array

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:

A writable RDKit RWMol instance.

Return type:

RWMol

rdmc.utils.parse_xyz_by_jensen(xyz: str, charge: int = 0, allow_charged_fragments: bool = False, use_huckel: bool = False, embed_chiral: bool = True, correct_CO: 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.

  • correctCO (bool, optional) – Defaults to True. In order to get correct RDKit molecule for carbon monoxide ([C-]#[O+]), allow_charged_fragments should be forced to True. This function contains a patch to correct that.

  • 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:

A RDKit Mol corresponding to the xyz.

Return type:

Mol

rdmc.utils.parse_xyz_by_openbabel(xyz: str, correct_CO: bool = True)

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

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

  • correctCO (bool, optional) – It is known that openbabel will parse carbon monoxide as [C]=O instead of [C-]#[O+]. This function contains a patch to correct that. Defaults to True.

Returns:

An openbabel molecule from the xyz

Return type:

ob.OBMol

rdmc.utils.rdkit_mol_to_openbabel_mol(rdmol: Union[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:

An openbabel OBMol instance.

Return type:

OBMol

rdmc.utils.rdkit_mol_to_openbabel_mol_manual(rdmol: Union[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:

An openbabel OBMol instance.

Return type:

OBMol

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:

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

Return type:

RWMol

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: Union[Conformer, RDKitConf], coords: Union[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.