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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
|
//
// Copyright (C) 2019-2025 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.
//
// Tests of substructure searching
//
#include <catch2/catch_all.hpp>
#include <tuple>
#include <utility>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <GraphMol/Substruct/SubstructMatch.h>
#include <GraphMol/FileParsers/FileParsers.h>
#include <GraphMol/QueryOps.h>
#include <GraphMol/MolPickler.h>
using namespace RDKit;
typedef std::tuple<std::string, std::string, size_t> matchCase;
class _IsSubstructOf : public Catch::Matchers::MatcherBase<const ROMol &> {
ROMol const *m_mol;
SubstructMatchParameters m_ps;
public:
_IsSubstructOf(const ROMol &m) : m_mol(&m) {}
_IsSubstructOf(const ROMol &m, SubstructMatchParameters ps)
: m_mol(&m), m_ps(std::move(ps)) {}
bool match(const ROMol &query) const override {
return !SubstructMatch(*m_mol, query, m_ps).empty();
}
std::string describe() const override {
std::ostringstream ss;
ss << "is not a substructure of " << MolToCXSmiles(*m_mol);
return ss.str();
}
};
static _IsSubstructOf IsSubstructOf(const ROMol &m,
const SubstructMatchParameters &ps) {
return _IsSubstructOf(m, ps);
}
static _IsSubstructOf IsSubstructOf(const ROMol &m) {
return _IsSubstructOf(m);
}
namespace Catch {
// ""_smiles returns an RWMol.
template <>
struct StringMaker<RDKit::RWMol> {
static std::string convert(RDKit::RWMol const &m) { return MolToCXSmiles(m); }
};
} // namespace Catch
TEST_CASE("substructure parameters", "[substruct]") {
SECTION("chirality") {
auto mol1 = "CCC[C@@H]1CN(CCC)CCN1"_smiles;
auto mol2 = "CCC[C@H]1CN(CCC)CCN1"_smiles;
REQUIRE(mol1);
REQUIRE(mol2);
SubstructMatchParameters ps;
// default is to ignore chirality:
CHECK(SubstructMatch(*mol1, *mol2, ps).size() == 1);
CHECK(SubstructMatch(*mol1, *mol1, ps).size() == 1);
ps.useChirality = true;
CHECK_THAT(*mol2, !IsSubstructOf(*mol1, ps));
CHECK(SubstructMatch(*mol1, *mol1, ps).size() == 1);
}
SECTION("conjugated matching aromaticity 1") {
auto mol1 = "C1=COC=C1"_smiles;
REQUIRE(mol1);
RWMol mol2(*mol1);
MolOps::Kekulize(mol2);
SubstructMatchParameters ps;
CHECK(SubstructMatch(*mol1, mol2, ps).size() == 0);
CHECK(SubstructMatch(mol2, *mol1, ps).size() == 0);
ps.aromaticMatchesConjugated = true;
CHECK(SubstructMatch(*mol1, mol2, ps).size() == 1);
CHECK(SubstructMatch(mol2, *mol1, ps).size() == 1);
}
SECTION("conjugated matching aromaticity 2") {
auto mol1 = "c1ccccc1"_smiles;
REQUIRE(mol1);
RWMol mol2(*mol1);
MolOps::Kekulize(mol2);
SubstructMatchParameters ps;
CHECK_THAT(mol2, !IsSubstructOf(*mol1));
CHECK_THAT(*mol1, !IsSubstructOf(mol2));
ps.aromaticMatchesConjugated = true;
CHECK(SubstructMatch(*mol1, mol2, ps).size() == 1);
CHECK(SubstructMatch(mol2, *mol1, ps).size() == 1);
}
SECTION("conjugated matching aromaticity bulk") {
std::vector<matchCase> examples;
examples.push_back(
std::make_tuple(std::string("c1ccccc1"), std::string("C1CCCCC1"), 0));
examples.push_back(
std::make_tuple(std::string("C1CCCCC1"), std::string("c1ccccc1"), 0));
examples.push_back(std::make_tuple(std::string("O=C1C=CC(=O)C=C1"),
std::string("c1ccccc1"), 1));
SubstructMatchParameters ps;
ps.aromaticMatchesConjugated = true;
for (const auto &example : examples) {
// std::cerr << " " << std::get<0>(example) << " - "
// << std::get<1>(example) << std::endl;
std::unique_ptr<RWMol> m1(SmilesToMol(std::get<0>(example)));
REQUIRE(m1);
std::unique_ptr<RWMol> m2(SmilesToMol(std::get<1>(example)));
CHECK(SubstructMatch(*m1, *m2, ps).size() == std::get<2>(example));
}
}
SECTION("looping") {
auto mol1 = "CC(=O)C(=O)C(=O)"_smiles;
auto mol2 = "C=O"_smiles;
REQUIRE(mol1);
REQUIRE(mol2);
for (auto match : SubstructMatch(*mol1, *mol2)) {
CHECK(match.size() == 2);
}
}
SECTION("atom properties") {
std::vector<matchCase> examples;
examples.push_back(
std::make_tuple(std::string("CCCCCCCCC"), std::string("CCC"), 7));
examples.push_back(
std::make_tuple(std::string("CCCCCCCCC |atomProp:0.test_prop.1|"),
std::string("CCC |atomProp:0.test_prop.1|"), 1));
examples.push_back(
std::make_tuple(std::string("CCCCCCCCC |atomProp:0.test_prop.1|"),
std::string("CCC"), 6));
examples.push_back(
std::make_tuple(std::string("CCCCCCCCC"),
std::string("CCC |atomProp:0.test_prop.1|"), 0));
examples.push_back(
std::make_tuple(std::string("CCCCCCCCC |atomProp:0.test_prop.1|"),
std::string("CCC |atomProp:0.test_prop.2|"), 0));
SubstructMatchParameters ps;
ps.atomProperties = {"test_prop"};
for (const auto &example : examples) {
std::unique_ptr<RWMol> m1(SmilesToMol(std::get<0>(example)));
REQUIRE(m1);
std::unique_ptr<RWMol> m2(SmilesToMol(std::get<1>(example)));
REQUIRE(m2);
CHECK(SubstructMatch(*m1, *m2, ps).size() == std::get<2>(example));
}
}
SECTION("bond properties") {
std::unique_ptr<RWMol> m(SmilesToMol("CCCCCCCCC"));
std::unique_ptr<RWMol> m_with_prop(SmilesToMol("CCCCCCCCC"));
m_with_prop->getBondWithIdx(0)->setProp("test_prop", "1");
std::unique_ptr<RWMol> q(SmilesToMol("CCC"));
std::unique_ptr<RWMol> q_with_prop(SmilesToMol("CCC"));
std::unique_ptr<RWMol> q_with_prop2(SmilesToMol("CCC"));
q_with_prop->getBondWithIdx(0)->setProp("test_prop", "1");
q_with_prop2->getBondWithIdx(0)->setProp("test_prop", "2");
SubstructMatchParameters ps;
ps.bondProperties = {"test_prop"};
CHECK(SubstructMatch(*m, *q, ps).size() == 7);
CHECK(SubstructMatch(*m_with_prop, *q_with_prop, ps).size() == 1);
CHECK(SubstructMatch(*m_with_prop, *q_with_prop2, ps).size() == 0);
CHECK(SubstructMatch(*m_with_prop, *q, ps).size() == 6);
CHECK(SubstructMatch(*m, *q_with_prop, ps).size() == 0);
// now check with bond and atom properties
m_with_prop->getAtomWithIdx(0)->setProp("test_prop", "1");
q_with_prop->getAtomWithIdx(0)->setProp("test_prop", "1");
ps.atomProperties = {"test_prop"};
CHECK(SubstructMatch(*m_with_prop, *q_with_prop, ps).size() == 1);
CHECK(SubstructMatch(*m_with_prop, *q_with_prop2, ps).size() == 0);
CHECK(SubstructMatch(*m_with_prop, *q, ps).size() == 6);
CHECK(SubstructMatch(*m, *q_with_prop, ps).size() == 0);
// Currently, a property set as an int will match to a property
// set as a different type if they cast to the same value
// TODO: Ensure property types are the same in substructure matching
q_with_prop->getBondWithIdx(0)->clearProp("test_prop");
q_with_prop->getBondWithIdx(0)->setProp<int>("test_prop", 1);
CHECK(SubstructMatch(*m_with_prop, *q_with_prop, ps).size() == 1);
}
}
namespace {
bool no_match(const ROMol &mol, const std::span<const unsigned int> &ids) {
RDUNUSED_PARAM(mol);
RDUNUSED_PARAM(ids);
return false;
}
bool always_match(const ROMol &mol, const std::span<const unsigned int> &ids) {
RDUNUSED_PARAM(mol);
RDUNUSED_PARAM(ids);
return true;
}
bool bigger(const ROMol &mol, const std::span<const unsigned int> &ids) {
RDUNUSED_PARAM(mol);
return std::accumulate(ids.begin(), ids.end(), 0) > 5;
}
} // namespace
TEST_CASE("providing a final match function", "[substruct]") {
SECTION("basics") {
auto mol1 = "CCOC"_smiles;
auto mol2 = "CCO"_smiles;
REQUIRE(mol1);
REQUIRE(mol2);
SubstructMatchParameters ps;
CHECK(SubstructMatch(*mol1, *mol2, ps).size() == 1);
ps.extraFinalCheck = &no_match;
CHECK(SubstructMatch(*mol1, *mol2, ps).size() == 0);
ps.extraFinalCheck = &always_match;
CHECK(SubstructMatch(*mol1, *mol2, ps).size() == 1);
}
SECTION("test 2") {
auto mol1 = "CCOCC"_smiles;
auto mol2 = "CCO"_smiles;
REQUIRE(mol1);
REQUIRE(mol2);
SubstructMatchParameters ps;
CHECK(SubstructMatch(*mol1, *mol2, ps).size() == 2);
ps.extraFinalCheck = &bigger;
CHECK(SubstructMatch(*mol1, *mol2, ps).size() == 1);
}
}
TEST_CASE("Enhanced stereochemistry", "[substruct][StereoGroup]") {
// Chirality specifications.
// 1. An achiral molecule: CC(O)C(CC)F means unknown/all stereoisomers
// 2. A chiral molecule: C[C@H](O)[C@H](CC)F means 1 stereoisomer
// 3. A chiral molecule with an AND specifier: C[C@H](O)[C@H](CC)F |a1:1,3|
// means both stereoisomers
// 4. A chiral molecule with an OR specifier: C[C@H](O)[C@H](CC)F |o1:1,3|
// means one of the two stereoisomers
auto mol_achiral = "CC(O)C(CC)F"_smiles;
auto mol_chiral = "C[C@H](O)[C@H](CC)F"_smiles;
auto mol_and = "C[C@H](O)[C@H](CC)F |&1:1,3|"_smiles;
auto mol_or = "C[C@H](O)[C@H](CC)F |o1:1,3|"_smiles;
auto mol_absolute = "C[C@H](O)[C@H](CC)F |a:1,3|"_smiles;
auto diastereomer = "C[C@H](O)[C@@H](CC)F"_smiles;
SubstructMatchParameters ps;
ps.useChirality = true;
ps.useEnhancedStereo = true;
SECTION("achiral search matches anything") {
CHECK_THAT(*mol_achiral, IsSubstructOf(*mol_chiral, ps));
CHECK_THAT(*mol_achiral, IsSubstructOf(*mol_and, ps));
CHECK_THAT(*mol_achiral, IsSubstructOf(*mol_or, ps));
CHECK_THAT(*mol_achiral, IsSubstructOf(*mol_absolute, ps));
CHECK_THAT(*mol_achiral, IsSubstructOf(*diastereomer, ps));
}
SECTION("chiral molecule is a substructure of AND or OR") {
CHECK_THAT(*mol_chiral, !IsSubstructOf(*mol_achiral, ps));
CHECK_THAT(*mol_chiral, IsSubstructOf(*mol_and, ps));
CHECK_THAT(*mol_chiral, IsSubstructOf(*mol_or, ps));
CHECK_THAT(*mol_chiral, !IsSubstructOf(*diastereomer, ps));
CHECK_THAT(*mol_absolute, !IsSubstructOf(*mol_achiral, ps));
CHECK_THAT(*mol_absolute, IsSubstructOf(*mol_and, ps));
CHECK_THAT(*mol_absolute, IsSubstructOf(*mol_or, ps));
CHECK_THAT(*mol_absolute, !IsSubstructOf(*diastereomer, ps));
}
SECTION("AND query only matches AND") {
// because it means BOTH, and only AND includes both.
CHECK_THAT(*mol_and, !IsSubstructOf(*mol_or, ps));
CHECK_THAT(*mol_and, IsSubstructOf(*mol_and, ps));
CHECK_THAT(*mol_and, !IsSubstructOf(*mol_absolute, ps));
CHECK_THAT(*mol_and, !IsSubstructOf(*mol_chiral, ps));
CHECK_THAT(*mol_and, !IsSubstructOf(*mol_achiral, ps));
}
SECTION("An OR query matches AND and OR") {
// because AND is both, so it's a superset of the molecules described in
// the OR
CHECK_THAT(*mol_or, !IsSubstructOf(*mol_chiral, ps));
CHECK_THAT(*mol_or, !IsSubstructOf(*mol_absolute, ps));
CHECK_THAT(*mol_or, !IsSubstructOf(*diastereomer, ps));
CHECK_THAT(*mol_or, IsSubstructOf(*mol_or, ps));
CHECK_THAT(*mol_or, IsSubstructOf(*mol_and, ps));
}
SECTION("AND and OR match their enantiomer") {
// This is, like, the point of And/Or
auto enantiomer = "C[C@@H](O)[C@@H](CC)F"_smiles;
CHECK_THAT(*enantiomer, IsSubstructOf(*mol_and, ps));
CHECK_THAT(*enantiomer, IsSubstructOf(*mol_or, ps));
}
SECTION("But not some arbitrary diastereomer") {
CHECK_THAT(*diastereomer, !IsSubstructOf(*mol_and, ps));
CHECK_THAT(*diastereomer, !IsSubstructOf(*mol_or, ps));
}
SECTION("Mixed stereo groups include single stereo groups") {
auto mol_mixed_or = "C[C@H](O)[C@H](CC)F |o1:1,o2:3|"_smiles;
CHECK_THAT(*mol_mixed_or, !IsSubstructOf(*mol_or, ps));
// OR refers to two of the 4 molecules that mol_mixed_or
CHECK_THAT(*mol_or, IsSubstructOf(*mol_mixed_or, ps));
auto mol_mixed_or2 = "C[C@H](O)[C@@H](CC)F |o1:1,o2:3|"_smiles;
CHECK_THAT(*mol_mixed_or2, !IsSubstructOf(*mol_or, ps));
CHECK_THAT(*mol_or, IsSubstructOf(*mol_mixed_or2, ps));
// I'm not sure about these ones, but they should be symmetric:
auto mol_mixed_or_and_abs = "C[C@H](O)[C@H](CC)F |o1:1|"_smiles;
CHECK_THAT(*mol_mixed_or_and_abs, !IsSubstructOf(*mol_or, ps));
CHECK_THAT(*mol_or, !IsSubstructOf(*mol_mixed_or_and_abs, ps));
auto mol_mixed_or_and_abs2 = "C[C@@H](O)[C@H](CC)F |o1:1|"_smiles;
CHECK_THAT(*mol_mixed_or_and_abs2, !IsSubstructOf(*mol_or, ps));
CHECK_THAT(*mol_or, !IsSubstructOf(*mol_mixed_or_and_abs, ps));
}
SECTION("It's OK to match part of a stereo group, though") {
auto mol_and_long = "F[C@@H](O)C[C@@H](CC)F |&1:1,3|"_smiles;
auto mol_and_partial = "F[C@@H](O)C |&1:1|"_smiles;
auto mol_or_long = "F[C@@H](O)C[C@@H](CC)F |o1:1,3|"_smiles;
auto mol_or_partial = "F[C@@H](O)C |o1:1|"_smiles;
CHECK_THAT(*mol_and_partial, IsSubstructOf(*mol_and_long, ps));
CHECK_THAT(*mol_or_partial, IsSubstructOf(*mol_or_long, ps));
CHECK_THAT(*mol_or_partial, IsSubstructOf(*mol_and_long, ps));
CHECK_THAT(*mol_and_partial, !IsSubstructOf(*mol_or_long, ps));
}
}
TEST_CASE("Github #4138: empty query produces non-empty results",
"[substruct][bug]") {
auto mol = "C1CCCCO1"_smiles;
auto emol = ""_smiles;
auto qry = "C"_smarts;
auto eqry = ""_smarts;
REQUIRE(mol);
REQUIRE(qry);
SECTION("empty query") {
{
auto matches = SubstructMatch(*mol, *eqry);
CHECK(matches.empty());
}
{
std::vector<MatchVectType> matches;
CHECK(!SubstructMatch(*mol, *eqry, matches));
CHECK(matches.empty());
}
{
MatchVectType match;
CHECK(!SubstructMatch(*mol, *eqry, match));
CHECK(match.empty());
}
}
SECTION("empty mol") {
{
auto matches = SubstructMatch(*emol, *qry);
CHECK(matches.empty());
}
{
std::vector<MatchVectType> matches;
CHECK(!SubstructMatch(*emol, *qry, matches));
CHECK(matches.empty());
}
{
MatchVectType match;
CHECK(!SubstructMatch(*emol, *qry, match));
CHECK(match.empty());
}
}
}
TEST_CASE("Github #4558: GetSubstructMatches() loops at 43690 iterations",
"[substruct][bug]") {
// We need LOTS of water molecules here.
auto num_mols = 22000u;
std::stringstream smi;
for (auto i = 1u; i < num_mols; ++i) {
smi << "[H]O[H].";
}
smi << "[H]O[H]"; // last one (notice we started at 1)
int debug = 0;
bool sanitize = false; // don't sanitize, it takes too long.
std::unique_ptr<ROMol> mol(SmilesToMol(smi.str(), debug, sanitize));
REQUIRE(mol);
auto qry = "[H]O[H]"_smarts;
SubstructMatchParameters ps;
ps.uniquify = false; // don't uniquify, it takes too long.
ps.maxMatches = 3 * num_mols; // exceed the numer of matches we expect
auto matches = SubstructMatch(*mol, *qry, ps);
CHECK(matches.size() == num_mols * 2);
}
TEST_CASE(
"Github #888: GetSubstructMatches uniquify and maxMatches don't work well together ") {
SECTION("Basics") {
auto m = "CCCCCC"_smiles;
auto q = "CC"_smarts;
REQUIRE(m);
REQUIRE(q);
SubstructMatchParameters ps;
ps.uniquify = false;
ps.maxMatches = 4;
auto matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 4);
ps.uniquify = true;
matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 4);
ps.useChirality = true;
matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 4);
}
SECTION("interaction with chirality") {
auto m = "C/C=C/C=C/C=C\\C=C/C=C/C"_smiles;
auto q = "C/C=C/C"_smarts;
REQUIRE(m);
REQUIRE(q);
SubstructMatchParameters ps;
ps.uniquify = false;
ps.maxMatches = 2;
auto matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 2);
ps.uniquify = true;
matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 2);
ps.useChirality = true;
matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 2);
ps.maxMatches = 1000;
matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 3);
}
SECTION("interactions with recursive SMARTS") {
{
auto m = "N#CC#N"_smiles;
REQUIRE(m);
auto q = "[!$(*#*)]"_smarts;
REQUIRE(q);
SubstructMatchParameters ps;
ps.uniquify = true;
ps.maxMatches = 1;
auto matches = SubstructMatch(*m, *q, ps);
CHECK(matches.empty());
}
{
auto m = "N#CC#N"_smiles;
REQUIRE(m);
auto q = "[$(*#*)&!D1]"_smarts;
REQUIRE(q);
SubstructMatchParameters ps;
ps.uniquify = true;
auto matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 2);
}
{
auto m = "N#CCC#N"_smiles;
REQUIRE(m);
auto q = "[!$(*#*)&!D1]-&!@[!$(*#*)&!D1]"_smarts;
REQUIRE(q);
SubstructMatchParameters ps;
auto matches = SubstructMatch(*m, *q, ps);
CHECK(matches.empty());
}
}
}
TEST_CASE("Github #6017: add maxRecursiveMatches to SubstructMatchParameters") {
SECTION("Basics") {
auto m = "OCC(O)C(O)C(O)C(O)CO"_smiles;
auto q = "[$(CO)][$(CO)]"_smarts;
REQUIRE(m);
REQUIRE(q);
SubstructMatchParameters ps;
ps.uniquify = true;
{
auto matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 5);
}
// if maxRecursiveMatches isn't larger than maxMatches this will fail
ps.maxMatches = 3;
{
auto matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 3);
}
}
SECTION("maxMatches larger than maxRecursiveMatches") {
auto m = "OCC(O)C(O)C(O)C(O)CO"_smiles;
auto q = "[$(CO)]C"_smarts;
REQUIRE(m);
REQUIRE(q);
SubstructMatchParameters ps;
ps.uniquify = true;
ps.maxMatches = 3;
ps.maxRecursiveMatches = 2;
{
auto matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == 3);
}
}
}
TEST_CASE(
"GitHub Issue #6983: SubstructMatch maxRecursiveMatches is not being honored",
"[bug][substruct]") {
constexpr unsigned num_atoms = 1005;
std::string smiles;
smiles.reserve(num_atoms * 2);
// 'smiles' already contains 1 O, so start from 1
// so we end up with 'num_atoms' water mols
smiles += "O";
for (unsigned i = 1; i < num_atoms; ++i) {
smiles += ".O";
}
std::unique_ptr<RWMol> m(SmilesToMol(smiles));
auto q = "[$(O)]"_smarts;
REQUIRE(m);
REQUIRE(q);
SubstructMatchParameters ps;
ps.maxMatches = num_atoms * 2;
ps.maxRecursiveMatches = ps.maxMatches;
{
auto matches = SubstructMatch(*m, *q, ps);
CHECK(matches.size() == num_atoms);
}
}
TEST_CASE("pickling HasPropWithValue queries") {
SubstructMatchParameters ps;
SECTION("basics int") {
auto mol = "CC"_smarts;
auto target = "CC"_smiles;
REQUIRE(mol);
REQUIRE(target);
RWMol mol2(*mol);
RWMol mol3(*mol);
mol->getAtomWithIdx(0)->expandQuery(makePropQuery<Atom, int>("foo", 1, 1));
mol->getBondWithIdx(0)->expandQuery(makePropQuery<Bond, int>("bar", 1, 0));
mol2.getAtomWithIdx(0)->expandQuery(makePropQuery<Atom, int>("foo", 1, 0));
mol2.getBondWithIdx(0)->expandQuery(makePropQuery<Bond, int>("bar", 1, 0));
mol3.getAtomWithIdx(0)->expandQuery(makePropQuery<Atom, int>("foo", 2, 0));
mol3.getBondWithIdx(0)->expandQuery(makePropQuery<Bond, int>("bar", 2, 0));
CHECK(SubstructMatch(*target, *mol, ps).size() == 0);
CHECK(SubstructMatch(*target, mol2, ps).size() == 0);
CHECK(SubstructMatch(*target, mol3, ps).size() == 0);
target->getAtomWithIdx(0)->setProp<int>("foo", 2);
target->getBondWithIdx(0)->setProp<int>("bar", 1);
CHECK(SubstructMatch(*target, *mol, ps).size() == 1);
CHECK(SubstructMatch(*target, mol2, ps).size() == 0);
CHECK(SubstructMatch(*target, mol3, ps).size() == 0);
{
std::string pkl;
MolPickler::pickleMol(*mol, pkl);
RWMol pklmol(pkl);
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
CHECK(SubstructMatch(*target, pklmol, ps).size() == 1);
// make sure we are idempotent in pickling
CHECK(SubstructMatch(*target, *mol, ps).size() == 1);
}
{
std::string pkl;
MolPickler::pickleMol(mol2, pkl);
RWMol pklmol(pkl);
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
CHECK(SubstructMatch(*target, pklmol, ps).size() == 0);
// make sure we are idempotent in pickling
CHECK(SubstructMatch(*target, mol2, ps).size() == 0);
}
{
std::string pkl;
MolPickler::pickleMol(mol3, pkl);
RWMol pklmol(pkl);
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
CHECK(SubstructMatch(*target, pklmol, ps).size() == 0);
// make sure we are idempotent in pickling
CHECK(SubstructMatch(*target, mol3, ps).size() == 0);
}
}
SECTION("basics string") {
auto mol = "CC"_smarts;
auto target = "CC"_smiles;
REQUIRE(mol);
REQUIRE(target);
RWMol mol2(*mol);
RWMol mol3(*mol);
mol->getAtomWithIdx(0)->expandQuery(
makePropQuery<Atom, std::string>("foo", "asdfs"));
mol->getBondWithIdx(0)->expandQuery(
makePropQuery<Bond, std::string>("bar", "dsafasdf"));
mol2.getAtomWithIdx(0)->expandQuery(
makePropQuery<Atom, std::string>("foo", "asdfs"));
mol2.getBondWithIdx(0)->expandQuery(
makePropQuery<Bond, std::string>("bar", "dsa"));
mol3.getAtomWithIdx(0)->expandQuery(
makePropQuery<Atom, std::string>("foo", "asd"));
mol3.getBondWithIdx(0)->expandQuery(
makePropQuery<Bond, std::string>("bar", "dsafasdf"));
CHECK(SubstructMatch(*target, *mol, ps).size() == 0);
CHECK(SubstructMatch(*target, mol2, ps).size() == 0);
CHECK(SubstructMatch(*target, mol3, ps).size() == 0);
target->getAtomWithIdx(0)->setProp<std::string>("foo", "asdfs");
target->getBondWithIdx(0)->setProp<std::string>("bar", "dsafasdf");
CHECK(SubstructMatch(*target, *mol, ps).size() == 1);
CHECK(SubstructMatch(*target, mol2, ps).size() == 0);
CHECK(SubstructMatch(*target, mol3, ps).size() == 0);
{
std::string pkl;
MolPickler::pickleMol(*mol, pkl);
RWMol pklmol(pkl);
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
REQUIRE(pklmol.getBondWithIdx(0)->hasQuery());
CHECK(SubstructMatch(*target, pklmol, ps).size() == 1);
// make sure we are idempotent in pickling
CHECK(SubstructMatch(*target, *mol, ps).size() == 1);
}
{
std::string pkl;
MolPickler::pickleMol(mol2, pkl);
RWMol pklmol(pkl);
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
REQUIRE(pklmol.getBondWithIdx(0)->hasQuery());
CHECK(SubstructMatch(*target, pklmol, ps).size() == 0);
// make sure we are idempotent in pickling
CHECK(SubstructMatch(*target, mol2, ps).size() == 0);
}
{
std::string pkl;
MolPickler::pickleMol(mol3, pkl);
RWMol pklmol(pkl);
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
REQUIRE(pklmol.getBondWithIdx(0)->hasQuery());
CHECK(SubstructMatch(*target, pklmol, ps).size() == 0);
// make sure we are idempotent in pickling
CHECK(SubstructMatch(*target, mol3, ps).size() == 0);
}
}
SECTION("basics EBV") {
auto mol = "CC"_smarts;
auto target = "CC"_smiles;
REQUIRE(mol);
REQUIRE(target);
ExplicitBitVect bv(10);
bv.setBit(0);
bv.setBit(2);
bv.setBit(3);
bv.setBit(5);
bv.setBit(7);
bv.setBit(9);
RWMol mol2(*mol);
RWMol mol3(*mol);
mol->getAtomWithIdx(0)->expandQuery(
makePropQuery<Atom, ExplicitBitVect>("foo", bv, 0.0));
mol->getBondWithIdx(0)->expandQuery(
makePropQuery<Bond, ExplicitBitVect>("bar", bv, 0.0));
ExplicitBitVect bv2(bv);
bv2.unsetBit(9);
mol2.getAtomWithIdx(0)->expandQuery(
makePropQuery<Atom, ExplicitBitVect>("foo", bv2, 0.0));
mol3.getBondWithIdx(0)->expandQuery(
makePropQuery<Bond, ExplicitBitVect>("bar", bv2, 0.0));
CHECK(SubstructMatch(*target, *mol, ps).size() == 0);
CHECK(SubstructMatch(*target, mol2, ps).size() == 0);
CHECK(SubstructMatch(*target, mol3, ps).size() == 0);
target->getAtomWithIdx(0)->setProp<ExplicitBitVect>("foo", bv);
target->getBondWithIdx(0)->setProp<ExplicitBitVect>("bar", bv);
CHECK(SubstructMatch(*target, *mol, ps).size() == 1);
CHECK(SubstructMatch(*target, mol2, ps).size() == 0);
CHECK(SubstructMatch(*target, mol3, ps).size() == 0);
{
std::string pkl;
MolPickler::pickleMol(*mol, pkl);
RWMol pklmol(pkl);
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
REQUIRE(pklmol.getBondWithIdx(0)->hasQuery());
CHECK(SubstructMatch(*target, pklmol, ps).size() == 1);
// make sure we are idempotent in pickling
CHECK(SubstructMatch(*target, *mol, ps).size() == 1);
}
{
std::string pkl;
MolPickler::pickleMol(mol2, pkl);
RWMol pklmol(pkl);
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
REQUIRE(pklmol.getBondWithIdx(0)->hasQuery());
CHECK(SubstructMatch(*target, pklmol, ps).size() == 0);
// make sure we are idempotent in pickling
CHECK(SubstructMatch(*target, mol2, ps).size() == 0);
}
{
std::string pkl;
MolPickler::pickleMol(mol3, pkl);
RWMol pklmol(pkl);
REQUIRE(pklmol.getAtomWithIdx(0)->hasQuery());
REQUIRE(pklmol.getBondWithIdx(0)->hasQuery());
CHECK(SubstructMatch(*target, pklmol, ps).size() == 0);
// make sure we are idempotent in pickling
CHECK(SubstructMatch(*target, mol3, ps).size() == 0);
}
}
}
TEST_CASE("specified query matches unspecified atom") {
SECTION("atom basics") {
auto q = "F[C@](Cl)(Br)C"_smarts;
REQUIRE(q);
auto m1 = "F[C@](Cl)(Br)C"_smiles;
REQUIRE(m1);
auto m2 = "FC(Cl)(Br)C"_smiles;
REQUIRE(m2);
auto m3 = "F[C@@](Cl)(Br)C"_smiles;
REQUIRE(m3);
SubstructMatchParameters ps;
ps.useChirality = true;
CHECK(SubstructMatch(*m1, *q, ps).size() == 1);
CHECK(SubstructMatch(*m2, *q, ps).empty());
CHECK(SubstructMatch(*m3, *q, ps).empty());
ps.specifiedStereoQueryMatchesUnspecified = true;
CHECK(SubstructMatch(*m1, *q, ps).size() == 1);
CHECK(SubstructMatch(*m2, *q, ps).size() == 1);
CHECK(SubstructMatch(*m3, *q, ps).empty());
}
SECTION("bond basics") {
auto q = "F/C=C/Br"_smarts;
REQUIRE(q);
auto m1 = "F/C=C/Br"_smiles;
REQUIRE(m1);
auto m2 = "FC=CBr"_smiles;
REQUIRE(m2);
auto m3 = "F/C=C\\Br"_smiles;
REQUIRE(m3);
SubstructMatchParameters ps;
ps.useChirality = true;
CHECK(SubstructMatch(*m1, *q, ps).size() == 1);
CHECK(SubstructMatch(*m2, *q, ps).empty());
CHECK(SubstructMatch(*m3, *q, ps).empty());
ps.specifiedStereoQueryMatchesUnspecified = true;
CHECK(SubstructMatch(*m1, *q, ps).size() == 1);
CHECK(SubstructMatch(*m2, *q, ps).size() == 1);
CHECK(SubstructMatch(*m3, *q, ps).empty());
}
}
TEST_CASE(
"Github 8162: conjugated triple bonds match aromatic bonds with aromaticMatchesConjugated") {
SECTION("as reported") {
auto qry = R"CTAB(ACS Document 1996
ChemDraw01092510212D
0 0 0 0 0 0 V3000
M V30 BEGIN CTAB
M V30 COUNTS 4 3 0 0 0
M V30 BEGIN ATOM
M V30 1 N 0.357236 0.412500 0.000000 0
M V30 2 C 1.071707 0.000000 0.000000 0
M V30 3 C -0.357236 0.000000 0.000000 0
M V30 4 N -1.071707 -0.412500 0.000000 0
M V30 END ATOM
M V30 BEGIN BOND
M V30 1 1 1 2
M V30 2 1 1 3
M V30 3 3 3 4
M V30 END BOND
M V30 END CTAB
M END)CTAB"_ctab;
REQUIRE(qry);
auto mol = "CN1C=NC=C1"_smiles;
REQUIRE(mol);
SubstructMatchParameters ps;
ps.aromaticMatchesConjugated = true;
CHECK(SubstructMatch(*mol, *qry, ps).empty());
}
SECTION("details") {
auto m_triple = "C#N"_smiles;
REQUIRE(m_triple);
m_triple->getBondWithIdx(0)->setIsConjugated(true);
auto m_double = "C=N"_smiles;
REQUIRE(m_double);
m_double->getBondWithIdx(0)->setIsConjugated(true);
auto m_single = "CN"_smiles;
REQUIRE(m_single);
m_single->getBondWithIdx(0)->setIsConjugated(true);
auto m_aromatic = "CN"_smiles;
REQUIRE(m_aromatic);
m_aromatic->getBondWithIdx(0)->setBondType(Bond::AROMATIC);
m_aromatic->getBondWithIdx(0)->setIsAromatic(true);
m_aromatic->getBondWithIdx(0)->setIsConjugated(true);
SubstructMatchParameters ps;
ps.aromaticMatchesConjugated = true;
CHECK(SubstructMatch(*m_triple, *m_aromatic, ps).empty());
CHECK(SubstructMatch(*m_aromatic, *m_triple, ps).empty());
CHECK(SubstructMatch(*m_double, *m_aromatic, ps).size() == 1);
CHECK(SubstructMatch(*m_aromatic, *m_double, ps).size() == 1);
CHECK(SubstructMatch(*m_single, *m_aromatic, ps).size() == 1);
CHECK(SubstructMatch(*m_aromatic, *m_single, ps).size() == 1);
}
}
TEST_CASE("Github #7295", "CIS/TRANS in aromatic ring") {
SECTION("Smart CIS/TRANS bonds should match single or aromatic") {
SubstructMatchParameters ps;
auto query =
"[O:1]=[c:2]1/[c:3](=[C:4]/[c:5]2:[c:10]:[c:9]:[c:8]:[c:7]:[c:6]:2):[s:11]:[c:12]2:[n:13]:1-[N:14]-[C:15]-[N:20]-[N:21]=2"_smarts;
auto mol =
"[O:1]=[c:2]1/[c:3](=[CH:4]/[c:5]2[cH:6][cH:7][cH:8][cH:9][cH:10]2)[s:11][c:12]2[n:13]1[NH:14][C:15]1([CH2:16][CH2:17][CH2:18][CH2:19]1)[NH:20][N:21]=2"_smiles;
CHECK(SubstructMatch(*mol, *query, ps).size() == 1);
ps.useChirality = true;
CHECK(SubstructMatch(*mol, *query, ps).size() == 1);
auto mol2 =
"[O:1]=[c:2]1\\[c:3](=[CH:4]/[c:5]2[cH:6][cH:7][cH:8][cH:9][cH:10]2)[s:11][c:12]2[n:13]1[NH:14][C:15]1([CH2:16][CH2:17][CH2:18][CH2:19]1)[NH:20][N:21]=2"_smiles;
CHECK(SubstructMatch(*mol2, *query, ps).size() == 0);
}
}
TEST_CASE(
"Github #8485: Allow single/double bonds to match aromatic in substructure search") {
SECTION("basics") {
auto m = "C1=CC=CC=C1"_smiles;
REQUIRE(m);
SubstructMatchParameters ps;
ps.aromaticMatchesSingleOrDouble = true;
{
auto q = "[#6]:[#6]"_smarts;
REQUIRE(q);
CHECK(!SubstructMatch(*m, *q).empty());
CHECK(!SubstructMatch(*m, *q, ps).empty());
}
{
auto q = "C-C"_smiles;
REQUIRE(q);
CHECK(SubstructMatch(*m, *q).empty());
CHECK(!SubstructMatch(*m, *q, ps).empty());
}
{
auto q = "C=C"_smiles;
REQUIRE(q);
CHECK(SubstructMatch(*m, *q).empty());
CHECK(!SubstructMatch(*m, *q, ps).empty());
}
}
SECTION("as reported") {
auto m = "C1=CC2=C(OC1=O)C(=CC=C2)C(=O)O"_smiles;
REQUIRE(m);
auto q = "CC(=O)OC1=CC=CC=C1C(=O)O"_smiles;
REQUIRE(q);
SubstructMatchParameters ps;
ps.aromaticMatchesSingleOrDouble = true;
CHECK(SubstructMatch(*m, *q).empty());
CHECK(!SubstructMatch(*m, *q, ps).empty());
}
SECTION("symmetry") {
auto m1 = "C1=CC=CC=C1"_smiles;
REQUIRE(m1);
auto m2 = "C1CCCCC1"_smiles;
REQUIRE(m2);
CHECK(SubstructMatch(*m1, *m2).empty());
CHECK(SubstructMatch(*m2, *m1).empty());
SubstructMatchParameters ps;
ps.aromaticMatchesSingleOrDouble = true;
CHECK(!SubstructMatch(*m1, *m2, ps).empty());
CHECK(!SubstructMatch(*m2, *m1, ps).empty());
}
}
|