3
* $Date: 2007-03-14 18:12:43 +0100 (Wed, 14 Mar 2007) $
6
* Copyright (C) 2002-2003 The Jmol Development Team
7
* Copyright (C) 2003-2007 The Chemistry Development Kit (CDK) project
9
* Contact: cdk-devel@lists.sourceforge.net
11
* This library is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public
13
* License as published by the Free Software Foundation; either
14
* version 2.1 of the License, or (at your option) any later version.
16
* This library is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* Lesser General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public
22
* License along with this library; if not, write to the Free Software
23
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25
package org.openscience.cdk.io;
27
import java.io.BufferedReader;
28
import java.io.InputStream;
29
import java.io.InputStreamReader;
30
import java.io.Reader;
31
import java.io.StringReader;
32
import java.util.StringTokenizer;
34
import javax.vecmath.Point3d;
36
import org.openscience.cdk.config.IsotopeFactory;
37
import org.openscience.cdk.exception.CDKException;
38
import org.openscience.cdk.interfaces.IAtom;
39
import org.openscience.cdk.interfaces.IAtomContainer;
40
import org.openscience.cdk.interfaces.IChemFile;
41
import org.openscience.cdk.interfaces.IChemModel;
42
import org.openscience.cdk.interfaces.IChemObject;
43
import org.openscience.cdk.interfaces.IChemSequence;
44
import org.openscience.cdk.interfaces.IMoleculeSet;
45
import org.openscience.cdk.io.formats.GhemicalMMFormat;
46
import org.openscience.cdk.io.formats.IResourceFormat;
47
import org.openscience.cdk.tools.LoggingTool;
50
* Reads Ghemical (<a href="http://www.uku.fi/~thassine/ghemical/">
51
* http://www.uku.fi/~thassine/ghemical/</a>)
52
* molecular mechanics (*.mm1gp) files.
56
* @author Egon Willighagen <egonw@sci.kun.nl>
58
public class GhemicalMMReader extends DefaultChemObjectReader {
60
private LoggingTool logger = null;
61
private BufferedReader input = null;
63
public GhemicalMMReader(Reader input) {
64
this.logger = new LoggingTool(this);
65
this.input = new BufferedReader(input);
68
public GhemicalMMReader(InputStream input) {
69
this(new InputStreamReader(input));
72
public GhemicalMMReader() {
73
this(new StringReader(""));
76
public IResourceFormat getFormat() {
77
return GhemicalMMFormat.getInstance();
80
public void setReader(Reader input) throws CDKException {
81
if (input instanceof BufferedReader) {
82
this.input = (BufferedReader)input;
84
this.input = new BufferedReader(input);
88
public void setReader(InputStream input) throws CDKException {
89
setReader(new InputStreamReader(input));
95
public boolean accepts(Class classObject) {
96
Class[] interfaces = classObject.getInterfaces();
97
for (int i=0; i<interfaces.length; i++) {
98
if (IChemModel.class.equals(interfaces[i])) return true;
99
if (IChemFile.class.equals(interfaces[i])) return true;
104
public IChemObject read(IChemObject object) throws CDKException {
105
if (object instanceof IChemModel) {
106
return (IChemObject) readChemModel((IChemModel)object);
107
} else if (object instanceof IChemFile) {
108
IChemSequence sequence = object.getBuilder().newChemSequence();
109
sequence.addChemModel((IChemModel)this.read(object.getBuilder().newChemModel()));
110
((IChemFile)object).addChemSequence(sequence);
113
throw new CDKException("Only supported is ChemModel.");
117
private IChemModel readChemModel(IChemModel model) throws CDKException {
118
int[] atoms = new int[1];
119
double[] atomxs = new double[1];
120
double[] atomys = new double[1];
121
double[] atomzs = new double[1];
122
double[] atomcharges = new double[1];
124
int[] bondatomid1 = new int[1];
125
int[] bondatomid2 = new int[1];
126
int[] bondorder = new int[1];
128
int numberOfAtoms = 0;
129
int numberOfBonds = 0;
132
String line = input.readLine();
133
while (line != null) {
134
StringTokenizer st = new StringTokenizer(line);
135
String command = st.nextToken();
136
if ("!Header".equals(command)) {
137
logger.warn("Ignoring header");
138
} else if ("!Info".equals(command)) {
139
logger.warn("Ignoring info");
140
} else if ("!Atoms".equals(command)) {
141
logger.info("Reading atom block");
142
// determine number of atoms to read
144
numberOfAtoms = Integer.parseInt(st.nextToken());
145
logger.debug(" #atoms: " + numberOfAtoms);
146
atoms = new int[numberOfAtoms];
147
atomxs = new double[numberOfAtoms];
148
atomys = new double[numberOfAtoms];
149
atomzs = new double[numberOfAtoms];
150
atomcharges = new double[numberOfAtoms];
152
for (int i = 0; i < numberOfAtoms; i++) {
153
line = input.readLine();
154
StringTokenizer atomInfoFields = new StringTokenizer(line);
155
int atomID = Integer.parseInt(atomInfoFields.nextToken());
156
atoms[atomID] = Integer.parseInt(atomInfoFields.nextToken());
157
logger.debug("Set atomic number of atom (" + atomID + ") to: " + atoms[atomID]);
159
} catch (Exception exception) {
160
logger.error("Error while reading Atoms block");
161
logger.debug(exception);
163
} else if ("!Bonds".equals(command)) {
164
logger.info("Reading bond block");
166
// determine number of bonds to read
167
numberOfBonds = Integer.parseInt(st.nextToken());
168
bondatomid1 = new int[numberOfAtoms];
169
bondatomid2 = new int[numberOfAtoms];
170
bondorder = new int[numberOfAtoms];
172
for (int i = 0; i < numberOfBonds; i++) {
173
line = input.readLine();
174
StringTokenizer bondInfoFields = new StringTokenizer(line);
175
bondatomid1[i] = Integer.parseInt(bondInfoFields.nextToken());
176
bondatomid2[i] = Integer.parseInt(bondInfoFields.nextToken());
177
String order = bondInfoFields.nextToken();
178
if ("D".equals(order)) {
180
} else if ("S".equals(order)) {
182
} else if ("T".equals(order)) {
185
// ignore order, i.e. set to single
186
logger.warn("Unrecognized bond order, using single bond instead. Found: " + order);
190
} catch (Exception exception) {
191
logger.error("Error while reading Bonds block");
192
logger.debug(exception);
194
} else if ("!Coord".equals(command)) {
195
logger.info("Reading coordinate block");
197
for (int i = 0; i < numberOfAtoms; i++) {
198
line = input.readLine();
199
StringTokenizer atomInfoFields = new StringTokenizer(line);
200
int atomID = Integer.parseInt(atomInfoFields.nextToken());
201
double x = Double.valueOf(atomInfoFields.nextToken()).doubleValue();
202
double y = Double.valueOf(atomInfoFields.nextToken()).doubleValue();
203
double z = Double.valueOf(atomInfoFields.nextToken()).doubleValue();
208
} catch (Exception exception) {
209
logger.error("Error while reading Coord block");
210
logger.debug(exception);
212
} else if ("!Charges".equals(command)) {
213
logger.info("Reading charges block");
215
for (int i = 0; i < numberOfAtoms; i++) {
216
line = input.readLine();
217
StringTokenizer atomInfoFields = new StringTokenizer(line);
218
int atomID = Integer.parseInt(atomInfoFields.nextToken());
219
double charge = Double.valueOf(atomInfoFields.nextToken()).doubleValue();
220
atomcharges[atomID] = charge;
222
} catch (Exception exception) {
223
logger.error("Error while reading Charges block");
224
logger.debug(exception);
226
} else if ("!End".equals(command)) {
227
logger.info("Found end of file");
229
IAtomContainer container = model.getBuilder().newAtomContainer();
230
for (int i = 0; i < numberOfAtoms; i++) {
232
IAtom atom = model.getBuilder().newAtom(IsotopeFactory.getInstance(container.getBuilder()).getElementSymbol(atoms[i]));
233
atom.setAtomicNumber(atoms[i]);
234
atom.setPoint3d(new Point3d(atomxs[i], atomys[i], atomzs[i]));
235
atom.setCharge(atomcharges[i]);
236
container.addAtom(atom);
237
logger.debug("Stored atom: " + atom);
238
} catch (Exception exception) {
239
logger.error("Cannot create an atom with atomic number: " + atoms[i]);
240
logger.debug(exception);
245
for (int i = 0; i < numberOfBonds; i++) {
246
container.addBond(bondatomid1[i], bondatomid2[i], bondorder[i]);
249
IMoleculeSet moleculeSet = model.getBuilder().newMoleculeSet();
250
moleculeSet.addMolecule(model.getBuilder().newMolecule(container));
251
model.setMoleculeSet(moleculeSet);
255
logger.warn("Skipping line: " + line);
258
line = input.readLine();
260
} catch (Exception exception) {
261
logger.error("Error while reading file");
262
logger.debug(exception);
265
// this should not happen, file is lacking !End command