/
BasicValidator.java
398 lines (377 loc) · 16.1 KB
/
BasicValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* $RCSfile$
* $Author$
* $Date$
* $Revision$
*
* Copyright (C) 2003-2006 The Chemistry Development Kit (CDK) project
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
package org.openscience.cdk.validate;
import org.openscience.cdk.Atom;
import org.openscience.cdk.AtomContainer;
import org.openscience.cdk.Bond;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IElement;
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.interfaces.IMoleculeSet;
import org.openscience.cdk.Molecule;
import org.openscience.cdk.PseudoAtom;
import org.openscience.cdk.Reaction;
import org.openscience.cdk.config.AtomTypeFactory;
import org.openscience.cdk.config.IsotopeFactory;
import org.openscience.cdk.tools.LoggingTool;
/**
* Validator which tests a number of basic chemical semantics.
*
* @author Egon Willighagen
* @cdk.created 2003-08-22
*/
public class BasicValidator extends AbstractValidator {
private static LoggingTool logger;
static {
logger = new LoggingTool(BasicValidator.class);
}
public BasicValidator() {
}
public ValidationReport validateAtom(Atom subject) {
ValidationReport report = new ValidationReport();
report.addReport(validateCharge(subject));
report.addReport(validateHydrogenCount(subject));
report.addReport(validatePseudoAtom(subject));
return report;
}
public ValidationReport validateBond(Bond subject) {
ValidationReport report = new ValidationReport();
report.addReport(validateStereoChemistry(subject));
report.addReport(validateMaxBondOrder(subject));
return report;
}
public ValidationReport validateIsotope(IIsotope subject) {
return validateIsotopeExistence(subject);
}
public ValidationReport validateMolecule(Molecule subject) {
ValidationReport report = new ValidationReport();
ValidationTest emptyMolecule = new ValidationTest(subject,
"Molecule does not contain any atom"
);
if (subject.getAtomCount() == 0) {
report.addError(emptyMolecule);
} else {
report.addOK(emptyMolecule);
ValidationTest massCalcProblem = new ValidationTest(subject,
"Molecule contains PseudoAtom's. Won't be able to calculate some properties, like molecular mass."
);
boolean foundMassCalcProblem = false;
for (int i=0; i<subject.getAtomCount(); i++) {
if (subject.getAtom(i) instanceof PseudoAtom) {
foundMassCalcProblem = true;
} else {
report.addReport(validateBondOrderSum(subject.getAtom(i), subject));
}
}
if (foundMassCalcProblem) {
report.addWarning(massCalcProblem);
} else {
report.addOK(massCalcProblem);
}
}
return report;
}
public ValidationReport validateReaction(Reaction subject) {
ValidationReport report = new ValidationReport();
AtomContainer container1 = new org.openscience.cdk.AtomContainer();
IMoleculeSet reactants = subject.getReactants();
for (int i=0; i<reactants.getAtomContainerCount(); i++) {
container1.add(reactants.getMolecule(i));
}
AtomContainer container2 = new org.openscience.cdk.AtomContainer();
IMoleculeSet products = subject.getProducts();
for (int i=0; i<products.getAtomContainerCount(); i++) {
container2.add(products.getMolecule(i));
}
report.addReport(validateAtomCountConservation(subject, container1, container2));
report.addReport(validateChargeConservation(subject, container1, container2));
return report;
}
// the Atom tests
private ValidationReport validateCharge(Atom atom) {
ValidationReport report = new ValidationReport();
ValidationTest tooCharged = new ValidationTest(atom, "Atom has an unlikely large positive or negative charge");
if (atom.getSymbol().equals("O") || atom.getSymbol().equals("N") ||
atom.getSymbol().equals("C") || atom.getSymbol().equals("H")) {
if (atom.getFormalCharge() == 0) {
report.addOK(tooCharged);
} else {
tooCharged.setDetails("Atom " + atom.getSymbol() + " has charge " + atom.getFormalCharge() );
if (atom.getFormalCharge() < -3) {
report.addError(tooCharged);
} else if (atom.getFormalCharge() < -1) {
report.addWarning(tooCharged);
} else if (atom.getFormalCharge() > 3) {
report.addError(tooCharged);
} else if (atom.getFormalCharge() > 1) {
report.addWarning(tooCharged);
}
}
} else {
if (atom.getFormalCharge() == 0) {
report.addOK(tooCharged);
} else {
tooCharged.setDetails("Atom " + atom.getSymbol() + " has charge " + atom.getFormalCharge() );
if (atom.getFormalCharge() < -4) {
report.addError(tooCharged);
} else if (atom.getFormalCharge() < -3) {
report.addWarning(tooCharged);
} else if (atom.getFormalCharge() > 4) {
report.addError(tooCharged);
} else if (atom.getFormalCharge() > 3) {
report.addWarning(tooCharged);
}
}
}
return report;
}
private ValidationReport validateHydrogenCount(Atom atom) {
ValidationReport report = new ValidationReport();
ValidationTest negativeHydrogenCount = new ValidationTest(atom,
"An Atom cannot have a negative number of hydrogens attached."
);
if (atom.getHydrogenCount() < 0 ) {
negativeHydrogenCount.setDetails(
"Atom has " + atom.getHydrogenCount() + " hydrogens."
);
report.addError(negativeHydrogenCount);
} else {
report.addOK(negativeHydrogenCount);
}
return report;
}
private ValidationReport validatePseudoAtom(Atom atom) {
ValidationReport report = new ValidationReport();
ValidationTest isElementOrPseudo = new ValidationTest(atom,
"Non-element atom must be of class PseudoAtom."
);
if (atom instanceof PseudoAtom) {
// that's fine
report.addOK(isElementOrPseudo);
} else {
// check wether atom is really an element
try {
IsotopeFactory isotopeFactory = IsotopeFactory.getInstance(atom.getBuilder());
IElement element = isotopeFactory.getElement(atom.getSymbol());
if (element == null) {
isElementOrPseudo.setDetails(
"Element " + atom.getSymbol() + " does not exist."
);
report.addError(isElementOrPseudo);
} else {
report.addOK(isElementOrPseudo);
}
} catch (Exception exception) {
// well... don't throw an error then.
isElementOrPseudo.setDetails(exception.toString());
report.addCDKError(isElementOrPseudo);
}
}
return report;
}
// the Bond tests
private ValidationReport validateStereoChemistry(Bond bond) {
ValidationReport report = new ValidationReport();
ValidationTest bondStereo = new ValidationTest(bond,
"Defining stereochemistry on bonds is not safe.",
"Use atom based stereochemistry."
);
if (bond.getStereo() != CDKConstants.STEREO_BOND_NONE) {
report.addWarning(bondStereo);
} else {
report.addOK(bondStereo);
}
return report;
}
private ValidationReport validateMaxBondOrder(Bond bond) {
ValidationReport report = new ValidationReport();
ValidationTest maxBO = new ValidationTest(bond,
"Bond order exceeds the maximum for one of its atoms."
);
try {
AtomTypeFactory structgenATF = AtomTypeFactory.getInstance(
"org/openscience/cdk/config/data/structgen_atomtypes.xml",
bond.getBuilder()
);
for (int i=0; i<bond.getAtomCount(); i++) {
org.openscience.cdk.interfaces.IAtom atom = bond.getAtom(i);
if (atom instanceof PseudoAtom) {
// ok, all is fine; we don't know the properties of pseudo atoms
break;
}
org.openscience.cdk.interfaces.IAtomType[] atomTypes = structgenATF.getAtomTypes(atom.getSymbol());
org.openscience.cdk.interfaces.IAtomType failedOn = null;
boolean foundMatchingAtomType = false;
for (int j=0; j<atomTypes.length; j++) {
if (bond.getOrder() <= atomTypes[j].getMaxBondOrder()) {
foundMatchingAtomType = true;
} else {
failedOn = atomTypes[j];
}
}
if (foundMatchingAtomType) {
report.addOK(maxBO);
} else {
if (failedOn != null) {
maxBO.setDetails(
"Bond order exceeds the one allowed for atom " +
atom.getSymbol() + " for which the maximum bond order is " +
failedOn.getMaxBondOrder()
);
}
report.addError(maxBO);
}
}
} catch (Exception exception) {
logger.error("Error while performing atom bos validation");
logger.debug(exception);
maxBO.setDetails("Error while performing atom bos validation: " +
exception.toString());
report.addCDKError(maxBO);
}
return report;
}
// the Isotope tests
public ValidationReport validateIsotopeExistence(IIsotope isotope) {
ValidationReport report = new ValidationReport();
ValidationTest isotopeExists = new ValidationTest(isotope,
"Isotope with this mass number is not known for this element."
);
try {
IsotopeFactory isotopeFac = IsotopeFactory.getInstance(isotope.getBuilder());
IIsotope[] isotopes = isotopeFac.getIsotopes(isotope.getSymbol());
if (isotope.getMassNumber() != 0) {
boolean foundKnownIsotope = false;
for (int i=0; i<isotopes.length; i++) {
if (isotopes[i].getMassNumber() == isotope.getMassNumber()) {
foundKnownIsotope = true;
}
}
if (!foundKnownIsotope) {
report.addError(isotopeExists);
} else {
report.addOK(isotopeExists);
}
} else {
// isotopic number is not set
report.addOK(isotopeExists);
}
} catch (Exception exception) {
// too bad...
}
return report;
}
// the Molecule tests
private ValidationReport validateBondOrderSum(org.openscience.cdk.interfaces.IAtom atom, Molecule molecule) {
ValidationReport report = new ValidationReport();
ValidationTest checkBondSum = new ValidationTest(atom,
"The atom's total bond order is too high."
);
try {
AtomTypeFactory structgenATF = AtomTypeFactory.getInstance(
"org/openscience/cdk/config/data/valency_atomtypes.xml",
atom.getBuilder()
);
int bos = (int)molecule.getBondOrderSum(atom);
org.openscience.cdk.interfaces.IAtomType[] atomTypes = structgenATF.getAtomTypes(atom.getSymbol());
if (atomTypes.length == 0) {
checkBondSum.setDetails(
"Cannot validate bond order sum for atom not in valency atom type list: " +
atom.getSymbol()
);
report.addWarning(checkBondSum);
} else {
org.openscience.cdk.interfaces.IAtomType failedOn = null;
boolean foundMatchingAtomType = false;
for (int j=0; j<atomTypes.length; j++) {
org.openscience.cdk.interfaces.IAtomType type = atomTypes[j];
if (atom.getFormalCharge() == type.getFormalCharge()) {
foundMatchingAtomType = true;
if (bos == type.getBondOrderSum()) {
// skip this atom type
} else {
failedOn = atomTypes[j];
}
}
}
if (foundMatchingAtomType) {
report.addOK(checkBondSum);
} else {
if (failedOn != null) {
checkBondSum.setDetails(
"Bond order exceeds the one allowed for atom " +
atom.getSymbol() + " for which the total bond order is " +
failedOn.getBondOrderSum()
);
}
report.addError(checkBondSum);
}
}
} catch (Exception exception) {
logger.error("Error while performing atom bos validation: ", exception.getMessage());
logger.debug(exception);
}
return report;
}
private ValidationReport validateAtomCountConservation(Reaction reaction,
AtomContainer reactants,
AtomContainer products) {
ValidationReport report = new ValidationReport();
ValidationTest atomCount = new ValidationTest(reaction,
"Atom count mismatch for reaction: the product side has a different atom count than the reactant side."
);
if (reactants.getAtomCount() != products.getAtomCount()) {
report.addError(atomCount);
} else {
report.addOK(atomCount);
}
return report;
}
private ValidationReport validateChargeConservation(Reaction reaction,
AtomContainer reactants,
AtomContainer products) {
ValidationReport report = new ValidationReport();
ValidationTest chargeConservation = new ValidationTest(reaction,
"Total formal charge is not preserved during the reaction"
);
java.util.Iterator atoms1 = reactants.atoms();
int totalCharge1 = 0;
while (atoms1.hasNext()) {
totalCharge1 =+ ((IAtom)atoms1.next()).getFormalCharge();
}
java.util.Iterator atoms2 = products.atoms();
int totalCharge2 = 0;
while (atoms2.hasNext()) {
totalCharge2 =+ ((IAtom)atoms2.next()).getFormalCharge();
}
if (totalCharge1 != totalCharge2) {
report.addError(chargeConservation);
} else {
report.addOK(chargeConservation);
}
return report;
}
}