Skip to content

@johnmay /gist:9804684
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
// 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
Something went wrong with that request. Please try again.