File: testRGroupInternals.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 (209 lines) | stat: -rw-r--r-- 8,399 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
//  Copyright (C) 2020 Gareth Jones, Glysade LLC
//  All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Novartis Institutes for BioMedical Research Inc.
//       nor the names of its contributors may be used to endorse or promote
//       products derived from this software without specific prior written
//       permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#include <string>
#include <vector>
#include <RDGeneral/test.h>
#include <RDGeneral/RDLog.h>
#include <RDGeneral/utils.h>
#include <GraphMol/RDKitBase.h>
#include <RDGeneral/Exceptions.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#define private public
#include <GraphMol/RGroupDecomposition/RGroupDecomp.h>
#include <GraphMol/RGroupDecomposition/RGroupDecompData.h>
#include <GraphMol/RGroupDecomposition/RGroupGa.h>

using namespace RDKit;

void testCoresLabelledProperly() {
  // Tests for an error in RGroupDecompositionParameters::prepareCore where
  // the same unindexed label could be mistakenly given to multiple rgroups
  // in different cores

  // See https://github.com/rdkit/rdkit/pull/3565

  BOOST_LOG(rdInfoLog)
      << "********************************************************\n";
  BOOST_LOG(rdInfoLog) << "Test cores labelled properly" << std::endl;

  std::vector<std::string> coreSmi = {"N1([*:1])CCN([*:2])CC1",
                                      "C1(O[*:1])CCC(O[*:2])CC1",
                                      "C1([*:1])CCC([*:2])CC1"};

  std::vector<ROMOL_SPTR> cores;
  for (const auto &s : coreSmi) {
    cores.emplace_back(SmartsToMol(s));
  }

  RGroupDecompositionParameters params;
  params.alignment = RGroupCoreAlignment::NoAlignment;
  params.scoreMethod = FingerprintVariance;
  RGroupDecomposition decomposition(cores, params);

  auto data = decomposition.data;
  std::set<int> rlabels;

  for (const auto &core : data->cores) {
    auto mol = core.second.core;
    for (const auto atom : mol->atoms()) {
      if (atom->hasProp(RLABEL)) {
        int rlabel = atom->getProp<int>(RLABEL);
        if (rlabel < 0) {
          TEST_ASSERT(rlabels.find(rlabel) == rlabels.end());
          rlabels.insert(rlabel);
        }
      }
    }
  }
}

std::pair<int, RData> makeRData(int attachment, std::vector<int> attachments,
                                const std::string &smiles) {
  auto rData = boost::make_shared<RGroupData>();
  auto mol = SmilesToMol(smiles);
  auto frags = MolOps::getMolFrags(*mol);
  for (auto &frag : frags) {
    rData->add(frag, attachments);
  }
  delete mol;
  std::pair<int, RData> pair(attachment, rData);
  return pair;
}

std::pair<int, RData> makeRData(int attachment, const std::string &smiles) {
  std::vector<int> attachments{attachment};
  return makeRData(attachment, attachments, smiles);
}

void testRingMatching3Score() {
  BOOST_LOG(rdInfoLog)
      << "********************************************************\n";
  BOOST_LOG(rdInfoLog)
      << "Test scoring function for RingMatching3- see GitHub ##3924"
      << std::endl;

  R_DECOMP decomp1Mol1 = {makeRData(-4, "*[H]"), makeRData(-2, "*[H]"),
                          makeRData(-1, "*[H]"), makeRData(1, "*C([H])([H])C")};
  R_DECOMP decomp1Mol2 = {makeRData(-4, "*[H]"), makeRData(-3, "*[H]"),
                          makeRData(-2, "*[H]"), makeRData(-1, "*[H]"),
                          makeRData(1, "*C([H])([H])I")};
  R_DECOMP decomp1Mol3 = {makeRData(-4, "*[H]"), makeRData(-2, "*[H]"),
                          makeRData(-1, "*[H]"), makeRData(1, "*C([H])([H])F")};
  RGroupMatch match1Mol1(0, 0, decomp1Mol1, nullptr);
  RGroupMatch match1Mol2(0, 0, decomp1Mol2, nullptr);
  RGroupMatch match1Mol3(0, 0, decomp1Mol3, nullptr);
  std::set<int> labels{-4, -3, -2, -1, 1};
  std::vector<RGroupMatch> matches1Mol1{match1Mol1};
  std::vector<RGroupMatch> matches1Mol2{match1Mol2};
  std::vector<RGroupMatch> matches1Mol3{match1Mol3};
  std::vector<std::vector<RGroupMatch>> allMatches1 = {
      matches1Mol1, matches1Mol2, matches1Mol3};
  std::vector<size_t> permutation{0, 0, 0};

  R_DECOMP decomp2Mol1 = {makeRData(-4, "*[H]"), makeRData(-3, "*C([H])([H])C"),
                          makeRData(-2, "*[H]"), makeRData(-1, "*[H]")};
  R_DECOMP decomp2Mol2 = {makeRData(-4, "*[H]"), makeRData(-3, "*[H]"),
                          makeRData(-2, "*[H]"), makeRData(-1, "*[H]"),
                          makeRData(1, "*C([H])([H])I")};
  R_DECOMP decomp2Mol3 = {makeRData(-4, "*[H]"), makeRData(-2, "*[H]"),
                          makeRData(-1, "*[H]"), makeRData(1, "*C([H])([H])F")};
  RGroupMatch match2Mol1(0, 1, decomp2Mol1, nullptr);
  RGroupMatch match2Mol2(0, 0, decomp2Mol2, nullptr);
  RGroupMatch match2Mol3(0, 0, decomp2Mol3, nullptr);
  std::vector<RGroupMatch> matches2Mol1{match2Mol1};
  std::vector<RGroupMatch> matches2Mol2{match2Mol2};
  std::vector<RGroupMatch> matches2Mol3{match2Mol3};
  std::vector<std::vector<RGroupMatch>> allMatches2 = {
      matches2Mol1, matches2Mol2, matches2Mol3};

  RGroupScorer scorer;
  auto test1 = scorer.matchScore(permutation, allMatches1, labels);
  auto test2 = scorer.matchScore(permutation, allMatches2, labels);

  // expect test1 to have better score than test2 since all halogens are on R1

  TEST_ASSERT(test1 > test2);

  auto testFp1 = fingerprintVarianceScore(permutation, allMatches1, labels);
  auto testFp2 = fingerprintVarianceScore(permutation, allMatches2, labels);

  TEST_ASSERT(testFp1 > testFp2);
}

void testGithub3746() {
  BOOST_LOG(rdInfoLog)
      << "********************************************************\n";
  BOOST_LOG(rdInfoLog) << "Test GA falls over to exhaustive on simple system"
                       << std::endl;
  const std::vector<ROMOL_SPTR> cores{"c1([*:1])c([*:2])c([*:3])ccc1"_smiles,
                                      "c1([*:1])c([*:2])c([*:3])cnc1"_smiles};
  const std::vector<const char *> smilesData{"c1(CO)cc(CN)ccc1",
                                             "c1(CO)cc(CN)cnc1"};

  RGroupDecompositionParameters params;
  params.onlyMatchAtRGroups = true;
  params.removeHydrogensPostMatch = true;
  params.matchingStrategy = GA;
  params.removeHydrogensPostMatch = true;

  RGroupDecomposition decomposition(cores, params);
  size_t i = 0;
  for (const auto &smi : smilesData) {
    ROMOL_SPTR mol(static_cast<ROMol *>(SmilesToMol(smi)));
    TEST_ASSERT(decomposition.add(*mol) == static_cast<int>(i++));
  }

  auto data = decomposition.data;
  RGroupGa ga(*data);
  auto numberPermutations = ga.numberPermutations();

  TEST_ASSERT(numberPermutations == 4);
  // criteria for exhaustive search instead of GA
  TEST_ASSERT(numberPermutations < ga.getPopsize() * 100);
}

int main() {
  RDLog::InitLogs();
  boost::logging::disable_logs("rdApp.debug");

  BOOST_LOG(rdInfoLog)
      << "********************************************************\n";
  BOOST_LOG(rdInfoLog) << "Testing R-Group Decomposition Internals\n";

  testRingMatching3Score();
  testGithub3746();
  testCoresLabelledProperly();

  BOOST_LOG(rdInfoLog)
      << "********************************************************\n";
  return 0;
}