Skip to content

Commit

Permalink
Merge branch 'master' of ssh://cdk.git.sourceforge.net/gitroot/cdk/cdk
Browse files Browse the repository at this point in the history
  • Loading branch information
egonw committed Jul 16, 2010
2 parents 2896226 + a586d6a commit eb6defd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
32 changes: 27 additions & 5 deletions src/main/org/openscience/cdk/io/MDLWriter.java
Expand Up @@ -86,6 +86,9 @@ public class MDLWriter extends DefaultChemObjectWriter {
LoggingToolFactory.createLoggingTool(MDLWriter.class);

private BooleanIOSetting forceWriteAs2DCoords;

/* Should aromatic bonds be written as bond type 4? If true, this makes the output a query file. */
private BooleanIOSetting writeAromaticBondTypes;

private BufferedWriter writer;

Expand Down Expand Up @@ -324,7 +327,7 @@ else if(atom.getValency()==0)
// write Bond block
Iterator<IBond> bonds = container.bonds().iterator();
while (bonds.hasNext()) {
IBond bond = (IBond) bonds.next();
IBond bond = bonds.next();

if (bond.getAtomCount() != 2) {
logger.warn("Skipping bond with more/less than two atoms: " + bond);
Expand All @@ -339,7 +342,13 @@ else if(atom.getValency()==0)
line = formatMDLInt(container.getAtomNumber(bond.getAtom(0)) + 1,3);
line += formatMDLInt(container.getAtomNumber(bond.getAtom(1)) + 1,3);
}
line += formatMDLInt((int)bond.getOrder().ordinal()+1,3);
int bondType;
if (writeAromaticBondTypes.isSet() && bond.getFlag(CDKConstants.ISAROMATIC))
bondType=4;
else
bondType=(int)bond.getOrder().ordinal()+1;
line += formatMDLInt(bondType,3);

line += " ";
switch(bond.getStereo()){
case UP:
Expand Down Expand Up @@ -513,26 +522,39 @@ protected static String formatMDLString(String s, int le) {
s += " ";
return s;
}


/**
* Initializes IO settings.<br>
* Please note with regards to "writeAromaticBondTypes": bond type values 4 through 8 are for SSS queries only,
* so a 'query file' is created if the container has aromatic bonds and this settings is true.
*/
private void initIOSettings() {
forceWriteAs2DCoords = new BooleanIOSetting(
"ForceWriteAs2DCoordinates",
IOSetting.LOW,
"Should coordinates always be written as 2D?",
"false"
);
writeAromaticBondTypes = new BooleanIOSetting(
"WriteAromaticBondTypes",
IOSetting.LOW,
"Should aromatic bonds be written as bond type 4?",
"false"
);
}

public void customizeJob() {
fireIOSettingQuestion(forceWriteAs2DCoords);
fireIOSettingQuestion(writeAromaticBondTypes);

}

public IOSetting[] getIOSettings() {
IOSetting[] settings = new IOSetting[1];
IOSetting[] settings = new IOSetting[2];
settings[0] = forceWriteAs2DCoords;
settings[1] = writeAromaticBondTypes;
return settings;
}

}


34 changes: 33 additions & 1 deletion src/test/org/openscience/cdk/io/MDLWriterTest.java
Expand Up @@ -24,6 +24,7 @@
package org.openscience.cdk.io;

import java.io.StringWriter;

import java.util.Properties;

import javax.vecmath.Point2d;
Expand All @@ -32,6 +33,7 @@
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import org.openscience.cdk.Atom;
import org.openscience.cdk.AtomContainer;
import org.openscience.cdk.CDKConstants;
Expand All @@ -49,8 +51,11 @@
import org.openscience.cdk.interfaces.IMoleculeSet;
import org.openscience.cdk.interfaces.IPseudoAtom;
import org.openscience.cdk.io.listener.PropertiesListener;
import org.openscience.cdk.nonotify.NoNotificationChemObjectBuilder;
import org.openscience.cdk.smiles.SmilesParser;
import org.openscience.cdk.templates.MoleculeFactory;


/**
* TestCase for the writer MDL mol files using one test file.
*
Expand Down Expand Up @@ -297,11 +302,38 @@ public class MDLWriterTest extends ChemObjectIOTest {
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);

}

/**
* Test option to write aromatic bonds with bond type "4".
* Please note: bond type values 4 through 8 are for SSS queries only.
* @throws Exception
*/
@Test public void testAromaticBondType4() throws Exception {
SmilesParser sp = new SmilesParser(NoNotificationChemObjectBuilder.getInstance());
String smiles = "c1ccccc1";
IMolecule benzene = sp.parseSmiles(smiles);


StringWriter writer = new StringWriter();
MDLWriter mdlWriter = new MDLWriter(writer);
mdlWriter.write(benzene);
Assert.assertTrue(writer.toString().indexOf("2 1 1 0 0 0 0") != -1);


writer = new StringWriter();
mdlWriter = new MDLWriter(writer);
Properties prop = new Properties();
prop.setProperty("WriteAromaticBondTypes","true");
PropertiesListener listener = new PropertiesListener(prop);
mdlWriter.addChemObjectIOListener(listener);
mdlWriter.customizeJob();
mdlWriter.write(benzene);
Assert.assertTrue(writer.toString().indexOf("2 1 4 0 0 0 0") != -1);
}

}

0 comments on commit eb6defd

Please sign in to comment.