valence check problem in JChem 5.9.0

User 870ab5b546

15-05-2012 23:58:16

The code:


    public static void checkValence(Molecule mol) throws ValenceException {
final String SELF = "ChemUtils.checkValence: ";
debugPrintMRV(SELF + "looking for valence errors in:\n", mol);
mol.valenceCheck();
final String VALENCE_ERROR = "At least one of the atoms in your response "
+ "has an invalid valence. If you don't see an atom underlined "
+ "in red, look for an atom that violates the octet rule, or "
+ "try expanding your shortcut groups. ";
for (int atomNum = 0; atomNum < mol.getAtomCount(); atomNum++) {
final MolAtom atom = mol.getAtom(atomNum);
if (isFreeHAtom(atom)) continue; // workaround as of JChem 5.4.1
if (atom.hasValenceError()) {
debugPrint(SELF + "Valence error at ",
atom, atomNum + 1, " in ", mol);
throw new ValenceException(VALENCE_ERROR);
} // if valence error
// workaround for valence check bug for atoms with radical flag
// such as 9-electron radicals
int unsharedElecCt = mol.getLonePairCount(atomNum);
final int radState = atom.getRadical();
switch (radState) {
case MolAtom.RAD1:
unsharedElecCt += 1; break;
case MolAtom.RAD2:
case MolAtom.RAD2_SINGLET:
case MolAtom.RAD2_TRIPLET:
unsharedElecCt += 2; break;
case MolAtom.RAD3:
case MolAtom.RAD3_DOUBLET:
case MolAtom.RAD3_QUARTET:
unsharedElecCt += 3; break;
default: continue;
} // radical state
debugPrint(SELF + "Radical state of atom ", atom, atomNum + 1,
" is ", radState);
final int totalElecCt = unsharedElecCt
+ atom.twicesumbonds(INCLUDE_EXPLICIT_H, USE_BOND_ORDERS)
+ atom.getImplicitHcount() * 2;
final int maxOuter = getMaxOuterElectrons(atom);
debugPrint(SELF + "for ", atom, atomNum + 1,
", totalElecCt = ", totalElecCt,
", maxOuter = ", maxOuter);
if (totalElecCt > maxOuter) {
debugPrint(SELF + "valence error at ", atom, atomNum + 1,
" in ", mol, " caught by workaround.");
throw new ValenceException(VALENCE_ERROR);
} // if too many electrons
} // for each atom
final String PENTACOORD_N = "[$(*=,#[NX3]=,#*),$(*:n(=,#*):*),$(*=,#N#*)]";
try {
final Molecule pentacoordN = MolImporter.importMol(PENTACOORD_N);
if (MolFunctions.containsSubstruct(mol, pentacoordN)) {
debugPrint(SELF + "pentavalent N error in ", mol);
throw new ValenceException(VALENCE_ERROR);
} // if molecule contains pentavalent N
} catch (MolFormatException e) { // unlikely
; // do nothing
} catch (MolFileException e) { // unlikely
; // do nothing
} // try
} // checkValence(Molecule)

The log says:


ChemUtils.checkValence: looking for valence errors in:
<?xml version="1.0"?><cml version="ChemAxon file format v5.9.0, generated by v5.9.0">
<MDocument><MChemicalStruct><molecule molID="m1"><atomArray><atom id="a1" elementType="C" x2="1.3064017093670146" y2="-5.921961412336609"></atom><atom id="a2" elementType="C" x2="1.3064017093670146" y2="-7.461995293454690"></atom><atom id="a3" elementType="C" x2="2.640071050415273" y2="-8.232012234013730"></atom><atom id="a4" elementType="C" x2="3.973740391463531" y2="-7.461995293454690"></atom><atom id="a5" elementType="O" x2="2.640071050415273" y2="-5.151944471777568"><scalar id="a5:prop1" title="paired electrons" convention="marvin:atomprop" dataType="xsd:integer" value="4"></scalar></atom><atom id="a6" elementType="C" x2="3.973740391463531" y2="-5.921961412336609"></atom><atom id="a7" elementType="O" formalCharge="2" radical="divalent1" x2="5.273115210265411" y2="-5.103836364652894"><scalar id="a7:prop1" title="paired electrons" convention="marvin:atomprop" dataType="xsd:integer" value="2"></scalar></atom><atom id="a8" elementType="C" x2="6.813115210265412" y2="-5.103836364652894"></atom><atom id="a9" elementType="H" formalCharge="-1" x2="5.6716965397232935" y2="-6.591362137138059"><scalar id="a9:prop1" title="paired electrons" convention="marvin:atomprop" dataType="xsd:integer" value="2"></scalar></atom></atomArray><bondArray><bond atomRefs2="a5 a1" order="1"></bond><bond atomRefs2="a1 a2" order="1"></bond><bond atomRefs2="a2 a3" order="1"></bond><bond atomRefs2="a3 a4" order="1"></bond><bond atomRefs2="a4 a6" order="1"></bond><bond atomRefs2="a5 a6" order="1"></bond><bond atomRefs2="a6 a7" order="1"></bond><bond atomRefs2="a7 a8" order="1"></bond></bondArray></molecule></MChemicalStruct></MDocument>
</cml>

ChemUtils.checkValence: Valence error at O[+2]7 in [H-].C[O++]C1CCCCO1

There should not be a valence error at this O atom.  JChem 5.6 did not generate a valence error from this structure.  When I copy the MRV and paste it into Marvin, Marvin does not show a valence error.  So why is JChem 5.9 saying there is a valence error?

User 870ab5b546

16-05-2012 16:20:54

The bug appears to be in either MoleculeGraph.valenceCheck() or MolAtom.hasValenceError().  Code with lots of debugging statements added:


    public static void checkValence(Molecule mol) throws ValenceException {
final String SELF = "ChemUtils.checkValence: ";
debugPrintMRV(SELF + "looking for valence errors in:\n", mol);
mol.valenceCheck();
debugPrintMRV(SELF + "after valence check:\n", mol);
final String VALENCE_ERROR = "At least one of the atoms in your response "
+ "has an invalid valence. If you don't see an atom underlined "
+ "in red, look for an atom that violates the octet rule, or "
+ "try expanding your shortcut groups. ";
for (int atomNum = 0; atomNum < mol.getAtomCount(); atomNum++) {
final MolAtom atom = mol.getAtom(atomNum);
if (isFreeHAtom(atom)) continue; // workaround as of JChem 5.4.1
final int radState = atom.getRadical();
if (atom.hasValenceError()) {
debugPrintMRV(SELF + "Valence error at ",
atom, atomNum + 1, " with charge ",
atom.getCharge(), " and radical state ",
radState, " in:\n", mol);
throw new ValenceException(VALENCE_ERROR);
} // if valence error
...
} // for each atom
...
}

private static boolean isFreeHAtom(MolAtom atom) {
return atom.getAtno() == 1
&& atom.getBondCount() == 0
&& atom.getCharge() == 0
&& atom.getRadical() == MolAtom.RAD1;
} // isFreeHAtom(MolAtom)

The log:


ChemUtils.checkValence: looking for valence errors in:
<?xml version="1.0"?><cml version="ChemAxon file format v5.9.0, generated by v5.9.0">
<MDocument><MChemicalStruct><molecule molID="m1"><atomArray><atom id="a1" elementType="C" x2="-5.470877347740520" y2="-3.8208254498036966"></atom><atom id="a2" elementType="O" formalCharge="2" radical="divalent1" x2="-4.137198225912484" y2="-3.0508254498036966"><scalar id="a2:prop1" title="paired electrons" convention="marvin:atomprop" dataType="xsd:integer" value="2"></scalar></atom><atom id="a3" elementType="C" x2="-2.8035191040844483" y2="-3.8208254498036966"></atom><atom id="a4" elementType="H" formalCharge="-1" x2="-4.137198225912484" y2="-1.5108254498036964"><scalar id="a4:prop1" title="paired electrons" convention="marvin:atomprop" dataType="xsd:integer" value="2"></scalar></atom></atomArray><bondArray><bond atomRefs2="a1 a2" order="1"></bond><bond atomRefs2="a2 a3" order="1"></bond></bondArray></molecule></MChemicalStruct></MDocument>
</cml>

ChemUtils.checkValence: after valence check:
<?xml version="1.0"?><cml version="ChemAxon file format v5.9.0, generated by v5.9.0">
<MDocument><MChemicalStruct><molecule molID="m1"><atomArray><atom id="a1" elementType="C" x2="-5.470877347740520" y2="-3.8208254498036966"></atom><atom id="a2" elementType="O" formalCharge="2" radical="divalent1" x2="-4.137198225912484" y2="-3.0508254498036966"><scalar id="a2:prop1" title="paired electrons" convention="marvin:atomprop" dataType="xsd:integer" value="2"></scalar></atom><atom id="a3" elementType="C" x2="-2.8035191040844483" y2="-3.8208254498036966"></atom><atom id="a4" elementType="H" formalCharge="-1" x2="-4.137198225912484" y2="-1.5108254498036964"><scalar id="a4:prop1" title="paired electrons" convention="marvin:atomprop" dataType="xsd:integer" value="2"></scalar></atom></atomArray><bondArray><bond atomRefs2="a1 a2" order="1"></bond><bond atomRefs2="a2 a3" order="1"></bond></bondArray></molecule></MChemicalStruct></MDocument>
</cml>

ChemUtils.checkValence: Valence error at O[+2]2 with charge 2 and radical state 6 in:
<?xml version="1.0"?><cml version="ChemAxon file format v5.9.0, generated by v5.9.0">
<MDocument><MChemicalStruct><molecule molID="m1"><atomArray><atom id="a1" elementType="C" x2="-5.470877347740520" y2="-3.8208254498036966"></atom><atom id="a2" elementType="O" formalCharge="2" radical="divalent1" x2="-4.137198225912484" y2="-3.0508254498036966"><scalar id="a2:prop1" title="paired electrons" convention="marvin:atomprop" dataType="xsd:integer" value="2"></scalar></atom><atom id="a3" elementType="C" x2="-2.8035191040844483" y2="-3.8208254498036966"></atom><atom id="a4" elementType="H" formalCharge="-1" x2="-4.137198225912484" y2="-1.5108254498036964"><scalar id="a4:prop1" title="paired electrons" convention="marvin:atomprop" dataType="xsd:integer" value="2"></scalar></atom></atomArray><bondArray><bond atomRefs2="a1 a2" order="1"></bond><bond atomRefs2="a2 a3" order="1"></bond></bondArray></molecule></MChemicalStruct></MDocument>
</cml>

The O atom in question is doubly charged and has a divalent singlet radical state.  It should not have a valence error.  JChem 5.6 did not find a valence error in this atom.


This code worked fine in JChem 5.4 and 5.6.


This bug is extremely serious for what we are doing.  Please fix as soon as possible.


I note that even in older versions of JChem, converting the MRV above to SMILES resulted in loss of the divalent singlet status of the O atom.  You may want to fix this problem as well.  

User 870ab5b546

16-05-2012 20:58:52

I have created a simple page to demonstrate the error.  The Java part of the page is:


<%
final String substrate = request.getParameter("substrate");
Molecule mol = null;
int numAtoms = 0;
if (substrate != null) {
mol = MolImporter.importMol(substrate);
numAtoms = mol.getAtomCount();
Utils.alwaysPrintMRV("valenceCheckTest.jsp: mol:\n", mol);
mol.valenceCheck();
}
%>

The log shows:


INFO: valenceCheckTest.jsp: mol:
<?xml version="1.0"?><cml version="ChemAxon file format v5.9.0, generated by v5.9.0">
<MDocument><MChemicalStruct><molecule molID="m1"><atomArray atomID="a1 a2 a3" elementType="C O C" formalCharge="0 2 0" radical="0 divalent1 0" x2="2.309999999999999 3.643679121828035 4.97735824365607" y2="-1.3336791218280357 -2.1036791218280357 -1.3336791218280353"></atomArray><bondArray><bond atomRefs2="a1 a2" order="1"></bond><bond atomRefs2="a2 a3" order="1"></bond></bondArray></molecule></MChemicalStruct></MDocument>
</cml>

ChemAxon e49cf225c6

17-05-2012 07:47:11

Thank you for reporting these bugs. We will fix them as soon as possible, hopefully in the next release.

User 870ab5b546

09-07-2012 18:41:02










dzatonyi wrote:

Thank you for reporting these bugs. We will fix them as soon as possible, hopefully in the next release.



Have they been fixed in JChem 5.10?

ChemAxon e49cf225c6

10-07-2012 08:35:41










bobgr wrote:










dzatonyi wrote:

Thank you for reporting these bugs. We will fix them as soon as possible, hopefully in the next release.



Have they been fixed in JChem 5.10?



We are currently working on our new valence calculator, which would fix this issue. But unfortunately we couldn't finish it  for 5.10, so it will be released in 5.11.

User 870ab5b546

10-07-2012 17:02:27

This bug in getImplicitHcount() might be related.

User 870ab5b546

02-01-2013 18:51:25

Have these bugs been fixed?

ChemAxon e49cf225c6

03-01-2013 08:53:20

Yes, they were fixed in 5.11.