Skip to content

Commit ecdba13

Browse files
committed
Add option to set program name in CTfile output.
1 parent 2e4ceab commit ecdba13

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.openscience.cdk.io.formats.MDLFormat;
4343
import org.openscience.cdk.io.setting.BooleanIOSetting;
4444
import org.openscience.cdk.io.setting.IOSetting;
45+
import org.openscience.cdk.io.setting.StringIOSetting;
4546
import org.openscience.cdk.isomorphism.matchers.Expr;
4647
import org.openscience.cdk.isomorphism.matchers.QueryBond;
4748
import org.openscience.cdk.sgroup.Sgroup;
@@ -115,6 +116,7 @@ public class MDLV2000Writer extends DefaultChemObjectWriter {
115116
public static final String OptWriteAromaticBondTypes = "WriteAromaticBondTypes";
116117
public static final String OptWriteQueryFormatValencies = "WriteQueryFormatValencies";
117118
public static final String OptWriteDefaultProperties = "WriteDefaultProperties";
119+
public static final String OptProgramName = "PorgramName";
118120

119121
private final static ILoggingTool logger = LoggingToolFactory.createLoggingTool(MDLV2000Writer.class);
120122

@@ -207,6 +209,8 @@ public static SPIN_MULTIPLICITY ofValue(int value) throws CDKException {
207209

208210
private BooleanIOSetting writeDefaultProps;
209211

212+
private StringIOSetting programNameOpt;
213+
210214
private BufferedWriter writer;
211215

212216
/**
@@ -336,6 +340,18 @@ private void writeChemFile(IChemFile file) throws Exception {
336340
writeMolecule(bigPile);
337341
}
338342

343+
private String getProgName() {
344+
String progname = programNameOpt.getSetting();
345+
if (progname == null)
346+
return " ";
347+
else if (progname.length() > 8)
348+
return progname.substring(0, 8);
349+
else if (progname.length() < 8)
350+
return String.format("%-8s", progname);
351+
else
352+
return progname;
353+
}
354+
339355
/**
340356
* Writes a Molecule to an OutputStream in MDL sdf format.
341357
*
@@ -364,7 +380,8 @@ public void writeMolecule(IAtomContainer container) throws Exception {
364380
* program input, internal registry number (R) if input through MDL
365381
* form. A blank line can be substituted for line 2.
366382
*/
367-
writer.write(" CDK ");
383+
writer.write(" ");
384+
writer.write(getProgName());
368385
writer.write(new SimpleDateFormat("MMddyyHHmm").format(System.currentTimeMillis()));
369386
if (dim != 0) {
370387
writer.write(Integer.toString(dim));
@@ -1166,6 +1183,10 @@ private void initIOSettings() {
11661183
IOSetting.Importance.LOW,
11671184
"Write trailing zero's on atom/bond property blocks even if they're not used.",
11681185
"true"));
1186+
programNameOpt = addSetting(new StringIOSetting(OptProgramName,
1187+
IOSetting.Importance.LOW,
1188+
"Program name to write at the top of the molfile header, should be exactly 8 characters long",
1189+
"CDK"));
11691190
}
11701191

11711192
/**

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.openscience.cdk.interfaces.ITetrahedralChirality.Stereo;
3737
import org.openscience.cdk.io.formats.IResourceFormat;
3838
import org.openscience.cdk.io.formats.MDLV3000Format;
39+
import org.openscience.cdk.io.setting.IOSetting;
40+
import org.openscience.cdk.io.setting.StringIOSetting;
3941
import org.openscience.cdk.sgroup.Sgroup;
4042
import org.openscience.cdk.sgroup.SgroupBracket;
4143
import org.openscience.cdk.sgroup.SgroupKey;
@@ -68,6 +70,7 @@
6870
import java.util.regex.Pattern;
6971

7072
import static org.openscience.cdk.CDKConstants.ATOM_ATOM_MAPPING;
73+
import static org.openscience.cdk.io.MDLV2000Writer.OptProgramName;
7174

7275
/**
7376
* Ctab V3000 format output. This writer provides output to the more modern (but less widely
@@ -89,14 +92,16 @@ public final class MDLV3000Writer extends DefaultChemObjectWriter {
8992
public static final SimpleDateFormat HEADER_DATE_FORMAT = new SimpleDateFormat("MMddyyHHmm");
9093
public static final NumberFormat DECIMAL_FORMAT = new DecimalFormat("#.####", DecimalFormatSymbols.getInstance(Locale.ROOT));
9194
private static final Pattern R_GRP_NUM = Pattern.compile("R(\\d+)");
92-
private V30LineWriter writer;
95+
private V30LineWriter writer;
96+
private StringIOSetting programNameOpt;
9397

9498
/**
9599
* Create a new V3000 writer, output to the provided JDK writer.
96100
*
97101
* @param writer output location
98102
*/
99103
public MDLV3000Writer(Writer writer) {
104+
this();
100105
this.writer = new V30LineWriter(writer);
101106
}
102107

@@ -106,13 +111,15 @@ public MDLV3000Writer(Writer writer) {
106111
* @param out output location
107112
*/
108113
public MDLV3000Writer(OutputStream out) throws CDKException {
114+
this();
109115
this.setWriter(out);
110116
}
111117

112118
/**
113119
* Default empty constructor.
114120
*/
115121
public MDLV3000Writer() {
122+
initIOSettings();
116123
}
117124

118125
/**
@@ -140,6 +147,17 @@ private static <T> Integer findIdx(Map<T, Integer> idxs, T obj) {
140147
return idx;
141148
}
142149

150+
private String getProgName() {
151+
String progname = programNameOpt.getSetting();
152+
if (progname == null)
153+
return " ";
154+
else if (progname.length() > 8)
155+
return progname.substring(0, 8);
156+
else if (progname.length() < 8)
157+
return String.format("%-8s", progname);
158+
else
159+
return progname;
160+
}
143161

144162
/**
145163
* Write the three line header of the MDL format: title, version/timestamp, remark.
@@ -162,7 +180,8 @@ private void writeHeader(IAtomContainer mol) throws IOException {
162180
* program input, internal registry number (R) if input through MDL
163181
* form. A blank line can be substituted for line 2.
164182
*/
165-
writer.writeDirect(" CDK ");
183+
writer.writeDirect(" ");
184+
writer.writeDirect(getProgName());
166185
writer.writeDirect(HEADER_DATE_FORMAT.format(System.currentTimeMillis()));
167186
final int dim = getNumberOfDimensions(mol);
168187
if (dim != 0) {
@@ -921,4 +940,16 @@ public void close() throws IOException {
921940
writer.close();
922941
}
923942
}
943+
944+
/**
945+
* Initializes IO settings.<br>
946+
* Please note with regards to "writeAromaticBondTypes": bond type values 4 through 8 are for SSS queries only,
947+
* so a 'query file' is created if the container has aromatic bonds and this settings is true.
948+
*/
949+
private void initIOSettings() {
950+
programNameOpt = addSetting(new StringIOSetting(OptProgramName,
951+
IOSetting.Importance.LOW,
952+
"Program name to write at the top of the molfile header, should be exactly 8 characters long",
953+
"CDK"));
954+
}
924955
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public class SDFWriter extends DefaultChemObjectWriter {
6666

6767
private final static ILoggingTool logger = LoggingToolFactory.createLoggingTool(SDFWriter.class);
6868

69+
public static final String OptAlwaysV3000 = "writeV3000";
70+
public static final String OptWriteData = "writeProperties";
71+
6972
private BufferedWriter writer;
7073
private BooleanIOSetting paramWriteData;
7174
private BooleanIOSetting paramWriteV3000;
@@ -355,10 +358,10 @@ private boolean isCDKInternalProperty(Object propKey) {
355358
}
356359

357360
private void initIOSettings() {
358-
paramWriteData = addSetting(new BooleanIOSetting("writeProperties",
361+
paramWriteData = addSetting(new BooleanIOSetting(OptWriteData,
359362
IOSetting.Importance.LOW,
360363
"Should molecule properties be written as non-structural data", "true"));
361-
paramWriteV3000 = addSetting(new BooleanIOSetting("writeV3000",
364+
paramWriteV3000 = addSetting(new BooleanIOSetting(OptAlwaysV3000,
362365
IOSetting.Importance.LOW,
363366
"Write all records as V3000", "false"));
364367
addSettings(new MDLV2000Writer().getSettings());

storage/ctab/src/test/java/org/openscience/cdk/io/SDFWriterTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Collections;
2828
import java.util.Properties;
2929

30+
import org.hamcrest.CoreMatchers;
3031
import org.hamcrest.Matchers;
3132
import org.junit.Assert;
3233
import org.junit.BeforeClass;
@@ -345,4 +346,28 @@ public void testPropertyOutput_none() throws CDKException, IOException {
345346
assertFalse(out.contains("> <two>"));
346347
assertFalse(out.contains("> <one>"));
347348
}
349+
350+
@Test
351+
public void setProgramName() {
352+
StringWriter sw = new StringWriter();
353+
try (SDFWriter sdfw = new SDFWriter(sw)) {
354+
sdfw.getSetting(MDLV2000Writer.OptWriteDefaultProperties)
355+
.setSetting("false");
356+
sdfw.getSetting(MDLV2000Writer.OptProgramName)
357+
.setSetting("Bioclipse");
358+
359+
sdfw.write(TestMoleculeFactory.make123Triazole());
360+
361+
sdfw.getSetting(SDFWriter.OptAlwaysV3000)
362+
.setSetting("true");
363+
364+
sdfw.write(TestMoleculeFactory.make123Triazole());
365+
} catch (IOException | CDKException e) {
366+
e.printStackTrace();
367+
}
368+
String sdf = sw.toString();
369+
for (String mol : sdf.split("\\$\\$\\$\\$", 2)) {
370+
assertThat(mol, CoreMatchers.containsString("Bioclip"));
371+
}
372+
}
348373
}

0 commit comments

Comments
 (0)