-
Notifications
You must be signed in to change notification settings - Fork 2
/
fragmentAcyclicSingleBonds.js
69 lines (64 loc) · 2.1 KB
/
fragmentAcyclicSingleBonds.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
import { getMF } from '../util/getMF';
export function fragmentAcyclicSingleBonds(molecule) {
const OCL = molecule.getOCL();
const atoms = [];
for (let i = 0; i < molecule.getAllAtoms(); i++) {
const atom = {};
atoms.push(atom);
atom.i = i;
atom.links = []; // we will store connected atoms of broken bonds
}
const bonds = [];
for (let i = 0; i < molecule.getAllBonds(); i++) {
const bond = {};
bonds.push(bond);
bond.i = i;
bond.order = molecule.getBondOrder(i);
bond.atom1 = molecule.getBondAtom(0, i);
bond.atom2 = molecule.getBondAtom(1, i);
bond.type = molecule.getBondType(i);
bond.isAromatic = molecule.isAromaticBond(i);
bond.isRingBond = molecule.isRingBond(i);
if (!bond.isAromatic && (bond.type & 0b11) === 1 && !bond.isRingBond) {
bond.selected = true;
atoms[bond.atom1].links.push(bond.atom2);
atoms[bond.atom2].links.push(bond.atom1);
}
}
const brokenMolecule = molecule.getCompactCopy();
for (const bond of bonds) {
if (bond.selected) {
brokenMolecule.markBondForDeletion(bond.i);
}
}
brokenMolecule.deleteMarkedAtomsAndBonds();
const fragmentMap = [];
const nbFragments = brokenMolecule.getFragmentNumbers(fragmentMap);
const results = [];
for (let i = 0; i < nbFragments; i++) {
const result = {};
result.atomMap = [];
const includeAtom = fragmentMap.map((id) => {
return id === i;
});
const fragment = new OCL.Molecule(0, 0);
const atomMap = [];
brokenMolecule.copyMoleculeByAtoms(fragment, includeAtom, false, atomMap);
// we will add some R groups at the level of the broken bonds
for (let j = 0; j < atomMap.length; j++) {
if (atomMap[j] > -1) {
result.atomMap.push(j);
if (atoms[j].links.length > 0) {
atoms[j].links.forEach(() => {
fragment.addBond(atomMap[j], fragment.addAtom(154), 1);
});
}
}
}
fragment.setFragment(false);
result.idCode = fragment.getIDCode();
result.mf = getMF(fragment).mf.replace(/R[1-9]?/, '');
results.push(result);
}
return results;
}