From: <mig...@us...> - 2008-11-16 10:20:17
|
Revision: 13121 http://cdk.svn.sourceforge.net/cdk/?rev=13121&view=rev Author: miguelrojasch Date: 2008-11-16 10:20:07 +0000 (Sun, 16 Nov 2008) Log Message: ----------- Added in MolecularFormulaManipulator the option to choose for set the integer value 1 or not in the character formula when a chemical formula contains an element with one atom. Modified Paths: -------------- cdk/branches/cdk-1.2.x/src/main/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulator.java cdk/branches/cdk-1.2.x/src/test/org/openscience/cdk/formula/MassToFormulaToolTest.java cdk/branches/cdk-1.2.x/src/test/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulatorTest.java Modified: cdk/branches/cdk-1.2.x/src/main/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulator.java =================================================================== --- cdk/branches/cdk-1.2.x/src/main/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulator.java 2008-11-16 10:15:56 UTC (rev 13120) +++ cdk/branches/cdk-1.2.x/src/main/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulator.java 2008-11-16 10:20:07 UTC (rev 13121) @@ -162,8 +162,10 @@ /** * Returns the string representation of the molecule formula. * - * @param formula The IMolecularFormula Object + * @param formula The IMolecularFormula Object * @param orderElements The order of Elements + * @param setOne True, when must be set the value 1 for elements with + * one atom * @return A String containing the molecular formula * * @see #getHTML(IMolecularFormula) @@ -172,13 +174,13 @@ * @see #generateOrderEle_Hill_WithCarbons() * */ - @TestMethod("testGetString_IMolecularFormula_String") - public static String getString(IMolecularFormula formula, String[] orderElements) { + @TestMethod("testGetString_IMolecularFormula_arrayString_boolean") + public static String getString(IMolecularFormula formula, String[] orderElements, boolean setOne) { String stringMF = ""; List<IIsotope> isotopesList = putInOrder(orderElements, formula); for (IIsotope isotope : isotopesList) { int elemCount = getElementCount(formula, isotope); - if (elemCount == 1) + if (elemCount == 1 && !setOne) stringMF = stringMF + isotope.getSymbol(); else stringMF = stringMF + isotope.getSymbol() + getElementCount(formula, isotope); @@ -203,10 +205,32 @@ @TestMethod("testGetString_IMolecularFormula") public static String getString(IMolecularFormula formula) { + return getString(formula, false); + } + + /** + * Returns the string representation of the molecule formula. + * Based on Hill System. The Hill system is a system of writing + * chemical formulas such that the number of carbon atoms in a + * molecule is indicated first, the number of hydrogen atoms next, + * and then the number of all other chemical elements subsequently, + * in alphabetical order. When the formula contains no carbon, all + * the elements, including hydrogen, are listed alphabetically. + * + * @param formula The IMolecularFormula Object + * @param setOne True, when must be set the value 1 for elements with + * one atom + * @return A String containing the molecular formula + * + * @see #getHTML(IMolecularFormula) + */ + @TestMethod("testGetString_IMolecularFormula_boolean") + public static String getString(IMolecularFormula formula, boolean setOne) { + if(containsElement(formula, formula.getBuilder().newElement("C"))) - return getString(formula, generateOrderEle_Hill_WithCarbons()); + return getString(formula, generateOrderEle_Hill_WithCarbons(), setOne); else - return getString(formula, generateOrderEle_Hill_NoCarbons()); + return getString(formula, generateOrderEle_Hill_NoCarbons(), setOne); } public static List<IIsotope> putInOrder(String[] orderElements, IMolecularFormula formula) { Modified: cdk/branches/cdk-1.2.x/src/test/org/openscience/cdk/formula/MassToFormulaToolTest.java =================================================================== --- cdk/branches/cdk-1.2.x/src/test/org/openscience/cdk/formula/MassToFormulaToolTest.java 2008-11-16 10:15:56 UTC (rev 13120) +++ cdk/branches/cdk-1.2.x/src/test/org/openscience/cdk/formula/MassToFormulaToolTest.java 2008-11-16 10:20:07 UTC (rev 13121) @@ -579,12 +579,12 @@ boolean found = false; for(IMolecularFormula formula : mfSet.molecularFormulas()) { String mf = MolecularFormulaManipulator.getString(formula); - if(mf.equals("C4H11N1O4")){ + if(mf.equals("C4H11NO4")){ found = true; break; } } - Assert.assertTrue("The molecular formula C4H11N1O4 should be found",found); + Assert.assertTrue("The molecular formula C4H11NO4 should be found",found); } /** @@ -624,12 +624,12 @@ boolean found = false; for(IMolecularFormula formula : mfSet.molecularFormulas()) { String mf = MolecularFormulaManipulator.getString(formula); - if(mf.equals("C11H10N1O2")){ + if(mf.equals("C11H10NO2")){ found = true; break; } } - Assert.assertTrue("The molecular formula C4H11N1O4 should be found",found); + Assert.assertTrue("The molecular formula C4H11NO4 should be found",found); } /** * A unit test suite for JUnit. C5H11N2O Modified: cdk/branches/cdk-1.2.x/src/test/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulatorTest.java =================================================================== --- cdk/branches/cdk-1.2.x/src/test/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulatorTest.java 2008-11-16 10:15:56 UTC (rev 13120) +++ cdk/branches/cdk-1.2.x/src/test/org/openscience/cdk/tools/manipulator/MolecularFormulaManipulatorTest.java 2008-11-16 10:20:07 UTC (rev 13121) @@ -166,7 +166,7 @@ * @return The test suite */ @Test - public void testGetString_IMolecularFormula_arrayString() { + public void testGetString_IMolecularFormula_arrayString_boolean() { IMolecularFormula formula = new MolecularFormula(); formula.addIsotope(builder.newIsotope("C"), 2); formula.addIsotope(builder.newIsotope("H"), 2); @@ -176,7 +176,7 @@ newOrder[0] = "H"; newOrder[1] = "C"; - Assert.assertEquals("H2C2",MolecularFormulaManipulator.getString(formula,newOrder)); + Assert.assertEquals("H2C2",MolecularFormulaManipulator.getString(formula,newOrder,true)); } /** @@ -253,7 +253,19 @@ Assert.assertEquals("CH4", MolecularFormulaManipulator.getString(mf1)); } + /** + * + */ @Test + public void testGetString_IMolecularFormula_boolean() { + IMolecularFormula mf1 = new MolecularFormula(); + mf1.addIsotope(builder.newIsotope("C"), 1); + mf1.addIsotope(builder.newIsotope("H"), 4); + + Assert.assertEquals("C1H4", MolecularFormulaManipulator.getString(mf1,true)); + } + + @Test public void testGetString_Isotopes() { IMolecularFormula mf1 = new MolecularFormula(); mf1.addIsotope(builder.newIsotope("C",12),9); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |