Skip to content

Commit

Permalink
Prevent loss of isotopes in Molfiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Apr 25, 2018
1 parent 9980412 commit 5d2b717
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IChemSequence;
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.interfaces.IPseudoAtom;
import org.openscience.cdk.interfaces.IStereoElement;
import org.openscience.cdk.interfaces.ITetrahedralChirality;
Expand Down Expand Up @@ -180,6 +181,8 @@ public static SPIN_MULTIPLICITY ofValue(int value) throws CDKException {

private BooleanIOSetting forceWriteAs2DCoords;

private BooleanIOSetting ignoreMajorIsotopes;

// The next two options are MDL Query format options, not really
// belonging to the MDLV2000 format, and will be removed when
// a MDLV2000QueryWriter is written.
Expand Down Expand Up @@ -495,7 +498,6 @@ public void writeMolecule(IAtomContainer container) throws Exception {
}
}
}

line += String.format(" 0 0 %d 0 0", parity);
}

Expand Down Expand Up @@ -761,15 +763,15 @@ else if (valence > 0 && valence < 15)
IAtom atom = container.getAtom(i);
if (!(atom instanceof IPseudoAtom)) {
Integer atomicMass = atom.getMassNumber();
if (ignoreMajorIsotopes.isSet() &&
isMajorIsotope(atom))
atomicMass = null;
if (atomicMass != null) {
int majorMass = Isotopes.getInstance().getMajorIsotope(atom.getSymbol()).getMassNumber();
if (atomicMass != majorMass) {
writer.write("M ISO 1 ");
writer.write(formatMDLInt(i + 1, 3));
writer.write(" ");
writer.write(formatMDLInt(atomicMass, 3));
writer.write('\n');
}
writer.write("M ISO 1 ");
writer.write(formatMDLInt(i + 1, 3));
writer.write(" ");
writer.write(formatMDLInt(atomicMass, 3));
writer.write('\n');
}
}
}
Expand Down Expand Up @@ -828,6 +830,13 @@ else if (valence > 0 && valence < 15)
writer.flush();
}

private boolean isMajorIsotope(IAtom atom) throws IOException {
if (atom.getMassNumber() == null)
return false;
IIsotope major = Isotopes.getInstance().getMajorIsotope(atom.getSymbol());
return major != null && major.getMassNumber().equals(atom.getMassNumber());
}

private void writeSgroups(IAtomContainer container, BufferedWriter writer, Map<IAtom,Integer> atomidxs) throws IOException {
List<Sgroup> sgroups = container.getProperty(CDKConstants.CTAB_SGROUPS);
if (sgroups == null)
Expand Down Expand Up @@ -1101,6 +1110,8 @@ protected static String formatMDLString(String s, int le) {
private void initIOSettings() {
forceWriteAs2DCoords = addSetting(new BooleanIOSetting("ForceWriteAs2DCoordinates", IOSetting.Importance.LOW,
"Should coordinates always be written as 2D?", "false"));
ignoreMajorIsotopes = addSetting(new BooleanIOSetting("IgnoreMajorIsotopes", IOSetting.Importance.LOW,
"Do not write atomic mass of major isotopes (e.g. [12]C)", "false"));
writeAromaticBondTypes = addSetting(new BooleanIOSetting("WriteAromaticBondTypes", IOSetting.Importance.LOW,
"Should aromatic bonds be written as bond type 4?", "false"));
writeQueryFormatValencies = addSetting(new BooleanIOSetting("WriteQueryFormatValencies",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.openscience.cdk.CDKConstants.ISAROMATIC;

Expand Down Expand Up @@ -881,4 +882,35 @@ public void writeMoreThan8Radicals() throws Exception {
assertThat(sw.toString(),
containsString("M RAD 8 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2"));
}

@Test
public void writeCarbon12() throws Exception {
IAtomContainer mol = builder.newAtomContainer();
IAtom atom = builder.newAtom();
atom.setSymbol("C");
atom.setMassNumber(12);
mol.addAtom(atom);
StringWriter sw = new StringWriter();
try (MDLV2000Writer mdlw = new MDLV2000Writer(sw)) {
mdlw.write(mol);
}
assertThat(sw.toString(),
containsString("M ISO 1 1 12"));
}

@Test
public void ignoreCarbon12() throws Exception {
IAtomContainer mol = builder.newAtomContainer();
IAtom atom = builder.newAtom();
atom.setSymbol("C");
atom.setMassNumber(12);
mol.addAtom(atom);
StringWriter sw = new StringWriter();
try (MDLV2000Writer mdlw = new MDLV2000Writer(sw)) {
mdlw.getSetting("IgnoreMajorIsotopes").setSetting("true");
mdlw.write(mol);
}
assertThat(sw.toString(),
not(containsString("M ISO 1 1 12")));
}
}

0 comments on commit 5d2b717

Please sign in to comment.