Exemple #1
0
    def test1Basics(self):
        rxna = rdChemReactions.ChemicalReaction()
        # also tests empty copy constructor
        for rxn in [rxna, rdChemReactions.ChemicalReaction(rxna)]:
            self.assertTrue(rxn.GetNumReactantTemplates() == 0)
            self.assertTrue(rxn.GetNumProductTemplates() == 0)

            r1 = Chem.MolFromSmarts('[C:1](=[O:2])O')
            rxn.AddReactantTemplate(r1)
            self.assertTrue(rxn.GetNumReactantTemplates() == 1)

            r1 = Chem.MolFromSmarts('[N:3]')
            rxn.AddReactantTemplate(r1)
            self.assertTrue(rxn.GetNumReactantTemplates() == 2)

            r1 = Chem.MolFromSmarts('[C:1](=[O:2])[N:3]')
            rxn.AddProductTemplate(r1)
            self.assertTrue(rxn.GetNumProductTemplates() == 1)

            reacts = (Chem.MolFromSmiles('C(=O)O'), Chem.MolFromSmiles('N'))
            ps = rxn.RunReactants(reacts)
            self.assertTrue(len(ps) == 1)
            self.assertTrue(len(ps[0]) == 1)
            self.assertTrue(ps[0][0].GetNumAtoms() == 3)

            ps = rxn.RunReactants(list(reacts))
            self.assertTrue(len(ps) == 1)
            self.assertTrue(len(ps[0]) == 1)
            self.assertTrue(ps[0][0].GetNumAtoms() == 3)
    def test1Basics(self):
        rxn = rdChemReactions.ChemicalReaction()
        self.failUnless(rxn.GetNumReactantTemplates() == 0)
        self.failUnless(rxn.GetNumProductTemplates() == 0)

        r1 = Chem.MolFromSmarts('[C:1](=[O:2])O')
        rxn.AddReactantTemplate(r1)
        self.failUnless(rxn.GetNumReactantTemplates() == 1)

        r1 = Chem.MolFromSmarts('[N:3]')
        rxn.AddReactantTemplate(r1)
        self.failUnless(rxn.GetNumReactantTemplates() == 2)

        r1 = Chem.MolFromSmarts('[C:1](=[O:2])[N:3]')
        rxn.AddProductTemplate(r1)
        self.failUnless(rxn.GetNumProductTemplates() == 1)

        reacts = (Chem.MolFromSmiles('C(=O)O'), Chem.MolFromSmiles('N'))
        ps = rxn.RunReactants(reacts)
        self.failUnless(len(ps) == 1)
        self.failUnless(len(ps[0]) == 1)
        self.failUnless(ps[0][0].GetNumAtoms() == 3)

        ps = rxn.RunReactants(list(reacts))
        self.failUnless(len(ps) == 1)
        self.failUnless(len(ps[0]) == 1)
        self.failUnless(ps[0][0].GetNumAtoms() == 3)
Exemple #3
0
    def testRandomSample(self):
        log("testRandomSample")
        rgroups = [[Chem.MolFromSmiles("C")] * 10,
                   [Chem.MolFromSmiles("N")] * 5,
                   [Chem.MolFromSmiles("O")] * 6]
        rxn = rdChemReactions.ChemicalReaction()

        randProd = rdChemReactions.RandomSampleStrategy()
        randProd.Initialize(rxn, rgroups)
        self.assertEquals(randProd.GetNumPermutations(), 10 * 5 * 6)
        groups = []
        for i in range(10 * 5 * 6):
            groups.append(tuple(randProd.next()))
        print(len(set(groups)), "out of", 10 * 5 * 6)

        randProd = rdChemReactions.RandomSampleStrategy()
        randProd.Initialize(rxn, rgroups)
        self.assertEquals(randProd.GetNumPermutations(), 10 * 5 * 6)
        groups = []
        for i in range(10):
            groups.append(tuple(randProd.next()))

        for i in range(3):
            print(i, len(set([g[i] for g in groups])), "out of", [10, 5, 6][i])
        copy.copy(randProd)
Exemple #4
0
    def test3MDLParsers(self):
        fileN = os.path.join(self.dataDir, 'AmideBond.rxn')
        rxna = rdChemReactions.ReactionFromRxnFile(fileN)
        print("*" * 44)
        print(fileN)
        print(rxna)
        for rxn in [rxna, rdChemReactions.ChemicalReaction(rxna)]:
            self.assertTrue(rxn)
            self.assertFalse(rxn._getImplicitPropertiesFlag())

            self.assertTrue(rxn.GetNumReactantTemplates() == 2)
            self.assertTrue(rxn.GetNumProductTemplates() == 1)

            reacts = (Chem.MolFromSmiles('C(=O)O'), Chem.MolFromSmiles('N'))
            ps = rxn.RunReactants(reacts)
            self.assertTrue(len(ps) == 1)
            self.assertTrue(len(ps[0]) == 1)
            self.assertTrue(ps[0][0].GetNumAtoms() == 3)

            with open(fileN, 'r') as rxnF:
                rxnBlock = rxnF.read()
            rxn = rdChemReactions.ReactionFromRxnBlock(rxnBlock)
            self.assertTrue(rxn)

            self.assertTrue(rxn.GetNumReactantTemplates() == 2)
            self.assertTrue(rxn.GetNumProductTemplates() == 1)

            reacts = (Chem.MolFromSmiles('C(=O)O'), Chem.MolFromSmiles('N'))
            ps = rxn.RunReactants(reacts)
            self.assertTrue(len(ps) == 1)
            self.assertTrue(len(ps[0]) == 1)
            self.assertTrue(ps[0][0].GetNumAtoms() == 3)
Exemple #5
0
    def testCartesianProduct(self):
        log("testCartesianProduct")
        rxn = rdChemReactions.ChemicalReaction()
        rgroups = [[Chem.MolFromSmiles("C")] * 10,
                   [Chem.MolFromSmiles("N")] * 5,
                   [Chem.MolFromSmiles("O")] * 6]

        cartProd = rdChemReactions.CartesianProductStrategy()
        cartProd.Initialize(rxn, rgroups)
        self.assertEquals(cartProd.GetNumPermutations(), 10 * 5 * 6)
        groups = []
        count = 0
        print(cartProd.__bool__())
        while cartProd:
            groups.append(tuple(cartProd.next()))


#      count += 1
#      assert count <= cartProd.GetNumPermutations()
        self.assertEquals(len(groups), 10 * 5 * 6)
        # see if we are equal to the Python implementation
        g = list(
            itertools.product(list(range(10)), list(range(5)), list(range(6))))
        self.assertEquals(set(g), set(groups))
        copy.copy(cartProd)
 def testProperties(self):
   smirks_thiourea = "[N;$(N-[#6]):3]=[C;$(C=S):1].[N;$(N[#6]);!$(N=*);!$([N-]);!$(N#*);!$([ND3]);!$([ND4]);!$(N[O,N]);!$(N[C,S]=[S,O,N]):2]>>[N:3]-[C:1]-[N+0:2]"
   rxn = rdChemReactions.ReactionFromSmarts(smirks_thiourea)
   self.assertFalse(rxn.HasProp("fooprop"))
   rxn.SetProp("fooprop", "bar", computed=True)
   rxn.SetIntProp("intprop", 3)
   self.assertTrue(rxn.HasProp("fooprop"))
   self.assertTrue(rxn.HasProp("intprop"))
   self.assertEquals(rxn.GetIntProp("intprop"), 3)
   nrxn = rdChemReactions.ChemicalReaction(rxn.ToBinary())
   self.assertFalse(nrxn.HasProp("fooprop"))
   nrxn = rdChemReactions.ChemicalReaction(rxn.ToBinary(Chem.PropertyPickleOptions.AllProps))
   self.assertTrue(nrxn.HasProp("fooprop"))
   nrxn.ClearComputedProps()
   self.assertFalse(nrxn.HasProp("fooprop"))
   self.assertTrue(nrxn.HasProp("intprop"))
   self.assertEquals(nrxn.GetIntProp("intprop"), 3)
Exemple #7
0
 def test_github_4162(self):
     rxn = rdChemReactions.ReactionFromSmarts(
         "[C:1](=[O:2])-[OD1].[N!H0:3]>>[C:1](=[O:2])[N:3]")
     rxn_copy = rdChemReactions.ChemicalReaction(rxn)
     rdChemReactions.SanitizeRxn(rxn)
     rdChemReactions.SanitizeRxn(rxn_copy)
     pkl = rxn.ToBinary()
     rxn_from_pickle = rdChemReactions.ChemicalReaction(pkl)
     rdChemReactions.SanitizeRxn(rxn_from_pickle)
     pkl = pickle.dumps(rxn)
     rxn_from_pickle = pickle.loads(pkl)
     rdChemReactions.SanitizeRxn(rxn_from_pickle)
     pkl = rxn_from_pickle.ToBinary()
     rxn_from_pickle = rdChemReactions.ChemicalReaction(pkl)
     rdChemReactions.SanitizeRxn(rxn_from_pickle)
     pkl = pickle.dumps(rxn_from_pickle)
     rxn_from_pickle = pickle.loads(pkl)
     rdChemReactions.SanitizeRxn(rxn_from_pickle)
Exemple #8
0
    def test20CheckCopyConstructedReactionAtomProps(self):
        RLABEL = "_MolFileRLabel"
        amine_rxn = '$RXN\n\n      ISIS     090220091541\n\n  2  1\n$MOL\n\n  -ISIS-  09020915412D\n\n  3  2  0  0  0  0  0  0  0  0999 V2000\n   -2.9083   -0.4708    0.0000 R#  0  0  0  0  0  0  0  0  0  1  0  0\n   -2.3995   -0.1771    0.0000 C   0  0  0  0  0  0  0  0  0  2  0  0\n   -2.4042    0.4125    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0\n  1  2  1  0  0  0  0\n  2  3  2  0  0  0  0\nV    2 aldehyde\nM  RGP  1   1   1\nM  END\n$MOL\n\n  -ISIS-  09020915412D\n\n  2  1  0  0  0  0  0  0  0  0999 V2000\n    2.8375   -0.2500    0.0000 R#  0  0  0  0  0  0  0  0  0  3  0  0\n    3.3463    0.0438    0.0000 N   0  0  0  0  0  0  0  0  0  4  0  0\n  1  2  1  0  0  0  0\nV    2 amine\nM  RGP  1   1   2\nM  END\n$MOL\n\n  -ISIS-  09020915412D\n\n  4  3  0  0  0  0  0  0  0  0999 V2000\n   13.3088    0.9436    0.0000 C   0  0  0  0  0  0  0  0  0  2  0  0\n   13.8206    1.2321    0.0000 R#  0  0  0  0  0  0  0  0  0  1  0  0\n   13.3028    0.3561    0.0000 N   0  0  0  0  0  0  0  0  0  4  0  0\n   12.7911    0.0676    0.0000 R#  0  0  0  0  0  0  0  0  0  3  0  0\n  1  3  1  0  0  0  0\n  1  2  1  0  0  0  0\n  3  4  1  0  0  0  0\nM  RGP  2   2   1   4   2\nM  END\n'
        rxn = rdChemReactions.ReactionFromRxnBlock(amine_rxn)
        res = []
        for atom in rxn.GetReactantTemplate(0).GetAtoms():
            if atom.HasProp(RLABEL):
                res.append((atom.GetIdx(), atom.GetProp(RLABEL)))
        rxn2 = rdChemReactions.ChemicalReaction(rxn)
        res2 = []

        for atom in rxn2.GetReactantTemplate(0).GetAtoms():
            if atom.HasProp(RLABEL):
                res2.append((atom.GetIdx(), atom.GetProp(RLABEL)))
        self.assertEquals(res, res2)
Exemple #9
0
    def testTimings(self):
        log("testTimings")
        rxn = rdChemReactions.ChemicalReaction()

        rgroups = [[Chem.MolFromSmiles("C")] * 17000,
                   [Chem.MolFromSmiles("N")] * 50000,
                   [Chem.MolFromSmiles("O")] * 4000]
        cartProd = rdChemReactions.CartesianProductStrategy()
        randProd = rdChemReactions.RandomSampleStrategy()
        randAllBBs = rdChemReactions.RandomSampleAllBBsStrategy()
        for r in [cartProd, randProd, randAllBBs]:
            r.Initialize(rxn, rgroups)
            num = 10000000
            t1 = time.time()
            r.Skip(num)
            t2 = time.time()
            print("%s Skipped %s in %s seconds" % (r, num, t2 - t1))
  def test2DaylightParser(self):
    rxna = rdChemReactions.ReactionFromSmarts('[C:1](=[O:2])O.[N:3]>>[C:1](=[O:2])[N:3]')
    for rxn in [rxna, rdChemReactions.ChemicalReaction(rxna)]:
      self.assertTrue(rxn)
      self.assertTrue(rxn.GetNumReactantTemplates()==2)
      self.assertTrue(rxn.GetNumProductTemplates()==1)
      self.assertTrue(rxn._getImplicitPropertiesFlag())

      reacts = (Chem.MolFromSmiles('C(=O)O'),Chem.MolFromSmiles('N'))
      ps = rxn.RunReactants(reacts)
      self.assertTrue(len(ps)==1)
      self.assertTrue(len(ps[0])==1)
      self.assertTrue(ps[0][0].GetNumAtoms()==3)  

      reacts = (Chem.MolFromSmiles('CC(=O)OC'),Chem.MolFromSmiles('CN'))
      ps = rxn.RunReactants(reacts)
      self.assertTrue(len(ps)==1)
      self.assertTrue(len(ps[0])==1)
      self.assertTrue(ps[0][0].GetNumAtoms()==5)  
    def test12Pickles(self):
        rxn = rdChemReactions.ReactionFromSmarts(
            '[C:1]1[O:2][N:3]1>>[C:1]1[O:2].[N:3]1')
        pkl = cPickle.dumps(rxn)
        rxn = cPickle.loads(pkl)
        mol = Chem.MolFromSmiles('C1ON1')
        products = rxn.RunReactants([mol])
        self.failUnlessEqual(len(products), 1)
        for p in products:
            self.failUnlessEqual(len(p), 1)
            self.failUnlessEqual(p[0].GetNumAtoms(), 3)
            self.failUnlessEqual(p[0].GetNumBonds(), 2)

        rxn = rdChemReactions.ChemicalReaction(rxn.ToBinary())
        products = rxn.RunReactants([mol])
        self.failUnlessEqual(len(products), 1)
        for p in products:
            self.failUnlessEqual(len(p), 1)
            self.failUnlessEqual(p[0].GetNumAtoms(), 3)
            self.failUnlessEqual(p[0].GetNumBonds(), 2)
  def test12Pickles(self):
    # 08/05/14
    # This test is changed due to a new behavior of the smarts
    # reaction parser which now allows using parenthesis in products
    # as well. original smiles: '[C:1]1[O:2][N:3]1>>[C:1]1[O:2].[N:3]1'
    rxn = rdChemReactions.ReactionFromSmarts('[C:1]1[O:2][N:3]1>>([C:1]1[O:2].[N:3]1)')
    pkl = cPickle.dumps(rxn)
    rxn = cPickle.loads(pkl)
    mol = Chem.MolFromSmiles('C1ON1')
    products = rxn.RunReactants([mol])
    self.assertEqual(len(products),1)
    for p in products:
      self.assertEqual(len(p),1)
      self.assertEqual(p[0].GetNumAtoms(),3)
      self.assertEqual(p[0].GetNumBonds(),2)

    rxn = rdChemReactions.ChemicalReaction(rxn.ToBinary())
    products = rxn.RunReactants([mol])
    self.assertEqual(len(products),1)
    for p in products:
      self.assertEqual(len(p),1)
      self.assertEqual(p[0].GetNumAtoms(),3)
      self.assertEqual(p[0].GetNumBonds(),2)
Exemple #13
0
    def testEvenPairsSampling(self):
        rxn = rdChemReactions.ChemicalReaction()

        rgroups = [[Chem.MolFromSmiles("C")] * 10,
                   [Chem.MolFromSmiles("N")] * 10,
                   [Chem.MolFromSmiles("O")] * 10]

        rxn = rdChemReactions.ChemicalReaction()
        count = 0
        pairs01 = {}
        pairs12 = {}
        pairs02 = {}

        strategy = rdChemReactions.EvenSamplePairsStrategy()
        strategy.Initialize(rxn, rgroups)
        # try 100 samples
        while count < 100:
            v = strategy.next()
            p01 = (v[0], v[1])
            p12 = (v[1], v[2])
            p02 = (v[0], v[2])
            pairs01[p01] = pairs01.get(p01, 0) + 1
            pairs12[p01] = pairs12.get(p12, 0) + 1
            pairs02[p01] = pairs02.get(p02, 0) + 1
            count += 1

        # each pair should be used rougly once
        self.assertEquals(np.median(list(pairs01.values())), 1.0)
        self.assertEquals(np.median(list(pairs02.values())), 1.0)
        self.assertEquals(np.median(list(pairs12.values())), 1.0)

        # now try 1000
        pairs01 = {}
        pairs12 = {}
        pairs02 = {}
        strategy = rdChemReactions.EvenSamplePairsStrategy()
        strategy.Initialize(rxn, rgroups)
        count = 0
        while count < 1000:
            v = strategy.next()
            p01 = (v[0], v[1])
            p12 = (v[1], v[2])
            p02 = (v[0], v[2])
            pairs01[p01] = pairs01.get(p01, 0) + 1
            pairs12[p01] = pairs12.get(p12, 0) + 1
            pairs02[p01] = pairs02.get(p02, 0) + 1
            count += 1

        # each pair should be used roughly 10 times
        self.assertTrue(9 <= np.median(list(pairs01.values())) <= 11)
        self.assertTrue(9 <= np.median(list(pairs02.values())) <= 11)
        self.assertTrue(9 <= np.median(list(pairs12.values())) <= 11)

        # now try 500
        pairs01 = {}
        pairs12 = {}
        pairs02 = {}
        strategy = rdChemReactions.EvenSamplePairsStrategy()
        strategy.Initialize(rxn, rgroups)
        count = 0
        while count < 500:
            v = strategy.next()
            p01 = (v[0], v[1])
            p12 = (v[1], v[2])
            p02 = (v[0], v[2])
            pairs01[p01] = pairs01.get(p01, 0) + 1
            pairs12[p01] = pairs12.get(p12, 0) + 1
            pairs02[p01] = pairs02.get(p02, 0) + 1
            count += 1

        # each pair should be used roughly 5 times
        self.assertTrue(4 <= np.median(list(pairs01.values())) <= 6)
        self.assertTrue(4 <= np.median(list(pairs02.values())) <= 6)
        self.assertTrue(4 <= np.median(list(pairs12.values())) <= 6)

        self.assertTrue("PAIRSTAT" in strategy.Stats())