diff --git a/__tests__/__snapshots__/library.test.ts.snap b/__tests__/__snapshots__/library.test.ts.snap index 09e5d1f2..4c2446fc 100644 --- a/__tests__/__snapshots__/library.test.ts.snap +++ b/__tests__/__snapshots__/library.test.ts.snap @@ -70,6 +70,7 @@ exports[`class prototypes > prototype properties of Molecule 1`] = ` "changeAtom", "changeAtomCharge", "changeBond", + "changeCustomLabelPosition", "clear", "convertStereoBondsToSingleBonds", "copyAtom", diff --git a/lib/extend/appendChangeCustomLabelPosition.js b/lib/extend/appendChangeCustomLabelPosition.js new file mode 100644 index 00000000..ee38acbe --- /dev/null +++ b/lib/extend/appendChangeCustomLabelPosition.js @@ -0,0 +1,52 @@ +/** + * Append the method changeCustomLabelPosition to the Molecule class in order + * to change the position of custom labels according to the provided option + * - 'superscript' will add a ] at the beginning of the custom label + * - 'normal' will remove a leading ] if present + * - 'auto' will set the label as superscript for non-carbon atoms and normal for carbon atoms + * @param {*} Molecule + */ +export function appendChangeCustomLabelPosition(Molecule) { + Molecule.prototype.changeCustomLabelPosition = + function changeCustomLabelPosition(customLabelPosition) { + switch (customLabelPosition) { + case 'superscript': + for (let i = 0; i < this.getAllAtoms(); i++) { + const customLabel = this.getAtomCustomLabel(i); + if (customLabel && !customLabel.startsWith(']')) { + this.setAtomCustomLabel(i, `]${customLabel}`); + } + } + break; + case 'normal': + for (let i = 0; i < this.getAllAtoms(); i++) { + const customLabel = this.getAtomCustomLabel(i); + if (customLabel?.startsWith(']')) { + this.setAtomCustomLabel(i, customLabel.slice(1)); + } + } + break; + case 'auto': + for (let i = 0; i < this.getAllAtoms(); i++) { + const customLabel = this.getAtomCustomLabel(i); + if (customLabel) { + const atomLabel = this.getAtomLabel(i); + if (atomLabel === 'C') { + // normal + if (customLabel.startsWith(']')) { + this.setAtomCustomLabel(i, customLabel.slice(1)); + } + } else if (!customLabel.startsWith(']')) { + this.setAtomCustomLabel(i, `]${customLabel}`); + } + } + } + break; + case undefined: + // nothing to do + break; + default: + break; + } + }; +} diff --git a/lib/extend/extendFromMolfile.js b/lib/extend/extendFromMolfile.js index 83f5b07b..9a9fc7dc 100644 --- a/lib/extend/extendFromMolfile.js +++ b/lib/extend/extendFromMolfile.js @@ -1,5 +1,3 @@ -import { changeMolfileCustomLabelPosition } from './utils/changeMolfileCustomLabelPosition.js'; - export function extendFromMolfile(Molecule) { const _fromMolfile = Molecule.fromMolfile; Molecule.fromMolfile = function fromMolfile(molfile, options = {}) { @@ -52,7 +50,7 @@ export function extendFromMolfile(Molecule) { } } } - changeMolfileCustomLabelPosition(molecule, customLabelPosition); + molecule.changeCustomLabelPosition(customLabelPosition); return molecule; }; } diff --git a/lib/extend/extendToMolfile.js b/lib/extend/extendToMolfile.js index 72466990..afb6c7f8 100644 --- a/lib/extend/extendToMolfile.js +++ b/lib/extend/extendToMolfile.js @@ -1,5 +1,3 @@ -import { changeMolfileCustomLabelPosition } from './utils/changeMolfileCustomLabelPosition.js'; - const CUSTOM_ATOMS_LABELS_TAGS = [ 'M STY', 'M SLB', @@ -23,7 +21,7 @@ export function extendToMolfile(Molecule) { customLabelPosition, removeCustomAtomLabels = false, } = options; - changeMolfileCustomLabelPosition(molecule, customLabelPosition); + molecule.changeCustomLabelPosition(customLabelPosition); const molfile = _toMolfile.call(molecule); diff --git a/lib/extend/index.js b/lib/extend/index.js index 2cc2ed7b..2aac6ba5 100644 --- a/lib/extend/index.js +++ b/lib/extend/index.js @@ -1,3 +1,4 @@ +import { appendChangeCustomLabelPosition } from './appendChangeCustomLabelPosition.js'; import { appendGetNextCustomAtomLabel } from './appendGetNextCustomAtomLabel.js'; import { extendFromMolfile } from './extendFromMolfile.js'; import { extendToMolfile } from './extendToMolfile.js'; @@ -29,10 +30,11 @@ export function extendOCL(OCL) { options.funcTol, ); }; + appendChangeCustomLabelPosition(Molecule); + appendGetNextCustomAtomLabel(Molecule); extendFromMolfile(Molecule); extendToMolfile(Molecule); extendToRxn(Reaction, Molecule); - appendGetNextCustomAtomLabel(Molecule); function parseMoleculeFromText(text) { // Empty input diff --git a/lib/extend/utils/changeMolfileCustomLabelPosition.js b/lib/extend/utils/changeMolfileCustomLabelPosition.js deleted file mode 100644 index d082e67c..00000000 --- a/lib/extend/utils/changeMolfileCustomLabelPosition.js +++ /dev/null @@ -1,44 +0,0 @@ -export function changeMolfileCustomLabelPosition( - molecule, - customLabelPosition, -) { - switch (customLabelPosition) { - case 'superscript': - for (let i = 0; i < molecule.getAllAtoms(); i++) { - const customLabel = molecule.getAtomCustomLabel(i); - if (customLabel && !customLabel.startsWith(']')) { - molecule.setAtomCustomLabel(i, `]${customLabel}`); - } - } - break; - case 'normal': - for (let i = 0; i < molecule.getAllAtoms(); i++) { - const customLabel = molecule.getAtomCustomLabel(i); - if (customLabel?.startsWith(']')) { - molecule.setAtomCustomLabel(i, customLabel.slice(1)); - } - } - break; - case 'auto': - for (let i = 0; i < molecule.getAllAtoms(); i++) { - const customLabel = molecule.getAtomCustomLabel(i); - if (customLabel) { - const atomLabel = molecule.getAtomLabel(i); - if (atomLabel === 'C') { - // normal - if (customLabel.startsWith(']')) { - molecule.setAtomCustomLabel(i, customLabel.slice(1)); - } - } else if (!customLabel.startsWith(']')) { - molecule.setAtomCustomLabel(i, `]${customLabel}`); - } - } - } - break; - case undefined: - // nothing to do - break; - default: - break; - } -} diff --git a/lib/index.d.ts b/lib/index.d.ts index e2ebefc5..303d1a6b 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -36,6 +36,8 @@ export interface MoleculeFromSmilesOptions { noStereo?: boolean; } +type CustomLabelPosition = 'normal' | 'superscript' | 'auto' | undefined; + interface FromMolfileOptions { /** * If set to 'superscript', it will add a ']' at the beginning of the custom label to be @@ -46,7 +48,7 @@ interface FromMolfileOptions { * Default: undefined (keep as is) * @default undefined */ - customLabelPosition?: 'normal' | 'superscript' | 'auto' | undefined; + customLabelPosition?: CustomLabelPosition; } interface ToMolfileOptions { @@ -67,7 +69,7 @@ interface ToMolfileOptions { * Default: undefined (keep as is) * @default undefined */ - customLabelPosition?: 'normal' | 'superscript' | 'auto' | undefined; + customLabelPosition?: CustomLabelPosition; /** * Remove custom atom labels * @default false @@ -769,6 +771,14 @@ export declare class Molecule { */ addSubstituent(substituent: Molecule, connectionAtom: number): number[]; + /** + * Change the position of custom labels according to the provided option + * - 'superscript' will add a ] at the beginning of the custom label + * - 'normal' will remove a leading ] if present + * - 'auto' will set the label as superscript for non-carbon atoms and normal for carbon atoms + */ + changeCustomLabelPosition(position?: CustomLabelPosition): string; + /** * Copies this molecule including parity settings, if valid. The original * content of destMol is replaced. Helper arrays are not copied and need to be