Skip to content

Commit faef4b1

Browse files
committed
Preliminary support for agents in RXNfiles
1 parent bd19601 commit faef4b1

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

storage/ctab/src/main/java/org/openscience/cdk/io/MDLRXNReader.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {
303303

304304
int reactantCount = 0;
305305
int productCount = 0;
306+
int agentCount = 0; // optional
306307
try {
307308
String countsLine = input.readLine();
308309
linecount++;
@@ -318,10 +319,13 @@ private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {
318319
* this line contains the number of reactants and products
319320
*/
320321
StringTokenizer tokenizer = new StringTokenizer(countsLine);
321-
reactantCount = Integer.valueOf(tokenizer.nextToken()).intValue();
322+
reactantCount = Integer.parseInt(tokenizer.nextToken());
322323
logger.info("Expecting " + reactantCount + " reactants in file");
323-
productCount = Integer.valueOf(tokenizer.nextToken()).intValue();
324+
productCount = Integer.parseInt(tokenizer.nextToken());
324325
logger.info("Expecting " + productCount + " products in file");
326+
if (tokenizer.hasMoreTokens())
327+
agentCount = Integer.parseInt(tokenizer.nextToken());
328+
logger.info("Expecting " + agentCount + " agents in file");
325329
} catch (IOException | NumberFormatException exception) {
326330
logger.debug(exception);
327331
throw new CDKException("Error while counts line of RXN file", exception);
@@ -330,7 +334,7 @@ private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {
330334
// now read the reactants
331335
try {
332336
for (int i = 1; i <= reactantCount; i++) {
333-
StringBuffer molFile = new StringBuffer();
337+
StringBuilder molFile = new StringBuilder();
334338
input.readLine(); // announceMDLFileLine
335339
String molFileLine = "";
336340
do {
@@ -358,7 +362,7 @@ private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {
358362
// now read the products
359363
try {
360364
for (int i = 1; i <= productCount; i++) {
361-
StringBuffer molFile = new StringBuffer();
365+
StringBuilder molFile = new StringBuilder();
362366
input.readLine(); // String announceMDLFileLine =
363367
String molFileLine = "";
364368
do {
@@ -383,6 +387,34 @@ private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {
383387
throw new CDKException("Error while reading products", exception);
384388
}
385389

390+
// now read the products
391+
try {
392+
for (int i = 1; i <= agentCount; i++) {
393+
StringBuilder molFile = new StringBuilder();
394+
input.readLine(); // String announceMDLFileLine =
395+
String molFileLine = "";
396+
do {
397+
molFileLine = input.readLine();
398+
molFile.append(molFileLine);
399+
molFile.append('\n');
400+
} while (!molFileLine.equals("M END"));
401+
402+
// read MDL molfile content
403+
MDLReader reader = new MDLReader(new StringReader(molFile.toString()), super.mode);
404+
IAtomContainer agent = (IAtomContainer) reader.read(builder.newInstance(IAtomContainer.class));
405+
reader.close();
406+
407+
// add reactant
408+
reaction.addAgent(agent);
409+
}
410+
} catch (CDKException exception) {
411+
// rethrow exception from MDLReader
412+
throw exception;
413+
} catch (IOException | IllegalArgumentException exception) {
414+
logger.debug(exception);
415+
throw new CDKException("Error while reading products", exception);
416+
}
417+
386418
// now try to map things, if wanted
387419
logger.info("Reading atom-atom mapping from file");
388420
// distribute all atoms over two AtomContainer's

storage/ctab/src/main/java/org/openscience/cdk/io/MDLRXNWriter.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import org.openscience.cdk.interfaces.IReactionSet;
3434
import org.openscience.cdk.io.formats.IResourceFormat;
3535
import org.openscience.cdk.io.formats.MDLFormat;
36+
import org.openscience.cdk.io.setting.BooleanIOSetting;
37+
import org.openscience.cdk.io.setting.IOSetting;
38+
import org.openscience.cdk.io.setting.StringIOSetting;
3639
import org.openscience.cdk.tools.ILoggingTool;
3740
import org.openscience.cdk.tools.LoggingToolFactory;
3841

@@ -68,6 +71,10 @@
6871
*/
6972
public class MDLRXNWriter extends DefaultChemObjectWriter {
7073

74+
public static final String OptWriteAgents = "WriteAgents";
75+
76+
private BooleanIOSetting writeAgents;
77+
7178
private BufferedWriter writer;
7279
private static ILoggingTool logger = LoggingToolFactory.createLoggingTool(MDLRXNWriter.class);
7380
private int reactionNumber;
@@ -89,6 +96,7 @@ public MDLRXNWriter(Writer out) {
8996
} catch (Exception exc) {
9097
}
9198
this.reactionNumber = 1;
99+
initIOSettings();
92100
}
93101

94102
/**
@@ -105,6 +113,13 @@ public MDLRXNWriter() {
105113
this(new StringWriter());
106114
}
107115

116+
private void initIOSettings() {
117+
writeAgents = addSetting(new BooleanIOSetting(OptWriteAgents,
118+
IOSetting.Importance.LOW,
119+
"Output agents in the RXN file",
120+
"true"));
121+
}
122+
108123
@Override
109124
public IResourceFormat getFormat() {
110125
return MDLFormat.getInstance();
@@ -198,7 +213,9 @@ private void writeReactionSet(IReactionSet reactions) throws CDKException {
198213
private void writeReaction(IReaction reaction) throws CDKException {
199214
int reactantCount = reaction.getReactantCount();
200215
int productCount = reaction.getProductCount();
201-
if (reactantCount <= 0 || productCount <= 0) {
216+
int agentCount = reaction.getAgents().getAtomContainerCount();
217+
if (reactantCount + productCount + agentCount == 0) {
218+
// JWM: an empty record is still valid though..?!?
202219
throw new CDKException("Either no reactants or no products present.");
203220
}
204221

@@ -230,6 +247,8 @@ private void writeReaction(IReaction reaction) throws CDKException {
230247
line = "";
231248
line += formatMDLInt(reactantCount, 3);
232249
line += formatMDLInt(productCount, 3);
250+
if (agentCount > 0 && writeAgents.isSet())
251+
line += formatMDLInt(agentCount, 3);
233252
writer.write(line);
234253
writer.write('\n');
235254

@@ -242,6 +261,8 @@ private void writeReaction(IReaction reaction) throws CDKException {
242261
}
243262
writeAtomContainerSet(reaction.getReactants());
244263
writeAtomContainerSet(reaction.getProducts());
264+
if (agentCount > 0 && writeAgents.isSet())
265+
writeAtomContainerSet(reaction.getAgents());
245266

246267
//write sdfields, if any
247268
if (rdFields != null) {

0 commit comments

Comments
 (0)