openchemlib-search-wasm 1.2.0 → 2.1.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.
Files changed (59) hide show
  1. package/README.md +290 -3
  2. package/lib/functionKeys/dictionary.json +72071 -0
  3. package/lib/functionKeys/index.d.ts +105 -0
  4. package/lib/functionKeys/index.d.ts.map +1 -0
  5. package/lib/functionKeys/index.js +98 -0
  6. package/lib/functionKeys/index.js.map +1 -0
  7. package/lib/functions.d.ts +38 -0
  8. package/lib/functions.d.ts.map +1 -0
  9. package/lib/functions.js +54 -0
  10. package/lib/functions.js.map +1 -0
  11. package/lib/hex.d.ts +31 -0
  12. package/lib/hex.d.ts.map +1 -0
  13. package/lib/hex.js +41 -0
  14. package/lib/hex.js.map +1 -0
  15. package/lib/index.d.ts +140 -2
  16. package/lib/index.d.ts.map +1 -1
  17. package/lib/index.js +184 -7
  18. package/lib/index.js.map +1 -1
  19. package/lib/records.d.ts +45 -0
  20. package/lib/records.d.ts.map +1 -0
  21. package/lib/records.js +96 -0
  22. package/lib/records.js.map +1 -0
  23. package/lib/scan.d.ts +99 -7
  24. package/lib/scan.d.ts.map +1 -1
  25. package/lib/scan.js +158 -11
  26. package/lib/scan.js.map +1 -1
  27. package/lib/strongHash.d.ts +9 -0
  28. package/lib/strongHash.d.ts.map +1 -0
  29. package/lib/strongHash.js +56 -0
  30. package/lib/strongHash.js.map +1 -0
  31. package/lib/substructureSearch.d.ts +8 -5
  32. package/lib/substructureSearch.d.ts.map +1 -1
  33. package/lib/substructureSearch.js +28 -5
  34. package/lib/substructureSearch.js.map +1 -1
  35. package/lib/types.d.ts +120 -3
  36. package/lib/types.d.ts.map +1 -1
  37. package/lib/types.js +17 -1
  38. package/lib/types.js.map +1 -1
  39. package/lib/wasm/load.d.ts.map +1 -1
  40. package/lib/wasm/load.js +9 -1
  41. package/lib/wasm/load.js.map +1 -1
  42. package/lib/wasm/log.d.ts +36 -0
  43. package/lib/wasm/log.d.ts.map +1 -0
  44. package/lib/wasm/log.js +52 -0
  45. package/lib/wasm/log.js.map +1 -0
  46. package/package.json +6 -3
  47. package/src/functionKeys/index.ts +181 -0
  48. package/src/functions.ts +56 -0
  49. package/src/hex.ts +43 -0
  50. package/src/index.ts +269 -5
  51. package/src/records.ts +142 -0
  52. package/src/scan.ts +259 -16
  53. package/src/strongHash.ts +57 -0
  54. package/src/substructureSearch.ts +29 -6
  55. package/src/types.ts +171 -2
  56. package/src/wasm/load.ts +10 -1
  57. package/src/wasm/log.ts +66 -0
  58. package/wasm/data.js +1 -1
  59. package/wasm/runtime.d.ts +6 -0
package/src/index.ts CHANGED
@@ -1,16 +1,44 @@
1
1
  import {
2
2
  buildIndexes,
3
3
  buildNoStereoHashes,
4
+ buildNoStereoIdCodes,
4
5
  buildNoStereoTautomerHashes,
6
+ buildNoStereoTautomerIdCodes,
7
+ buildQueryWords,
8
+ buildScreens,
9
+ readKeys,
5
10
  } from './scan.ts';
6
11
  import type { HashOptions } from './types.ts';
7
- import { INDEX_WORDS } from './types.ts';
12
+ import { DEFAULT_MAX_TAUTOMERS, INDEX_WORDS, NO_ID_CODE } from './types.ts';
8
13
 
9
14
  export { substructureSearch } from './substructureSearch.ts';
15
+ export { setLogHandler } from './wasm/log.ts';
16
+ export { hashToHex, hexToHash } from './hex.ts';
17
+ export { strongHash } from './strongHash.ts';
10
18
  export { similaritySearch } from './similaritySearch.ts';
11
- export { INDEX_WORDS, NO_HASH } from './types.ts';
19
+ export {
20
+ getMoleculeRecords,
21
+ getMoleculeSizes,
22
+ getQueryAtomFloor,
23
+ } from './records.ts';
24
+ export {
25
+ getFunction,
26
+ getFunctionKeyFragments,
27
+ getFunctions,
28
+ } from './functions.ts';
29
+ export {
30
+ DEFAULT_MAX_TAUTOMERS,
31
+ INDEX_WORDS,
32
+ NO_HASH,
33
+ NO_ID_CODE,
34
+ RECORD_WORDS,
35
+ } from './types.ts';
36
+ export type { LogHandler, LogStream } from './wasm/log.ts';
12
37
  export type {
13
38
  HashOptions,
39
+ MoleculeRecords,
40
+ MoleculeRecordsOptions,
41
+ MoleculeSizes,
14
42
  ResultBuffer,
15
43
  SearchMode,
16
44
  SearchOptions,
@@ -74,6 +102,111 @@ export function getIndexes(idCodes: string[]): Int32Array[] {
74
102
  return indexes;
75
103
  }
76
104
 
105
+ /**
106
+ * Builds the 512-bit screen of one idcode, as sixteen 32-bit words: bit i is set when key i of the
107
+ * screen dictionary is a substructure of the molecule.
108
+ *
109
+ * The screen sits next to the FragFp fingerprint of {@link getIndex}. A molecule is a candidate for
110
+ * a query only if it holds every bit of {@link getQueryIndex} in its fingerprint and every bit of
111
+ * {@link getQueryScreen} in its screen; requiring both lets through about a sixth of the
112
+ * non-matching molecules FragFp alone lets through. The dictionary is 512 OpenChemLib query
113
+ * fragments, matched the way FragFp matches its own keys, and a screen costs about 0.7 times a
114
+ * fingerprint.
115
+ *
116
+ * ```js
117
+ * const isCandidate = (query, molecule) =>
118
+ * query.every((word, i) => (word & molecule[i]) === word);
119
+ * isCandidate(getQueryScreen(query), getScreen(idCode));
120
+ * ```
121
+ * @param idCode - The molecule to screen, as an idcode.
122
+ * @returns Sixteen words. An idcode that will not parse gives sixteen zeros, which no non-empty
123
+ * query screen is a subset of.
124
+ */
125
+ export function getScreen(idCode: string): Int32Array {
126
+ const result = new Int32Array(INDEX_WORDS);
127
+ buildScreens([idCode], result, 0, 1);
128
+ return result;
129
+ }
130
+
131
+ /**
132
+ * Builds the 512-bit screen of every idcode, sixteen 32-bit words each.
133
+ *
134
+ * See {@link getScreen}. The screens are written into one buffer and returned as a view per
135
+ * molecule, each 8-byte aligned, as {@link getIndexes} does.
136
+ * @param idCodes - The molecules to screen, as idcodes.
137
+ * @returns One sixteen-word view per idcode, in order.
138
+ */
139
+ export function getScreens(idCodes: string[]): Int32Array[] {
140
+ const all = new Int32Array(idCodes.length * INDEX_WORDS);
141
+ buildScreens(idCodes, all, 0, idCodes.length);
142
+ const screens = new Array<Int32Array>(idCodes.length);
143
+ for (let i = 0; i < idCodes.length; i++) {
144
+ screens[i] = all.subarray(i * INDEX_WORDS, (i + 1) * INDEX_WORDS);
145
+ }
146
+ return screens;
147
+ }
148
+
149
+ /**
150
+ * Builds the screen of a query: the bits every molecule the query matches has in its
151
+ * {@link getScreen} screen.
152
+ *
153
+ * The query is screened on the fragment OpenChemLib's substructure search actually matches, so an
154
+ * exclude group cutting an aromatic ring and the atom ring sizes of the query are taken into account
155
+ * as the search takes them; a query with a "match formal bond order" bond gets no bits.
156
+ * @param idCodeQuery - The query, as an idcode; read as a fragment whatever its own fragment flag.
157
+ * @returns Sixteen words.
158
+ * @throws {Error} If the query cannot be parsed.
159
+ */
160
+ export function getQueryScreen(idCodeQuery: string): Int32Array {
161
+ return buildQueryWords('screen', idCodeQuery);
162
+ }
163
+
164
+ /**
165
+ * Builds the FragFp fingerprint of a query: the bits every molecule the query matches has in its
166
+ * {@link getIndex} fingerprint.
167
+ *
168
+ * It equals OpenChemLib's own query fingerprint, except for the queries that one wrongly rejects
169
+ * true matches of: an exclude group cutting an aromatic ring, or a "match formal bond order" bond
170
+ * (Actelion/openchemlib#177). The first is fingerprinted on the fragment the search matches; the
171
+ * second gets no bits.
172
+ * @param idCodeQuery - The query, as an idcode; read as a fragment whatever its own fragment flag.
173
+ * @returns Sixteen words.
174
+ * @throws {Error} If the query cannot be parsed.
175
+ */
176
+ export function getQueryIndex(idCodeQuery: string): Int32Array {
177
+ return buildQueryWords('index', idCodeQuery);
178
+ }
179
+
180
+ /**
181
+ * Returns the 512 key fragments of FragFp, in bit order: bit i of {@link getIndex} is set when key i
182
+ * is a substructure of the molecule. They are OpenChemLib's own keys, the list `openchemlib`'s
183
+ * `SSSearcherWithIndex.getKeyIDCode()` returns.
184
+ *
185
+ * Bit i sits in word `i >> 5`, the first key in the highest bit of word 0:
186
+ *
187
+ * ```js
188
+ * const keys = getFragFpKeys();
189
+ * const index = getIndex(idCode);
190
+ * const held = keys.filter((key, i) => index[i >> 5] & (1 << (31 - (i & 31))));
191
+ * ```
192
+ * @returns 512 idcodes, each a query fragment, in bit order. The array is the caller's own.
193
+ */
194
+ export function getFragFpKeys(): string[] {
195
+ return readKeys('index');
196
+ }
197
+
198
+ /**
199
+ * Returns the 512 key fragments of the screen, in bit order: bit i of {@link getScreen} is set when
200
+ * key i is a substructure of the molecule. The words are laid out as {@link getFragFpKeys} shows.
201
+ *
202
+ * The list is frozen: changing, adding or reordering a key changes every stored screen, so it only
203
+ * changes in a major release.
204
+ * @returns 512 idcodes, each a query fragment, in bit order. The array is the caller's own.
205
+ */
206
+ export function getScreenKeys(): string[] {
207
+ return readKeys('screen');
208
+ }
209
+
77
210
  /**
78
211
  * Hashes one idcode to the 64-bit value OpenChemLib identifies a molecule by up to tautomerism and
79
212
  * stereochemistry.
@@ -99,9 +232,21 @@ export function getNoStereoTautomerHash(
99
232
  idCode: string,
100
233
  options: HashOptions = {},
101
234
  ): bigint {
102
- const { largestFragmentOnly = false } = options;
235
+ const {
236
+ largestFragmentOnly = false,
237
+ tautomerCounts,
238
+ maxTautomers = DEFAULT_MAX_TAUTOMERS,
239
+ } = options;
103
240
  const result = new BigInt64Array(1);
104
- buildNoStereoTautomerHashes([idCode], result, largestFragmentOnly, 0, 1);
241
+ buildNoStereoTautomerHashes(
242
+ [idCode],
243
+ result,
244
+ largestFragmentOnly,
245
+ 0,
246
+ 1,
247
+ tautomerCounts,
248
+ maxTautomers,
249
+ );
105
250
  return result[0] as bigint;
106
251
  }
107
252
 
@@ -126,7 +271,11 @@ export function getNoStereoTautomerHashes(
126
271
  idCodes: string[],
127
272
  options: HashOptions = {},
128
273
  ): BigInt64Array {
129
- const { largestFragmentOnly = false } = options;
274
+ const {
275
+ largestFragmentOnly = false,
276
+ tautomerCounts,
277
+ maxTautomers = DEFAULT_MAX_TAUTOMERS,
278
+ } = options;
130
279
  const result = new BigInt64Array(idCodes.length);
131
280
  buildNoStereoTautomerHashes(
132
281
  idCodes,
@@ -134,6 +283,8 @@ export function getNoStereoTautomerHashes(
134
283
  largestFragmentOnly,
135
284
  0,
136
285
  idCodes.length,
286
+ tautomerCounts,
287
+ maxTautomers,
137
288
  );
138
289
  return result;
139
290
  }
@@ -192,3 +343,116 @@ export function getNoStereoHashes(
192
343
  buildNoStereoHashes(idCodes, result, largestFragmentOnly, 0, idCodes.length);
193
344
  return result;
194
345
  }
346
+
347
+ /**
348
+ * Canonizes one idcode to the form OpenChemLib identifies a molecule by up to stereochemistry.
349
+ *
350
+ * This is the string {@link getNoStereoHash} hashes, for a database that stores the canonical form
351
+ * itself. Prefer the hash where the column is only ever compared: it is a signed 64-bit integer, so
352
+ * it stores and indexes in 8 bytes where these idcodes average around 25 characters.
353
+ *
354
+ * ```js
355
+ * getNoStereoIdCode(idCode); // 'gGX`BDdwMT@@'
356
+ * ```
357
+ * @param idCode - The molecule to canonize, as an idcode.
358
+ * @param options - See {@link HashOptions}.
359
+ * @returns Its canonical no-stereo idcode, or `NO_ID_CODE` (`''`) for an idcode that will not parse
360
+ * or a molecule OpenChemLib cannot canonize.
361
+ */
362
+ export function getNoStereoIdCode(
363
+ idCode: string,
364
+ options: HashOptions = {},
365
+ ): string {
366
+ const { largestFragmentOnly = false } = options;
367
+ const result = [NO_ID_CODE];
368
+ buildNoStereoIdCodes([idCode], result, largestFragmentOnly, 0, 1);
369
+ return result[0] as string;
370
+ }
371
+
372
+ /**
373
+ * Canonizes every idcode to the form OpenChemLib identifies a molecule by up to stereochemistry.
374
+ *
375
+ * See {@link getNoStereoIdCode} for what the form identifies.
376
+ * @param idCodes - The molecules to canonize, as idcodes.
377
+ * @param options - See {@link HashOptions}.
378
+ * @returns One idcode per input, in order, with `NO_ID_CODE` (`''`) for each one that could not be
379
+ * canonized.
380
+ */
381
+ export function getNoStereoIdCodes(
382
+ idCodes: string[],
383
+ options: HashOptions = {},
384
+ ): string[] {
385
+ const { largestFragmentOnly = false } = options;
386
+ const result = new Array<string>(idCodes.length).fill(NO_ID_CODE);
387
+ buildNoStereoIdCodes(idCodes, result, largestFragmentOnly, 0, idCodes.length);
388
+ return result;
389
+ }
390
+
391
+ /**
392
+ * Canonizes one idcode to the form OpenChemLib identifies a molecule by up to tautomerism and
393
+ * stereochemistry.
394
+ *
395
+ * This is the string {@link getNoStereoTautomerHash} hashes. It is the generic tautomer's idcode, so
396
+ * it is longer than the molecule's own — around twice it for a molecule with tautomeric sites — and
397
+ * it is not a structure: parsing it back gives a molecule with every tautomeric bond normalized, not
398
+ * the compound. Use it as a key, never as something to draw.
399
+ * @param idCode - The molecule to canonize, as an idcode.
400
+ * @param options - See {@link HashOptions}.
401
+ * @returns Its canonical no-stereo generic-tautomer idcode, or `NO_ID_CODE` (`''`) for an idcode that
402
+ * will not parse or a molecule OpenChemLib cannot canonize.
403
+ */
404
+ export function getNoStereoTautomerIdCode(
405
+ idCode: string,
406
+ options: HashOptions = {},
407
+ ): string {
408
+ const {
409
+ largestFragmentOnly = false,
410
+ tautomerCounts,
411
+ maxTautomers = DEFAULT_MAX_TAUTOMERS,
412
+ } = options;
413
+ const result = [NO_ID_CODE];
414
+ buildNoStereoTautomerIdCodes(
415
+ [idCode],
416
+ result,
417
+ largestFragmentOnly,
418
+ 0,
419
+ 1,
420
+ tautomerCounts,
421
+ maxTautomers,
422
+ );
423
+ return result[0] as string;
424
+ }
425
+
426
+ /**
427
+ * Canonizes every idcode to the form OpenChemLib identifies a molecule by up to tautomerism and
428
+ * stereochemistry.
429
+ *
430
+ * See {@link getNoStereoTautomerIdCode} for what the form identifies, and
431
+ * {@link getNoStereoTautomerHashes} for what it costs — the canonization is the expensive part, not
432
+ * the hashing, so these cost what those do.
433
+ * @param idCodes - The molecules to canonize, as idcodes.
434
+ * @param options - See {@link HashOptions}.
435
+ * @returns One idcode per input, in order, with `NO_ID_CODE` (`''`) for each one that could not be
436
+ * canonized.
437
+ */
438
+ export function getNoStereoTautomerIdCodes(
439
+ idCodes: string[],
440
+ options: HashOptions = {},
441
+ ): string[] {
442
+ const {
443
+ largestFragmentOnly = false,
444
+ tautomerCounts,
445
+ maxTautomers = DEFAULT_MAX_TAUTOMERS,
446
+ } = options;
447
+ const result = new Array<string>(idCodes.length).fill(NO_ID_CODE);
448
+ buildNoStereoTautomerIdCodes(
449
+ idCodes,
450
+ result,
451
+ largestFragmentOnly,
452
+ 0,
453
+ idCodes.length,
454
+ tautomerCounts,
455
+ maxTautomers,
456
+ );
457
+ return result;
458
+ }
package/src/records.ts ADDED
@@ -0,0 +1,142 @@
1
+ import { recoveringFromTraps } from './scan.ts';
2
+ import type {
3
+ MoleculeRecords,
4
+ MoleculeRecordsOptions,
5
+ MoleculeSizes,
6
+ } from './types.ts';
7
+ import {
8
+ DEFAULT_MAX_TAUTOMERS,
9
+ NO_HASH,
10
+ NO_ID_CODE,
11
+ RECORD_WORDS,
12
+ } from './types.ts';
13
+ import { loadOCL } from './wasm/load.ts';
14
+
15
+ const { Search } = await loadOCL();
16
+
17
+ /**
18
+ * Computes everything a molecule table stores about each idcode, parsing each one once: the
19
+ * canonical idcode, the FragFp, the screen and the function bits, the atom count and the relative
20
+ * weight, and the no-stereo and tautomer hashes.
21
+ *
22
+ * Each value is the one the dedicated function returns — `getIndexes`, `getScreens`, `getFunctions`,
23
+ * `getNoStereoHashes`, `getNoStereoTautomerHashes` — with one deliberate exception: a molecule
24
+ * whose tautomer enumeration reached `maxTautomers` gets its no-stereo hash as its tautomer hash,
25
+ * because a key from a truncated enumeration depends on where it stopped and is not canonical.
26
+ * `tautomerCounts` says which molecules those are.
27
+ *
28
+ * ```js
29
+ * const records = getMoleculeRecords(idCodes, { maxTautomers: 5000 });
30
+ * const words = records.words.subarray(48 * i, 48 * (i + 1)); // FragFp, screen, functions
31
+ * ```
32
+ * @param idCodes - The molecules to read, as idcodes.
33
+ * @param options - See {@link MoleculeRecordsOptions}.
34
+ * @returns One entry per idcode in each array, in input order. A molecule that cannot be read gets
35
+ * `NO_ID_CODE`, zeros, 0 atoms, a `NaN` weight, `NO_HASH` and a tautomer count of 0, and does not
36
+ * cost the others in the batch.
37
+ */
38
+ export function getMoleculeRecords(
39
+ idCodes: string[],
40
+ options: MoleculeRecordsOptions = {},
41
+ ): MoleculeRecords {
42
+ const { maxTautomers = DEFAULT_MAX_TAUTOMERS } = options;
43
+ const count = idCodes.length;
44
+ const records: MoleculeRecords = {
45
+ idCodes: new Array<string>(count).fill(NO_ID_CODE),
46
+ words: new Int32Array(count * RECORD_WORDS),
47
+ atomCounts: new Int32Array(count),
48
+ weights: new Float64Array(count).fill(Number.NaN),
49
+ noStereoHashes: new BigInt64Array(count),
50
+ tautomerHashes: new BigInt64Array(count),
51
+ tautomerCounts: new Int32Array(count),
52
+ };
53
+ recoveringFromTraps(
54
+ (from, to) =>
55
+ Search.getMoleculeRecords(
56
+ idCodes,
57
+ records.idCodes,
58
+ records.words,
59
+ records.atomCounts,
60
+ records.weights,
61
+ records.noStereoHashes,
62
+ records.tautomerHashes,
63
+ records.tautomerCounts,
64
+ maxTautomers,
65
+ from,
66
+ to,
67
+ ),
68
+ (index) => markUnreadable(records, index),
69
+ 0,
70
+ count,
71
+ );
72
+ return records;
73
+ }
74
+
75
+ /**
76
+ * Reads the atom count and the relative weight of each idcode — exactly the `atomCounts` and
77
+ * `weights` of {@link getMoleculeRecords}, and nothing else, so it costs one parse and one formula
78
+ * per molecule.
79
+ * @param idCodes - The molecules to read, as idcodes.
80
+ * @returns One entry per idcode in each array, in input order: 0 atoms and a `NaN` weight for a
81
+ * molecule that cannot be read.
82
+ */
83
+ export function getMoleculeSizes(idCodes: string[]): MoleculeSizes {
84
+ const count = idCodes.length;
85
+ const sizes: MoleculeSizes = {
86
+ atomCounts: new Int32Array(count),
87
+ weights: new Float64Array(count).fill(Number.NaN),
88
+ };
89
+ recoveringFromTraps(
90
+ (from, to) =>
91
+ Search.getMoleculeSizes(
92
+ idCodes,
93
+ sizes.atomCounts,
94
+ sizes.weights,
95
+ from,
96
+ to,
97
+ ),
98
+ (index) => {
99
+ sizes.atomCounts[index] = 0;
100
+ sizes.weights[index] = Number.NaN;
101
+ },
102
+ 0,
103
+ count,
104
+ );
105
+ return sizes;
106
+ }
107
+
108
+ /**
109
+ * The fewest atoms a molecule needs to contain the query: its atoms once the exclude groups are
110
+ * removed, plain hydrogens aside. Each of them maps onto a distinct atom of any match, so a molecule
111
+ * whose atom count is below the floor cannot match and needs no substructure test.
112
+ *
113
+ * Wildcards and atom lists count — each still takes an atom of the match. A plain hydrogen does not,
114
+ * because it may match an implicit one; a deuterium or a tritium does.
115
+ * @param idCodeQuery - The query, as an idcode; read as a fragment whatever its own fragment flag.
116
+ * @returns The floor, or 0 for a query with no atoms.
117
+ * @throws {Error} If the query cannot be parsed.
118
+ */
119
+ export function getQueryAtomFloor(idCodeQuery: string): number {
120
+ try {
121
+ return Search.getQueryAtomFloor(idCodeQuery);
122
+ } catch (error) {
123
+ throw new Error(`"${idCodeQuery}" is not a valid query idcode`, {
124
+ cause: error,
125
+ });
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Writes the "could not read this" record of one molecule over whatever a trap left there.
131
+ * @param records - The records being filled.
132
+ * @param index - The molecule's position.
133
+ */
134
+ function markUnreadable(records: MoleculeRecords, index: number): void {
135
+ records.idCodes[index] = NO_ID_CODE;
136
+ records.words.fill(0, index * RECORD_WORDS, (index + 1) * RECORD_WORDS);
137
+ records.atomCounts[index] = 0;
138
+ records.weights[index] = Number.NaN;
139
+ records.noStereoHashes[index] = NO_HASH;
140
+ records.tautomerHashes[index] = NO_HASH;
141
+ records.tautomerCounts[index] = 0;
142
+ }