Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added IO option to allow saving aromatic SMILES
Signed-off-by: Rajarshi Guha <rajarshi.guha@gmail.com>
  • Loading branch information
egonw authored and rajarshi committed Jan 14, 2010
1 parent 7d6a9b6 commit 84a44e0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/org/openscience/cdk/io/SMILESWriter.java
Expand Up @@ -44,6 +44,8 @@
import org.openscience.cdk.interfaces.IMoleculeSet;
import org.openscience.cdk.io.formats.IResourceFormat;
import org.openscience.cdk.io.formats.SMILESFormat;
import org.openscience.cdk.io.setting.BooleanIOSetting;
import org.openscience.cdk.io.setting.IOSetting;
import org.openscience.cdk.smiles.SmilesGenerator;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
Expand All @@ -63,6 +65,8 @@ public class SMILESWriter extends DefaultChemObjectWriter {
LoggingToolFactory.createLoggingTool(SMILESWriter.class);
private BufferedWriter writer;

private BooleanIOSetting useAromaticityFlag;

/**
* Contructs a new SMILESWriter that can write a list of SMILES to a Writer
*
Expand All @@ -77,6 +81,7 @@ public SMILESWriter(Writer out) {
}
} catch (Exception exc) {
}
initIOSettings();
}

public SMILESWriter(OutputStream output) {
Expand Down Expand Up @@ -170,6 +175,7 @@ public void writeMoleculeSet(IMoleculeSet som)
*/
public void writeMolecule(IMolecule molecule) {
SmilesGenerator sg = new SmilesGenerator();
sg.setUseAromaticityFlag(useAromaticityFlag.isSet());
String smiles = "";
try {
smiles = sg.createSMILES(molecule);
Expand All @@ -183,4 +189,23 @@ public void writeMolecule(IMolecule molecule) {
logger.debug(exc);
}
}

private void initIOSettings() {
useAromaticityFlag = new BooleanIOSetting(
"UseAromaticity",
IOSetting.LOW,
"Should aromaticity information be stored in the SMILES?",
"false"
);
}

public void customizeJob() {
fireIOSettingQuestion(useAromaticityFlag);
}

public IOSetting[] getIOSettings() {
IOSetting[] settings = new IOSetting[1];
settings[0] = useAromaticityFlag;
return settings;
}
}
17 changes: 17 additions & 0 deletions src/test/org/openscience/cdk/io/SMILESWriterTest.java
Expand Up @@ -23,13 +23,15 @@
package org.openscience.cdk.io;

import java.io.StringWriter;
import java.util.Properties;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openscience.cdk.Molecule;
import org.openscience.cdk.MoleculeSet;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.io.listener.PropertiesListener;
import org.openscience.cdk.templates.MoleculeFactory;

/**
Expand Down Expand Up @@ -60,4 +62,19 @@ public class SMILESWriterTest extends ChemObjectIOTest {
Assert.assertTrue(stringWriter.toString().contains("C=C"));
}

@Test public void testWriteAromatic() throws Exception {
StringWriter stringWriter = new StringWriter();
IMolecule benzene = MoleculeFactory.makeBenzene();
SMILESWriter smilesWriter = new SMILESWriter(stringWriter);

Properties prop = new Properties();
prop.setProperty("UseAromaticity","true");
PropertiesListener listener = new PropertiesListener(prop);
smilesWriter.addChemObjectIOListener(listener);
smilesWriter.customizeJob();
smilesWriter.write(benzene);
smilesWriter.close();
Assert.assertFalse(stringWriter.toString().contains("C=C"));
Assert.assertTrue(stringWriter.toString().contains("ccc"));
}
}

0 comments on commit 84a44e0

Please sign in to comment.