~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/io/Mol2Writer.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Revision: 8719 $ $Author: egonw $ $Date: 2007-08-28 10:29:51 +0200 (Tue, 28 Aug 2007) $
 
2
 * 
 
3
 * Copyright (C) 2005-2007  Egon Willighagen <egonw@users.sf.net>
 
4
 * 
 
5
 * Contact: cdk-devel@lists.sourceforge.net
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 */
 
21
package org.openscience.cdk.io;
 
22
 
 
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;
 
32
 
 
33
import java.io.*;
 
34
import java.util.Iterator;
 
35
 
 
36
/**
 
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.
 
40
 *
 
41
 * @cdk.module io
 
42
 *
 
43
 * @author     Egon Willighagen
 
44
 */
 
45
public class Mol2Writer extends DefaultChemObjectWriter {
 
46
 
 
47
    private BufferedWriter writer;
 
48
        private LoggingTool logger;
 
49
    
 
50
    public Mol2Writer() {
 
51
        this(new StringWriter());
 
52
    }
 
53
 
 
54
    /**
 
55
     * Constructs a new Mol2 writer.
 
56
     * @param out the stream to write the Mol2 file to.
 
57
     */
 
58
    public Mol2Writer(Writer out) {
 
59
        logger = new LoggingTool(this);
 
60
        try {
 
61
                if (out instanceof BufferedWriter) {
 
62
                writer = (BufferedWriter)out;
 
63
            } else {
 
64
                writer = new BufferedWriter(out);
 
65
            }
 
66
        } catch (Exception exc) {
 
67
        }
 
68
    }
 
69
 
 
70
    public Mol2Writer(OutputStream output) {
 
71
        this(new OutputStreamWriter(output));
 
72
    }
 
73
 
 
74
    public IResourceFormat getFormat() {
 
75
        return Mol2Format.getInstance();
 
76
    }
 
77
    
 
78
    public void setWriter(Writer out) throws CDKException {
 
79
        if (out instanceof BufferedWriter) {
 
80
            writer = (BufferedWriter)out;
 
81
        } else {
 
82
            writer = new BufferedWriter(out);
 
83
        }
 
84
    }
 
85
 
 
86
    public void setWriter(OutputStream output) throws CDKException {
 
87
        setWriter(new OutputStreamWriter(output));
 
88
    }
 
89
 
 
90
    /**
 
91
     * Flushes the output and closes this object.
 
92
     */
 
93
    public void close() throws IOException {
 
94
        writer.close();
 
95
    }
 
96
 
 
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;
 
101
                }
 
102
                return false;
 
103
        }
 
104
 
 
105
    public void write(IChemObject object) throws CDKException {
 
106
        if (object instanceof IMolecule) {
 
107
            try {
 
108
                writeMolecule((IMolecule)object);
 
109
            } catch(Exception ex) {
 
110
                throw new CDKException("Error while writing Mol2 file: " + ex.getMessage(), ex);
 
111
            }
 
112
        } else {
 
113
            throw new CDKException("Mol2Writer only supports output of Molecule classes.");
 
114
        }
 
115
    }
 
116
 
 
117
    /**
 
118
     * Writes a single frame in XYZ format to the Writer.
 
119
     *
 
120
     * @param mol the Molecule to write
 
121
     */
 
122
    public void writeMolecule(IMolecule mol) throws IOException {
 
123
 
 
124
        try {
 
125
 
 
126
/*
 
127
#        Name: benzene 
 
128
#        Creating user name: tom 
 
129
#        Creation time: Wed Dec 28 00:18:30 1988 
 
130
 
 
131
#        Modifying user name: tom 
 
132
#        Modification time: Wed Dec 28 00:18:30 1988
 
133
*/
 
134
 
 
135
                logger.debug("Writing header...");
 
136
            if (mol.getProperty(CDKConstants.TITLE) != null) {
 
137
                writer.write("#        Name: " + mol.getProperty(CDKConstants.TITLE) + "\n");
 
138
            }
 
139
            // FIXME: add other types of meta data
 
140
            writer.newLine();
 
141
 
 
142
/*
 
143
@<TRIPOS>MOLECULE 
 
144
benzene 
 
145
12 12 1  0       0 
 
146
SMALL 
 
147
NO_CHARGES 
 
148
*/
 
149
 
 
150
            logger.debug("Writing molecule block...");
 
151
            writer.write("@<TRIPOS>MOLECULE\n");
 
152
            writer.write(mol.getID() + "\n");
 
153
            writer.write(mol.getAtomCount() + " " + 
 
154
                        mol.getBondCount() +
 
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
 
158
 
 
159
/*
 
160
@<TRIPOS>ATOM 
 
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 
 
173
*/
 
174
 
 
175
            // write atom block
 
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 + " " +
 
181
                             atom.getID() + " ");
 
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 ");
 
190
                } else {
 
191
                    writer.write("0.000 0.000 0.000 ");
 
192
                }
 
193
                writer.write(atom.getSymbol()+ "\n"); // FIXME: should use perceived Mol2 Atom Types!
 
194
            }
 
195
 
 
196
/*
 
197
@<TRIPOS>BOND 
 
198
1       1       2       ar 
 
199
2       1       6       ar 
 
200
3       2       3       ar 
 
201
4       3       4       ar 
 
202
5       4       5       ar 
 
203
6       5       6       ar 
 
204
7       1       7       1 
 
205
8       2       8       1 
 
206
9       3       9       1 
 
207
10      4       10      1 
 
208
11      5       11      1 
 
209
12      6       12      1
 
210
*/
 
211
 
 
212
            // write bond block
 
213
            logger.debug("Writing bond block...");
 
214
            writer.write("@<TRIPOS>BOND\n");
 
215
 
 
216
            int counter = 0;
 
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()) +
 
224
                             "\n");
 
225
                counter++;
 
226
            } 
 
227
 
 
228
        } catch (IOException e) {
 
229
            throw e;
 
230
        }
 
231
    }
 
232
}
 
233
 
 
234