3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.HashMap;
6import java.util.HashSet;
10import java.util.logging.Level;
11import java.util.logging.Logger;
13import org.openscience.cdk.Isotope;
14import org.openscience.cdk.formula.MolecularFormula;
15import org.openscience.cdk.interfaces.IAtom;
16import org.openscience.cdk.interfaces.IAtomContainer;
17import org.openscience.cdk.interfaces.IIsotope;
18import org.openscience.cdk.tools.manipulator.MolecularFormulaManipulator;
20import denoptim.exception.DENOPTIMException;
21import denoptim.io.DenoptimIO;
40 Map<String,Double> elementsMap =
new HashMap<String,Double>();
41 String[] words = formula.split(
"\\s+");
42 for (
int i=0; i<words.length; i++)
47 while (characterIdx<words[i].length()
48 && Character.isLetter(words[i].charAt(characterIdx)))
52 elSymbol = words[i].substring(0,characterIdx);
53 if (characterIdx<words[i].length())
55 elCount = Double.parseDouble(words[i].substring(characterIdx));
59 elementsMap.put(elSymbol, elCount);
88 ArrayList<Map<String,Integer>> allAtmCounts =
89 new ArrayList<Map<String,Integer>>();
91 String[] mols = formula.split(
",");
94 List<Double> stocFact =
new ArrayList<Double>();
95 List<Boolean> tuneStFact =
new ArrayList<Boolean>();
96 List<Boolean> tuneStFactToInt =
new ArrayList<Boolean>();
97 for (
int i=0; i < mols.length; i++)
100 tuneStFact.add(i,
false);
101 tuneStFactToInt.add(i,
false);
102 String locForm = mols[i].trim();
103 boolean found =
false;
105 if (locForm.contains(
"("))
108 if (locForm.lastIndexOf(
")") == (locForm.length()-1))
111 String[] spt = locForm.split(
"[()]");
112 if (spt[0].contains(
"n"))
115 if (spt[0].lastIndexOf(
"n") == 0)
118 tuneStFact.set(i,
true);
121 }
else if (spt[0].lastIndexOf(
"n") ==
125 stocFact.set(i,Double.parseDouble(
126 spt[0].substring(0,spt[0].length() -1)));
127 tuneStFact.set(i,
true);
131 }
else if (spt[0].contains(
"x"))
134 if (spt[0].lastIndexOf(
"x") == 0)
137 tuneStFact.set(i,
true);
140 }
else if (spt[0].lastIndexOf(
"x")
141 == (spt[0].length()-1))
144 stocFact.set(i,Double.parseDouble(
145 spt[0].substring(0,spt[0].length() -1)));
146 tuneStFact.set(i,
true);
151 stocFact.set(i,Double.parseDouble(spt[0]));
155 }
else if (locForm.lastIndexOf(
"(") == 0)
158 String[] sptR = locForm.split(
"[()]");
162 if (sptR[2].length() == 1)
164 tuneStFact.set(i,
true);
166 }
else if (sptR[2].lastIndexOf(
"x")
167 == (sptR[0].length()-1)) {
169 stocFact.set(i,Double.parseDouble(
170 sptR[2].substring(0,sptR[2].length() -1)));
171 tuneStFact.set(i,
true);
181 formula +
"' has a syntax that is not "
186 double mod = stocFact.get(i) % 1.0;
188 tuneStFactToInt.set(i,
true);
193 Map<String,Integer> locCount =
new HashMap<String,Integer>();
194 String [] lmnts = locForm.split(
"\\s+");
195 for (
int l = 0; l < lmnts.length; l++)
198 if (lmnts[l].endsWith(
"+") || lmnts[l].endsWith(
"-"))
202 String elSymbol =
"";
205 while (fd<lmnts[l].length()
206 && Character.isLetter(lmnts[l].charAt(fd)))
210 elSymbol = lmnts[l].substring(0,fd);
211 if (fd<lmnts[l].length())
213 elCount = Integer.parseInt(lmnts[l].substring(fd));
219 locCount.put(elSymbol,elCount);
222 allAtmCounts.add(i,locCount);
227 double largestMass = 0.0;
228 for (
int i = 0; i < mols.length; i++)
230 MolecularFormula molForm =
new MolecularFormula();
231 for (String el : allAtmCounts.get(i).keySet())
233 IIsotope is =
new Isotope(el);
234 molForm.addIsotope(is,allAtmCounts.get(i).get(el));
236 double mass = MolecularFormulaManipulator.getMass(molForm);
237 if (mass > largestMass)
244 Set<String> allEl =
new HashSet<String>();
245 for (
int i = 0; i < mols.length; i++)
246 for (String el : allAtmCounts.get(i).keySet())
250 Map<String,ArrayList<Double>> elemAnalFormula =
251 new HashMap<String,ArrayList<Double>>();
252 for (
int i = 0; i < mols.length; i++)
254 for (String el : allEl)
257 if (allAtmCounts.get(i).containsKey(el))
259 num = allAtmCounts.get(i).get(el);
261 if (!elemAnalFormula.containsKey(el))
262 elemAnalFormula.put(el,
new ArrayList<Double>(Arrays.asList(num)));
264 elemAnalFormula.get(el).add(num);
269 for (
int i = 0; i < mols.length; i++)
271 for (String el : allEl)
274 if (allAtmCounts.get(i).containsKey(el))
277 if (tuneStFactToInt.get(i))
278 pf = pf * (1.0 / stocFact.get(i));
279 num = allAtmCounts.get(i).get(el) * pf * stocFact.get(i);
281 if (!elemAnalFormula.containsKey(el))
282 elemAnalFormula.put(el,
new ArrayList<Double>(
283 Arrays.asList(num)));
285 elemAnalFormula.get(el).add(num);
292 for (
int n = 2; n <= mols.length; n++)
296 boolean doTuning =
true;
297 for (
int i = 0; i < mols.length; i++)
299 if (!tuneStFact.get(i))
305 for (String el : allEl)
307 double thisElCount = 0.0;
309 for (
int i = 0; i < n; i++)
311 if (allAtmCounts.get(i).containsKey(el))
314 if (tuneStFactToInt.get(i))
315 pf = pf * (1.0 / stocFact.get(i));
316 thisElCount = thisElCount + allAtmCounts.get(i).get(el) * pf * stocFact.get(i);
319 elemAnalFormula.get(el).add(thisElCount);
325 for (
int pf = 2; pf < limSF ; pf ++)
328 double thisElCountTune = 0.0;
330 for (
int i = 0; i < n; i++)
332 if (allAtmCounts.get(i).containsKey(el))
334 thisElCountTune = thisElCountTune + allAtmCounts.get(i).get(el) * pfd * stocFact.get(i);
337 elemAnalFormula.get(el).add(thisElCountTune);
344 return elemAnalFormula;
389 Map<String,ArrayList<Double>> elemAnalFormula =
391 if (logger!=
null && logger.getLevel() == Level.FINEST)
393 StringBuilder sb =
new StringBuilder();
394 for (String el : elemAnalFormula.keySet())
395 sb.append(
DenoptimIO.
NL).append(el+
" "+elemAnalFormula.get(el));
396 logger.log(Level.FINEST,
"Elemental analysis from formula: "
402 if (logger!=
null && logger.getLevel() == Level.FINEST)
404 StringBuilder sb =
new StringBuilder();
405 for (String el : elemAnalMolInfo.keySet())
406 sb.append(
DenoptimIO.
NL).append(el+
" "+elemAnalMolInfo.get(el));
407 logger.log(Level.FINEST,
"Elemental analysis from atom structure: "
417 int numCandidates = 0;
418 for (String el : elemAnalFormula.keySet())
420 numCandidates = elemAnalFormula.get(el).size();
424 boolean foundMatch =
false;
426 for (
int i=0; i<numCandidates; i++)
428 for (String el : elemAnalFormula.keySet())
430 if (elemAnalFormula.get(el).get(i)>0.01
431 && !elemAnalMolInfo.containsKey(el))
433 continue loopOverCandidate;
435 if (elemAnalMolInfo.containsKey(el))
438 elemAnalFormula.get(el).get(i)
439 - elemAnalMolInfo.get(el))
442 continue loopOverCandidate;
445 if (Math.abs(elemAnalFormula.get(el).get(i) - 0.0)
448 continue loopOverCandidate;
467 Map<String,Double> elemAnalMolInfo =
new HashMap<String,Double>();
468 for (IAtom atm : mol.atoms())
470 String elSymbol = atm.getSymbol();
472 if (atm.getMassNumber()!=
null && atm.getMassNumber() == 2)
474 if (elemAnalMolInfo.keySet().contains(elSymbol))
476 double num = elemAnalMolInfo.get(elSymbol) + 1.0;
477 elemAnalMolInfo.put(elSymbol,num);
479 elemAnalMolInfo.put(elSymbol,1.0);
482 return elemAnalMolInfo;
Utility methods for input/output.
static final String NL
Newline character from system.