3
* $Date: 2007-08-28 10:29:51 +0200 (Tue, 28 Aug 2007) $
6
* Copyright (C) 1997-2007 The Chemistry Development Kit (CDK) project
8
* Contact: cdk-devel@lists.sourceforge.net
10
* This program is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public License
12
* as published by the Free Software Foundation; either version 2.1
13
* of the License, or (at your option) any later version.
14
* All we ask is that proper credit is given for our work, which includes
15
* - but is not limited to - adding the above copyright notice to the beginning
16
* of your source code files, and to any copyright notice that you may distribute
17
* with programs based on this work.
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU Lesser General Public License for more details.
24
* You should have received a copy of the GNU Lesser General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29
package org.openscience.cdk.io;
31
import org.openscience.cdk.CDKConstants;
32
import org.openscience.cdk.config.IsotopeFactory;
33
import org.openscience.cdk.exception.CDKException;
34
import org.openscience.cdk.interfaces.*;
35
import org.openscience.cdk.io.formats.IResourceFormat;
36
import org.openscience.cdk.io.formats.MDLFormat;
37
import org.openscience.cdk.tools.LoggingTool;
38
import org.openscience.cdk.tools.manipulator.ChemFileManipulator;
41
import java.text.NumberFormat;
42
import java.text.SimpleDateFormat;
46
* Writes MDL mol files and SD files.
48
* A MDL mol file contains a single molecule, whereas a MDL SD file contains
49
* one or more molecules. This class is capable of writing both mol files and
50
* SD files. The correct format is automatically chosen:
52
* <li>if {@link #write(IChemObject)} is called with a {@link org.openscience.cdk.MoleculeSet MoleculeSet}
53
* as an argument a SD files is written</li>
54
* <li>if one of the two writeMolecule methods (either {@link #writeMolecule(IMolecule) this one} or
55
* {@link #writeMolecule(org.openscience.cdk.interfaces.IMolecule)} that one}) is called the first time, a mol file is written</li>
56
* <li>if one of the two writeMolecule methods is called more than once the output is a SD file</li>
59
* <p>Thus, to write several molecules to a single SD file you can either use {@link #write(IChemObject)} and pass
60
* a {@link org.openscience.cdk.MoleculeSet MoleculeSet} or you can repeatedly call one of the two
61
* writeMolecule methods.
62
* <p>For writing a MDL molfile you can this code:
64
* MDLWriter writer = new MDLWriter(new FileWriter(new File("output.mol")));
65
* writer.write((Molecule)molecule);
69
* See {@cdk.cite DAL92}.
72
* @cdk.keyword file format, MDL molfile
75
public class MDLWriter extends DefaultChemObjectWriter {
77
private BufferedWriter writer;
78
private LoggingTool logger;
79
private int moleculeNumber;
80
public Map sdFields=null;
81
//private boolean writeAromatic=true;
86
* Contructs a new MDLWriter that can write an array of
87
* Molecules to a Writer.
89
* @param out The Writer to write to
91
public MDLWriter(Writer out) {
92
logger = new LoggingTool(this);
94
if (out instanceof BufferedWriter) {
95
writer = (BufferedWriter)out;
97
writer = new BufferedWriter(out);
99
} catch (Exception exc) {
101
this.moleculeNumber = 1;
105
* Contructs a new MDLWriter that can write an array of
106
* Molecules to a given OutputStream.
108
* @param output The OutputStream to write to
110
public MDLWriter(OutputStream output) {
111
this(new OutputStreamWriter(output));
115
this(new StringWriter());
118
public IResourceFormat getFormat() {
119
return MDLFormat.getInstance();
122
public void setWriter(Writer out) throws CDKException {
123
if (out instanceof BufferedWriter) {
124
writer = (BufferedWriter)out;
126
writer = new BufferedWriter(out);
130
public void setWriter(OutputStream output) throws CDKException {
131
setWriter(new OutputStreamWriter(output));
136
* Method does not do anything until now.
139
public void dontWriteAromatic(){
140
//writeAromatic=false;
144
* Here you can set a map which will be used to build sd fields in the file.
145
* The entries will be translated to sd fields like this:<br>
146
* > <key><br>
150
* @param map The map to be used, map of String-String pairs
152
public void setSdFields(Map map){
157
* Flushes the output and closes this object.
159
public void close() throws IOException {
163
public boolean accepts(Class classObject) {
164
Class[] interfaces = classObject.getInterfaces();
165
for (int i=0; i<interfaces.length; i++) {
166
if (IMolecule.class.equals(interfaces[i])) return true;
167
if (IChemFile.class.equals(interfaces[i])) return true;
168
if (IChemModel.class.equals(interfaces[i])) return true;
169
if (IMoleculeSet.class.equals(interfaces[i])) return true;
175
* Writes a IChemObject to the MDL molfile formated output.
176
* It can only output ChemObjects of type ChemFile, Molecule and
179
* @param object class must be of type ChemFile, Molecule or MoleculeSet.
181
* @see org.openscience.cdk.ChemFile
183
public void write(IChemObject object) throws CDKException {
185
if (object instanceof IMoleculeSet) {
186
writeMoleculeSet((IMoleculeSet)object);
188
} else if (object instanceof IChemFile) {
189
writeChemFile((IChemFile)object);
191
} else if (object instanceof IChemModel) {
192
IChemFile file = object.getBuilder().newChemFile();
193
IChemSequence sequence = object.getBuilder().newChemSequence();
194
sequence.addChemModel((IChemModel)object);
195
file.addChemSequence(sequence);
196
writeChemFile((IChemFile)file);
198
} else if (object instanceof IMolecule) {
199
writeMolecule((IMolecule)object);
202
} catch (Exception ex) {
203
logger.error(ex.getMessage());
205
throw new CDKException("Exception while writing MDL file: " + ex.getMessage(), ex);
207
throw new CDKException("Only supported is writing of ChemFile, MoleculeSet, AtomContainer and Molecule objects.");
211
* Writes an array of Molecules to an OutputStream in MDL sdf format.
213
* @param som Array of Molecules that is written to an OutputStream
215
private void writeMoleculeSet(IMoleculeSet som)
217
java.util.Iterator molecules = som.molecules();
218
while (molecules.hasNext())
220
IMolecule mol = (IMolecule)molecules.next();
223
boolean[] isVisible=new boolean[mol.getAtomCount()];
224
for(int k=0;k<isVisible.length;k++){
229
catch (Exception exc)
235
private void writeChemFile(IChemFile file) throws Exception {
236
List moleculesList = ChemFileManipulator.getAllAtomContainers(file);
237
for (int i=0; i<moleculesList.size(); i++) {
238
writeMolecule(file.getBuilder().newMolecule((IAtomContainer)moleculesList.get(i)));
244
* Writes a Molecule to an OutputStream in MDL sdf format.
246
* @param container Molecule that is written to an OutputStream
248
public void writeMolecule(IMolecule container) throws Exception {
250
// taking care of the $$$$ signs:
251
// we do not write such a sign at the end of the first molecule, thus we have to write on BEFORE the second molecule
252
if(moleculeNumber == 2) {
253
writer.write("$$$$");
256
// write header block
257
// lines get shortened to 80 chars, that's in the spec
258
String title = (String)container.getProperty(CDKConstants.TITLE);
259
if (title == null) title = "";
260
if(title.length()>80)
261
title=title.substring(0,80);
262
writer.write(title + "\n");
265
* This line has the format:
266
* IIPPPPPPPPMMDDYYHHmmddSSssssssssssEEEEEEEEEEEERRRRRR
267
* (FORTRAN: A2<--A8--><---A10-->A2I2<--F10.5-><---F12.5--><-I6-> )
268
* User's first and last initials (l), program name (P),
269
* date/time (M/D/Y,H:m), dimensional codes (d), scaling factors (S, s),
270
* energy (E) if modeling program input, internal registry number (R)
271
* if input through MDL form.
272
* A blank line can be substituted for line 2.
274
writer.write(" CDK ");
275
writer.write(new SimpleDateFormat("M/d/y,H:m",Locale.US).format(
276
Calendar.getInstance(TimeZone.getDefault()).getTime())
280
String comment = (String)container.getProperty(CDKConstants.REMARK);
281
if (comment == null) comment = "";
282
if(comment.length()>80)
283
comment=comment.substring(0,80);
284
writer.write(comment + "\n");
287
line += formatMDLInt(container.getAtomCount(), 3);
288
line += formatMDLInt(container.getBondCount(), 3);
289
line += " 0 0 0 0 0 0 0 0999 V2000\n";
293
for (int f = 0; f < container.getAtomCount(); f++) {
294
IAtom atom = container.getAtom(f);
296
if (atom.getPoint3d() != null) {
297
line += formatMDLFloat((float) atom.getPoint3d().x);
298
line += formatMDLFloat((float) atom.getPoint3d().y);
299
line += formatMDLFloat((float) atom.getPoint3d().z) + " ";
300
} else if (atom.getPoint2d() != null) {
301
line += formatMDLFloat((float) atom.getPoint2d().x);
302
line += formatMDLFloat((float) atom.getPoint2d().y);
305
// if no coordinates available, then output a number
307
line += formatMDLFloat((float)0.0);
308
line += formatMDLFloat((float)0.0);
309
line += formatMDLFloat((float)0.0) + " ";
311
if(container.getAtom(f) instanceof IPseudoAtom)
312
line += formatMDLString(((IPseudoAtom) container.getAtom(f)).getLabel(), 3);
314
line += formatMDLString(container.getAtom(f).getSymbol(), 3);
315
line += " 0 0 0 0 0 0 0 0 0 0 0 0";
321
Iterator bonds = container.bonds();
322
while (bonds.hasNext()) {
323
IBond bond = (IBond) bonds.next();
325
if (bond.getAtomCount() != 2) {
326
logger.warn("Skipping bond with more/less than two atoms: " + bond);
328
if (bond.getStereo() == CDKConstants.STEREO_BOND_UP_INV ||
329
bond.getStereo() == CDKConstants.STEREO_BOND_DOWN_INV) {
330
// turn around atom coding to correct for inv stereo
331
line = formatMDLInt(container.getAtomNumber(bond.getAtom(1)) + 1,3);
332
line += formatMDLInt(container.getAtomNumber(bond.getAtom(0)) + 1,3);
334
line = formatMDLInt(container.getAtomNumber(bond.getAtom(0)) + 1,3);
335
line += formatMDLInt(container.getAtomNumber(bond.getAtom(1)) + 1,3);
337
line += formatMDLInt((int)bond.getOrder(),3);
339
switch(bond.getStereo()){
340
case CDKConstants.STEREO_BOND_UP:
343
case CDKConstants.STEREO_BOND_UP_INV:
346
case CDKConstants.STEREO_BOND_DOWN:
349
case CDKConstants.STEREO_BOND_DOWN_INV:
361
// write formal atomic charges
362
for (int i = 0; i < container.getAtomCount(); i++) {
363
IAtom atom = container.getAtom(i);
364
int charge = atom.getFormalCharge();
366
writer.write("M CHG 1 ");
367
writer.write(formatMDLInt(i+1,3));
369
writer.write(formatMDLInt(charge,3));
374
// write formal isotope information
375
for (int i = 0; i < container.getAtomCount(); i++) {
376
IAtom atom = container.getAtom(i);
377
if (!(atom instanceof IPseudoAtom)) {
378
int atomicMass = atom.getMassNumber();
379
int majorMass = IsotopeFactory.getInstance(atom.getBuilder()).getMajorIsotope(atom.getSymbol()).getMassNumber();
380
if (atomicMass != 0 && atomicMass != majorMass) {
381
writer.write("M ISO 1 ");
382
writer.write(formatMDLInt(i+1,3));
384
writer.write(formatMDLInt(atomicMass,3));
391
writer.write("M END");
393
//write sdfields, if any
395
Set set = sdFields.keySet();
396
Iterator iterator = set.iterator();
397
while (iterator.hasNext()) {
398
Object element = iterator.next();
399
writer.write("> <"+(String)element+">");
401
writer.write(sdFields.get(element).toString());
406
// taking care of the $$$$ signs:
407
// we write such a sign at the end of all except the first molecule
408
if(moleculeNumber != 1) {
409
writer.write("$$$$");
417
* Formats an int to fit into the connectiontable and changes it
420
* @param i The int to be formated
421
* @param l Length of the String
422
* @return The String to be written into the connectiontable
424
private String formatMDLInt(int i, int l) {
425
String s = "", fs = "";
426
NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
427
nf.setParseIntegerOnly(true);
428
nf.setMinimumIntegerDigits(1);
429
nf.setMaximumIntegerDigits(l);
430
nf.setGroupingUsed(false);
433
for (int f = 0; f < l; f++)
443
* Formats a float to fit into the connectiontable and changes it
446
* @param fl The float to be formated
447
* @return The String to be written into the connectiontable
449
private String formatMDLFloat(float fl) {
450
String s = "", fs = "";
452
NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
453
nf.setMinimumIntegerDigits(1);
454
nf.setMaximumIntegerDigits(4);
455
nf.setMinimumFractionDigits(4);
456
nf.setMaximumFractionDigits(4);
457
nf.setGroupingUsed(false);
460
for (int f = 0; f < l; f++)
469
* Formats a String to fit into the connectiontable.
471
* @param s The String to be formated
472
* @param le The length of the String
473
* @return The String to be written in the connectiontable
475
private String formatMDLString(String s, int le) {
478
return s.substring(0, le);
481
for (int f = 0; f < l; f++)