Skip to content

Commit 42da2c1

Browse files
committed
Sping multicity round tripping in molfiles, SAChem added support for reading from V2000, makes sense to also support V3000 and output in both since there is currently information loss.
1 parent 7ec685d commit 42da2c1

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

base/core/src/main/java/org/openscience/cdk/CDKConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ public class CDKConstants {
424424
*/
425425
public static final String CTAB_SGROUPS = "cdk:CtabSgroups";
426426

427+
/**
428+
* Enumeration of all valid radical values.
429+
*/
430+
public static final String SPIN_MULTIPLICITY = "cdk:SpinMultiplicity";
427431

428432
/**
429433
* Property for reaction objects where the conditions of reactions can be placed.

storage/ctab/src/main/java/org/openscience/cdk/io/MDLV2000Reader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,8 @@ void readPropertiesFast(final BufferedReader input, final IAtomContainer contain
10731073
index = readMolfileInt(line, st) - 1;
10741074
int value = readMolfileInt(line, st + 4);
10751075
SPIN_MULTIPLICITY multiplicity = SPIN_MULTIPLICITY.ofValue(value);
1076+
1077+
container.getAtom(offset + index).setProperty(CDKConstants.SPIN_MULTIPLICITY, multiplicity);
10761078

10771079
for (int e = 0; e < multiplicity.getSingleElectrons(); e++)
10781080
container.addSingleElectron(offset + index);

storage/ctab/src/main/java/org/openscience/cdk/io/MDLV2000Writer.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,16 +694,22 @@ else if (e.equals(new Expr(Expr.Type.ALIPHATIC_ORDER, 2).or(new Expr(Expr.Type.I
694694
if (container.getSingleElectronCount() > 0) {
695695
Map<Integer, SPIN_MULTIPLICITY> atomIndexSpinMap = new LinkedHashMap<Integer, SPIN_MULTIPLICITY>();
696696
for (int i = 0; i < container.getAtomCount(); i++) {
697-
int eCount = container.getConnectedSingleElectronsCount(container.getAtom(i));
697+
IAtom atom = container.getAtom(i);
698+
int eCount = container.getConnectedSingleElectronsCount(atom);
698699
switch (eCount) {
699700
case 0:
700701
continue;
701702
case 1:
702703
atomIndexSpinMap.put(i, SPIN_MULTIPLICITY.Monovalent);
703704
break;
704705
case 2:
705-
// information loss, divalent but singlet or triplet?
706-
atomIndexSpinMap.put(i, SPIN_MULTIPLICITY.DivalentSinglet);
706+
SPIN_MULTIPLICITY multiplicity = atom.getProperty(CDKConstants.SPIN_MULTIPLICITY);
707+
if (multiplicity != null)
708+
atomIndexSpinMap.put(i, multiplicity);
709+
else {
710+
// information loss, divalent but singlet or triplet?
711+
atomIndexSpinMap.put(i, SPIN_MULTIPLICITY.DivalentSinglet);
712+
}
707713
break;
708714
default:
709715
logger.debug("Invalid number of radicals found: " + eCount);

storage/ctab/src/main/java/org/openscience/cdk/io/MDLV3000Reader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ public void readAtomBlock(IAtomContainer readData) throws CDKException {
343343
}
344344
break;
345345
case "RAD":
346-
int numElectons = MDLV2000Writer.SPIN_MULTIPLICITY.ofValue(Integer.parseInt(value))
347-
.getSingleElectrons();
346+
MDLV2000Writer.SPIN_MULTIPLICITY spinMultiplicity = MDLV2000Writer.SPIN_MULTIPLICITY.ofValue(Integer.parseInt(value));
347+
int numElectons = spinMultiplicity.getSingleElectrons();
348+
atom.setProperty(CDKConstants.SPIN_MULTIPLICITY, spinMultiplicity);
348349
while (numElectons-- > 0) {
349350
readData.addSingleElectron(readData.getBuilder().newInstance(ISingleElectron.class, atom));
350351
}

storage/ctab/src/main/java/org/openscience/cdk/io/MDLV3000Writer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,12 @@ private void writeAtomBlock(IAtomContainer mol, IAtom[] atoms, Map<IChemObject,
261261
case 1: // 2
262262
rad = MDLV2000Writer.SPIN_MULTIPLICITY.Monovalent.getValue();
263263
break;
264-
case 2: // 1 or 3? Information loss as to which
265-
rad = MDLV2000Writer.SPIN_MULTIPLICITY.DivalentSinglet.getValue();
264+
case 2:
265+
MDLV2000Writer.SPIN_MULTIPLICITY spinMultiplicity = atom.getProperty(CDKConstants.SPIN_MULTIPLICITY);
266+
if (spinMultiplicity != null)
267+
rad = spinMultiplicity.getValue();
268+
else // 1 or 3? Information loss as to which
269+
rad = MDLV2000Writer.SPIN_MULTIPLICITY.DivalentSinglet.getValue();
266270
break;
267271
}
268272

0 commit comments

Comments
 (0)