/
MDLRXNV3000Reader.java
305 lines (276 loc) · 11 KB
/
MDLRXNV3000Reader.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* $Revision$ $Author$ $Date$
*
* Copyright (C) 2003-2008 Egon Willighagen <egonw@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This library 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.
*
* This library 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 library; 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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.StringTokenizer;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IReaction;
import org.openscience.cdk.interfaces.IReactionSet;
import org.openscience.cdk.io.formats.IResourceFormat;
import org.openscience.cdk.io.formats.MDLRXNV3000Format;
import org.openscience.cdk.io.setting.IOSetting;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
/**
* Class that implements the new MDL mol format introduced in August 2002.
* The overall syntax is compatible with the old format, but I consider
* the format completely different, and thus implemented a separate Reader
* for it.
*
* @cdk.module io
* @cdk.githash
*
* @author Egon Willighagen <egonw@sci.kun.nl>
* @cdk.created 2003-10-05
*
* @cdk.keyword MDL V3000
* @cdk.require java1.4+
*/
@TestClass("org.openscience.cdk.io.MDLRXNV3000ReaderTest")
public class MDLRXNV3000Reader extends DefaultChemObjectReader {
BufferedReader input = null;
private static ILoggingTool logger =
LoggingToolFactory.createLoggingTool(MDLRXNV3000Reader.class);
public MDLRXNV3000Reader(Reader in) {
this(in, Mode.RELAXED);
}
public MDLRXNV3000Reader(Reader in, Mode mode) {
if (in instanceof BufferedReader) {
input = (BufferedReader)in;
} else {
input = new BufferedReader(in);
}
initIOSettings();
super.mode = mode;
}
public MDLRXNV3000Reader(InputStream input) {
this(input, Mode.RELAXED);
}
public MDLRXNV3000Reader(InputStream input, Mode mode) {
this(new InputStreamReader(input), mode);
}
public MDLRXNV3000Reader() {
this(new StringReader(""));
}
@TestMethod("testGetFormat")
public IResourceFormat getFormat() {
return MDLRXNV3000Format.getInstance();
}
@TestMethod("testSetReader_Reader")
public void setReader(Reader input) throws CDKException {
if (input instanceof BufferedReader) {
this.input = (BufferedReader)input;
} else {
this.input = new BufferedReader(input);
}
}
@TestMethod("testSetReader_InputStream")
public void setReader(InputStream input) throws CDKException {
setReader(new InputStreamReader(input));
}
@TestMethod("testAccepts")
public boolean accepts(Class classObject) {
Class[] interfaces = classObject.getInterfaces();
for (int i=0; i<interfaces.length; i++) {
if (IChemModel.class.equals(interfaces[i])) return true;
if (IReaction.class.equals(interfaces[i])) return true;
}
Class superClass = classObject.getSuperclass();
if (superClass != null) return this.accepts(superClass);
return false;
}
@TestMethod("testReadReactions1")
public <T extends IChemObject> T read(T object) throws CDKException {
if (object instanceof IReaction) {
return (T)readReaction(object.getBuilder());
} else if (object instanceof IChemModel) {
IChemModel model = object.getBuilder().newInstance(IChemModel.class);
IReactionSet reactionSet = object.getBuilder().newInstance(IReactionSet.class);
reactionSet.addReaction(readReaction(object.getBuilder()));
model.setReactionSet(reactionSet);
return (T)model;
} else {
throw new CDKException("Only supported are Reaction and ChemModel, and not " +
object.getClass().getName() + "."
);
}
}
/**
* Reads the command on this line. If the line is continued on the next, that
* part is added.
*
* @return Returns the command on this line.
*/
private String readCommand() throws CDKException {
String line = readLine();
if (line.startsWith("M V30 ")) {
String command = line.substring(7);
if (command.endsWith("-")) {
command = command.substring(0, command.length()-1);
command += readCommand();
}
return command;
} else {
throw new CDKException("Could not read MDL file: unexpected line: " + line);
}
}
private String readLine() throws CDKException {
String line = null;
try {
line = input.readLine();
logger.debug("read line: " + line);
} catch (Exception exception) {
String error = "Unexpected error while reading file: " + exception.getMessage();
logger.error(error);
logger.debug(exception);
throw new CDKException(error, exception);
}
return line;
}
private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {
IReaction reaction = builder.newInstance(IReaction.class);
readLine(); // first line should be $RXN
readLine(); // second line
readLine(); // third line
readLine(); // fourth line
int reactantCount = 0;
int productCount = 0;
boolean foundCOUNTS = false;
while (isReady() && !foundCOUNTS) {
String command = readCommand();
if (command.startsWith("COUNTS")) {
StringTokenizer tokenizer = new StringTokenizer(command);
try {
tokenizer.nextToken();
reactantCount = Integer.valueOf(tokenizer.nextToken()).intValue();
logger.info("Expecting " + reactantCount + " reactants in file");
productCount = Integer.valueOf(tokenizer.nextToken()).intValue();
logger.info("Expecting " + productCount + " products in file");
} catch (Exception exception) {
logger.debug(exception);
throw new CDKException("Error while counts line of RXN file", exception);
}
foundCOUNTS = true;
} else {
logger.warn("Waiting for COUNTS line, but found: " + command);
}
}
// now read the reactants
for (int i=1; i<=reactantCount; i++) {
StringBuffer molFile = new StringBuffer();
String announceMDLFileLine = readCommand();
if (!announceMDLFileLine.equals("BEGIN REACTANT")) {
String error = "Excepted start of reactant, but found: " + announceMDLFileLine;
logger.error(error);
throw new CDKException(error);
}
String molFileLine = "";
while (!molFileLine.endsWith("END REACTANT")) {
molFileLine = readLine();
molFile.append(molFileLine);
molFile.append(System.getProperty("line.separator"));
};
try {
// read MDL molfile content
MDLV3000Reader reader = new MDLV3000Reader(
new StringReader(molFile.toString()),
super.mode
);
IAtomContainer reactant = (IAtomContainer)reader.read(
builder.newInstance(IAtomContainer.class));
// add reactant
reaction.addReactant(reactant);
} catch (Exception exception) {
String error = "Error while reading reactant: " + exception.getMessage();
logger.error(error);
logger.debug(exception);
throw new CDKException(error, exception);
}
}
// now read the products
for (int i=1; i<=productCount; i++) {
StringBuffer molFile = new StringBuffer();
String announceMDLFileLine = readCommand();
if (!announceMDLFileLine.equals("BEGIN PRODUCT")) {
String error = "Excepted start of product, but found: " + announceMDLFileLine;
logger.error(error);
throw new CDKException(error);
}
String molFileLine = "";
while (!molFileLine.endsWith("END PRODUCT")) {
molFileLine = readLine();
molFile.append(molFileLine);
molFile.append(System.getProperty("line.separator"));
};
try {
// read MDL molfile content
MDLV3000Reader reader = new MDLV3000Reader(
new StringReader(molFile.toString()));
IAtomContainer product = (IAtomContainer)reader.read(
builder.newInstance(IAtomContainer.class));
// add product
reaction.addProduct(product);
} catch (Exception exception) {
String error = "Error while reading product: " + exception.getMessage();
logger.error(error);
logger.debug(exception);
throw new CDKException(error, exception);
}
}
return reaction;
}
private boolean isReady() throws CDKException {
try {
return input.ready();
} catch (Exception exception) {
String error = "Unexpected error while reading file: " + exception.getMessage();
logger.error(error);
logger.debug(exception);
throw new CDKException(error, exception);
}
}
@TestMethod("testAccepts")
public boolean accepts(IChemObject object) {
if (object instanceof IReaction) {
return true;
} else if (object instanceof IChemModel) {
return true;
}
return false;
}
@TestMethod("testClose")
public void close() throws IOException {
input.close();
}
private void initIOSettings() {
}
}