openchemlib-search-wasm 2.1.2 → 2.1.3

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 (78) hide show
  1. package/LICENSE +451 -25
  2. package/README.md +12 -5
  3. package/lib/functionKeys/index.d.ts +14 -80
  4. package/lib/functionKeys/index.js +3 -98
  5. package/lib/index.d.ts +118 -258
  6. package/lib/index.js +33 -327
  7. package/package.json +24 -32
  8. package/lib/functionKeys/dictionary.json +0 -72071
  9. package/lib/functionKeys/index.d.ts.map +0 -1
  10. package/lib/functionKeys/index.js.map +0 -1
  11. package/lib/functions.d.ts +0 -38
  12. package/lib/functions.d.ts.map +0 -1
  13. package/lib/functions.js +0 -54
  14. package/lib/functions.js.map +0 -1
  15. package/lib/hex.d.ts +0 -31
  16. package/lib/hex.d.ts.map +0 -1
  17. package/lib/hex.js +0 -41
  18. package/lib/hex.js.map +0 -1
  19. package/lib/index.d.ts.map +0 -1
  20. package/lib/index.js.map +0 -1
  21. package/lib/jpath.d.ts +0 -28
  22. package/lib/jpath.d.ts.map +0 -1
  23. package/lib/jpath.js +0 -73
  24. package/lib/jpath.js.map +0 -1
  25. package/lib/records.d.ts +0 -45
  26. package/lib/records.d.ts.map +0 -1
  27. package/lib/records.js +0 -96
  28. package/lib/records.js.map +0 -1
  29. package/lib/runSearch.d.ts +0 -41
  30. package/lib/runSearch.d.ts.map +0 -1
  31. package/lib/runSearch.js +0 -50
  32. package/lib/runSearch.js.map +0 -1
  33. package/lib/scan.d.ts +0 -159
  34. package/lib/scan.d.ts.map +0 -1
  35. package/lib/scan.js +0 -281
  36. package/lib/scan.js.map +0 -1
  37. package/lib/similaritySearch.d.ts +0 -28
  38. package/lib/similaritySearch.d.ts.map +0 -1
  39. package/lib/similaritySearch.js +0 -86
  40. package/lib/similaritySearch.js.map +0 -1
  41. package/lib/strongHash.d.ts +0 -9
  42. package/lib/strongHash.d.ts.map +0 -1
  43. package/lib/strongHash.js +0 -56
  44. package/lib/strongHash.js.map +0 -1
  45. package/lib/substructureSearch.d.ts +0 -29
  46. package/lib/substructureSearch.d.ts.map +0 -1
  47. package/lib/substructureSearch.js +0 -104
  48. package/lib/substructureSearch.js.map +0 -1
  49. package/lib/types.d.ts +0 -308
  50. package/lib/types.d.ts.map +0 -1
  51. package/lib/types.js +0 -52
  52. package/lib/types.js.map +0 -1
  53. package/lib/wasm/load.d.ts +0 -10
  54. package/lib/wasm/load.d.ts.map +0 -1
  55. package/lib/wasm/load.js +0 -50
  56. package/lib/wasm/load.js.map +0 -1
  57. package/lib/wasm/log.d.ts +0 -36
  58. package/lib/wasm/log.d.ts.map +0 -1
  59. package/lib/wasm/log.js +0 -52
  60. package/lib/wasm/log.js.map +0 -1
  61. package/src/functionKeys/index.ts +0 -181
  62. package/src/functions.ts +0 -56
  63. package/src/hex.ts +0 -43
  64. package/src/index.ts +0 -458
  65. package/src/jpath.ts +0 -77
  66. package/src/records.ts +0 -142
  67. package/src/runSearch.ts +0 -99
  68. package/src/scan.ts +0 -441
  69. package/src/similaritySearch.ts +0 -104
  70. package/src/strongHash.ts +0 -57
  71. package/src/substructureSearch.ts +0 -120
  72. package/src/types.ts +0 -403
  73. package/src/wasm/load.ts +0 -59
  74. package/src/wasm/log.ts +0 -66
  75. package/wasm/data.d.ts +0 -2
  76. package/wasm/data.js +0 -3
  77. package/wasm/runtime.d.ts +0 -31
  78. package/wasm/runtime.js +0 -30
package/src/index.ts DELETED
@@ -1,458 +0,0 @@
1
- import {
2
- buildIndexes,
3
- buildNoStereoHashes,
4
- buildNoStereoIdCodes,
5
- buildNoStereoTautomerHashes,
6
- buildNoStereoTautomerIdCodes,
7
- buildQueryWords,
8
- buildScreens,
9
- readKeys,
10
- } from './scan.ts';
11
- import type { HashOptions } from './types.ts';
12
- import { DEFAULT_MAX_TAUTOMERS, INDEX_WORDS, NO_ID_CODE } from './types.ts';
13
-
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';
18
- export { similaritySearch } from './similaritySearch.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';
37
- export type {
38
- HashOptions,
39
- MoleculeRecords,
40
- MoleculeRecordsOptions,
41
- MoleculeSizes,
42
- ResultBuffer,
43
- SearchMode,
44
- SearchOptions,
45
- SearchResult,
46
- SearchStep,
47
- SimilaritySearchOptions,
48
- SimilaritySearchResult,
49
- SubstructureSearchOptions,
50
- SubstructureSearchResult,
51
- TautomerHashOptions,
52
- } from './types.ts';
53
- export { SimilarityResult, SubstructureResult } from './types.ts';
54
-
55
- /**
56
- * Builds the 512-bit FragFp fingerprint of one idcode, as sixteen 32-bit words.
57
- *
58
- * This is what a fingerprint table is made of, and building it is the expensive part of importing a
59
- * library: about 897 µs per molecule, against 4484 µs for `openchemlib-js` — six minutes instead of
60
- * thirty for 400,000 molecules. Calling this once per molecule costs the same as one batched call,
61
- * so a row-at-a-time importer needs no restructuring to get that.
62
- *
63
- * The words are in the same order `openchemlib-js`'s `createIndex` produces, so they are
64
- * interchangeable with an existing table, and a `BigInt64Array` view over the result is exactly the
65
- * eight columns `openchemlib-sqlite` stores — no conversion, no copy:
66
- *
67
- * ```js
68
- * const index = getIndex(idCode);
69
- * const columns = new BigInt64Array(index.buffer, index.byteOffset, 8); // ss_index0..7
70
- * insert.run(mw, entryId, ...columns);
71
- * ```
72
- * @param idCode - The molecule to fingerprint, as an idcode.
73
- * @returns Sixteen words. An idcode that will not parse gives sixteen zeros, which no non-empty
74
- * query is a subset of, so it can never be a false candidate.
75
- */
76
- export function getIndex(idCode: string): Int32Array {
77
- const result = new Int32Array(INDEX_WORDS);
78
- buildIndexes([idCode], result, 0, 1);
79
- return result;
80
- }
81
-
82
- /**
83
- * Builds the 512-bit FragFp fingerprint of every idcode, sixteen 32-bit words each.
84
- *
85
- * See {@link getIndex} for the one-molecule form, which costs the same per molecule: the 512 key
86
- * fragments OpenChemLib matches against are parsed once and held for the module's lifetime, so
87
- * there is nothing for a batch to amortise.
88
- *
89
- * The fingerprints are written into one buffer and returned as a view per molecule, so the array
90
- * costs one allocation rather than one per molecule, and every view is still 8-byte aligned and can
91
- * be read as the eight `BigInt64` columns {@link getIndex} shows.
92
- * @param idCodes - The molecules to fingerprint, as idcodes.
93
- * @returns One sixteen-word view per idcode, in order.
94
- */
95
- export function getIndexes(idCodes: string[]): Int32Array[] {
96
- const all = new Int32Array(idCodes.length * INDEX_WORDS);
97
- buildIndexes(idCodes, all, 0, idCodes.length);
98
- const indexes = new Array<Int32Array>(idCodes.length);
99
- for (let i = 0; i < idCodes.length; i++) {
100
- indexes[i] = all.subarray(i * INDEX_WORDS, (i + 1) * INDEX_WORDS);
101
- }
102
- return indexes;
103
- }
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
-
210
- /**
211
- * Hashes one idcode to the 64-bit value OpenChemLib identifies a molecule by up to tautomerism and
212
- * stereochemistry.
213
- *
214
- * Every tautomer of a structure and every stereoisomer of it give the same hash, so two records key
215
- * to the same value whether the compound was drawn as the keto or the enol form, and whether or not
216
- * its stereo centres were assigned. That is what makes it the column to look a compound up by when
217
- * "the same molecule" means the same constitution rather than the same drawing.
218
- *
219
- * It is OpenChemLib's own `CanonizerUtil.getNoStereoTautomerHash`, so the value matches what any
220
- * other OpenChemLib build computes, and it is a signed 64-bit integer — what an SQLite `INTEGER`
221
- * column stores and indexes directly:
222
- *
223
- * ```js
224
- * insert.run(entryId, getNoStereoTautomerHash(idCode));
225
- * ```
226
- * @param idCode - The molecule to hash, as an idcode.
227
- * @param options - See {@link HashOptions}.
228
- * @returns Its hash, or `NO_HASH` (`0n`) for an idcode that will not parse or a molecule
229
- * OpenChemLib cannot canonize.
230
- */
231
- export function getNoStereoTautomerHash(
232
- idCode: string,
233
- options: HashOptions = {},
234
- ): bigint {
235
- const {
236
- largestFragmentOnly = false,
237
- tautomerCounts,
238
- maxTautomers = DEFAULT_MAX_TAUTOMERS,
239
- } = options;
240
- const result = new BigInt64Array(1);
241
- buildNoStereoTautomerHashes(
242
- [idCode],
243
- result,
244
- largestFragmentOnly,
245
- 0,
246
- 1,
247
- tautomerCounts,
248
- maxTautomers,
249
- );
250
- return result[0] as bigint;
251
- }
252
-
253
- /**
254
- * Hashes every idcode to its no-stereo tautomer hash, one 64-bit value each.
255
- *
256
- * See {@link getNoStereoTautomerHash} for what the hash identifies. A drug-like molecule costs
257
- * around 130 µs, against 520 µs for `openchemlib-js`, and almost all of that is canonizing the
258
- * generic tautomer rather than the hashing itself. That cost is nothing like uniform: it is driven
259
- * by how many tautomeric sites the molecule has, and one with a great many can take the better part
260
- * of a second before OpenChemLib abandons the enumeration — so a library import belongs in a worker
261
- * rather than on the main thread.
262
- *
263
- * The hashes come back in one `BigInt64Array` rather than one value per molecule, so a table of
264
- * them costs a single allocation and can be written straight into a column.
265
- * @param idCodes - The molecules to hash, as idcodes.
266
- * @param options - See {@link HashOptions}.
267
- * @returns One hash per idcode, in order, with `NO_HASH` (`0n`) for each one that could not be
268
- * hashed.
269
- */
270
- export function getNoStereoTautomerHashes(
271
- idCodes: string[],
272
- options: HashOptions = {},
273
- ): BigInt64Array {
274
- const {
275
- largestFragmentOnly = false,
276
- tautomerCounts,
277
- maxTautomers = DEFAULT_MAX_TAUTOMERS,
278
- } = options;
279
- const result = new BigInt64Array(idCodes.length);
280
- buildNoStereoTautomerHashes(
281
- idCodes,
282
- result,
283
- largestFragmentOnly,
284
- 0,
285
- idCodes.length,
286
- tautomerCounts,
287
- maxTautomers,
288
- );
289
- return result;
290
- }
291
-
292
- /**
293
- * Hashes one idcode to the 64-bit value OpenChemLib identifies a molecule by up to stereochemistry.
294
- *
295
- * Every stereoisomer of a structure gives the same hash, and nothing else does: unlike
296
- * {@link getNoStereoTautomerHash} the tautomers stay apart, so the keto and the enol form of a
297
- * compound key differently. That makes it the narrower of the two identity columns — the one to use
298
- * when a record's stereo centres are unreliable but the bonds it was drawn with are not.
299
- *
300
- * It is OpenChemLib's own `CanonizerUtil.getNoStereoHash`, so the value matches what any other
301
- * OpenChemLib build computes, and it is a signed 64-bit integer — what an SQLite `INTEGER` column
302
- * stores and indexes directly:
303
- *
304
- * ```js
305
- * insert.run(entryId, getNoStereoHash(idCode));
306
- * ```
307
- * @param idCode - The molecule to hash, as an idcode.
308
- * @param options - See {@link HashOptions}.
309
- * @returns Its hash, or `NO_HASH` (`0n`) for an idcode that will not parse or a molecule
310
- * OpenChemLib cannot canonize.
311
- */
312
- export function getNoStereoHash(
313
- idCode: string,
314
- options: HashOptions = {},
315
- ): bigint {
316
- const { largestFragmentOnly = false } = options;
317
- const result = new BigInt64Array(1);
318
- buildNoStereoHashes([idCode], result, largestFragmentOnly, 0, 1);
319
- return result[0] as bigint;
320
- }
321
-
322
- /**
323
- * Hashes every idcode to its no-stereo hash, one 64-bit value each.
324
- *
325
- * See {@link getNoStereoHash} for what the hash identifies. It costs a fraction of
326
- * {@link getNoStereoTautomerHashes} because it canonizes the molecule itself instead of enumerating
327
- * its tautomers, and its cost is even across a library rather than driven by how tautomeric each
328
- * molecule happens to be.
329
- *
330
- * The hashes come back in one `BigInt64Array` rather than one value per molecule, so a table of
331
- * them costs a single allocation and can be written straight into a column.
332
- * @param idCodes - The molecules to hash, as idcodes.
333
- * @param options - See {@link HashOptions}.
334
- * @returns One hash per idcode, in order, with `NO_HASH` (`0n`) for each one that could not be
335
- * hashed.
336
- */
337
- export function getNoStereoHashes(
338
- idCodes: string[],
339
- options: HashOptions = {},
340
- ): BigInt64Array {
341
- const { largestFragmentOnly = false } = options;
342
- const result = new BigInt64Array(idCodes.length);
343
- buildNoStereoHashes(idCodes, result, largestFragmentOnly, 0, idCodes.length);
344
- return result;
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/jpath.ts DELETED
@@ -1,77 +0,0 @@
1
- /**
2
- * Reads the idcode every entry carries, following a jpath.
3
- *
4
- * The jpath is the dot-separated form the rest of the ecosystem uses (`get-jpaths`,
5
- * `derived-props`): `idCode`, `molecule.idCode`, `spectra.0.idCode`. A numeric segment indexes an
6
- * array, because `array['0']` and `array[0]` are the same property.
7
- *
8
- * The path is split once and walked with a plain loop per entry, so a scan of 400,000 entries costs
9
- * one string split rather than 400,000 of them.
10
- * @param entries - The entries to read.
11
- * @param jpath - Where the idcode sits in each entry.
12
- * @returns One idcode per entry, in order. An entry with no string at the jpath gives an empty
13
- * string, which the scan reports as unparsable like any other idcode it cannot read.
14
- * @throws {Error} If no entry at all has a string there, which means the jpath is wrong rather than
15
- * the data.
16
- */
17
- export function readIdCodes(
18
- entries: readonly unknown[],
19
- jpath: string,
20
- ): string[] {
21
- const segments = jpath.split('.');
22
- const idCodes = new Array<string>(entries.length);
23
- let found = 0;
24
-
25
- if (segments.length === 1) {
26
- const key = segments[0] as string;
27
- for (let i = 0; i < entries.length; i++) {
28
- const value = (entries[i] as Record<string, unknown> | null)?.[key];
29
- if (typeof value === 'string') {
30
- idCodes[i] = value;
31
- found++;
32
- } else {
33
- idCodes[i] = '';
34
- }
35
- }
36
- } else {
37
- for (let i = 0; i < entries.length; i++) {
38
- let value: unknown = entries[i];
39
- for (const segment of segments) {
40
- if (value === null || value === undefined) break;
41
- value = (value as Record<string, unknown>)[segment];
42
- }
43
- if (typeof value === 'string') {
44
- idCodes[i] = value;
45
- found++;
46
- } else {
47
- idCodes[i] = '';
48
- }
49
- }
50
- }
51
-
52
- if (found === 0 && entries.length > 0) {
53
- throw new Error(
54
- `no entry holds a string at jpath "${jpath}": pass the jpath option if the idcode is` +
55
- ' somewhere else',
56
- );
57
- }
58
- return idCodes;
59
- }
60
-
61
- /**
62
- * Turns whatever the caller passed into the array of idcodes the scan reads.
63
- *
64
- * An array of idcodes is used as it is — that is the hot path a worker scanning a decoded chunk
65
- * takes, and copying it would allocate one reference per molecule for nothing.
66
- * @param entries - Idcodes, or entries carrying one at `jpath`.
67
- * @param jpath - Where the idcode sits in an entry.
68
- * @returns The idcodes to scan.
69
- */
70
- export function toIdCodes(
71
- entries: readonly unknown[],
72
- jpath: string,
73
- ): string[] {
74
- if (entries.length === 0) return [];
75
- if (typeof entries[0] === 'string') return entries as unknown as string[];
76
- return readIdCodes(entries, jpath);
77
- }
package/src/records.ts DELETED
@@ -1,142 +0,0 @@
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
- }