From 9f9974d186765642cdc93dbd206f3b6e5ea29b52 Mon Sep 17 00:00:00 2001 From: Luc Patiny Date: Sun, 13 Aug 2023 17:50:20 +0200 Subject: [PATCH] feat: add getFinalRanks (#171) --- __tests__/__snapshots__/library.js.snap | 1 + __tests__/molecule.js | 43 ++++++++++++++++++- .../research/gwt/minimal/JSMolecule.java | 9 +++- types.d.ts | 9 ++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/__tests__/__snapshots__/library.js.snap b/__tests__/__snapshots__/library.js.snap index 03f01370..13639bcd 100644 --- a/__tests__/__snapshots__/library.js.snap +++ b/__tests__/__snapshots__/library.js.snap @@ -142,6 +142,7 @@ exports[`prototype properties of Molecule 1`] = ` "getElectronValenceCorrection", "getExcludedNeighbourCount", "getExplicitHydrogens", + "getFinalRanks", "getFisherProjectionParity", "getFragmentAtoms", "getFragmentNumbers", diff --git a/__tests__/molecule.js b/__tests__/molecule.js index 12c255d5..a0f75a36 100644 --- a/__tests__/molecule.js +++ b/__tests__/molecule.js @@ -116,7 +116,7 @@ describe('Molecule', () => { expect(mol.getChiralText()).toBe('this enantiomer'); }); - it.skip('getCanonizedIDCode', () => { + it('getCanonizedIDCode', () => { let idcode = 'didH@@RYm^Fh@BHx@'; let mol = Molecule.fromIDCode(idcode); expect( @@ -138,6 +138,47 @@ describe('Molecule', () => { ).toBe(idcode); }); + it('getFinalRanks', () => { + const molecule = Molecule.fromSmiles('CCC'); + molecule.setAtomicNo(0, 8) + const atoms = []; + const ranks = [...molecule.getFinalRanks()]; + for (let i = 0; i < molecule.getAllAtoms(); i++) { + atoms.push(molecule.getAtomLabel(i), ranks[i]); + } + expect(atoms).toStrictEqual(['O', 3, 'C', 2, 'C', 1]) + const molecule2 = Molecule.fromSmiles('CCC'); + molecule2.setAtomicNo(2, 8) + const atoms2 = []; + const ranks2 = [...molecule2.getFinalRanks()]; + for (let i = 0; i < molecule2.getAllAtoms(); i++) { + atoms2.push(molecule2.getAtomLabel(i), ranks2[i]); + } + expect(atoms2).toStrictEqual(['C', 1, 'C', 2, 'O', 3]) + }) + + it('getFinalRanks of xMolecule', () => { + const molecule = Molecule.fromSmiles('CCCO'); + const xAtomicNumber = Molecule.getAtomicNoFromLabel( + 'X', + Molecule.cPseudoAtomX, + ); + molecule.addImplicitHydrogens(); + for (let i = 0; i < molecule.getAllAtoms(); i++) { + // hydrogens are not taken into account during canonization, we need to change them with an atom with a valence of 1 + if (molecule.getAtomicNo(i) === 1) { + molecule.setAtomicNo(i, xAtomicNumber); + } + } + + const ranks = [...molecule.getFinalRanks()]; + expect(ranks).toStrictEqual([ + 3, 1, 2, 4, 11, + 10, 9, 5, 6, 7, + 8, 12 + ]) + }) + it('should have a method that returns the OCL object', () => { const molecule = Molecule.fromSmiles('C'); const OCL = molecule.getOCL(); diff --git a/src/com/actelion/research/gwt/minimal/JSMolecule.java b/src/com/actelion/research/gwt/minimal/JSMolecule.java index 41e70bb0..27a250c1 100644 --- a/src/com/actelion/research/gwt/minimal/JSMolecule.java +++ b/src/com/actelion/research/gwt/minimal/JSMolecule.java @@ -153,6 +153,13 @@ public String getCanonizedIDCode(int flag) { return canonizer.getIDCode(); } + + + public int[] getFinalRanks(int flag) { + Canonizer canonizer = new Canonizer(oclMolecule, flag); + return canonizer.getFinalRank(); + } + public native JavaScriptObject getIDCodeAndCoordinates() /*-{ return { @@ -249,7 +256,7 @@ private void addImplicitHydrogens(int atomNumber) { @JsIgnore public static JSMolecule fromSmiles(String smiles, boolean createCoordinates, - boolean readStereoFeatures) throws Exception { + boolean readStereoFeatures) throws Exception { SmilesParser parser = new SmilesParser(); JSMolecule mol = new JSMolecule(); parser.parse(mol.oclMolecule, smiles.getBytes(), createCoordinates, readStereoFeatures); diff --git a/types.d.ts b/types.d.ts index b8ef325c..ff18d364 100644 --- a/types.d.ts +++ b/types.d.ts @@ -463,8 +463,17 @@ export declare class Molecule { options?: IMoleculeToSVGOptions, ): string; + /** + * Get an ID code for the molecule + * @param flag + */ getCanonizedIDCode(flag: number): string; + /** + * Returns the canonic numbering of the atoms. + */ + getFinalRanks(flag: number): number[]; + /** * Returns an object with both the ID code and coordinates of the molecule. */