From: <mig...@us...> - 2006-05-08 09:25:44
|
Revision: 6192 Author: miguelrojasch Date: 2006-05-08 02:25:26 -0700 (Mon, 08 May 2006) ViewCVS: http://svn.sourceforge.net/cdk/?rev=6192&view=rev Log Message: ----------- new methods which return the total positive formal charge and negative of a atomContainer Modified Paths: -------------- trunk/cdk/src/org/openscience/cdk/test/tools/manipulator/AtomContainerManipulatorTest.java trunk/cdk/src/org/openscience/cdk/tools/manipulator/AtomContainerManipulator.java Modified: trunk/cdk/src/org/openscience/cdk/test/tools/manipulator/AtomContainerManipulatorTest.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/test/tools/manipulator/AtomContainerManipulatorTest.java 2006-05-08 09:07:00 UTC (rev 6191) +++ trunk/cdk/src/org/openscience/cdk/test/tools/manipulator/AtomContainerManipulatorTest.java 2006-05-08 09:25:26 UTC (rev 6192) @@ -73,7 +73,6 @@ mol.addBond(1,5,1); assertEquals(6, mol.getAtomCount()); assertEquals(5, mol.getBondCount()); - System.out.println(mol); assertEquals(0, AtomContainerManipulator.getTotalHydrogenCount(mol)); } @@ -125,6 +124,51 @@ assertEquals("incorrect hydrogen count", 4, ac.getAtomAt(0).getHydrogenCount()); assertEquals("incorrect hydrogen count", 4, ac.getAtomAt(1).getHydrogenCount()); } + /** + * Test total formal charge. + * + * @throws IOException + * @throws ClassNotFoundException + * @throws CDKException + */ + public void testgetTotalFormalCharge() throws IOException, ClassNotFoundException, CDKException + { + SmilesParser parser = new SmilesParser(); + Molecule mol = parser.parseSmiles("[C-]C[C+][C+]C"); + int totalCharge = AtomContainerManipulator.getTotalFormalCharge((IAtomContainer)mol); + + assertEquals(1,totalCharge); + } + /** + * Test total positive formal charge. + * + * @throws IOException + * @throws ClassNotFoundException + * @throws CDKException + */ + public void testgetTotalPositiveFormalCharge() throws IOException, ClassNotFoundException, CDKException + { + SmilesParser parser = new SmilesParser(); + Molecule mol = parser.parseSmiles("[C-]C[C+][C+]C"); + int totalCharge = AtomContainerManipulator.getTotalPositiveFormalCharge((IAtomContainer)mol); + + assertEquals(2,totalCharge); + } + /** + * Test total negative formal charge. + * + * @throws IOException + * @throws ClassNotFoundException + * @throws CDKException + */ + public void testgetTotalNegativeFormalCharge() throws IOException, ClassNotFoundException, CDKException + { + SmilesParser parser = new SmilesParser(); + Molecule mol = parser.parseSmiles("[C-]C[C+][C+]C"); + int totalCharge = AtomContainerManipulator.getTotalNegativeFormalCharge((IAtomContainer)mol); + + assertEquals(-1,totalCharge); + } } Modified: trunk/cdk/src/org/openscience/cdk/tools/manipulator/AtomContainerManipulator.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/tools/manipulator/AtomContainerManipulator.java 2006-05-08 09:07:00 UTC (rev 6191) +++ trunk/cdk/src/org/openscience/cdk/tools/manipulator/AtomContainerManipulator.java 2006-05-08 09:25:26 UTC (rev 6192) @@ -101,12 +101,36 @@ * @return The summed formal charges of all atoms in this AtomContainer. */ public static int getTotalFormalCharge(IAtomContainer atomContainer) { + int chargeP = getTotalNegativeFormalCharge(atomContainer); + int chargeN = getTotalPositiveFormalCharge(atomContainer); + int totalCharge = chargeP + chargeN; + + return totalCharge; + } + /** + * @return The summed negative formal charges of all atoms in this AtomContainer. + */ + public static int getTotalNegativeFormalCharge(IAtomContainer atomContainer) { int charge = 0; for (int i = 0; i < atomContainer.getAtomCount(); i++) { - charge += atomContainer.getAtomAt(i).getFormalCharge(); + double chargeI = atomContainer.getAtomAt(i).getFormalCharge(); + if(chargeI < 0) + charge += chargeI; } return charge; } + /** + * @return The summed positive formal charges of all atoms in this AtomContainer. + */ + public static int getTotalPositiveFormalCharge(IAtomContainer atomContainer) { + int charge = 0; + for (int i = 0; i < atomContainer.getAtomCount(); i++) { + double chargeI = atomContainer.getAtomAt(i).getFormalCharge(); + if(chargeI > 0) + charge += chargeI; + } + return charge; + } /** * @return The summed implicit hydrogens of all atoms in this AtomContainer. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |