/
SMILESWriter.java
213 lines (193 loc) · 6.48 KB
/
SMILESWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* $RCSfile$
* $Author$
* $Date$
* $Revision$
*
* Copyright (C) 1997-2007 The Chemistry Development Kit (CDK) project
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All we ask is that proper credit is given for our work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
package org.openscience.cdk.io;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IMolecule;
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;
/**
* Writes the SMILES strings to a plain text file.
*
* @cdk.module smiles
* @cdk.githash
*
* @cdk.keyword file format, SMILES
*/
@TestClass("org.openscience.cdk.io.SMILESWriterTest")
public class SMILESWriter extends DefaultChemObjectWriter {
private static ILoggingTool logger =
LoggingToolFactory.createLoggingTool(SMILESWriter.class);
private BufferedWriter writer;
private BooleanIOSetting useAromaticityFlag;
/**
* Contructs a new SMILESWriter that can write a list of SMILES to a Writer
*
* @param out The Writer to write to
*/
public SMILESWriter(Writer out) {
try {
if (out instanceof BufferedWriter) {
writer = (BufferedWriter)out;
} else {
writer = new BufferedWriter(out);
}
} catch (Exception exc) {
}
initIOSettings();
}
public SMILESWriter(OutputStream output) {
this(new OutputStreamWriter(output));
}
public SMILESWriter() {
this(new StringWriter());
}
@TestMethod("testGetFormat")
public IResourceFormat getFormat() {
return SMILESFormat.getInstance();
}
public void setWriter(Writer out) throws CDKException {
if (out instanceof BufferedWriter) {
writer = (BufferedWriter)out;
} else {
writer = new BufferedWriter(out);
}
}
public void setWriter(OutputStream output) throws CDKException {
setWriter(new OutputStreamWriter(output));
}
/**
* Contructs a new SMILESWriter that can write an list of SMILES to a given OutputStream
*
* @param out The OutputStream to write to
*/
public SMILESWriter(FileOutputStream out) {
this(new OutputStreamWriter(out));
}
/**
* Flushes the output and closes this object
*/
@TestMethod("testClose")
public void close() throws IOException {
writer.flush();
writer.close();
}
@TestMethod("testAccepts")
public boolean accepts(Class classObject) {
Class[] interfaces = classObject.getInterfaces();
for (int i=0; i<interfaces.length; i++) {
if (IMoleculeSet.class.equals(interfaces[i])) return true;
if (IMolecule.class.equals(interfaces[i])) return true;
}
Class superClass = classObject.getSuperclass();
if (superClass != null) return this.accepts(superClass);
return false;
}
/**
* Writes the content from object to output.
*
* @param object IChemObject of which the data is outputted.
*/
public void write(IChemObject object) throws CDKException {
if (object instanceof IMoleculeSet) {
writeMoleculeSet((IMoleculeSet)object);
} else if (object instanceof IMolecule) {
writeMolecule((IMolecule)object);
} else {
throw new CDKException("Only supported is writing of ChemFile and Molecule objects.");
}
}
/**
* Writes a list of molecules to an OutputStream
*
* @param som MoleculeSet that is written to an OutputStream
*/
public void writeMoleculeSet(IMoleculeSet som)
{
writeMolecule(som.getMolecule(0));
for (int i = 1; i <= som.getMoleculeCount() - 1; i++) {
try {
writeMolecule(som.getMolecule(i));
} catch (Exception exc) {
}
}
}
/**
* Writes the content from molecule to output.
*
* @param molecule Molecule of which the data is outputted.
*/
public void writeMolecule(IMolecule molecule) {
SmilesGenerator sg = new SmilesGenerator();
sg.setUseAromaticityFlag(useAromaticityFlag.isSet());
String smiles = "";
try {
smiles = sg.createSMILES(molecule);
logger.debug("Generated SMILES: " + smiles);
writer.write(smiles);
writer.newLine();
writer.flush();
logger.debug("file flushed...");
} catch(Exception exc) {
logger.error("Error while writing Molecule: ", exc.getMessage());
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;
}
}