-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreateJson.js
More file actions
60 lines (50 loc) · 1.73 KB
/
createJson.js
File metadata and controls
60 lines (50 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* eslint-disable no-await-in-loop */
import { writeFileSync } from 'node:fs';
import { readFile, readdir } from 'node:fs/promises';
import { join } from 'node:path';
import { Canonizer, Molecule } from 'openchemlib';
// we will create a database of molecule to number
// this database will be ordered by molecule size
async function createAutoLabelingJson() {
const allFiles = await readdir(join(import.meta.dirname, 'templates'), {
recursive: true,
});
const files = allFiles.filter((file) => file.endsWith('.mol'));
const moleculeDatabase = [];
for (const file of files) {
// console.log(file);
const molfile = await readFile(
join(import.meta.dirname, 'templates', file),
'utf8',
);
const molecule = Molecule.fromMolfile(molfile);
const mf = molecule.getMolecularFormula().formula;
const mw = molecule.getMolecularFormula().relativeWeight;
molecule.setFragment(true);
const canonizer = new Canonizer(molecule, { encodeAtomCustomLabels: true });
const idCode = canonizer.getIDCode();
try {
Molecule.fromIDCode(idCode);
} catch {
// eslint-disable-next-line no-console
console.log(molfile);
continue;
}
const coordinates = canonizer.getEncodedCoordinates(false);
moleculeDatabase.push({
idCode,
coordinates,
mf,
mw,
label: file.replace(/.*\//, '').replace('.mol', '').replaceAll('_', ' '),
});
}
return moleculeDatabase;
}
const moleculeDatabase = await createAutoLabelingJson();
moleculeDatabase.sort((a, b) => b.mw - a.mw);
const target = join(import.meta.dirname, '../../src/util/autoLabelDatabase.ts');
writeFileSync(
target,
`export const autoLabelDatabase = ${JSON.stringify(moleculeDatabase, null, 2)};`,
);