@kent-tokyo/chematic 0.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.
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # chematic-wasm
2
+
3
+ WebAssembly bindings for [chematic](https://github.com/kent-tokyo/chematic), a pure-Rust cheminformatics library.
4
+
5
+ This crate exposes `#[wasm_bindgen]` bindings so that chematic can be used directly from JavaScript and TypeScript in the browser or Node.js.
6
+
7
+ ## Features
8
+
9
+ - Parse SMILES strings into molecule handles
10
+ - Compute molecular descriptors: molecular weight, TPSA, formula, heavy atom count, H-bond donors/acceptors
11
+ - Lipinski Rule-of-Five check
12
+ - Canonical SMILES generation
13
+ - ECFP4 fingerprints and Tanimoto similarity
14
+
15
+ ## Usage
16
+
17
+ Build with [wasm-pack](https://rustwasm.github.io/wasm-pack/):
18
+
19
+ ```sh
20
+ wasm-pack build --target web
21
+ ```
22
+
23
+ Then in JavaScript/TypeScript:
24
+
25
+ ```js
26
+ import init, { parse_smiles, tanimoto_ecfp4 } from './pkg/chematic_wasm.js';
27
+
28
+ await init();
29
+
30
+ const mol = parse_smiles('c1ccccc1');
31
+ console.log(mol.atom_count()); // 6
32
+ console.log(mol.molecular_weight()); // ~78.11
33
+ console.log(mol.formula()); // "C6H6"
34
+ console.log(mol.lipinski_passes()); // true
35
+
36
+ const aspirin = parse_smiles('CC(=O)Oc1ccccc1C(=O)O');
37
+ const sim = tanimoto_ecfp4(mol, aspirin);
38
+ console.log(sim); // < 1.0
39
+ ```
@@ -0,0 +1,69 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * A handle to a parsed molecule. Owns the molecule behind an `Rc` so that
6
+ * it can be cheaply cloned on the JS side without copying atom/bond data.
7
+ */
8
+ export class MolHandle {
9
+ private constructor();
10
+ free(): void;
11
+ [Symbol.dispose](): void;
12
+ /**
13
+ * Number of heavy atoms (explicit atoms in the graph; does not count implicit H).
14
+ */
15
+ atom_count(): number;
16
+ /**
17
+ * Number of bonds.
18
+ */
19
+ bond_count(): number;
20
+ /**
21
+ * Canonical SMILES string.
22
+ */
23
+ canonical_smiles(): string;
24
+ /**
25
+ * Molecular formula string (Hill notation: C first, H second, then alphabetical).
26
+ */
27
+ formula(): string;
28
+ /**
29
+ * Number of hydrogen bond acceptors (Lipinski: all N and O atoms).
30
+ */
31
+ hba_count(): number;
32
+ /**
33
+ * Number of hydrogen bond donors (N-H or O-H groups).
34
+ */
35
+ hbd_count(): number;
36
+ /**
37
+ * Number of non-hydrogen heavy atoms.
38
+ */
39
+ heavy_atom_count(): number;
40
+ /**
41
+ * Returns `true` if the molecule satisfies Lipinski's Rule of Five.
42
+ */
43
+ lipinski_passes(): boolean;
44
+ /**
45
+ * Average molecular weight (Da).
46
+ */
47
+ molecular_weight(): number;
48
+ /**
49
+ * Topological polar surface area (Ų).
50
+ */
51
+ tpsa(): number;
52
+ }
53
+
54
+ /**
55
+ * Compute the ECFP4 fingerprint as a bit-packed byte vector (256 bytes = 2048 bits).
56
+ */
57
+ export function ecfp4_bitvec(mol: MolHandle): Uint8Array;
58
+
59
+ /**
60
+ * Parse a SMILES string into a `MolHandle`.
61
+ *
62
+ * Returns a JS error string on parse failure.
63
+ */
64
+ export function parse_smiles(s: string): MolHandle;
65
+
66
+ /**
67
+ * Tanimoto similarity between two molecules using ECFP4 fingerprints.
68
+ */
69
+ export function tanimoto_ecfp4(a: MolHandle, b: MolHandle): number;
@@ -0,0 +1,9 @@
1
+ /* @ts-self-types="./chematic_wasm.d.ts" */
2
+ import * as wasm from "./chematic_wasm_bg.wasm";
3
+ import { __wbg_set_wasm } from "./chematic_wasm_bg.js";
4
+
5
+ __wbg_set_wasm(wasm);
6
+ wasm.__wbindgen_start();
7
+ export {
8
+ MolHandle, ecfp4_bitvec, parse_smiles, tanimoto_ecfp4
9
+ } from "./chematic_wasm_bg.js";
@@ -0,0 +1,283 @@
1
+ /**
2
+ * A handle to a parsed molecule. Owns the molecule behind an `Rc` so that
3
+ * it can be cheaply cloned on the JS side without copying atom/bond data.
4
+ */
5
+ export class MolHandle {
6
+ static __wrap(ptr) {
7
+ const obj = Object.create(MolHandle.prototype);
8
+ obj.__wbg_ptr = ptr;
9
+ MolHandleFinalization.register(obj, obj.__wbg_ptr, obj);
10
+ return obj;
11
+ }
12
+ __destroy_into_raw() {
13
+ const ptr = this.__wbg_ptr;
14
+ this.__wbg_ptr = 0;
15
+ MolHandleFinalization.unregister(this);
16
+ return ptr;
17
+ }
18
+ free() {
19
+ const ptr = this.__destroy_into_raw();
20
+ wasm.__wbg_molhandle_free(ptr, 0);
21
+ }
22
+ /**
23
+ * Number of heavy atoms (explicit atoms in the graph; does not count implicit H).
24
+ * @returns {number}
25
+ */
26
+ atom_count() {
27
+ const ret = wasm.molhandle_atom_count(this.__wbg_ptr);
28
+ return ret >>> 0;
29
+ }
30
+ /**
31
+ * Number of bonds.
32
+ * @returns {number}
33
+ */
34
+ bond_count() {
35
+ const ret = wasm.molhandle_bond_count(this.__wbg_ptr);
36
+ return ret >>> 0;
37
+ }
38
+ /**
39
+ * Canonical SMILES string.
40
+ * @returns {string}
41
+ */
42
+ canonical_smiles() {
43
+ let deferred1_0;
44
+ let deferred1_1;
45
+ try {
46
+ const ret = wasm.molhandle_canonical_smiles(this.__wbg_ptr);
47
+ deferred1_0 = ret[0];
48
+ deferred1_1 = ret[1];
49
+ return getStringFromWasm0(ret[0], ret[1]);
50
+ } finally {
51
+ wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
52
+ }
53
+ }
54
+ /**
55
+ * Molecular formula string (Hill notation: C first, H second, then alphabetical).
56
+ * @returns {string}
57
+ */
58
+ formula() {
59
+ let deferred1_0;
60
+ let deferred1_1;
61
+ try {
62
+ const ret = wasm.molhandle_formula(this.__wbg_ptr);
63
+ deferred1_0 = ret[0];
64
+ deferred1_1 = ret[1];
65
+ return getStringFromWasm0(ret[0], ret[1]);
66
+ } finally {
67
+ wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
68
+ }
69
+ }
70
+ /**
71
+ * Number of hydrogen bond acceptors (Lipinski: all N and O atoms).
72
+ * @returns {number}
73
+ */
74
+ hba_count() {
75
+ const ret = wasm.molhandle_hba_count(this.__wbg_ptr);
76
+ return ret >>> 0;
77
+ }
78
+ /**
79
+ * Number of hydrogen bond donors (N-H or O-H groups).
80
+ * @returns {number}
81
+ */
82
+ hbd_count() {
83
+ const ret = wasm.molhandle_hbd_count(this.__wbg_ptr);
84
+ return ret >>> 0;
85
+ }
86
+ /**
87
+ * Number of non-hydrogen heavy atoms.
88
+ * @returns {number}
89
+ */
90
+ heavy_atom_count() {
91
+ const ret = wasm.molhandle_heavy_atom_count(this.__wbg_ptr);
92
+ return ret >>> 0;
93
+ }
94
+ /**
95
+ * Returns `true` if the molecule satisfies Lipinski's Rule of Five.
96
+ * @returns {boolean}
97
+ */
98
+ lipinski_passes() {
99
+ const ret = wasm.molhandle_lipinski_passes(this.__wbg_ptr);
100
+ return ret !== 0;
101
+ }
102
+ /**
103
+ * Average molecular weight (Da).
104
+ * @returns {number}
105
+ */
106
+ molecular_weight() {
107
+ const ret = wasm.molhandle_molecular_weight(this.__wbg_ptr);
108
+ return ret;
109
+ }
110
+ /**
111
+ * Topological polar surface area (Ų).
112
+ * @returns {number}
113
+ */
114
+ tpsa() {
115
+ const ret = wasm.molhandle_tpsa(this.__wbg_ptr);
116
+ return ret;
117
+ }
118
+ }
119
+ if (Symbol.dispose) MolHandle.prototype[Symbol.dispose] = MolHandle.prototype.free;
120
+
121
+ /**
122
+ * Compute the ECFP4 fingerprint as a bit-packed byte vector (256 bytes = 2048 bits).
123
+ * @param {MolHandle} mol
124
+ * @returns {Uint8Array}
125
+ */
126
+ export function ecfp4_bitvec(mol) {
127
+ _assertClass(mol, MolHandle);
128
+ const ret = wasm.ecfp4_bitvec(mol.__wbg_ptr);
129
+ var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
130
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
131
+ return v1;
132
+ }
133
+
134
+ /**
135
+ * Parse a SMILES string into a `MolHandle`.
136
+ *
137
+ * Returns a JS error string on parse failure.
138
+ * @param {string} s
139
+ * @returns {MolHandle}
140
+ */
141
+ export function parse_smiles(s) {
142
+ const ptr0 = passStringToWasm0(s, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
143
+ const len0 = WASM_VECTOR_LEN;
144
+ const ret = wasm.parse_smiles(ptr0, len0);
145
+ if (ret[2]) {
146
+ throw takeFromExternrefTable0(ret[1]);
147
+ }
148
+ return MolHandle.__wrap(ret[0]);
149
+ }
150
+
151
+ /**
152
+ * Tanimoto similarity between two molecules using ECFP4 fingerprints.
153
+ * @param {MolHandle} a
154
+ * @param {MolHandle} b
155
+ * @returns {number}
156
+ */
157
+ export function tanimoto_ecfp4(a, b) {
158
+ _assertClass(a, MolHandle);
159
+ _assertClass(b, MolHandle);
160
+ const ret = wasm.tanimoto_ecfp4(a.__wbg_ptr, b.__wbg_ptr);
161
+ return ret;
162
+ }
163
+ export function __wbg___wbindgen_throw_1506f2235d1bdba0(arg0, arg1) {
164
+ throw new Error(getStringFromWasm0(arg0, arg1));
165
+ }
166
+ export function __wbindgen_cast_0000000000000001(arg0, arg1) {
167
+ // Cast intrinsic for `Ref(String) -> Externref`.
168
+ const ret = getStringFromWasm0(arg0, arg1);
169
+ return ret;
170
+ }
171
+ export function __wbindgen_init_externref_table() {
172
+ const table = wasm.__wbindgen_externrefs;
173
+ const offset = table.grow(4);
174
+ table.set(0, undefined);
175
+ table.set(offset + 0, undefined);
176
+ table.set(offset + 1, null);
177
+ table.set(offset + 2, true);
178
+ table.set(offset + 3, false);
179
+ }
180
+ const MolHandleFinalization = (typeof FinalizationRegistry === 'undefined')
181
+ ? { register: () => {}, unregister: () => {} }
182
+ : new FinalizationRegistry(ptr => wasm.__wbg_molhandle_free(ptr, 1));
183
+
184
+ function _assertClass(instance, klass) {
185
+ if (!(instance instanceof klass)) {
186
+ throw new Error(`expected instance of ${klass.name}`);
187
+ }
188
+ }
189
+
190
+ function getArrayU8FromWasm0(ptr, len) {
191
+ ptr = ptr >>> 0;
192
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
193
+ }
194
+
195
+ function getStringFromWasm0(ptr, len) {
196
+ return decodeText(ptr >>> 0, len);
197
+ }
198
+
199
+ let cachedUint8ArrayMemory0 = null;
200
+ function getUint8ArrayMemory0() {
201
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
202
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
203
+ }
204
+ return cachedUint8ArrayMemory0;
205
+ }
206
+
207
+ function passStringToWasm0(arg, malloc, realloc) {
208
+ if (realloc === undefined) {
209
+ const buf = cachedTextEncoder.encode(arg);
210
+ const ptr = malloc(buf.length, 1) >>> 0;
211
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
212
+ WASM_VECTOR_LEN = buf.length;
213
+ return ptr;
214
+ }
215
+
216
+ let len = arg.length;
217
+ let ptr = malloc(len, 1) >>> 0;
218
+
219
+ const mem = getUint8ArrayMemory0();
220
+
221
+ let offset = 0;
222
+
223
+ for (; offset < len; offset++) {
224
+ const code = arg.charCodeAt(offset);
225
+ if (code > 0x7F) break;
226
+ mem[ptr + offset] = code;
227
+ }
228
+ if (offset !== len) {
229
+ if (offset !== 0) {
230
+ arg = arg.slice(offset);
231
+ }
232
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
233
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
234
+ const ret = cachedTextEncoder.encodeInto(arg, view);
235
+
236
+ offset += ret.written;
237
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
238
+ }
239
+
240
+ WASM_VECTOR_LEN = offset;
241
+ return ptr;
242
+ }
243
+
244
+ function takeFromExternrefTable0(idx) {
245
+ const value = wasm.__wbindgen_externrefs.get(idx);
246
+ wasm.__externref_table_dealloc(idx);
247
+ return value;
248
+ }
249
+
250
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
251
+ cachedTextDecoder.decode();
252
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
253
+ let numBytesDecoded = 0;
254
+ function decodeText(ptr, len) {
255
+ numBytesDecoded += len;
256
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
257
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
258
+ cachedTextDecoder.decode();
259
+ numBytesDecoded = len;
260
+ }
261
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
262
+ }
263
+
264
+ const cachedTextEncoder = new TextEncoder();
265
+
266
+ if (!('encodeInto' in cachedTextEncoder)) {
267
+ cachedTextEncoder.encodeInto = function (arg, view) {
268
+ const buf = cachedTextEncoder.encode(arg);
269
+ view.set(buf);
270
+ return {
271
+ read: arg.length,
272
+ written: buf.length
273
+ };
274
+ };
275
+ }
276
+
277
+ let WASM_VECTOR_LEN = 0;
278
+
279
+
280
+ let wasm;
281
+ export function __wbg_set_wasm(val) {
282
+ wasm = val;
283
+ }
Binary file
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@kent-tokyo/chematic",
3
+ "type": "module",
4
+ "collaborators": [
5
+ "kent-tokyo <kent-tokyo@users.noreply.github.com>"
6
+ ],
7
+ "description": "WebAssembly bindings for chematic — use chematic from JavaScript/TypeScript",
8
+ "version": "0.1.3",
9
+ "license": "MIT OR Apache-2.0",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/kent-tokyo/chematic"
13
+ },
14
+ "files": [
15
+ "chematic_wasm_bg.wasm",
16
+ "chematic_wasm.js",
17
+ "chematic_wasm_bg.js",
18
+ "chematic_wasm.d.ts"
19
+ ],
20
+ "main": "chematic_wasm.js",
21
+ "homepage": "https://github.com/kent-tokyo/chematic",
22
+ "types": "chematic_wasm.d.ts",
23
+ "sideEffects": [
24
+ "./chematic_wasm.js",
25
+ "./snippets/*"
26
+ ],
27
+ "keywords": [
28
+ "cheminformatics",
29
+ "chemistry",
30
+ "smiles",
31
+ "wasm",
32
+ "rdkit",
33
+ "fingerprints",
34
+ "tanimoto"
35
+ ]
36
+ }