Пример #1
0
    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))
Пример #2
0
    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())
Пример #3
0
    def test1Small(self):
        #writer = Chem.SDWriter("test.sdf")
        # single double and tripple atoms cases should not fail
        mol = Chem.MolFromSmiles('O')
        rdDistGeom.EmbedMolecule(mol, 10, 1)
        conf = mol.GetConformer()
        self.assertTrue(lstEq(conf.GetAtomPosition(0), [0.0, 0.0, 0.0]))
        #writer.write(mol)

        mol = Chem.MolFromSmiles('CO')
        rdDistGeom.EmbedMolecule(mol, 10, 1)
        conf = mol.GetConformer()
        self.assertTrue(lstEq(conf.GetAtomPosition(0), [0.69192, 0.0, 0.0]))
        self.assertTrue(lstEq(conf.GetAtomPosition(1), [-0.69192, 0.0, 0.0]))
        #writer.write(mol)

        mol = Chem.MolFromSmiles('CCC')
        rdDistGeom.EmbedMolecule(mol, 10, 1)
        conf = mol.GetConformer()
        self.assertTrue(
            lstEq(conf.GetAtomPosition(0), [-1.21676, -0.2989, 0.0]))
        self.assertTrue(
            lstEq(conf.GetAtomPosition(1), [-0.00604, 0.59337, 0.0]))
        self.assertTrue(
            lstEq(conf.GetAtomPosition(2), [1.22281, -0.29446, 0.0]))
        #writer.write(mol)

        mol = Chem.MolFromSmiles('O=C=O')
        rdDistGeom.EmbedMolecule(mol, 10, 1)
        conf = mol.GetConformer()

        #writer.write(mol)
        self.assertTrue(
            lstEq(conf.GetAtomPosition(0), [-1.2180, -0.06088, 0.0]))
        self.assertTrue(
            lstEq(conf.GetAtomPosition(1), [-0.00408, 0.12116, 0.0]))
        self.assertTrue(
            lstEq(conf.GetAtomPosition(2), [1.22207, -0.060276, 0.0]))

        mol = Chem.MolFromSmiles('C=C=C=C')
        rdDistGeom.EmbedMolecule(mol, 10, 1)
        conf = mol.GetConformer()

        #writer.write(mol)

        d1 = computeDist(conf.GetAtomPosition(0), conf.GetAtomPosition(1))
        self.assertTrue(feq(d1, 1.31, 0.01))
        d2 = computeDist(conf.GetAtomPosition(0), conf.GetAtomPosition(2))
        self.assertTrue(feq(d2, 2.59, 0.05))
        d3 = computeDist(conf.GetAtomPosition(0), conf.GetAtomPosition(3))
        self.assertTrue(feq(d3, 3.84, 0.1))
        d4 = computeDist(conf.GetAtomPosition(1), conf.GetAtomPosition(2))
        self.assertTrue(feq(d4, 1.29, 0.01))
        d5 = computeDist(conf.GetAtomPosition(1), conf.GetAtomPosition(3))
        self.assertTrue(feq(d5, 2.54, 0.1))
        d6 = computeDist(conf.GetAtomPosition(2), conf.GetAtomPosition(3))
        self.assertTrue(feq(d6, 1.31, 0.01))
Пример #4
0
    def test7MMFFO3A(self):
        " make sure we generate an error if parameters are missing (github issue 158) "

        m1 = Chem.MolFromSmiles('c1ccccc1Cl')
        rdDistGeom.EmbedMolecule(m1)
        m2 = Chem.MolFromSmiles('c1ccccc1B(O)O')
        rdDistGeom.EmbedMolecule(m1)

        self.assertRaises(ValueError, lambda: rdMolAlign.GetO3A(m1, m2))
        self.assertRaises(ValueError, lambda: rdMolAlign.GetO3A(m2, m1))
Пример #5
0
    def test7MMFFO3A(self):
      " make sure we generate an error if parameters are missing (github issue 158) "
      sdf = os.path.join(RDConfig.RDBaseDir,'Code','GraphMol',
                         'MolAlign', 'test_data', 'ref_e2.sdf')

      m1 = Chem.MolFromSmiles('c1ccccc1Cl')
      rdDistGeom.EmbedMolecule(m1)
      m2 = Chem.MolFromSmiles('c1ccccc1B(O)O')
      rdDistGeom.EmbedMolecule(m1)

      self.failUnlessRaises(ValueError,lambda :rdMolAlign.GetO3A(m1, m2))
      self.failUnlessRaises(ValueError,lambda :rdMolAlign.GetO3A(m2, m1))
Пример #6
0
  def testIncludeOnly(self):
    cfac = ChemicalFeatures.BuildFeatureFactory(
      os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'MolChemicalFeatures', 'test_data',
                   'featDef.txt'))
    self.failUnless(cfac.GetNumFeatureDefs() == 2)

    mol = Chem.MolFromSmiles("COCN")
    rdDistGeom.EmbedMolecule(mol)

    self.failUnless(cfac.GetNumMolFeatures(mol, includeOnly="HBondAcceptor") == 2)
    self.failUnless(cfac.GetNumMolFeatures(mol, includeOnly="HBondDonor") == 1)
    self.failUnless(cfac.GetNumMolFeatures(mol, includeOnly="Bogus") == 0)

    self.failUnlessRaises(IndexError, lambda: cfac.GetMolFeature(mol, 1, includeOnly="HBondDonor"))
    self.failUnlessRaises(IndexError,
                          lambda: cfac.GetMolFeature(mol, 2, includeOnly="HBondAcceptor"))
    f = cfac.GetMolFeature(mol, 0, includeOnly="HBondDonor")
    self.failUnless(f.GetFamily() == 'HBondDonor')

    feats = cfac.GetFeaturesForMol(mol, includeOnly="HBondAcceptor")
    self.failUnless(len(feats) == 2)
    feats = cfac.GetFeaturesForMol(mol, includeOnly="HBondDonor")
    self.failUnless(len(feats) == 1)
    feats = cfac.GetFeaturesForMol(mol, includeOnly="Bogus")
    self.failUnless(len(feats) == 0)
Пример #7
0
def saveimg(smile, pltnm):
    atorvastatin = Chem.MolFromSmiles(smile)
    mh = Chem.AddHs(atorvastatin)
    rdDistGeom.EmbedMolecule(mh)
    _, res = rdEHTTools.RunMol(mh)
    static_chgs = res.GetAtomicCharges()[:atorvastatin.GetNumAtoms()]
    d = Draw.MolDraw2DCairo(400, 400)
    SimilarityMaps.GetSimilarityMapFromWeights(atorvastatin,
                                               list(static_chgs),
                                               draw2d=d)
    d.FinishDrawing()
    thing = show_png(d.GetDrawingText())
    name = "http://localhost:5006/fol/static/" + pltnm + ".png"
    thing.save(
        "C:\\Users\\patil.py\\Documents\\11F-Drive\\PFastWebLocalApp\\FlavorTool\\fol\\static\\"
        + pltnm + ".png")

    p = figure(x_range=(0, 1),
               y_range=(0, 1),
               toolbar_location=None,
               plot_width=200,
               plot_height=200)
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_color = None
    p.axis.visible = False
    thing = WheelZoomTool()
    p.add_tools(thing)
    p.toolbar.active_scroll = thing

    p.image_url(url=[name], x=0, y=1, w=1, h=1)
    return p
Пример #8
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)
Пример #9
0
    def test12CrippenO3A(self):
        " test CrippenO3A with constraints "

        #we superimpose two identical coplanar 4-phenylpyridines:
        #1) the usual way
        #2) forcing the pyridine nitrogen to match with the para
        #   carbon of the phenyl ring
        m = Chem.MolFromSmiles('n1ccc(cc1)-c1ccccc1')
        m1 = Chem.AddHs(m)
        rdDistGeom.EmbedMolecule(m1)
        mp = ChemicalForceFields.MMFFGetMoleculeProperties(m1)
        ff = ChemicalForceFields.MMFFGetMoleculeForceField(m1, mp)
        ff.Minimize()
        sub1 = m1.GetSubstructMatch(Chem.MolFromSmarts('nccc-cccc'))
        nIdx = sub1[0]
        cIdx = sub1[-1]
        dihe = sub1[2:6]
        rdMolTransforms.SetDihedralDeg(m1.GetConformer(), dihe[0], dihe[1],
                                       dihe[2], dihe[3], 0)
        m2 = copy.copy(m1)
        rdMolAlign.RandomTransform(m2)
        m3 = copy.copy(m2)
        pyO3A = rdMolAlign.GetCrippenO3A(m2, m1)
        pyO3A.Align()
        d = m2.GetConformer().GetAtomPosition(cIdx). \
          Distance(m1.GetConformer().GetAtomPosition(cIdx))
        self.assertAlmostEqual(d, 0, 0)
        pyO3A = rdMolAlign.GetCrippenO3A(m3, m1, constraintMap=[[cIdx, nIdx]])
        pyO3A.Align()
        d = m3.GetConformer().GetAtomPosition(cIdx). \
          Distance(m1.GetConformer().GetAtomPosition(cIdx))
        self.assertAlmostEqual(d, 7, 0)
Пример #10
0
 def testGithub2057(self):
         # ensure that ETKDG is the default Embedder
     mol = Chem.AddHs(Chem.MolFromSmiles('OCCC'))
     fn = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'DistGeomHelpers', 'test_data',
                       'simple_torsion.etkdg.mol')
     ref = Chem.MolFromMolFile(fn, removeHs=False)
     self.assertEqual(rdDistGeom.EmbedMolecule(mol, randomSeed=42), 0)
     self._compareConfs(mol, ref, 0, 0)
Пример #11
0
 def test10ETKDGv2(self):
     mol = Chem.AddHs(Chem.MolFromSmiles('n1cccc(C)c1ON'))
     fn = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'DistGeomHelpers', 'test_data',
                       'torsion.etkdg.v2.mol')
     ref = Chem.MolFromMolFile(fn, removeHs=False)
     params = rdDistGeom.ETKDGv2()
     params.randomSeed = 42
     self.assertEqual(rdDistGeom.EmbedMolecule(mol, params), 0)
     self._compareConfs(mol, ref, 0, 0)
Пример #12
0
 def testOptimizeMolecule(self):
     m = Chem.AddHs(Chem.MolFromSmiles("CCCO"))
     self.assertIsNotNone(m)
     self.assertEqual(rdDistGeom.EmbedMolecule(m), 0)
     mp = ChemicalForceFields.MMFFGetMoleculeProperties(m)
     ff = ChemicalForceFields.MMFFGetMoleculeForceField(m, mp)
     before = ff.CalcEnergy()
     self.assertEqual(
         ChemicalForceFields.OptimizeMolecule(ff, maxIters=200), 0)
     after = ff.CalcEnergy()
     self.assertLess(after, before)
Пример #13
0
    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)

        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)

        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)
Пример #14
0
    def test14Github385(self):
        """ test github issue 385:
        O3A code generating incorrect results for multiconformer molecules
      """
        def _multiConfFromSmiles(smiles, nConfs=10, maxIters=500):
            """Adds hydrogens to molecule and optimises a chosen number of conformers.  Returns the optimised RDKit mol."""
            idea = Chem.MolFromSmiles(smiles)
            idea = Chem.AddHs(idea)
            confs = rdDistGeom.EmbedMultipleConfs(idea, nConfs)

            for conf in confs:
                opt = ChemicalForceFields.MMFFOptimizeMolecule(
                    idea, confId=conf, maxIters=maxIters)
            return idea

        def _confsToAlignedMolsList(multiConfMol):
            """Input is a multiconformer RDKit mol.  Output is an aligned set of conformers as a list of RDKit mols."""
            rdMolAlign.AlignMolConformers(multiConfMol)
            ms = []
            cids = [x.GetId() for x in multiConfMol.GetConformers()]
            for cid in cids:
                newmol = Chem.Mol(multiConfMol)
                for ocid in cids:
                    if ocid == cid:
                        continue
                    newmol.RemoveConformer(ocid)
                ms.append(newmol)
            return ms

        reference = Chem.MolFromSmiles("c1ccccc1N2CCC(NS(=O)(=O)C(F)(F)F)CC2")
        reference = Chem.AddHs(reference)
        rdDistGeom.EmbedMolecule(reference)
        idea1 = _multiConfFromSmiles("c1ccccc1C2CCCCC2", 10)

        idea1_mols = _confsToAlignedMolsList(idea1)
        cids = [x.GetId() for x in idea1.GetConformers()]

        refParams = ChemicalForceFields.MMFFGetMoleculeProperties(reference)
        prbParams = ChemicalForceFields.MMFFGetMoleculeProperties(idea1)

        for i in range(len(cids)):
            o3a1 = rdMolAlign.GetO3A(idea1_mols[i], reference, prbParams,
                                     refParams)
            score1 = o3a1.Score()

            o3a2 = rdMolAlign.GetO3A(idea1,
                                     reference,
                                     prbParams,
                                     refParams,
                                     prbCid=cids[i])
            score2 = o3a2.Score()
            self.assertAlmostEqual(score1, score2, 3)
Пример #15
0
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)
Пример #16
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
Пример #17
0
    def test7ConstrainedEmbedding(self):
        ofile = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol',
                             'DistGeomHelpers', 'test_data', 'constrain1.sdf')
        suppl = Chem.SDMolSupplier(ofile)
        ref = next(suppl)
        probe = copy.deepcopy(ref)

        cMap = {}
        for i in range(5):
            cMap[i] = ref.GetConformer().GetAtomPosition(i)
        ci = rdDistGeom.EmbedMolecule(probe, coordMap=cMap, randomSeed=23)
        self.assertTrue(ci > -1)
        algMap = list(zip(range(5), range(5)))
        ssd = rdMolAlign.AlignMol(probe, ref, atomMap=algMap)
        self.assertTrue(ssd < 0.1)
Пример #18
0
def template_embed_optimize(molecule_embed,molecule,mol_1,args,log):

	#assigning and embedding onto the core
	num_atom_match = molecule_embed.GetSubstructMatch(mol_1)

	#add H's to molecule
	molecule_embed = Chem.AddHs(molecule_embed)

	#definition of coordmap, the coreconfID(the firstone =-1)
	coordMap = {}
	coreConfId=-1
	randomseed=-1
	force_constant=10000

	# This part selects which atoms from molecule are the atoms of the core
	try:
		coreConf = mol_1.GetConformer(coreConfId)
	except:
		pass
	for k, idxI in enumerate(num_atom_match):
		core_mol_1 = coreConf.GetAtomPosition(k)
		coordMap[idxI] = core_mol_1

	ci = rdDistGeom.EmbedMolecule(molecule_embed, coordMap=coordMap, randomSeed=randomseed)

	if ci < 0:
		log.write('Could not embed molecule.')
		coordMap = None
		algMap = None

	if ci >= 0:
		GetFF = Chem.UFFGetMoleculeForceField(molecule_embed,confId=-1)

		#algin molecule to the core
		algMap = [(k, l) for l, k in enumerate(num_atom_match)]

		for k, idxI in enumerate(num_atom_match):
			for l in range(k + 1, len(num_atom_match)):
				idxJ = num_atom_match[l]
				d = coordMap[idxI].Distance(coordMap[idxJ])
				GetFF.AddDistanceConstraint(idxI, idxJ, d, d, force_constant)
		GetFF.Initialize()
		GetFF.Minimize(maxIts=args.opt_steps_RDKit)
		# rotate the embedded conformation onto the core_mol:
		rdMolAlign.AlignMol(molecule_embed, mol_1, atomMap=algMap,reflect=True,maxIters=100)

	return molecule_embed, coordMap, algMap, ci
    def testBasic(self):
        cfac = ChemicalFeatures.BuildFeatureFactory(
            os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol',
                         'MolChemicalFeatures', 'test_data', 'featDef.txt'))
        self.failUnless(cfac.GetNumFeatureDefs() == 2)

        fNames = cfac.GetFeatureFamilies()
        self.failUnless(len(fNames) == 2)
        self.failUnless(fNames[0] == 'HBondDonor')
        self.failUnless(fNames[1] == 'HBondAcceptor')

        mol = Chem.MolFromSmiles("COCN")
        rdDistGeom.EmbedMolecule(mol,
                                 30,
                                 100,
                                 useExpTorsionAnglePrefs=False,
                                 useBasicKnowledge=False)

        self.failUnless(cfac.GetNumMolFeatures(mol) == 3)
        for i in range(cfac.GetNumMolFeatures(mol)):
            self.failUnless(cfac.GetMolFeature(mol, i))
        # check that the recompute argument works:
        self.failUnless(cfac.GetMolFeature(mol, 0))
        for i in range(cfac.GetNumMolFeatures(mol)):
            self.failUnless(cfac.GetMolFeature(mol, i, "", False))
        self.failUnlessRaises(IndexError, lambda: cfac.GetMolFeature(mol, 3))

        feats = cfac.GetFeaturesForMol(mol)
        self.failUnless(len(feats) == 3)
        fTypes = ['HBondDonor', 'HBondAcceptor', 'HBondAcceptor']

        positions = [[1.3041, -0.6079, 0.0924], [-0.7066, 0.5994, 0.1824],
                     [1.3041, -0.6079, 0.0924]]
        targetAids = [[3], [1], [3]]
        for i, feat in enumerate(feats):
            self.assertEqual(feat.GetFamily(), fTypes[i])
            pos = list(feat.GetPos())
            aids = list(feat.GetAtomIds())
            self.assertEqual(aids, targetAids[i])
            self.assertTrue(lstFeq(pos, positions[i]))
            nmol = feat.GetMol()
            self.assertEqual(Chem.MolToSmiles(nmol), "COCN")
            ncfac = feat.GetFactory()
            self.assertEqual(ncfac.GetNumFeatureDefs(), 2)
            self.assertEqual(feat.GetActiveConformer(), -1)
Пример #20
0
    def testBasic(self):
        cfac = ChemicalFeatures.BuildFeatureFactory(
            os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol',
                         'MolChemicalFeatures', 'test_data', 'featDef.txt'))
        self.failUnless(cfac.GetNumFeatureDefs() == 2)

        fNames = cfac.GetFeatureFamilies()
        self.failUnless(len(fNames) == 2)
        self.failUnless(fNames[0] == 'HBondDonor')
        self.failUnless(fNames[1] == 'HBondAcceptor')

        mol = Chem.MolFromSmiles("COCN")
        rdDistGeom.EmbedMolecule(mol, 30, 100)

        self.failUnless(cfac.GetNumMolFeatures(mol) == 3)
        for i in range(cfac.GetNumMolFeatures(mol)):
            self.failUnless(cfac.GetMolFeature(mol, i))
        # check that the recompute argument works:
        self.failUnless(cfac.GetMolFeature(mol, 0))
        for i in range(cfac.GetNumMolFeatures(mol)):
            self.failUnless(cfac.GetMolFeature(mol, i, "", False))
        self.failUnlessRaises(IndexError, lambda: cfac.GetMolFeature(mol, 3))

        feats = cfac.GetFeaturesForMol(mol)
        self.failUnless(len(feats) == 3)
        fTypes = ['HBondDonor', 'HBondAcceptor', 'HBondAcceptor']

        positions = [[1.3215, -0.6284, 0.0967], [-0.7136, 0.6241, 0.1862],
                     [1.3215, -0.6284, 0.0967]]
        targetAids = [[3], [1], [3]]
        i = 0
        for feat in feats:
            self.failUnless(feat.GetFamily() == fTypes[i])
            pos = list(feat.GetPos())
            aids = list(feat.GetAtomIds())
            self.failUnless(aids == targetAids[i])
            self.failUnless(lstFeq(pos, positions[i]))
            nmol = feat.GetMol()
            self.failUnless(Chem.MolToSmiles(nmol) == "COCN")
            ncfac = feat.GetFactory()
            self.failUnless(ncfac.GetNumFeatureDefs() == 2)
            i += 1
Пример #21
0
def compareWithOld(smilesFile, sdFile):
    smiSup = Chem.SmilesMolSupplier(smilesFile, ",", 0, -1)
    sdsup = Chem.SDMolSupplier(sdFile)
    im = 0
    for mol in smiSup:
        cid = rdDistGeom.EmbedMolecule(mol, 10, 1)
        omol = sdsup[im]
        assert cid == 0
        conf = mol.GetConformer(0)
        oconf = omol.GetConformer()
        nat = mol.GetNumAtoms()
        for i in range(nat):
            #atm = mol.GetAtomWithIdx(i)
            #oatm = omol.GetAtomWithIdx(i)
            pos = conf.GetAtomPosition(i)
            opos = oconf.GetAtomPosition(i)
            if not lstEq(pos, opos):
                return 0
        im += 1
    return 1
Пример #22
0
  def testIssue231(self):
    fdefs = """
DefineFeature HDonor1 [N,O;!H0]
  Family HBondDonor
  Weights 1.0
EndFeature
DefineFeature HAcceptor1 [N,O;H0]
  Family HBondAcceptor
  Weights 1.0
EndFeature
"""
    cfac = ChemicalFeatures.BuildFeatureFactoryFromString(fdefs)

    m = Chem.MolFromSmiles('O=CCCN')
    rdDistGeom.EmbedMolecule(m)
    feats = cfac.GetFeaturesForMol(m)
    for feat in feats:
      feat.GetPos()
    m = None
    for feat in feats:
      feat.GetPos()
Пример #23
0
def convertimplicttosdf(imol, sdffile):
    if check_implicit_hydrogens(imol) or not imol.GetConformer().Is3D():
        msg = ''

        rdDistGeom.EmbedMolecule(imol)
        if rdForceFieldHelpers.MMFFHasAllMoleculeParams(imol):

            res = rdForceFieldHelpers.MMFFOptimizeMolecule(imol)
            if res < 0:
                raise Geometry3DGenerationError(
                    'Cannot generate 3D coordinates')
            elif res > 0:
                msg = 'WARNING: Optimitzation has not converged.'
        elif rdForceFieldHelpers.UFFHasAllMoleculeParams(imol):
            res = rdForceFieldHelpers.UFFOptimizeMolecule(imol)
            if res < 0:
                raise Geometry3DGenerationError(
                    'Cannot generate 3D coordinates')
            elif res > 0:
                msg = 'WARNING: Optimitzation has not converged.'

    return (imol, msg)
Пример #24
0
def FormatConversion(
    inputmol: Chem.Mol,
    method='AM1',
    version='7.1',
    outfile=None,
    outdir=None,
    sdf=False,
) -> Tuple[str, str]:
    """Prepare a molecule to be optimized by MOPAC.

    The preparation goes as follows:

    1. all hydrogens are made explicit,
    2. a 3D conformer is generated from RDKit's stochastic search,
       based on distance geometry and  exerimental crystallographic knowledge,
       Wang S., J. Chem. Inf. Model. (2020), 60(4),2044-2058.
    3. the conformer is converted to a first MOPAC input file with OpenBabel,
    4. semi-empirical method and MOPAC version is added to the MOPAC input file.

    A SDF can also be written to disk from the generated conformer to ease debugging.

    :param method: MOPAC semi-empirical method to be used for molecular geometry oprimization
    :param version: version of MOPAC to use
    :param outfile: name of the output the MOPAC input file
    :param outdir: directory where to create the MOPAC input file
        If not specified, a temporary directory is created but
        THE USER MUST REMOVE IT THEMSELVES USING GeOpt.Dispose.
    :param sdf: whether a sdf file should also be created for easier debugging.
    :return: the directory where the MOPAC input file was created
    """
    # Ensure method and MOPAC version are compatible
    if not IsMethodSupportedByMOPAC(method, version):
        raise ValueError(
            f'Method {method} is not supported by MOPAC {version}.')
    # Step 1: add Hs
    inputmol = Chem.AddHs(inputmol)
    # Step 2: generate conformer
    rdDistGeom.EmbedMolecule(inputmol, rdDistGeom.ETKDGv3())
    pybelmol = pybel.readstring('sdf', Chem.MolToMolBlock(inputmol))
    running_dir = tempfile.mkdtemp() if outdir is None else outdir
    mpo_name = 'temp' if outfile is None else outfile
    if sdf:
        outputmol = pybel.Outputfile('sdf',
                                     os.path.join(running_dir,
                                                  f'{mpo_name}.sdf'),
                                     overwrite=True)
        outputmol.write(pybelmol)
        outputmol.close()
    # Step 3: create first MOPAC input file
    outputmol = pybel.Outputfile('mop',
                                 os.path.join(running_dir, f'{mpo_name}.dat'),
                                 overwrite=True)
    outputmol.write(pybelmol)
    outputmol.close()
    # Step 4: add additional information to MOPAC input file
    with open(os.path.join(running_dir, f'{mpo_name}.dat'), 'r') as f:
        data = f.readlines()
    data[0] = method + (' PRTCHAR \n' if version == '2016' else '  \n')
    with open(os.path.join(running_dir, f'{mpo_name}.dat'), 'w') as f:
        f.write("".join(data))
    return running_dir, f'{mpo_name}.dat'
Пример #25
0
    def test6Chirality(self):
        # turn on chirality and we should get chiral volume that is pretty consistent and
        # positive
        tgtVol = 13.0
        smiles = "Cl[C@](C)(F)Br"
        mol = Chem.MolFromSmiles(smiles)
        cids = rdDistGeom.EmbedMultipleConfs(mol,
                                             30,
                                             maxAttempts=30,
                                             randomSeed=100)
        self.assertTrue(len(cids) == 30)
        for cid in cids:
            conf = mol.GetConformer(cid)
            vol = computeChiralVol(conf.GetAtomPosition(0),
                                   conf.GetAtomPosition(2),
                                   conf.GetAtomPosition(3),
                                   conf.GetAtomPosition(4))
            self.assertTrue(abs(vol - tgtVol) < 1)

        # turn of chirality and now we should see both chiral forms
        smiles = "ClC(C)(F)Br"
        mol = Chem.MolFromSmiles(smiles)
        cids = rdDistGeom.EmbedMultipleConfs(mol,
                                             30,
                                             maxAttempts=30,
                                             randomSeed=120)
        self.assertTrue(len(cids) == 30)
        nPos = 0
        nNeg = 0
        for cid in cids:
            conf = mol.GetConformer(cid)
            vol = computeChiralVol(conf.GetAtomPosition(0),
                                   conf.GetAtomPosition(2),
                                   conf.GetAtomPosition(3),
                                   conf.GetAtomPosition(4))
            self.assertTrue(abs(vol - tgtVol) < 1 or abs(vol + tgtVol) < 1)
            if vol < 0:
                nNeg += 1
            else:
                nPos += 1
        self.assertTrue(nPos > 0)
        self.assertTrue(nNeg > 0)

        tgtVol = 5.0
        for i in range(10):
            smiles = "Cl[C@H](F)Br"
            mol = Chem.MolFromSmiles(smiles)
            ci = rdDistGeom.EmbedMolecule(mol, 30, (i + 1) * 10)
            conf = mol.GetConformer(ci)
            vol = computeChiralVol(conf.GetAtomPosition(0),
                                   conf.GetAtomPosition(1),
                                   conf.GetAtomPosition(2),
                                   conf.GetAtomPosition(3))
            self.assertTrue(abs(vol - tgtVol) < 1, "%s %s" % (vol, tgtVol))

        tgtVol = 3.5
        expected = [
            -3.62, -3.67, -3.72, 3.91, 3.95, 3.98, 3.90, 3.94, 3.98, 3.91
        ]
        nPos = 0
        nNeg = 0
        for i in range(30):
            smiles = "ClC(F)Br"
            mol = Chem.MolFromSmiles(smiles)
            ci = rdDistGeom.EmbedMolecule(mol, 30, (i + 1) * 10)
            conf = mol.GetConformer(ci)
            vol = computeChiralVol(conf.GetAtomPosition(0),
                                   conf.GetAtomPosition(1),
                                   conf.GetAtomPosition(2),
                                   conf.GetAtomPosition(3))
            self.assertTrue(abs(vol - tgtVol) < 1 or abs(vol + tgtVol) < 1)
            if vol < 0:
                nNeg += 1
            else:
                nPos += 1

        self.assertTrue(nPos > 0)
        self.assertTrue(nNeg > 0)

        smiles = "Cl[C@H](F)Br"
        m = Chem.MolFromSmiles(smiles)
        mol = Chem.AddHs(m)
        cids = rdDistGeom.EmbedMultipleConfs(mol,
                                             10,
                                             maxAttempts=30,
                                             randomSeed=100)
        self.assertTrue(len(cids) == 10)
        tgtVol = 10.5
        for cid in cids:
            conf = mol.GetConformer(cid)
            vol = computeChiralVol(conf.GetAtomPosition(0),
                                   conf.GetAtomPosition(2),
                                   conf.GetAtomPosition(3),
                                   conf.GetAtomPosition(4))
            self.assertTrue(abs(vol - tgtVol) < 2.)

        # let's try a little more complicated system
        expectedV1 = -2.0
        expectedV2 = -2.9

        for i in range(5):
            smi = "C1=CC=C(C=C1)[C@H](OC1=C[NH]N=C1)C(=O)[NH]C[C@H](Cl)C1=CC=NC=C1"
            mol = Chem.MolFromSmiles(smi)
            ci = rdDistGeom.EmbedMolecule(mol, randomSeed=(i + 1) * 15)
            self.assertTrue(ci >= 0)
            ff = ChemicalForceFields.UFFGetMoleculeForceField(mol, 10.0, ci)
            ff.Minimize()

            conf = mol.GetConformer(ci)
            vol1 = computeChiralVol(conf.GetAtomPosition(6),
                                    conf.GetAtomPosition(3),
                                    conf.GetAtomPosition(7),
                                    conf.GetAtomPosition(13))
            self.assertTrue(
                abs(vol1 - expectedV1) < 1 or abs(vol1 + expectedV1) < 1)
            if vol1 < 0:
                nNeg += 1
            else:
                nPos += 1

            vol2 = computeChiralVol(conf.GetAtomPosition(17),
                                    conf.GetAtomPosition(16),
                                    conf.GetAtomPosition(18),
                                    conf.GetAtomPosition(19))
            self.assertTrue(
                abs(vol2 - expectedV2) < 1 or abs(vol2 + expectedV2) < 1)

        # remove the chiral specification and we should see other chiral
        # forms of the compound
        expectedV1 = 2.0  #[-2.30, -2.31, -2.30,  2.30, -1.77]
        expectedV2 = 2.8  #[2.90,  2.89,  2.69, -2.90, -2.93]

        self.assertTrue(nPos > 0)
        self.assertTrue(nNeg > 0)
        for i in range(5):
            smi = "C1=CC=C(C=C1)C(OC1=C[NH]N=C1)C(=O)[NH]CC(Cl)C1=CC=NC=C1"
            mol = Chem.MolFromSmiles(smi)
            ci = rdDistGeom.EmbedMolecule(mol, 30, (i + 1) * 10)
            ff = ChemicalForceFields.UFFGetMoleculeForceField(mol, 10.0, ci)
            ff.Minimize()

            conf = mol.GetConformer(ci)
            vol1 = computeChiralVol(conf.GetAtomPosition(6),
                                    conf.GetAtomPosition(3),
                                    conf.GetAtomPosition(7),
                                    conf.GetAtomPosition(13))
            vol2 = computeChiralVol(conf.GetAtomPosition(17),
                                    conf.GetAtomPosition(16),
                                    conf.GetAtomPosition(18),
                                    conf.GetAtomPosition(19))
            self.assertTrue(abs(abs(vol1) - expectedV1) < 1.0)
            self.assertTrue(abs(abs(vol2) - expectedV2) < 1.0)
Пример #26
0
                #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)
Пример #27
0
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
Пример #28
0
                parents = ','.join(
                    ['parent="%s"' % x for x in residues.keys()])
                cmd = 'SetProperty atom %s: select=on' % parents
                tmp = self.doc.DoCommand(cmd)
            if showSurface:
                # create the surface:
                self.doc.DoCommand('Surface')
                obj = Displayable(self.doc)
                self.displayables[name] = obj
                self.doc.DoCommand('SetProperty surface id="*":RD_Visual=%d' %
                                   obj.id)

                self.doc.DoCommand('UnSelectAll')

            self.SetDisplayUpdate(True)

    def Redraw(self):
        self.SetDisplayUpdate(True)


if __name__ == '__main__':
    from rdkit import Chem
    from rdkit.Chem import rdDistGeom, rdForceFieldHelpers

    m = Chem.MolFromSmiles('c1cccc2c1cccc2')
    rdDistGeom.EmbedMolecule(m)
    rdForceFieldHelpers.UFFOptimizeMolecule(m)

    s = MolViewer()
    s.ShowMol(m)
Пример #29
0
def sample_mol():
    mol = rdmolfiles.MolFromSmiles('CN=C=O')
    mol = rdmolops.AddHs(mol, addCoords=True)
    rdDistGeom.EmbedMolecule(mol, rdDistGeom.ETKDG())
    return rdmolops.RemoveHs(mol)