Skip to content

Instantly share code, notes, and snippets.

@johnmay
Created September 29, 2014 10:50
Show Gist options
  • Select an option

  • Save johnmay/cf1d3767d04eb424557f to your computer and use it in GitHub Desktop.

Select an option

Save johnmay/cf1d3767d04eb424557f to your computer and use it in GitHub Desktop.
static IAtomContainer mykekule(IAtomContainer org) throws Exception {
final IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance();
final SmilesParser smipar = new SmilesParser(bldr);
final SmilesGenerator smigen = SmilesGenerator.unique();
final int n = org.getAtomCount();
int[] ordering = new int[n];
// generate a kekule assignment via SMILES and store the output order (a permutation of
// atom indices)
IAtomContainer cpy = smipar.parseSmiles(smigen.create(org, ordering));
// index atoms for lookup
final Map<IAtom,Integer> atomIndexMap = new HashMap<>();
for (IAtom atom : org.atoms())
atomIndexMap.put(atom, atomIndexMap.size());
// util to get atom index -> bond map
EdgeToBondMap bondMap = EdgeToBondMap.withSpaceFor(cpy);
GraphUtil.toAdjList(cpy, bondMap);
for (IBond bond : org.bonds()) {
// atom indices
int u = atomIndexMap.get(bond.getAtom(0));
int v = atomIndexMap.get(bond.getAtom(1));
// atom indices in 'cpy'
int uCpy = ordering[u];
int vCpy = ordering[v];
// propagate the assigned bond order
bond.setOrder(bondMap.get(uCpy, vCpy).getOrder());
// note the following would also work to get the cpy bond
// cpy.getBond(cpy.getAtom(uCpy), cpy.getAtom(vCpy));
}
return org;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment