File: testMultithreadedMolSupplier.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 (423 lines) | stat: -rw-r--r-- 14,630 bytes parent folder | download | duplicates (2)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//
//  Copyright (C) 2020 Shrey Aryan
//
//   @@ 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 <chrono>
#include <memory>

#include <GraphMol/Fingerprints/MorganFingerprints.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <RDGeneral/test.h>
#include <RDStreams/streams.h>

#include <boost/dynamic_bitset.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>

#include "MultithreadedSDMolSupplier.h"
#include "MultithreadedSmilesMolSupplier.h"

namespace io = boost::iostreams;
using namespace RDKit;
using namespace std::chrono;

// thread safe printing for debugging
// Usage Example: PrintThread{} << "something";
struct PrintThread : public std::stringstream {
  inline static std::mutex cout_mutex;
  ~PrintThread() override {
    std::lock_guard<std::mutex> l{cout_mutex};
    std::cout << rdbuf();
  }
};

void testSmiConcurrent(std::istream *strm, bool takeOwnership,
                       std::string delimiter, int smilesColumn, int nameColumn,
                       bool titleLine, bool sanitize,
                       unsigned int numWriterThreads, size_t sizeInputQueue,
                       size_t sizeOutputQueue, unsigned int expectedResult,
                       bool extras = false) {
  unsigned int nMols = 0;
  boost::dynamic_bitset<> bitVector(expectedResult);
  MultithreadedSmilesMolSupplier sup(
      strm, takeOwnership, delimiter, smilesColumn, nameColumn, titleLine,
      sanitize, numWriterThreads, sizeInputQueue, sizeOutputQueue);
  // we have not called the next method yet
  TEST_ASSERT(sup.getLastRecordId() == 0);
  // initially no bit is set in the bitVector, sanity check
  TEST_ASSERT(!bitVector.any());

  while (!sup.atEnd()) {
    ROMol *mol = sup.next();
    if (mol) {
      unsigned int id = sup.getLastRecordId();
      bitVector[id - 1] = 1;
      if (extras) {
        std::unique_ptr<ExplicitBitVect> fp(
            MorganFingerprints::getFingerprintAsBitVect(*mol, 2, 2048));
      }
      ++nMols;
    }
    delete mol;
  }
  // if all bits are set then we have seen possible ids
  TEST_ASSERT(bitVector.all());
  TEST_ASSERT(nMols == expectedResult);
}

void testSmiConcurrent(std::string path, std::string delimiter,
                       int smilesColumn, int nameColumn, bool titleLine,
                       bool sanitize, unsigned int numWriterThreads,
                       size_t sizeInputQueue, size_t sizeOutputQueue,
                       unsigned int expectedResult, bool extras = false) {
  std::string rdbase = getenv("RDBASE");
  std::string fname = rdbase + path;
  std::istream *strm = new std::ifstream(fname.c_str());
  testSmiConcurrent(strm, true, delimiter, smilesColumn, nameColumn, titleLine,
                    sanitize, numWriterThreads, sizeInputQueue, sizeOutputQueue,
                    expectedResult, extras);
}

void testSmiOld(std::string path, std::string delimiter, int smilesColumn,
                int nameColumn, bool titleLine, bool sanitize,
                unsigned int expectedResult, bool extras = false) {
  unsigned int numMols = 0;
  SmilesMolSupplier sup(path, delimiter, smilesColumn, nameColumn, titleLine,
                        sanitize);
  while (!sup.atEnd()) {
    ROMol *mol = sup.next();
    if (mol) {
      if (extras) {
        std::unique_ptr<ExplicitBitVect> fp(
            MorganFingerprints::getFingerprintAsBitVect(*mol, 2, 2048));
      }
      ++numMols;
    }
    delete mol;
  }
  TEST_ASSERT(numMols == expectedResult);
}

void testSmiProperties() {
  std::string rdbase = getenv("RDBASE");
  std::string fname =
      rdbase + "/Code/GraphMol/FileParsers/test_data/fewSmi.2.csv";
  std::vector<std::string> nameVector, tpsaVector;
  std::string tempStr;
  SmilesMolSupplier sup(fname, ",", 1, 0, true);
  MultithreadedSmilesMolSupplier multiSup(fname, ",", 1, 0, true);
  while (!multiSup.atEnd()) {
    std::unique_ptr<ROMol> mol{multiSup.next()};
    if (mol != nullptr) {
      mol->getProp(common_properties::_Name, tempStr);
      nameVector.push_back(tempStr);
      mol->getProp("TPSA", tempStr);
      tpsaVector.push_back(tempStr);
    }
  }

  while (!sup.atEnd()) {
    std::unique_ptr<ROMol> mol{sup.next()};
    if (mol != nullptr) {
      mol->getProp(common_properties::_Name, tempStr);
      TEST_ASSERT(std::find(nameVector.begin(), nameVector.end(), tempStr) !=
                  nameVector.end());
      mol->getProp("TPSA", tempStr);
      TEST_ASSERT(std::find(tpsaVector.begin(), tpsaVector.end(), tempStr) !=
                  tpsaVector.end());
    }
  }
}
void testSmiCorrectness() {
  /*
          TEST CORRECTNESS
  */
  std::string path;
  unsigned int expectedResult;
  std::string rdbase = getenv("RDBASE");
  path = "/Code/GraphMol/FileParsers/test_data/fewSmi.csv";
  expectedResult = 10;
  testSmiConcurrent(path, ",", 1, 0, false, true, 2, 5, 5, expectedResult);

  path = "/Code/GraphMol/FileParsers/test_data/fewSmi.2.csv";
  expectedResult = 10;
  testSmiConcurrent(path, ",", 1, 0, true, true, 2, 5, 5, expectedResult);

  path = "/Code/GraphMol/FileParsers/test_data/first_200.tpsa.csv";
  expectedResult = 200;
  testSmiConcurrent(path, ",", 0, -1, true, true, 2, 5, 5, expectedResult);

#ifdef RDK_USE_BOOST_IOSTREAMS

  path = rdbase + "/Regress/Data/znp.50k.smi.gz";
  std::istream *strm = new gzstream(path);
  expectedResult = 50000;
  testSmiConcurrent(strm, true, " \t", 0, 1, false, false, 3, 1000, 100,
                    expectedResult);
#endif
  /*

     TEST PROPERTIES

  */
  testSmiProperties();
}

void testSDConcurrent(std::istream *strm, bool takeOwnership, bool sanitize,
                      bool removeHs, bool strictParsing,
                      unsigned int numWriterThreads, size_t sizeInputQueue,
                      size_t sizeOutputQueue, unsigned int expectedResult,
                      bool extras = false) {
  unsigned int nMols = 0;
  boost::dynamic_bitset<> bitVector(expectedResult);
  MultithreadedSDMolSupplier sup(strm, takeOwnership, sanitize, removeHs,
                                 strictParsing, numWriterThreads,
                                 sizeInputQueue, sizeOutputQueue);
  // we have not called the next method yet
  TEST_ASSERT(sup.getLastRecordId() == 0);
  // initially no bit is set in the bitVector, sanity check
  TEST_ASSERT(!bitVector.any());
  while (!sup.atEnd()) {
    ROMol *mol = sup.next();
    if (mol) {
      unsigned int id = sup.getLastRecordId();
      bitVector[id - 1] = 1;
      ++nMols;
      if (extras) {
        std::unique_ptr<ExplicitBitVect> fp(
            MorganFingerprints::getFingerprintAsBitVect(*mol, 2, 2048));
      }
    }
    delete mol;
  }
  // if all bits are set then we have seen possible ids
  TEST_ASSERT(bitVector.all());
  TEST_ASSERT(nMols == expectedResult);
}

void testSDConcurrent(std::string path, bool sanitize, bool removeHs,
                      bool strictParsing, unsigned int numWriterThreads,
                      size_t sizeInputQueue, size_t sizeOutputQueue,
                      unsigned int expectedResult, bool extras = false) {
  std::string rdbase = getenv("RDBASE");
  std::string fname = rdbase + path;
  std::istream *strm = new std::ifstream(fname.c_str());
  testSDConcurrent(strm, true, sanitize, removeHs, strictParsing,
                   numWriterThreads, sizeInputQueue, sizeOutputQueue,
                   expectedResult, extras);
}

void testSDProperties() {
  std::string rdbase = getenv("RDBASE");
  std::string fname =
      rdbase + "/Code/GraphMol/FileParsers/test_data/NCI_aids_few.sdf";
  std::vector<std::string> nameVector;
  std::string tempStr;
  SDMolSupplier sup(fname, false);
  MultithreadedSDMolSupplier multiSup(fname, false);

  while (!multiSup.atEnd()) {
    std::unique_ptr<ROMol> mol{multiSup.next()};
    if (mol != nullptr) {
      TEST_ASSERT(mol->hasProp(common_properties::_Name));
      mol->getProp(common_properties::_Name, tempStr);
      nameVector.push_back(tempStr);
      TEST_ASSERT(mol->hasProp("NCI_AIDS_Antiviral_Screen_Conclusion"));
      TEST_ASSERT(mol->hasProp("CAS_RN"));
      TEST_ASSERT(mol->hasProp("NSC"));
    }
  }

  while (!sup.atEnd()) {
    std::unique_ptr<ROMol> mol{sup.next()};
    if (mol != nullptr) {
      mol->getProp(common_properties::_Name, tempStr);
      TEST_ASSERT(std::find(nameVector.begin(), nameVector.end(), tempStr) !=
                  nameVector.end());
    }
  }
}

void testSDOld(std::string path, bool sanitize, bool removeHs,
               bool strictParsing, unsigned int expectedResult,
               bool extras = false) {
  unsigned int numMols = 0;
  SDMolSupplier sup(path, sanitize, removeHs, strictParsing);
  while (!sup.atEnd()) {
    ROMol *mol = sup.next();
    if (mol) {
      ++numMols;
      if (extras) {
        std::unique_ptr<ExplicitBitVect> fp(
            MorganFingerprints::getFingerprintAsBitVect(*mol, 2, 2048));
      }
    }
    delete mol;
  }
  TEST_ASSERT(numMols == expectedResult);
}

void testSDCorrectness() {
  /*
          TEST CORRECTNESS
  */
  std::string rdbase = getenv("RDBASE");
  std::string path;
  unsigned int expectedResult;
  path = "/Code/GraphMol/FileParsers/test_data/NCI_aids_few.sdf";
  expectedResult = 16;
  testSDConcurrent(path, false, true, true, 2, 5, 5, expectedResult);

  path = "/Code/GraphMol/FileParsers/test_data/esters_end.sdf";
  expectedResult = 6;
  testSDConcurrent(path, true, true, true, 2, 5, 5, expectedResult);

  // strict parsing results in reading 0 out of 2 records
  path = "/Code/GraphMol/FileParsers/test_data/strictLax1.sdf";
  expectedResult = 0;
  testSDConcurrent(path, true, true, true, 2, 5, 5, expectedResult);

  expectedResult = 2;
  testSDConcurrent(path, true, true, false, 2, 5, 5, expectedResult);

  path = "/Code/GraphMol/FileParsers/test_data/O2.sdf";
  expectedResult = 1;
  testSDConcurrent(path, true, true, false, 2, 5, 5, expectedResult);

#ifdef RDK_USE_BOOST_IOSTREAMS

  path = rdbase + "/Regress/Data/mols.1000.sdf.gz";
  std::istream *strm = new gzstream(path);
  expectedResult = 1000;
  testSDConcurrent(strm, true, false, true, true, 2, 5, 5, expectedResult);

#endif
  /*
     TEST PROPERTIES
  */
  testSDProperties();
}

void testPerformance() {
  /*
     TEST PERFORMANCE

     NOTE: Only use this method when you have
     extracted the files znp.50k.smi.gz and chembl26_very_active.sdf.gz in the
     $RDBASE/Regress/Data directory.
  */
  std::string rdbase = getenv("RDBASE");
  unsigned int maxThreadCount =
      std::min(std::thread::hardware_concurrency() + 4, 4u);
#if 1

  {
    std::string path = "/Regress/Data/znp.50k.smi";
    std::string gzpath = "/Regress/Data/znp.50k.smi.gz";
    unsigned int expectedResult = 50000;
    auto start = high_resolution_clock::now();
    // NOTE: have to use path instead of stream, since the tellg()
    //       method, which is used in implementation of the supplier
    // 			 fails for this file.

    std::string delim = " \t";
    int smilesColumn = 0;
    int nameColumn = 1;
    bool titleLine = false;
    bool sanitize = true;
    bool extras = true;
    testSmiOld(rdbase + path, delim, smilesColumn, nameColumn, titleLine,
               sanitize, expectedResult, extras);
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    std::cout << "Duration for SmilesMolSupplier: " << duration.count()
              << " (milliseconds) \n";

#if RDK_USE_BOOST_IOSTREAMS
    for (unsigned int i = maxThreadCount; i >= 1; --i) {
      std::istream *strm = new gzstream(rdbase + gzpath);
      start = high_resolution_clock::now();
      bool takeOwnership = true;
      testSmiConcurrent(strm, takeOwnership, delim, smilesColumn, nameColumn,
                        titleLine, sanitize, i, 1000, 100, expectedResult,
                        extras);
      stop = high_resolution_clock::now();
      duration = duration_cast<milliseconds>(stop - start);
      std::cout << "Duration for testSmiConcurent with " << i
                << "  writer threads: " << duration.count()
                << " (milliseconds) \n";
    }
#endif
  }
#endif

  {
    std::string delim = " \t";
    bool sanitize = true;
    bool removeHs = true;
    bool strictParsing = false;
    bool extras = true;

    std::string path = "/Regress/Data/chembl26_very_active.sdf";
    std::string gzpath = "/Regress/Data/chembl26_very_active.sdf.gz";
    unsigned int expectedResult = 35767;
    auto start = high_resolution_clock::now();
    // NOTE: have to use path instead of stream, since the tellg()
    //       method, which is used in implementation of the supplier
    // 			 fails for this file.
    testSDOld(rdbase + path, sanitize, removeHs, strictParsing, expectedResult,
              extras);
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    std::cout << "Duration for SDMolSupplier: " << duration.count()
              << " (milliseconds) \n";

#if RDK_USE_BOOST_IOSTREAMS
    for (unsigned int i = maxThreadCount; i >= 1; --i) {
      std::istream *strm = new gzstream(rdbase + gzpath);
      bool takeOwnership = true;
      start = high_resolution_clock::now();
      testSDConcurrent(strm, takeOwnership, sanitize, removeHs, strictParsing,
                       i, 1000, 4000, expectedResult, extras);
      stop = high_resolution_clock::now();
      duration = duration_cast<milliseconds>(stop - start);
      std::cout << "Duration for testSDConcurent with " << i
                << "  writer threads: " << duration.count()
                << " (milliseconds) \n";
    }
#endif
  }
}

int main() {
  RDLog::InitLogs();

#ifdef RDK_TEST_MULTITHREADED

  BOOST_LOG(rdErrorLog) << "\n-----------------------------------------\n";
  testSmiCorrectness();
  BOOST_LOG(rdErrorLog) << "Finished: testSmiCorrectness()\n";
  BOOST_LOG(rdErrorLog) << "-----------------------------------------\n\n";

  BOOST_LOG(rdErrorLog) << "\n-----------------------------------------\n";
  testSDCorrectness();
  BOOST_LOG(rdErrorLog) << "Finished: testSDCorrectness()\n";
  BOOST_LOG(rdErrorLog) << "-----------------------------------------\n\n";

  /*
    BOOST_LOG(rdErrorLog) << "\n-----------------------------------------\n";
    testPerformance();
    BOOST_LOG(rdErrorLog) << "Finished: testPerformance()\n";
    BOOST_LOG(rdErrorLog) << "-----------------------------------------\n\n";
  */

#endif

  return 0;
}