Skip to content

Commit

Permalink
Add option to set program name in CTfile output.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Mar 15, 2019
1 parent 2e4ceab commit ecdba13
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.openscience.cdk.io.formats.MDLFormat;
import org.openscience.cdk.io.setting.BooleanIOSetting;
import org.openscience.cdk.io.setting.IOSetting;
import org.openscience.cdk.io.setting.StringIOSetting;
import org.openscience.cdk.isomorphism.matchers.Expr;
import org.openscience.cdk.isomorphism.matchers.QueryBond;
import org.openscience.cdk.sgroup.Sgroup;
Expand Down Expand Up @@ -115,6 +116,7 @@ public class MDLV2000Writer extends DefaultChemObjectWriter {
public static final String OptWriteAromaticBondTypes = "WriteAromaticBondTypes";
public static final String OptWriteQueryFormatValencies = "WriteQueryFormatValencies";
public static final String OptWriteDefaultProperties = "WriteDefaultProperties";
public static final String OptProgramName = "PorgramName";

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

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

private BooleanIOSetting writeDefaultProps;

private StringIOSetting programNameOpt;

private BufferedWriter writer;

/**
Expand Down Expand Up @@ -336,6 +340,18 @@ private void writeChemFile(IChemFile file) throws Exception {
writeMolecule(bigPile);
}

private String getProgName() {
String progname = programNameOpt.getSetting();
if (progname == null)
return " ";
else if (progname.length() > 8)
return progname.substring(0, 8);
else if (progname.length() < 8)
return String.format("%-8s", progname);
else
return progname;
}

/**
* Writes a Molecule to an OutputStream in MDL sdf format.
*
Expand Down Expand Up @@ -364,7 +380,8 @@ public void writeMolecule(IAtomContainer container) throws Exception {
* program input, internal registry number (R) if input through MDL
* form. A blank line can be substituted for line 2.
*/
writer.write(" CDK ");
writer.write(" ");
writer.write(getProgName());
writer.write(new SimpleDateFormat("MMddyyHHmm").format(System.currentTimeMillis()));
if (dim != 0) {
writer.write(Integer.toString(dim));
Expand Down Expand Up @@ -1166,6 +1183,10 @@ private void initIOSettings() {
IOSetting.Importance.LOW,
"Write trailing zero's on atom/bond property blocks even if they're not used.",
"true"));
programNameOpt = addSetting(new StringIOSetting(OptProgramName,
IOSetting.Importance.LOW,
"Program name to write at the top of the molfile header, should be exactly 8 characters long",
"CDK"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.openscience.cdk.interfaces.ITetrahedralChirality.Stereo;
import org.openscience.cdk.io.formats.IResourceFormat;
import org.openscience.cdk.io.formats.MDLV3000Format;
import org.openscience.cdk.io.setting.IOSetting;
import org.openscience.cdk.io.setting.StringIOSetting;
import org.openscience.cdk.sgroup.Sgroup;
import org.openscience.cdk.sgroup.SgroupBracket;
import org.openscience.cdk.sgroup.SgroupKey;
Expand Down Expand Up @@ -68,6 +70,7 @@
import java.util.regex.Pattern;

import static org.openscience.cdk.CDKConstants.ATOM_ATOM_MAPPING;
import static org.openscience.cdk.io.MDLV2000Writer.OptProgramName;

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

/**
* Create a new V3000 writer, output to the provided JDK writer.
*
* @param writer output location
*/
public MDLV3000Writer(Writer writer) {
this();
this.writer = new V30LineWriter(writer);
}

Expand All @@ -106,13 +111,15 @@ public MDLV3000Writer(Writer writer) {
* @param out output location
*/
public MDLV3000Writer(OutputStream out) throws CDKException {
this();
this.setWriter(out);
}

/**
* Default empty constructor.
*/
public MDLV3000Writer() {
initIOSettings();
}

/**
Expand Down Expand Up @@ -140,6 +147,17 @@ private static <T> Integer findIdx(Map<T, Integer> idxs, T obj) {
return idx;
}

private String getProgName() {
String progname = programNameOpt.getSetting();
if (progname == null)
return " ";
else if (progname.length() > 8)
return progname.substring(0, 8);
else if (progname.length() < 8)
return String.format("%-8s", progname);
else
return progname;
}

/**
* Write the three line header of the MDL format: title, version/timestamp, remark.
Expand All @@ -162,7 +180,8 @@ private void writeHeader(IAtomContainer mol) throws IOException {
* program input, internal registry number (R) if input through MDL
* form. A blank line can be substituted for line 2.
*/
writer.writeDirect(" CDK ");
writer.writeDirect(" ");
writer.writeDirect(getProgName());
writer.writeDirect(HEADER_DATE_FORMAT.format(System.currentTimeMillis()));
final int dim = getNumberOfDimensions(mol);
if (dim != 0) {
Expand Down Expand Up @@ -921,4 +940,16 @@ public void close() throws IOException {
writer.close();
}
}

/**
* Initializes IO settings.<br>
* Please note with regards to "writeAromaticBondTypes": bond type values 4 through 8 are for SSS queries only,
* so a 'query file' is created if the container has aromatic bonds and this settings is true.
*/
private void initIOSettings() {
programNameOpt = addSetting(new StringIOSetting(OptProgramName,
IOSetting.Importance.LOW,
"Program name to write at the top of the molfile header, should be exactly 8 characters long",
"CDK"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public class SDFWriter extends DefaultChemObjectWriter {

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

public static final String OptAlwaysV3000 = "writeV3000";
public static final String OptWriteData = "writeProperties";

private BufferedWriter writer;
private BooleanIOSetting paramWriteData;
private BooleanIOSetting paramWriteV3000;
Expand Down Expand Up @@ -355,10 +358,10 @@ private boolean isCDKInternalProperty(Object propKey) {
}

private void initIOSettings() {
paramWriteData = addSetting(new BooleanIOSetting("writeProperties",
paramWriteData = addSetting(new BooleanIOSetting(OptWriteData,
IOSetting.Importance.LOW,
"Should molecule properties be written as non-structural data", "true"));
paramWriteV3000 = addSetting(new BooleanIOSetting("writeV3000",
paramWriteV3000 = addSetting(new BooleanIOSetting(OptAlwaysV3000,
IOSetting.Importance.LOW,
"Write all records as V3000", "false"));
addSettings(new MDLV2000Writer().getSettings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Collections;
import java.util.Properties;

import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -345,4 +346,28 @@ public void testPropertyOutput_none() throws CDKException, IOException {
assertFalse(out.contains("> <two>"));
assertFalse(out.contains("> <one>"));
}

@Test
public void setProgramName() {
StringWriter sw = new StringWriter();
try (SDFWriter sdfw = new SDFWriter(sw)) {
sdfw.getSetting(MDLV2000Writer.OptWriteDefaultProperties)
.setSetting("false");
sdfw.getSetting(MDLV2000Writer.OptProgramName)
.setSetting("Bioclipse");

sdfw.write(TestMoleculeFactory.make123Triazole());

sdfw.getSetting(SDFWriter.OptAlwaysV3000)
.setSetting("true");

sdfw.write(TestMoleculeFactory.make123Triazole());
} catch (IOException | CDKException e) {
e.printStackTrace();
}
String sdf = sw.toString();
for (String mol : sdf.split("\\$\\$\\$\\$", 2)) {
assertThat(mol, CoreMatchers.containsString("Bioclip"));
}
}
}

0 comments on commit ecdba13

Please sign in to comment.