openchemlib-search-wasm 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -0
- package/lib/hex.d.ts +31 -0
- package/lib/hex.d.ts.map +1 -0
- package/lib/hex.js +41 -0
- package/lib/hex.js.map +1 -0
- package/lib/index.d.ts +58 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +83 -7
- package/lib/index.js.map +1 -1
- package/lib/scan.d.ts +37 -7
- package/lib/scan.d.ts.map +1 -1
- package/lib/scan.js +90 -11
- package/lib/scan.js.map +1 -1
- package/lib/strongHash.d.ts +9 -0
- package/lib/strongHash.d.ts.map +1 -0
- package/lib/strongHash.js +56 -0
- package/lib/strongHash.js.map +1 -0
- package/lib/types.d.ts +37 -1
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +11 -0
- package/lib/types.js.map +1 -1
- package/lib/wasm/load.d.ts.map +1 -1
- package/lib/wasm/load.js +9 -1
- package/lib/wasm/load.js.map +1 -1
- package/lib/wasm/log.d.ts +36 -0
- package/lib/wasm/log.d.ts.map +1 -0
- package/lib/wasm/log.js +52 -0
- package/lib/wasm/log.js.map +1 -0
- package/package.json +1 -1
- package/src/hex.ts +43 -0
- package/src/index.ts +147 -5
- package/src/scan.ts +169 -16
- package/src/strongHash.ts +57 -0
- package/src/types.ts +56 -0
- package/src/wasm/load.ts +10 -1
- package/src/wasm/log.ts +66 -0
- package/wasm/data.js +1 -1
- package/wasm/runtime.d.ts +6 -0
package/src/index.ts
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
1
|
import {
|
|
2
2
|
buildIndexes,
|
|
3
3
|
buildNoStereoHashes,
|
|
4
|
+
buildNoStereoIdCodes,
|
|
4
5
|
buildNoStereoTautomerHashes,
|
|
6
|
+
buildNoStereoTautomerIdCodes,
|
|
5
7
|
} from './scan.ts';
|
|
6
8
|
import type { HashOptions } from './types.ts';
|
|
7
|
-
import { INDEX_WORDS } from './types.ts';
|
|
9
|
+
import { DEFAULT_MAX_TAUTOMERS, INDEX_WORDS, NO_ID_CODE } from './types.ts';
|
|
8
10
|
|
|
9
11
|
export { substructureSearch } from './substructureSearch.ts';
|
|
12
|
+
export { setLogHandler } from './wasm/log.ts';
|
|
13
|
+
export { hashToHex, hexToHash } from './hex.ts';
|
|
14
|
+
export { strongHash } from './strongHash.ts';
|
|
10
15
|
export { similaritySearch } from './similaritySearch.ts';
|
|
11
|
-
export {
|
|
16
|
+
export {
|
|
17
|
+
DEFAULT_MAX_TAUTOMERS,
|
|
18
|
+
INDEX_WORDS,
|
|
19
|
+
NO_HASH,
|
|
20
|
+
NO_ID_CODE,
|
|
21
|
+
} from './types.ts';
|
|
22
|
+
export type { LogHandler, LogStream } from './wasm/log.ts';
|
|
12
23
|
export type {
|
|
13
24
|
HashOptions,
|
|
14
25
|
ResultBuffer,
|
|
@@ -99,9 +110,21 @@ export function getNoStereoTautomerHash(
|
|
|
99
110
|
idCode: string,
|
|
100
111
|
options: HashOptions = {},
|
|
101
112
|
): bigint {
|
|
102
|
-
const {
|
|
113
|
+
const {
|
|
114
|
+
largestFragmentOnly = false,
|
|
115
|
+
tautomerCounts,
|
|
116
|
+
maxTautomers = DEFAULT_MAX_TAUTOMERS,
|
|
117
|
+
} = options;
|
|
103
118
|
const result = new BigInt64Array(1);
|
|
104
|
-
buildNoStereoTautomerHashes(
|
|
119
|
+
buildNoStereoTautomerHashes(
|
|
120
|
+
[idCode],
|
|
121
|
+
result,
|
|
122
|
+
largestFragmentOnly,
|
|
123
|
+
0,
|
|
124
|
+
1,
|
|
125
|
+
tautomerCounts,
|
|
126
|
+
maxTautomers,
|
|
127
|
+
);
|
|
105
128
|
return result[0] as bigint;
|
|
106
129
|
}
|
|
107
130
|
|
|
@@ -126,7 +149,11 @@ export function getNoStereoTautomerHashes(
|
|
|
126
149
|
idCodes: string[],
|
|
127
150
|
options: HashOptions = {},
|
|
128
151
|
): BigInt64Array {
|
|
129
|
-
const {
|
|
152
|
+
const {
|
|
153
|
+
largestFragmentOnly = false,
|
|
154
|
+
tautomerCounts,
|
|
155
|
+
maxTautomers = DEFAULT_MAX_TAUTOMERS,
|
|
156
|
+
} = options;
|
|
130
157
|
const result = new BigInt64Array(idCodes.length);
|
|
131
158
|
buildNoStereoTautomerHashes(
|
|
132
159
|
idCodes,
|
|
@@ -134,6 +161,8 @@ export function getNoStereoTautomerHashes(
|
|
|
134
161
|
largestFragmentOnly,
|
|
135
162
|
0,
|
|
136
163
|
idCodes.length,
|
|
164
|
+
tautomerCounts,
|
|
165
|
+
maxTautomers,
|
|
137
166
|
);
|
|
138
167
|
return result;
|
|
139
168
|
}
|
|
@@ -192,3 +221,116 @@ export function getNoStereoHashes(
|
|
|
192
221
|
buildNoStereoHashes(idCodes, result, largestFragmentOnly, 0, idCodes.length);
|
|
193
222
|
return result;
|
|
194
223
|
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Canonizes one idcode to the form OpenChemLib identifies a molecule by up to stereochemistry.
|
|
227
|
+
*
|
|
228
|
+
* This is the string {@link getNoStereoHash} hashes, for a database that stores the canonical form
|
|
229
|
+
* itself. Prefer the hash where the column is only ever compared: it is a signed 64-bit integer, so
|
|
230
|
+
* it stores and indexes in 8 bytes where these idcodes average around 25 characters.
|
|
231
|
+
*
|
|
232
|
+
* ```js
|
|
233
|
+
* getNoStereoIdCode(idCode); // 'gGX`BDdwMT@@'
|
|
234
|
+
* ```
|
|
235
|
+
* @param idCode - The molecule to canonize, as an idcode.
|
|
236
|
+
* @param options - See {@link HashOptions}.
|
|
237
|
+
* @returns Its canonical no-stereo idcode, or `NO_ID_CODE` (`''`) for an idcode that will not parse
|
|
238
|
+
* or a molecule OpenChemLib cannot canonize.
|
|
239
|
+
*/
|
|
240
|
+
export function getNoStereoIdCode(
|
|
241
|
+
idCode: string,
|
|
242
|
+
options: HashOptions = {},
|
|
243
|
+
): string {
|
|
244
|
+
const { largestFragmentOnly = false } = options;
|
|
245
|
+
const result = [NO_ID_CODE];
|
|
246
|
+
buildNoStereoIdCodes([idCode], result, largestFragmentOnly, 0, 1);
|
|
247
|
+
return result[0] as string;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Canonizes every idcode to the form OpenChemLib identifies a molecule by up to stereochemistry.
|
|
252
|
+
*
|
|
253
|
+
* See {@link getNoStereoIdCode} for what the form identifies.
|
|
254
|
+
* @param idCodes - The molecules to canonize, as idcodes.
|
|
255
|
+
* @param options - See {@link HashOptions}.
|
|
256
|
+
* @returns One idcode per input, in order, with `NO_ID_CODE` (`''`) for each one that could not be
|
|
257
|
+
* canonized.
|
|
258
|
+
*/
|
|
259
|
+
export function getNoStereoIdCodes(
|
|
260
|
+
idCodes: string[],
|
|
261
|
+
options: HashOptions = {},
|
|
262
|
+
): string[] {
|
|
263
|
+
const { largestFragmentOnly = false } = options;
|
|
264
|
+
const result = new Array<string>(idCodes.length).fill(NO_ID_CODE);
|
|
265
|
+
buildNoStereoIdCodes(idCodes, result, largestFragmentOnly, 0, idCodes.length);
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Canonizes one idcode to the form OpenChemLib identifies a molecule by up to tautomerism and
|
|
271
|
+
* stereochemistry.
|
|
272
|
+
*
|
|
273
|
+
* This is the string {@link getNoStereoTautomerHash} hashes. It is the generic tautomer's idcode, so
|
|
274
|
+
* it is longer than the molecule's own — around twice it for a molecule with tautomeric sites — and
|
|
275
|
+
* it is not a structure: parsing it back gives a molecule with every tautomeric bond normalized, not
|
|
276
|
+
* the compound. Use it as a key, never as something to draw.
|
|
277
|
+
* @param idCode - The molecule to canonize, as an idcode.
|
|
278
|
+
* @param options - See {@link HashOptions}.
|
|
279
|
+
* @returns Its canonical no-stereo generic-tautomer idcode, or `NO_ID_CODE` (`''`) for an idcode that
|
|
280
|
+
* will not parse or a molecule OpenChemLib cannot canonize.
|
|
281
|
+
*/
|
|
282
|
+
export function getNoStereoTautomerIdCode(
|
|
283
|
+
idCode: string,
|
|
284
|
+
options: HashOptions = {},
|
|
285
|
+
): string {
|
|
286
|
+
const {
|
|
287
|
+
largestFragmentOnly = false,
|
|
288
|
+
tautomerCounts,
|
|
289
|
+
maxTautomers = DEFAULT_MAX_TAUTOMERS,
|
|
290
|
+
} = options;
|
|
291
|
+
const result = [NO_ID_CODE];
|
|
292
|
+
buildNoStereoTautomerIdCodes(
|
|
293
|
+
[idCode],
|
|
294
|
+
result,
|
|
295
|
+
largestFragmentOnly,
|
|
296
|
+
0,
|
|
297
|
+
1,
|
|
298
|
+
tautomerCounts,
|
|
299
|
+
maxTautomers,
|
|
300
|
+
);
|
|
301
|
+
return result[0] as string;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Canonizes every idcode to the form OpenChemLib identifies a molecule by up to tautomerism and
|
|
306
|
+
* stereochemistry.
|
|
307
|
+
*
|
|
308
|
+
* See {@link getNoStereoTautomerIdCode} for what the form identifies, and
|
|
309
|
+
* {@link getNoStereoTautomerHashes} for what it costs — the canonization is the expensive part, not
|
|
310
|
+
* the hashing, so these cost what those do.
|
|
311
|
+
* @param idCodes - The molecules to canonize, as idcodes.
|
|
312
|
+
* @param options - See {@link HashOptions}.
|
|
313
|
+
* @returns One idcode per input, in order, with `NO_ID_CODE` (`''`) for each one that could not be
|
|
314
|
+
* canonized.
|
|
315
|
+
*/
|
|
316
|
+
export function getNoStereoTautomerIdCodes(
|
|
317
|
+
idCodes: string[],
|
|
318
|
+
options: HashOptions = {},
|
|
319
|
+
): string[] {
|
|
320
|
+
const {
|
|
321
|
+
largestFragmentOnly = false,
|
|
322
|
+
tautomerCounts,
|
|
323
|
+
maxTautomers = DEFAULT_MAX_TAUTOMERS,
|
|
324
|
+
} = options;
|
|
325
|
+
const result = new Array<string>(idCodes.length).fill(NO_ID_CODE);
|
|
326
|
+
buildNoStereoTautomerIdCodes(
|
|
327
|
+
idCodes,
|
|
328
|
+
result,
|
|
329
|
+
largestFragmentOnly,
|
|
330
|
+
0,
|
|
331
|
+
idCodes.length,
|
|
332
|
+
tautomerCounts,
|
|
333
|
+
maxTautomers,
|
|
334
|
+
);
|
|
335
|
+
return result;
|
|
336
|
+
}
|
package/src/scan.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { ResultBuffer, SearchMode } from './types.ts';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_MAX_TAUTOMERS,
|
|
4
|
+
INDEX_WORDS,
|
|
5
|
+
NO_HASH,
|
|
6
|
+
NO_ID_CODE,
|
|
7
|
+
SimilarityResult,
|
|
8
|
+
SubstructureResult,
|
|
9
|
+
} from './types.ts';
|
|
3
10
|
import { loadOCL } from './wasm/load.ts';
|
|
4
11
|
|
|
5
12
|
// Top-level await: the WASM module is instantiated when this module is imported, so the searches are
|
|
@@ -122,9 +129,8 @@ function firstPending(
|
|
|
122
129
|
/**
|
|
123
130
|
* Fingerprints `idCodes[from .. to)` into `result`, sixteen words per molecule.
|
|
124
131
|
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
* for a molecule that could not be read.
|
|
132
|
+
* A molecule that cannot be read gets sixteen zeros, which no non-empty query is a subset of, so it
|
|
133
|
+
* is never a false candidate.
|
|
128
134
|
* @param idCodes - The molecules to fingerprint.
|
|
129
135
|
* @param result - The caller's buffer, `16 * idCodes.length` words long.
|
|
130
136
|
* @param from - The first index to fingerprint.
|
|
@@ -137,20 +143,27 @@ export function buildIndexes(
|
|
|
137
143
|
from: number,
|
|
138
144
|
to: number,
|
|
139
145
|
): number {
|
|
140
|
-
return
|
|
146
|
+
return recoveringFromTraps(
|
|
147
|
+
(first, last) => Search.getIndexes(idCodes, result, first, last),
|
|
148
|
+
(index) => result.fill(0, index * INDEX_WORDS, (index + 1) * INDEX_WORDS),
|
|
149
|
+
from,
|
|
150
|
+
to,
|
|
151
|
+
);
|
|
141
152
|
}
|
|
142
153
|
|
|
143
154
|
/**
|
|
144
155
|
* Hashes `idCodes[from .. to)` into `result`, one no-stereo tautomer hash per molecule.
|
|
145
156
|
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* returns for a molecule it cannot canonize, so the two failures are reported the same way.
|
|
157
|
+
* A molecule that cannot be read gets `NO_HASH`, which is also what OpenChemLib returns for one it
|
|
158
|
+
* cannot canonize, so the two failures are reported the same way.
|
|
149
159
|
* @param idCodes - The molecules to hash.
|
|
150
160
|
* @param result - The caller's buffer, `idCodes.length` entries long.
|
|
151
161
|
* @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
|
|
152
162
|
* @param from - The first index to hash.
|
|
153
163
|
* @param to - One past the last index to hash.
|
|
164
|
+
* @param tautomerCounts - Filled with how many tautomers each molecule needed; an entry that reached
|
|
165
|
+
* `maxTautomers` is one OpenChemLib stopped early on.
|
|
166
|
+
* @param maxTautomers - The ceiling on that enumeration, which is what bounds the cost.
|
|
154
167
|
* @returns How many molecules were hashed.
|
|
155
168
|
*/
|
|
156
169
|
export function buildNoStereoTautomerHashes(
|
|
@@ -159,11 +172,23 @@ export function buildNoStereoTautomerHashes(
|
|
|
159
172
|
largestFragmentOnly: boolean,
|
|
160
173
|
from: number,
|
|
161
174
|
to: number,
|
|
175
|
+
tautomerCounts: Int32Array = new Int32Array(idCodes.length),
|
|
176
|
+
maxTautomers: number = DEFAULT_MAX_TAUTOMERS,
|
|
162
177
|
): number {
|
|
163
|
-
return
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
178
|
+
return recoveringFromTraps(
|
|
179
|
+
(first, last) =>
|
|
180
|
+
Search.getNoStereoTautomerHashes(
|
|
181
|
+
idCodes,
|
|
182
|
+
result,
|
|
183
|
+
tautomerCounts,
|
|
184
|
+
largestFragmentOnly,
|
|
185
|
+
maxTautomers,
|
|
186
|
+
first,
|
|
187
|
+
last,
|
|
188
|
+
),
|
|
189
|
+
(index) => {
|
|
190
|
+
result[index] = NO_HASH;
|
|
191
|
+
},
|
|
167
192
|
from,
|
|
168
193
|
to,
|
|
169
194
|
);
|
|
@@ -188,11 +213,139 @@ export function buildNoStereoHashes(
|
|
|
188
213
|
from: number,
|
|
189
214
|
to: number,
|
|
190
215
|
): number {
|
|
191
|
-
return
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
216
|
+
return recoveringFromTraps(
|
|
217
|
+
(first, last) =>
|
|
218
|
+
Search.getNoStereoHashes(
|
|
219
|
+
idCodes,
|
|
220
|
+
result,
|
|
221
|
+
largestFragmentOnly,
|
|
222
|
+
first,
|
|
223
|
+
last,
|
|
224
|
+
),
|
|
225
|
+
(index) => {
|
|
226
|
+
result[index] = NO_HASH;
|
|
227
|
+
},
|
|
228
|
+
from,
|
|
229
|
+
to,
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Canonizes `idCodes[from .. to)` into `result`, one no-stereo idcode per molecule.
|
|
235
|
+
*
|
|
236
|
+
* This is the string {@link buildNoStereoHashes} hashes, for a caller that stores the canonical form
|
|
237
|
+
* rather than a 64-bit key. A molecule that cannot be read keeps the `NO_ID_CODE` the caller's array
|
|
238
|
+
* was filled with.
|
|
239
|
+
* @param idCodes - The molecules to canonize.
|
|
240
|
+
* @param result - The caller's array, `idCodes.length` entries long, filled with `NO_ID_CODE`.
|
|
241
|
+
* @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
|
|
242
|
+
* @param from - The first index to canonize.
|
|
243
|
+
* @param to - One past the last index to canonize.
|
|
244
|
+
* @returns How many molecules were canonized.
|
|
245
|
+
*/
|
|
246
|
+
export function buildNoStereoIdCodes(
|
|
247
|
+
idCodes: string[],
|
|
248
|
+
result: string[],
|
|
249
|
+
largestFragmentOnly: boolean,
|
|
250
|
+
from: number,
|
|
251
|
+
to: number,
|
|
252
|
+
): number {
|
|
253
|
+
return recoveringFromTraps(
|
|
254
|
+
(first, last) =>
|
|
255
|
+
Search.getNoStereoIdCodes(
|
|
256
|
+
idCodes,
|
|
257
|
+
result,
|
|
258
|
+
largestFragmentOnly,
|
|
259
|
+
first,
|
|
260
|
+
last,
|
|
261
|
+
),
|
|
262
|
+
(index) => {
|
|
263
|
+
result[index] = NO_ID_CODE;
|
|
264
|
+
},
|
|
265
|
+
from,
|
|
266
|
+
to,
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Canonizes `idCodes[from .. to)` into `result`, one no-stereo generic-tautomer idcode per molecule.
|
|
272
|
+
*
|
|
273
|
+
* Fails the same way {@link buildNoStereoIdCodes} does.
|
|
274
|
+
* @param idCodes - The molecules to canonize.
|
|
275
|
+
* @param result - The caller's array, `idCodes.length` entries long, filled with `NO_ID_CODE`.
|
|
276
|
+
* @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
|
|
277
|
+
* @param from - The first index to canonize.
|
|
278
|
+
* @param to - One past the last index to canonize.
|
|
279
|
+
* @param tautomerCounts - Filled with how many tautomers each molecule needed; an entry that reached
|
|
280
|
+
* `maxTautomers` is one OpenChemLib stopped early on.
|
|
281
|
+
* @param maxTautomers - The ceiling on that enumeration, which is what bounds the cost.
|
|
282
|
+
* @returns How many molecules were canonized.
|
|
283
|
+
*/
|
|
284
|
+
export function buildNoStereoTautomerIdCodes(
|
|
285
|
+
idCodes: string[],
|
|
286
|
+
result: string[],
|
|
287
|
+
largestFragmentOnly: boolean,
|
|
288
|
+
from: number,
|
|
289
|
+
to: number,
|
|
290
|
+
tautomerCounts: Int32Array = new Int32Array(idCodes.length),
|
|
291
|
+
maxTautomers: number = DEFAULT_MAX_TAUTOMERS,
|
|
292
|
+
): number {
|
|
293
|
+
return recoveringFromTraps(
|
|
294
|
+
(first, last) =>
|
|
295
|
+
Search.getNoStereoTautomerIdCodes(
|
|
296
|
+
idCodes,
|
|
297
|
+
result,
|
|
298
|
+
tautomerCounts,
|
|
299
|
+
largestFragmentOnly,
|
|
300
|
+
maxTautomers,
|
|
301
|
+
first,
|
|
302
|
+
last,
|
|
303
|
+
),
|
|
304
|
+
(index) => {
|
|
305
|
+
result[index] = NO_ID_CODE;
|
|
306
|
+
},
|
|
195
307
|
from,
|
|
196
308
|
to,
|
|
197
309
|
);
|
|
198
310
|
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Runs a ranged WASM call, and redoes the range one molecule at a time if it traps.
|
|
314
|
+
*
|
|
315
|
+
* A malformed idcode makes OpenChemLib's bit-stream parser read past its input, which WebAssembly
|
|
316
|
+
* traps on — the same trap {@link scanRange} recovers from. The Java guard catches an empty or
|
|
317
|
+
* non-ASCII idcode, but a malformed ASCII one reaches the parser and traps there, which would
|
|
318
|
+
* otherwise lose every molecule in the range including the good ones. The trap leaves the module
|
|
319
|
+
* usable, and a per-molecule call costs what a batched one does because the 512 key fragments are
|
|
320
|
+
* parsed once for the module's lifetime, so redoing the range singly costs nothing that was not
|
|
321
|
+
* already going to be spent.
|
|
322
|
+
*
|
|
323
|
+
* There is no cap on the failures: these functions promise a per-molecule "could not read this"
|
|
324
|
+
* answer rather than an exception, so an array that is entirely garbage yields one of those per
|
|
325
|
+
* entry.
|
|
326
|
+
* @param call - Runs the WASM call over a range, returning how many molecules it handled.
|
|
327
|
+
* @param markUnreadable - Writes the "could not read this" answer for one molecule.
|
|
328
|
+
* @param from - The first index to handle.
|
|
329
|
+
* @param to - One past the last index to handle.
|
|
330
|
+
* @returns How many molecules were handled.
|
|
331
|
+
*/
|
|
332
|
+
function recoveringFromTraps(
|
|
333
|
+
call: (from: number, to: number) => number,
|
|
334
|
+
markUnreadable: (index: number) => void,
|
|
335
|
+
from: number,
|
|
336
|
+
to: number,
|
|
337
|
+
): number {
|
|
338
|
+
try {
|
|
339
|
+
return call(from, to);
|
|
340
|
+
} catch {
|
|
341
|
+
let handled = 0;
|
|
342
|
+
for (let index = from; index < to; index++) {
|
|
343
|
+
try {
|
|
344
|
+
handled += call(index, index + 1);
|
|
345
|
+
} catch {
|
|
346
|
+
markUnreadable(index);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return handled;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenChemLib's 64-bit StrongHasher, which is how every `CanonizerUtil` hash is defined: the
|
|
3
|
+
* molecule is reduced to a canonical idcode, and that string is hashed. `openchemlib` exposes the
|
|
4
|
+
* canonical forms but not the hasher, so this is its `CanonizerUtil.StrongHasher` transcribed.
|
|
5
|
+
*
|
|
6
|
+
* It is here so a caller that already holds a canonical idcode — one of this package's, or a column
|
|
7
|
+
* of them in a database — can get its hash without canonizing again. That matters because
|
|
8
|
+
* canonizing a generic tautomer is the most expensive thing this package does, and asking for both
|
|
9
|
+
* the idcode and the hash would otherwise pay for it twice.
|
|
10
|
+
*
|
|
11
|
+
* ```js
|
|
12
|
+
* strongHash(getNoStereoTautomerIdCode(idCode)) === getNoStereoTautomerHash(idCode);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
const MASK_64 = (1n << 64n) - 1n;
|
|
16
|
+
const HSTART = 0xbb40e64da205b064n;
|
|
17
|
+
const HMULT = 7664345821815920749n;
|
|
18
|
+
const BYTE_TABLE = buildByteTable();
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `openchemlib-js` exposes `CanonizerUtil.getIDCode` but not the hash built from it, so the hash
|
|
22
|
+
* tests hash that idcode here. This is OpenChemLib's `StrongHasher.hash` transcribed, and having it
|
|
23
|
+
* written a second way is what makes those comparisons a real cross-check.
|
|
24
|
+
* @param text - The idcode to hash.
|
|
25
|
+
* @returns Its 64-bit hash, signed, as a `BigInt64Array` entry holds it.
|
|
26
|
+
*/
|
|
27
|
+
export function strongHash(text: string): bigint {
|
|
28
|
+
let h = HSTART;
|
|
29
|
+
for (let i = text.length - 1; i >= 0; i--) {
|
|
30
|
+
const character = BigInt(text.codePointAt(i) as number);
|
|
31
|
+
h =
|
|
32
|
+
((h * HMULT) & MASK_64) ^
|
|
33
|
+
(BYTE_TABLE[Number(character & 0xffn)] as bigint);
|
|
34
|
+
h =
|
|
35
|
+
((h * HMULT) & MASK_64) ^
|
|
36
|
+
(BYTE_TABLE[Number((character >> 8n) & 0xffn)] as bigint);
|
|
37
|
+
}
|
|
38
|
+
return BigInt.asIntN(64, h);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* OpenChemLib's `CanonizerUtil.StrongHasher` byte table, built the same way the Java does.
|
|
43
|
+
* @returns The 256 seeds, as unsigned 64-bit values.
|
|
44
|
+
*/
|
|
45
|
+
function buildByteTable(): bigint[] {
|
|
46
|
+
const table = new Array<bigint>(256);
|
|
47
|
+
let h = 0x544b2fbacaaf1684n;
|
|
48
|
+
for (let i = 0; i < 256; i++) {
|
|
49
|
+
for (let j = 0; j < 31; j++) {
|
|
50
|
+
h = ((h >> 7n) ^ h) & MASK_64;
|
|
51
|
+
h = ((h << 11n) ^ h) & MASK_64;
|
|
52
|
+
h = ((h >> 10n) ^ h) & MASK_64;
|
|
53
|
+
}
|
|
54
|
+
table[i] = h;
|
|
55
|
+
}
|
|
56
|
+
return table;
|
|
57
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -194,7 +194,9 @@ export interface OCLSearch {
|
|
|
194
194
|
getNoStereoTautomerHashes: (
|
|
195
195
|
idCodes: string[],
|
|
196
196
|
result: BigInt64Array,
|
|
197
|
+
tautomerCounts: Int32Array,
|
|
197
198
|
largestFragmentOnly: boolean,
|
|
199
|
+
maxTautomers: number,
|
|
198
200
|
from: number,
|
|
199
201
|
to: number,
|
|
200
202
|
) => number;
|
|
@@ -206,6 +208,24 @@ export interface OCLSearch {
|
|
|
206
208
|
from: number,
|
|
207
209
|
to: number,
|
|
208
210
|
) => number;
|
|
211
|
+
/** Returns how many molecules in the range were canonized. */
|
|
212
|
+
getNoStereoIdCodes: (
|
|
213
|
+
idCodes: string[],
|
|
214
|
+
result: string[],
|
|
215
|
+
largestFragmentOnly: boolean,
|
|
216
|
+
from: number,
|
|
217
|
+
to: number,
|
|
218
|
+
) => number;
|
|
219
|
+
/** Returns how many molecules in the range were canonized. */
|
|
220
|
+
getNoStereoTautomerIdCodes: (
|
|
221
|
+
idCodes: string[],
|
|
222
|
+
result: string[],
|
|
223
|
+
tautomerCounts: Int32Array,
|
|
224
|
+
largestFragmentOnly: boolean,
|
|
225
|
+
maxTautomers: number,
|
|
226
|
+
from: number,
|
|
227
|
+
to: number,
|
|
228
|
+
) => number;
|
|
209
229
|
};
|
|
210
230
|
}
|
|
211
231
|
|
|
@@ -221,6 +241,29 @@ export interface HashOptions {
|
|
|
221
241
|
* @default false
|
|
222
242
|
*/
|
|
223
243
|
largestFragmentOnly?: boolean;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The ceiling on how many tautomers OpenChemLib enumerates before it settles for what it has.
|
|
247
|
+
* Only the tautomer functions read it.
|
|
248
|
+
*
|
|
249
|
+
* The cost of a tautomer key is set by this, not by the molecule's size: the median molecule needs
|
|
250
|
+
* a hundred microseconds and one in a hundred runs into the ceiling and spends the better part of
|
|
251
|
+
* a second getting there. Lowering it bounds that tail, at the price of a key that may not be
|
|
252
|
+
* canonical for the molecules that hit it — compare `tautomerCounts` against this value to find
|
|
253
|
+
* them.
|
|
254
|
+
* @default 100000 — OpenChemLib's own
|
|
255
|
+
*/
|
|
256
|
+
maxTautomers?: number;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Filled with how many tautomers were enumerated for each molecule, if given. An entry that
|
|
260
|
+
* reached {@link HashOptions.maxTautomers} is one OpenChemLib stopped early on, which is the only
|
|
261
|
+
* signal that a key may not be canonical.
|
|
262
|
+
*
|
|
263
|
+
* It is the caller's buffer, `idCodes.length` entries long, so a table of flags costs one
|
|
264
|
+
* allocation. Only the tautomer functions write it.
|
|
265
|
+
*/
|
|
266
|
+
tautomerCounts?: Int32Array;
|
|
224
267
|
}
|
|
225
268
|
|
|
226
269
|
/** {@link HashOptions} under the name the tautomer hashes introduced it with. */
|
|
@@ -232,3 +275,16 @@ export type TautomerHashOptions = HashOptions;
|
|
|
232
275
|
* usable "no hash" check.
|
|
233
276
|
*/
|
|
234
277
|
export const NO_HASH = 0n;
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The canonical idcode of a molecule that has none: its idcode could not be parsed, or OpenChemLib
|
|
281
|
+
* could not canonize it. The empty string is never a valid idcode, so an equality test against it is
|
|
282
|
+
* the idcode counterpart of {@link NO_HASH}.
|
|
283
|
+
*/
|
|
284
|
+
export const NO_ID_CODE = '';
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* How many tautomers OpenChemLib enumerates before it settles for what it has, unless
|
|
288
|
+
* {@link HashOptions.maxTautomers} says otherwise. It is OpenChemLib's own ceiling.
|
|
289
|
+
*/
|
|
290
|
+
export const DEFAULT_MAX_TAUTOMERS = 100_000;
|
package/src/wasm/load.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { wasmGzipBase64 } from '../../wasm/data.js';
|
|
|
2
2
|
import { load } from '../../wasm/runtime.js';
|
|
3
3
|
import type { OCLSearch } from '../types.ts';
|
|
4
4
|
|
|
5
|
+
import { createConsoleImport } from './log.ts';
|
|
6
|
+
|
|
5
7
|
let modulePromise: Promise<OCLSearch> | undefined;
|
|
6
8
|
|
|
7
9
|
/**
|
|
@@ -27,7 +29,14 @@ export function loadOCL(): Promise<OCLSearch> {
|
|
|
27
29
|
|
|
28
30
|
async function instantiate(): Promise<OCLSearch> {
|
|
29
31
|
const wasmBytes = await gunzip(base64ToBytes(wasmGzipBase64));
|
|
30
|
-
const teavm = await load(wasmBytes, {
|
|
32
|
+
const teavm = await load(wasmBytes, {
|
|
33
|
+
memory: { maxSize: MAX_LINEAR_MEMORY },
|
|
34
|
+
// The runtime's own `teavmConsole` puts Java's System.out on `console.log`, so OpenChemLib's
|
|
35
|
+
// "Tautomer count exceeds maximum" lands on the host's stdout — see src/wasm/log.ts.
|
|
36
|
+
installImports(imports) {
|
|
37
|
+
imports.teavmConsole = createConsoleImport();
|
|
38
|
+
},
|
|
39
|
+
});
|
|
31
40
|
return teavm.exports as unknown as OCLSearch;
|
|
32
41
|
}
|
|
33
42
|
|
package/src/wasm/log.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** Which of Java's two standard streams a line came from. */
|
|
2
|
+
export type LogStream = 'stdout' | 'stderr';
|
|
3
|
+
|
|
4
|
+
/** Called with one line OpenChemLib wrote to a standard stream. */
|
|
5
|
+
export type LogHandler = (message: string, stream: LogStream) => void;
|
|
6
|
+
|
|
7
|
+
/** TeaVM imports one character sink per stream; it writes a character at a time. */
|
|
8
|
+
interface TeaVMConsole {
|
|
9
|
+
putcharStdout: (code: number) => void;
|
|
10
|
+
putcharStderr: (code: number) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const NEWLINE = 10;
|
|
14
|
+
|
|
15
|
+
let handler: LogHandler | null = null;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Routes what OpenChemLib writes to Java's standard streams wherever you want it.
|
|
19
|
+
*
|
|
20
|
+
* Nothing is written anywhere by default, and that is the point: OpenChemLib prints
|
|
21
|
+
* `Tautomer count exceeds maximum: <idcode>` for every molecule whose tautomers it stops
|
|
22
|
+
* enumerating, which an import emits thousands of times, and the generated TeaVM runtime maps that
|
|
23
|
+
* onto `console.log` — the same stream a host writing structured logs is using, whose output it
|
|
24
|
+
* then corrupts. The message says nothing a caller can act on; the hash is still returned and still
|
|
25
|
+
* identifies the molecule. So the default is to drop it, and a host that wants it says where.
|
|
26
|
+
*
|
|
27
|
+
* It applies whether or not the module has been instantiated yet, so it can be set at startup or
|
|
28
|
+
* around one call.
|
|
29
|
+
*
|
|
30
|
+
* ```js
|
|
31
|
+
* setLogHandler((message, stream) => logger.debug({ stream }, message));
|
|
32
|
+
* ```
|
|
33
|
+
* @param next - Called once per line, or `null` to go back to discarding them.
|
|
34
|
+
*/
|
|
35
|
+
export function setLogHandler(next: LogHandler | null): void {
|
|
36
|
+
handler = next;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Builds the `teavmConsole` import that replaces the generated runtime's own, which calls
|
|
41
|
+
* `console.log` and `console.error`.
|
|
42
|
+
* @returns The import object to install over it.
|
|
43
|
+
*/
|
|
44
|
+
export function createConsoleImport(): TeaVMConsole {
|
|
45
|
+
return {
|
|
46
|
+
putcharStdout: lineSink('stdout'),
|
|
47
|
+
putcharStderr: lineSink('stderr'),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Collects characters into a line and hands each finished one to the current handler.
|
|
53
|
+
* @param stream - The stream these characters belong to.
|
|
54
|
+
* @returns The per-character callback the runtime imports.
|
|
55
|
+
*/
|
|
56
|
+
function lineSink(stream: LogStream): (code: number) => void {
|
|
57
|
+
let line = '';
|
|
58
|
+
return (code: number) => {
|
|
59
|
+
if (code === NEWLINE) {
|
|
60
|
+
handler?.(line, stream);
|
|
61
|
+
line = '';
|
|
62
|
+
} else {
|
|
63
|
+
line += String.fromCodePoint(code);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|