Skip to content

Instantly share code, notes, and snippets.

@johnmay
Created October 7, 2015 10:53
Show Gist options
  • Select an option

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

Select an option

Save johnmay/773e29f6862214208972 to your computer and use it in GitHub Desktop.
static int aromStatus(IAtomContainer mol) {
int res = 0;
for (IAtom atom : mol.atoms()) {
if (atom.getImplicitHydrogenCount() == null && atom.getFlag(CDKConstants.ISAROMATIC)) {
// N and P are ambiguous
if (atom.getAtomicNumber() == 7 || atom.getAtomicNumber() == 15)
res = 2;
else if (res < 2)
res = 1;
}
}
return res;
}
public static void main(String[] args) throws CDKException, FileNotFoundException {
final MDLV2000Reader mdlr = new MDLV2000Reader(new FileInputStream("/Users/john/Desktop/bad.mol"));
IAtomContainer mol = mdlr.read(new AtomContainer(0, 0, 0, 0));
// if res=2 it's ambiguous, res=1 should be fine.
// here we try to fix both
if (aromStatus(mol) > 0) {
for (IBond bond : mol.bonds()) {
if (bond.getOrder() == IBond.Order.UNSET) {
bond.setFlag(CDKConstants.ISAROMATIC, true); // needed?
bond.setOrder(IBond.Order.SINGLE);
}
}
// need hydrogen counts, can add via other method
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
CDKHydrogenAdder.getInstance(mol.getBuilder()).addImplicitHydrogens(mol);
// percieveAtomTypesAndConfigureAtoms wipes arom flags on atoms (bug)
for (IBond bond : mol.bonds()) {
if (bond.getFlag(CDKConstants.ISAROMATIC)) {
bond.getAtom(0).setFlag(CDKConstants.ISAROMATIC, true);
bond.getAtom(1).setFlag(CDKConstants.ISAROMATIC, true);
}
}
Kekulization.kekulize(mol);
}
System.out.println(SmilesGenerator.generic().create(mol));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment