Skip to content

Commit

Permalink
Use the unset bond order when reading SMILES as non kekulized.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Jul 20, 2016
1 parent bad6166 commit 7c77138
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 23 deletions.
8 changes: 5 additions & 3 deletions base/test/src/test/java/org/openscience/cdk/CDKTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,11 @@ protected void addImplicitHydrogens(IAtomContainer container) throws Exception {
*
* @param container the atom container to check
*/
protected void assertAllSingleAndAromatic(IAtomContainer container) throws Exception {
for (Iterator<IBond> bonds = container.bonds().iterator(); bonds.hasNext();)
Assert.assertEquals(IBond.Order.SINGLE, bonds.next().getOrder());
protected void assertAllSingleOrAromatic(IAtomContainer container) throws Exception {
for (IBond bond : container.bonds()) {
if (!bond.isAromatic())
Assert.assertEquals(IBond.Order.SINGLE, bond.getOrder());
}

for (IAtom atom : container.atoms()) {
if (atom.getSymbol().equals("H"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,16 @@ IBond toCDKBond(Edge edge, IAtom[] atoms) {
bond.setIsAromatic(true);
atoms[u].setIsAromatic(true);
atoms[v].setIsAromatic(true);
break;
case IMPLICIT:
if (atoms[u].isAromatic() && atoms[v].isAromatic()) {
bond.setIsAromatic(true);
bond.setOrder(IBond.Order.UNSET);
bond.setFlag(CDKConstants.SINGLE_OR_DOUBLE, true);
atoms[u].setIsAromatic(true);
atoms[v].setIsAromatic(true);
}
break;
}

return bond;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.google.common.collect.Maps;

import org.openscience.cdk.CDK;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.config.Isotopes;
import org.openscience.cdk.config.IsotopeFactory;
Expand Down Expand Up @@ -267,6 +268,8 @@ private Bond toBeamEdgeLabel(IBond b) throws CDKException {
case QUADRUPLE:
return Bond.QUADRUPLE;
default:
if (!this.aromatic && b.getFlag(CDKConstants.ISAROMATIC))
throw new CDKException("Cannot write Kekulé SMILES output due to aromatic bond with unset bond order - molecule should be Kekulized");
throw new CDKException("Unsupported bond order: " + order);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ public boolean isOK(IAtomContainer m) throws CDKException {
* @throws CDKException if something went wrong.
*/
public IAtomContainer fixAromaticBondOrders(IAtomContainer atomContainer) throws CDKException {

// preset all bond orders to single
for (IBond bond : atomContainer.bonds()) {
if (bond.isAromatic() && bond.getOrder() == IBond.Order.UNSET)
bond.setOrder(IBond.Order.SINGLE);
}

// OK, we take advantage here from the fact that this class does not take
// into account rings larger than 7 atoms. See fixAromaticBondOrders().
IRingSet rs = allRingsFinder.findAllRings(atomContainer, 7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
* @author Lucy Entwistle
* @cdk.module smiles
* @cdk.githash
* @deprecated use {@link org.openscience.cdk.aromaticity.Kekulization}
*/
@Deprecated
public class FixBondOrdersTool {

private boolean interrupted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void benzene() throws IOException {
assertThat(a.getImplicitHydrogenCount(), is(1));
}
for (IBond b : ac.bonds()) {
assertThat(b.getOrder(), is(IBond.Order.SINGLE));
assertThat(b.getOrder(), is(IBond.Order.UNSET));
assertTrue(b.getFlag(CDKConstants.ISAROMATIC));
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ public void imidazole() throws IOException {
}

for (IBond b : ac.bonds()) {
assertThat(b.getOrder(), is(IBond.Order.SINGLE));
assertThat(b.getOrder(), is(IBond.Order.UNSET));
assertTrue(b.getFlag(CDKConstants.ISAROMATIC));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,13 @@ public void bug328() throws Exception {
is(canon("Clc1ccc(Cl)c2[nH]c([nH0]c21)C(F)(F)F")));
}

@Test(expected = CDKException.class)
public void warnOnBadInput() throws Exception {
SmilesParser smipar = new SmilesParser(SilentChemObjectBuilder.getInstance());
smipar.kekulise(false);
IAtomContainer mol = smipar.parseSmiles("c1ccccc1");
System.err.println(SmilesGenerator.isomeric().create(mol));
}

/**
* @see https://tech.knime.org/forum/cdk/buggy-behavior-of-molecule-to-cdk-node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void testBug1363882() throws Exception {
@Test(timeout = 1000)
public void testBug1535587() throws Exception {
String smiles = "COC(=O)c2ccc3n([H])c1ccccc1c3(c2)";
IAtomContainer mol = loadExact(smiles);
IAtomContainer mol = load(smiles);
atomtype(mol);
assertAtomTypesPerceived(mol);
Assert.assertEquals(18, mol.getAtomCount());
Expand All @@ -164,7 +164,7 @@ public void testBug1535587() throws Exception {
@Test(timeout = 1000)
public void testBug1579235() throws Exception {
String smiles = "c2cc1cccn1cc2";
IAtomContainer mol = loadExact(smiles);
IAtomContainer mol = load(smiles);
atomtype(mol);
assertAtomTypesPerceived(mol);
Assert.assertEquals(9, mol.getAtomCount());
Expand Down Expand Up @@ -196,7 +196,7 @@ public void testBug1579229() throws Exception {
@Test(timeout = 1000)
public void testBug1579230() throws Exception {
String smiles = "Cc1cccc2sc3nncn3c12";
IAtomContainer mol = loadExact(smiles);
IAtomContainer mol = load(smiles);
atomtype(mol);
assertAtomTypesPerceived(mol);
Assert.assertEquals(13, mol.getAtomCount());
Expand Down Expand Up @@ -1554,13 +1554,13 @@ public void test3amino4methylpyridine() throws Exception {
@org.junit.Test
public void testPyrrole1() throws Exception {
String smiles = "[nH]1cccc1";
IAtomContainer mol = loadExact(smiles);
IAtomContainer mol = load(smiles);
atomtype(mol);
assertAtomTypesPerceived(mol);

Assert.assertEquals(5, mol.getAtomCount());

assertAllSingleAndAromatic(mol);
assertAllSingleOrAromatic(mol);

assertAtomSymbols(new String[]{"N", "C", "C", "C", "C"}, mol);

Expand All @@ -1577,13 +1577,13 @@ public void testPyrrole1() throws Exception {
@org.junit.Test
public void testPyrrole2() throws Exception {
String smiles = "n1([H])cccc1";
IAtomContainer mol = loadExact(smiles);
IAtomContainer mol = load(smiles);
atomtype(mol);
assertAtomTypesPerceived(mol);

Assert.assertEquals(6, mol.getAtomCount());

assertAllSingleAndAromatic(mol);
assertAllSingleOrAromatic(mol);

assertAtomSymbols(new String[]{"N", "H", "C", "C", "C", "C"}, mol);

Expand All @@ -1609,13 +1609,13 @@ public void testPyrrole3() throws Exception {
@org.junit.Test
public void testPyrroleAnion1() throws Exception {
String smiles = "[n-]1cccc1";
IAtomContainer mol = loadExact(smiles);
IAtomContainer mol = load(smiles);
atomtype(mol);
assertAtomTypesPerceived(mol);

Assert.assertEquals(5, mol.getAtomCount());

assertAllSingleAndAromatic(mol);
assertAllSingleOrAromatic(mol);

assertAtomSymbols(new String[]{"N", "C", "C", "C", "C"}, mol);

Expand All @@ -1632,13 +1632,13 @@ public void testPyrroleAnion1() throws Exception {
@org.junit.Test
public void testImidazole1() throws Exception {
String smiles = "[nH]1cncc1";
IAtomContainer mol = loadExact(smiles);
IAtomContainer mol = load(smiles);
atomtype(mol);
assertAtomTypesPerceived(mol);

Assert.assertEquals(5, mol.getAtomCount());

assertAllSingleAndAromatic(mol);
assertAllSingleOrAromatic(mol);

assertAtomSymbols(new String[]{"N", "C", "N", "C", "C"}, mol);

Expand All @@ -1655,13 +1655,13 @@ public void testImidazole1() throws Exception {
@org.junit.Test
public void testImidazole2() throws Exception {
String smiles = "n1([H])cncc1";
IAtomContainer mol = loadExact(smiles);
IAtomContainer mol = load(smiles);
atomtype(mol);
assertAtomTypesPerceived(mol);

Assert.assertEquals(6, mol.getAtomCount());

assertAllSingleAndAromatic(mol);
assertAllSingleOrAromatic(mol);

assertAtomSymbols(new String[]{"N", "H", "C", "N", "C", "C"}, mol);

Expand All @@ -1687,13 +1687,13 @@ public void testImidazole3() throws Exception {
@org.junit.Test
public void testImidazole4() throws Exception {
String smiles = "n1cc[nH]c1";
IAtomContainer mol = loadExact(smiles);
IAtomContainer mol = load(smiles);
atomtype(mol);
assertAtomTypesPerceived(mol);

Assert.assertEquals(5, mol.getAtomCount());

assertAllSingleAndAromatic(mol);
assertAllSingleOrAromatic(mol);

assertAtomSymbols(new String[]{"N", "C", "C", "N", "C"}, mol);

Expand Down Expand Up @@ -1730,13 +1730,13 @@ public void testPyridine1() throws Exception {
@org.junit.Test
public void testPyrimidine1() throws Exception {
String smiles = "n1cnccc1";
IAtomContainer mol = loadExact(smiles);
IAtomContainer mol = load(smiles);
atomtype(mol);
assertAtomTypesPerceived(mol);

Assert.assertEquals(6, mol.getAtomCount());

assertAllSingleAndAromatic(mol);
assertAllSingleOrAromatic(mol);

assertAtomSymbols(new String[]{"N", "C", "N", "C", "C", "C"}, mol);

Expand Down Expand Up @@ -2431,7 +2431,7 @@ public void testPreserveAromaticityAndPerceiveAtomTypes() throws Exception {
public void testAromaticBoron() throws Exception {
IAtomContainer mol = loadExact("c1cc2c3cc1.c1cb23cc1");
Assert.assertNotNull(mol);
assertAllSingleAndAromatic(mol);
assertAllSingleOrAromatic(mol);
}

/**
Expand Down

0 comments on commit 7c77138

Please sign in to comment.