Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add possitility to generate smarts and kekule isomeric smiles #204

Merged
merged 8 commits into from
May 13, 2024
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.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ exports[`prototype properties of Molecule 1`] = `
"toMolfile",
"toMolfileV3",
"toSVG",
"toSmarts",
lpatiny marked this conversation as resolved.
Show resolved Hide resolved
"toSmiles",
"translateCoords",
"validate",
Expand Down
24 changes: 23 additions & 1 deletion __tests__/molecule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs');

const Molecule = require('../minimal').Molecule;
const { Molecule } = require('../minimal');

describe('from and to SMILES', () => {
it.each(['C', 'COCOC', 'c1cc2cccc3c4cccc5cccc(c(c1)c23)c54'])(
Expand Down Expand Up @@ -34,8 +34,30 @@ describe('from and to SMILES', () => {
const mol = Molecule.fromSmiles(input);
expect(mol.toIsomericSmiles()).toBe(output);
});

it('smiles options', () => {
const mol = Molecule.fromSmiles('C1=CC=CC=C1');
expect(mol.toSmiles()).toBe('C1(=CC=CC=C1)');
expect(mol.toIsomericSmiles()).toBe('c1ccccc1');
expect(mol.toIsomericSmiles({ kekulizedOutput: true })).toBe('C1=CC=CC=C1');
expect(mol.toIsomericSmiles({ createSmarts: true })).toBe('c1ccccc1');
mol.setAtomMapNo(0, 1);
expect(mol.toIsomericSmiles({ includeMapping: true })).toBe('c1cc[cH:1]cc1');

const fragment = Molecule.fromSmiles('C1=CC=CC=C1C');
fragment.setFragment(true);
fragment.setAtomicNo(6, 1)
expect(fragment.toIsomericSmiles({ createSmarts: true })).toBe('c1cc[c;!H0]cc1');
})
});

it('smarts options', () => {
const fragment = Molecule.fromSmiles('C1=CC=CC=C1C');
fragment.setFragment(true);
fragment.setAtomicNo(6, 1)
expect(fragment.toSmarts()).toBe('c1cc[c;!H0]cc1');
})

describe('Molecule', () => {
it('medley', () => {
const idcode =
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
},
"homepage": "https://github.com/cheminfo/openchemlib-js",
"devDependencies": {
"@types/jest": "^29.5.12",
"benchmark": "^2.1.4",
"eslint": "^8.56.0",
"eslint-config-cheminfo": "^9.1.1",
Expand Down
33 changes: 30 additions & 3 deletions src/com/actelion/research/gwt/minimal/JSMolecule.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,38 @@ public native Object getOCL()
}-*/;

public String toSmiles() {
// we still allow to old code that do not care about stereo features and provide another SMILES
return new SmilesCreator().generateSmiles(oclMolecule);
}

public String toIsomericSmiles(boolean includeAtomMapping) {
return new IsomericSmilesCreator(oclMolecule, includeAtomMapping).getSmiles();
public native String toIsomericSmiles(JavaScriptObject options)
/*-{
options = options || {}
var createSmarts = options.createSmarts === true;
var includeMapping = options.includeMapping === true;
var kekulizedOutput = options.kekulizedOutput === true;
return [email protected]::toIsomericSmilesInternal(ZZZ)(createSmarts, includeMapping, kekulizedOutput);
}-*/;

@JsIgnore
public String toIsomericSmilesInternal(boolean createSmarts, boolean includeMapping,
boolean kekulizedOutput) {
int mode = 0;
if (createSmarts) {
mode |= IsomericSmilesCreator.MODE_CREATE_SMARTS;
}
if (includeMapping) {
mode |= IsomericSmilesCreator.MODE_INCLUDE_MAPPING;
}
if (kekulizedOutput) {
mode |= IsomericSmilesCreator.MODE_KEKULIZED_OUTPUT;
}
return new IsomericSmilesCreator(oclMolecule, mode).getSmiles();
}

public String toSmarts() {
return new IsomericSmilesCreator(oclMolecule, IsomericSmilesCreator.MODE_CREATE_SMARTS)
.getSmiles();
}

public String toMolfile() {
Expand Down Expand Up @@ -293,7 +320,7 @@ public StereoMolecule getStereoMolecule() {
return oclMolecule;
}

// coming form Canonizer.java
// coming from Canonizer.java
public static final int CANONIZER_CREATE_SYMMETRY_RANK = 1;
public static final int CANONIZER_CONSIDER_DIASTEREOTOPICITY = 2;
public static final int CANONIZER_CONSIDER_ENANTIOTOPICITY = 4;
Expand Down
26 changes: 24 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ export interface IHoseCodesOptions {
type: 0 | 1;
}

export interface ISmilesGeneratorOptions {
/**
* Whether to create SMILES with SMARTS capabilities.
* @default `false`
*/
createSmarts?: boolean;
/**
* Whether to include mapping information (atomMapNo) in the SMILES.
* @default `false`
*/
includeMapping?: boolean;
/**
* Should localisation of double bonds be attempted?
* @default `false`
*/
kekulizedOutput?: boolean;
}

export interface Rectangle {
/**
* X-coordinate of the top-left corner.
Expand Down Expand Up @@ -93,6 +111,8 @@ export declare class Molecule {

// based on JSMolecule.java you can do a regexp ".*static.* (int|long|double)(.*) = .*;" and replace with "$2: number;"

// bonds to represent aromaticity

static CANONIZER_CREATE_SYMMETRY_RANK: number;
static CANONIZER_CONSIDER_DIASTEREOTOPICITY: number;
static CANONIZER_CONSIDER_ENANTIOTOPICITY: number;
Expand Down Expand Up @@ -437,7 +457,9 @@ export declare class Molecule {

toSmiles(): string;

toIsomericSmiles(): string;
toSmarts(): string;

toIsomericSmiles(options?: ISmilesGeneratorOptions): string;

/**
* Returns a MDL Molfile V2000 string.
Expand Down Expand Up @@ -1302,7 +1324,7 @@ export declare class Molecule {
* @param mapNo
* @param autoMapped
*/
setAtomMapNo(atom: number, mapNo: number, autoMapped: boolean): void;
setAtomMapNo(atom: number, mapNo: number, autoMapped?: boolean): void;

/**
* Set atom to specific isotop or to have a natural isotop distribution
Expand Down
Loading