File: testPropertyLists.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (239 lines) | stat: -rw-r--r-- 9,592 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
//  Copyright (C) 2019 Greg Landrum
//   @@ 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 <catch2/catch_all.hpp>

#include <GraphMol/RDKitBase.h>
#include <GraphMol/FileParsers/FileParsers.h>
#include <GraphMol/FileParsers/FileParserUtils.h>
#include <GraphMol/FileParsers/MolSupplier.h>
#include <GraphMol/SmilesParse/SmilesParse.h>

using namespace RDKit;

TEST_CASE("Property list conversion", "[atom_list_properties]") {
  SECTION("basics: iprops") {
    auto m = "COC"_smiles;
    REQUIRE(m);
    m->setProp("atom.iprop.foo1", "1   6 9");
    m->setProp("atom.iprop.foo2", "3 n/a   9");
    m->setProp("atom.iprop.foo3", "[?]  5 1 ?");
    m->setProp("atom.iprop.foo4", "[foo] 3 foo   9");
    FileParserUtils::applyMolListPropsToAtoms<std::int64_t>(*m, "atom.iprop.");
    CHECK(m->getAtomWithIdx(0)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo2"));
    CHECK(!m->getAtomWithIdx(1)->hasProp("foo2"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo2"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo3"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo3"));
    CHECK(!m->getAtomWithIdx(2)->hasProp("foo3"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo4"));
    CHECK(!m->getAtomWithIdx(1)->hasProp("foo4"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo4"));
    CHECK(m->getAtomWithIdx(1)->getProp<std::int64_t>("foo1") == 6);
    CHECK(m->getAtomWithIdx(2)->getProp<std::int64_t>("foo2") == 9);
    CHECK(m->getAtomWithIdx(1)->getProp<std::int64_t>("foo3") == 1);
    CHECK(m->getAtomWithIdx(1)->getProp<std::string>("foo3") == "1");
  }
  SECTION("basics: dprops") {
    auto m = "COC"_smiles;
    REQUIRE(m);
    m->setProp("atom.dprop.foo1", "1   6 9");
    m->setProp("atom.dprop.foo2", "3 n/a   9");
    m->setProp("atom.dprop.foo3", "[?]  5 1 ?");
    FileParserUtils::applyMolListPropsToAtoms<double>(*m, "atom.dprop.");
    CHECK(m->getAtomWithIdx(0)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo2"));
    CHECK(!m->getAtomWithIdx(1)->hasProp("foo2"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo2"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo3"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo3"));
    CHECK(!m->getAtomWithIdx(2)->hasProp("foo3"));
    CHECK(m->getAtomWithIdx(1)->getProp<double>("foo1") == 6);
    CHECK(m->getAtomWithIdx(2)->getProp<double>("foo2") == 9);
    CHECK(m->getAtomWithIdx(1)->getProp<double>("foo3") == 1);
  }
  SECTION("basics: props") {
    auto m = "COC"_smiles;
    REQUIRE(m);
    m->setProp("atom.prop.foo1", "1   6 9");
    m->setProp("atom.prop.foo2", "3 n/a   9");
    m->setProp("atom.prop.foo3", "[?]  5 1 ?");
    FileParserUtils::applyMolListPropsToAtoms<std::string>(*m, "atom.prop.");
    CHECK(m->getAtomWithIdx(0)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo2"));
    CHECK(!m->getAtomWithIdx(1)->hasProp("foo2"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo2"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo3"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo3"));
    CHECK(!m->getAtomWithIdx(2)->hasProp("foo3"));
    CHECK(m->getAtomWithIdx(1)->getProp<std::string>("foo1") == "6");
    CHECK(m->getAtomWithIdx(2)->getProp<std::string>("foo2") == "9");
    CHECK(m->getAtomWithIdx(1)->getProp<std::string>("foo3") == "1");
  }
  SECTION("basics: bprops") {
    auto m = "COC"_smiles;
    REQUIRE(m);
    m->setProp("atom.bprop.foo1", "1   0 0");
    m->setProp("atom.bprop.foo2", "0 n/a   1");
    m->setProp("atom.bprop.foo3", "[?]  0 1 ?");
    FileParserUtils::applyMolListPropsToAtoms<bool>(*m, "atom.bprop.");
    CHECK(m->getAtomWithIdx(0)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo2"));
    CHECK(!m->getAtomWithIdx(1)->hasProp("foo2"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo2"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo3"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo3"));
    CHECK(!m->getAtomWithIdx(2)->hasProp("foo3"));
    CHECK(m->getAtomWithIdx(1)->getProp<bool>("foo1") == false);
    CHECK(m->getAtomWithIdx(2)->getProp<bool>("foo2") == true);
    CHECK(m->getAtomWithIdx(1)->getProp<bool>("foo3") == true);
  }
}
TEST_CASE("processMolPropertyLists", "[atom_list_properties]") {
  SECTION("basics") {
    auto m = "COC"_smiles;
    REQUIRE(m);
    m->setProp("atom.iprop.foo1", "1   6 9");
    m->setProp("atom.dprop.foo2", "3 n/a   9");
    m->setProp("atom.prop.foo3", "[?]  5 1 ?");
    m->setProp("atom.bprop.foo4", "1 0 0");
    FileParserUtils::processMolPropertyLists(*m);
    CHECK(m->getAtomWithIdx(0)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo1"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo2"));
    CHECK(!m->getAtomWithIdx(1)->hasProp("foo2"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo2"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo3"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo3"));
    CHECK(!m->getAtomWithIdx(2)->hasProp("foo3"));
    CHECK(m->getAtomWithIdx(0)->hasProp("foo4"));
    CHECK(m->getAtomWithIdx(1)->hasProp("foo4"));
    CHECK(m->getAtomWithIdx(2)->hasProp("foo4"));
    CHECK(m->getAtomWithIdx(1)->getProp<std::int64_t>("foo1") == 6);
    CHECK(m->getAtomWithIdx(2)->getProp<double>("foo2") == 9);
    CHECK(m->getAtomWithIdx(1)->getProp<std::string>("foo3") == "1");
    CHECK(m->getAtomWithIdx(1)->getProp<bool>("foo4") == false);
  }
}

TEST_CASE("basic SDF handling", "[SDF][atom_list_properties]") {
  std::string sdf = R"SDF(
     RDKit  2D

  3  3  0  0  0  0  0  0  0  0999 V2000
    0.8660    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
   -0.4330    0.7500    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
   -0.4330   -0.7500    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0
  2  3  1  0
  3  1  1  0
M  END
>  <atom.dprop.PartialCharge>  (1) 
0.008 -0.314 0.008

>  <atom.iprop.NumHeavyNeighbors>  (1) 
2 2 2

>  <atom.prop.AtomLabel>  (1)
C1 N2 C3

>  <atom.bprop.IsCarbon>  (1) 
1 0 1

>  <atom.prop.PartiallyMissing>  (1) 
one n/a three

>  <atom.iprop.PartiallyMissingInt>  (1) 
[?] 2 2 ?

$$$$
)SDF";
  SECTION("no processing") {
    RDKit::SDMolSupplier suppl;
    suppl.setData(sdf);
    suppl.setProcessPropertyLists(false);
    std::unique_ptr<RDKit::ROMol> m(suppl[0]);
    REQUIRE(m);
    CHECK(m->hasProp("atom.prop.AtomLabel"));
    CHECK(!m->getAtomWithIdx(0)->hasProp("AtomLabel"));
  }
  SECTION("with processing") {
    RDKit::SDMolSupplier suppl;
    suppl.setData(sdf);
    std::unique_ptr<RDKit::ROMol> m(suppl[0]);
    REQUIRE(m);
    CHECK(m->hasProp("atom.prop.AtomLabel"));
    CHECK(m->getAtomWithIdx(0)->hasProp("AtomLabel"));
  }
}

TEST_CASE("createAtomPropertyLists", "[atom_list_properties]") {
  SECTION("basics") {
    auto m = "COC"_smiles;
    REQUIRE(m);
    m->getAtomWithIdx(0)->setProp<std::int64_t>("foo1", 1);
    m->getAtomWithIdx(2)->setProp<std::int64_t>("foo1", 9);
    FileParserUtils::createAtomIntPropertyList(*m, "foo1");
    REQUIRE(m->hasProp("atom.iprop.foo1"));
    CHECK(m->getProp<std::string>("atom.iprop.foo1") == "1 n/a 9");

    m->getAtomWithIdx(0)->setProp<double>("foo2", 1);
    m->getAtomWithIdx(1)->setProp<double>("foo2", 4);
    m->getAtomWithIdx(2)->setProp<double>("foo2", 9);
    FileParserUtils::createAtomDoublePropertyList(*m, "foo2");
    REQUIRE(m->hasProp("atom.dprop.foo2"));
    CHECK(m->getProp<std::string>("atom.dprop.foo2") == "1 4 9");

    m->getAtomWithIdx(0)->setProp<std::string>("foo3", "1");
    m->getAtomWithIdx(1)->setProp<std::string>("foo3", "4");
    FileParserUtils::createAtomStringPropertyList(*m, "foo3", "?");
    REQUIRE(m->hasProp("atom.prop.foo3"));
    CHECK(m->getProp<std::string>("atom.prop.foo3") == "[?] 1 4 ?");

    m->getAtomWithIdx(0)->setProp<bool>("foo4", 1);
    m->getAtomWithIdx(1)->setProp<bool>("foo4", 0);
    m->getAtomWithIdx(2)->setProp<bool>("foo4", 0);
    FileParserUtils::createAtomBoolPropertyList(*m, "foo4");
    REQUIRE(m->hasProp("atom.bprop.foo4"));
    CHECK(m->getProp<std::string>("atom.bprop.foo4") == "1 0 0");
  }
  SECTION("long lines") {
    auto m = "COC"_smiles;
    REQUIRE(m);
    m->getAtomWithIdx(0)->setProp<std::string>("foo1", std::string(80, 'a'));
    m->getAtomWithIdx(1)->setProp<std::string>("foo1", std::string(80, 'b'));
    m->getAtomWithIdx(2)->setProp<std::string>("foo1", std::string(80, 'c'));
    FileParserUtils::createAtomStringPropertyList(*m, "foo1");
    REQUIRE(m->hasProp("atom.prop.foo1"));
    std::string ps = m->getProp<std::string>("atom.prop.foo1");
    CHECK(ps.length() > 240);
    CHECK(ps.find("\n") != std::string::npos);
    for (auto &atom : m->atoms()) {
      atom->clearProp("foo1");
    }
    FileParserUtils::applyMolListPropsToAtoms<std::string>(*m, "atom.prop.");
    CHECK(m->getAtomWithIdx(0)->getProp<std::string>("foo1") ==
          std::string(80, 'a'));
    CHECK(m->getAtomWithIdx(1)->getProp<std::string>("foo1") ==
          std::string(80, 'b'));
    CHECK(m->getAtomWithIdx(2)->getProp<std::string>("foo1") ==
          std::string(80, 'c'));
  }
}