openchemlib-search-wasm 1.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.
Files changed (47) hide show
  1. package/LICENSE +275 -0
  2. package/README.md +172 -0
  3. package/lib/index.d.ts +42 -0
  4. package/lib/index.d.ts.map +1 -0
  5. package/lib/index.js +55 -0
  6. package/lib/index.js.map +1 -0
  7. package/lib/jpath.d.ts +28 -0
  8. package/lib/jpath.d.ts.map +1 -0
  9. package/lib/jpath.js +73 -0
  10. package/lib/jpath.js.map +1 -0
  11. package/lib/runSearch.d.ts +41 -0
  12. package/lib/runSearch.d.ts.map +1 -0
  13. package/lib/runSearch.js +50 -0
  14. package/lib/runSearch.js.map +1 -0
  15. package/lib/scan.d.ts +40 -0
  16. package/lib/scan.d.ts.map +1 -0
  17. package/lib/scan.js +103 -0
  18. package/lib/scan.js.map +1 -0
  19. package/lib/similaritySearch.d.ts +28 -0
  20. package/lib/similaritySearch.d.ts.map +1 -0
  21. package/lib/similaritySearch.js +86 -0
  22. package/lib/similaritySearch.js.map +1 -0
  23. package/lib/substructureSearch.d.ts +26 -0
  24. package/lib/substructureSearch.d.ts.map +1 -0
  25. package/lib/substructureSearch.js +81 -0
  26. package/lib/substructureSearch.js.map +1 -0
  27. package/lib/types.d.ts +169 -0
  28. package/lib/types.d.ts.map +1 -0
  29. package/lib/types.js +30 -0
  30. package/lib/types.js.map +1 -0
  31. package/lib/wasm/load.d.ts +10 -0
  32. package/lib/wasm/load.d.ts.map +1 -0
  33. package/lib/wasm/load.js +42 -0
  34. package/lib/wasm/load.js.map +1 -0
  35. package/package.json +80 -0
  36. package/src/index.ts +68 -0
  37. package/src/jpath.ts +77 -0
  38. package/src/runSearch.ts +99 -0
  39. package/src/scan.ts +141 -0
  40. package/src/similaritySearch.ts +104 -0
  41. package/src/substructureSearch.ts +97 -0
  42. package/src/types.ts +197 -0
  43. package/src/wasm/load.ts +50 -0
  44. package/wasm/data.d.ts +2 -0
  45. package/wasm/data.js +3 -0
  46. package/wasm/runtime.d.ts +25 -0
  47. package/wasm/runtime.js +30 -0
@@ -0,0 +1,41 @@
1
+ import type { ResultBuffer, SearchMode, SearchStep } from './types.ts';
2
+ /** What a mode does with the entries a step just wrote. */
3
+ export interface StepTally {
4
+ /** Matches counted so far. */
5
+ matched: number;
6
+ /** Idcodes that could not be parsed. */
7
+ unparsable: number;
8
+ }
9
+ export interface RunSearchParams {
10
+ mode: SearchMode;
11
+ idCodeQuery: string;
12
+ idCodes: string[];
13
+ /** The buffer the scan fills, already reset to the mode's "not yet" value. */
14
+ result: ResultBuffer;
15
+ /** Updated as the scan goes, and read back into the returned counts. */
16
+ tally: StepTally;
17
+ /**
18
+ * Tallies `result[from .. to)` into `tally`, collecting whatever the mode collects.
19
+ * @returns True when the scan has everything it was asked for and should stop.
20
+ */
21
+ collectRange: (from: number, to: number) => boolean;
22
+ stepSize: number | undefined;
23
+ onStep: ((step: SearchStep) => boolean | void) | undefined;
24
+ /** True when the mode can stop before the end, so the scan must be cut into steps. */
25
+ stoppable: boolean;
26
+ }
27
+ /**
28
+ * Drives a scan, one step at a time, and reports it.
29
+ *
30
+ * A step is a single call into WebAssembly over a range of the array. Cutting the scan up is free —
31
+ * 800 chunked calls measure the same as one call over the whole corpus — so the only reason to do
32
+ * it is to hear about the scan or to stop it, and a search that wants neither runs as one call.
33
+ * @param params - The mode, the query, the buffer to fill and what to do with each step.
34
+ * @returns How far the scan got, how long it took and whether it stopped early.
35
+ */
36
+ export declare function runSearch(params: RunSearchParams): {
37
+ processed: number;
38
+ elapsed: number;
39
+ stopped: boolean;
40
+ };
41
+ //# sourceMappingURL=runSearch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runSearch.d.ts","sourceRoot":"","sources":["../src/runSearch.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AASvE,2DAA2D;AAC3D,MAAM,WAAW,SAAS;IACxB,8BAA8B;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,wCAAwC;IACxC,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,UAAU,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,8EAA8E;IAC9E,MAAM,EAAE,YAAY,CAAC;IACrB,wEAAwE;IACxE,KAAK,EAAE,SAAS,CAAC;IACjB;;;OAGG;IACH,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC;IACpD,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,MAAM,EAAE,CAAC,CAAC,IAAI,EAAE,UAAU,KAAK,OAAO,GAAG,IAAI,CAAC,GAAG,SAAS,CAAC;IAC3D,sFAAsF;IACtF,SAAS,EAAE,OAAO,CAAC;CACpB;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,eAAe,GAAG;IAClD,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CAClB,CAgDA"}
@@ -0,0 +1,50 @@
1
+ import { checkQuery, scanRange } from "./scan.js";
2
+ /**
3
+ * How many entries one step covers when the caller asked to hear about the scan but did not say how
4
+ * often. Sized from the measured cost of a molecule — ~22 µs to match a fragment, ~947 µs to build
5
+ * a fingerprint — so a step lands near 100 ms either way.
6
+ */
7
+ const DEFAULT_STEP = { substructure: 4096, similarity: 128 };
8
+ /**
9
+ * Drives a scan, one step at a time, and reports it.
10
+ *
11
+ * A step is a single call into WebAssembly over a range of the array. Cutting the scan up is free —
12
+ * 800 chunked calls measure the same as one call over the whole corpus — so the only reason to do
13
+ * it is to hear about the scan or to stop it, and a search that wants neither runs as one call.
14
+ * @param params - The mode, the query, the buffer to fill and what to do with each step.
15
+ * @returns How far the scan got, how long it took and whether it stopped early.
16
+ */
17
+ export function runSearch(params) {
18
+ const { mode, idCodeQuery, idCodes, result, tally, collectRange, onStep, stepSize, stoppable, } = params;
19
+ checkQuery(mode, idCodeQuery);
20
+ const total = idCodes.length;
21
+ const stepped = onStep !== undefined || stoppable;
22
+ const step = Math.max(1, stepSize ?? (stepped ? DEFAULT_STEP[mode] : total));
23
+ const started = performance.now();
24
+ let processed = 0;
25
+ let stopped = false;
26
+ while (processed < total) {
27
+ const to = Math.min(processed + step, total);
28
+ scanRange(mode, idCodeQuery, idCodes, result, processed, to);
29
+ const enough = collectRange(processed, to);
30
+ processed = to;
31
+ if (enough) {
32
+ stopped = processed < total;
33
+ break;
34
+ }
35
+ const goOn = onStep?.({
36
+ result,
37
+ processed,
38
+ total,
39
+ matched: tally.matched,
40
+ unparsable: tally.unparsable,
41
+ elapsed: performance.now() - started,
42
+ });
43
+ if (goOn === false) {
44
+ stopped = processed < total;
45
+ break;
46
+ }
47
+ }
48
+ return { processed, elapsed: performance.now() - started, stopped };
49
+ }
50
+ //# sourceMappingURL=runSearch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runSearch.js","sourceRoot":"","sources":["../src/runSearch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGlD;;;;GAIG;AACH,MAAM,YAAY,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC;AA6B7D;;;;;;;;GAQG;AACH,MAAM,UAAU,SAAS,CAAC,MAAuB;IAK/C,MAAM,EACJ,IAAI,EACJ,WAAW,EACX,OAAO,EACP,MAAM,EACN,KAAK,EACL,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,SAAS,GACV,GAAG,MAAM,CAAC;IAEX,UAAU,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IAE9B,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC;IA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
package/lib/scan.d.ts ADDED
@@ -0,0 +1,40 @@
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
+ * Unlike the searches, a malformed idcode needs no recovery here: the WASM side writes sixteen
31
+ * zeros for it and carries on, because a fingerprint no query can be a subset of is the safe answer
32
+ * for a molecule that could not be read.
33
+ * @param idCodes - The molecules to fingerprint.
34
+ * @param result - The caller's buffer, `16 * idCodes.length` words long.
35
+ * @param from - The first index to fingerprint.
36
+ * @param to - One past the last index to fingerprint.
37
+ * @returns How many molecules were parsed and fingerprinted.
38
+ */
39
+ export declare function buildIndexes(idCodes: string[], result: Int32Array, from: number, to: number): number;
40
+ //# sourceMappingURL=scan.d.ts.map
@@ -0,0 +1 @@
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;AAc3D;;;;;;;;;;;;;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;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,MAAM,EAAE,EACjB,MAAM,EAAE,UAAU,EAClB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACT,MAAM,CAER"}
package/lib/scan.js ADDED
@@ -0,0 +1,103 @@
1
+ import { 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
+ * Unlike the searches, a malformed idcode needs no recovery here: the WASM side writes sixteen
92
+ * zeros for it and carries on, because a fingerprint no query can be a subset of is the safe answer
93
+ * for a molecule that could not be read.
94
+ * @param idCodes - The molecules to fingerprint.
95
+ * @param result - The caller's buffer, `16 * idCodes.length` words long.
96
+ * @param from - The first index to fingerprint.
97
+ * @param to - One past the last index to fingerprint.
98
+ * @returns How many molecules were parsed and fingerprinted.
99
+ */
100
+ export function buildIndexes(idCodes, result, from, to) {
101
+ return Search.getIndexes(idCodes, result, from, to);
102
+ }
103
+ //# sourceMappingURL=scan.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scan.js","sourceRoot":"","sources":["../src/scan.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAClE,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;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AAI,WAAW,+BAA+B,EAAE;YAC9D,KAAK,EAAE,KAAK;SACb,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAED,SAAS,GAAG,CACV,IAAgB,EAChB,WAAmB,EACnB,OAAiB,EACjB,MAAoB,EACpB,IAAY,EACZ,EAAU;IAEV,IAAI,IAAI,KAAK,YAAY,EAAE,CAAC;QAC1B,MAAM,CAAC,gBAAgB,CACrB,WAAW,EACX,OAAO,EACP,MAAsB,EACtB,IAAI,EACJ,EAAE,CACH,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,MAAM,CAAC,QAAQ,CAAC,WAAW,EAAE,OAAO,EAAE,MAAoB,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;IACxE,CAAC;AACH,CAAC;AAED,SAAS,YAAY,CACnB,IAAgB,EAChB,MAAoB,EACpB,IAAY,EACZ,EAAU;IAEV,KAAK,IAAI,KAAK,GAAG,IAAI,EAAE,KAAK,GAAG,EAAE,EAAE,KAAK,EAAE,EAAE,CAAC;QAC3C,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAW,CAAC;QACtC,MAAM,OAAO,GACX,IAAI,KAAK,YAAY;YACnB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;YACrB,CAAC,CAAC,KAAK,KAAK,kBAAkB,CAAC,WAAW,CAAC;QAC/C,IAAI,OAAO;YAAE,OAAO,KAAK,CAAC;IAC5B,CAAC;IACD,OAAO,CAAC,CAAC,CAAC;AACZ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAC1B,OAAiB,EACjB,MAAkB,EAClB,IAAY,EACZ,EAAU;IAEV,OAAO,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;AACtD,CAAC"}
@@ -0,0 +1,28 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,86 @@
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
@@ -0,0 +1 @@
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,GAAG,OAAO,CAAC;IAEZ,MAAM,OAAO,GAAG,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAC1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O,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"}
@@ -0,0 +1,26 @@
1
+ import type { SubstructureSearchOptions, SubstructureSearchResult } from './types.ts';
2
+ /**
3
+ * Tests a query fragment against many molecules and returns the ones that contain it.
4
+ *
5
+ * The entries are either idcodes or objects carrying one — a database row, a candidate a prescreen
6
+ * yielded — and what comes back is those same objects, so nothing has to map positions back onto
7
+ * the array they came from. `result` still holds one status code per entry for a caller that wants
8
+ * the whole picture rather than the hits.
9
+ *
10
+ * The scan is synchronous. `onStep` reports it and can stop it, but the thread is not yielded:
11
+ * a caller that needs a responsive main thread slices the array across calls, or runs this in a
12
+ * worker.
13
+ * @param idCodeQuery - The query, as an idcode. It is searched as a fragment whatever its own
14
+ * fragment flag says, which is what `openchemlib`'s `SSSearcher` does with `setFragment(true)`.
15
+ * @param entries - The molecules to test: idcodes, or objects holding one at `options.jpath`.
16
+ * @param options - Where the idcode sits, how many matches are enough, and how to report progress.
17
+ * @returns The matching entries, their positions, the status buffer and the counts.
18
+ * @throws {Error} If the query cannot be parsed, if more than 100 idcodes cannot be, or if no entry
19
+ * holds a string at the jpath.
20
+ * @example
21
+ * ```js
22
+ * const { matches } = substructureSearch('gFp@DiTt@@B', rows, { jpath: 'molecule.idCode' });
23
+ * ```
24
+ */
25
+ export declare function substructureSearch<Entry>(idCodeQuery: string, entries: readonly Entry[], options?: SubstructureSearchOptions): SubstructureSearchResult<Entry>;
26
+ //# sourceMappingURL=substructureSearch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"substructureSearch.d.ts","sourceRoot":"","sources":["../src/substructureSearch.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,yBAAyB,EACzB,wBAAwB,EACzB,MAAM,YAAY,CAAC;AAGpB;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EACtC,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,SAAS,KAAK,EAAE,EACzB,OAAO,GAAE,yBAA8B,GACtC,wBAAwB,CAAC,KAAK,CAAC,CA6DjC"}
@@ -0,0 +1,81 @@
1
+ import { toIdCodes } from "./jpath.js";
2
+ import { runSearch } from "./runSearch.js";
3
+ import { SubstructureResult } from "./types.js";
4
+ /**
5
+ * Tests a query fragment against many molecules and returns the ones that contain it.
6
+ *
7
+ * The entries are either idcodes or objects carrying one — a database row, a candidate a prescreen
8
+ * yielded — and what comes back is those same objects, so nothing has to map positions back onto
9
+ * the array they came from. `result` still holds one status code per entry for a caller that wants
10
+ * the whole picture rather than the hits.
11
+ *
12
+ * The scan is synchronous. `onStep` reports it and can stop it, but the thread is not yielded:
13
+ * a caller that needs a responsive main thread slices the array across calls, or runs this in a
14
+ * worker.
15
+ * @param idCodeQuery - The query, as an idcode. It is searched as a fragment whatever its own
16
+ * fragment flag says, which is what `openchemlib`'s `SSSearcher` does with `setFragment(true)`.
17
+ * @param entries - The molecules to test: idcodes, or objects holding one at `options.jpath`.
18
+ * @param options - Where the idcode sits, how many matches are enough, and how to report progress.
19
+ * @returns The matching entries, their positions, the status buffer and the counts.
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 } = substructureSearch('gFp@DiTt@@B', rows, { jpath: 'molecule.idCode' });
25
+ * ```
26
+ */
27
+ export function substructureSearch(idCodeQuery, entries, options = {}) {
28
+ const { jpath = 'idCode', limit = Number.MAX_SAFE_INTEGER, collect = true, onStep, stepSize, } = options;
29
+ const idCodes = toIdCodes(entries, jpath);
30
+ // A fresh Uint8Array is already all zeros, which is `unprocessed`.
31
+ const result = new Uint8Array(entries.length);
32
+ const tally = { matched: 0, unparsable: 0 };
33
+ const indexes = [];
34
+ const bounded = limit < entries.length;
35
+ // A whole step is tallied before the limit is checked, so `matched` and `unparsable` always
36
+ // describe exactly the entries that were scanned. The overshoot that buys is under one step, and
37
+ // `indexes` is cut back to `limit` below.
38
+ const collectRange = (from, to) => {
39
+ for (let i = from; i < to; i++) {
40
+ const value = result[i];
41
+ if (value === SubstructureResult.match) {
42
+ tally.matched++;
43
+ if (collect)
44
+ indexes.push(i);
45
+ }
46
+ else if (value === SubstructureResult.unparsable) {
47
+ tally.unparsable++;
48
+ }
49
+ }
50
+ return bounded && tally.matched >= limit;
51
+ };
52
+ const run = runSearch({
53
+ mode: 'substructure',
54
+ idCodeQuery,
55
+ idCodes,
56
+ result,
57
+ tally,
58
+ collectRange,
59
+ stepSize,
60
+ onStep,
61
+ stoppable: bounded,
62
+ });
63
+ if (indexes.length > limit)
64
+ indexes.length = limit;
65
+ const matches = new Array(indexes.length);
66
+ for (let i = 0; i < indexes.length; i++) {
67
+ matches[i] = entries[indexes[i]];
68
+ }
69
+ return {
70
+ matches,
71
+ indexes,
72
+ result,
73
+ matched: tally.matched,
74
+ unparsable: tally.unparsable,
75
+ processed: run.processed,
76
+ total: entries.length,
77
+ elapsed: run.elapsed,
78
+ stopped: run.stopped,
79
+ };
80
+ }
81
+ //# sourceMappingURL=substructureSearch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"substructureSearch.js","sourceRoot":"","sources":["../src/substructureSearch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAK3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,kBAAkB,CAChC,WAAmB,EACnB,OAAyB,EACzB,UAAqC,EAAE;IAEvC,MAAM,EACJ,KAAK,GAAG,QAAQ,EAChB,KAAK,GAAG,MAAM,CAAC,gBAAgB,EAC/B,OAAO,GAAG,IAAI,EACd,MAAM,EACN,QAAQ,GACT,GAAG,OAAO,CAAC;IAEZ,MAAM,OAAO,GAAG,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAC1C,mEAAmE;IACnE,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC9C,MAAM,KAAK,GAAG,EAAE,OAAO,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;IAC5C,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,MAAM,OAAO,GAAG,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC;IAEvC,4FAA4F;IAC5F,iGAAiG;IACjG,0CAA0C;IAC1C,MAAM,YAAY,GAAG,CAAC,IAAY,EAAE,EAAU,EAAW,EAAE;QACzD,KAAK,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC;YAC/B,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,CAAW,CAAC;YAClC,IAAI,KAAK,KAA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