Pages

Tuesday, July 30, 2013

CTR #5: Convert a SMILES string to canonical SMILES

The CDK SmilesGenerator generates a canonical SMILES by default, so that one can simply parse the SMILES and generate a SMILES again to convert a SMILES string to a canonical SMILES:

import org.openscience.cdk.smiles.SmilesGenerator;
import org.openscience.cdk.smiles.SmilesParser;
import org.openscience.cdk.silent.SilentChemObjectBuilder;
 
parser = new SmilesParser(
  SilentChemObjectBuilder.getInstance()
);
generator = new SmilesGenerator();
 
smi = [
  "CN2C(=O)N(C)C(=O)C1=C2N=CN1C",
  "CN1C=NC2=C1C(=O)N(C)C(=O)N2C"
]
can = [];
 
smi.each { smiles ->
  can.add(
    generator.createSMILES(
      parser.parseSmiles(smiles)
    )
  )
}
 
assert can[0] == can[1]