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
|
//
// Copyright (C) 2025 Tad Hurst 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 <algorithm>
#include <fstream>
#include <string>
#include <sstream>
#include <streambuf>
#include "RDGeneral/test.h"
#include <catch2/catch_all.hpp>
#include <RDGeneral/Invariant.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/FileParsers/FileParsers.h>
#include <GraphMol/Atropisomers.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <RDGeneral/BoostStartInclude.h>
#include <boost/lexical_cast.hpp>
#include <RDGeneral/BoostEndInclude.h>
#include <filesystem>
using namespace RDKit;
using namespace v2::FileParsers;
using namespace RDKit;
class ScsiMolTest {
public:
public:
bool generateExpectedFiles = false;
ScsiMolTest() {}
class ScsiTest {
public:
std::string fileName;
unsigned int totalAtomCount;
unsigned int totalBondCount;
unsigned int sgroupCount;
unsigned int totalQueryAtomCount;
unsigned int totalQueryBondCount;
unsigned int querySgroupCount;
bool scsrExpandResult;
SCSRBaseHbondOptions scsrBaseHbondOptions;
ScsiTest(std::string fileNameInit, bool scsrExpandResult,
SCSRBaseHbondOptions scsrBaseHbondOptions,
unsigned int totalAtomCountInit, unsigned int totalBondCountInit,
unsigned int sgroupCountInit, unsigned int totalQueryAtomCountInit,
unsigned int totalQueryBondCountInit,
unsigned int querySgroupCountInit = 0)
: fileName(fileNameInit),
totalAtomCount(totalAtomCountInit),
totalBondCount(totalBondCountInit),
sgroupCount(sgroupCountInit),
totalQueryAtomCount(totalQueryAtomCountInit),
totalQueryBondCount(totalQueryBondCountInit),
querySgroupCount(querySgroupCountInit),
scsrExpandResult(scsrExpandResult),
scsrBaseHbondOptions(scsrBaseHbondOptions) {};
};
void testScsiFiles(const ScsiTest *scsiTest) {
BOOST_LOG(rdInfoLog) << "testing scsr files" << std::endl;
INFO(scsiTest->fileName);
std::string rdbase = getenv("RDBASE");
std::string fName = rdbase +
"/Code/GraphMol/FileParsers/test_data/macromols/" +
scsiTest->fileName;
RDKit::v2::FileParsers::MolFileParserParams pp;
pp.sanitize = true;
pp.removeHs = false;
pp.strictParsing = true;
RDKit::v2::FileParsers::MolFromSCSRParams molFromSCSRParams;
molFromSCSRParams.includeLeavingGroups = true;
molFromSCSRParams.scsrBaseHbondOptions = scsiTest->scsrBaseHbondOptions;
std::unique_ptr<RDKit::RWMol> mol;
if (scsiTest->scsrExpandResult) {
REQUIRE_NOTHROW(mol = MolFromSCSRFile(fName, pp, molFromSCSRParams));
} else {
REQUIRE_THROWS(mol = MolFromSCSRFile(fName, pp, molFromSCSRParams));
return;
}
RDKit::Chirality::removeNonExplicit3DChirality(*(mol.get()));
CHECK(mol != nullptr);
CHECK(mol->getNumAtoms() == scsiTest->totalAtomCount);
CHECK(mol->getNumBonds() == scsiTest->totalBondCount);
CHECK(getSubstanceGroups(*mol).size() == scsiTest->sgroupCount);
// now make the expanded mol in "query" mode - not including any leaving
// groups
molFromSCSRParams.includeLeavingGroups = false;
std::unique_ptr<RDKit::RWMol> molNoLeavingGroups;
if (scsiTest->scsrExpandResult) {
REQUIRE_NOTHROW(molNoLeavingGroups =
MolFromSCSRFile(fName, pp, molFromSCSRParams));
} else {
REQUIRE_THROWS(molNoLeavingGroups =
MolFromSCSRFile(fName, pp, molFromSCSRParams));
return;
}
CHECK(molNoLeavingGroups != nullptr);
CHECK(molNoLeavingGroups->getNumAtoms() == scsiTest->totalQueryAtomCount);
CHECK(molNoLeavingGroups->getNumBonds() == scsiTest->totalQueryBondCount);
CHECK(getSubstanceGroups(*molNoLeavingGroups).size() ==
scsiTest->querySgroupCount);
}
void threeLetterCodeTest(const ScsiTest *scsiTest) {
BOOST_LOG(rdInfoLog) << "testing scsr files with three letter codes"
<< std::endl;
std::string rdbase = getenv("RDBASE");
std::string fName = rdbase +
"/Code/GraphMol/FileParsers/test_data/macromols/" +
scsiTest->fileName;
RDKit::v2::FileParsers::MolFileParserParams pp;
pp.sanitize = false;
pp.removeHs = false;
pp.strictParsing = false;
RDKit::v2::FileParsers::MolFromSCSRParams molFromSCSRParams;
molFromSCSRParams.includeLeavingGroups = true;
molFromSCSRParams.scsrTemplateNames =
RDKit::v2::FileParsers::SCSRTemplateNames::AsEntered;
molFromSCSRParams.scsrBaseHbondOptions = scsiTest->scsrBaseHbondOptions;
std::unique_ptr<RDKit::RWMol> mol;
if (scsiTest->scsrExpandResult) {
REQUIRE_NOTHROW(mol = MolFromSCSRFile(fName, pp, molFromSCSRParams));
} else {
REQUIRE_THROWS(mol = MolFromSCSRFile(fName, pp, molFromSCSRParams));
return;
}
RDKit::Chirality::removeNonExplicit3DChirality(*(mol.get()));
CHECK(mol);
CHECK(mol->getNumAtoms() == scsiTest->totalAtomCount);
CHECK(mol->getNumBonds() == scsiTest->totalBondCount);
CHECK(getSubstanceGroups(*mol).size() == scsiTest->sgroupCount);
molFromSCSRParams.includeLeavingGroups = true;
molFromSCSRParams.scsrTemplateNames =
RDKit::v2::FileParsers::SCSRTemplateNames::UseFirstName;
std::unique_ptr<RWMol> mol2;
if (scsiTest->scsrExpandResult) {
REQUIRE_NOTHROW(mol2 = MolFromSCSRFile(fName, pp, molFromSCSRParams));
} else {
REQUIRE_THROWS(mol2 = MolFromSCSRFile(fName, pp, molFromSCSRParams));
}
// const std::unique_ptr<RDKit::RWMol> mol;
CHECK(mol2);
CHECK(mol2->getNumAtoms() == scsiTest->totalQueryAtomCount);
CHECK(mol2->getNumBonds() == scsiTest->totalQueryBondCount);
CHECK(getSubstanceGroups(*mol2).size() == scsiTest->querySgroupCount);
};
};
TEST_CASE("scsiTests", "scsiTests") {
SECTION("basics") {
std::list<ScsiMolTest::ScsiTest> scsiTests{
ScsiMolTest::ScsiTest("Conjugate.mol", true, SCSRBaseHbondOptions::Auto,
91, 91, 14, 87, 87, 10),
ScsiMolTest::ScsiTest("ModifiedPeptide2.mol", true,
SCSRBaseHbondOptions::Auto, 438, 444, 81, 407,
413, 50),
ScsiMolTest::ScsiTest("DnaBadPairs_NoCh.mol", true,
SCSRBaseHbondOptions::Auto, 84, 94, 14, 80, 90,
10),
ScsiMolTest::ScsiTest("DnaBadPairs.mol", true,
SCSRBaseHbondOptions::UseSapAll, 84, 94, 14, 80,
90, 10),
ScsiMolTest::ScsiTest("DnaTest.mol", false,
SCSRBaseHbondOptions::UseSapAll, 254, 300, 14,
250, 296, 10),
ScsiMolTest::ScsiTest("wobblePairs2.mol", true,
SCSRBaseHbondOptions::UseSapAll, 169, 196, 26,
165, 192, 22),
ScsiMolTest::ScsiTest("wobblePairs.mol", true,
SCSRBaseHbondOptions::UseSapAll, 169, 196, 26,
165, 192, 22),
ScsiMolTest::ScsiTest("KellanRNA.mol", true,
SCSRBaseHbondOptions::UseSapAll, 299, 353, 44,
295, 349, 40),
ScsiMolTest::ScsiTest("DnaTest2.mol", true,
SCSRBaseHbondOptions::UseSapAll, 83, 97, 14, 79,
93, 10),
ScsiMolTest::ScsiTest("DnaTest3.mol", true,
SCSRBaseHbondOptions::UseSapAll, 165, 194, 26,
161, 190, 22),
ScsiMolTest::ScsiTest("KellanError.mol", true,
SCSRBaseHbondOptions::UseSapAll, 244, 263, 39,
236, 255, 31),
ScsiMolTest::ScsiTest("TestRNA2_fixed.mol", true,
SCSRBaseHbondOptions::UseSapAll, 106, 117, 17,
104, 115, 15),
ScsiMolTest::ScsiTest("DnaTest.mol", true, SCSRBaseHbondOptions::Auto,
254, 300, 38, 250, 296, 34),
ScsiMolTest::ScsiTest("wobblePairs2.mol", true,
SCSRBaseHbondOptions::Auto, 169, 196, 26, 165,
192, 22),
ScsiMolTest::ScsiTest("wobblePairs.mol", true,
SCSRBaseHbondOptions::Auto, 169, 196, 26, 165,
192, 22),
ScsiMolTest::ScsiTest("DnaBadPairs.mol", true,
SCSRBaseHbondOptions::Auto, 84, 94, 14, 80, 90,
10),
ScsiMolTest::ScsiTest("DnaTest2.mol", true, SCSRBaseHbondOptions::Auto,
83, 97, 14, 79, 93, 10),
ScsiMolTest::ScsiTest("DnaTest3.mol", true, SCSRBaseHbondOptions::Auto,
165, 194, 26, 161, 190, 22),
ScsiMolTest::ScsiTest("KellanError.mol", true,
SCSRBaseHbondOptions::Auto, 244, 263, 39, 236,
255, 31),
ScsiMolTest::ScsiTest("TestRNA2_fixed.mol", true,
SCSRBaseHbondOptions::Auto, 106, 117, 17, 104,
115, 15),
ScsiMolTest::ScsiTest("TrastuzumabMaxPlus3Register.mol", true,
SCSRBaseHbondOptions::UseSapAll, 7606, 7793, 1451,
7080, 7267, 925),
ScsiMolTest::ScsiTest("TrastuzumabMaxRegister.mol", true,
SCSRBaseHbondOptions::UseSapAll, 7576, 7763, 1445,
7053, 7240, 922),
ScsiMolTest::ScsiTest("Mixed.mol", true,
SCSRBaseHbondOptions::UseSapAll, 51, 54, 8, 51,
54, 8),
ScsiMolTest::ScsiTest("CrossLink.mol", true,
SCSRBaseHbondOptions::UseSapAll, 47, 48, 10, 45,
46, 8),
ScsiMolTest::ScsiTest("cyclic.mol", true,
SCSRBaseHbondOptions::UseSapAll, 45, 47, 8, 45,
47, 8),
ScsiMolTest::ScsiTest("Triplet.mol", true,
SCSRBaseHbondOptions::UseSapAll, 30, 30, 6, 27,
27, 3),
ScsiMolTest::ScsiTest("FromBioviaDoc.mol", true,
SCSRBaseHbondOptions::UseSapAll, 27, 26, 7, 25,
24, 5),
ScsiMolTest::ScsiTest("testSCSR.mol", true,
SCSRBaseHbondOptions::UseSapAll, 64, 66, 15, 57,
59, 8),
ScsiMolTest::ScsiTest("badAtomName.mol", false,
SCSRBaseHbondOptions::UseSapAll, 0, 0, 0, 0, 0,
0),
ScsiMolTest::ScsiTest("badClass.mol", false,
SCSRBaseHbondOptions::UseSapAll, 0, 0, 0, 0, 0,
0),
ScsiMolTest::ScsiTest("badClassTemplate.mol", false,
SCSRBaseHbondOptions::UseSapAll, 0, 0, 0, 0, 0,
0),
ScsiMolTest::ScsiTest("badMissingTemplate.mol", false,
SCSRBaseHbondOptions::UseSapAll, 0, 0, 0, 0, 0,
0),
ScsiMolTest::ScsiTest("obj3dTest.mol", true,
SCSRBaseHbondOptions::UseSapAll, 27, 26, 7, 25,
24, 5),
ScsiMolTest::ScsiTest("obj3dTest2.mol", true,
SCSRBaseHbondOptions::UseSapAll, 27, 26, 7, 25,
24, 5),
ScsiMolTest::ScsiTest("obj3dFoundTwice.mol", false,
SCSRBaseHbondOptions::UseSapAll, 27, 26, 0, 27,
26, 0),
ScsiMolTest::ScsiTest("SgroupFoundTwice.mol", false,
SCSRBaseHbondOptions::UseSapAll, 0, 0, 0, 0, 0,
0),
};
ScsiMolTest scsiMolTest;
for (auto scsiTest : scsiTests) {
BOOST_LOG(rdInfoLog) << "Test: " << scsiTest.fileName << std::endl;
scsiMolTest.testScsiFiles(&scsiTest);
}
}
}
TEST_CASE("nestedParens", "nestedParens") {
SECTION("basics") {
BOOST_LOG(rdInfoLog) << "testing names with parens" << std::endl;
std::string filename = "Ugly.mol";
INFO(filename);
std::string rdbase = getenv("RDBASE");
std::string fName =
rdbase + "/Code/GraphMol/FileParsers/test_data/macromols/" + filename;
RDKit::v2::FileParsers::MolFileParserParams pp;
pp.sanitize = true;
pp.removeHs = false;
pp.strictParsing = true;
RDKit::v2::FileParsers::MolFromSCSRParams molFromSCSRParams;
molFromSCSRParams.includeLeavingGroups = true;
molFromSCSRParams.scsrBaseHbondOptions = SCSRBaseHbondOptions::Auto;
std::unique_ptr<RDKit::RWMol> mol;
REQUIRE_NOTHROW(mol = MolFromSCSRFile(fName, pp, molFromSCSRParams));
CHECK(mol != nullptr);
CHECK(mol->getNumAtoms() == 140);
CHECK(mol->getNumBonds() == 151);
CHECK(getSubstanceGroups(*mol).size() == 11);
// check that the macro atoms parsed have the correct names. In the
// output mol these names will not appear in the SGROUP that defined the
// macroatom. we willcheck just a couple of them
std::string sgroupName;
getSubstanceGroups(*mol)[1].getProp("LABEL", sgroupName);
std::string expected = "((cPr)O(2S-Me)Et)NGly";
CHECK(sgroupName.substr(0, expected.length()) == expected);
getSubstanceGroups(*mol)[5].getProp("LABEL", sgroupName);
expected = "Phe(b-Me2)";
CHECK(sgroupName.substr(0, expected.length()) == expected);
getSubstanceGroups(*mol)[7].getProp("LABEL", sgroupName);
expected = "(PhO(2S-Me)Et)NGly";
CHECK(sgroupName.substr(0, expected.length()) == expected);
}
}
TEST_CASE("threeLetterCodeTest", "threeLetterCodeTest") {
SECTION("basics") {
std::list<ScsiMolTest::ScsiTest> scsiTests{
ScsiMolTest::ScsiTest("PepTla.mol", true,
SCSRBaseHbondOptions::UseSapAll, 26, 25, 7, 26,
25, 7),
};
ScsiMolTest scsiMolTest;
for (auto scsiTest : scsiTests) {
BOOST_LOG(rdInfoLog) << "Test: " << scsiTest.fileName << std::endl;
scsiMolTest.threeLetterCodeTest(&scsiTest);
}
}
}
|