Skip to content
Permalink
Browse files

Added Ninas modifcations to ensure that getting a molecular formula i…

…ncludes 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>
  • Loading branch information...
rajarshi authored and egonw committed Jun 25, 2012
1 parent eba09ca commit 37ca43cc4ea0f0a7efaf10edbbe60ef57a44e8ce
@@ -756,17 +756,25 @@ public static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer
* @see #getMolecularFormula(IAtomContainer)
*/
@TestMethod("testGetMolecularFormula_IAtomContainer_IMolecularFormula")
public static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer, IMolecularFormula formula) {
int charge = 0;
for (IAtom iAtom : atomContainer.atoms()) {
formula.addIsotope(iAtom);
charge += iAtom.getFormalCharge();
}
formula.setCharge(charge);
return formula;
}

/**
public static IMolecularFormula getMolecularFormula(IAtomContainer atomContainer, IMolecularFormula formula) {
int charge = 0;
IAtom hAtom = null;
for (IAtom iAtom : atomContainer.atoms()) {
formula.addIsotope(iAtom);
charge += iAtom.getFormalCharge();

if (iAtom.getImplicitHydrogenCount() != null &&
(iAtom.getImplicitHydrogenCount() > 0)) {
if (hAtom == null) hAtom =
atomContainer.getBuilder().newInstance(IAtom.class, "H");
formula.addIsotope(hAtom, iAtom.getImplicitHydrogenCount());
}
}
formula.setCharge(charge);
return formula;
}

/**
* Method that actually does the work of convert the IMolecularFormula
* to IAtomContainer.
* <p> The hydrogens must be implicit.
@@ -20,12 +20,6 @@
*/
package org.openscience.cdk.tools.manipulator;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.Atom;
@@ -46,8 +40,14 @@
import org.openscience.cdk.io.MDLV2000Reader;
import org.openscience.cdk.nonotify.NoNotificationChemObjectBuilder;
import org.openscience.cdk.smiles.SmilesParser;
import org.openscience.cdk.templates.MoleculeFactory;
import org.openscience.cdk.tools.CDKHydrogenAdder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
* Checks the functionality of the MolecularFormulaManipulator.
*
@@ -885,7 +885,7 @@ public void testSingleAtomFromSmiles() throws InvalidSmilesException {
IAtomContainer mol = sp.parseSmiles("C");
IMolecularFormula mf = MolecularFormulaManipulator.getMolecularFormula(mol);
double exactMass = MolecularFormulaManipulator.getTotalExactMass(mf);
Assert.assertEquals(12.0000, exactMass, 0.0001);
Assert.assertEquals(16.0313, exactMass, 0.0001);
}

@Test
@@ -1055,4 +1055,26 @@ public void testAmericum() {
Assert.assertNotNull(formula);
Assert.assertEquals("Am", MolecularFormulaManipulator.getString(formula));
}

/**
* @cdk.bug 2983334
*/
@Test
public void testImplicitH() throws Exception {

CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(NoNotificationChemObjectBuilder.getInstance());

IAtomContainer mol = MoleculeFactory.makeBenzene();

IMolecularFormula f = MolecularFormulaManipulator.getMolecularFormula(mol);
Assert.assertEquals("C6", MolecularFormulaManipulator.getString(f));

Assert.assertEquals(6, mol.getAtomCount());
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
adder.addImplicitHydrogens(mol);
Assert.assertEquals(6, mol.getAtomCount());
f = MolecularFormulaManipulator.getMolecularFormula(mol);
Assert.assertEquals("C6H6", MolecularFormulaManipulator.getString(f));

}
}

0 comments on commit 37ca43c

Please sign in to comment.
You can’t perform that action at this time.