Skip to content

Commit 3ddd312

Browse files
refactor: simplify molecule parsing
Simplify molecule parsing with Molecule.fromText(text) which introduced in OpenChemLib (v9.19+) close #3972
1 parent 9393149 commit 3ddd312

2 files changed

Lines changed: 65 additions & 25 deletions

File tree

src/data/molecules/Molecule.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import getAtomsFromMF from '../utilities/getAtomsFromMF.js';
55

66
export interface StateMoleculeExtended
77
extends
8-
Required<Pick<StateMolecule, 'id' | 'molfile' | 'label'>>,
9-
Omit<StateMolecule, 'id' | 'molfile' | 'label'> {
8+
Required<Pick<StateMolecule, 'id' | 'molfile' | 'label'>>,
9+
Omit<StateMolecule, 'id' | 'molfile' | 'label'> {
1010
mf: string;
1111
em: number;
1212
mw: number;
@@ -22,24 +22,47 @@ export interface MoleculeBoundingRect {
2222
}
2323

2424
export const DRAGGABLE_STRUCTURE_INITIAL_BOUNDING_REACT: MoleculeBoundingRect =
25-
{
26-
x: 10,
27-
y: 10,
28-
width: 130,
29-
height: 120,
30-
};
25+
{
26+
x: 10,
27+
y: 10,
28+
width: 130,
29+
height: 120,
30+
};
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;
37+
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;
3459
export function initMolecule(
35-
options: Partial<StateMolecule> = {},
60+
options: InitializeMoleculeOptions,
3661
): StateMoleculeExtended {
37-
const id = options.id || crypto.randomUUID();
38-
const label = options.label || 'p#';
39-
const molfile = options.molfile || '';
62+
const { id = crypto.randomUUID(), label = 'p#' } = options;
4063

41-
const mol = Molecule.fromMolfile(molfile);
42-
const mfInfo = mol.getMolecularFormula();
64+
const { molecule, molfile } = resolveMolecule(options);
65+
const mfInfo = molecule.getMolecularFormula();
4366

4467
return {
4568
id,
@@ -49,7 +72,7 @@ export function initMolecule(
4972
mf: mfInfo.formula,
5073
em: mfInfo.absoluteWeight,
5174
mw: mfInfo.relativeWeight,
52-
svg: mol.toSVG(50, 50),
75+
svg: molecule.toSVG(50, 50),
5376
atoms: getAtomsFromMF(mfInfo.formula),
5477
};
5578
}

src/data/molecules/MoleculeManager.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { StateMolecule } from '@zakodium/nmrium-core';
2-
import { readSDF, readSMILES } from '@zakodium/nmrium-core-plugins';
2+
import { readSDF } from '@zakodium/nmrium-core-plugins';
33
import { Molecule } from 'openchemlib';
44

55
import type { StateMoleculeExtended } from './Molecule.js';
@@ -137,20 +137,37 @@ function validateMolecules(molecules: StateMolecule[]) {
137137
* @param text - text containing one or several molecules in SMILES, molfile or SDF format
138138
* @returns
139139
*/
140-
export function getMolecules(text?: string) {
141-
if (!text?.trim()) {
142-
throw new Error(parseErrorMessage);
140+
141+
function parseSDF(text: string) {
142+
try {
143+
const molecules = readSDF(text);
144+
validateMolecules(molecules);
145+
return molecules;
146+
} catch (error) {
147+
throw new Error(parseErrorMessage, { cause: error });
143148
}
149+
}
144150

145-
// parse SDF
146-
let molecules = [];
147-
const parse = /v[23]000/i.test(text) ? readSDF : readSMILES;
151+
function parseMolText(text: string) {
152+
let molecule: Molecule | null;
148153
try {
149-
molecules = parse(text);
154+
molecule = Molecule.fromText(text);
150155
} catch (error) {
151156
throw new Error(parseErrorMessage, { cause: error });
152157
}
153158

154-
validateMolecules(molecules);
155-
return molecules;
159+
if (!molecule) {
160+
throw new Error(parseErrorMessage);
161+
}
162+
163+
return [initMolecule({ molecule })];
164+
}
165+
export function getMolecules(text?: string) {
166+
if (!text?.trim()) {
167+
throw new Error(parseErrorMessage);
168+
}
169+
170+
const isSDF = /v[23]000/i.test(text);
171+
172+
return isSDF ? parseSDF(text) : parseMolText(text);
156173
}

0 commit comments

Comments
 (0)