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 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
|
//
//
// Copyright (C) 2018-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 <RDGeneral/test.h>
#include <RDGeneral/RDLog.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/MolOps.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/SubstanceGroup.h>
#include <RDGeneral/FileParseException.h>
#include <RDGeneral/BadFileException.h>
#include "GraphMol/FileParsers/FileParsers.h"
#include "GraphMol/FileParsers/MolSupplier.h"
#include "GraphMol/FileParsers/MolWriters.h"
#include <memory>
#include <cstdlib>
using namespace RDKit;
/* Auxiliary functions */
void testIdxVector(const std::vector<unsigned int> &groupVector,
const std::vector<unsigned int> &reference) {
size_t vecSize = reference.size();
TEST_ASSERT(groupVector.size() == vecSize);
auto sgItr = groupVector.begin();
for (auto refItr = reference.begin(); refItr != reference.end();
++sgItr, ++refItr) {
TEST_ASSERT(1 + *sgItr == *refItr);
}
}
void testBrackets(
const std::vector<SubstanceGroup::Bracket> &brackets,
const std::vector<std::array<std::array<double, 3>, 3>> &reference) {
TEST_ASSERT(brackets.size() == 2);
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k) {
TEST_ASSERT(std::abs(brackets[i][j][k] - reference[i][j][k]) < 1.e-6);
}
}
}
}
RWMol buildSampleMolecule() {
// This builds a RDKit::RWMol with all implemented SubstanceGroup features in
// order to test them. SubstanceGroups and features probably do not make any
// sense.
//// Initialize Molecule ////
RWMol mol;
// Add some atoms and bonds
for (unsigned i = 0; i < 6; ++i) {
mol.addAtom(new Atom(6), false, true);
if (i > 0) {
mol.addBond(i - 1, i, Bond::SINGLE);
}
}
//// First SubstanceGroup ////
{
SubstanceGroup sg(&mol, "MUL");
sg.setProp("index", 1u);
sg.setProp("SUBTYPE", "BLO");
sg.setProp("MULT", "n");
sg.setProp("CONNECT", "HH");
// Add some atoms and bonds
for (unsigned i = 0; i < 3; ++i) {
sg.addAtomWithIdx(i);
sg.addParentAtomWithIdx(i);
sg.addBondWithIdx(i); // add 2 CBONDs + 1 XBOND
}
sg.setProp("COMPNO", 7u);
sg.setProp("ESTATE", "E");
SubstanceGroup::Bracket bracket1 = {{RDGeom::Point3D(1., 3., 0.),
RDGeom::Point3D(5., 7., 0.),
RDGeom::Point3D(0., 0., 0.)}};
sg.addBracket(bracket1);
SubstanceGroup::Bracket bracket2 = {{RDGeom::Point3D(2., 4., 0.),
RDGeom::Point3D(6., 8., 0.),
RDGeom::Point3D(0., 0., 0.)}};
sg.addBracket(bracket2);
// Vector should not be parsed (not a SUP group)
sg.addCState(2, RDGeom::Point3D());
sg.setProp("CLASS", "TEST CLASS");
sg.addAttachPoint(0, 0, "XX");
sg.setProp("BRKTYP", "PAREN");
addSubstanceGroup(mol, sg);
}
//// Second SubstanceGroup ////
{
SubstanceGroup sg(&mol, "SUP");
sg.setProp("index", 2u);
// Add some atoms and bonds
for (unsigned i = 3; i < 6; ++i) {
sg.addAtomWithIdx(i);
sg.addParentAtomWithIdx(i);
sg.addBondWithIdx(i - 1); // add 1 XBOND + 2 CBONDs
}
sg.setProp("LABEL", "TEST LABEL");
// V2000 has only x and y coords; z value restricted to 0.
RDGeom::Point3D vector(3., 4., 0.);
sg.addCState(2, vector); // Vector should be parsed now!
sg.addAttachPoint(3, -1, "YY");
addSubstanceGroup(mol, sg);
}
//// Third SubstanceGroup ////
{
SubstanceGroup sg(&mol, "DAT");
sg.setProp("index", 3u);
sg.setProp("FIELDNAME", "SAMPLE FIELD NAME"); // 30 char max
// Field Type is ignored in V3000
sg.setProp("FIELDINFO", "SAMPLE FIELD INFO"); // 20 char max
sg.setProp("QUERYTYPE", "PQ"); // 2 char max
sg.setProp("QUERYOP", "SAMPLE QUERY OP"); // 15 char max (rest of line)
// This should be properly formatted, but format is not checked
sg.setProp("FIELDDISP", "SAMPLE FIELD DISP");
STR_VECT dataFields = {"SAMPLE DATA FIELD 1", "SAMPLE DATA FIELD 2",
"SAMPLE DATA FIELD 3"};
sg.setProp("DATAFIELDS", dataFields);
addSubstanceGroup(mol, sg);
}
// We have to set a parent with a lower index in V2000 mol blocks:
const auto &sgroups = getSubstanceGroups(mol);
sgroups.at(1).setProp<unsigned int>("PARENT", 1u);
return mol;
}
void checkSampleMolecule(const RWMol &mol) {
// Test a molecule created by buildSampleMolecule (or a copy)
const auto &sgroups = getSubstanceGroups(mol);
TEST_ASSERT(sgroups.size() == 3);
{
// First SubstanceGroup
const auto &sg = sgroups.at(0);
TEST_ASSERT(sg.getProp<std::string>("TYPE") == "MUL");
TEST_ASSERT(sg.getProp<std::string>("SUBTYPE") == "BLO");
TEST_ASSERT(sg.getProp<std::string>("MULT") == "n");
TEST_ASSERT(sg.getProp<std::string>("CONNECT") == "HH");
std::vector<unsigned int> atoms_reference = {1, 2, 3};
auto atoms = sg.getAtoms();
testIdxVector(atoms, atoms_reference);
std::vector<unsigned int> patoms_reference = {1, 2, 3};
testIdxVector(sg.getParentAtoms(), patoms_reference);
std::vector<unsigned int> bonds_reference = {1, 2, 3};
auto bonds = sg.getBonds();
// bonds are not sorted in V3000; sort them here
std::sort(bonds.begin(), bonds.end());
testIdxVector(bonds, bonds_reference);
TEST_ASSERT(sg.getBondType(bonds[0]) == SubstanceGroup::BondType::CBOND);
TEST_ASSERT(sg.getBondType(bonds[1]) == SubstanceGroup::BondType::CBOND);
TEST_ASSERT(sg.getBondType(bonds[2]) == SubstanceGroup::BondType::XBOND);
TEST_ASSERT(sg.getProp<unsigned int>("COMPNO") == 7);
TEST_ASSERT(sg.getProp<std::string>("ESTATE") == "E");
std::vector<std::array<std::array<double, 3>, 3>> brackets_reference = {
{{{{1., 3., 0.}}, {{5., 7., 0.}}, {{0., 0., 0.}}}},
{{{{2., 4., 0.}}, {{6., 8., 0.}}, {{0., 0., 0.}}}},
};
testBrackets(sg.getBrackets(), brackets_reference);
auto cstates = sg.getCStates();
TEST_ASSERT(cstates.size() == 1);
TEST_ASSERT(cstates[0].bondIdx == bonds[2]);
TEST_ASSERT(cstates[0].vector.x == 0.);
TEST_ASSERT(cstates[0].vector.y == 0.);
TEST_ASSERT(cstates[0].vector.z == 0.);
TEST_ASSERT(sg.getProp<std::string>("CLASS") == "TEST CLASS");
auto ap = sg.getAttachPoints();
TEST_ASSERT(ap.size() == 1);
TEST_ASSERT(ap[0].aIdx == atoms[0]);
TEST_ASSERT(ap[0].lvIdx == static_cast<int>(atoms[0]));
TEST_ASSERT(ap[0].id == "XX");
TEST_ASSERT(sg.getProp<std::string>("BRKTYP") == "PAREN");
}
{
// Second SubstanceGroup
const auto &sg = sgroups.at(1);
TEST_ASSERT(sg.getProp<std::string>("TYPE") == "SUP");
std::vector<unsigned int> atoms_reference = {4, 5, 6};
auto atoms = sg.getAtoms();
testIdxVector(atoms, atoms_reference);
std::vector<unsigned int> patoms_reference = {4, 5, 6};
testIdxVector(sg.getParentAtoms(), patoms_reference);
std::vector<unsigned int> bonds_reference = {3, 4, 5};
auto bonds = sg.getBonds();
// bonds are not sorted in V3000; sort them here
std::sort(bonds.begin(), bonds.end());
testIdxVector(bonds, bonds_reference);
TEST_ASSERT(sg.getBondType(bonds[0]) == SubstanceGroup::BondType::XBOND);
TEST_ASSERT(sg.getBondType(bonds[1]) == SubstanceGroup::BondType::CBOND);
TEST_ASSERT(sg.getBondType(bonds[2]) == SubstanceGroup::BondType::CBOND);
TEST_ASSERT(sg.getProp<std::string>("LABEL") == "TEST LABEL");
auto cstates = sg.getCStates();
TEST_ASSERT(cstates.size() == 1);
TEST_ASSERT(cstates[0].bondIdx == bonds[0]);
TEST_ASSERT(cstates[0].vector.x == 3.);
TEST_ASSERT(cstates[0].vector.y == 4.);
TEST_ASSERT(cstates[0].vector.z == 0.);
auto ap = sg.getAttachPoints();
TEST_ASSERT(ap.size() == 1);
TEST_ASSERT(ap[0].aIdx == atoms[0]);
TEST_ASSERT(ap[0].lvIdx == -1);
TEST_ASSERT(ap[0].id == "YY");
TEST_ASSERT(sg.getProp<unsigned int>("PARENT") == 1u);
}
{
// Third SubstanceGroup
const auto &sg = sgroups.at(2);
TEST_ASSERT(sg.getProp<std::string>("TYPE") == "DAT");
TEST_ASSERT(sg.getProp<std::string>("FIELDNAME") == "SAMPLE FIELD NAME");
TEST_ASSERT(sg.getProp<std::string>("FIELDINFO") == "SAMPLE FIELD INFO");
TEST_ASSERT(sg.getProp<std::string>("QUERYTYPE") == "PQ");
TEST_ASSERT(sg.getProp<std::string>("QUERYOP") == "SAMPLE QUERY OP");
TEST_ASSERT(sg.getProp<std::string>("FIELDDISP") == "SAMPLE FIELD DISP");
auto dataFields = sg.getProp<STR_VECT>("DATAFIELDS");
TEST_ASSERT(dataFields.size() == 3);
TEST_ASSERT(dataFields[0] == "SAMPLE DATA FIELD 1");
TEST_ASSERT(dataFields[1] == "SAMPLE DATA FIELD 2");
TEST_ASSERT(dataFields[2] == "SAMPLE DATA FIELD 3");
}
}
/* End Auxiliary functions */
void testCreateSubstanceGroups() {
BOOST_LOG(rdInfoLog) << " ----------> Testing basic SubstanceGroup creation"
<< std::endl;
// Create two SubstanceGroups and add them to a molecule
RWMol mol;
{
SubstanceGroup sg0(&mol, "DAT");
SubstanceGroup sg1(&mol, "SUP");
addSubstanceGroup(mol, sg0);
addSubstanceGroup(mol, sg1);
}
const auto &sgroups = getSubstanceGroups(mol);
TEST_ASSERT(sgroups.size() == 2);
TEST_ASSERT(sgroups.at(0).getProp<std::string>("TYPE") == "DAT");
TEST_ASSERT(sgroups.at(1).getProp<std::string>("TYPE") == "SUP");
}
void testParseSubstanceGroups(const std::string &rdbase) {
BOOST_LOG(rdInfoLog) << " ----------> Parsing Issue3432136_1.mol (V2000)"
<< std::endl;
{
std::string fName =
rdbase + "/Code/GraphMol/FileParsers/test_data/Issue3432136_1.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
const auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
const auto &sgroup = sgroups.at(0);
TEST_ASSERT(sgroup.getProp<std::string>("TYPE") == "MON");
std::vector<unsigned int> atoms_reference = {2, 3, 4, 1, 5};
testIdxVector(sgroup.getAtoms(), atoms_reference);
std::vector<unsigned int> bonds_reference =
{}; // No bonds defined in this mol
testIdxVector(sgroup.getBonds(), bonds_reference);
std::vector<std::array<std::array<double, 3>, 3>> brackets_reference = {
{{{{-3.9679, -0.1670, 0.}}, {{-3.9679, 2.1705, 0.}}, {{0., 0., 0.}}}},
{{{{-0.7244, 2.1705, 0.}}, {{-0.7244, -0.1670, 0.}}, {{0., 0., 0.}}}},
};
testBrackets(sgroup.getBrackets(), brackets_reference);
}
BOOST_LOG(rdInfoLog) << " ----------> Parsing Issue3432136_1.v3k.mol (V3000) "
<< std::endl;
{
std::string fName =
rdbase + "/Code/GraphMol/FileParsers/test_data/Issue3432136_1.v3k.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
const auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
const auto sgroup = sgroups.at(0);
TEST_ASSERT(sgroup.getProp<std::string>("TYPE") == "MON");
std::vector<unsigned int> atoms_reference = {2, 3, 4, 1, 5};
testIdxVector(sgroup.getAtoms(), atoms_reference);
std::vector<unsigned int> bonds_reference =
{}; // No bonds defined in this mol
testIdxVector(sgroup.getBonds(), bonds_reference);
}
BOOST_LOG(rdInfoLog) << " ----------> Parsing Issue3432136_2.v3k.mol (V3000) "
<< std::endl;
{
std::string fName =
rdbase + "/Code/GraphMol/FileParsers/test_data/Issue3432136_2.v3k.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
const auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
const auto sgroup = sgroups.at(0);
TEST_ASSERT(sgroup.getProp<std::string>("TYPE") == "SUP");
TEST_ASSERT(sgroup.getProp<std::string>("CLASS") == "DEMOCLASS");
TEST_ASSERT(sgroup.getProp<std::string>("LABEL") == "abbrev");
std::vector<unsigned int> atoms_reference = {6, 7, 8, 9, 11, 12};
testIdxVector(sgroup.getAtoms(), atoms_reference);
std::vector<unsigned int> bonds_reference = {5};
testIdxVector(sgroup.getBonds(), bonds_reference);
auto bond = sgroup.getBonds()[0];
TEST_ASSERT(sgroup.getBondType(bond) == SubstanceGroup::BondType::XBOND);
}
BOOST_LOG(rdInfoLog) << " ----------> Parsing Issue3432136_2.mol (V2000) "
<< std::endl;
{
std::string fName =
rdbase + "/Code/GraphMol/FileParsers/test_data/Issue3432136_2.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
const auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
const auto sgroup = sgroups.at(0);
TEST_ASSERT(sgroup.getProp<std::string>("TYPE") == "SUP");
std::vector<unsigned int> atoms_reference = {6, 7, 8, 9, 11, 12};
testIdxVector(sgroup.getAtoms(), atoms_reference);
std::vector<unsigned int> bonds_reference = {5};
testIdxVector(sgroup.getBonds(), bonds_reference);
auto bond = sgroup.getBonds()[0];
TEST_ASSERT(sgroup.getBondType(bond) == SubstanceGroup::BondType::XBOND);
}
}
void testSubstanceGroupsRoundTrip(const std::string &rdbase, bool forceV3000) {
BOOST_LOG(rdInfoLog)
<< " ----------> Testing SubstanceGroup writing & parsing Roundtrip ("
<< (forceV3000 ? "V3000" : "V2000") << ')' << std::endl;
std::string fName =
rdbase +
"/Code/GraphMol/FileParsers/test_data/testSubstanceGroupsSample_" +
(forceV3000 ? "V3000" : "V2000") + ".mol";
{
auto sampleMol = buildSampleMolecule();
const auto &sgroups = getSubstanceGroups(sampleMol);
TEST_ASSERT(sgroups.size() == 3);
auto writer = SDWriter(fName);
writer.setForceV3000(forceV3000);
writer.write(sampleMol);
writer.close();
}
std::unique_ptr<RWMol> roundtripMol(MolFileToMol(fName));
checkSampleMolecule(*roundtripMol);
}
void testPickleSubstanceGroups() {
BOOST_LOG(rdInfoLog)
<< " ----------> Testing SubstanceGroup pickling & unpickling Roundtrip"
<< std::endl;
std::string pkl;
{
auto sampleMol = buildSampleMolecule();
MolPickler::pickleMol(sampleMol, pkl);
}
RWMol roundtripMol(pkl);
checkSampleMolecule(roundtripMol);
}
void testModifyMol() {
BOOST_LOG(rdInfoLog)
<< " ----------> Test dropping SubstanceGroups on modification"
<< std::endl;
auto mol = buildSampleMolecule();
// make sure that calling clear() on the molecule actually clears out
// SubstanceGroups
// This was GitHub #3167
{
auto tmol = mol;
TEST_ASSERT(getSubstanceGroups(tmol).size() == 3);
tmol.clear();
TEST_ASSERT(getSubstanceGroups(tmol).size() == 0);
}
auto mol_copy = mol;
const auto &sgroups = getSubstanceGroups(mol);
TEST_ASSERT(sgroups.size() == 3);
{ // insertion does not affect SubstanceGroups
const auto &sgroups = getSubstanceGroups(mol_copy);
TEST_ASSERT(sgroups.size() == 3);
mol_copy.insertMol(mol);
TEST_ASSERT(sgroups.size() == 3);
}
{
// adding an atom does not affect SubstanceGroups
mol_copy = mol;
const auto &sgroups = getSubstanceGroups(mol_copy);
TEST_ASSERT(sgroups.size() == 3);
mol_copy.addAtom();
TEST_ASSERT(sgroups.size() == 3);
}
{
// replacing an atom does not drop SubstanceGroups that include that atom
mol_copy = mol;
const auto &sgroups = getSubstanceGroups(mol_copy);
TEST_ASSERT(sgroups.size() == 3);
auto new_atom = Atom();
mol_copy.replaceAtom(1, &new_atom);
TEST_ASSERT(sgroups.size() == 3);
}
{
// replacing a bond does not drop SubstanceGroups that include that bond
mol_copy = mol;
const auto &sgroups = getSubstanceGroups(mol_copy);
TEST_ASSERT(sgroups.size() == 3);
auto new_bond = Bond(Bond::SINGLE);
mol_copy.replaceBond(1, &new_bond);
TEST_ASSERT(sgroups.size() == 3);
}
{
// removing an atom will drop SubstanceGroups that include that atom
mol_copy = mol;
const auto &sgroups = getSubstanceGroups(mol_copy);
TEST_ASSERT(sgroups.size() == 3);
mol_copy.removeAtom(1);
TEST_ASSERT(sgroups.size() == 1);
}
{
// creating a new bond between existing atoms does not affect
// SubstanceGroups
mol_copy = mol;
const auto &sgroups = getSubstanceGroups(mol_copy);
TEST_ASSERT(sgroups.size() == 3);
mol_copy.addBond(1, 3, Bond::SINGLE);
TEST_ASSERT(sgroups.size() == 3);
}
{
// removing a bond will drop SubstanceGroups that involve that bond
mol_copy = mol;
const auto &sgroups = getSubstanceGroups(mol_copy);
TEST_ASSERT(sgroups.size() == 3);
mol_copy.removeBond(1, 2);
TEST_ASSERT(sgroups.size() == 1);
}
{
// creating a partial bond does not effect SubstanceGroups
mol_copy = mol;
const auto &sgroups = getSubstanceGroups(mol_copy);
TEST_ASSERT(sgroups.size() == 3);
auto *b = mol_copy.createPartialBond(1, Bond::SINGLE);
TEST_ASSERT(sgroups.size() == 3);
delete b;
}
}
void testSubstanceGroupChanges(const std::string &rdbase) {
BOOST_LOG(rdInfoLog) << " ----------> Test SubstanceGroup property changes"
<< std::endl;
std::string fName =
rdbase +
"/Code/GraphMol/FileParsers/sgroup_test_data/Sgroups_Data_01.mol";
std::unique_ptr<ROMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
auto &sgroups1 = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups1.size() == 2);
TEST_ASSERT(sgroups1[0].hasProp("FIELDNAME"));
TEST_ASSERT(sgroups1[0].getProp<std::string>("FIELDNAME") == "pH");
sgroups1[0].setProp("FIELDNAME", "pKa");
const auto &sgroups2 = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups2.size() == 2);
TEST_ASSERT(sgroups2[0].hasProp("FIELDNAME"));
TEST_ASSERT(sgroups2[0].getProp<std::string>("FIELDNAME") == "pKa");
}
void testSubstanceGroupsAndRemoveAtoms(const std::string &rdbase) {
BOOST_LOG(rdInfoLog)
<< " ----------> Test impact of removeAtom on SubstanceGroups"
<< std::endl;
{
std::string fName =
rdbase + "/Code/GraphMol/test_data/sgroups_and_remove_atoms_1.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 13);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
TEST_ASSERT(sgroups[0].getAtoms().size() == 3);
std::vector<unsigned int> tgt{10, 11, 12};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 1);
tgt = {9};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
auto aps = sgroups[0].getAttachPoints();
TEST_ASSERT(aps.size() == 1);
TEST_ASSERT(aps[0].aIdx == 11);
TEST_ASSERT(aps[0].lvIdx == 3);
}
// remove an atom that's not in an S-group
mol->removeAtom(9);
TEST_ASSERT(mol->getNumAtoms() == 12);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
TEST_ASSERT(sgroups[0].getAtoms().size() == 3);
std::vector<unsigned int> tgt{9, 10, 11};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 1);
tgt = {8};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
auto aps = sgroups[0].getAttachPoints();
TEST_ASSERT(aps.size() == 1);
TEST_ASSERT(aps[0].aIdx == 10);
TEST_ASSERT(aps[0].lvIdx == 3);
}
// remove an atom that is in an S-group
mol->removeAtom(10);
TEST_ASSERT(mol->getNumAtoms() == 11);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.empty());
}
}
{
// example with hs to be removed
std::string fName =
rdbase + "/Code/GraphMol/test_data/sgroups_and_remove_atoms_2.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 14);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 2);
TEST_ASSERT(sgroups[0].getAtoms().size() == 3);
std::vector<unsigned int> tgt{9, 11, 12};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 1);
TEST_ASSERT(sgroups[0].getBonds()[0] == 9);
TEST_ASSERT(sgroups[1].getAtoms().size() == 2);
tgt = {10, 13};
TEST_ASSERT(sgroups[1].getAtoms() == tgt);
TEST_ASSERT(sgroups[1].getBonds().size() == 1);
TEST_ASSERT(sgroups[1].getBonds()[0] == 10);
}
// remove an atom in the first S group, make sure the second one survives
mol->removeAtom(11);
TEST_ASSERT(mol->getNumAtoms() == 13);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
TEST_ASSERT(sgroups[0].getAtoms().size() == 2);
std::vector<unsigned int> tgt{10, 12};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 1);
TEST_ASSERT(sgroups[0].getBonds()[0] == 9);
}
}
{ // example with CSTATE
std::string fName =
rdbase + "/Code/GraphMol/test_data/sgroups_and_remove_atoms_3.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 14);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
TEST_ASSERT(sgroups[0].getAtoms().size() == 7);
std::vector<unsigned int> tgt{7, 8, 9, 10, 11, 12, 13};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 1);
tgt = {8};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
auto cstates = sgroups[0].getCStates();
TEST_ASSERT(cstates.size() == 1);
TEST_ASSERT(cstates[0].bondIdx == 8);
}
// remove an atom that's not in an S-group
mol->removeAtom(1);
TEST_ASSERT(mol->getNumAtoms() == 13);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
TEST_ASSERT(sgroups[0].getAtoms().size() == 7);
std::vector<unsigned int> tgt{6, 7, 8, 9, 10, 11, 12};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 1);
tgt = {6};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
auto cstates = sgroups[0].getCStates();
TEST_ASSERT(cstates.size() == 1);
TEST_ASSERT(cstates[0].bondIdx == 6);
}
// remove an atom that is in an S-group
mol->removeAtom(10);
TEST_ASSERT(mol->getNumAtoms() == 12);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.empty());
}
}
{ // example with PATOMS
std::string fName =
rdbase + "/Code/GraphMol/test_data/sgroups_and_remove_atoms_4.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 9);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
TEST_ASSERT(sgroups[0].getAtoms().size() == 6);
std::vector<unsigned int> tgt{2, 3, 5, 6, 7, 8};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 2);
tgt = {7, 6};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
TEST_ASSERT(sgroups[0].getParentAtoms().size() == 3);
tgt = {2, 3, 5};
TEST_ASSERT(sgroups[0].getParentAtoms() == tgt);
}
// remove an atom that's not in an S-group
mol->removeAtom(0u);
TEST_ASSERT(mol->getNumAtoms() == 8);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
TEST_ASSERT(sgroups[0].getAtoms().size() == 6);
std::vector<unsigned int> tgt{1, 2, 4, 5, 6, 7};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 2);
tgt = {6, 5};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
TEST_ASSERT(sgroups[0].getParentAtoms().size() == 3);
tgt = {1, 2, 4};
TEST_ASSERT(sgroups[0].getParentAtoms() == tgt);
}
// remove an atom that is in an S-group
mol->removeAtom(7);
TEST_ASSERT(mol->getNumAtoms() == 7);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.empty());
}
}
{ // example with parent
std::string fName =
rdbase + "/Code/GraphMol/test_data/sgroups_and_remove_atoms_5.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 18);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 3);
TEST_ASSERT(sgroups[0].hasProp("index"))
TEST_ASSERT(sgroups[0].getProp<unsigned int>("index") == 1);
TEST_ASSERT(sgroups[0].getAtoms().size() == 3);
std::vector<unsigned int> tgt{3, 2, 7};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 2);
tgt = {1, 8};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
TEST_ASSERT(sgroups[1].hasProp("index"))
TEST_ASSERT(sgroups[1].getProp<unsigned int>("index") == 2);
TEST_ASSERT(sgroups[1].getAtoms().size() == 6);
tgt = {5, 4, 10, 15, 16, 17};
TEST_ASSERT(sgroups[1].getAtoms() == tgt);
TEST_ASSERT(sgroups[1].getBonds().size() == 2);
tgt = {8, 16};
TEST_ASSERT(sgroups[1].getBonds() == tgt);
TEST_ASSERT(sgroups[1].hasProp("PARENT"))
TEST_ASSERT(sgroups[1].getProp<unsigned int>("PARENT") == 1);
}
// remove an atom that's not in an S-group
mol->removeAtom(0u);
TEST_ASSERT(mol->getNumAtoms() == 17);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 3);
TEST_ASSERT(sgroups[0].getAtoms().size() == 3);
std::vector<unsigned int> tgt{2, 1, 7};
}
// remove an atom that is in a parent S-group
mol->removeAtom(1);
TEST_ASSERT(mol->getNumAtoms() == 16);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 1);
TEST_ASSERT(sgroups[0].getAtoms().size() == 4);
TEST_ASSERT(sgroups[0].hasProp("index"))
TEST_ASSERT(sgroups[0].getProp<unsigned int>("index") == 3);
}
}
{ // example with sgroup hierarchy
std::string fName =
rdbase + "/Code/GraphMol/test_data/sgroups_and_remove_atoms_6.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 13);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 3);
TEST_ASSERT(sgroups[0].hasProp("index"))
TEST_ASSERT(sgroups[0].getProp<unsigned int>("index") == 1);
TEST_ASSERT(sgroups[0].getAtoms().size() == 3);
std::vector<unsigned int> tgt{3, 2, 7};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 2);
tgt = {1, 8};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
TEST_ASSERT(sgroups[1].hasProp("index"))
TEST_ASSERT(sgroups[1].getProp<unsigned int>("index") == 2);
TEST_ASSERT(sgroups[1].getAtoms().size() == 3);
tgt = {5, 4, 10};
TEST_ASSERT(sgroups[1].getAtoms() == tgt);
TEST_ASSERT(sgroups[1].getBonds().size() == 2);
tgt = {8, 11};
TEST_ASSERT(sgroups[1].getBonds() == tgt);
TEST_ASSERT(sgroups[1].hasProp("PARENT"))
TEST_ASSERT(sgroups[1].getProp<unsigned int>("PARENT") == 1);
TEST_ASSERT(sgroups[2].hasProp("index"))
TEST_ASSERT(sgroups[2].getProp<unsigned int>("index") == 3);
TEST_ASSERT(sgroups[2].getAtoms().size() == 2);
tgt = {9, 8};
TEST_ASSERT(sgroups[2].getAtoms() == tgt);
TEST_ASSERT(sgroups[2].getBonds().size() == 2);
tgt = {5, 10};
TEST_ASSERT(sgroups[2].getBonds() == tgt);
TEST_ASSERT(sgroups[2].hasProp("PARENT"))
TEST_ASSERT(sgroups[2].getProp<unsigned int>("PARENT") == 2);
}
// remove an atom that's not in an S-group
mol->removeAtom(0u);
TEST_ASSERT(mol->getNumAtoms() == 12);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 3);
TEST_ASSERT(sgroups[0].getAtoms().size() == 3);
std::vector<unsigned int> tgt{2, 1, 6};
}
// remove an atom that is in an S-group at the top of the hierarchy
mol->removeAtom(1);
TEST_ASSERT(mol->getNumAtoms() == 11);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 0);
}
}
{ // example with things in odd order and large id values
// NOTE that biovia draw doesn't parse this file properly
std::string fName =
rdbase + "/Code/GraphMol/test_data/sgroups_and_remove_atoms_7.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 18);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 3);
TEST_ASSERT(sgroups[2].hasProp("index"))
TEST_ASSERT(sgroups[2].getProp<unsigned int>("index") == 20);
TEST_ASSERT(sgroups[2].getAtoms().size() == 6);
std::vector<unsigned int> tgt{5, 4, 10, 15, 16, 17};
TEST_ASSERT(sgroups[2].getAtoms() == tgt);
TEST_ASSERT(sgroups[2].getBonds().size() == 2);
tgt = {8, 16};
TEST_ASSERT(sgroups[2].getBonds() == tgt);
TEST_ASSERT(sgroups[2].getParentAtoms().size() == 3);
tgt = {5, 4, 10};
TEST_ASSERT(sgroups[2].getParentAtoms() == tgt);
TEST_ASSERT(sgroups[2].hasProp("PARENT"))
TEST_ASSERT(sgroups[2].getProp<unsigned int>("PARENT") == 10);
TEST_ASSERT(sgroups[1].hasProp("index"))
TEST_ASSERT(sgroups[1].getProp<unsigned int>("index") == 10);
TEST_ASSERT(sgroups[1].getAtoms().size() == 3);
tgt = {3, 2, 7};
TEST_ASSERT(sgroups[1].getAtoms() == tgt);
TEST_ASSERT(sgroups[1].getBonds().size() == 2);
tgt = {1, 8};
TEST_ASSERT(sgroups[1].getBonds() == tgt);
TEST_ASSERT(sgroups[1].getParentAtoms().size() == 0);
}
}
{ // copolymer example with PARENT
std::string fName =
rdbase + "/Code/GraphMol/test_data/sgroups_copolymer.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 9);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 3);
TEST_ASSERT(sgroups[2].hasProp("index"))
TEST_ASSERT(sgroups[2].getProp<unsigned int>("index") == 10);
TEST_ASSERT(sgroups[2].getAtoms().size() == 5);
std::vector<unsigned int> tgt{3, 2, 4, 5, 7};
TEST_ASSERT(sgroups[2].getAtoms() == tgt);
TEST_ASSERT(sgroups[2].getBonds().size() == 2);
tgt = {1, 5};
TEST_ASSERT(sgroups[2].getBonds() == tgt);
TEST_ASSERT(!sgroups[2].hasProp("PARENT"))
TEST_ASSERT(sgroups[0].hasProp("index"))
TEST_ASSERT(sgroups[0].getProp<unsigned int>("index") == 2);
TEST_ASSERT(sgroups[0].getAtoms().size() == 2);
tgt = {3, 2};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 2);
tgt = {1, 3};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
TEST_ASSERT(sgroups[0].hasProp("PARENT"))
TEST_ASSERT(sgroups[0].getProp<unsigned int>("PARENT") == 10);
}
// remove an atom that's not in an S-group
mol->removeAtom(0u);
TEST_ASSERT(mol->getNumAtoms() == 8);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 3);
TEST_ASSERT(sgroups[2].hasProp("index"))
TEST_ASSERT(sgroups[2].getProp<unsigned int>("index") == 10);
TEST_ASSERT(sgroups[2].getAtoms().size() == 5);
std::vector<unsigned int> tgt{2, 1, 3, 4, 6};
TEST_ASSERT(sgroups[2].getAtoms() == tgt);
TEST_ASSERT(sgroups[2].getBonds().size() == 2);
tgt = {0, 4};
TEST_ASSERT(sgroups[2].getBonds() == tgt);
TEST_ASSERT(!sgroups[2].hasProp("PARENT"))
TEST_ASSERT(sgroups[0].hasProp("index"))
TEST_ASSERT(sgroups[0].getProp<unsigned int>("index") == 2);
TEST_ASSERT(sgroups[0].getAtoms().size() == 2);
tgt = {2, 1};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 2);
tgt = {0, 2};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
TEST_ASSERT(sgroups[0].hasProp("PARENT"))
TEST_ASSERT(sgroups[0].getProp<unsigned int>("PARENT") == 10);
}
// remove an atom from parent, make sure children also get deleted
mol->removeAtom(1u);
TEST_ASSERT(mol->getNumAtoms() == 7);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 0);
}
}
{ // copolymer example 2 with PARENT, same as the previous but with a
// different ordering in the input file
std::string fName =
rdbase + "/Code/GraphMol/test_data/sgroups_copolymer2.mol";
std::unique_ptr<RWMol> mol(MolFileToMol(fName));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 9);
{
auto &sgroups = getSubstanceGroups(*mol);
TEST_ASSERT(sgroups.size() == 3);
TEST_ASSERT(sgroups[2].hasProp("index"))
TEST_ASSERT(sgroups[2].getProp<unsigned int>("index") == 10);
TEST_ASSERT(sgroups[2].getAtoms().size() == 5);
std::vector<unsigned int> tgt{3, 2, 4, 5, 7};
TEST_ASSERT(sgroups[2].getAtoms() == tgt);
TEST_ASSERT(sgroups[2].getBonds().size() == 2);
tgt = {1, 5};
TEST_ASSERT(sgroups[2].getBonds() == tgt);
TEST_ASSERT(!sgroups[2].hasProp("PARENT"))
TEST_ASSERT(sgroups[0].hasProp("index"))
TEST_ASSERT(sgroups[0].getProp<unsigned int>("index") == 2);
TEST_ASSERT(sgroups[0].getAtoms().size() == 2);
tgt = {3, 2};
TEST_ASSERT(sgroups[0].getAtoms() == tgt);
TEST_ASSERT(sgroups[0].getBonds().size() == 2);
tgt = {1, 3};
TEST_ASSERT(sgroups[0].getBonds() == tgt);
TEST_ASSERT(sgroups[0].hasProp("PARENT"))
TEST_ASSERT(sgroups[0].getProp<unsigned int>("PARENT") == 10);
}
}
}
void testSubstanceGroupsAndRemoveHs(const std::string &rdbase) {
BOOST_LOG(rdInfoLog) << " ----------> Test impact of SubstanceGroups on "
"removeHs (GitHub #3169)"
<< std::endl;
{
std::string fName =
rdbase + "/Code/GraphMol/test_data/sgroups_and_remove_Hs_1.mol";
bool sanitize = true;
bool removeHs = false;
std::unique_ptr<RWMol> mol(MolFileToMol(fName, sanitize, removeHs));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 8);
TEST_ASSERT(getSubstanceGroups(*mol).size() == 1);
{
RWMol mol_copy = *mol;
MolOps::RemoveHsParameters ps;
ps.removeInSGroups = true;
MolOps::removeHs(mol_copy, ps);
TEST_ASSERT(mol_copy.getNumAtoms() == 6);
TEST_ASSERT(getSubstanceGroups(mol_copy).size() == 1);
}
{ // check removeAllHs() too
RWMol mol_copy = *mol;
MolOps::removeAllHs(mol_copy);
TEST_ASSERT(mol_copy.getNumAtoms() == 6);
TEST_ASSERT(getSubstanceGroups(mol_copy).size() == 1);
}
}
}
void testKeepSpecialHsOnRemoval() {
BOOST_LOG(rdInfoLog) << "-----------------------\n Testing github 5399: "
<< "Allow removal of non-special H atoms in SGroups"
<< std::endl;
// Keep XBOND Hydrogens
{
auto mol = R"CTAB(
RDKit 2D
0 0 0 0 0 0 0 0 0 0999 V3000
M V30 BEGIN CTAB
M V30 COUNTS 5 4 1 0 0
M V30 BEGIN ATOM
M V30 1 C -3.914286 2.714286 0.000000 0
M V30 2 H -2.914286 2.714286 0.000000 0
M V30 3 H -4.247615 1.771475 0.000000 0
M V30 4 H -4.491638 3.530781 0.000000 0
M V30 5 H -4.904803 2.576897 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 1 1 4
M V30 4 1 1 5
M V30 END BOND
M V30 BEGIN SGROUP
M V30 1 SRU 0 ATOMS=(3 1 3 4) XBONDS=(2 1 4) CONNECT=HT
M V30 END SGROUP
M V30 END CTAB
M END)CTAB"_ctab;
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 3);
auto sgs = getSubstanceGroups(*mol);
TEST_ASSERT(sgs.size() == 1);
std::vector<unsigned> ref_atoms{0};
TEST_ASSERT(sgs[0].getAtoms() == ref_atoms);
}
// Keep SAP aIdx (but remove lvIdx).
// (This molecule is completely bogus).
{
auto molblock = R"CTAB(
RDKit 2D
0 0 0 0 0 0 0 0 0 0999 V3000
M V30 BEGIN CTAB
M V30 COUNTS 4 2 1 0 0
M V30 BEGIN ATOM
M V30 1 C -1.8551 -2.605 0 0
M V30 2 C -0.5214 -3.375 0 0
M V30 3 H -0.5214 -4.9149 0 0
M V30 4 H 0.8123 -5.6849 0 0
M V30 END ATOM
M V30 BEGIN BOND
M V30 1 1 1 2
M V30 2 1 2 3
M V30 END BOND
M V30 BEGIN SGROUP
M V30 1 SUP 0 ATOMS=(2 3 4) SAP=(3 3 4 1) XBONDS=(1 2) LABEL="bogus"
M V30 END SGROUP
M V30 END CTAB
M END)CTAB";
bool sanitize = false; // One H has a weird valence
bool removeHs = false;
std::unique_ptr<RWMol> mol(MolBlockToMol(molblock, sanitize, removeHs));
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 4);
MolOps::removeAllHs(*mol);
TEST_ASSERT(mol->getNumAtoms() == 3);
auto sgs = getSubstanceGroups(*mol);
TEST_ASSERT(sgs.size() == 1);
std::vector<unsigned> ref_atoms{2};
TEST_ASSERT(sgs[0].getAtoms() == ref_atoms);
auto saps = sgs[0].getAttachPoints();
TEST_ASSERT(saps.size() == 1);
TEST_ASSERT(saps[0].lvIdx == -1);
}
// H atom part of a CState bond
{
auto mol = R"CTAB(
RDKit 2D
0 0 0 0 0 0 0 0 0 0999 V3000
M V30 BEGIN CTAB
M V30 COUNTS 5 4 1 0 0
M V30 BEGIN ATOM
M V30 1 H 12.0001 -4.402 0 0
M V30 2 C 10.4601 -4.402 0 0
M V30 3 H 8.1485 -3.075 0 0
M V30 4 O 9.6883 -3.0729 0 0
M V30 5 O 9.6919 -5.725 0 0
M V30 END ATOM
M V30 BEGIN BOND
M V30 1 1 1 2
M V30 2 1 4 3
M V30 3 2 2 5
M V30 4 1 4 2
M V30 END BOND
M V30 BEGIN SGROUP
M V30 1 SUP 0 ATOMS=(4 2 3 4 5) SAP=(3 1 2 1) XBONDS=(1 1) CSTATE=(4 1 0 0.82 0) LABEL=Boc
M V30 END SGROUP
M V30 END CTAB
M END)CTAB"_ctab;
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 4);
auto sgs = getSubstanceGroups(*mol);
TEST_ASSERT(sgs.size() == 1);
std::vector<unsigned> ref_atoms{{1, 2, 3}};
TEST_ASSERT(sgs[0].getAtoms() == ref_atoms);
auto csts = sgs[0].getCStates();
TEST_ASSERT(csts.size() == 1);
TEST_ASSERT(csts[0].bondIdx == 0);
}
// SGroups with only H atoms
{
auto mol = R"CTAB(
RDKit 2D
0 0 0 0 0 0 0 0 0 0999 V3000
M V30 BEGIN CTAB
M V30 COUNTS 5 4 2 0 0
M V30 BEGIN ATOM
M V30 1 C -3.514286 2.200000 0.000000 0
M V30 2 H -2.514286 2.200000 0.000000 0
M V30 3 H -3.847615 1.257190 0.000000 0
M V30 4 H -4.091638 3.016495 0.000000 0
M V30 5 H -4.504803 2.062612 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 1 1 4
M V30 4 1 1 5
M V30 END BOND
M V30 BEGIN SGROUP
M V30 1 DAT 0 ATOMS=(1 4) FIELDNAME="dummy1" FIELDDATA="H1"
M V30 2 DAT 0 ATOMS=(1 5) FIELDNAME="dummy2" FIELDDATA="H2"
M V30 END SGROUP
M V30 END CTAB
M END)CTAB"_ctab;
TEST_ASSERT(mol);
TEST_ASSERT(mol->getNumAtoms() == 3);
auto sgs = getSubstanceGroups(*mol);
TEST_ASSERT(sgs.size() == 2);
for (unsigned i = 0; i < 2; ++i) {
std::vector<unsigned> ref_atoms{i + 1};
TEST_ASSERT(sgs[i].getAtoms() == ref_atoms);
}
}
}
int main() {
std::string rdbase = std::string(getenv("RDBASE"));
if (rdbase.empty()) {
std::cerr << "\n\n RDBASE has not been set, aborting.\n\n";
return 1;
}
RDLog::InitLogs();
testCreateSubstanceGroups();
testParseSubstanceGroups(rdbase);
testSubstanceGroupsRoundTrip(rdbase, false); // test V2000
testSubstanceGroupsRoundTrip(rdbase, true); // test V3000
testPickleSubstanceGroups();
testModifyMol();
testSubstanceGroupChanges(rdbase);
testSubstanceGroupsAndRemoveAtoms(rdbase);
testSubstanceGroupsAndRemoveHs(rdbase);
testKeepSpecialHsOnRemoval();
return 0;
}
|