/
squareplanar.cpp
283 lines (236 loc) · 7.47 KB
/
squareplanar.cpp
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
#include <openbabel/stereo/squareplanar.h>
#include <openbabel/mol.h>
#include <openbabel/oberror.h>
#include <algorithm> // std::rotate
#include <cassert>
namespace OpenBabel {
//
// OBSquarePlanarStereo::Config struct
//
bool OBSquarePlanarStereo::Config::operator==(const Config &other) const
{
if (center != other.center)
return false;
if ((refs.size() != 4) || (other.refs.size() != 4))
return false;
Config u1, u2;
if (!OBStereo::ContainsSameRefs(refs, other.refs)) {
// find a ref that occurs in both
for (OBStereo::ConstRefIter i = refs.begin(); i != refs.end(); ++i)
if (OBStereo::ContainsRef(other.refs, *i)) {
u1 = OBTetraPlanarStereo::ToConfig(*this, *i, OBStereo::ShapeU); // refs[0] = u1.refs[0]
u2 = OBTetraPlanarStereo::ToConfig(other, *i, OBStereo::ShapeU); // refs[0] = u2.refs[0]
}
// check if they actualy share an id...
if (u1.refs.empty())
return false;
} else {
// normalize the other Config struct
u1 = OBTetraPlanarStereo::ToConfig(*this, refs.at(0), OBStereo::ShapeU); // refs[0] = u1.refs[0]
u2 = OBTetraPlanarStereo::ToConfig(other, refs.at(0), OBStereo::ShapeU); // refs[0] = u2.refs[0]
// both now start with the same ref
//
// 2 possiblilities:
//
// 1 2 3 4 1 2 3 4
// | | | | <- in any case, refs[0] & refs[2] remain unchanged
// 1 2 3 4 1 4 3 2
//
return (u1.refs[2] == u2.refs[2]);
}
// possibilities:
//
// 1 2 3 4
// | | <- refs[0] & refs[2] remain unchanged
// 1 H 3 H
//
// 1 2 3 4
// | | <- refs[0] & refs[3] remain unchanged
// 1 H H 4
//
// 1 2 3 4
// | | <- refs[0] & refs[1] remain unchanged
// 1 2 H H
if ((u1.refs[2] == OBStereo::ImplicitRef) || (u2.refs[2] == OBStereo::ImplicitRef)) {
// 1 2 H 4
if ((u1.refs[3] == OBStereo::ImplicitRef) || (u2.refs[3] == OBStereo::ImplicitRef)) {
return (u1.refs[1] == u2.refs[1]); // 1 2 H H
} else {
return (u1.refs[3] == u2.refs[3]); // 1 H H 4
}
} else
return (u1.refs[2] == u2.refs[2]); // 1 2 3 4 & 1 H 3 4 & 1 2 3 H
return false;
}
//
// OBSquarePlanar class
//
OBSquarePlanarStereo::OBSquarePlanarStereo(OBMol *mol) : OBTetraPlanarStereo(mol)
{
}
OBSquarePlanarStereo::~OBSquarePlanarStereo()
{
}
bool OBSquarePlanarStereo::IsValid() const
{
if (m_cfg.center == OBStereo::NoRef)
return false;
if (m_cfg.refs.size() != 4)
return false;
return true;
}
void OBSquarePlanarStereo::SetConfig(const Config &config)
{
if (config.center == OBStereo::NoRef) {
obErrorLog.ThrowError(__FUNCTION__,
"OBSquarePlanarStereo::SetConfig : center id is invalid.", obError);
m_cfg = Config();
return;
}
if (config.refs.size() != 4) {
std::stringstream ss;
ss << "OBSquarePlanarStereo::SetConfig : found " << config.refs.size();
ss << " reference ids, should be 4.";
obErrorLog.ThrowError(__FUNCTION__, ss.str(), obError);
m_cfg = Config();
return;
}
// store using U shape
m_cfg = OBTetraPlanarStereo::ToConfig(config, config.refs.at(0), OBStereo::ShapeU);
}
OBSquarePlanarStereo::Config OBSquarePlanarStereo::GetConfig(OBStereo::Shape shape) const
{
if (!IsValid())
return Config();
return OBTetraPlanarStereo::ToConfig(m_cfg, m_cfg.refs.at(0), shape);
}
OBSquarePlanarStereo::Config OBSquarePlanarStereo::GetConfig(unsigned long start,
OBStereo::Shape shape) const
{
if (!IsValid())
return Config();
return OBTetraPlanarStereo::ToConfig(m_cfg, start, shape);
}
bool OBSquarePlanarStereo::operator==(const OBSquarePlanarStereo &other) const
{
if (!IsValid() || !other.IsValid())
return false;
Config u = OBTetraPlanarStereo::ToConfig(other.GetConfig(),
m_cfg.refs.at(0), OBStereo::ShapeU);
unsigned long a1 = u.refs.at(0);
unsigned long b1 = u.refs.at(2);
if ((a1 == OBStereo::ImplicitRef) && (b1 == OBStereo::ImplicitRef)) {
a1 = u.refs.at(1);
b1 = u.refs.at(3);
}
if (b1 != OBStereo::ImplicitRef)
if (a1 == GetTransRef(b1))
return true;
if (a1 != OBStereo::ImplicitRef)
if (b1 == GetTransRef(a1))
return true;
return false;
}
bool OBSquarePlanarStereo::IsTrans(unsigned long id1, unsigned long id2) const
{
return (GetTransRef(id1) == id2);
}
bool OBSquarePlanarStereo::IsCis(unsigned long id1, unsigned long id2) const
{
if (m_cfg.refs.size() != 4)
return false;
std::vector<unsigned long> cis = GetCisRefs(id1);
if (cis.size() != 2)
return false;
if ((cis.at(0) == id2) || (cis.at(1) == id2))
return true;
return false;
}
unsigned long OBSquarePlanarStereo::GetTransRef(unsigned long id) const
{
if (m_cfg.refs.size() != 4)
return false;
// find id1
for (int i = 0; i < 4; ++i) {
if (m_cfg.refs.at(i) == id) {
// use it's index to compare id2 with the opposite reference id
int j = (i > 1) ? i - 2 : i + 2;
return m_cfg.refs.at(j);
}
}
// id not found
return OBStereo::NoRef;
}
std::vector<unsigned long> OBSquarePlanarStereo::GetCisRefs(unsigned long id) const
{
std::vector<unsigned long> refs;
if (m_cfg.refs.size() != 4)
return refs;
// find id
for (int i = 0; i < 4; ++i) {
if (m_cfg.refs.at(i) == id) {
// use it's index to get the left/right reference ids
int j = (i > 0) ? i - 1 : 3;
int k = (i < 3) ? i + 1 : 0;
refs.push_back(m_cfg.refs.at(j));
refs.push_back(m_cfg.refs.at(k));
return refs;
}
}
// id not found
return refs;
}
OBGenericData* OBSquarePlanarStereo::Clone(OBBase *mol) const
{
OBSquarePlanarStereo *data = new OBSquarePlanarStereo(static_cast<OBMol*>(mol));
data->SetConfig(m_cfg);
return data;
}
} // namespace OpenBabel
namespace std {
ostream& operator<<(ostream &out, const OpenBabel::OBSquarePlanarStereo &ct)
{
OpenBabel::OBSquarePlanarStereo::Config cfg = ct.GetConfig();
out << "OBSquarePlanarStereo(center = " << cfg.center;
out << ", refs = ";
for (OpenBabel::OBStereo::Refs::iterator i = cfg.refs.begin(); i != cfg.refs.end(); ++i)
if (*i != OpenBabel::OBStereo::ImplicitRef)
out << *i << " ";
else
out << "H ";
switch (cfg.shape) {
case OpenBabel::OBStereo::ShapeU:
out << ", shape = U)";
break;
case OpenBabel::OBStereo::ShapeZ:
out << ", shape = Z)";
break;
case OpenBabel::OBStereo::Shape4:
out << ", shape = 4)";
break;
}
return out;
}
ostream& operator<<(ostream &out, const OpenBabel::OBSquarePlanarStereo::Config &cfg)
{
out << "OBSquarePlanarStereo::Config(center = " << cfg.center;
out << ", refs = ";
for (OpenBabel::OBStereo::Refs::const_iterator i = cfg.refs.begin(); i != cfg.refs.end(); ++i)
if (*i != OpenBabel::OBStereo::ImplicitRef)
out << *i << " ";
else
out << "H ";
switch (cfg.shape) {
case OpenBabel::OBStereo::ShapeU:
out << ", shape = U)";
break;
case OpenBabel::OBStereo::ShapeZ:
out << ", shape = Z)";
break;
case OpenBabel::OBStereo::Shape4:
out << ", shape = 4)";
break;
}
return out;
}
} // namespace std