Esempio n. 1
0
    def test_init_from_and_to_stream(self):
        mols = makeStereoExamples() * 10
        holder = rdSubstructLibrary.CachedSmilesMolHolder()

        # one day I'll fix this, but we need to write text but read binary
        #  grrr....  something about the python_streambuf handler.
        slib = rdSubstructLibrary.SubstructLibrary(holder, None)

        for mol in mols:
            holder.AddSmiles(Chem.MolToSmiles(mol, isomericSmiles=True))

        if rdSubstructLibrary.SubstructLibraryCanSerialize():
            fd, path = tempfile.mkstemp()
            with open(path, 'w') as file:
                slib.ToStream(file)

            with open(path, 'rb') as file:
                slib2 = rdSubstructLibrary.SubstructLibrary()
                slib2.InitFromStream(file)
                self.assertEqual(len(slib), len(slib2))

        from io import StringIO, BytesIO
        s = StringIO()
        slib.ToStream(s)

        sb = BytesIO(s.getvalue().encode("ascii"))
        self.assertTrue(len(sb.getvalue()) > 0)
        slib3 = rdSubstructLibrary.SubstructLibrary()
        slib3.InitFromStream(sb)
        self.assertEqual(len(slib), len(slib2))
Esempio n. 2
0
  def test0SubstructLibrary(self):
    for fpholderCls in [None, rdSubstructLibrary.PatternHolder]:
      for holder in [rdSubstructLibrary.MolHolder(), rdSubstructLibrary.CachedMolHolder(),
                     rdSubstructLibrary.CachedSmilesMolHolder()]:
        if fpholderCls: fpholder = fpholderCls()
        else: fpholder = None
        slib_ = rdSubstructLibrary.SubstructLibrary(holder, fpholder)
        for i in range(100):
            m = Chem.MolFromSmiles("c1ccccc1")
            self.assertEqual(slib_.AddMol(m), i)

        libs = [slib_]
        if rdSubstructLibrary.SubstructLibraryCanSerialize():
          serialized1 = pickle.loads(pickle.dumps(slib_))
          serialized2 = rdSubstructLibrary.SubstructLibrary(slib_.Serialize())
          libs.append(serialized1)
          libs.append(serialized2)
          
        for slib in libs:
          res = slib.GetMatches(m)
          t2 = time.time()
          self.assertTrue(len(res) == 100)

          res = slib.GetMatches(m)

          self.assertEqual(len(res), 100)
          self.assertTrue(set(res) == set(list(range(100))))

          res = slib.GetMatches(m, maxResults=100);
          self.assertEqual(len(res), 100)
          self.assertEqual(len(slib.GetMatches(m, startIdx=0, endIdx=100)), 100)

          self.assertTrue(slib.HasMatch(m))
          self.assertEqual(slib.CountMatches(m), 100)
Esempio n. 3
0
    def testOptions(self):
        mols = makeStereoExamples() * 10

        for holderCls in [
                rdSubstructLibrary.MolHolder,
                rdSubstructLibrary.CachedMolHolder,
                rdSubstructLibrary.CachedSmilesMolHolder,
                rdSubstructLibrary.CachedTrustedSmilesMolHolder,
        ]:
            holder = holderCls()
            slib_ = rdSubstructLibrary.SubstructLibrary(holder, None)

            for mol in mols:
                slib_.AddMol(mol)

            libs = [slib_]
            if rdSubstructLibrary.SubstructLibraryCanSerialize():
                serialized1 = pickle.loads(pickle.dumps(slib_))
                serialized2 = rdSubstructLibrary.SubstructLibrary(
                    slib_.Serialize())
                libs.append(serialized1)
                libs.append(serialized2)

            for slib in libs:
                core = Chem.MolFromSmarts("C-1-C-C-O-C(-*)(-*)1")
                res = slib.GetMatches(core)
                self.assertEqual(
                    len(res),
                    len([
                        x for x in mols
                        if x.HasSubstructMatch(core, useChirality=True)
                    ]))

                core = Chem.MolFromSmarts("C-1-C-C-O-C(-[O])(-[N])1")
                core.SetProp("core", "core")
                res = slib.GetMatches(core, useChirality=False)
                self.assertEqual(
                    len(res),
                    len([
                        x for x in mols
                        if x.HasSubstructMatch(core, useChirality=False)
                    ]))

                core = Chem.MolFromSmarts("C-1-C-C-O-[C@@](-[O])(-[N])1")
                res = slib.GetMatches(core, useChirality=False)
                self.assertEqual(
                    len(res),
                    len([
                        x for x in mols
                        if x.HasSubstructMatch(core, useChirality=False)
                    ]))

                core = Chem.MolFromSmarts("C-1-C-C-O-[C@@](-[O])(-[N])1")
                res = slib.GetMatches(core)
                self.assertEqual(
                    len(res),
                    len([
                        x for x in mols
                        if x.HasSubstructMatch(core, useChirality=True)
                    ]))
Esempio n. 4
0
    def test1SubstructLibrary(self):
        for keyholderCls in [None, rdSubstructLibrary.KeyFromPropHolder]:
            for fpholderCls in [None, rdSubstructLibrary.PatternHolder]:
                for holder in [
                        rdSubstructLibrary.MolHolder(),
                        rdSubstructLibrary.CachedMolHolder(),
                        rdSubstructLibrary.CachedSmilesMolHolder()
                ]:
                    if fpholderCls: fpholder = fpholderCls()
                    else: fpholder = None
                    if keyholderCls:
                        keyholder = keyholderCls()
                        self.assertEqual(keyholder.GetPropName(), "_Name")
                    else:
                        keyholder = None

                    slib_ = rdSubstructLibrary.SubstructLibrary(
                        holder, fpholder, keyholder)
                    mols = []
                    for i in range(100):
                        m = Chem.MolFromSmiles("c1ccccc1")
                        m.SetProp("_Name", str(i * 2))
                        self.assertEqual(slib_.AddMol(m), i * 2)
                        mols.append(m)
                        m2 = Chem.MolFromSmiles("CCCC")
                        m2.SetProp("_Name", str(i * 2 + 1))
                        self.assertEqual(slib_.AddMol(m2), i * 2 + 1)
                        mols.append(m2)

                    libs = [slib_]
                    if rdSubstructLibrary.SubstructLibraryCanSerialize():
                        serialized1 = pickle.loads(pickle.dumps(slib_))
                        serialized2 = rdSubstructLibrary.SubstructLibrary(
                            slib_.Serialize())
                        libs.append(serialized1)
                        libs.append(serialized2)

                    for slib in libs:
                        res = slib.GetMatches(m)
                        self.assertEqual(len(res), 100)
                        self.assertEqual(set(res), set(list(range(0, 200, 2))))
                        if keyholderCls:
                            self.assertEqual(
                                [str(idx) for idx in res],
                                [str(idx) for idx in range(0, 200, 2)])

                        res = slib.GetMatches(m2)
                        self.assertEqual(len(res), 100)
                        self.assertTrue(
                            set(res) == set(list(range(1, 200, 2))))
                        if keyholderCls:
                            self.assertEqual(
                                [str(idx) for idx in res],
                                [str(idx) for idx in range(1, 200, 2)])

                        res = slib.GetMatches(m)
                        self.assertEqual(len(res), 100)

                        res = slib.GetMatches(m, maxResults=100)
                        self.assertEqual(len(res), 100)

                        self.assertEqual(
                            len(slib.GetMatches(m, startIdx=0, endIdx=50 * 2)),
                            50)
                        self.assertEqual(
                            len(
                                slib.GetMatches(m2,
                                                startIdx=1,
                                                endIdx=50 * 2 + 1)), 50)

                        self.assertTrue(slib.HasMatch(m))
                        self.assertTrue(slib.HasMatch(m2))
                        self.assertEqual(slib.CountMatches(m), 100)
                        self.assertEqual(slib.CountMatches(m2), 100)