-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathbond.ts
339 lines (293 loc) · 9.38 KB
/
bond.ts
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
/****************************************************************************
* Copyright 2021 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
import {
Atom,
Bond,
StereoLabel,
Vec2,
Neighbor,
StereoValidator
} from 'ketcher-core'
import {
AtomAdd,
AtomAttr,
BondAdd,
BondAttr,
BondDelete,
CalcImplicitH,
FragmentAdd
} from '../operations'
import { atomForNewBond, atomGetAttr } from './utils'
import {
fromAtomMerge,
fromStereoAtomAttrs,
mergeFragmentsIfNeeded,
mergeSgroups
} from './atom'
import Action from '../shared/action'
import ReStruct from '../../render/restruct'
import utils from '../shared/utils'
export function fromBondAddition(
restruct: ReStruct,
bond: Bond,
begin: any,
end: any,
pos?: Vec2,
pos2?: Vec2
): [Action, number, number, number] {
// eslint-disable-line
if (end === undefined) {
const atom = atomForNewBond(restruct, begin)
end = atom.atom
pos = atom.pos
}
const action = new Action()
const struct = restruct.molecule
let mergeFragments = false
let frid = null
if (!(typeof begin === 'number')) {
if (typeof end === 'number') frid = atomGetAttr(restruct, end, 'fragment')
} else {
frid = atomGetAttr(restruct, begin, 'fragment')
if (typeof end === 'number') mergeFragments = true
}
if (frid == null)
frid = (action.addOp(new FragmentAdd().perform(restruct)) as FragmentAdd)
.frid
if (!(typeof begin === 'number')) {
begin.fragment = frid
begin = (action.addOp(new AtomAdd(begin, pos).perform(restruct)) as AtomAdd)
.data.aid
if (typeof end === 'number') mergeSgroups(action, restruct, [begin], end)
pos = pos2
} else if (atomGetAttr(restruct, begin, 'label') === '*') {
action.addOp(new AtomAttr(begin, 'label', 'C').perform(restruct))
}
if (!(typeof end === 'number')) {
end.fragment = frid
// TODO: <op>.data.aid here is a hack, need a better way to access the id of a created atom
end = (action.addOp(new AtomAdd(end, pos).perform(restruct)) as AtomAdd)
.data.aid
if (typeof begin === 'number') mergeSgroups(action, restruct, [end], begin)
} else if (atomGetAttr(restruct, end, 'label') === '*') {
action.addOp(new AtomAttr(end, 'label', 'C').perform(restruct))
}
const bid = (action.addOp(
new BondAdd(begin, end, bond).perform(restruct)
) as BondAdd).data.bid
const bnd = struct.bonds.get(bid)
if (bnd) {
action.addOp(new CalcImplicitH([bnd.begin, bnd.end]).perform(restruct))
action.mergeWith(fromBondStereoUpdate(restruct, bnd))
}
action.operations.reverse()
if (mergeFragments) mergeFragmentsIfNeeded(action, restruct, begin, end)
return [action, begin, end, bid]
}
export function fromBondsAttrs(
restruct: ReStruct,
ids: Array<number> | number,
attrs: Bond,
reset?: boolean
): Action {
const struct = restruct.molecule
const action = new Action()
const bids = Array.isArray(ids) ? ids : [ids]
bids.forEach(bid => {
Object.keys(Bond.attrlist).forEach(key => {
if (!(key in attrs) && !reset) return
const value = key in attrs ? attrs[key] : Bond.attrGetDefault(key)
action.addOp(new BondAttr(bid, key, value).perform(restruct))
if (key === 'stereo' && key in attrs) {
const bond = struct.bonds.get(bid)
if (bond) {
action.addOp(
new CalcImplicitH([bond.begin, bond.end]).perform(restruct)
)
action.mergeWith(fromBondStereoUpdate(restruct, bond))
}
}
})
})
return action
}
export function fromBondsMerge(
restruct: ReStruct,
mergeMap: Map<number, number>
): Action {
const struct = restruct.molecule
const atomPairs = new Map()
let action = new Action()
mergeMap.forEach((dstId, srcId) => {
const bond = struct.bonds.get(srcId)
const bondCI = struct.bonds.get(dstId)
if (!bond || !bondCI) return
const params = utils.mergeBondsParams(struct, bond, struct, bondCI)
if (!params.merged) return
atomPairs.set(bond.begin, !params.cross ? bondCI.begin : bondCI.end)
atomPairs.set(bond.end, !params.cross ? bondCI.end : bondCI.begin)
})
atomPairs.forEach((dst, src) => {
action = fromAtomMerge(restruct, src, dst).mergeWith(action)
})
return action
}
function fromBondFlipping(restruct: ReStruct, id: number): Action {
const bond = restruct.molecule.bonds.get(id)
const action = new Action()
action.addOp(new BondDelete(id).perform(restruct))
// TODO: find better way to avoid problem with bond.begin = 0
if (Number.isInteger(bond?.end) && Number.isInteger(bond?.begin)) {
action.addOp(new BondAdd(bond?.end, bond?.begin, bond).perform(restruct))
}
// todo: swap atoms stereoLabels and stereoAtoms in fragment
return action
}
export function fromBondStereoUpdate(
restruct: ReStruct,
bond: Bond,
withReverse?: boolean
): Action {
const action = new Action()
const struct = restruct.molecule
const beginFrId = struct.atoms.get(bond.begin)?.fragment
const endFrId = struct.atoms.get(bond.end)?.fragment
const fragmentStereoBonds: Array<Bond> = []
const stereoAtomsMap = new Map()
struct.bonds.forEach(bond => {
if (bond.stereo > 0) {
if (struct.atoms.get(bond.begin)?.fragment === beginFrId) {
fragmentStereoBonds.push(bond)
}
if (
beginFrId !== endFrId &&
struct.atoms.get(bond.begin)?.fragment === endFrId
) {
fragmentStereoBonds.push(bond)
}
}
})
const correctAtomIds: Array<number> = []
fragmentStereoBonds.forEach((bond: Bond | undefined) => {
if (bond) {
const beginNeighs: Array<Neighbor> | undefined = struct.atomGetNeighbors(
bond.begin
)
const endNeighs: Array<Neighbor> | undefined = struct.atomGetNeighbors(
bond.end
)
if (
StereoValidator.isCorrectStereoCenter(
bond,
beginNeighs,
endNeighs,
struct
)
) {
const stereoLabel = struct.atoms.get(bond.begin)?.stereoLabel
if (
stereoLabel == null ||
stereoAtomsMap.get(bond.begin)?.stereoLabel == null
) {
stereoAtomsMap.set(bond.begin, {
stereoParity: getStereoParity(bond.stereo),
stereoLabel: stereoLabel || `${StereoLabel.Abs}`
})
}
correctAtomIds.push(bond.begin)
} else {
if (!correctAtomIds.includes(bond.begin)) {
stereoAtomsMap.set(bond.begin, {
stereoParity: Atom.PATTERN.STEREO_PARITY.NONE,
stereoLabel: null
})
}
}
}
})
// in case the stereo band is flipped, changed or removed
// TODO the duplication of the code below should be fixed, mayby by function
if (!correctAtomIds.includes(bond.begin)) {
stereoAtomsMap.set(bond.begin, {
stereoParity: Atom.PATTERN.STEREO_PARITY.NONE,
stereoLabel: null
})
}
if (!correctAtomIds.includes(bond.end)) {
stereoAtomsMap.set(bond.end, {
stereoParity: Atom.PATTERN.STEREO_PARITY.NONE,
stereoLabel: null
})
}
stereoAtomsMap.forEach((stereoProp, aId) => {
if (struct.atoms.get(aId)?.stereoLabel !== stereoProp.stereoLabel) {
action.mergeWith(
fromStereoAtomAttrs(restruct, aId, stereoProp, withReverse)
)
}
})
return action
}
function getStereoParity(stereo: Number): number | null {
let newAtomParity: number | null = null
switch (stereo) {
case Bond.PATTERN.STEREO.UP:
newAtomParity = Atom.PATTERN.STEREO_PARITY.ODD
break
case Bond.PATTERN.STEREO.EITHER:
newAtomParity = Atom.PATTERN.STEREO_PARITY.EITHER
break
case Bond.PATTERN.STEREO.DOWN:
newAtomParity = Atom.PATTERN.STEREO_PARITY.EVEN
break
}
return newAtomParity
}
export function bondChangingAction(
restruct: ReStruct,
itemID: number,
bond: Bond,
bondProps: any
): Action {
const action = new Action()
let newItemId
if (
((bondProps.stereo !== Bond.PATTERN.STEREO.NONE && //
bondProps.type === Bond.PATTERN.TYPE.SINGLE) ||
bond.type === Bond.PATTERN.TYPE.DATIVE) &&
bond.type === bondProps.type &&
bond.stereo === bondProps.stereo
) {
action.mergeWith(fromBondFlipping(restruct, itemID))
newItemId = (action.operations[1] as BondAdd).data.bid
}
// if bondTool is stereo and equal to bond for change
const loop = plainBondTypes.includes(bondProps.type) ? plainBondTypes : null
if (
bondProps.stereo === Bond.PATTERN.STEREO.NONE &&
bondProps.type === Bond.PATTERN.TYPE.SINGLE &&
bond.stereo === Bond.PATTERN.STEREO.NONE &&
loop
)
// if `Single bond` tool is chosen and bond for change in `plainBondTypes`
bondProps.type = loop[(loop.indexOf(bond.type) + 1) % loop.length]
return fromBondsAttrs(restruct, newItemId, bondProps).mergeWith(action)
}
const plainBondTypes = [
Bond.PATTERN.TYPE.SINGLE,
Bond.PATTERN.TYPE.DOUBLE,
Bond.PATTERN.TYPE.TRIPLE
]