Chemistry Toolkit Rosetta Wiki
Advertisement

A SMILES string is a way to represent a 2D molecular graph as a 1D string. In most cases there are many possible SMILES strings for the same structure. Canonicalization is a way to determine which of all possible SMILES will be used as the reference SMILES for a molecular graph.

Suppose you want to find if a structure already exists in a data set. In graph theory this is the graph isomorphism problem. Using the canonical SMILES instead of the graphs reduces the problem to a simple text matching problem. Keep track of the canonical SMILES for each compound in a database and convert the query structure to its canonical SMILES. If that SMILES doesn't already exist then it is a new structure.

There is no universal canonical SMILES. Every toolkit uses a different algorithm, and sometimes the algorithm changes with different versions of the toolkit. There are even different forms of canonical SMILES, depending on if atomic properties like isotope are important for the result.

Canonical SMILES is mostly important inside of software tools. It isn't meant as an exchange format and no one really types in canonical SMILES. That's why this task doesn't have any form of I/O. The point of this task is to see how to convert a in-memory SMILES string to a molecule then generate the canonical SMILES for it.

Implementation

Parse two SMILES strings and convert them to canonical form. Check that the results give the same string. The input SMILES structures are:

CN2C(=O)N(C)C(=O)C1=C2N=CN1C
CN1C=NC2=C1C(=O)N(C)C(=O)N2C

Indigo/C++

#include "base_cpp/scanner.h"
#include "base_cpp/output.h"
#include "molecule/molecule.h"
#include "molecule/smiles_loader.h"
#include "molecule/molecule_arom.h"
#include "molecule/canonical_smiles_saver.h"

int main (int argc, char *argv[])
{
   const char *smiles1 = "CN2C(=O)N(C)C(=O)C1=C2N=CN1C";
   const char *smiles2 = "CN1C=NC2=C1C(=O)N(C)C(=O)N2C";

   Array<char> cansmiles1, cansmiles2;

   try
   {
      {
         BufferScanner scanner(smiles1);
         SmilesLoader loader(scanner);
         Molecule mol;

         loader.loadMolecule(mol, false);
         mol.calcImplicitHydrogens(true);
         mol.findBondsInRings();
         MoleculeAromatizer::aromatizeBonds(mol);

         ArrayOutput output(cansmiles1);
         CanonicalSmilesSaver saver(output);
         saver.saveMolecule(mol);
      }

      {
         BufferScanner scanner(smiles2);
         SmilesLoader loader(scanner);
         Molecule mol;

         loader.loadMolecule(mol, false);
         mol.calcImplicitHydrogens(true);
         mol.findBondsInRings();
         MoleculeAromatizer::aromatizeBonds(mol);

         ArrayOutput output(cansmiles2);
         CanonicalSmilesSaver saver(output);
         saver.saveMolecule(mol);
      }
   }
   catch (Exception &e)
   {
      fprintf(stderr, "error: %s\n", e.message());
      return -1;
   }

   if (cansmiles1.memcmp(cansmiles2) != 0)
   {
      fprintf(stderr, "SMILES strings do not match:\n%.*s\n%.*s\n",
              cansmiles1.size(), cansmiles1.ptr(),
              cansmiles2.size(), cansmiles2.ptr());
   }

   return 0;
}

Instructions:

  1. Unpack 'graph' and 'molecule' projects into some folder
  2. Create 'utils' folder nearby
  3. Paste the following code into utils/cansmiles.cpp file:
  4. Compile the file using the following commands:
    $ cd graph; make CONF=Release32; cd ..
    $ cd molecule; make CONF=Release32; cd ..
    $ cd utils
    $ gcc cansmiles.cpp -o cansmiles -O3 -m32 -I.. -I../common ../molecule/dist/Release32/GNU-Linux-x86/libmolecule.a ../graph/dist/Release32/GNU-Linux-x86/libgraph.a -lpthread -lstdc++
  5. Run the program like that:
    $ ./cansmiles "CN2C(=O)N(C)C(=O)C1=C2N=CN1C"
    No output is expected.

OpenBabel/Pybel

import pybel

smiles = ["CN2C(=O)N(C)C(=O)C1=C2N=CN1C",
          "CN1C=NC2=C1C(=O)N(C)C(=O)N2C"]

cans = [pybel.readstring("smi", smile).write("can") for smile in smiles]
assert cans[0] == cans[1]

OpenEye/Python

from openeye.oechem import *

def canonicalize(smiles):
    mol = OEGraphMol()
    OEParseSmiles(mol, smiles)
    return OECreateCanSmiString(mol)

assert (canonicalize("CN2C(=O)N(C)C(=O)C1=C2N=CN1C") ==
        canonicalize("CN1C=NC2=C1C(=O)N(C)C(=O)N2C"))

RDKit/Python

from rdkit import Chem

smis = ["CN2C(=O)N(C)C(=O)C1=C2N=CN1C",
          "CN1C=NC2=C1C(=O)N(C)C(=O)N2C"]

cans = [Chem.MolToSmiles(Chem.MolFromSmiles(smi),True) for smi in smis]
assert cans[0] == cans[1]
Advertisement