File: RascalCluster.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 (382 lines) | stat: -rw-r--r-- 13,500 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
//
// Copyright (C) David Cosgrove 2023
//
//   @@ 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.
//
// This file contains an implementation of the clustering algorithm
// described in
// 'A Line Graph Algorithm for Clustering Chemical Structures Based
// on Common Substructural Cores', JW Raymond, PW Willett.
// https://match.pmf.kg.ac.rs/electronic_versions/Match48/match48_197-207.pdf
// https://eprints.whiterose.ac.uk/77598/
// It uses the RASCAL MCES algorithm to perform a fuzzy clustering
// of a set of molecules.

#include <algorithm>
#include <iterator>
#include <list>
#include <thread>
#include <vector>

#include <RDGeneral/RDThreads.h>
#include <GraphMol/ROMol.h>
#include <GraphMol/MolOps.h>
#include <GraphMol/RascalMCES/RascalClusterOptions.h>
#include <GraphMol/RascalMCES/RascalDetails.h>
#include <GraphMol/RascalMCES/RascalMCES.h>
#include <GraphMol/RascalMCES/RascalResult.h>

namespace RDKit {
namespace RascalMCES {
namespace details {
ClusNode calcMolMolSimilarity(
    const std::tuple<
        size_t, size_t, const std::vector<std::shared_ptr<ROMol>> *,
        const RascalOptions *, const RascalClusterOptions *> &toDo) {
  auto i = std::get<0>(toDo);
  auto j = std::get<1>(toDo);
  auto mols = std::get<2>(toDo);
  auto opts = std::get<3>(toDo);
  auto clusOpts = std::get<4>(toDo);
  auto res = rascalMCES(*(*mols)[i], *(*mols)[j], *opts);
  ClusNode cn;
  cn.d_mol1Num = i;
  cn.d_mol2Num = j;
  if (res.empty()) {
    // tier1Sim and tier2Sim were above the threshold, but no MCES
    // was found.
    cn.d_sim = 0.0;
  } else {
    if (res.front().getBondMatches().empty()) {
      cn.d_sim = 0.0;
    } else {
      res.front().trimSmallFrags();
      res.front().largestFragsOnly(clusOpts->maxNumFrags);
      cn.d_sim = res.front().getSimilarity();
      if (cn.d_sim >= opts->similarityThreshold) {
        cn.d_res = std::shared_ptr<RascalResult>(new RascalResult(res.front()));
      }
    }
  }
  return cn;
}

std::vector<std::vector<ClusNode>> buildProximityGraph(
    const std::vector<std::shared_ptr<ROMol>> &mols,
    const RascalClusterOptions &clusOpts) {
  if (mols.size() < 2) {
    return std::vector<std::vector<ClusNode>>();
  }
  std::vector<std::vector<ClusNode>> proxGraph =
      std::vector<std::vector<ClusNode>>(
          mols.size(), std::vector<ClusNode>(mols.size(), ClusNode()));
  std::vector<
      std::tuple<size_t, size_t, const std::vector<std::shared_ptr<ROMol>> *,
                 const RascalOptions *, const RascalClusterOptions *>>
      toDo;

  RascalOptions opts;
  opts.similarityThreshold = clusOpts.similarityCutoff;
  for (size_t i = 0; i < mols.size() - 1; ++i) {
    for (size_t j = i + 1; j < mols.size(); ++j) {
      toDo.push_back({i, j, &mols, &opts, &clusOpts});
    }
  }

  auto buildProxGraphPart =
      [](const std::vector<std::tuple<
             size_t, size_t, const std::vector<std::shared_ptr<ROMol>> *,
             const RascalOptions *, const RascalClusterOptions *>> &toDo,
         std::vector<ClusNode> &molSims, size_t start, size_t finish) -> void {
    if (start > toDo.size()) {
      return;
    }
    if (finish > toDo.size()) {
      finish = toDo.size();
    }
    std::transform(toDo.begin() + start, toDo.begin() + finish,
                   molSims.begin() + start, calcMolMolSimilarity);
  };

  std::vector<ClusNode> molSims(toDo.size());
#if RDK_BUILD_THREADSAFE_SSS
  auto numThreads = getNumThreadsToUse(clusOpts.numThreads);
  if (numThreads > 1) {
    size_t eachThread = 1 + (toDo.size() / numThreads);
    size_t start = 0;
    std::vector<std::thread> threads;
    for (unsigned int i = 0U; i < numThreads; ++i, start += eachThread) {
      threads.push_back(std::thread(buildProxGraphPart, std::ref(toDo),
                                    std::ref(molSims), start,
                                    start + eachThread));
    }
    for (auto &t : threads) {
      t.join();
    }
  } else {
    std::transform(toDo.begin(), toDo.end(), molSims.begin(),
                   calcMolMolSimilarity);
  }
#else
  std::transform(toDo.begin(), toDo.end(), molSims.begin(),
                 calcMolMolSimilarity);
#endif
  for (const auto &cn : molSims) {
    proxGraph[cn.d_mol1Num][cn.d_mol2Num] =
        proxGraph[cn.d_mol2Num][cn.d_mol1Num] = cn;
  }
  return proxGraph;
}

// Split the proximity graph into its disconnected components,
// returning vectors of the molecule numbers of the disconnected
// graphs.
std::vector<std::vector<unsigned int>> disconnectProximityGraphs(
    std::vector<std::vector<ClusNode>> &proxGraph) {
  std::vector<std::vector<unsigned int>> subGraphs;
  std::vector<bool> done(proxGraph.size(), false);
  auto nextStart = std::find(done.begin(), done.end(), false);
  while (nextStart != done.end()) {
    std::list<unsigned int> nodes;
    std::list<unsigned int> toDo(1, std::distance(done.begin(), nextStart));
    while (!toDo.empty()) {
      auto nextNode = toDo.front();
      toDo.pop_front();
      if (!done[nextNode]) {
        nodes.push_back(nextNode);
      }
      done[nextNode] = true;
      for (size_t i = 0; i < proxGraph.size(); ++i) {
        if (!done[i] && proxGraph[nextNode][i].d_res) {
          toDo.push_back(i);
          nodes.push_back(i);
          done[i] = true;
        }
      }
    }
    nodes.sort();
    subGraphs.push_back(std::vector(nodes.begin(), nodes.end()));
    nextStart = std::find(done.begin(), done.end(), false);
  }
  return subGraphs;
}

// Calculate G_{ij} for the molecule.  p is the number of bonds that
// a fragment must exceed for it to be counted in the formula.
double g_ij(const std::shared_ptr<ROMol> &mol, double a, double b,
            unsigned int p) {
  auto molFrags = MolOps::getMolFrags(*mol, false);
  int numBigFrags = 0;
  for (const auto &mf : molFrags) {
    if (mf->getNumBonds() > p) {
      ++numBigFrags;
    }
  }
  numBigFrags = numBigFrags == 0 ? molFrags.size() : numBigFrags;
  double g = mol->getNumAtoms();
  g += b * (1.0 - a * (numBigFrags - 1)) * mol->getNumBonds();
  return g;
}

std::vector<std::vector<unsigned int>> makeSubClusters(
    const std::vector<ClusNode> &nbors, const RascalClusterOptions &clusOpts) {
  std::vector<std::vector<unsigned int>> subClusters;

  std::vector<const ClusNode *> tmpNbors;
  for (const auto &n : nbors) {
    tmpNbors.push_back(&n);
  }

  while (!tmpNbors.empty()) {
    subClusters.push_back(std::vector<unsigned int>{
        tmpNbors.front()->d_mol1Num, tmpNbors.front()->d_mol2Num});
    auto m1 = tmpNbors.front()->d_res->getMcesMol();
    auto g_12 = g_ij(m1, clusOpts.a, clusOpts.b, clusOpts.minFragSize);
    for (size_t i = 1; i < tmpNbors.size(); ++i) {
      auto m2 = tmpNbors[i]->d_res->getMcesMol();
      auto g_13 = g_ij(m2, clusOpts.a, clusOpts.b, clusOpts.minFragSize);

      auto results = RDKit::RascalMCES::rascalMCES(*m1, *m2);
      if (results.empty() || results.front().getBondMatches().empty()) {
        continue;
      }
      auto res = results.front();
      auto g_12_13 =
          g_ij(res.getMcesMol(), clusOpts.a, clusOpts.b, clusOpts.minFragSize);
      double sim = g_12_13 / std::min(g_12, g_13);
      if (sim > clusOpts.minIntraClusterSim) {
        subClusters.back().push_back(tmpNbors[i]->d_mol2Num);
        subClusters.back().push_back(tmpNbors[i]->d_mol1Num);
        tmpNbors[i] = nullptr;
      }
    }
    tmpNbors.front() = nullptr;
    tmpNbors.erase(std::remove(tmpNbors.begin(), tmpNbors.end(), nullptr),
                   tmpNbors.end());
    std::sort(subClusters.back().begin(), subClusters.back().end());
    subClusters.back().erase(
        std::unique(subClusters.back().begin(), subClusters.back().end()),
        subClusters.back().end());
  }
  return subClusters;
}

std::vector<std::vector<unsigned int>> formInitialClusters(
    const std::vector<unsigned int> &subGraph,
    const std::vector<std::vector<ClusNode>> &proxGraph,
    const RascalClusterOptions &clusOpts) {
  std::vector<std::vector<unsigned int>> clusters;
  if (subGraph.size() < 2) {
    return clusters;
  }
  for (auto i : subGraph) {
    std::vector<ClusNode> nbors;
    for (auto j : subGraph) {
      if (proxGraph[i][j].d_res) {
        nbors.push_back(proxGraph[i][j]);
      }
    }
    std::sort(nbors.begin(), nbors.end(),
              [](const ClusNode &c1, const ClusNode &c2) -> bool {
                return c1.d_sim > c2.d_sim;
              });
    if (!nbors.empty()) {
      auto subClusters = makeSubClusters(nbors, clusOpts);
      clusters.insert(clusters.end(), subClusters.begin(), subClusters.end());
    }
  }
  std::sort(clusters.begin(), clusters.end(),
            [](const std::vector<unsigned int> &c1,
               const std::vector<unsigned int> &c2) -> bool {
              if (c1.size() == c2.size()) {
                return c1.front() < c2.front();
              } else {
                return c1.size() > c2.size();
              }
            });
  clusters.erase(std::unique(clusters.begin(), clusters.end()), clusters.end());
  return clusters;
}

std::vector<std::vector<unsigned int>> mergeClusters(
    const std::vector<std::vector<unsigned int>> &clusters,
    const RascalClusterOptions &clusOpts) {
  std::vector<std::vector<unsigned int>> outClusters(clusters);

  if (outClusters.size() < 2) {
    return outClusters;
  }

  for (size_t i = 0; i < outClusters.size() - 1; ++i) {
    for (size_t j = i + 1; j < outClusters.size(); ++j) {
      std::vector<int> inCommon;
      std::set_intersection(outClusters[i].begin(), outClusters[i].end(),
                            outClusters[j].begin(), outClusters[j].end(),
                            std::back_inserter(inCommon));
      double s =
          double(inCommon.size()) / std::min(double(outClusters[i].size()),
                                             double(outClusters[j].size()));
      if (s > clusOpts.clusterMergeSim) {
        outClusters[i].insert(outClusters[i].end(), outClusters[j].begin(),
                              outClusters[j].end());
        outClusters[j].clear();
        std::sort(outClusters[i].begin(), outClusters[i].end());
        outClusters[i].erase(
            std::unique(outClusters[i].begin(), outClusters[i].end()),
            outClusters[i].end());
      }
    }
    outClusters.erase(
        std::remove_if(outClusters.begin(), outClusters.end(),
                       [](const std::vector<unsigned int> &c) -> bool {
                         return c.empty();
                       }),
        outClusters.end());
  }

  return outClusters;
}

void sortClusterMembersByMeanSim(
    const std::vector<std::vector<ClusNode>> &proxGraph,
    std::vector<std::vector<unsigned int>> &clusters) {
  for (auto &clus : clusters) {
    std::vector<std::pair<unsigned int, double>> clusSims;
    for (unsigned int i = 0U; i < clus.size(); ++i) {
      double totSim = 0.0;
      for (unsigned int j = 0U; j < clus.size(); ++j) {
        if (i != j) {
          totSim += proxGraph[clus[i]][clus[j]].d_sim;
        }
      }
      clusSims.push_back({clus[i], totSim / (clus.size() - 1)});
    }
    std::sort(clusSims.begin(), clusSims.end(),
              [](const std::pair<unsigned int, double> &p1,
                 const std::pair<unsigned int, double> &p2) -> bool {
                return p1.second > p2.second;
              });
    std::transform(
        clusSims.begin(), clusSims.end(), clus.begin(),
        [](const std::pair<unsigned int, double> &p) -> unsigned int {
          return p.first;
        });
  }
}

std::vector<std::vector<unsigned int>> makeClusters(
    const std::vector<std::vector<unsigned int>> &subGraphs,
    const std::vector<std::vector<ClusNode>> &proxGraph,
    const RascalClusterOptions &clusOpts) {
  std::vector<std::vector<unsigned int>> clusters;
  for (const auto &sg : subGraphs) {
    auto theseClusters = formInitialClusters(sg, proxGraph, clusOpts);
    auto mergedClusters = mergeClusters(theseClusters, clusOpts);
    clusters.insert(clusters.end(), mergedClusters.begin(),
                    mergedClusters.end());
  }
  std::sort(clusters.begin(), clusters.end(),
            [](const std::vector<unsigned int> &c1,
               const std::vector<unsigned int> &c2) -> bool {
              return c1.size() > c2.size();
            });
  return clusters;
}

std::vector<unsigned int> collectSingletons(
    const std::vector<std::vector<ClusNode>> &proxGraph) {
  std::vector<unsigned int> singletons;
  for (size_t i = 0; i < proxGraph.size(); ++i) {
    bool single = true;
    for (const auto &cn : proxGraph[i]) {
      if (cn.d_res) {
        single = false;
        break;
      }
    }
    if (single) {
      singletons.push_back(i);
    }
  }
  return singletons;
}
}  // namespace details

std::vector<std::vector<unsigned int>> rascalCluster(
    const std::vector<std::shared_ptr<ROMol>> &mols,
    const RascalClusterOptions &clusOpts) {
  auto proxGraph = details::buildProximityGraph(mols, clusOpts);
  auto subGraphs = details::disconnectProximityGraphs(proxGraph);
  auto clusters = details::makeClusters(subGraphs, proxGraph, clusOpts);
  auto singletons = details::collectSingletons(proxGraph);
  clusters.push_back(singletons);
  details::sortClusterMembersByMeanSim(proxGraph, clusters);
  return clusters;
}

}  // namespace RascalMCES
}  // namespace RDKit