openchemlib-search-wasm 2.1.1 → 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/lib/scan.d.ts DELETED
@@ -1,159 +0,0 @@
1
- import type { ResultBuffer, SearchMode } from './types.ts';
2
- /**
3
- * Scans `idCodes[from .. to)` into `result`, recording an entry the parser chokes on and carrying on.
4
- *
5
- * The range is passed through to WebAssembly rather than sliced in JavaScript, because the idcodes
6
- * are read out of the JS array one at a time: a caller can scan the same array in as many pieces as
7
- * it likes without re-converting it.
8
- * @param mode - Which search to run.
9
- * @param idCodeQuery - The query, as an idcode.
10
- * @param idCodes - The molecules to scan.
11
- * @param result - The caller's buffer, already reset.
12
- * @param from - The first index to scan.
13
- * @param to - One past the last index to scan.
14
- * @throws {Error} If more than 100 idcodes in the range cannot be parsed.
15
- */
16
- export declare function scanRange(mode: SearchMode, idCodeQuery: string, idCodes: string[], result: ResultBuffer, from: number, to: number): void;
17
- /**
18
- * Parses the query against an empty range, so a query the parser chokes on is reported as such.
19
- *
20
- * Without it the recovery above would blame the molecules for a bad query and walk the whole array
21
- * one wasted call at a time.
22
- * @param mode - Which search the query is for.
23
- * @param idCodeQuery - The query, as an idcode.
24
- * @throws {Error} If the query cannot be parsed.
25
- */
26
- export declare function checkQuery(mode: SearchMode, idCodeQuery: string): void;
27
- /**
28
- * Fingerprints `idCodes[from .. to)` into `result`, sixteen words per molecule.
29
- *
30
- * A molecule that cannot be read gets sixteen zeros, which no non-empty query is a subset of, so it
31
- * is never a false candidate.
32
- * @param idCodes - The molecules to fingerprint.
33
- * @param result - The caller's buffer, `16 * idCodes.length` words long.
34
- * @param from - The first index to fingerprint.
35
- * @param to - One past the last index to fingerprint.
36
- * @returns How many molecules were parsed and fingerprinted.
37
- */
38
- export declare function buildIndexes(idCodes: string[], result: Int32Array, from: number, to: number): number;
39
- /**
40
- * Screens `idCodes[from .. to)` into `result`, sixteen words per molecule.
41
- *
42
- * Fails like {@link buildIndexes}: sixteen zeros for an idcode the WASM side cannot read.
43
- * @param idCodes - The molecules to screen.
44
- * @param result - The caller's buffer, `16 * idCodes.length` words long.
45
- * @param from - The first index to screen.
46
- * @param to - One past the last index to screen.
47
- * @returns How many molecules were parsed and screened.
48
- */
49
- export declare function buildScreens(idCodes: string[], result: Int32Array, from: number, to: number): number;
50
- /**
51
- * Builds the function bits of `idCodes[from .. to)` into `result`, sixteen words per molecule.
52
- *
53
- * Fails like {@link buildIndexes}: sixteen zeros for an idcode the WASM side cannot read.
54
- * @param idCodes - The molecules to read.
55
- * @param result - The caller's buffer, `16 * idCodes.length` words long.
56
- * @param from - The first index to read.
57
- * @param to - One past the last index to read.
58
- * @returns How many molecules were parsed and read.
59
- */
60
- export declare function buildFunctions(idCodes: string[], result: Int32Array, from: number, to: number): number;
61
- /**
62
- * Reads the fragments of the 512 stored function keys, in bit order.
63
- * @returns Per key, its fragment idcodes.
64
- */
65
- export declare function readFunctionKeys(): string[][];
66
- /**
67
- * Builds what every molecule a query matches has: its FragFp words, or its screen words.
68
- * @param kind - `'index'` for the FragFp words, `'screen'` for the screen words.
69
- * @param idCodeQuery - The query, as an idcode.
70
- * @returns Sixteen words.
71
- * @throws {Error} If the query cannot be parsed.
72
- */
73
- export declare function buildQueryWords(kind: 'index' | 'screen', idCodeQuery: string): Int32Array;
74
- /**
75
- * Reads the key fragments behind the bits of the FragFp words, or of the screen words.
76
- * @param kind - `'index'` for FragFp's keys, `'screen'` for the screen's.
77
- * @returns 512 idcodes, in bit order.
78
- */
79
- export declare function readKeys(kind: 'index' | 'screen'): string[];
80
- /**
81
- * Hashes `idCodes[from .. to)` into `result`, one no-stereo tautomer hash per molecule.
82
- *
83
- * A molecule that cannot be read gets `NO_HASH`, which is also what OpenChemLib returns for one it
84
- * cannot canonize, so the two failures are reported the same way.
85
- * @param idCodes - The molecules to hash.
86
- * @param result - The caller's buffer, `idCodes.length` entries long.
87
- * @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
88
- * @param from - The first index to hash.
89
- * @param to - One past the last index to hash.
90
- * @param tautomerCounts - Filled with how many tautomers each molecule needed; an entry that reached
91
- * `maxTautomers` is one OpenChemLib stopped early on.
92
- * @param maxTautomers - The ceiling on that enumeration, which is what bounds the cost.
93
- * @returns How many molecules were hashed.
94
- */
95
- export declare function buildNoStereoTautomerHashes(idCodes: string[], result: BigInt64Array, largestFragmentOnly: boolean, from: number, to: number, tautomerCounts?: Int32Array, maxTautomers?: number): number;
96
- /**
97
- * Hashes `idCodes[from .. to)` into `result`, one no-stereo hash per molecule.
98
- *
99
- * Fails the same way {@link buildNoStereoTautomerHashes} does: `0` for an idcode the WASM side
100
- * cannot read and for a molecule OpenChemLib cannot canonize.
101
- * @param idCodes - The molecules to hash.
102
- * @param result - The caller's buffer, `idCodes.length` entries long.
103
- * @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
104
- * @param from - The first index to hash.
105
- * @param to - One past the last index to hash.
106
- * @returns How many molecules were hashed.
107
- */
108
- export declare function buildNoStereoHashes(idCodes: string[], result: BigInt64Array, largestFragmentOnly: boolean, from: number, to: number): number;
109
- /**
110
- * Canonizes `idCodes[from .. to)` into `result`, one no-stereo idcode per molecule.
111
- *
112
- * This is the string {@link buildNoStereoHashes} hashes, for a caller that stores the canonical form
113
- * rather than a 64-bit key. A molecule that cannot be read keeps the `NO_ID_CODE` the caller's array
114
- * was filled with.
115
- * @param idCodes - The molecules to canonize.
116
- * @param result - The caller's array, `idCodes.length` entries long, filled with `NO_ID_CODE`.
117
- * @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
118
- * @param from - The first index to canonize.
119
- * @param to - One past the last index to canonize.
120
- * @returns How many molecules were canonized.
121
- */
122
- export declare function buildNoStereoIdCodes(idCodes: string[], result: string[], largestFragmentOnly: boolean, from: number, to: number): number;
123
- /**
124
- * Canonizes `idCodes[from .. to)` into `result`, one no-stereo generic-tautomer idcode per molecule.
125
- *
126
- * Fails the same way {@link buildNoStereoIdCodes} does.
127
- * @param idCodes - The molecules to canonize.
128
- * @param result - The caller's array, `idCodes.length` entries long, filled with `NO_ID_CODE`.
129
- * @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
130
- * @param from - The first index to canonize.
131
- * @param to - One past the last index to canonize.
132
- * @param tautomerCounts - Filled with how many tautomers each molecule needed; an entry that reached
133
- * `maxTautomers` is one OpenChemLib stopped early on.
134
- * @param maxTautomers - The ceiling on that enumeration, which is what bounds the cost.
135
- * @returns How many molecules were canonized.
136
- */
137
- export declare function buildNoStereoTautomerIdCodes(idCodes: string[], result: string[], largestFragmentOnly: boolean, from: number, to: number, tautomerCounts?: Int32Array, maxTautomers?: number): number;
138
- /**
139
- * Runs a ranged WASM call, and redoes the range one molecule at a time if it traps.
140
- *
141
- * A malformed idcode makes OpenChemLib's bit-stream parser read past its input, which WebAssembly
142
- * traps on — the same trap {@link scanRange} recovers from. The Java guard catches an empty or
143
- * non-ASCII idcode, but a malformed ASCII one reaches the parser and traps there, which would
144
- * otherwise lose every molecule in the range including the good ones. The trap leaves the module
145
- * usable, and a per-molecule call costs what a batched one does because the 512 key fragments are
146
- * parsed once for the module's lifetime, so redoing the range singly costs nothing that was not
147
- * already going to be spent.
148
- *
149
- * There is no cap on the failures: these functions promise a per-molecule "could not read this"
150
- * answer rather than an exception, so an array that is entirely garbage yields one of those per
151
- * entry.
152
- * @param call - Runs the WASM call over a range, returning how many molecules it handled.
153
- * @param markUnreadable - Writes the "could not read this" answer for one molecule.
154
- * @param from - The first index to handle.
155
- * @param to - One past the last index to handle.
156
- * @returns How many molecules were handled.
157
- */
158
- export declare function recoveringFromTraps(call: (from: number, to: number) => number, markUnreadable: (index: number) => void, from: number, to: number): number;
159
- //# sourceMappingURL=scan.d.ts.map
package/lib/scan.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"scan.d.ts","sourceRoot":"","sources":["../src/scan.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAqB3D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,UAAU,EAChB,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM,EAAE,EACjB,MAAM,EAAE,YAAY,EACpB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACT,IAAI,CA0BN;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI,CAStE;AAwCD;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,MAAM,EAAE,EACjB,MAAM,EAAE,UAA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,OAAO,EAC5B,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACT,MAAM,CAgBR;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,4BAA4B,CAC1C,OAAO,EAAE,MAAM,EAAE,EACjB,MAAM,EAAE,MAAM,EAAE,EAChB,mBAAmB,EAAE,OAAO,EAC5B,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,cAAc,GAAE,UAA2C,EAC3D,YAAY,GAAE,MAA8B,GAC3C,MAAM,CAkBR;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,KAAK,MAAM,EAC1C,cAAc,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,EACvC,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACT,MAAM,CAcR"}
package/lib/scan.js DELETED
@@ -1,281 +0,0 @@
1
- import { DEFAULT_MAX_TAUTOMERS, INDEX_WORDS, NO_HASH, NO_ID_CODE, SimilarityResult, SubstructureResult, } from "./types.js";
2
- import { loadOCL } from "./wasm/load.js";
3
- // Top-level await: the WASM module is instantiated when this module is imported, so the searches are
4
- // plain calls. A worker pays it once, at startup, for about 25 ms from the built package.
5
- const { Search } = await loadOCL();
6
- // A malformed idcode makes OpenChemLib's bit-stream parser read past its input, which WebAssembly
7
- // traps on. The trap unwinds into JS and leaves the module usable, so the entry it died on is
8
- // recorded as unparsable and the scan resumes after it. Beyond this many, the input is not a list
9
- // of idcodes at all and guessing further would cost one call per entry.
10
- const MAX_UNPARSABLE = 100;
11
- /**
12
- * Scans `idCodes[from .. to)` into `result`, recording an entry the parser chokes on and carrying on.
13
- *
14
- * The range is passed through to WebAssembly rather than sliced in JavaScript, because the idcodes
15
- * are read out of the JS array one at a time: a caller can scan the same array in as many pieces as
16
- * it likes without re-converting it.
17
- * @param mode - Which search to run.
18
- * @param idCodeQuery - The query, as an idcode.
19
- * @param idCodes - The molecules to scan.
20
- * @param result - The caller's buffer, already reset.
21
- * @param from - The first index to scan.
22
- * @param to - One past the last index to scan.
23
- * @throws {Error} If more than 100 idcodes in the range cannot be parsed.
24
- */
25
- export function scanRange(mode, idCodeQuery, idCodes, result, from, to) {
26
- const unparsable = mode === 'similarity'
27
- ? SimilarityResult.unparsable
28
- : SubstructureResult.unparsable;
29
- let cursor = from;
30
- let failures = 0;
31
- while (cursor < to) {
32
- try {
33
- run(mode, idCodeQuery, idCodes, result, cursor, to);
34
- return;
35
- }
36
- catch (error) {
37
- const failed = firstPending(mode, result, cursor, to);
38
- if (failed === -1 || failures >= MAX_UNPARSABLE) {
39
- throw new Error(`the scan failed at index ${failed === -1 ? cursor : failed} after ${failures}` +
40
- ` unparsable idcode(s): check that "${idCodeQuery}" is a valid query idcode and that` +
41
- ' idCodes holds idcodes', { cause: error });
42
- }
43
- result[failed] = unparsable;
44
- failures++;
45
- cursor = failed + 1;
46
- }
47
- }
48
- }
49
- /**
50
- * Parses the query against an empty range, so a query the parser chokes on is reported as such.
51
- *
52
- * Without it the recovery above would blame the molecules for a bad query and walk the whole array
53
- * one wasted call at a time.
54
- * @param mode - Which search the query is for.
55
- * @param idCodeQuery - The query, as an idcode.
56
- * @throws {Error} If the query cannot be parsed.
57
- */
58
- export function checkQuery(mode, idCodeQuery) {
59
- const empty = mode === 'similarity' ? new Float32Array(0) : new Uint8Array(0);
60
- try {
61
- run(mode, idCodeQuery, [], empty, 0, 0);
62
- }
63
- catch (error) {
64
- throw new Error(`"${idCodeQuery}" is not a valid query idcode`, {
65
- cause: error,
66
- });
67
- }
68
- }
69
- function run(mode, idCodeQuery, idCodes, result, from, to) {
70
- if (mode === 'similarity') {
71
- Search.similaritySearch(idCodeQuery, idCodes, result, from, to);
72
- }
73
- else {
74
- Search.ssSearch(idCodeQuery, idCodes, result, from, to);
75
- }
76
- }
77
- function firstPending(mode, result, from, to) {
78
- for (let index = from; index < to; index++) {
79
- const value = result[index];
80
- const pending = mode === 'similarity'
81
- ? Number.isNaN(value)
82
- : value === SubstructureResult.unprocessed;
83
- if (pending)
84
- return index;
85
- }
86
- return -1;
87
- }
88
- /**
89
- * Fingerprints `idCodes[from .. to)` into `result`, sixteen words per molecule.
90
- *
91
- * A molecule that cannot be read gets sixteen zeros, which no non-empty query is a subset of, so it
92
- * is never a false candidate.
93
- * @param idCodes - The molecules to fingerprint.
94
- * @param result - The caller's buffer, `16 * idCodes.length` words long.
95
- * @param from - The first index to fingerprint.
96
- * @param to - One past the last index to fingerprint.
97
- * @returns How many molecules were parsed and fingerprinted.
98
- */
99
- export function buildIndexes(idCodes, result, from, to) {
100
- return recoveringFromTraps((first, last) => Search.getIndexes(idCodes, result, first, last), (index) => result.fill(0, index * INDEX_WORDS, (index + 1) * INDEX_WORDS), from, to);
101
- }
102
- /**
103
- * Screens `idCodes[from .. to)` into `result`, sixteen words per molecule.
104
- *
105
- * Fails like {@link buildIndexes}: sixteen zeros for an idcode the WASM side cannot read.
106
- * @param idCodes - The molecules to screen.
107
- * @param result - The caller's buffer, `16 * idCodes.length` words long.
108
- * @param from - The first index to screen.
109
- * @param to - One past the last index to screen.
110
- * @returns How many molecules were parsed and screened.
111
- */
112
- export function buildScreens(idCodes, result, from, to) {
113
- return Search.getScreens(idCodes, result, from, to);
114
- }
115
- /**
116
- * Builds the function bits of `idCodes[from .. to)` into `result`, sixteen words per molecule.
117
- *
118
- * Fails like {@link buildIndexes}: sixteen zeros for an idcode the WASM side cannot read.
119
- * @param idCodes - The molecules to read.
120
- * @param result - The caller's buffer, `16 * idCodes.length` words long.
121
- * @param from - The first index to read.
122
- * @param to - One past the last index to read.
123
- * @returns How many molecules were parsed and read.
124
- */
125
- export function buildFunctions(idCodes, result, from, to) {
126
- return recoveringFromTraps((first, last) => Search.getFunctions(idCodes, result, first, last), (index) => result.fill(0, index * INDEX_WORDS, (index + 1) * INDEX_WORDS), from, to);
127
- }
128
- /**
129
- * Reads the fragments of the 512 stored function keys, in bit order.
130
- * @returns Per key, its fragment idcodes.
131
- */
132
- export function readFunctionKeys() {
133
- return Search.getFunctionKeys()
134
- .split('\n')
135
- .map((key) => key.split(' '));
136
- }
137
- /**
138
- * Builds what every molecule a query matches has: its FragFp words, or its screen words.
139
- * @param kind - `'index'` for the FragFp words, `'screen'` for the screen words.
140
- * @param idCodeQuery - The query, as an idcode.
141
- * @returns Sixteen words.
142
- * @throws {Error} If the query cannot be parsed.
143
- */
144
- export function buildQueryWords(kind, idCodeQuery) {
145
- const result = new Int32Array(INDEX_WORDS);
146
- try {
147
- if (kind === 'index') {
148
- Search.getQueryIndex(idCodeQuery, result);
149
- }
150
- else {
151
- Search.getQueryScreen(idCodeQuery, result);
152
- }
153
- }
154
- catch (error) {
155
- throw new Error(`"${idCodeQuery}" is not a valid query idcode`, {
156
- cause: error,
157
- });
158
- }
159
- return result;
160
- }
161
- /**
162
- * Reads the key fragments behind the bits of the FragFp words, or of the screen words.
163
- * @param kind - `'index'` for FragFp's keys, `'screen'` for the screen's.
164
- * @returns 512 idcodes, in bit order.
165
- */
166
- export function readKeys(kind) {
167
- const joined = kind === 'index' ? Search.getFragFpKeys() : Search.getScreenKeys();
168
- return joined.split('\n');
169
- }
170
- /**
171
- * Hashes `idCodes[from .. to)` into `result`, one no-stereo tautomer hash per molecule.
172
- *
173
- * A molecule that cannot be read gets `NO_HASH`, which is also what OpenChemLib returns for one it
174
- * cannot canonize, so the two failures are reported the same way.
175
- * @param idCodes - The molecules to hash.
176
- * @param result - The caller's buffer, `idCodes.length` entries long.
177
- * @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
178
- * @param from - The first index to hash.
179
- * @param to - One past the last index to hash.
180
- * @param tautomerCounts - Filled with how many tautomers each molecule needed; an entry that reached
181
- * `maxTautomers` is one OpenChemLib stopped early on.
182
- * @param maxTautomers - The ceiling on that enumeration, which is what bounds the cost.
183
- * @returns How many molecules were hashed.
184
- */
185
- export function buildNoStereoTautomerHashes(idCodes, result, largestFragmentOnly, from, to, tautomerCounts = new Int32Array(idCodes.length), maxTautomers = DEFAULT_MAX_TAUTOMERS) {
186
- return recoveringFromTraps((first, last) => Search.getNoStereoTautomerHashes(idCodes, result, tautomerCounts, largestFragmentOnly, maxTautomers, first, last), (index) => {
187
- result[index] = NO_HASH;
188
- }, from, to);
189
- }
190
- /**
191
- * Hashes `idCodes[from .. to)` into `result`, one no-stereo hash per molecule.
192
- *
193
- * Fails the same way {@link buildNoStereoTautomerHashes} does: `0` for an idcode the WASM side
194
- * cannot read and for a molecule OpenChemLib cannot canonize.
195
- * @param idCodes - The molecules to hash.
196
- * @param result - The caller's buffer, `idCodes.length` entries long.
197
- * @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
198
- * @param from - The first index to hash.
199
- * @param to - One past the last index to hash.
200
- * @returns How many molecules were hashed.
201
- */
202
- export function buildNoStereoHashes(idCodes, result, largestFragmentOnly, from, to) {
203
- return recoveringFromTraps((first, last) => Search.getNoStereoHashes(idCodes, result, largestFragmentOnly, first, last), (index) => {
204
- result[index] = NO_HASH;
205
- }, from, to);
206
- }
207
- /**
208
- * Canonizes `idCodes[from .. to)` into `result`, one no-stereo idcode per molecule.
209
- *
210
- * This is the string {@link buildNoStereoHashes} hashes, for a caller that stores the canonical form
211
- * rather than a 64-bit key. A molecule that cannot be read keeps the `NO_ID_CODE` the caller's array
212
- * was filled with.
213
- * @param idCodes - The molecules to canonize.
214
- * @param result - The caller's array, `idCodes.length` entries long, filled with `NO_ID_CODE`.
215
- * @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
216
- * @param from - The first index to canonize.
217
- * @param to - One past the last index to canonize.
218
- * @returns How many molecules were canonized.
219
- */
220
- export function buildNoStereoIdCodes(idCodes, result, largestFragmentOnly, from, to) {
221
- return recoveringFromTraps((first, last) => Search.getNoStereoIdCodes(idCodes, result, largestFragmentOnly, first, last), (index) => {
222
- result[index] = NO_ID_CODE;
223
- }, from, to);
224
- }
225
- /**
226
- * Canonizes `idCodes[from .. to)` into `result`, one no-stereo generic-tautomer idcode per molecule.
227
- *
228
- * Fails the same way {@link buildNoStereoIdCodes} does.
229
- * @param idCodes - The molecules to canonize.
230
- * @param result - The caller's array, `idCodes.length` entries long, filled with `NO_ID_CODE`.
231
- * @param largestFragmentOnly - Whether to strip and neutralize down to the largest fragment first.
232
- * @param from - The first index to canonize.
233
- * @param to - One past the last index to canonize.
234
- * @param tautomerCounts - Filled with how many tautomers each molecule needed; an entry that reached
235
- * `maxTautomers` is one OpenChemLib stopped early on.
236
- * @param maxTautomers - The ceiling on that enumeration, which is what bounds the cost.
237
- * @returns How many molecules were canonized.
238
- */
239
- export function buildNoStereoTautomerIdCodes(idCodes, result, largestFragmentOnly, from, to, tautomerCounts = new Int32Array(idCodes.length), maxTautomers = DEFAULT_MAX_TAUTOMERS) {
240
- return recoveringFromTraps((first, last) => Search.getNoStereoTautomerIdCodes(idCodes, result, tautomerCounts, largestFragmentOnly, maxTautomers, first, last), (index) => {
241
- result[index] = NO_ID_CODE;
242
- }, from, to);
243
- }
244
- /**
245
- * Runs a ranged WASM call, and redoes the range one molecule at a time if it traps.
246
- *
247
- * A malformed idcode makes OpenChemLib's bit-stream parser read past its input, which WebAssembly
248
- * traps on — the same trap {@link scanRange} recovers from. The Java guard catches an empty or
249
- * non-ASCII idcode, but a malformed ASCII one reaches the parser and traps there, which would
250
- * otherwise lose every molecule in the range including the good ones. The trap leaves the module
251
- * usable, and a per-molecule call costs what a batched one does because the 512 key fragments are
252
- * parsed once for the module's lifetime, so redoing the range singly costs nothing that was not
253
- * already going to be spent.
254
- *
255
- * There is no cap on the failures: these functions promise a per-molecule "could not read this"
256
- * answer rather than an exception, so an array that is entirely garbage yields one of those per
257
- * entry.
258
- * @param call - Runs the WASM call over a range, returning how many molecules it handled.
259
- * @param markUnreadable - Writes the "could not read this" answer for one molecule.
260
- * @param from - The first index to handle.
261
- * @param to - One past the last index to handle.
262
- * @returns How many molecules were handled.
263
- */
264
- export function recoveringFromTraps(call, markUnreadable, from, to) {
265
- try {
266
- return call(from, to);
267
- }
268
- catch {
269
- let handled = 0;
270
- for (let index = from; index < to; index++) {
271
- try {
272
- handled += call(index, index + 1);
273
- }
274
- catch {
275
- markUnreadable(index);
276
- }
277
- }
278
- return handled;
279
- }
280
- }
281
- //# sourceMappingURL=scan.js.map
package/lib/scan.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"scan.js","sourceRoot":"","sources":["../src/scan.ts"],"names":[],"mappings":"AACA,OAAO,EACL,qBAAqB,EACrB,WAAW,EACX,OAAO,EACP,UAAU,EACV,gBAAgB,EAChB,kBAAkB,GACnB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC,qGAAqG;AACrG,0FAA0F;AAC1F,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,OAAO,EAAE,CAAC;AAEnC,kGAAkG;AAClG,8FAA8F;AAC9F,kGAAkG;AAClG,wEAAwE;AACxE,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,SAAS,CACvB,IAAgB,EAChB,WAAmB,EACnB,OAAiB,EACjB,MAAoB,EACpB,IAAY,EACZ,EAAU;IAEV,MAAM,UAAU,GACd,IAAI,KAAK,YAAY;QACnB,CAAC,CAAC,gBAAgB,CAAC,UAAU;QAC7B,CAAC,CAAC,kBAAkB,CAAC,UAAU,CAAC;IACpC,IAAI,MAAM,GAAG,IAAI,CAAC;IAClB,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,OAAO,MAAM,GAAG,EAAE,EAAE,CAAC;QACnB,IAAI,CAAC;YACH,GAAG,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;YACpD,OAAO;QACT,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,MAAM,GAAG,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YAAC,MAAM,CAAC;gBACP,cAAc,CAAC,KAAK,CAAC,CAAC;YACxB,CAAC;QACH,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;AACH,CAAC"}
@@ -1,28 +0,0 @@
1
- import type { SimilaritySearchOptions, SimilaritySearchResult } from './types.ts';
2
- /**
3
- * Ranks many molecules by how similar they are to a query, on OpenChemLib's 512-bit FragFp
4
- * fingerprint.
5
- *
6
- * The entries are either idcodes or objects carrying one, and `matches` comes back **most similar
7
- * first**, with `similarities` holding each one's Tanimoto coefficient at the same position. The
8
- * two arrays are read together: the entries are the caller's own objects, so hanging the number on
9
- * a copy of each would allocate the result set twice.
10
- *
11
- * The fingerprint has to be built from each idcode, and that is essentially the whole cost: 947 µs
12
- * per molecule against 0.03 µs for the comparison itself. When the caller already stores
13
- * fingerprints — as `openchemlib-sqlite` does — comparing those directly is about 30,000 times
14
- * cheaper than calling this. Use this when idcodes are all you have.
15
- * @param idCodeQuery - The query, as an idcode.
16
- * @param entries - The molecules to compare against: idcodes, or objects holding one at
17
- * `options.jpath`.
18
- * @param options - Where the idcode sits, the threshold, how many to keep, and progress reporting.
19
- * @returns The entries at or above the threshold, most similar first, with their coefficients.
20
- * @throws {Error} If the query cannot be parsed, if more than 100 idcodes cannot be, or if no entry
21
- * holds a string at the jpath.
22
- * @example
23
- * ```js
24
- * const { matches, similarities } = similaritySearch(query, rows, { threshold: 0.8, limit: 20 });
25
- * ```
26
- */
27
- export declare function similaritySearch<Entry>(idCodeQuery: string, entries: readonly Entry[], options?: SimilaritySearchOptions): SimilaritySearchResult<Entry>;
28
- //# sourceMappingURL=similaritySearch.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"similaritySearch.d.ts","sourceRoot":"","sources":["../src/similaritySearch.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,uBAAuB,EACvB,sBAAsB,EACvB,MAAM,YAAY,CAAC;AAGpB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EACpC,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,SAAS,KAAK,EAAE,EACzB,OAAO,GAAE,uBAA4B,GACpC,sBAAsB,CAAC,KAAK,CAAC,CAkE/B"}
@@ -1,86 +0,0 @@
1
- import { toIdCodes } from "./jpath.js";
2
- import { runSearch } from "./runSearch.js";
3
- import { SimilarityResult } from "./types.js";
4
- /**
5
- * Ranks many molecules by how similar they are to a query, on OpenChemLib's 512-bit FragFp
6
- * fingerprint.
7
- *
8
- * The entries are either idcodes or objects carrying one, and `matches` comes back **most similar
9
- * first**, with `similarities` holding each one's Tanimoto coefficient at the same position. The
10
- * two arrays are read together: the entries are the caller's own objects, so hanging the number on
11
- * a copy of each would allocate the result set twice.
12
- *
13
- * The fingerprint has to be built from each idcode, and that is essentially the whole cost: 947 µs
14
- * per molecule against 0.03 µs for the comparison itself. When the caller already stores
15
- * fingerprints — as `openchemlib-sqlite` does — comparing those directly is about 30,000 times
16
- * cheaper than calling this. Use this when idcodes are all you have.
17
- * @param idCodeQuery - The query, as an idcode.
18
- * @param entries - The molecules to compare against: idcodes, or objects holding one at
19
- * `options.jpath`.
20
- * @param options - Where the idcode sits, the threshold, how many to keep, and progress reporting.
21
- * @returns The entries at or above the threshold, most similar first, with their coefficients.
22
- * @throws {Error} If the query cannot be parsed, if more than 100 idcodes cannot be, or if no entry
23
- * holds a string at the jpath.
24
- * @example
25
- * ```js
26
- * const { matches, similarities } = similaritySearch(query, rows, { threshold: 0.8, limit: 20 });
27
- * ```
28
- */
29
- export function similaritySearch(idCodeQuery, entries, options = {}) {
30
- const { jpath = 'idCode', threshold = 0, limit = Number.MAX_SAFE_INTEGER, collect = true, onStep, stepSize, } = options;
31
- const idCodes = toIdCodes(entries, jpath);
32
- const result = new Float32Array(entries.length);
33
- result.fill(SimilarityResult.unprocessed);
34
- const tally = { matched: 0, unparsable: 0 };
35
- const indexes = [];
36
- const collectRange = (from, to) => {
37
- for (let i = from; i < to; i++) {
38
- const value = result[i];
39
- if (value === SimilarityResult.unparsable) {
40
- tally.unparsable++;
41
- }
42
- else if (value >= threshold) {
43
- tally.matched++;
44
- if (collect)
45
- indexes.push(i);
46
- }
47
- }
48
- // A better match can always still be coming, so `limit` decides what is kept, never when to
49
- // stop. Only `onStep` returning false ends a similarity scan early.
50
- return false;
51
- };
52
- const run = runSearch({
53
- mode: 'similarity',
54
- idCodeQuery,
55
- idCodes,
56
- result,
57
- tally,
58
- collectRange,
59
- stepSize,
60
- onStep,
61
- stoppable: false,
62
- });
63
- indexes.sort((a, b) => result[b] - result[a]);
64
- if (indexes.length > limit)
65
- indexes.length = limit;
66
- const matches = new Array(indexes.length);
67
- const similarities = new Float32Array(indexes.length);
68
- for (let i = 0; i < indexes.length; i++) {
69
- const index = indexes[i];
70
- matches[i] = entries[index];
71
- similarities[i] = result[index];
72
- }
73
- return {
74
- matches,
75
- similarities,
76
- indexes,
77
- result,
78
- matched: tally.matched,
79
- unparsable: tally.unparsable,
80
- processed: run.processed,
81
- total: entries.length,
82
- elapsed: run.elapsed,
83
- stopped: run.stopped,
84
- };
85
- }
86
- //# sourceMappingURL=similaritySearch.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"similaritySearch.js","sourceRoot":"","sources":["../src/similaritySearch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAK3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,gBAAgB,CAC9B,WAAmB,EACnB,OAAyB,EACzB,UAAmC,EAAE;IAErC,MAAM,EACJ,KAAK,GAAG,QAAQ,EAChB,SAAS,GAAG,CAAC,EACb,KAAK,GAAG,MAAM,CAAC,gBAAgB,EAC/B,OAAO,GAAG,IAAI,EACd,MAAM,EACN,QAAQ,GACT,GA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AAO,CAAC,MAAM,GAAG,KAAK;QAAE,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC;IAEnD,MAAM,OAAO,GAAG,IAAI,KAAK,CAAQ,OAAO,CAAC,MAAM,CAAC,CAAC;IACjD,MAAM,YAAY,GAAG,IAAI,YAAY,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACtD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACxC,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,CAAW,CAAC;QACnC,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,KAAK,CAAU,CAAC;QACrC,YAAY,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,CAAW,CAAC;IAC5C,CAAC;IAED,OAAO;QACL,OAAO;QACP,YAAY;QACZ,OAAO;QACP,MAAM;QACN,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,UAAU,EAAE,KAAK,CAAC,UAAU;QAC5B,SAAS,EAAE,GAAG,CAAC,SAAS;QACxB,KAAK,EAAE,OAAO,CAAC,MAAM;QACrB,OAAO,EAAE,GAAG,CAAC,OAAO;QACpB,OAAO,EAAE,GAAG,CAAC,OAAO;KACrB,CAAC;AACJ,CAAC"}
@@ -1,9 +0,0 @@
1
- /**
2
- * `openchemlib-js` exposes `CanonizerUtil.getIDCode` but not the hash built from it, so the hash
3
- * tests hash that idcode here. This is OpenChemLib's `StrongHasher.hash` transcribed, and having it
4
- * written a second way is what makes those comparisons a real cross-check.
5
- * @param text - The idcode to hash.
6
- * @returns Its 64-bit hash, signed, as a `BigInt64Array` entry holds it.
7
- */
8
- export declare function strongHash(text: string): bigint;
9
- //# sourceMappingURL=strongHash.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"strongHash.d.ts","sourceRoot":"","sources":["../src/strongHash.ts"],"names":[],"mappings":"AAmBA;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAY/C"}
package/lib/strongHash.js DELETED
@@ -1,56 +0,0 @@
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
- * `openchemlib-js` exposes `CanonizerUtil.getIDCode` but not the hash built from it, so the hash
21
- * tests hash that idcode here. This is OpenChemLib's `StrongHasher.hash` transcribed, and having it
22
- * written a second way is what makes those comparisons a real cross-check.
23
- * @param text - The idcode to hash.
24
- * @returns Its 64-bit hash, signed, as a `BigInt64Array` entry holds it.
25
- */
26
- export function strongHash(text) {
27
- let h = HSTART;
28
- for (let i = text.length - 1; i >= 0; i--) {
29
- const character = BigInt(text.codePointAt(i));
30
- h =
31
- ((h * HMULT) & MASK_64) ^
32
- BYTE_TABLE[Number(character & 0xffn)];
33
- h =
34
- ((h * HMULT) & MASK_64) ^
35
- BYTE_TABLE[Number((character >> 8n) & 0xffn)];
36
- }
37
- return BigInt.asIntN(64, h);
38
- }
39
- /**
40
- * OpenChemLib's `CanonizerUtil.StrongHasher` byte table, built the same way the Java does.
41
- * @returns The 256 seeds, as unsigned 64-bit values.
42
- */
43
- function buildByteTable() {
44
- const table = new Array(256);
45
- let h = 0x544b2fbacaaf1684n;
46
- for (let i = 0; i < 256; i++) {
47
- for (let j = 0; j < 31; j++) {
48
- h = ((h >> 7n) ^ h) & MASK_64;
49
- h = ((h << 11n) ^ h) & MASK_64;
50
- h = ((h >> 10n) ^ h) & MASK_64;
51
- }
52
- table[i] = h;
53
- }
54
- return table;
55
- }
56
- //# sourceMappingURL=strongHash.js.map