Skip to content

Commit

Permalink
MDL reading and writing atom value line, including test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Rajarshi Guha <rajarshi.guha@gmail.com>
  • Loading branch information
M L Rijnbeek authored and rajarshi committed May 21, 2010
1 parent 088afce commit c88bd3c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/org/openscience/cdk/io/MDLV2000Reader.java
Expand Up @@ -813,6 +813,12 @@ private IMolecule readMolecule(IMolecule molecule) throws CDKException {
}
}
}
if (line.startsWith("V ")) {
Integer atomNumber = new Integer(line.substring(3,6).trim());
IAtom atomWithComment = molecule.getAtom(atomNumber - 1);
atomWithComment.setProperty(CDKConstants.COMMENT, line.substring(7));
}

if (!lineRead) {
logger.warn("Skipping line in property block: ", line);
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/org/openscience/cdk/io/MDLWriter.java
Expand Up @@ -371,6 +371,20 @@ else if(atom.getValency()==0)
writer.newLine();
}
}

// Write Atom Value
for (int i = 0; i < container.getAtomCount(); i++) {
IAtom atom = container.getAtom(i);
if(atom.getProperty(CDKConstants.COMMENT)!=null
&& atom.getProperty(CDKConstants.COMMENT) instanceof String
&& !((String)atom.getProperty(CDKConstants.COMMENT)).trim().equals("") ) {
writer.write("V ");
writer.write(formatMDLInt(i+1,3));
writer.write(" ");
writer.write((String)atom.getProperty(CDKConstants.COMMENT));
writer.newLine();
}
}

// write formal atomic charges
for (int i = 0; i < container.getAtomCount(); i++) {
Expand Down
10 changes: 10 additions & 0 deletions src/test/data/mdl/atomValueLines.mol
@@ -0,0 +1,10 @@

CDK 0507101526

2 1 0 0 0 0 0 0 0 0999 V2000
0.0000 0.0000 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2 1 2 0 0 0 0
V 1 Oxygen comment
V 2 Carbon comment
M END
11 changes: 11 additions & 0 deletions src/test/org/openscience/cdk/io/MDLV2000ReaderTest.java
Expand Up @@ -796,4 +796,15 @@ private void testShortLinesForMode (IChemObjectReader.Mode mode) throws Exceptio
Assert.assertNotNull(molOne.getAtom(0).getPoint3d());
}

@Test public void testAtomValueLines() throws Exception {
String filename = "data/mdl/atomValueLines.mol";
InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename);
MDLV2000Reader reader = new MDLV2000Reader(ins);
Molecule testMolecule = new Molecule();
Molecule result = reader.read(testMolecule);
IAtom oxygen = result.getAtom(0);
Assert.assertTrue(oxygen.getSymbol().equals("O"));
Assert.assertEquals(oxygen.getProperty(CDKConstants.COMMENT), "Oxygen comment");
}

}
25 changes: 25 additions & 0 deletions src/test/org/openscience/cdk/io/MDLWriterTest.java
Expand Up @@ -279,4 +279,29 @@ public class MDLWriterTest extends ChemObjectIOTest {
}


/**
* Test writing of comments made on individual atoms into an Atom Value lines.
*/
@Test public void testAtomValueLine() throws Exception {
IAtom carbon = builder.newInstance(IAtom.class, "C");
carbon.setProperty(CDKConstants.COMMENT, "Carbon comment");
IAtom oxygen = builder.newInstance(IAtom.class, "O");
oxygen.setProperty(CDKConstants.COMMENT, "Oxygen comment");
IBond bond = builder.newInstance(IBond.class,carbon, oxygen, CDKConstants.BONDORDER_DOUBLE);

Molecule molecule = new Molecule();
molecule.addAtom(oxygen);
molecule.addAtom(carbon);
molecule.addBond(bond);

StringWriter writer = new StringWriter();
MDLWriter mdlWriter = new MDLWriter(writer);
mdlWriter.write(molecule);
System.out.println(writer.toString());

Assert.assertTrue(writer.toString().indexOf("V 1 Oxygen comment") != -1);
Assert.assertTrue(writer.toString().indexOf("V 2 Carbon comment") != -1);

}

}

0 comments on commit c88bd3c

Please sign in to comment.