def test9EmbedParams(self): mol = Chem.AddHs(Chem.MolFromSmiles('OCCC')) fn = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'DistGeomHelpers', 'test_data', 'simple_torsion.dg.mol') ref = Chem.MolFromMolFile(fn, removeHs=False) params = rdDistGeom.EmbedParameters() params.randomSeed = 42 self.assertEqual(rdDistGeom.EmbedMolecule(mol, params), 0) self._compareConfs(mol, ref, 0, 0) fn = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'DistGeomHelpers', 'test_data', 'simple_torsion.etdg.mol') ref = Chem.MolFromMolFile(fn, removeHs=False) params = rdDistGeom.EmbedParameters() params.randomSeed = 42 params.useExpTorsionAnglePrefs = True self.assertEqual(rdDistGeom.EmbedMolecule(mol, params), 0) self._compareConfs(mol, ref, 0, 0) params = rdDistGeom.ETDG() params.randomSeed = 42 self.assertEqual(rdDistGeom.EmbedMolecule(mol, params), 0) self._compareConfs(mol, ref, 0, 0) fn = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'DistGeomHelpers', 'test_data', 'simple_torsion.etkdg.mol') ref = Chem.MolFromMolFile(fn, removeHs=False) params = rdDistGeom.EmbedParameters() params.randomSeed = 42 params.useExpTorsionAnglePrefs = True params.useBasicKnowledge = True self.assertEqual(rdDistGeom.EmbedMolecule(mol, params), 0) self._compareConfs(mol, ref, 0, 0) params = rdDistGeom.ETKDG() params.randomSeed = 42 self.assertEqual(rdDistGeom.EmbedMolecule(mol, params), 0) self._compareConfs(mol, ref, 0, 0) fn = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'DistGeomHelpers', 'test_data', 'simple_torsion.kdg.mol') ref = Chem.MolFromMolFile(fn, removeHs=False) params = rdDistGeom.EmbedParameters() params.randomSeed = 42 params.useBasicKnowledge = True self.assertEqual(rdDistGeom.EmbedMolecule(mol, params), 0) self._compareConfs(mol, ref, 0, 0) params = rdDistGeom.KDG() params.randomSeed = 42 self.assertEqual(rdDistGeom.EmbedMolecule(mol, params), 0) self._compareConfs(mol, ref, 0, 0)
def test_mols(): mols = [] all_smiles = [ 'CN=C=O', 'Cc1ccccc1', 'CC1=CC2CC(CC1)O2', 'CCCCCCCCCCCCCCCC' ] for smiles in all_smiles: mol = rdmolfiles.MolFromSmiles(smiles) mol = rdmolops.AddHs(mol, addCoords=True) rdDistGeom.EmbedMolecule(mol, rdDistGeom.ETKDG()) mol = rdmolops.RemoveHs(mol) mol.SetProp('Fitness', str(np.random.rand(1)[0])) mols.append(mol) return mols
def test6RmsPruning(self): smiles = [ 'CC(C)CC(NC(C1[N+]CCC1)=O)C([O-])=O', 'CC(NC(CO)C(O)c1ccc([N+]([O-])=O)cc1)=O', 'CC([N+])C(NC(C)C(N1C(C=O)CCC1)=O)=O', 'CC(NC1C(O)C=C(C([O-])=O)OC1C(O)C(O)CO)=O', 'CCCC=C(NC(C1CC1(C)C)=O)C([O-])=O', 'OCC(O)C(O)C(Cn1c2c(cc(C)c(C)c2)nc-2c(=O)[nH]c(=O)nc12)O' ] nconfs = [] expected = [4, 5, 5, 4, 5, 4] expected = [3, 3, 5, 4, 4, 4] for smi in smiles: mol = Chem.MolFromSmiles(smi) cids = rdDistGeom.EmbedMultipleConfs(mol, 50, maxAttempts=30, randomSeed=100, pruneRmsThresh=1.5) nconfs.append(len(cids)) d = [abs(x - y) for x, y in zip(expected, nconfs)] # print(nconfs) self.assertTrue(max(d) <= 1) # previous settings params = rdDistGeom.ETKDG() params.randomSeed = 100 params.maxIterations = 30 params.pruneRmsThresh = 1.5 params.useSymmetryForPruning = False nconfs = [] expected = [4, 5, 5, 4, 5, 4] for smi in smiles: mol = Chem.MolFromSmiles(smi) cids = rdDistGeom.EmbedMultipleConfs(mol, 50, params) nconfs.append(len(cids)) d = [abs(x - y) for x, y in zip(expected, nconfs)] # print(nconfs) self.assertTrue(max(d) <= 1)
def construct_pos_matrix(mol: rdchem.Mol, out_size: Optional[int] = -1) -> np.ndarray: """Construct relative positions from each atom within the molecule. Params: ------- mol: rdkit.Chem.rdchem.Mol Molecule of interest. out_size: int, optional, default=-1 The size of the returned array. If this option is negative, it does not take any effect. Otherwise, it must be larger than or equal to the number of atoms in the input molecule. If so, the end of the array is padded with zeros. Returns: -------- pos_matrix: np.ndarray, shape=(n,n,3) Relative position (XYZ) coordinates from one atom the others in the mol. Examples: --------- ```python >>> from rdkit import Chem >>> from rdkit.Chem import AllChem >>> smiles = 'N[C@@]([H])([C@]([H])(O2)C)C(=O)N[C@@]([H])(CC(=O)N)C(=O)N[C@@]([H])([C@]([H])' \ '(O)C)C(=O)N[C@@]([H])(Cc1ccc(O)cc1)C(=O)2' >>> mol = Chem.MolFromSmiles(smiles) >>> mol = Chem.AddHs(mol, addCoords=True) >>> AllChem.EmbedMolecule(mol, AllChem.ETKDG()) >>> mol = Chem.RemoveHs(mol) >>> pos_matrix = construct_pos_matrix(mol, out_size=-1) >>> pos_matrix.shape (34,34,3) >>> pos_matrix = construct_pos_matrix(mol, out_size=49) >>> pos_matrix.shape (49,49,3) ``` """ # Obtain initial distance geometry between atoms, if unavilable if mol.GetNumConformers() == 0: mol = rdmolops.AddHs(mol, addCoords=True) rdDistGeom.EmbedMolecule(mol, rdDistGeom.ETKDG()) mol = rdmolops.RemoveHs(mol) coords = mol.GetConformer().GetPositions() # shape=(N,3) N = mol.GetNumAtoms() # Determine appropiate output size to generate feature matrix of same size for all mols. if out_size < 0: size = N elif out_size >= N: size = out_size else: raise ValueError( '`out_size` (N={}) is smaller than number of atoms in mol (N={})'. format(out_size, N)) pos_matrix = np.zeros(shape=(size, size, 3), dtype=np.float) for atom_idx in range(N): atom_pos = coords[atom_idx] # central atom of interest for neighbor_idx in range(N): neigh_pos = coords[neighbor_idx] # neighboring atom pos_matrix[ atom_idx, neighbor_idx] = atom_pos - neigh_pos # dist between neighbor -> center return pos_matrix
def sample_mol(): mol = rdmolfiles.MolFromSmiles('CN=C=O') mol = rdmolops.AddHs(mol, addCoords=True) rdDistGeom.EmbedMolecule(mol, rdDistGeom.ETKDG()) return rdmolops.RemoveHs(mol)
#print("({}, {}) {} < x < {}".format(i, j, bm[j, i], bm[i, j])) if bm[i, j] < bm[j, i]: print( " ***** Assertion failed !! (Before smoothing) *****" ) assert (bm[i, j] >= bm[j, i]) if DG.DoTriangleSmoothing(bm) == False: print("Smoothing failed") for i in range(len(bm)): for j in range(i + 1, len(bm)): #print("({}, {}) {} < x < {}".format(i, j, bm[j, i], bm[i, j])) if bm[i, j] < bm[j, i]: print( " ***** Assertion failed !! (After smoothing) *****") assert (bm[i, j] >= bm[j, i]) ps = rdDistGeom.EmbedParameters() ps.useRandomCoords = True ps.SetBoundsMat(bm) ps.randomSeed = 0xf00d try: rdDistGeom.EmbedMolecule(mol, ps) except: print("Failed to generate coordinates") ps = rdDistGeom.ETKDG() ps.useRandomCoords = True ps.randomSeed = 0xf00d rdDistGeom.EmbedMolecule(mol, ps) w.write(mol)