Permalink
Browse files

Marked the atoms, bonds and molecule with the SINGLE_OR_DOUBLE-flag i…

…f needed and tested it
  • Loading branch information...
1 parent 46b1e9e commit 640813915be514a00e4ce72b9e6032cfc4cac47b @KlasJoensson KlasJoensson committed with egonw Apr 10, 2012
@@ -171,7 +171,7 @@
/**
* Flag used for marking uncertainty of the bond order.
- * If used on an <br>
+ * If used on an
* <ul>
* <li>IMolecule it means that one or several of the bonds have
* this flag raised (which may indicate aromaticity).</li>
@@ -36,8 +36,10 @@
import org.openscience.cdk.aromaticity.CDKHueckelAromaticityDetector;
import org.openscience.cdk.atomtype.CDKAtomTypeMatcher;
import org.openscience.cdk.exception.InvalidSmilesException;
+import org.openscience.cdk.exception.NoSuchAtomException;
import org.openscience.cdk.exception.NoSuchAtomTypeException;
import org.openscience.cdk.graph.ConnectivityChecker;
+import org.openscience.cdk.graph.SpanningTree;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IAtomContainerSet;
@@ -48,6 +50,8 @@
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IPseudoAtom;
import org.openscience.cdk.interfaces.IReaction;
+import org.openscience.cdk.interfaces.IRing;
+import org.openscience.cdk.interfaces.IRingSet;
import org.openscience.cdk.interfaces.ITetrahedralChirality;
import org.openscience.cdk.interfaces.ITetrahedralChirality.Stereo;
import org.openscience.cdk.tools.CDKHydrogenAdder;
@@ -270,7 +274,26 @@ public IAtomContainer parseSmiles(String smiles) throws InvalidSmilesException {
}
}
this.addImplicitHydrogens(molecule);
-
+
+ // Set the flag SINGLE_OR_DOUBLE to false if the bond with it isn't in a ring.
+ if (molecule.getFlag(CDKConstants.SINGLE_OR_DOUBLE)) {
+ SpanningTree molTree = new SpanningTree(molecule);
+ try {
+ IRingSet rings = molTree.getAllRings();
+ for (int j = 0; j < rings.getAtomContainerCount(); j++) {
+ for (IBond bond : rings.getAtomContainer(j).bonds())
+ if (bond.getAtom(0).getFlag(CDKConstants.SINGLE_OR_DOUBLE) &&
+ bond.getAtom(1).getFlag(CDKConstants.SINGLE_OR_DOUBLE)
+ ) {
+ bond.setFlag(CDKConstants.SINGLE_OR_DOUBLE, true);
+ }
+ }
+ } catch (NoSuchAtomException exception) {
+ logger.error("Caught unexpected Exception while identifying the rings.");
+ logger.debug(exception);
+ }
+ }
+
if (!preservingAromaticity ) {
this.perceiveAromaticity(molecule);
}
@@ -357,6 +380,11 @@ private IAtomContainer parseString(String smiles) throws InvalidSmilesException
currentSymbol = currentSymbol.toUpperCase();
atom = builder.newInstance(IAtom.class,currentSymbol);
atom.setHybridization(Hybridization.SP2);
+ // If the letter is small we rise the flag
+ atom.setFlag(CDKConstants.SINGLE_OR_DOUBLE, true);
+ // If the flag hasn't been raised on the molecule, lets do it.
+ if (molecule.getFlag(CDKConstants.SINGLE_OR_DOUBLE) != true)
+ molecule.setFlag(CDKConstants.SINGLE_OR_DOUBLE, true);
if (preservingAromaticity ) {
atom.setFlag(CDKConstants.ISAROMATIC, true);
}
@@ -69,6 +69,40 @@
private static SmilesParser sp = new SmilesParser(SilentChemObjectBuilder.getInstance());
+ @Test (timeout=1000)
+ public void testSingleOrDoubleFlag() throws Exception {
+ String smiles = "c1cccn1c2cccn2";
+ IMolecule mol = sp.parseSmiles(smiles);
+ assertAtomTypesPerceived(mol);
+ // Let's check the atoms first...
+ Assert.assertTrue(mol.getAtom(0).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getAtom(1).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getAtom(2).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getAtom(3).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getAtom(4).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+
+ Assert.assertTrue(mol.getAtom(5).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getAtom(6).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getAtom(7).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getAtom(8).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getAtom(9).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ // ...and then the bonds...
+ // ...in the first ring...
+ Assert.assertTrue(mol.getBond(0).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getBond(1).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getBond(2).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getBond(3).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getBond(4).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ // ...then the bond in between the rings...
+ Assert.assertFalse(mol.getBond(5).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ // ...and at last the bonds in the other ring.
+ Assert.assertTrue(mol.getBond(6).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getBond(7).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getBond(8).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getBond(9).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ Assert.assertTrue(mol.getBond(10).getFlag(CDKConstants.SINGLE_OR_DOUBLE));
+ }
+
/** @cdk.bug 1363882 */
@Test (timeout=1000)
public void testBug1363882() throws Exception {

0 comments on commit 6408139

Please sign in to comment.