/
bond.js
213 lines (170 loc) · 6.65 KB
/
bond.js
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
/****************************************************************************
* Copyright 2018 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 Struct from '../../chem/struct';
import op from '../shared/op';
import utils from '../shared/utils';
import Action from '../shared/action';
import { atomGetAttr, atomForNewBond, atomGetDegree, atomGetSGroups } from './utils';
import { fromAtomMerge, mergeFragments } from './atom';
import { removeSgroupIfNeeded, removeAtomFromSgroupIfNeeded } from './sgroup';
import { fromFragmentSplit } from './fragment';
export function fromBondAddition(restruct, bond, begin, end, pos, pos2) { // eslint-disable-line
if (end === undefined) {
var atom = atomForNewBond(restruct, begin);
end = atom.atom;
pos = atom.pos;
}
var action = new Action();
var 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') {
var frid2 = atomGetAttr(restruct, end, 'fragment');
mergeFragments(action, restruct, frid, frid2);
}
}
if (frid == null)
frid = action.addOp(new op.FragmentAdd().perform(restruct)).frid;
if (!(typeof begin === 'number')) {
begin.fragment = frid;
begin = action.addOp(new op.AtomAdd(begin, pos).perform(restruct)).data.aid;
pos = pos2;
} else if (atomGetAttr(restruct, begin, 'label') === '*') {
action.addOp(new op.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 op.AtomAdd(end, pos).perform(restruct)).data.aid;
if (typeof begin === 'number') {
atomGetSGroups(restruct, begin).forEach((sid) => {
action.addOp(new op.SGroupAtomAdd(sid, end).perform(restruct));
});
}
} else if (atomGetAttr(restruct, end, 'label') === '*') {
action.addOp(new op.AtomAttr(end, 'label', 'C').perform(restruct));
}
var bid = action.addOp(new op.BondAdd(begin, end, bond).perform(restruct)).data.bid;
action.operations.reverse();
return [action, begin, end, bid];
}
export function fromBondDeletion(restruct, id) {
var action = new Action();
var bond = restruct.molecule.bonds.get(id);
var frid = restruct.molecule.atoms.get(bond.begin).fragment;
var atomsToRemove = [];
action.addOp(new op.BondDelete(id));
if (atomGetDegree(restruct, bond.begin) === 1) {
if (removeAtomFromSgroupIfNeeded(action, restruct, bond.begin))
atomsToRemove.push(bond.begin);
action.addOp(new op.AtomDelete(bond.begin));
}
if (atomGetDegree(restruct, bond.end) === 1) {
if (removeAtomFromSgroupIfNeeded(action, restruct, bond.end))
atomsToRemove.push(bond.end);
action.addOp(new op.AtomDelete(bond.end));
}
removeSgroupIfNeeded(action, restruct, atomsToRemove);
action = action.perform(restruct);
action.mergeWith(fromFragmentSplit(restruct, frid));
return action;
}
/**
* @param restruct { ReStruct }
* @param ids { Array<number>|number }
* @param attrs { Bond }
* @param reset? { boolean }
*/
export function fromBondsAttrs(restruct, ids, attrs, reset) {
const action = new Action();
const bids = Array.isArray(ids) ? ids : [ids];
bids.forEach((bid) => {
Object.keys(Struct.Bond.attrlist).forEach((key) => {
if (!(key in attrs) && !reset)
return;
const value = (key in attrs) ? attrs[key] : Struct.Bond.attrGetDefault(key);
action.addOp(new op.BondAttr(bid, key, value));
});
});
return action.perform(restruct);
}
/**
* @param restruct { ReStruct }
* @param mergeMap { Map<number, number> }
*/
export function fromBondsMerge(restruct, mergeMap) {
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);
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, id) {
const bond = restruct.molecule.bonds.get(id);
const action = new Action();
action.addOp(new op.BondDelete(id));
action.addOp(new op.BondAdd(bond.end, bond.begin, bond)).data.bid = id;
return action.perform(restruct);
}
/**
* @param restruct { ReStruct }
* @param itemID - bond id in structure
* @param bond - bond for change
* @param bondProps - bondTool properties
* @returns Action
*/
export function bondChangingAction(restruct, itemID, bond, bondProps) {
if (bondProps.stereo !== Struct.Bond.PATTERN.STEREO.NONE && //
bondProps.type === Struct.Bond.PATTERN.TYPE.SINGLE &&
bond.type === bondProps.type && bond.stereo === bondProps.stereo)
// if bondTool is stereo and equal to bond for change
return fromBondFlipping(restruct, itemID);
const loop = plainBondTypes.includes(bondProps.type) ? plainBondTypes : null;
if (bondProps.stereo === Struct.Bond.PATTERN.STEREO.NONE &&
bondProps.type === Struct.Bond.PATTERN.TYPE.SINGLE &&
bond.stereo === Struct.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];
const isFlip = bondFlipRequired(restruct.molecule, bond, bondProps);
const action = isFlip ? fromBondFlipping(restruct, itemID) : new Action();
return fromBondsAttrs(restruct, itemID, bondProps).mergeWith(action);
}
function bondFlipRequired(struct, bond, attrs) {
return attrs.type === Struct.Bond.PATTERN.TYPE.SINGLE &&
bond.stereo === Struct.Bond.PATTERN.STEREO.NONE &&
attrs.stereo !== Struct.Bond.PATTERN.STEREO.NONE &&
struct.atoms.get(bond.begin).neighbors.length <
struct.atoms.get(bond.end).neighbors.length;
}
const plainBondTypes = [
Struct.Bond.PATTERN.TYPE.SINGLE,
Struct.Bond.PATTERN.TYPE.DOUBLE,
Struct.Bond.PATTERN.TYPE.TRIPLE
];