$darkmode
DENOPTIM
FormulaUtilsTest.java
Go to the documentation of this file.
1package denoptim.utils;
2
3/*
4 * DENOPTIM
5 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
6 * and Marco Foscato <marco.foscato@uib.no>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published
10 * by the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22import static org.junit.jupiter.api.Assertions.assertEquals;
23import static org.junit.jupiter.api.Assertions.assertFalse;
24import static org.junit.jupiter.api.Assertions.assertTrue;
25
26import java.util.ArrayList;
27import java.util.Collections;
28import java.util.List;
29import java.util.Map;
30
31import org.junit.jupiter.api.Test;
32import org.openscience.cdk.Atom;
33import org.openscience.cdk.interfaces.IAtomContainer;
34import org.openscience.cdk.interfaces.IChemObjectBuilder;
35import org.openscience.cdk.silent.SilentChemObjectBuilder;
36
43public class FormulaUtilsTest
44{
45
49 private IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
50
51//------------------------------------------------------------------------------
52
53 @Test
54 public void testParseFormula() throws Exception
55 {
56 String formula = "H2 O";
57 Map<String, Double> elCounts = FormulaUtils.parseFormula(formula);
58 assertEquals("H 2.0 O 1.0",convertSimpleToString(elCounts));
59
60 formula = "H2 O1";
61 elCounts = FormulaUtils.parseFormula(formula);
62 assertEquals("H 2.0 O 1.0",convertSimpleToString(elCounts));
63
64 formula = "O H2";
65 elCounts = FormulaUtils.parseFormula(formula);
66 assertEquals("H 2.0 O 1.0",convertSimpleToString(elCounts));
67
68 formula = "O1 H2";
69 elCounts = FormulaUtils.parseFormula(formula);
70 assertEquals("H 2.0 O 1.0",convertSimpleToString(elCounts));
71
72 formula = "O H2 C3 W2 Ca";
73 elCounts = FormulaUtils.parseFormula(formula);
74 assertEquals("C 3.0 Ca 1.0 H 2.0 O 1.0 W 2.0",
75 convertSimpleToString(elCounts));
76 }
77
78//------------------------------------------------------------------------------
79
80 @Test
81 public void testParsingOfCSDFormula() throws Exception
82 {
83 String formula = "H2 O";
84 Map<String, ArrayList<Double>> elCounts = FormulaUtils.parseCSDFormula(formula);
85 assertEquals("H 2.0 2.0 O 1.0 1.0",convertToString(elCounts));
86
87 formula = "H2 O1";
88 elCounts = FormulaUtils.parseCSDFormula(formula);
89 assertEquals("H 2.0 2.0 O 1.0 1.0",convertToString(elCounts));
90
91 formula = "O H2";
92 elCounts = FormulaUtils.parseCSDFormula(formula);
93 assertEquals("H 2.0 2.0 O 1.0 1.0",convertToString(elCounts));
94
95 formula = "O1 H2";
96 elCounts = FormulaUtils.parseCSDFormula(formula);
97 assertEquals("H 2.0 2.0 O 1.0 1.0",convertToString(elCounts));
98
99 formula = "O H2 C3 W2 Ca";
100 elCounts = FormulaUtils.parseCSDFormula(formula);
101 assertEquals("C 3.0 3.0 Ca 1.0 1.0 H 2.0 2.0 O 1.0 1.0 W 2.0 2.0",
102 convertToString(elCounts));
103
104 formula = "H2 O, C2 H6 O";
105 elCounts = FormulaUtils.parseCSDFormula(formula);
106 assertEquals("C 0.0 2.0 0.0 2.0 2.0 "
107 + "H 2.0 6.0 2.0 6.0 8.0 "
108 + "O 1.0 1.0 1.0 1.0 2.0",
109 convertToString(elCounts));
110
111 formula = "C2 H6 O 2-";
112 elCounts = FormulaUtils.parseCSDFormula(formula);
113 assertEquals("C 2.0 2.0 H 6.0 6.0 O 1.0 1.0",
114 convertToString(elCounts));
115
116 formula = "2(C16 H36 N1 1+),C8 Au1 N4 S4 2-,2(C3 H6 O1)";
117 elCounts = FormulaUtils.parseCSDFormula(formula);
118 assertEquals(
119 "Au 0.0 1.0 0.0 0.0 1.0 0.0 1.0 1.0 "
120 + "C 16.0 8.0 3.0 32.0 8.0 6.0 40.0 46.0 "
121 + "H 36.0 0.0 6.0 72.0 0.0 12.0 72.0 84.0 "
122 + "N 1.0 4.0 0.0 2.0 4.0 0.0 6.0 6.0 "
123 + "O 0.0 0.0 1.0 0.0 0.0 2.0 0.0 2.0 "
124 + "S 0.0 4.0 0.0 0.0 4.0 0.0 4.0 4.0",
125 convertToString(elCounts));
126
127 formula = "F5 Pd1,0.25(Br),0.5(Cl2)";
128 elCounts = FormulaUtils.parseCSDFormula(formula);
129 assertEquals(
130 "Br 0.0 1.0 0.0 0.0 1.0 0.0 1.0 1.0 "
131 + "Cl 0.0 0.0 2.0 0.0 0.0 2.0 0.0 2.0 "
132 + "F 5.0 0.0 0.0 5.0 0.0 0.0 5.0 5.0 "
133 + "Pd 1.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0",
134 convertToString(elCounts));
135
136 formula = "(H2 O)n,6n(N),2n(C),2n(F2)";
137 elCounts = FormulaUtils.parseCSDFormula(formula);
138 assertEquals(
139 "C 0.0 0.0 1.0 0.0 0.0 0.0 2.0 0.0 0.0 0.0 0.0 0.0 0.0 2.0 4.0 6.0 8.0 10.0 2.0 4.0 6.0 8.0 10.0 "
140 + "F 0.0 0.0 0.0 2.0 0.0 0.0 0.0 4.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 4.0 8.0 12.0 16.0 20.0 "
141 + "H 2.0 0.0 0.0 0.0 2.0 0.0 0.0 0.0 2.0 4.0 6.0 8.0 10.0 2.0 4.0 6.0 8.0 10.0 2.0 4.0 6.0 8.0 10.0 "
142 + "N 0.0 1.0 0.0 0.0 0.0 6.0 0.0 0.0 6.0 12.0 18.0 24.0 30.0 6.0 12.0 18.0 24.0 30.0 6.0 12.0 18.0 24.0 30.0 "
143 + "O 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0 1.0 2.0 3.0 4.0 5.0",
144 convertToString(elCounts));
145 }
146
147//------------------------------------------------------------------------------
148
149 private String convertSimpleToString(Map<String,Double> elCounts)
150 {
151 List<String> els = new ArrayList<String>();
152 els.addAll(elCounts.keySet());
153 Collections.sort(els);
154 StringBuilder sb = new StringBuilder();
155 for (String el : els)
156 {
157 sb.append(" ").append(el);
158 sb.append(GeneralUtils.getEnglishFormattedDecimal(" #.#", 1, elCounts.get(el)));
159 }
160 return sb.toString().trim();
161 }
162
163//------------------------------------------------------------------------------
164
165 private String convertToString(Map<String, ArrayList<Double>> elCounts)
166 {
167 List<String> els = new ArrayList<String>();
168 els.addAll(elCounts.keySet());
169 Collections.sort(els);
170 StringBuilder sb = new StringBuilder();
171 for (String el : els)
172 {
173 sb.append(" ").append(el);
174 for (Double v : elCounts.get(el))
175 {
176 sb.append(GeneralUtils.getEnglishFormattedDecimal(" #.#", 1, v));
177 }
178 }
179 return sb.toString().trim();
180 }
181
182//------------------------------------------------------------------------------
183
184 @Test
185 public void testCompareFormulaAndElementalAnalysis() throws Exception
186 {
187 String formula = "H2 O";
188 IAtomContainer mol = builder.newAtomContainer();
189 mol.addAtom(new Atom("H"));
190 mol.addAtom(new Atom("H"));
191 mol.addAtom(new Atom("O"));
192 assertTrue(FormulaUtils.compareFormulaAndElementalAnalysis(formula, mol));
193
194 mol.addAtom(new Atom("O"));
195 assertFalse(FormulaUtils.compareFormulaAndElementalAnalysis(formula, mol));
196
197 formula = "3(H2 O)";
198 mol = builder.newAtomContainer();
199 mol.addAtom(new Atom("H"));
200 mol.addAtom(new Atom("H"));
201 mol.addAtom(new Atom("O"));
202 mol.addAtom(new Atom("H"));
203 mol.addAtom(new Atom("H"));
204 mol.addAtom(new Atom("O"));
205 mol.addAtom(new Atom("H"));
206 mol.addAtom(new Atom("H"));
207 mol.addAtom(new Atom("O"));
208 assertTrue(FormulaUtils.compareFormulaAndElementalAnalysis(formula, mol));
209
210 formula = "C6 H6,2(H2 O)"; // Only largest molecule present!
211 mol = builder.newAtomContainer();
212 mol.addAtom(new Atom("C"));
213 mol.addAtom(new Atom("H"));
214 mol.addAtom(new Atom("C"));
215 mol.addAtom(new Atom("H"));
216 mol.addAtom(new Atom("C"));
217 mol.addAtom(new Atom("H"));
218 mol.addAtom(new Atom("C"));
219 mol.addAtom(new Atom("H"));
220 mol.addAtom(new Atom("C"));
221 mol.addAtom(new Atom("H"));
222 mol.addAtom(new Atom("C"));
223 mol.addAtom(new Atom("H"));
224 assertTrue(FormulaUtils.compareFormulaAndElementalAnalysis(formula, mol));
225
226 formula = "(C6 H6)n,2n(H2 O),n(C2 H4)"; // all mols
227 mol = builder.newAtomContainer();
228 mol.addAtom(new Atom("C"));
229 mol.addAtom(new Atom("H"));
230 mol.addAtom(new Atom("C"));
231 mol.addAtom(new Atom("H"));
232 mol.addAtom(new Atom("C"));
233 mol.addAtom(new Atom("H"));
234 mol.addAtom(new Atom("C"));
235 mol.addAtom(new Atom("H"));
236 mol.addAtom(new Atom("C"));
237 mol.addAtom(new Atom("H"));
238 mol.addAtom(new Atom("C"));
239 mol.addAtom(new Atom("H"));
240 mol.addAtom(new Atom("H"));
241 mol.addAtom(new Atom("H"));
242 mol.addAtom(new Atom("O"));
243 mol.addAtom(new Atom("H"));
244 mol.addAtom(new Atom("H"));
245 mol.addAtom(new Atom("O"));
246 mol.addAtom(new Atom("C"));
247 mol.addAtom(new Atom("H"));
248 mol.addAtom(new Atom("H"));
249 mol.addAtom(new Atom("C"));
250 mol.addAtom(new Atom("H"));
251 mol.addAtom(new Atom("H"));
252 assertTrue(FormulaUtils.compareFormulaAndElementalAnalysis(formula, mol));
253
254 }
255
256//------------------------------------------------------------------------------
257
258}
Utilities for manipulating molecular formulas.
static boolean compareFormulaAndElementalAnalysis(String formula, IAtomContainer mol)
Compares the molecular formula formatted as from the Cambridge Structural Database (CSD) against the ...
static Map< String, ArrayList< Double > > parseCSDFormula(String formula)
Reads a molecular formula formatted as from the Cambridge Structural Database and produces a profile ...
static Map< String, Double > parseFormula(String formula)
Reads a molecular formula written as "H2 O" or "C6 H12 O6".
Unit test for tools manipulating molecular formulae.
IChemObjectBuilder builder
Private builder of atom containers.
String convertSimpleToString(Map< String, Double > elCounts)
String convertToString(Map< String, ArrayList< Double > > elCounts)
static String getEnglishFormattedDecimal(String pattern, int decimals, double value)
Formats a decimal number using the given pattern but with English format as for separators.