File: test1.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (303 lines) | stat: -rw-r--r-- 9,470 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// $Id$
//
//  Copyright (C) 2003-2006 Rational Discovery LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include <RDGeneral/test.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/RDKitQueries.h>
#include <RDGeneral/RDLog.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>

#include "FragCatGenerator.h"

#include "FragCatalogEntry.h"
#include "FragCatParams.h"
#include "FragCatalogUtils.h"
#include "FragFPGenerator.h"
#include <cstdlib>
#include <iostream>
#include <fstream>

#include <GraphMol/Subgraphs/SubgraphUtils.h>
#include <GraphMol/Subgraphs/Subgraphs.h>
#include <GraphMol/FileParsers/MolSupplier.h>
#include <RDGeneral/types.h>
#include <RDGeneral/FileParseException.h>
#include <DataStructs/BitVects.h>

using namespace RDKit;

void testMols(std::vector<std::unique_ptr<ROMol>> &mols, FragFPGenerator &fpGen,
              FragCatalog &fcat) {
  int nDone = 0;
  for (auto &&mi : mols) {
    ExplicitBitVect *fp = fpGen.getFPForMol(*mi, fcat);
    switch (nDone) {
      case 0:
        TEST_ASSERT(fp->getNumOnBits() == 3);
        TEST_ASSERT((*fp)[0]);
        TEST_ASSERT((*fp)[1]);
        TEST_ASSERT((*fp)[2]);
        break;
      case 1:
        TEST_ASSERT(fp->getNumOnBits() == 2);
        TEST_ASSERT((*fp)[1]);
        TEST_ASSERT((*fp)[3]);
        break;
      case 2:
        TEST_ASSERT(fp->getNumOnBits() == 3);
        TEST_ASSERT((*fp)[1]);
        TEST_ASSERT((*fp)[4]);
        TEST_ASSERT((*fp)[5]);
        break;
      case 3:
        TEST_ASSERT(fp->getNumOnBits() == 3);
        TEST_ASSERT((*fp)[1]);
        TEST_ASSERT((*fp)[6]);
        TEST_ASSERT((*fp)[7]);
        break;
      case 4:
        TEST_ASSERT(fp->getNumOnBits() == 2);
        TEST_ASSERT((*fp)[0]);
        TEST_ASSERT((*fp)[8]);
        break;
      case 5:
        TEST_ASSERT(fp->getNumOnBits() == 3);
        TEST_ASSERT((*fp)[0]);
        TEST_ASSERT((*fp)[6]);
        TEST_ASSERT((*fp)[9]);
        break;
      case 6:
        TEST_ASSERT(fp->getNumOnBits() == 5);
        TEST_ASSERT((*fp)[0]);
        TEST_ASSERT((*fp)[1]);
        TEST_ASSERT((*fp)[2]);
        TEST_ASSERT((*fp)[3]);
        TEST_ASSERT((*fp)[10]);
        break;
    }
    delete fp;
    nDone += 1;
  }
}

void test1() {
  std::string rdbase = getenv("RDBASE");
  std::string fname = rdbase + "/Code/GraphMol/FragCatalog/test_data/mols.smi";
  std::string fgrpFile =
      rdbase + "/Code/GraphMol/FragCatalog/test_data/funcGroups.txt";
  SmilesMolSupplier suppl(fname, " ", 0, 1, false);

  auto *fparams = new FragCatParams(1, 6, fgrpFile, 1.0e-8);
  TEST_ASSERT(fparams->getNumFuncGroups() == 15);
  FragCatalog fcat(fparams);
  FragCatGenerator catGen;

  std::vector<std::unique_ptr<ROMol>> mols;
  unsigned int nDone = 0;
  ROMol *m = suppl.next();
  while (m) {
    mols.push_back(std::unique_ptr<ROMol>(m));
    nDone += 1;
    catGen.addFragsFromMol(*m, &fcat);
    try {
      m = suppl.next();
    } catch (FileParseException &) {
      m = nullptr;
    }
  }
  TEST_ASSERT(mols.size() == 16);
  TEST_ASSERT(nDone == 16);
  int nents = fcat.getNumEntries();
  std::cerr << " " << nents << std::endl;
  TEST_ASSERT(nents == 21);
  FragFPGenerator fpGen;

  BOOST_LOG(rdInfoLog) << "----- Test 1" << std::endl;
  testMols(mols, fpGen, fcat);
  BOOST_LOG(rdInfoLog) << "---- Done" << std::endl;

  //----------------------------------------------------------
  //  SERIALIZATION TESTS
  //----------------------------------------------------------

  // make sure we can pickle and unpickle parameter objects:
  FragCatParams newParams;
  std::string pickle = fparams->Serialize();
  newParams.initFromString(pickle);
  TEST_ASSERT(newParams.getLowerFragLength() == fparams->getLowerFragLength());
  TEST_ASSERT(newParams.getUpperFragLength() == fparams->getUpperFragLength());
  TEST_ASSERT(newParams.getTolerance() == fparams->getTolerance());
  TEST_ASSERT(newParams.getNumFuncGroups() == fparams->getNumFuncGroups());

  // make sure we can pickle and unpickle catalog entries:
  const FragCatalogEntry *fpEntry = fcat.getEntryWithIdx(0);
  auto *fpEntry2 = new FragCatalogEntry();
  fpEntry2->initFromString(fpEntry->Serialize());
  TEST_ASSERT(fpEntry->getDescription() == fpEntry2->getDescription());
  TEST_ASSERT(fpEntry->getOrder() == fpEntry2->getOrder());
  TEST_ASSERT(fpEntry->getBitId() == fpEntry2->getBitId());
  TEST_ASSERT(fpEntry2->match(fpEntry, 1e-8));
  TEST_ASSERT(fpEntry->match(fpEntry2, 1e-8));
  delete fpEntry2;

  // test catalogs' initFromString:
  FragCatalog fcat2;
  fcat2.initFromString(fcat.Serialize());
  TEST_ASSERT(fcat2.getNumEntries() == fcat.getNumEntries());
  BOOST_LOG(rdInfoLog) << "----- Test 2" << std::endl;
  testMols(mols, fpGen, fcat2);
  BOOST_LOG(rdInfoLog) << "---- Done" << std::endl;

  // and the pickle ctor:
  FragCatalog *fcat3 = new FragCatalog(fcat.Serialize());
  TEST_ASSERT(fcat3->getNumEntries() == fcat.getNumEntries());
  BOOST_LOG(rdInfoLog) << "----- Test 3" << std::endl;
  testMols(mols, fpGen, *fcat3);
  BOOST_LOG(rdInfoLog) << "---- Done" << std::endl;

  //----------------------------------------------------------
  // test issue 115
  //----------------------------------------------------------
  BOOST_LOG(rdInfoLog) << "----- Test Issue 115" << std::endl;
  delete fparams;
  fparams = new FragCatParams(3, 3, fgrpFile, 1.0e-8);
  TEST_ASSERT(fparams->getNumFuncGroups() == 15);
  delete fcat3;
  fcat3 = new FragCatalog(fparams);
  suppl.reset();
  nDone = 0;
  while (!suppl.atEnd()) {
    nDone++;
    ROMol *m = suppl.next();
    catGen.addFragsFromMol(*m, fcat3);
    delete m;
  }
  TEST_ASSERT(nDone == suppl.length());
  TEST_ASSERT(fcat3->getNumEntries() == 21);
  TEST_ASSERT(fcat3->getFPLength() == 10);
  for (unsigned int i = 0; i < fcat3->getFPLength(); i++) {
    TEST_ASSERT(fcat3->getEntryWithBitId(i)->getOrder() == 3);
  }
  BOOST_LOG(rdInfoLog) << "---- Done" << std::endl;

  //----------------------------------------------------------
  // test issue 117
  //----------------------------------------------------------
  BOOST_LOG(rdInfoLog) << "----- Test Issue 117" << std::endl;
  delete fparams;
  fparams = new FragCatParams(1, 2, fgrpFile, 1.0e-8);
  delete fcat3;
  fcat3 = new FragCatalog(fparams);
  ROMol *tmpMol = SmilesToMol("OC(N)CO");
  TEST_ASSERT(tmpMol);
  catGen.addFragsFromMol(*tmpMol, fcat3);
  TEST_ASSERT(fcat3->getNumEntries() == 1);
  // std::cout << fcat3->getEntryWithBitId(0)->getDescription() << std::endl;
  TEST_ASSERT(fcat3->getEntryWithBitId(0)->getDescription() ==
              "C(<-O>)<-N>C<-O>");
  BOOST_LOG(rdInfoLog) << "---- Done" << std::endl;

  delete tmpMol;
  delete fparams;
  delete fcat3;
}

void testIssue294() {
  BOOST_LOG(rdInfoLog) << "---- Test Issue294\n  Watch memory usage."
                       << std::endl;
  std::string rdbase = getenv("RDBASE");
  std::string fname = rdbase + "/Code/GraphMol/FragCatalog/test_data/mols.smi";
  std::string fgrpFile =
      rdbase + "/Code/GraphMol/FragCatalog/test_data/funcGroups.txt";
  SmilesMolSupplier suppl(fname, " ", 0, 1, false);

  auto *fparams = new FragCatParams(1, 6, fgrpFile, 1.0e-8);
  FragCatalog fcat(fparams);
  FragCatGenerator catGen;

  std::vector<std::unique_ptr<ROMol>> mols;
  ROMol *m = suppl.next();
  while (m) {
    mols.push_back(std::unique_ptr<ROMol>(m));
    catGen.addFragsFromMol(*m, &fcat);
    try {
      m = suppl.next();
    } catch (FileParseException &) {
      m = nullptr;
    }
  }
  int nents = fcat.getNumEntries();
  TEST_ASSERT(nents == 21);
  FragFPGenerator fpGen;

  for (unsigned int i = 0; i < 200; i++) {
    for (auto &&mi : mols) {
      ExplicitBitVect *fp = fpGen.getFPForMol(*mi, fcat);
      delete fp;
    }
  }
  delete fparams;
  BOOST_LOG(rdInfoLog) << "---- Done" << std::endl;
}

void testWhiteSpaceInSmarts() {
  BOOST_LOG(rdInfoLog) << "----- Test Whitespace in SMARTS Fragment string."
                       << std::endl;

  std::vector<std::string> data(
      {"          ",    // whitespace only
       "          \n",  // whitespace plus new line
       " //   initial space plus comment\nfluorine\t[F]\n"});

  std::vector<size_t> reference_sizes({0, 0, 1});

  auto reference = reference_sizes.cbegin();
  for (const auto &smarts : data) {
    std::istringstream input(smarts);
    auto groups = readFuncGroups(input);
    TEST_ASSERT(groups.size() == *reference);
    ++reference;
  }
  BOOST_LOG(rdInfoLog) << "---- Done" << std::endl;
}

void testFragmentWithoutSmarts() {
  BOOST_LOG(rdInfoLog) << "----- Test Fragment string without SMARTS."
                       << std::endl;

  std::vector<std::string> data(
      {"//   Name	SMARTS\nnonsense\n",
       "//   Name	SMARTS\nnonsense no new line",
       "//   Name	SMARTS\nnonsense with tab\t\n"});

  for (const auto &smarts : data) {
    bool ok = false;
    std::istringstream input(smarts);
    RDKit::MOL_SPTR_VECT groups;
    try {
      groups = readFuncGroups(input);
    } catch (const Invar::Invariant &) {
      ok = true;
    }
    TEST_ASSERT(ok);
    TEST_ASSERT(groups.empty());
  }
  BOOST_LOG(rdInfoLog) << "---- Done" << std::endl;
}

int main() {
  RDLog::InitLogs();
  test1();
  testIssue294();
  testWhiteSpaceInSmarts();
  testFragmentWithoutSmarts();
  return 0;
}