Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __tests__/__snapshots__/library.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ exports[`class prototypes > prototype properties of Molecule 1`] = `
"changeAtom",
"changeAtomCharge",
"changeBond",
"changeCustomLabelPosition",
"clear",
"convertStereoBondsToSingleBonds",
"copyAtom",
Expand Down
52 changes: 52 additions & 0 deletions lib/extend/appendChangeCustomLabelPosition.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
}
4 changes: 1 addition & 3 deletions lib/extend/extendFromMolfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { changeMolfileCustomLabelPosition } from './utils/changeMolfileCustomLabelPosition.js';

export function extendFromMolfile(Molecule) {
const _fromMolfile = Molecule.fromMolfile;
Molecule.fromMolfile = function fromMolfile(molfile, options = {}) {
Expand Down Expand Up @@ -52,7 +50,7 @@ export function extendFromMolfile(Molecule) {
}
}
}
changeMolfileCustomLabelPosition(molecule, customLabelPosition);
molecule.changeCustomLabelPosition(customLabelPosition);
return molecule;
};
}
4 changes: 1 addition & 3 deletions lib/extend/extendToMolfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { changeMolfileCustomLabelPosition } from './utils/changeMolfileCustomLabelPosition.js';

const CUSTOM_ATOMS_LABELS_TAGS = [
'M STY',
'M SLB',
Expand All @@ -23,7 +21,7 @@ export function extendToMolfile(Molecule) {
customLabelPosition,
removeCustomAtomLabels = false,
} = options;
changeMolfileCustomLabelPosition(molecule, customLabelPosition);
molecule.changeCustomLabelPosition(customLabelPosition);

const molfile = _toMolfile.call(molecule);

Expand Down
4 changes: 3 additions & 1 deletion lib/extend/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { appendChangeCustomLabelPosition } from './appendChangeCustomLabelPosition.js';
import { appendGetNextCustomAtomLabel } from './appendGetNextCustomAtomLabel.js';
import { extendFromMolfile } from './extendFromMolfile.js';
import { extendToMolfile } from './extendToMolfile.js';
Expand Down Expand Up @@ -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
Expand Down
44 changes: 0 additions & 44 deletions lib/extend/utils/changeMolfileCustomLabelPosition.js

This file was deleted.

14 changes: 12 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,7 +48,7 @@ interface FromMolfileOptions {
* Default: undefined (keep as is)
* @default undefined
*/
customLabelPosition?: 'normal' | 'superscript' | 'auto' | undefined;
customLabelPosition?: CustomLabelPosition;
}

interface ToMolfileOptions {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading