Skip to content

Commit 37ca43c

Browse files
rajarshiegonw
authored andcommitted
Added Ninas modifcations to ensure that getting a molecular formula includes implicit hydrogens. Addressed bug 2983334. Also updated a unit test to take into account that H's are being considered
Change-Id: I9881e31c05d0f1bfb965393bd806e562a6158a0e Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
1 parent eba09ca commit 37ca43c

2 files changed

Lines changed: 48 additions & 18 deletions

File tree

src/main/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulator.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -756,17 +756,25 @@ public static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer
756756
* @see #getMolecularFormula(IAtomContainer)
757757
*/
758758
@TestMethod("testGetMolecularFormula_IAtomContainer_IMolecularFormula")
759-
public static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer, IMolecularFormula formula) {
760-
int charge = 0;
761-
for (IAtom iAtom : atomContainer.atoms()) {
762-
formula.addIsotope(iAtom);
763-
charge += iAtom.getFormalCharge();
764-
}
765-
formula.setCharge(charge);
766-
return formula;
767-
}
768-
769-
/**
759+
public static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer, IMolecularFormula formula) {
760+
int charge = 0;
761+
IAtom hAtom = null;
762+
for (IAtom iAtom : atomContainer.atoms()) {
763+
formula.addIsotope(iAtom);
764+
charge += iAtom.getFormalCharge();
765+
766+
if (iAtom.getImplicitHydrogenCount() != null &&
767+
(iAtom.getImplicitHydrogenCount() > 0)) {
768+
if (hAtom == null) hAtom =
769+
atomContainer.getBuilder().newInstance(IAtom.class, "H");
770+
formula.addIsotope(hAtom, iAtom.getImplicitHydrogenCount());
771+
}
772+
}
773+
formula.setCharge(charge);
774+
return formula;
775+
}
776+
777+
/**
770778
* Method that actually does the work of convert the IMolecularFormula
771779
* to IAtomContainer.
772780
* <p> The hydrogens must be implicit.

src/test/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulatorTest.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
*/
2121
package org.openscience.cdk.tools.manipulator;
2222

23-
import java.io.IOException;
24-
import java.io.InputStream;
25-
import java.util.ArrayList;
26-
import java.util.Arrays;
27-
import java.util.List;
28-
2923
import org.junit.Assert;
3024
import org.junit.Test;
3125
import org.openscience.cdk.Atom;
@@ -46,8 +40,14 @@
4640
import org.openscience.cdk.io.MDLV2000Reader;
4741
import org.openscience.cdk.nonotify.NoNotificationChemObjectBuilder;
4842
import org.openscience.cdk.smiles.SmilesParser;
43+
import org.openscience.cdk.templates.MoleculeFactory;
4944
import org.openscience.cdk.tools.CDKHydrogenAdder;
5045

46+
import java.io.IOException;
47+
import java.io.InputStream;
48+
import java.util.Arrays;
49+
import java.util.List;
50+
5151
/**
5252
* Checks the functionality of the MolecularFormulaManipulator.
5353
*
@@ -885,7 +885,7 @@ public void testSingleAtomFromSmiles() throws InvalidSmilesException {
885885
IAtomContainer mol = sp.parseSmiles("C");
886886
IMolecularFormula mf = MolecularFormulaManipulator.getMolecularFormula(mol);
887887
double exactMass = MolecularFormulaManipulator.getTotalExactMass(mf);
888-
Assert.assertEquals(12.0000, exactMass, 0.0001);
888+
Assert.assertEquals(16.0313, exactMass, 0.0001);
889889
}
890890

891891
@Test
@@ -1055,4 +1055,26 @@ public void testAmericum() {
10551055
Assert.assertNotNull(formula);
10561056
Assert.assertEquals("Am", MolecularFormulaManipulator.getString(formula));
10571057
}
1058+
1059+
/**
1060+
* @cdk.bug 2983334
1061+
*/
1062+
@Test
1063+
public void testImplicitH() throws Exception {
1064+
1065+
CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(NoNotificationChemObjectBuilder.getInstance());
1066+
1067+
IAtomContainer mol = MoleculeFactory.makeBenzene();
1068+
1069+
IMolecularFormula f = MolecularFormulaManipulator.getMolecularFormula(mol);
1070+
Assert.assertEquals("C6", MolecularFormulaManipulator.getString(f));
1071+
1072+
Assert.assertEquals(6, mol.getAtomCount());
1073+
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
1074+
adder.addImplicitHydrogens(mol);
1075+
Assert.assertEquals(6, mol.getAtomCount());
1076+
f = MolecularFormulaManipulator.getMolecularFormula(mol);
1077+
Assert.assertEquals("C6H6", MolecularFormulaManipulator.getString(f));
1078+
1079+
}
10581080
}

0 commit comments

Comments
 (0)