def to_smiles( mol: Chem.rdchem.Mol, canonical: bool = True, isomeric: bool = True, ordered: bool = False, explicit_bonds: bool = False, explicit_hs: bool = False, randomize: bool = False, cxsmiles: bool = False, allow_to_fail: bool = False, ) -> Optional[str]: """Convert a mol to a SMILES. Args: mol: a molecule. canonical: if false no attempt will be made to canonicalize the molecule. isomeric: whether to include information about stereochemistry in the SMILES. ordered: whether to force reordering of the atoms first. explicit_bonds: if true, all bond orders will be explicitly indicated in the output SMILES. explicit_hs: if true, all H counts will be explicitly indicated in the output SMILES. randomize: whether to randomize the generated smiles. Override `canonical`. cxsmiles: Whether to return a CXSMILES instead of a SMILES. allow_to_fail: Raise an error if the conversion to SMILES fails. Return None otherwise. """ if ordered and canonical is False: mol = dm.reorder_atoms(mol) if randomize: mol = dm.randomize_atoms(mol) canonical = False smiles = None try: if cxsmiles: smiles = Chem.MolToCXSmiles( # type: ignore mol, isomericSmiles=isomeric, canonical=canonical, allBondsExplicit=explicit_bonds, allHsExplicit=explicit_hs, ) else: smiles = Chem.MolToSmiles( # type: ignore mol, isomericSmiles=isomeric, canonical=canonical, allBondsExplicit=explicit_bonds, allHsExplicit=explicit_hs, ) except Exception as e: if allow_to_fail: raise e return None return smiles
def test_reorder_atoms(): smiles = "c1ccc(C(=O)O)c(c1)OC(=O)C" mol = dm.to_mol(smiles, add_hs=False, explicit_only=False) orders = [a.GetAtomicNum() for a in mol.GetAtoms()] assert orders == [6, 6, 6, 6, 6, 8, 8, 6, 6, 8, 6, 8, 6] mol = dm.reorder_atoms(mol) orders = [a.GetAtomicNum() for a in mol.GetAtoms()] assert orders == [6, 8, 8, 8, 6, 6, 6, 6, 8, 6, 6, 6, 6]
def test_reorder_atoms_without_atoms(): mol1 = dm.to_mol("") mol2 = dm.reorder_atoms(mol1) assert dm.to_smiles(mol1) == dm.to_smiles(mol2)