-
Notifications
You must be signed in to change notification settings - Fork 2
/
Reactions.js
171 lines (158 loc) · 5.44 KB
/
Reactions.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
import { appendOCLReaction } from './utils/appendOCLReaction.js';
import { applyOneReactantReactions } from './utils/applyOneReactantReactions.js';
import { checkIfExistsOrAddInfo } from './utils/checkIfExistsOrAddInfo.js';
import { getFilteredTrees } from './utils/getFilteredTrees.js';
import { getLeaves } from './utils/getLeaves.js';
import { getNodes } from './utils/getNodes.js';
export class Reactions {
/**
*
* @param {object} [options={}]
* @param {import('cheminfo-types').Logger} logger
* @param {number} [options.maxDepth=5]
* @param {function} [options.moleculeInfoCallback]
* @param {boolean} [options.skipProcessed=true]
*/
constructor(OCL, options = {}) {
this.moleculeInfoCallback = options.moleculeInfoCallback;
this.maxDepth = options.maxDepth ?? 5;
this.limitReactions = options.limitReactions ?? 200;
this.skipProcessed = options.skipProcessed ?? true;
this.logger = options.logger;
this.processedMolecules = new Map();
this.OCL = OCL;
this.trees = [];
this.moleculeInfo = {}; // a cache containing molecule information like mw, etc.
}
/**
* We need to call this method for all the reactants on which we want to apply the reactions.
* If there is only one reactant, we call this method with an array of one reactant.
* If there are multiple reactants, we call this method with an array of the reactants.
* This method has to be called for all the reactants
* @param {import('openchemlib').Molecule[]|string[]} molecules
*/
appendHead(moleculesOrIDCodes) {
if (!Array.isArray(moleculesOrIDCodes)) {
throw new TypeError('reactants must be an array');
}
const molecules = moleculesOrIDCodes.map(
(molecule) =>
checkIfExistsOrAddInfo(this.processedMolecules, molecule, {
moleculeInfoCallback: this.moleculeInfoCallback,
}).info,
);
const tree = {
molecules,
depth: 0,
isValid: true, // this node could be implied in reactions
};
this.trees.push(tree);
}
/**
* Returns all the leaves of the trees
* @returns
*/
getLeaves() {
return getLeaves(this.trees);
}
/**
* Returns all the nodes of the trees
* @returns
*/
getNodes() {
return getNodes(this.trees);
}
getParentMap() {
const parentMap = new Map();
const nodes = this.getNodes();
for (const node of nodes) {
if (node.children) {
for (const child of node.children) {
parentMap.set(child, node);
}
}
}
return parentMap;
}
/**
* When applying reactions some branches may be dead because it can not be implied in any reaction.
* This is the case when we specify a 'min' reaction depth.
* This will returno only the valid nodes
* @returns
*/
getValidNodes() {
return this.getNodes().filter((node) => node.isValid);
}
/**
*
* @param {object} [options={}]
* @param {(object):boolean} [options.filter] - a function that will be called for each node and return true if the node should be kept
*/
getFilteredReactions(options = {}) {
const filteredReactions = new Reactions();
filteredReactions.moleculeInfoCallback = this.moleculeInfoCallback;
filteredReactions.maxDepth = this.maxDepth;
filteredReactions.limitReactions = this.limitReactions;
filteredReactions.skipProcessed = this.skipProcessed;
filteredReactions.logger = this.logger;
filteredReactions.processedMolecules = this.processedMolecules;
filteredReactions.OCL = this.OCL;
filteredReactions.moleculeInfo = this.moleculeInfo; // a cache containing molecule information like mw, etc.
filteredReactions.trees = getFilteredTrees(this, options);
return filteredReactions;
}
/**
*
* @param {object[]} reactions - array of reactions that should be applied
* @param {object} [options={}]
* @param {number} [options.min=0] min depth of the reaction
* @param {number} [options.max=3] max depth of the reaction
*/
applyOneReactantReactions(reactions, options = {}) {
const { min = 0, max = 3 } = options;
clearAsFromProcessedMolecules(this.processedMolecules);
const nodes = this.getNodes().filter((node) => node.isValid);
nodes.forEach((node) => {
node.currentDepth = 0;
});
reactions = appendOCLReaction(reactions, this.OCL);
const stats = { counter: 0 };
// Start the recursion by applying the first level of reactions
for (const node of nodes) {
let todoCurrentLevel = applyOneReactantReactions(node, reactions, {
OCL: this.OCL,
currentDepth: 1,
processedMolecules: this.processedMolecules,
moleculeInfoCallback: this.moleculeInfoCallback,
maxDepth: this.maxDepth,
maxCurrentDepth: max,
stats,
limitReactions: this.limitReactions,
});
do {
const nexts = [];
for (const todo of todoCurrentLevel) {
nexts.push(todo());
}
todoCurrentLevel = nexts.flat();
} while (todoCurrentLevel.length > 0);
}
const newNodes = this.getNodes().filter((node) => node.isValid);
for (const node of newNodes) {
if (node.currentDepth < min || node.currentDepth > max) {
node.isValid = false;
}
delete node.currentDepth;
}
}
}
function clearAsFromProcessedMolecules(processedMolecules) {
for (const [, value] of processedMolecules) {
if (value.asReagent) {
value.asReagent = false;
}
if (value.asProduct) {
value.asProduct = false;
}
}
}