Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Modified getTotalHydrogenCount(IAtomContainer) to actually provide …
…the total hydrogen count. the only classes invoking this are AtomContainerSetManipulator and MoleculeSetManipulator the test on each of these were tested and worked as expected. - Added method to just count implicit hydrogens (as the pervious method actually did) - Modified existing unit test to reflect the change to total hydrogen count - Added three new unit tests for the new implicit method - Added null check and exception throw on new/existing methods

Change-Id: Ic96d8b7fea6546d46c0f8ea329a2cc5a3ba74093
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Dec 12, 2012
1 parent 8d0642e commit 856781e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 17 deletions.
Expand Up @@ -33,6 +33,7 @@
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.atomtype.CDKAtomTypeMatcher;
import org.openscience.cdk.config.Elements;
import org.openscience.cdk.config.IsotopeFactory;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
Expand Down Expand Up @@ -94,7 +95,7 @@ public static IAtomContainer extractSubstructure(
substructure.removeAtomAndConnectedElectronContainers(atom);
}
}

return substructure;
}

Expand Down Expand Up @@ -266,35 +267,82 @@ public static int getTotalPositiveFormalCharge(IAtomContainer atomContainer) {
}

/**
* Count the total number of hydrogens (implicit and explicit).
* Counts the number of hydrogens on the provided IAtomContainer. As this
* method will sum all implicit hydrogens on each atom it is important to
* ensure the atoms have already been perceived (and thus have an implicit
* hydrogen count) (see. {@link #percieveAtomTypesAndConfigureAtoms}).
*
* @param atomContainer the atom container to consider
* @return The summed implicit hydrogens of all atoms in this AtomContainer.
* @param container the container to count the hydrogens on
* @return the total number of hydrogens
* @see org.openscience.cdk.interfaces.IAtom#getImplicitHydrogenCount()
* @see #percieveAtomTypesAndConfigureAtoms
* @throws IllegalArgumentException if the provided container was null
*/
@TestMethod("testGetTotalHydrogenCount_IAtomContainer,testGetTotalHydrogenCount_IAtomContainer_zeroImplicit,testGetTotalHydrogenCount_IAtomContainer_nullImplicit,testGetTotalHydrogenCount_ImplicitHydrogens")
public static int getTotalHydrogenCount(IAtomContainer atomContainer) {
int hCount = 0;
for (int i = 0; i < atomContainer.getAtomCount(); i++) {
Integer ihcount = atomContainer.getAtom(i).getImplicitHydrogenCount();
if (ihcount != CDKConstants.UNSET)
hCount += ihcount;
public static int getTotalHydrogenCount(IAtomContainer container) {
if(container == null)
throw new IllegalArgumentException("null container provided");
int hydrogens = 0;
for (IAtom atom : container.atoms()) {

if(Elements.HYDROGEN.getSymbol().equals(atom.getSymbol())) {
hydrogens++;
}

// rare but a hydrogen may have an implicit hydrogen so we don't use 'else'
Integer implicit = atom.getImplicitHydrogenCount();
if (implicit != null) {
hydrogens += implicit;
}

}
return hCount;
return hydrogens;
}


/**
* Counts the number of implicit hydrogens on the provided IAtomContainer.
* As this method will sum all implicit hydrogens on each atom it is
* important to ensure the atoms have already been perceived (and thus have
* an implicit hydrogen count) (see.
* {@link #percieveAtomTypesAndConfigureAtoms}).
*
* @param container the container to count the implicit hydrogens on
* @return the total number of implicit hydrogens
* @see org.openscience.cdk.interfaces.IAtom#getImplicitHydrogenCount()
* @see #percieveAtomTypesAndConfigureAtoms
* @throws IllegalArgumentException if the provided container was null
*/
@TestMethod("testGetImplicitHydrogenCount_unperceived,testGetImplicitHydrogenCount_null,testGetImplicitHydrogenCount_adenine")
public static int getImplicitHydrogenCount(IAtomContainer container){
if(container == null)
throw new IllegalArgumentException("null container provided");
int count = 0;
for(IAtom atom : container.atoms()){
Integer implicit = atom.getImplicitHydrogenCount();
if(implicit != null) {
count += implicit;
}
}
return count;
}

/**
* Count explicit hydrogens.
*
* @param atomContainer the atom container to consider
* @return The number of explicit hydrogens on the given IAtom.
* @throws IllegalArgumentException if either the container or atom were null
*/
@TestMethod("testCountExplicitH")
@TestMethod("testCountExplicitH,testCountExplicitH_IAtomContainer_Null,testCountExplicitH_Null_IAtom")
public static int countExplicitHydrogens(IAtomContainer atomContainer, IAtom atom) {
int hCount = 0;
for (IAtom iAtom : atomContainer.getConnectedAtomsList(atom)) {
IAtom connectedAtom = iAtom;
if (connectedAtom.getSymbol().equals("H"))
if(atomContainer == null || atom == null )
throw new IllegalArgumentException("null container or atom provided");
int hCount = 0;
for (IAtom connected : atomContainer.getConnectedAtomsList(atom)) {
if (Elements.HYDROGEN.getSymbol().equals(connected.getSymbol())) {
hCount++;
}
}
return hCount;
}
Expand Down
Expand Up @@ -77,6 +77,9 @@ public void testExtractSubstructure() throws CloneNotSupportedException {
Assert.assertEquals(6, ringSubstructure.getBondCount());
}

/**
* @cdk.bug 1254
*/
@Test
public void testGetTotalHydrogenCount_IAtomContainer() throws IOException, ClassNotFoundException, CDKException {
IAtomContainer mol = new AtomContainer(); // ethene
Expand All @@ -93,7 +96,8 @@ public void testGetTotalHydrogenCount_IAtomContainer() throws IOException, Class
mol.addBond(1, 5, IBond.Order.SINGLE);
Assert.assertEquals(6, mol.getAtomCount());
Assert.assertEquals(5, mol.getBondCount());
Assert.assertEquals(0, AtomContainerManipulator.getTotalHydrogenCount(mol));
// total includes explicit and implicit (we don't have any implicit to 4 is expected)
Assert.assertEquals(4, AtomContainerManipulator.getTotalHydrogenCount(mol));
}

@Test public void testConvertImplicitToExplicitHydrogens_IAtomContainer() throws Exception {
Expand Down Expand Up @@ -594,6 +598,22 @@ public void testGetTotalCharge() throws IOException, ClassNotFoundException, CDK
Assert.assertEquals(1.0, totalCharge, 0.01);
}

/**
* @cdk.bug 1254
*/
@Test(expected = IllegalArgumentException.class)
public void testCountExplicitH_Null_IAtom() {
AtomContainerManipulator.countExplicitHydrogens(null, DefaultChemObjectBuilder.getInstance().newInstance(IAtom.class));
}

/**
* @cdk.bug 1254
*/
@Test(expected = IllegalArgumentException.class)
public void testCountExplicitH_IAtomContainer_Null() {
AtomContainerManipulator.countExplicitHydrogens(DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class), null);
}

@Test
public void testCountExplicitH() {
IAtomContainer container = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class);
Expand Down Expand Up @@ -648,6 +668,39 @@ public void testCountH() throws Exception {

}

/**
* @cdk.bug 1254
*/
@Test
public void testGetImplicitHydrogenCount_unperceived() throws Exception {
IAtomContainer container = MoleculeFactory.makeAdenine();
Assert.assertEquals("Container has not been atom-typed - should have 0 implicit hydrogens",
0,
AtomContainerManipulator.getImplicitHydrogenCount(container));
}

/**
* @cdk.bug 1254
*/
@Test(expected = IllegalArgumentException.class)
public void testGetImplicitHydrogenCount_null() throws Exception {
AtomContainerManipulator.getImplicitHydrogenCount(null);
}

/**
* @cdk.bug 1254
*/
@Test
public void testGetImplicitHydrogenCount_adenine() throws Exception {
IAtomContainer container = MoleculeFactory.makeAdenine();
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(container);
CDKHydrogenAdder.getInstance(DefaultChemObjectBuilder.getInstance()).addImplicitHydrogens(container);
Assert.assertEquals("Adenine should have 5 implicit hydrogens",
5,
AtomContainerManipulator.getImplicitHydrogenCount(container));

}

@Test
public void testReplaceAtom() {
IAtomContainer container = DefaultChemObjectBuilder.getInstance().newInstance(IAtomContainer.class);
Expand Down

0 comments on commit 856781e

Please sign in to comment.