Skip to content

Commit

Permalink
Special handling of atom type 'X', leave hydrogens alone unless the v…
Browse files Browse the repository at this point in the history
…alue is null and we then set it to 0. Note this conditional was only added recently to ensure pseudo atoms don't have an unset hydrogen count.

Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Jul 22, 2014
1 parent 3836141 commit a952ead
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Expand Up @@ -36,11 +36,13 @@
import org.openscience.cdk.PseudoAtom;
import org.openscience.cdk.atomtype.CDKAtomTypeMatcher;
import org.openscience.cdk.config.Elements;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IAtomType;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IChemFile;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IElement;
import org.openscience.cdk.interfaces.IMolecularFormula;
import org.openscience.cdk.io.MDLV2000Reader;
Expand All @@ -51,6 +53,9 @@
import org.openscience.cdk.tools.manipulator.ChemFileManipulator;
import org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* Tests CDK's hydrogen adding capabilities in terms of
* example molecules.
Expand Down Expand Up @@ -781,6 +786,30 @@ public void testPseudoAtom() throws Exception {
}
Assert.assertEquals(1, hCount);
}

@Test public void unknownAtomTypeLeavesHydrogenCountAlone() throws Exception {
IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance();
CDKHydrogenAdder hydrogenAdder = CDKHydrogenAdder.getInstance(bldr);
IAtomContainer container = bldr.newInstance(IAtomContainer.class);
IAtom atom = bldr.newInstance(IAtom.class, "C");
atom.setImplicitHydrogenCount(3);
atom.setAtomTypeName("X");
container.addAtom(atom);
hydrogenAdder.addImplicitHydrogens(container);
assertThat(atom.getImplicitHydrogenCount(), is(3));
}

@Test public void unknownAtomTypeLeavesHydrogenCountAloneUnlessNull() throws Exception {
IChemObjectBuilder bldr = SilentChemObjectBuilder.getInstance();
CDKHydrogenAdder hydrogenAdder = CDKHydrogenAdder.getInstance(bldr);
IAtomContainer container = bldr.newInstance(IAtomContainer.class);
IAtom atom = bldr.newInstance(IAtom.class, "C");
atom.setImplicitHydrogenCount(null);
atom.setAtomTypeName("X");
container.addAtom(atom);
hydrogenAdder.addImplicitHydrogens(container);
assertThat(atom.getImplicitHydrogenCount(), is(0));
}

private void findAndConfigureAtomTypesForAllAtoms(IAtomContainer container) throws Exception {
Iterator<IAtom> atoms = container.atoms().iterator();
Expand Down
Expand Up @@ -125,7 +125,8 @@ public void addImplicitHydrogens(IAtomContainer container, IAtom atom) throws CD
throw new CDKException("IAtom is not typed! " + atom.getSymbol());

if ("X".equals(atom.getAtomTypeName())) {
atom.setImplicitHydrogenCount(0);
if (atom.getImplicitHydrogenCount() == null)
atom.setImplicitHydrogenCount(0);
return;
}

Expand Down

0 comments on commit a952ead

Please sign in to comment.