Skip to content

Commit

Permalink
Updated a method based on Ninas suggestion to avoid modifiying a atom…
Browse files Browse the repository at this point in the history
… container when looping over it. This avoids a concurrent modification exception. Also updated some Javadocs

Change-Id: If01762c6cff615d7a25de8b8f9bcee49c41eba16
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
rajarshi authored and egonw committed Jun 25, 2012
1 parent 3f3fab5 commit eba09ca
Showing 1 changed file with 30 additions and 9 deletions.
Expand Up @@ -226,6 +226,9 @@ public static double getTotalNaturalAbundance(IAtomContainer atomContainer) {
}

/**
* Get the total formal charge on a molecule.
*
* @param atomContainer the atom container to consider
* @return The summed formal charges of all atoms in this AtomContainer.
*/
@TestMethod("testGetTotalFormalCharge_IAtomContainer")
Expand All @@ -236,6 +239,9 @@ public static int getTotalFormalCharge(IAtomContainer atomContainer) {
return chargeP + chargeN;
}
/**
* Get the total formal negative charge on a molecule.
*
* @param atomContainer the atom container to consider
* @return The summed negative formal charges of all atoms in this AtomContainer.
*/
@TestMethod("testGetTotalNegativeFormalCharge_IAtomContainer")
Expand All @@ -249,6 +255,9 @@ public static int getTotalNegativeFormalCharge(IAtomContainer atomContainer) {
return charge;
}
/**
* Get the total positive formal charge on a molecule.
*
* @param atomContainer the atom container to consider
* @return The summed positive formal charges of all atoms in this AtomContainer.
*/
@TestMethod("testGetTotalPositiveFormalCharge_IAtomContainer")
Expand All @@ -263,6 +272,9 @@ public static int getTotalPositiveFormalCharge(IAtomContainer atomContainer) {
}

/**
* Count the total number of hydrogens (implicit and explicit).
*
* @param atomContainer the atom container to consider
* @return The summed implicit hydrogens of all atoms in this AtomContainer.
*/
@TestMethod("testGetTotalHydrogenCount_IAtomContainer,testGetTotalHydrogenCount_IAtomContainer_zeroImplicit,testGetTotalHydrogenCount_IAtomContainer_nullImplicit,testGetTotalHydrogenCount_ImplicitHydrogens")
Expand All @@ -277,6 +289,9 @@ public static int getTotalHydrogenCount(IAtomContainer atomContainer) {
}

/**
* Count explicit hydrogens.
*
* @param atomContainer the atom container to consider
* @return The number of explicit hydrogens on the given IAtom.
*/
@TestMethod("testCountExplicitH")
Expand All @@ -294,29 +309,35 @@ public static int countExplicitHydrogens(IAtomContainer atomContainer, IAtom ato
* Adds explicit hydrogens (without coordinates) to the IAtomContainer,
* equaling the number of set implicit hydrogens.
*
* @param atomContainer the atom container to consider
* @cdk.keyword hydrogens, adding
*/
@TestMethod("testConvertImplicitToExplicitHydrogens_IAtomContainer")
public static void convertImplicitToExplicitHydrogens(IAtomContainer atomContainer) {
List<IAtom> hydrogens = new ArrayList<IAtom>();
List<IBond> newBonds = new ArrayList<IBond>();
List<Integer> atomIndex = new ArrayList<Integer>();

for (IAtom atom : atomContainer.atoms()) {
if (!atom.getSymbol().equals("H")) {
Integer hCount = atom.getImplicitHydrogenCount();
if (hCount != null) {
for (int i = 0; i < hCount; i++) {
IAtom hydrogen = atom.getBuilder().newInstance(IAtom.class,"H");

IAtom hydrogen = atom.getBuilder().newInstance(IAtom.class, "H");
hydrogen.setAtomTypeName("H");
atomContainer.addAtom(hydrogen);
atomContainer.addBond(
atom.getBuilder().newInstance(IBond.class,
atom, hydrogen,
CDKConstants.BONDORDER_SINGLE
)
);
hydrogens.add(hydrogen);
newBonds.add(atom.getBuilder().newInstance(IBond.class,
atom, hydrogen, CDKConstants.BONDORDER_SINGLE
));
}
atom.setImplicitHydrogenCount(0);
atomIndex.add(atomContainer.getAtomNumber(atom));
}
}
}
for (Integer index : atomIndex) atomContainer.getAtom(index).setImplicitHydrogenCount(0);
for (IAtom atom : hydrogens) atomContainer.addAtom(atom);
for (IBond bond : newBonds) atomContainer.addBond(bond);
}

/**
Expand Down

0 comments on commit eba09ca

Please sign in to comment.