File: nontetrahedral_tests.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 (298 lines) | stat: -rw-r--r-- 11,119 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
//
//  Copyright (C) 2021 Greg Landrum and other RDKit contributors
//
//   @@ 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 <catch2/catch_all.hpp>

#include <iostream>
#include <fstream>

#include <GraphMol/RDKitBase.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/Chirality.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <GraphMol/SmilesParse/SmartsWrite.h>
#include <GraphMol/FileParsers/FileParsers.h>
#include <boost/algorithm/string.hpp>

using namespace RDKit;

void inorganicSanitize(RDKit::RWMol &mol) {
  unsigned int failed = 0;
  unsigned int flags = RDKit::MolOps::SANITIZE_ALL;
  flags &= ~RDKit::MolOps::SANITIZE_CLEANUP;
  flags &= ~RDKit::MolOps::SANITIZE_PROPERTIES;

  mol.updatePropertyCache(false);
  RDKit::MolOps::sanitizeMol(mol, failed, flags);
}

TEST_CASE("bulk parse test") {
  Chirality::setAllowNontetrahedralChirality(true);
  std::string pathName = getenv("RDBASE");
  pathName += "/Code/GraphMol/SmilesParse/test_data/inorganic_stereo.smi";
  std::ifstream ins(pathName);
  REQUIRE(ins.good());
  while (!ins.eof()) {
    auto inl = getLine(ins);
    if (inl.empty()) {
      continue;
    }
    std::vector<std::string> tokens;
    boost::split(tokens, inl, boost::is_any_of(" \t"));
    if (tokens.size() < 2) {
      continue;
    }
    SmilesParserParams ps;
    ps.sanitize = false;
    std::unique_ptr<RWMol> mol{SmilesToMol(tokens[0], ps)};
    if (!mol) {
      std::cerr << tokens[1] << std::endl;
    }
    REQUIRE(mol);
    inorganicSanitize(*mol);
    bool hasNontet = false;
    for (const auto atom : mol->atoms()) {
      if (Chirality::hasNonTetrahedralStereo(atom)) {
        hasNontet = true;
        break;
      }
    }
    if (!hasNontet) {
      std::cerr << tokens[0] << std::endl;
    }
    CHECK(hasNontet);
  }
}

TEST_CASE("TH and @ are equivalent") {
  Chirality::setAllowNontetrahedralChirality(true);
  SECTION("@TH") {
    auto m = "F[C@THH](C)O"_smiles;
    REQUIRE(m);
    CHECK(MolToSmiles(*m) == "C[C@H](O)F");
  }
  SECTION("@TH1") {
    auto m = "F[C@TH1H](C)O"_smiles;
    REQUIRE(m);
    CHECK(MolToSmiles(*m) == "C[C@H](O)F");
  }
  SECTION("@TH2") {
    auto m = "F[C@TH2H](C)O"_smiles;
    REQUIRE(m);
    CHECK(MolToSmiles(*m) == "C[C@@H](O)F");
  }
}

TEST_CASE("non-canonical non-tetrahedral output") {
  Chirality::setAllowNontetrahedralChirality(true);
  SECTION("no reordering") {
    // clang-format off
    std::vector<std::string> data = {
        "[CH3][Pt@SP1]([F])([OH])[Cl]",        "[CH3][Pt@SP2]([F])([OH])[Cl]",
        "[CH3][Pt@TB1]([F])([OH])([NH2])[Cl]",     "[CH3][Pt@TB2]([F])([OH])([NH2])[Cl]",
        "[CH3][Pt@OH1]([F])([OH])([NH2])([Br])[Cl]", "[CH3][Pt@OH2]([F])([OH])([NH2])([Br])[Cl]",
    };
    // clang-format on
    for (const auto &smi : data) {
      std::unique_ptr<RWMol> m{SmilesToMol(smi)};
      REQUIRE(m);
      SmilesWriteParams writeps;
      writeps.canonical = false;
      // be sure to skip stereo assignment
      m->setProp(common_properties::_StereochemDone, true);
      CHECK(MolToSmiles(*m, writeps) == smi);
    }
  }
  SECTION("reordering") {
    // clang-format off
    std::vector<std::pair<std::string,std::string>> data = {
        {"F[Pt@SP1](C)(O)Cl","[CH3][Pt@SP3]([OH])([F])[Cl]",},
        {"F[Pt@SP2](C)(O)Cl","[CH3][Pt@SP1]([OH])([F])[Cl]"},
        {"S[As@TB1](F)(Cl)(Br)N","N[As@TB6](F)(S)(Cl)Br"},
        {"C[Pt@OH1](F)(O)(N)(Br)Cl","[CH3][Pt@OH16]([NH2])([OH])([F])([Cl])[Br]"},
    };
    // clang-format on
    for (const auto &pr : data) {
      std::unique_ptr<RWMol> m{SmilesToMol(pr.first)};
      REQUIRE(m);
      CHECK(MolToSmiles(*m) == pr.second);
    }
  }
}

TEST_CASE("SP getChiralAcrossBond et al.") {
  Chirality::setAllowNontetrahedralChirality(true);
  SECTION("basics") {
    {
      auto m = "C[Pt@SP1](F)(O)Cl"_smiles;
      REQUIRE(m);
      std::vector<std::pair<unsigned int, unsigned int>> bpairs = {{0, 2},
                                                                   {1, 3}};
      for (auto pr : bpairs) {
        CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                             m->getBondWithIdx(pr.first))
                  ->getIdx() == pr.second);
        CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                             m->getBondWithIdx(pr.second))
                  ->getIdx() == pr.first);
      }

      // we just need to check the other forms for one of the sets of pairs
      // since we know it's the same code underneath
      std::vector<std::pair<unsigned int, unsigned int>> apairs = {{0, 3},
                                                                   {2, 4}};
      for (auto pr : apairs) {
        CHECK(Chirality::getChiralAcrossAtom(m->getAtomWithIdx(1),
                                             m->getAtomWithIdx(pr.first))
                  ->getIdx() == pr.second);
        CHECK(Chirality::getChiralAcrossAtom(m->getAtomWithIdx(1),
                                             m->getAtomWithIdx(pr.second))
                  ->getIdx() == pr.first);
      }
      std::vector<std::pair<unsigned int, unsigned int>> abpairs = {
          {0, 2}, {2, 3}, {3, 0}, {4, 1}};
      for (auto pr : abpairs) {
        CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                             m->getAtomWithIdx(pr.first))
                  ->getIdx() == pr.second);
        CHECK(Chirality::getChiralAcrossAtom(m->getAtomWithIdx(1),
                                             m->getBondWithIdx(pr.second))
                  ->getIdx() == pr.first);
      }
    }
    {
      auto m = "C[Pt@SP2](F)(O)Cl"_smiles;
      REQUIRE(m);
      std::vector<std::pair<unsigned int, unsigned int>> pairs = {{0, 1},
                                                                  {2, 3}};
      for (auto pr : pairs) {
        CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                             m->getBondWithIdx(pr.first))
                  ->getIdx() == pr.second);
        CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                             m->getBondWithIdx(pr.second))
                  ->getIdx() == pr.first);
      }
    }
    {
      auto m = "C[Pt@SP3](F)(O)Cl"_smiles;
      REQUIRE(m);
      std::vector<std::pair<unsigned int, unsigned int>> pairs = {{0, 3},
                                                                  {1, 2}};
      for (auto pr : pairs) {
        CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                             m->getBondWithIdx(pr.first))
                  ->getIdx() == pr.second);
        CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                             m->getBondWithIdx(pr.second))
                  ->getIdx() == pr.first);
      }
    }
  }
  SECTION("3 real ligands") {
    {
      auto m = "C[Pt@SP1](F)O"_smiles;
      REQUIRE(m);
      CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                           m->getBondWithIdx(0))
                ->getIdx() == 1);
      CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                           m->getBondWithIdx(1))
                ->getIdx() == 0);
      CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                           m->getBondWithIdx(2)) == nullptr);
    }
  }
}
TEST_CASE("getChiralAcross edges") {
  Chirality::setAllowNontetrahedralChirality(true);
  SECTION("central atom isn't chiral") {
    auto m = "C[Pt](F)(O)CC"_smiles;
    REQUIRE(m);
    CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                         m->getBondWithIdx(1)) == nullptr);
  }
  SECTION("others") {
    auto m = "C[Pt@SP1](F)(O)CC"_smiles;
    REQUIRE(m);
    // not the central atom:
    CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(0),
                                         m->getBondWithIdx(1)) == nullptr);
    // not a bond to central atom
    CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                         m->getBondWithIdx(4)) == nullptr);
    CHECK(Chirality::getChiralAcrossAtom(m->getAtomWithIdx(1),
                                         m->getBondWithIdx(4)) == nullptr);
    // atom not connected to central atom
    CHECK(Chirality::getChiralAcrossBond(m->getAtomWithIdx(1),
                                         m->getAtomWithIdx(5)) == nullptr);
    CHECK(Chirality::getChiralAcrossAtom(m->getAtomWithIdx(1),
                                         m->getAtomWithIdx(5)) == nullptr);
  }
}

TEST_CASE("hasNonTetrahedralStereo") {
  Chirality::setAllowNontetrahedralChirality(true);
  std::vector<std::pair<std::string, bool>> data = {
      {"C[Pt@SP1](F)(O)CC", true},     {"C[Pt@SP](F)(O)CC", true},
      {"C[Pt](F)(O)CC", false},        {"C[Pt@](F)(O)CC", false},
      {"C[Pt@TH1](F)(O)CC", false},    {"C[Pt@TB1](N)(F)(O)CC", true},
      {"C[Pt@TB](N)(F)(O)CC", true},   {"C[Pt@OH1](N)(N)(F)(O)CC", true},
      {"C[Pt@OH](N)(N)(F)(O)CC", true}};
  for (const auto &pr : data) {
    std::unique_ptr<RWMol> m{SmilesToMol(pr.first)};
    REQUIRE(m);
    CHECK(Chirality::hasNonTetrahedralStereo(m->getAtomWithIdx(1)) ==
          pr.second);
  }
}

TEST_CASE("zero permutation is in SMILES") {
  Chirality::setAllowNontetrahedralChirality(true);
  std::vector<std::string> smis = {"C[CH2][Pt@SP]([CH3])([OH])[F]",
                                   "[CH3][Pt@TB]([NH2])([F])([Cl])[Br]",
                                   "[CH3][Pt@OH]([NH2])([F])([Cl])([Br])[I]"};
  for (auto smi : smis) {
    std::unique_ptr<RWMol> m{SmilesToMol(smi)};
    REQUIRE(m);
    CHECK(MolToSmiles(*m) == smi);
  }
}

TEST_CASE("do not write bogus permutation values") {
  Chirality::setAllowNontetrahedralChirality(true);
  auto m = "CC[Pt@SP](C)(O)F"_smiles;
  REQUIRE(m);
  m->getAtomWithIdx(2)->setProp(common_properties::_chiralPermutation, 10);
  CHECK_THROWS_AS(MolToSmiles(*m), ValueErrorException);
  m->getAtomWithIdx(2)->setProp(common_properties::_chiralPermutation, -1);
  CHECK_THROWS_AS(MolToSmiles(*m), ValueErrorException);
}

TEST_CASE("do not read from/write to SMILES when disabled") {
  Chirality::setAllowNontetrahedralChirality(false);
  SECTION("parsing") {
    auto m = "CC[Pt@SP](C)(O)F"_smiles;
    REQUIRE(m);
    CHECK(m->getAtomWithIdx(2)->getChiralTag() ==
          Atom::ChiralType::CHI_UNSPECIFIED);
    CHECK(
        !m->getAtomWithIdx(2)->hasProp(common_properties::_chiralPermutation));
  }
  SECTION("writing") {
    auto m = "CC[Pt](C)(O)F"_smiles;
    REQUIRE(m);
    m->getAtomWithIdx(2)->setChiralTag(Atom::ChiralType::CHI_SQUAREPLANAR);
    m->getAtomWithIdx(2)->setProp(common_properties::_chiralPermutation, 1);
    auto smi = MolToSmiles(*m);
    CHECK(smi == "C[CH2][Pt]([CH3])([OH])[F]");
  }
}