Skip to content

Instantly share code, notes, and snippets.

@johnmay
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmay/9804684 to your computer and use it in GitHub Desktop.
Save johnmay/9804684 to your computer and use it in GitHub Desktop.
// compute labelling (others are available)
final long[] labels = Canon.label(m, GraphUtil.toAdjList(m));
IAtom[] atoms = AtomContainerManipulator.getAtomArray(m);
IBond[] bonds = AtomContainerManipulator.getBondArray(m);
// atoms don't know their index
for (int i = 0; i < labels.length; i++)
atoms[i].setProperty("rank", labels);
// sort atoms and bonds (ensure neighbours are provided in the same order)
Arrays.sort(atoms, new Comparator<IAtom>() {
@Override public int compare(IAtom a, IAtom b) {
return a.getProperty("rank", Long.class)
.compareTo(a.getProperty("rank", Long.class));
}
});
Arrays.sort(bonds, new Comparator<IBond>() {
@Override public int compare(IBond a, IBond b) {
long a1 = a.getAtom(0).getProperty("rank");
long a2 = a.getAtom(1).getProperty("rank");
long b1 = b.getAtom(0).getProperty("rank");
long b2 = b.getAtom(1).getProperty("rank");
int cmp = Longs.compare(Math.min(a1, a2), Math.min(b1, b2));
return cmp != 0 ? cmp : Longs.compare(Math.max(a1, a2), Math.max(b1, b2));
}
});
// set the new orderings
m.setAtoms(atoms);
m.setBonds(bonds);
// clean up
for (IAtom a : m.atoms()) {
a.removeProperty("rank");
if (a.getProperties().isEmpty())
a.setProperties(null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment