Skip to content

Commit

Permalink
Correct MDL radical to/from electron count semantics.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Aug 10, 2016
1 parent 2f50ba9 commit 700f2c2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 53 deletions.
Expand Up @@ -41,8 +41,6 @@
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.interfaces.IPseudoAtom;
import org.openscience.cdk.interfaces.ISingleElectron;
import org.openscience.cdk.interfaces.IStereoElement;
import org.openscience.cdk.interfaces.ITetrahedralChirality;
import org.openscience.cdk.interfaces.ITetrahedralChirality.Stereo;
import org.openscience.cdk.io.formats.IResourceFormat;
import org.openscience.cdk.io.formats.MDLV2000Format;
Expand Down Expand Up @@ -1886,24 +1884,11 @@ private void readPropertiesSlow(BufferedReader input, IAtomContainer container,
StringTokenizer st = new StringTokenizer(line.substring(9));
for (int i = 1; i <= infoCount; i++) {
int atomNumber = Integer.parseInt(st.nextToken().trim());
int spinMultiplicity = Integer.parseInt(st.nextToken().trim());
MDLV2000Writer.SPIN_MULTIPLICITY spin = MDLV2000Writer.SPIN_MULTIPLICITY.NONE;
if (spinMultiplicity > 0) {
int rad = Integer.parseInt(st.nextToken().trim());
MDLV2000Writer.SPIN_MULTIPLICITY spin = MDLV2000Writer.SPIN_MULTIPLICITY.None;
if (rad > 0) {
IAtom radical = container.getAtom(atomNumber - 1);
switch (spinMultiplicity) {
case 1:
spin = MDLV2000Writer.SPIN_MULTIPLICITY.DOUBLET;
break;
case 2:
spin = MDLV2000Writer.SPIN_MULTIPLICITY.SINGLET;
break;
case 3:
spin = MDLV2000Writer.SPIN_MULTIPLICITY.TRIPLET;
break;
default:
logger.debug("Invalid spin multiplicity found: " + spinMultiplicity);
break;
}
spin = MDLV2000Writer.SPIN_MULTIPLICITY.ofValue(rad);
for (int j = 0; j < spin.getSingleElectrons(); j++) {
container.addSingleElectron(container.getBuilder().newInstance(ISingleElectron.class,
radical));
Expand Down
66 changes: 33 additions & 33 deletions storage/io/src/main/java/org/openscience/cdk/io/MDLV2000Writer.java
Expand Up @@ -24,35 +24,12 @@
*/
package org.openscience.cdk.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.config.Isotopes;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IBond.Order;
import org.openscience.cdk.interfaces.IChemFile;
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemObject;
Expand All @@ -74,6 +51,28 @@
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
import org.openscience.cdk.tools.manipulator.ChemFileManipulator;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Writes MDL molfiles, which contains a single molecule (see {@cdk.cite DAL92}).
* For writing a MDL molfile you can this code:
Expand Down Expand Up @@ -118,7 +117,10 @@ public class MDLV2000Writer extends DefaultChemObjectWriter {
*/
public enum SPIN_MULTIPLICITY {

NONE(0, 0), SINGLET(2, 1), DOUBLET(1, 2), TRIPLET(3, 2);
None(0, 0),
Monovalent(2, 1),
DivalentSinglet(1, 2),
DivalentTriplet(3, 2);

// the radical SDF value
private final int value;
Expand Down Expand Up @@ -158,13 +160,13 @@ public int getSingleElectrons() {
public static SPIN_MULTIPLICITY ofValue(int value) throws CDKException {
switch (value) {
case 0:
return NONE;
return None;
case 1:
return DOUBLET;
return DivalentSinglet;
case 2:
return SINGLET;
return Monovalent;
case 3:
return TRIPLET;
return DivalentTriplet;
default:
throw new CDKException("unknown spin multiplicity: " + value);
}
Expand Down Expand Up @@ -712,13 +714,11 @@ else if (valence > 0 && valence < 15)
case 0:
continue;
case 1:
atomIndexSpinMap.put(i, SPIN_MULTIPLICITY.SINGLET);
atomIndexSpinMap.put(i, SPIN_MULTIPLICITY.Monovalent);
break;
case 2:
atomIndexSpinMap.put(i, SPIN_MULTIPLICITY.DOUBLET);
break;
case 3:
atomIndexSpinMap.put(i, SPIN_MULTIPLICITY.TRIPLET);
// information loss, divalent but singlet or triplet?
atomIndexSpinMap.put(i, SPIN_MULTIPLICITY.DivalentSinglet);
break;
default:
logger.debug("Invalid number of radicals found: " + eCount);
Expand Down
Expand Up @@ -221,7 +221,16 @@ private void writeAtomBlock(IAtomContainer mol, IAtom[] atoms, Map<IChemObject,
final int chg = nullAsZero(atom.getFormalCharge());
final int mass = nullAsZero(atom.getMassNumber());
final int hcnt = nullAsZero(atom.getImplicitHydrogenCount());
final int rad = mol.getConnectedSingleElectronsCount(atom);
final int elec = mol.getConnectedSingleElectronsCount(atom);
int rad = 0;
switch (elec) {
case 1: // 2
rad = MDLV2000Writer.SPIN_MULTIPLICITY.Monovalent.getValue();
break;
case 2: // 1 or 3? Information loss as to which
rad = MDLV2000Writer.SPIN_MULTIPLICITY.DivalentSinglet.getValue();
break;
}

int expVal = 0;
for (IBond bond : mol.getConnectedBondsList(atom)) {
Expand Down

0 comments on commit 700f2c2

Please sign in to comment.