Skip to content

Commit a9d532e

Browse files
committed
Optionally truncate long data in SDF output
1 parent c3e90ed commit a9d532e

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ public class SDFWriter extends DefaultChemObjectWriter {
6868

6969
public static final String OptAlwaysV3000 = "writeV3000";
7070
public static final String OptWriteData = "writeProperties";
71+
public static final String OptTruncateLongData = "TruncateLongData";
7172

72-
private BufferedWriter writer;
73+
74+
private BufferedWriter writer;
7375
private BooleanIOSetting paramWriteData;
7476
private BooleanIOSetting paramWriteV3000;
77+
private BooleanIOSetting truncateData;
7578
private Set<String> propertiesToWrite;
7679

7780
/**
@@ -289,8 +292,20 @@ private void writeMolecule(IAtomContainer container) throws CDKException {
289292
if (isPrimitiveDataValue(val)) {
290293
writer.write("> <" + cleanHeaderKey + ">");
291294
writer.write('\n');
292-
if (val != null)
293-
writer.write(val.toString());
295+
if (val != null) {
296+
String valStr = val.toString();
297+
int maxDataLen = 200; // set in the spec
298+
if (truncateData.isSet()) {
299+
for (String line : valStr.split("\n")) {
300+
if (line.length() > maxDataLen)
301+
writer.write(line.substring(0, maxDataLen));
302+
else
303+
writer.write(valStr);
304+
}
305+
} else {
306+
writer.write(valStr);
307+
}
308+
}
294309
writer.write('\n');
295310
writer.write('\n');
296311
} else {
@@ -364,6 +379,9 @@ private void initIOSettings() {
364379
paramWriteV3000 = addSetting(new BooleanIOSetting(OptAlwaysV3000,
365380
IOSetting.Importance.LOW,
366381
"Write all records as V3000", "false"));
382+
truncateData = addSetting(new BooleanIOSetting(OptTruncateLongData,
383+
IOSetting.Importance.LOW,
384+
"Truncate long data files >200 characters", "false"));
367385
addSettings(new MDLV2000Writer().getSettings());
368386
addSettings(new MDLV3000Writer().getSettings());
369387
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import org.openscience.cdk.smiles.InvPair;
4949
import org.openscience.cdk.templates.TestMoleculeFactory;
5050

51-
import static org.junit.Assert.assertThat;
51+
import static org.hamcrest.MatcherAssert.assertThat;
5252
import static org.openscience.cdk.CDKConstants.ISAROMATIC;
5353

5454
import static org.junit.Assert.assertFalse;
@@ -370,4 +370,33 @@ public void setProgramName() {
370370
assertThat(mol, CoreMatchers.containsString("Bioclip"));
371371
}
372372
}
373+
374+
@Test
375+
public void optionallyTruncateLongProperties() {
376+
StringWriter sw = new StringWriter();
377+
try (SDFWriter sdfw = new SDFWriter(sw)) {
378+
sdfw.getSetting(MDLV2000Writer.OptWriteDefaultProperties)
379+
.setSetting("false");
380+
sdfw.getSetting(SDFWriter.OptTruncateLongData)
381+
.setSetting("true");
382+
IAtomContainer mol = TestMoleculeFactory.make123Triazole();
383+
mol.setProperty("MyLongField",
384+
"ThisIsAVeryLongFieldThatShouldBeWrapped" +
385+
"ThisIsAVeryLongFieldThatShouldBeWrapped" +
386+
"ThisIsAVeryLongFieldThatShouldBeWrapped" +
387+
"ThisIsAVeryLongFieldThatShouldBeWrapped" +
388+
"ThisIsAVeryLongFieldThatShouldBeWrapped" +
389+
"ThisIsAVeryLongFieldThatShouldBeWrapped" +
390+
"ThisIsAVeryLongFieldThatShouldBeWrapped" +
391+
"ThisIsAVeryLongFieldThatShouldBeWrapped" +
392+
"ThisIsAVeryLongFieldThatShouldBeWrapped" +
393+
"ThisIsAVeryLongFieldThatShouldBeWrapped");
394+
sdfw.write(mol);
395+
} catch (IOException | CDKException e) {
396+
e.printStackTrace();
397+
}
398+
String sdf = sw.toString();
399+
assertThat(sdf,
400+
CoreMatchers.containsString("ThisIsAVeryLongFieldThatShouldBeWrappedThisIsAVeryLongFieldThatShouldBeWrappedThisIsAVeryLongFieldThatShouldBeWrappedThisIsAVeryLongFieldThatShouldBeWrappedThisIsAVeryLongFieldThatShouldBeWrappedThisI\n"));
401+
}
373402
}

0 commit comments

Comments
 (0)