Skip to content

Commit

Permalink
Atom-Atom-Mapping is now read and written in MDL files. Note the read…
Browse files Browse the repository at this point in the history
…ing until now was into ID field, which is not in line with description of ID field in ChemObject (Returns the identifier (ID) of this object). Also added tests for MDLWriter/Reader/2000Reader.

Signed-off-by: Rajarshi Guha <rajarshi.guha@gmail.com>
  • Loading branch information
Stefan Kuhn authored and rajarshi committed May 12, 2010
1 parent d6d6ab0 commit 6b787c2
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/org/openscience/cdk/CDKConstants.java
Expand Up @@ -291,6 +291,8 @@ public class CDKConstants {
*/
public static final String REST_H = "cdk:RestH";

public static final String ATOM_ATOM_MAPPING = "cdk:AtomAtomMapping";


/* **************************************
* Some predefined property names for *
Expand Down
2 changes: 1 addition & 1 deletion src/main/org/openscience/cdk/io/MDLReader.java
Expand Up @@ -465,7 +465,7 @@ private IMolecule readMolecule(IMolecule molecule) throws CDKException {
try {
int reactionAtomID = Integer.parseInt(reactionAtomIDString);
if (reactionAtomID != 0) {
atom.setID(reactionAtomIDString);
atom.setProperty(CDKConstants.ATOM_ATOM_MAPPING, reactionAtomIDString);
}
} catch (Exception exception) {
logger.error("Mapping number ", reactionAtomIDString, " is not an integer.");
Expand Down
2 changes: 1 addition & 1 deletion src/main/org/openscience/cdk/io/MDLV2000Reader.java
Expand Up @@ -545,7 +545,7 @@ private IMolecule readMolecule(IMolecule molecule) throws CDKException {
try {
int reactionAtomID = Integer.parseInt(reactionAtomIDString);
if (reactionAtomID != 0) {
atom.setID(reactionAtomIDString);
atom.setProperty(CDKConstants.ATOM_ATOM_MAPPING, reactionAtomID);
}
} catch (Exception exception) {
logger.error("Mapping number ", reactionAtomIDString, " is not an integer.");
Expand Down
10 changes: 9 additions & 1 deletion src/main/org/openscience/cdk/io/MDLWriter.java
Expand Up @@ -308,7 +308,15 @@ else if(atom.getValency()==0)
line += formatMDLInt(15, 3);
else
line += formatMDLInt(atom.getValency(), 3);
line += " 0 0 0 0 0 0";
line += " 0 0 0";

if (container.getAtom(f).getProperty(CDKConstants.ATOM_ATOM_MAPPING) != null) {
int value = ((Integer)container.getAtom(f).getProperty(CDKConstants.ATOM_ATOM_MAPPING)).intValue();
line += formatMDLInt(value, 3);
} else {
line += formatMDLInt(0, 3);
}
line += " 0 0";
writer.write(line);
writer.newLine();
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/data/mdl/a-pinene-with-atom-atom-mapping.mol
@@ -0,0 +1,26 @@
a-pinen.mol
ChemDraw08319810042D

10 11 0 0 0 0 0 0 0 0 1 V2000
-2.0500 -1.1000 0.0000 C 0 0 0 0 0 0 0 0 0 1 0 0
-0.7500 -0.3500 0.0000 C 0 0 0 0 0 0 0 0 0 15 0 0
-0.5500 -1.1000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7500 -1.8500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7500 -0.3500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.8100 -1.4100 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.3625 1.1000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.0500 0.4000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.9375 1.8500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-1.6625 1.8500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
1 3 1 0 0 0 0
3 4 1 0 0 0 0
2 5 1 0 0 0 0
5 6 2 0 0 0 0
4 6 1 0 0 0 0
2 7 1 0 0 0 0
3 7 1 0 0 0 0
5 8 1 0 0 0 0
7 9 1 0 0 0 0
7 10 1 0 0 0 0
M END
15 changes: 15 additions & 0 deletions src/test/org/openscience/cdk/io/MDLReaderTest.java
Expand Up @@ -31,9 +31,11 @@
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.ChemFile;
import org.openscience.cdk.ChemModel;
import org.openscience.cdk.ChemObject;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.Molecule;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
Expand Down Expand Up @@ -205,5 +207,18 @@ public class MDLReaderTest extends SimpleChemObjectReaderTest {
Assert.assertEquals(IBond.Stereo.E_OR_Z,mol.getBond(7).getStereo());
Assert.assertEquals(IBond.Stereo.UP_OR_DOWN,mol.getBond(11).getStereo());
}


@Test public void testReadAtomAtomMapping() throws Exception {
String filename = "data/mdl/a-pinene-with-atom-atom-mapping.mol";
logger.info("Testing: " + filename);
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
MDLV2000Reader reader = new MDLV2000Reader(ins);

IMolecule mol = reader.read(new Molecule());
Assert.assertNotNull(mol);
Assert.assertEquals(1, ((Integer)mol.getAtom(0).getProperty(CDKConstants.ATOM_ATOM_MAPPING)).intValue());
Assert.assertEquals(15, ((Integer)mol.getAtom(1).getProperty(CDKConstants.ATOM_ATOM_MAPPING)).intValue());
Assert.assertNull(mol.getAtom(2).getProperty(CDKConstants.ATOM_ATOM_MAPPING));
}
}
12 changes: 12 additions & 0 deletions src/test/org/openscience/cdk/io/MDLV2000ReaderTest.java
Expand Up @@ -772,4 +772,16 @@ private void testShortLinesForMode (IChemObjectReader.Mode mode) throws Exceptio
Assert.assertEquals(mol.getAtomCount(), 5);
Assert.assertEquals(mol.getBondCount(), 4);
}

@Test public void testReadAtomAtomMapping() throws Exception {
String filename = "data/mdl/a-pinene-with-atom-atom-mapping.mol";
logger.info("Testing: " + filename);
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
MDLV2000Reader reader = new MDLV2000Reader(ins);
IMolecule mol = reader.read(new Molecule());
Assert.assertNotNull(mol);
Assert.assertEquals(1, ((Integer)mol.getAtom(0).getProperty(CDKConstants.ATOM_ATOM_MAPPING)).intValue());
Assert.assertEquals(15, ((Integer)mol.getAtom(1).getProperty(CDKConstants.ATOM_ATOM_MAPPING)).intValue());
Assert.assertNull(mol.getAtom(2).getProperty(CDKConstants.ATOM_ATOM_MAPPING));
}
}
12 changes: 12 additions & 0 deletions src/test/org/openscience/cdk/io/MDLWriterTest.java
Expand Up @@ -120,6 +120,18 @@ public class MDLWriterTest extends ChemObjectIOTest {
Assert.assertTrue(output.indexOf("0 0 0 0 0 15 0 0 0 0 0 0") != -1);
}

@Test public void testWriteAtomAtomMapping() throws Exception {
StringWriter writer = new StringWriter();
Molecule molecule = MoleculeFactory.makeAlphaPinene();
molecule.getAtom(0).setProperty(CDKConstants.ATOM_ATOM_MAPPING,1);
molecule.getAtom(1).setProperty(CDKConstants.ATOM_ATOM_MAPPING,15);
MDLWriter mdlWriter = new MDLWriter(writer);
mdlWriter.write(molecule);
String output = writer.toString();
Assert.assertTrue(output.indexOf("0 0 0 0 0 0 0 0 0 1 0 0") != -1);
Assert.assertTrue(output.indexOf("0 0 0 0 0 0 0 0 0 15 0 0") != -1);
}

/**
* Test for bug #1778479 "MDLWriter writes empty PseudoAtom label string".
* When a molecule contains an IPseudoAtom without specifying the atom label
Expand Down

0 comments on commit 6b787c2

Please sign in to comment.