File: test-reactions.cpp

package info (click to toggle)
rdkit 202503.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 222,000 kB
  • sloc: cpp: 411,111; python: 78,482; ansic: 26,181; java: 8,285; javascript: 4,404; sql: 2,393; yacc: 1,626; lex: 1,267; cs: 1,090; makefile: 581; xml: 229; fortran: 183; sh: 121
file content (164 lines) | stat: -rw-r--r-- 6,810 bytes parent folder | download
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
//
//  Copyright (c) 2025 Glysade Inc and other RDkit contributors
//  All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Novartis Institutes for BioMedical Research Inc.
//       nor the names of its contributors may be used to endorse or promote
//       products derived from this software without specific prior written
//       permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#include "chemdraw.h"
#include "chemdrawreaction.h"
#include <catch2/catch_all.hpp>
#include "RDGeneral/test.h"
#include <RDGeneral/Invariant.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/ChemReactions/Reaction.h>
#include <GraphMol/ChemReactions/ReactionParser.h>
#include <GraphMol/ChemReactions/SanitizeRxn.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>


#include <filesystem>
using namespace RDKit;
using namespace RDKit::v2;

TEST_CASE("CDXML Parser") {
  std::string cdxmlbase =
      std::string(getenv("RDBASE")) + "/Code/GraphMol/test_data/CDXML/";
  SECTION("CDXML REACTION") {
    auto fname = cdxmlbase + "rxn2.cdxml";
    std::vector<std::string> expected = {
        "Cl[c:1]1[cH:4][cH:3][cH:2][cH:6][cH:5]1",
        "OC(O)B[c:7]1[cH:8][cH:9][cH:10][cH:11][cH:12]1",
        "[cH:1]1[cH:4][cH:3][cH:2][c:6](-[c:7]2[cH:8][cH:9][cH:10][cH:11][cH:12]2)[cH:5]1"};

    auto rxns = ChemDrawFileToChemicalReactions(fname);
    CHECK(rxns.size() == 1);
    unsigned int i = 0;
    int count = 0;
    for (auto &mol : rxns[0]->getReactants()) {
      CHECK(mol->getProp<unsigned int>("CDX_SCHEME_ID") == 397);
      CHECK(mol->getProp<unsigned int>("CDX_STEP_ID") == 398);
      CHECK(mol->getProp<unsigned int>("CDX_REAGENT_ID") == i++);
      CHECK(MolToSmiles(*mol) == expected[count++]);
    }
    i = 0;
    for (auto &mol : rxns[0]->getProducts()) {
      CHECK(mol->getProp<unsigned int>("CDX_SCHEME_ID") == 397);
      CHECK(mol->getProp<unsigned int>("CDX_STEP_ID") == 398);
      CHECK(mol->getProp<unsigned int>("CDX_PRODUCT_ID") == i++);
      CHECK(MolToSmiles(*mol) == expected[count++]);
    }

    auto smarts = ChemicalReactionToRxnSmarts(*rxns[0]);
    CHECK(
        smarts ==
        "[#6&D2:2]1:[#6&D2:3]:[#6&D2:4]:[#6&D3:1](:[#6&D2:5]:[#6&D2:6]:1)-[#17&D1].[#6&D3](-[#5&D2]-[#6&D3:7]1:[#6&D2:8]:[#6&D2:9]:[#6&D2:10]:[#6&D2:11]:[#6&D2:12]:1)(-[#8&D1])-[#8&D1]>>[#6&D2:1]1:[#6&D2:5]:[#6&D3:6](:[#6&D2:2]:[#6&D2:3]:[#6&D2:4]:1)-[#6&D3:7]1:[#6&D2:8]:[#6&D2:9]:[#6&D2:10]:[#6&D2:11]:[#6&D2:12]:1");
  }

  SECTION("Github #7528 CDXML Grouped Agents in Reactions") {
    // The failing case had fragments grouped with labels, ensure the grouped
    // cersion and the ungrouped versions have the same results
    auto fname = cdxmlbase + "github7467-grouped-fragments.cdxml";
    auto rxns = ChemDrawFileToChemicalReactions(fname);
    CHECK(rxns.size() == 1);
    fname = cdxmlbase + "github7467-ungrouped-fragments.cdxml";
    auto rxns2 = ChemDrawFileToChemicalReactions(fname);

    CHECK(ChemicalReactionToRxnSmarts(*rxns[0]) ==
          ChemicalReactionToRxnSmarts(*rxns2[0]));

    // Check to see if our understanding of grouped reagents in reactions is
    // correct
    fname = cdxmlbase + "reaction-with-grouped-templates.cdxml";
    auto rxns3 = ChemDrawFileToChemicalReactions(fname);
    CHECK(rxns3.size() == 1);
    std::string rxnb = R"RXN($RXN

      Mrv2004  062120241319

  2  0
$MOL

  Mrv2004 06212413192D

  5  5  0  0  0  0            999 V2000
    2.6221   -4.6475    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    2.6221   -5.4725    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.4070   -5.7274    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.8918   -5.0600    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    3.4070   -4.3926    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  2  0  0  0  0
  2  3  1  0  0  0  0
  3  4  2  0  0  0  0
  4  5  1  0  0  0  0
  5  1  1  0  0  0  0
M  END
$MOL

  Mrv2004 06212413192D

 11 11  0  0  0  0            999 V2000
    6.9305   -4.5100    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    6.9305   -5.3350    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.6450   -5.7475    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.3594   -5.3350    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.3594   -4.5100    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    7.6450   -4.0975    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.6171   -4.4825    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    8.6171   -5.3075    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    9.4020   -5.5624    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    9.8868   -4.8950    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
    9.4020   -4.2276    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  6  1  1  0  0  0  0
  2  3  1  0  0  0  0
  3  4  1  0  0  0  0
  4  5  1  0  0  0  0
  5  6  1  0  0  0  0
  7  8  2  0  0  0  0
 11  7  1  0  0  0  0
  8  9  1  0  0  0  0
  9 10  2  0  0  0  0
 10 11  1  0  0  0  0
M  END
)RXN";
    std::unique_ptr<ChemicalReaction> rxn_mb{RxnBlockToChemicalReaction(rxnb)};
    // CDXMLToReaction is sanitized by default, this might be a mistake...
    unsigned int failed;
    RxnOps::sanitizeRxn(
        *rxn_mb, failed,
        RxnOps::SANITIZE_ADJUST_REACTANTS | RxnOps::SANITIZE_ADJUST_PRODUCTS,
        RxnOps::MatchOnlyAtRgroupsAdjustParams());

    CHECK(rxns3[0]->getNumReactantTemplates() ==
          rxn_mb->getNumReactantTemplates());
    CHECK(ChemicalReactionToRxnSmarts(*rxns3[0]) ==
          ChemicalReactionToRxnSmarts(*rxn_mb));
  }
}