1
/* $Revision: 8719 $ $Author: egonw $ $Date: 2007-08-28 10:29:51 +0200 (Tue, 28 Aug 2007) $
3
* Copyright (C) 2005-2007 Egon Willighagen <egonw@users.sf.net>
5
* Contact: cdk-devel@lists.sourceforge.net
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
package org.openscience.cdk.io;
23
import org.openscience.cdk.CDKConstants;
24
import org.openscience.cdk.exception.CDKException;
25
import org.openscience.cdk.interfaces.IAtom;
26
import org.openscience.cdk.interfaces.IBond;
27
import org.openscience.cdk.interfaces.IChemObject;
28
import org.openscience.cdk.interfaces.IMolecule;
29
import org.openscience.cdk.io.formats.IResourceFormat;
30
import org.openscience.cdk.io.formats.Mol2Format;
31
import org.openscience.cdk.tools.LoggingTool;
34
import java.util.Iterator;
37
* An output Writer that writes molecular data into the
38
* <a href="http://www.tripos.com/data/support/mol2.pdf">Tripos Mol2 format</a>.
39
* Writes the atoms and the bonds only at this moment.
43
* @author Egon Willighagen
45
public class Mol2Writer extends DefaultChemObjectWriter {
47
private BufferedWriter writer;
48
private LoggingTool logger;
51
this(new StringWriter());
55
* Constructs a new Mol2 writer.
56
* @param out the stream to write the Mol2 file to.
58
public Mol2Writer(Writer out) {
59
logger = new LoggingTool(this);
61
if (out instanceof BufferedWriter) {
62
writer = (BufferedWriter)out;
64
writer = new BufferedWriter(out);
66
} catch (Exception exc) {
70
public Mol2Writer(OutputStream output) {
71
this(new OutputStreamWriter(output));
74
public IResourceFormat getFormat() {
75
return Mol2Format.getInstance();
78
public void setWriter(Writer out) throws CDKException {
79
if (out instanceof BufferedWriter) {
80
writer = (BufferedWriter)out;
82
writer = new BufferedWriter(out);
86
public void setWriter(OutputStream output) throws CDKException {
87
setWriter(new OutputStreamWriter(output));
91
* Flushes the output and closes this object.
93
public void close() throws IOException {
97
public boolean accepts(Class classObject) {
98
Class[] interfaces = classObject.getInterfaces();
99
for (int i=0; i<interfaces.length; i++) {
100
if (IMolecule.class.equals(interfaces[i])) return true;
105
public void write(IChemObject object) throws CDKException {
106
if (object instanceof IMolecule) {
108
writeMolecule((IMolecule)object);
109
} catch(Exception ex) {
110
throw new CDKException("Error while writing Mol2 file: " + ex.getMessage(), ex);
113
throw new CDKException("Mol2Writer only supports output of Molecule classes.");
118
* Writes a single frame in XYZ format to the Writer.
120
* @param mol the Molecule to write
122
public void writeMolecule(IMolecule mol) throws IOException {
128
# Creating user name: tom
129
# Creation time: Wed Dec 28 00:18:30 1988
131
# Modifying user name: tom
132
# Modification time: Wed Dec 28 00:18:30 1988
135
logger.debug("Writing header...");
136
if (mol.getProperty(CDKConstants.TITLE) != null) {
137
writer.write("# Name: " + mol.getProperty(CDKConstants.TITLE) + "\n");
139
// FIXME: add other types of meta data
150
logger.debug("Writing molecule block...");
151
writer.write("@<TRIPOS>MOLECULE\n");
152
writer.write(mol.getID() + "\n");
153
writer.write(mol.getAtomCount() + " " +
155
"\n"); // that's the minimum amount of info required the format
156
writer.write("SMALL\n"); // no biopolymer
157
writer.write("NO CHARGES\n"); // other options include Gasteiger charges
161
1 C1 1.207 2.091 0.000 C.ar 1 BENZENE 0.000
162
2 C2 2.414 1.394 0.000 C.ar 1 BENZENE 0.000
163
3 C3 2.414 0.000 0.000 C.ar 1 BENZENE 0.000
164
4 C4 1.207 -0.697 0.000 C.ar 1 BENZENE 0.000
165
5 C5 0.000 0.000 0.000 C.ar 1 BENZENE 0.000
166
6 C6 0.000 1.394 0.000 C.ar 1 BENZENE 0.000
167
7 H1 1.207 3.175 0.000 H 1 BENZENE 0.000
168
8 H2 3.353 1.936 0.000 H 1 BENZENE 0.000
169
9 H3 3.353 -0.542 0.000 H 1 BENZENE 0.000
170
10 H4 1.207 -1.781 0.000 H 1 BENZENE 0.000
171
11 H5 -0.939 -0.542 0.000 H 1 BENZENE 0.000
172
12 H6 -0.939 1.936 0.000 H 1 BENZENE 0.000
176
logger.debug("Writing atom block...");
177
writer.write("@<TRIPOS>ATOM\n");
178
for (int i = 0; i < mol.getAtomCount(); i++) {
179
IAtom atom = mol.getAtom(i);
180
writer.write(i + " " +
182
if (atom.getPoint3d() != null) {
183
writer.write(atom.getPoint3d().x + " ");
184
writer.write(atom.getPoint3d().y + " ");
185
writer.write(atom.getPoint3d().z + " ");
186
} else if (atom.getPoint2d() != null) {
187
writer.write(atom.getPoint2d().x + " ");
188
writer.write(atom.getPoint2d().y + " ");
189
writer.write(" 0.000 ");
191
writer.write("0.000 0.000 0.000 ");
193
writer.write(atom.getSymbol()+ "\n"); // FIXME: should use perceived Mol2 Atom Types!
213
logger.debug("Writing bond block...");
214
writer.write("@<TRIPOS>BOND\n");
217
Iterator bonds = mol.bonds();
218
while (bonds.hasNext()) {
219
IBond bond = (IBond) bonds.next();
220
writer.write(counter + " " +
221
mol.getAtomNumber(bond.getAtom(0)) + " " +
222
mol.getAtomNumber(bond.getAtom(1)) + " " +
223
((int)bond.getOrder()) +
228
} catch (IOException e) {