|
45 | 45 | import java.io.InputStreamReader; |
46 | 46 | import java.io.Reader; |
47 | 47 | import java.io.StringReader; |
| 48 | +import java.util.ArrayList; |
48 | 49 | import java.util.Iterator; |
| 50 | +import java.util.List; |
49 | 51 | import java.util.StringTokenizer; |
50 | 52 |
|
51 | 53 | /** |
@@ -198,118 +200,72 @@ private IReaction readReaction(IChemObjectBuilder builder) throws CDKException { |
198 | 200 | throw new CDKException("Error while reading header of RXN file", exception); |
199 | 201 | } |
200 | 202 |
|
201 | | - int reactantCount = 0; |
202 | | - int productCount = 0; |
| 203 | + int numReactans = 0; |
| 204 | + int numProducts = 0; |
203 | 205 | int agentCount = 0; |
204 | 206 | try { |
205 | 207 | String countsLine = input.readLine(); |
206 | 208 | /* |
207 | 209 | * this line contains the number of reactants and products |
208 | 210 | */ |
209 | 211 | StringTokenizer tokenizer = new StringTokenizer(countsLine); |
210 | | - reactantCount = Integer.valueOf(tokenizer.nextToken()); |
211 | | - logger.info("Expecting " + reactantCount + " reactants in file"); |
212 | | - productCount = Integer.valueOf(tokenizer.nextToken()); |
| 212 | + numReactans = Integer.valueOf(tokenizer.nextToken()); |
| 213 | + logger.info("Expecting " + numReactans + " reactants in file"); |
| 214 | + numProducts = Integer.valueOf(tokenizer.nextToken()); |
213 | 215 | if (tokenizer.hasMoreTokens()) { |
214 | 216 | agentCount = Integer.valueOf(tokenizer.nextToken()); |
215 | 217 | // ChemAxon extension, technically BIOVIA now support this but |
216 | 218 | // not documented yet |
217 | 219 | if (mode == Mode.STRICT && agentCount > 0) |
218 | 220 | throw new CDKException("RXN files uses agent count extension"); |
219 | 221 | } |
220 | | - logger.info("Expecting " + productCount + " products in file"); |
| 222 | + logger.info("Expecting " + numProducts + " products in file"); |
221 | 223 | } catch (IOException | NumberFormatException exception) { |
222 | 224 | logger.debug(exception); |
223 | 225 | throw new CDKException("Error while counts line of RXN file", exception); |
224 | 226 | } |
225 | 227 |
|
226 | | - // now read the reactants |
| 228 | + // now read the molecules |
227 | 229 | try { |
228 | | - for (int i = 1; i <= reactantCount; i++) { |
229 | | - StringBuffer molFile = new StringBuffer(); |
230 | | - String molFileLine = ""; |
231 | | - while (!input.readLine().startsWith("$MOL")) { |
232 | | - // skip |
233 | | - } |
234 | | - do { |
235 | | - molFileLine = input.readLine(); |
236 | | - molFile.append(molFileLine); |
237 | | - molFile.append('\n'); |
238 | | - } while (!molFileLine.equals("M END")); |
239 | | - |
240 | | - // read MDL molfile content |
241 | | - // Changed this to mdlv2000 reader |
242 | | - MDLV2000Reader reader = new MDLV2000Reader(new StringReader(molFile.toString()), super.mode); |
243 | | - IAtomContainer reactant = (IAtomContainer) reader.read(builder.newInstance(IAtomContainer.class)); |
244 | | - reader.close(); |
245 | | - |
246 | | - // add reactant |
247 | | - reaction.addReactant(reactant); |
| 230 | + String line = input.readLine(); |
| 231 | + if (line == null || !line.startsWith("$MOL")) { |
| 232 | + throw new CDKException("Expected $MOL to start, was" + line); |
248 | 233 | } |
249 | | - } catch (CDKException exception) { |
250 | | - // rethrow exception from MDLReader |
251 | | - throw exception; |
252 | | - } catch (IOException | IllegalArgumentException exception) { |
253 | | - logger.debug(exception); |
254 | | - throw new CDKException("Error while reading reactant", exception); |
255 | | - } |
256 | 234 |
|
257 | | - // now read the products |
258 | | - try { |
259 | | - for (int i = 1; i <= productCount; i++) { |
260 | | - StringBuffer molFile = new StringBuffer(); |
261 | | - while (!input.readLine().startsWith("$MOL")) { |
262 | | - // skip |
263 | | - } |
264 | | - String molFileLine = ""; |
265 | | - do { |
266 | | - molFileLine = input.readLine(); |
267 | | - molFile.append(molFileLine); |
268 | | - molFile.append('\n'); |
269 | | - } while (!molFileLine.equals("M END")); |
270 | | - |
271 | | - // read MDL molfile content |
272 | | - MDLV2000Reader reader = new MDLV2000Reader(new StringReader(molFile.toString())); |
273 | | - IAtomContainer product = (IAtomContainer) reader.read(builder.newInstance(IAtomContainer.class)); |
274 | | - reader.close(); |
| 235 | + List<IAtomContainer> components = new ArrayList<>(); |
275 | 236 |
|
276 | | - // add reactant |
277 | | - reaction.addProduct(product); |
| 237 | + StringBuilder sb = new StringBuilder(); |
| 238 | + while ((line = input.readLine()) != null) { |
| 239 | + if (line.startsWith("$MOL")) { |
| 240 | + processMol(builder.newAtomContainer(), components, sb); |
| 241 | + sb.setLength(0); |
| 242 | + } else { |
| 243 | + sb.append(line).append('\n'); |
| 244 | + } |
278 | 245 | } |
279 | | - } catch (CDKException exception) { |
280 | | - // rethrow exception from MDLReader |
281 | | - throw exception; |
282 | | - } catch (IOException | IllegalArgumentException exception) { |
283 | | - logger.debug(exception); |
284 | | - throw new CDKException("Error while reading products", exception); |
285 | | - } |
286 | | - |
287 | | - // now read the products |
288 | | - try { |
289 | | - for (int i = 1; i <= agentCount; i++) { |
290 | | - StringBuffer molFile = new StringBuffer(); |
291 | | - input.readLine(); // String announceMDLFileLine = |
292 | | - String molFileLine = ""; |
293 | | - do { |
294 | | - molFileLine = input.readLine(); |
295 | | - molFile.append(molFileLine); |
296 | | - molFile.append('\n'); |
297 | | - } while (!molFileLine.equals("M END")); |
298 | 246 |
|
299 | | - // read MDL molfile content |
300 | | - MDLV2000Reader reader = new MDLV2000Reader(new StringReader(molFile.toString())); |
301 | | - IAtomContainer product = (IAtomContainer) reader.read(builder.newInstance(IAtomContainer.class)); |
302 | | - reader.close(); |
| 247 | + // last record |
| 248 | + if (sb.length() > 0) |
| 249 | + processMol(builder.newAtomContainer(), components, sb); |
303 | 250 |
|
304 | | - // add reactant |
305 | | - reaction.addAgent(product); |
| 251 | + for (IAtomContainer component : components.subList(0, numReactans)) { |
| 252 | + reaction.addReactant(component); |
| 253 | + } |
| 254 | + for (IAtomContainer component : components.subList(numReactans, |
| 255 | + numReactans+numProducts)) { |
| 256 | + reaction.addProduct(component); |
306 | 257 | } |
| 258 | + for (IAtomContainer component : components.subList(numReactans+numProducts, |
| 259 | + components.size())) { |
| 260 | + reaction.addAgent(component); |
| 261 | + } |
| 262 | + |
307 | 263 | } catch (CDKException exception) { |
308 | 264 | // rethrow exception from MDLReader |
309 | 265 | throw exception; |
310 | 266 | } catch (IOException | IllegalArgumentException exception) { |
311 | 267 | logger.debug(exception); |
312 | | - throw new CDKException("Error while reading products", exception); |
| 268 | + throw new CDKException("Error while reading reactant", exception); |
313 | 269 | } |
314 | 270 |
|
315 | 271 | // now try to map things, if wanted |
@@ -348,6 +304,12 @@ private IReaction readReaction(IChemObjectBuilder builder) throws CDKException { |
348 | 304 | return reaction; |
349 | 305 | } |
350 | 306 |
|
| 307 | + private void processMol(IAtomContainer mol, List<IAtomContainer> components, StringBuilder sb) throws CDKException, IOException { |
| 308 | + MDLV2000Reader reader = new MDLV2000Reader(new StringReader(sb.toString()), super.mode); |
| 309 | + components.add(reader.read(mol)); |
| 310 | + reader.close(); |
| 311 | + } |
| 312 | + |
351 | 313 | @Override |
352 | 314 | public void close() throws IOException { |
353 | 315 | input.close(); |
|
0 commit comments