forked from schrodinger/coordgenlibs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketcherMinimizerBond.cpp
216 lines (202 loc) · 6.15 KB
/
sketcherMinimizerBond.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
/*
Contributors: Nicola Zonta
Copyright Schrodinger, LLC. All rights reserved
*/
#include "sketcherMinimizerBond.h"
#include "sketcherMinimizerAtom.h"
#include "sketcherMinimizerMaths.h"
#include "sketcherMinimizerMolecule.h"
#include "sketcherMinimizerRing.h"
#include <algorithm>
using namespace std;
sketcherMinimizerAtom* sketcherMinimizerBond::startAtomCIPFirstNeighbor() const
{
if (bondOrder != 2) {
return nullptr;
}
sketcherMinimizerAtom* a = startAtom;
if (a->neighbors.size() == 2) {
if (a->neighbors[0] == endAtom) {
return a->neighbors[1];
} else {
return a->neighbors[0];
}
} else if (a->neighbors.size() == 3) {
std::vector<sketcherMinimizerAtom*> ats;
for (sketcherMinimizerAtom* n : a->neighbors) {
if (n != endAtom) {
ats.push_back(n);
}
}
if (ats.size() == 2) {
return sketcherMinimizerAtom::CIPPriority(ats[0], ats[1],
startAtom);
}
return nullptr;
} else {
return nullptr;
}
}
sketcherMinimizerAtom* sketcherMinimizerBond::endAtomCIPFirstNeighbor() const
{
if (bondOrder != 2) {
return nullptr;
}
sketcherMinimizerAtom* a = endAtom;
if (a->neighbors.size() == 2) {
if (a->neighbors[0] == startAtom) {
return a->neighbors[1];
} else {
return a->neighbors[0];
}
} else if (a->neighbors.size() == 3) {
std::vector<sketcherMinimizerAtom*> ats;
for (sketcherMinimizerAtom* n : a->neighbors) {
if (n != startAtom) {
ats.push_back(n);
}
}
if (ats.size() == 2) {
return sketcherMinimizerAtom::CIPPriority(ats[0], ats[1], endAtom);
}
return nullptr;
} else {
return nullptr;
}
}
void sketcherMinimizerBond::setAbsoluteStereoFromStereoInfo()
{
if (isStereo() && m_stereo.atom1 != nullptr && m_stereo.atom2 != nullptr) {
auto firstCIPNeighborStart = startAtomCIPFirstNeighbor();
auto firstCIPNeighborEnd = endAtomCIPFirstNeighbor();
if (firstCIPNeighborStart != nullptr && firstCIPNeighborEnd) {
bool invert = false;
if (m_stereo.atom1 != firstCIPNeighborStart &&
m_stereo.atom1 != firstCIPNeighborEnd) {
invert = !invert;
}
if (m_stereo.atom2 != firstCIPNeighborStart &&
m_stereo.atom2 != firstCIPNeighborEnd) {
invert = !invert;
}
bool settingIsZ =
(m_stereo.stereo == sketcherMinimizerBondStereoInfo::cis);
if (invert) {
settingIsZ = !settingIsZ;
}
isZ = settingIsZ;
}
}
if (m_stereo.stereo == sketcherMinimizerBondStereoInfo::unspecified) {
m_ignoreZE = true;
}
}
bool sketcherMinimizerBond::checkStereoChemistry() const
{
if (!isStereo()) {
return true;
}
if (isInSmallRing()) {
return true;
}
sketcherMinimizerAtom* firstCIPNeighborStart = startAtomCIPFirstNeighbor();
if (firstCIPNeighborStart == nullptr) {
return true;
}
sketcherMinimizerAtom* firstCIPNeighborEnd = endAtomCIPFirstNeighbor();
if (firstCIPNeighborEnd == nullptr) {
return true;
}
return (sketcherMinimizerMaths::sameSide(
firstCIPNeighborStart->getCoordinates(),
firstCIPNeighborEnd->getCoordinates(),
getStartAtom()->getCoordinates(),
getEndAtom()->getCoordinates()) == isZ);
}
bool sketcherMinimizerBond::isInSmallRing() const
{
for (auto ring : rings) {
if (!ring->isMacrocycle()) {
return true;
}
}
return false;
}
bool sketcherMinimizerBond::isInMacrocycle() const
{
for (auto ring : rings) {
if (ring->isMacrocycle()) {
return true;
}
}
return false;
}
bool sketcherMinimizerBond::isTerminal() const
{
return (getStartAtom()->getBonds().size() == 1 ||
getEndAtom()->getBonds().size() == 1);
}
bool sketcherMinimizerBond::isInterFragment() const
{
if (getStartAtom()->getBonds().size() == 1) {
return false;
}
if (getEndAtom()->getBonds().size() == 1) {
return false;
}
if (sketcherMinimizerAtom::shareARing(getStartAtom(), getEndAtom())) {
return false;
}
if (isStereo()) {
return false;
}
return true;
}
bool sketcherMinimizerBond::markedAsCis(sketcherMinimizerAtom* atom1,
sketcherMinimizerAtom* atom2) const
{
sketcherMinimizerAtom* firstCIPNeighbor1 = startAtomCIPFirstNeighbor();
sketcherMinimizerAtom* firstCIPNeighbor2 = endAtomCIPFirstNeighbor();
bool cis = isZ;
if (atom1 != firstCIPNeighbor1 && atom1 != firstCIPNeighbor2) {
cis = !cis;
}
if (atom2 != firstCIPNeighbor1 && atom2 != firstCIPNeighbor2) {
cis = !cis;
}
return cis;
}
bool sketcherMinimizerBond::isStereo() const
{
if (bondOrder != 2) {
return false;
}
if (m_ignoreZE) {
return false;
}
sketcherMinimizerRing* ring =
sketcherMinimizerAtom::shareARing(getStartAtom(), getEndAtom());
return !(ring && !ring->isMacrocycle());
}
void sketcherMinimizerBond::flip()
{
size_t totalAtomsNumber = getStartAtom()->getMolecule()->getAtoms().size();
vector<sketcherMinimizerAtom*> atoms =
getStartAtom()->getSubmolecule(getEndAtom());
if (atoms.size() > totalAtomsNumber / 2) {
atoms = getEndAtom()->getSubmolecule(getStartAtom());
}
vector<sketcherMinimizerBond*> allBonds =
getStartAtom()->getMolecule()->getBonds();
for (sketcherMinimizerAtom* atom : atoms) {
sketcherMinimizerAtom::mirrorCoordinates(atom, this);
}
for (sketcherMinimizerBond* bond : allBonds) {
if (find(atoms.begin(), atoms.end(), bond->getStartAtom()) !=
atoms.end() &&
find(atoms.begin(), atoms.end(), bond->getEndAtom()) !=
atoms.end()) {
bond->isWedge = !bond->isWedge;
}
}
}