def testProvidingCPCI(self): """ test for a ring molecule, repeated generating a conformer with and without enforcing an additional +ve interaction between a pair of non-bonded atoms (termed CPCI, custom pairwise charge-like interaciton), in every iteration, applying CPCI should yield a conformer where this pair of atoms are further apart. """ for i in range(5): ps = rdDistGeom.EmbedParameters() ps.randomSeed = i ps.useBasicKnowledge = True ps.useRandomCoords = False m1 = Chem.MolFromSmiles("C1CCCC1C") self.assertEqual(rdDistGeom.EmbedMolecule(m1, ps), 0) m2 = Chem.MolFromSmiles("C1CCCC1C") ps = rdDistGeom.EmbedParameters() ps.randomSeed = i ps.useRandomCoords = False ps.useBasicKnowledge = True ps.SetCPCI({(0, 3): 0.9}) self.assertEqual(rdDistGeom.EmbedMolecule(m2, ps), 0) conf1 = m1.GetConformer() conf2 = m2.GetConformer() self.assertTrue( (conf2.GetAtomPosition(3) - conf2.GetAtomPosition(0)).Length() > (conf1.GetAtomPosition(3) - conf1.GetAtomPosition(0)).Length())
def testScaleBoundsMatForce(self): """ for pentane, set a target distance for the 1-5 distance, and generate conformers with changing weights for (all) the atom pair distance restraints, the conformer with the stronger weight for the atom pairs will always have a 1-5 distance closer to the target value than that with the weaker weight. """ target = 4 for i in range(5): ps = rdDistGeom.EmbedParameters() ps.randomSeed = i ps.useBasicKnowledge = True ps.useRandomCoords = False m1 = Chem.MolFromSmiles("CCCCC") bm1 = rdDistGeom.GetMoleculeBoundsMatrix(m1) bm1[0,4] = target bm1[4,0] = target DG.DoTriangleSmoothing(bm1) ps.boundsMatForceScaling = 0.1 ps.SetBoundsMat(bm1) self.assertEqual(rdDistGeom.EmbedMolecule(m1,ps),0) m2 = Chem.MolFromSmiles("CCCCC") ps = rdDistGeom.EmbedParameters() ps.randomSeed = i ps.useBasicKnowledge = True ps.useRandomCoords = False ps.boundsMatForceScaling = 10 ps.SetBoundsMat(bm1) self.assertEqual(rdDistGeom.EmbedMolecule(m2,ps),0) conf1 = m1.GetConformer() conf2 = m2.GetConformer() self.assertTrue(abs((conf2.GetAtomPosition(4)-conf2.GetAtomPosition(0)).Length() - target) < abs((conf1.GetAtomPosition(4)-conf1.GetAtomPosition(0)).Length() - target))
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 testProvidingBoundsMatrix(self): m1 = Chem.MolFromSmiles("C1CCC1C") bm1 = rdDistGeom.GetMoleculeBoundsMatrix(m1) bm1[0, 3] = 1.21 bm1[3, 0] = 1.20 bm1[2, 3] = 1.21 bm1[3, 2] = 1.20 bm1[4, 3] = 1.21 bm1[3, 4] = 1.20 DG.DoTriangleSmoothing(bm1) ps = rdDistGeom.EmbedParameters() ps.useRandomCoords = True ps.SetBoundsMat(bm1) ps.randomSeed = 0xf00d self.assertEqual(rdDistGeom.EmbedMolecule(m1, ps), 0) conf = m1.GetConformer() self.assertAlmostEqual( (conf.GetAtomPosition(3) - conf.GetAtomPosition(0)).Length(), 1.2, delta=0.05) self.assertAlmostEqual( (conf.GetAtomPosition(3) - conf.GetAtomPosition(2)).Length(), 1.2, delta=0.05) self.assertAlmostEqual( (conf.GetAtomPosition(3) - conf.GetAtomPosition(4)).Length(), 1.2, delta=0.05)
def embed_bounds_matrix(mol: Mol, bounds_matrix: np.ndarray, seed: int = 42) -> int: DistanceGeometry.DoTriangleSmoothing(bounds_matrix) ps = rdDistGeom.EmbedParameters() ps.numThreads = 0 # max number of threads supported by the system will be used ps.useRandomCoords = True # recommended for larger molecules ps.clearConfs = False ps.randomSeed = seed ps.SetBoundsMat(bounds_matrix) return rdDistGeom.EmbedMolecule(mol, ps)
#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)