File: mces_cluster_catch.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 (152 lines) | stat: -rw-r--r-- 5,014 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
//
//  Copyright (C) 2023 David Cosgrove
//
//   @@ 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 <random>
#include <vector>

#include <GraphMol/FileParsers/MolSupplier.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <GraphMol/Substruct/SubstructMatch.h>

#include <catch2/catch_all.hpp>

#include <GraphMol/RascalMCES/RascalMCES.h>
#include <GraphMol/RascalMCES/RascalClusterOptions.h>
#include <GraphMol/RascalMCES/RascalResult.h>

TEST_CASE("Small test", "[basics]") {
  std::string fName = getenv("RDBASE");
  fName += "/Contrib/Fastcluster/cdk2.smi";
  RDKit::SmilesMolSupplier suppl(fName, "\t", 1, 0, false);
  std::vector<std::shared_ptr<RDKit::ROMol>> mols;
  while (!suppl.atEnd()) {
    std::shared_ptr<RDKit::ROMol> mol(suppl.next());
    if (!mol) {
      continue;
    }
    mols.push_back(mol);
  }
  RDKit::RascalMCES::RascalClusterOptions clusOpts;
  auto clusters = RDKit::RascalMCES::rascalCluster(mols, clusOpts);
  REQUIRE(clusters.size() == 8);
  std::vector<size_t> expSizes{7, 7, 6, 2, 2, 2, 2, 20};
  for (size_t i = 0; i < 8; ++i) {
    REQUIRE(clusters[i].size() == expSizes[i]);
  }
}

TEST_CASE("BLSets subset", "[basics]") {
  std::string fName = getenv("RDBASE");
  fName += "/Code/GraphMol/RascalMCES/data/test_cluster1.smi";
  RDKit::SmilesMolSupplier suppl(fName, "\t", 1, 0, false);
  std::vector<std::shared_ptr<RDKit::ROMol>> mols;
  while (!suppl.atEnd()) {
    std::shared_ptr<RDKit::ROMol> mol(suppl.next());
    if (!mol) {
      continue;
    }
    mols.push_back(mol);
  }
  auto clusters = RDKit::RascalMCES::rascalCluster(mols);
  REQUIRE(clusters.size() == 12);
  std::vector<size_t> expSizes{8, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 21};
  for (size_t i = 0; i < 12; ++i) {
    REQUIRE(clusters[i].size() == expSizes[i]);
  }
}

TEST_CASE("ChEMBL 1907596") {
  std::string fName = getenv("RDBASE");
  fName += "/Code/GraphMol/RascalMCES/data/chembl_1907596.smi";
  std::cout << fName << std::endl;
  RDKit::SmilesMolSupplier suppl(fName, "\t", 1, 0, false);
  std::vector<std::shared_ptr<RDKit::ROMol>> mols;
  while (!suppl.atEnd()) {
    std::shared_ptr<RDKit::ROMol> mol(suppl.next());
    if (!mol) {
      continue;
    }
    mols.push_back(mol);
  }
  RDKit::RascalMCES::RascalClusterOptions clusOpts;
  clusOpts.similarityCutoff = 0.7;
  auto clusters = RDKit::RascalMCES::rascalCluster(mols, clusOpts);
  REQUIRE(clusters.size() == 21);
  std::vector<size_t> expSizes{342, 71, 64, 33, 23, 11, 10, 6, 6, 5, 5,
                               4,   3,  3,  3,  3,  3,  2,  2, 2, 14};
  for (size_t i = 0; i < 21; ++i) {
    REQUIRE(clusters[i].size() == expSizes[i]);
  }
}

TEST_CASE("Small Butina test", "[basics]") {
  std::string fName = getenv("RDBASE");
  fName += "/Contrib/Fastcluster/cdk2.smi";
  RDKit::SmilesMolSupplier suppl(fName, "\t", 1, 0, false);
  std::vector<std::shared_ptr<RDKit::ROMol>> mols;
  while (!suppl.atEnd()) {
    std::shared_ptr<RDKit::ROMol> mol(suppl.next());
    if (!mol) {
      continue;
    }
    mols.push_back(mol);
  }
  RDKit::RascalMCES::RascalClusterOptions clusOpts;
  auto clusters = RDKit::RascalMCES::rascalButinaCluster(mols, clusOpts);
  unsigned int numMols = 0;
  for (const auto &cl : clusters) {
    numMols += cl.size();
  }
  REQUIRE(numMols == mols.size());
  REQUIRE(clusters.size() == 29);
  std::vector<size_t> expSizes{6, 6, 6, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                               1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  for (size_t i = 0; i < 29; ++i) {
    REQUIRE(clusters[i].size() == expSizes[i]);
  }
}

TEST_CASE("Small test, smaller number of threads", "[basics]") {
  // I'm not sure how to test whether this has had the desired effect,
  // but at least we'll know that it runs ok.
  std::string fName = getenv("RDBASE");
  fName += "/Contrib/Fastcluster/cdk2.smi";
  RDKit::SmilesMolSupplier suppl(fName, "\t", 1, 0, false);
  std::vector<std::shared_ptr<RDKit::ROMol>> mols;
  while (!suppl.atEnd()) {
    std::shared_ptr<RDKit::ROMol> mol(suppl.next());
    if (!mol) {
      continue;
    }
    mols.push_back(mol);
  }
  {
    RDKit::RascalMCES::RascalClusterOptions clusOpts;
    clusOpts.numThreads = 2;
    auto clusters = RDKit::RascalMCES::rascalCluster(mols, clusOpts);
    REQUIRE(clusters.size() == 8);
    std::vector<size_t> expSizes{7, 7, 6, 2, 2, 2, 2, 20};
    for (size_t i = 0; i < 8; ++i) {
      REQUIRE(clusters[i].size() == expSizes[i]);
    }
  }
  {
    RDKit::RascalMCES::RascalClusterOptions clusOpts;
    clusOpts.numThreads = -2;
    auto clusters = RDKit::RascalMCES::rascalCluster(mols, clusOpts);
    REQUIRE(clusters.size() == 8);
    std::vector<size_t> expSizes{7, 7, 6, 2, 2, 2, 2, 20};
    for (size_t i = 0; i < 8; ++i) {
      REQUIRE(clusters[i].size() == expSizes[i]);
    }
  }
}