-
Notifications
You must be signed in to change notification settings - Fork 2
/
combineSmiles.js
168 lines (156 loc) · 5.15 KB
/
combineSmiles.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
const MAX_R = 10;
/**
* Generate molecules and calculate predicted properties form a list of smiles and fragments
* @param {string} [coreSmiles]
* @param {array} [fragments] Array of {smiles,R1,R2,...}
* @param {import('openchemlib')} OCL - openchemlib library
* @param {object} [options={}]
* @param {function} [options.onStep] method to execute each new molecules
* @param {boolean} [options.complexity] returns only the number of molecules to evaluate
* @return {Promise} promise that resolves to molecules or complexity as a number
*/
export async function combineSmiles(coreSmiles, fragments, OCL, options = {}) {
const { complexity = false } = options;
const core = getCore(coreSmiles);
const rGroups = getRGroups(core, fragments);
if (complexity) {
return getComplexity(rGroups);
}
return generate(core, rGroups, OCL, options);
}
function getComplexity(rGroups) {
let complexity = 1;
for (const rGroup of rGroups) {
complexity *= rGroup.smiles.length;
}
return complexity;
}
async function generate(core, rGroups, OCL, options = {}) {
const { onStep } = options;
const molecules = {};
const sizes = new Array(rGroups.length);
const currents = new Array(rGroups.length);
for (let i = 0; i < rGroups.length; i++) {
sizes[i] = rGroups[i].smiles.length - 1;
currents[i] = 0;
}
let position = 0;
let counter = 0;
while (true) {
counter++;
while (position < currents.length) {
if (currents[position] < sizes[position]) {
if (onStep) {
// eslint-disable-next-line no-await-in-loop
await onStep(counter);
}
appendMolecule(molecules, core, rGroups, currents, OCL);
currents[position]++;
for (let i = 0; i < position; i++) {
currents[i] = 0;
}
position = 0;
} else {
position++;
}
}
if ((position = currents.length)) {
if (onStep) {
// eslint-disable-next-line no-await-in-loop
await onStep(counter);
}
appendMolecule(molecules, core, rGroups, currents, OCL);
break;
}
}
return Object.keys(molecules)
.map((key) => molecules[key])
.sort((m1, m2) => m1.mw - m2.mw);
}
function appendMolecule(molecules, core, rGroups, currents, OCL) {
let newSmiles = core.smiles;
for (let i = 0; i < currents.length; i++) {
newSmiles += `.${rGroups[i].smiles[currents[i]]}`;
}
const currentMol = OCL.Molecule.fromSmiles(newSmiles);
const idCode = currentMol.getIDCode();
if (!molecules[idCode]) {
const molecule = {};
molecules[idCode] = molecule;
molecule.smiles = currentMol.toSmiles();
molecule.combinedSmiles = newSmiles;
molecule.idCode = idCode;
molecule.molfile = currentMol.toMolfile();
const props = new OCL.MoleculeProperties(currentMol);
molecule.nbHAcceptor = props.acceptorCount;
molecule.nbHDonor = props.donorCount;
molecule.logP = props.logP;
molecule.logS = props.logS;
molecule.PSA = props.polarSurfaceArea;
molecule.nbRottable = props.rotatableBondCount;
molecule.nbStereoCenter = props.stereoCenterCount;
const mf = currentMol.getMolecularFormula();
molecule.mf = mf.formula;
molecule.mw = mf.relativeWeight;
}
}
function getCore(coreSmiles) {
const core = {
originalSmiles: coreSmiles,
smiles: coreSmiles.replace(/\[R(?<group>[1-4])\]/g, '%5$<group>'),
};
for (let i = 0; i < MAX_R; i++) {
if (core.originalSmiles.indexOf(`[R${i}]`) > -1) core[`R${i}`] = true;
}
return core;
}
function getRGroups(core, fragments) {
const rGroups = {};
for (const fragment of fragments) {
if (fragment.smiles) {
const smiles = updateRPosition(fragment.smiles);
for (let i = 0; i < MAX_R; i++) {
if (core[`R${i}`]) {
// we only consider the R that are in the core
if (fragment[`R${i}`]) {
if (!rGroups[`R${i}`]) {
rGroups[`R${i}`] = {
group: `R${i}`,
smiles: [],
};
}
rGroups[`R${i}`].smiles.push(smiles.replace(/\[R\]/, `(%5${i})`));
}
}
}
}
}
return Object.keys(rGroups).map((key) => rGroups[key]);
}
function updateRPosition(smiles) {
// R group should not be at the beginning
if (smiles.indexOf('[R]') !== 0) return smiles;
if (smiles.length === 3) return '[H][R]';
// we are in trouble ... we need to move the R
const newSmiles = smiles.replace('[R]', '');
// we need to check where we should put the R group
let level = 0;
for (let j = 0; j < newSmiles.length; j++) {
const currentChar = newSmiles.charAt(j);
const currentSubstring = newSmiles.substr(j);
if (currentChar === '(') {
level++;
} else if (currentChar === ')') {
level--;
} else if (level === 0) {
if (currentSubstring.match(/^[a-z]/)) {
return `${newSmiles.substr(0, j + 1)}([R])${newSmiles.substr(j + 1)}`;
} else if (currentSubstring.match(/^[A-Z][a-z]/)) {
return `${newSmiles.substr(0, j + 2)}([R])${newSmiles.substr(j + 2)}`;
} else if (currentSubstring.match(/^[A-Z]/)) {
return `${newSmiles.substr(0, j + 1)}([R])${newSmiles.substr(j + 1)}`;
}
}
}
return smiles;
}