Skip to content

Commit deeb8cc

Browse files
refactor: simplify molecule initialization
1 parent 77d23da commit deeb8cc

2 files changed

Lines changed: 31 additions & 59 deletions

File tree

src/data/molecules/Molecule.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,24 @@ export const DRAGGABLE_STRUCTURE_INITIAL_BOUNDING_REACT: MoleculeBoundingRect =
3131

3232
export type MoleculesView = Record<string, MoleculeView>;
3333

34-
type MoleculeInput = { molecule: Molecule } | { molfile: string };
35-
type StateMoleculeOptions = Omit<Partial<StateMolecule>, 'molfile'>;
36-
type InitializeMoleculeOptions = MoleculeInput & StateMoleculeOptions;
34+
type InitializeMoleculeOptions = { text: string } & Omit<
35+
Partial<StateMolecule>,
36+
'molfile'
37+
>;
3738

38-
function resolveMolecule(options: MoleculeInput) {
39-
if ('molecule' in options) {
40-
const { molecule } = options;
41-
return {
42-
molecule,
43-
molfile: molecule.toMolfileV3(),
44-
};
45-
}
46-
const { molfile } = options;
47-
return {
48-
molecule: Molecule.fromMolfile(molfile),
49-
molfile,
50-
};
51-
}
52-
53-
export function initMolecule(
54-
options: { molecule: Molecule } & StateMoleculeOptions,
55-
): StateMoleculeExtended;
56-
export function initMolecule(
57-
options: { molfile: string } & StateMoleculeOptions,
58-
): StateMoleculeExtended;
5939
export function initMolecule(
6040
options: InitializeMoleculeOptions,
6141
): StateMoleculeExtended {
62-
const { id = crypto.randomUUID(), label = 'p#' } = options;
42+
const { id = crypto.randomUUID(), label = 'p#', text } = options;
6343

64-
const { molecule, molfile } = resolveMolecule(options);
44+
const molecule = Molecule.fromText(text);
45+
46+
if (!molecule) {
47+
throw new Error(
48+
'Failed to parse SMILES or molfile. Please paste a valid format',
49+
);
50+
}
51+
const molfile = molecule.toMolfileV3();
6552
const mfInfo = molecule.getMolecularFormula();
6653

6754
return {

src/data/molecules/MoleculeManager.ts

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@ export function fromJSON(
1414

1515
const molecules: StateMoleculeExtended[] = [];
1616
for (const mol of mols) {
17-
const molecule = Molecule.fromMolfile(mol.molfile);
18-
19-
const atomCount = molecule.getAllAtoms();
20-
21-
if (atomCount === 0) {
22-
continue;
23-
}
24-
17+
const {
18+
molfile,
19+
label = `P${getLabelNumber(reservedNumbers)}`,
20+
id,
21+
...others
22+
} = mol;
2523
const moleculeOverride = {
26-
...mol,
27-
molfile: molecule.toMolfileV3(),
28-
label: mol.label || `P${getLabelNumber(reservedNumbers)}`,
29-
id: mol.id,
24+
...others,
25+
text: molfile,
26+
label,
27+
id,
3028
};
31-
32-
molecules.push(initMolecule(moleculeOverride));
29+
try {
30+
molecules.push(initMolecule(moleculeOverride));
31+
} catch {
32+
continue;
33+
}
3334
}
3435

3536
return molecules;
@@ -49,10 +50,9 @@ export function addMolfile(
4950

5051
// try to parse molfile
5152
// this will throw if the molecule can not be parsed !
52-
const molecule = Molecule.fromMolfile(molfile);
5353
molecules.push(
5454
initMolecule({
55-
molfile: molecule.toMolfileV3(),
55+
text: molfile,
5656
label: label ?? `P${getLabelNumber(reservedNumbers)}`,
5757
id,
5858
}),
@@ -66,9 +66,8 @@ export function setMolfile(
6666
const { molfile, id, label } = currentMolecule;
6767
// try to parse molfile
6868
// this will throw if the molecule can not be parsed !
69-
const molecule = Molecule.fromMolfile(molfile);
7069
const _mol = initMolecule({
71-
molfile: molecule.toMolfileV3(),
70+
text: molfile,
7271
id,
7372
label,
7473
});
@@ -148,26 +147,12 @@ function parseSDF(text: string) {
148147
}
149148
}
150149

151-
function parseMolText(text: string) {
152-
let molecule: Molecule | null;
153-
try {
154-
molecule = Molecule.fromText(text);
155-
} catch (error) {
156-
throw new Error(parseErrorMessage, { cause: error });
157-
}
158-
159-
if (!molecule) {
160-
throw new Error(parseErrorMessage);
161-
}
162-
163-
return [initMolecule({ molecule })];
164-
}
165150
export function getMolecules(text?: string) {
166151
if (!text?.trim()) {
167152
throw new Error(parseErrorMessage);
168153
}
169154

170155
const isSDF = /v[23]000/i.test(text);
171156

172-
return isSDF ? parseSDF(text) : parseMolText(text);
157+
return isSDF ? parseSDF(text) : [initMolecule({ text })];
173158
}

0 commit comments

Comments
 (0)