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

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/io/GhemicalMMReader.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
/* $RCSfile$
 
2
 * $Author: egonw $
 
3
 * $Date: 2007-03-14 18:12:43 +0100 (Wed, 14 Mar 2007) $
 
4
 * $Revision: 8118 $
 
5
 *
 
6
 * Copyright (C) 2002-2003  The Jmol Development Team
 
7
 * Copyright (C) 2003-2007  The Chemistry Development Kit (CDK) project
 
8
 *
 
9
 * Contact: cdk-devel@lists.sourceforge.net
 
10
 *
 
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.
 
15
 *
 
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.
 
20
 *
 
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.
 
24
 */
 
25
package org.openscience.cdk.io;
 
26
 
 
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;
 
33
 
 
34
import javax.vecmath.Point3d;
 
35
 
 
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;
 
48
 
 
49
/**
 
50
 * Reads Ghemical (<a href="http://www.uku.fi/~thassine/ghemical/">
 
51
 * http://www.uku.fi/~thassine/ghemical/</a>)
 
52
 * molecular mechanics (*.mm1gp) files.
 
53
 *
 
54
 * @cdk.module io
 
55
 *
 
56
 * @author Egon Willighagen <egonw@sci.kun.nl>
 
57
 */
 
58
public class GhemicalMMReader extends DefaultChemObjectReader {
 
59
 
 
60
    private LoggingTool logger = null;
 
61
    private BufferedReader input = null;
 
62
 
 
63
    public GhemicalMMReader(Reader input) {
 
64
        this.logger = new LoggingTool(this);
 
65
        this.input = new BufferedReader(input);
 
66
    }
 
67
 
 
68
    public GhemicalMMReader(InputStream input) {
 
69
        this(new InputStreamReader(input));
 
70
    }
 
71
    
 
72
    public GhemicalMMReader() {
 
73
        this(new StringReader(""));
 
74
    }
 
75
 
 
76
    public IResourceFormat getFormat() {
 
77
        return GhemicalMMFormat.getInstance();
 
78
    }
 
79
 
 
80
    public void setReader(Reader input) throws CDKException {
 
81
        if (input instanceof BufferedReader) {
 
82
            this.input = (BufferedReader)input;
 
83
        } else {
 
84
            this.input = new BufferedReader(input);
 
85
        }
 
86
    }
 
87
 
 
88
    public void setReader(InputStream input) throws CDKException {
 
89
        setReader(new InputStreamReader(input));
 
90
    }
 
91
 
 
92
    public void close() {
 
93
    }
 
94
    
 
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;
 
100
                }
 
101
                return false;
 
102
        }
 
103
 
 
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);
 
111
                return object;
 
112
        } else {
 
113
            throw new CDKException("Only supported is ChemModel.");
 
114
        }
 
115
    }
 
116
    
 
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];
 
123
        
 
124
        int[] bondatomid1 = new int[1];
 
125
        int[] bondatomid2 = new int[1];
 
126
        int[] bondorder = new int[1];
 
127
        
 
128
        int numberOfAtoms = 0;
 
129
        int numberOfBonds = 0;
 
130
        
 
131
        try {
 
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
 
143
                    try {
 
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];
 
151
                        
 
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]);
 
158
                        }
 
159
                    } catch (Exception exception) {
 
160
                        logger.error("Error while reading Atoms block");
 
161
                        logger.debug(exception);
 
162
                    }
 
163
                } else if ("!Bonds".equals(command)) {
 
164
                    logger.info("Reading bond block");
 
165
                    try {
 
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];
 
171
                        
 
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)) {
 
179
                                bondorder[i] = 2;
 
180
                            } else if ("S".equals(order)) {
 
181
                                bondorder[i] = 1;
 
182
                            } else if ("T".equals(order)) {
 
183
                                bondorder[i] = 3;
 
184
                            } else {
 
185
                                // ignore order, i.e. set to single
 
186
                                logger.warn("Unrecognized bond order, using single bond instead. Found: " + order);
 
187
                                bondorder[i] = 1;
 
188
                            }
 
189
                        }
 
190
                    } catch (Exception exception) {
 
191
                        logger.error("Error while reading Bonds block");
 
192
                        logger.debug(exception);
 
193
                    }
 
194
                } else if ("!Coord".equals(command)) {
 
195
                    logger.info("Reading coordinate block");
 
196
                    try {
 
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();
 
204
                            atomxs[atomID] = x;
 
205
                            atomys[atomID] = y;
 
206
                            atomzs[atomID] = z;
 
207
                        }
 
208
                    } catch (Exception exception) {
 
209
                        logger.error("Error while reading Coord block");
 
210
                        logger.debug(exception);
 
211
                    }
 
212
                } else if ("!Charges".equals(command)) {
 
213
                    logger.info("Reading charges block");
 
214
                    try {
 
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;
 
221
                        }
 
222
                    } catch (Exception exception) {
 
223
                        logger.error("Error while reading Charges block");
 
224
                        logger.debug(exception);
 
225
                    }
 
226
                } else if ("!End".equals(command)) {
 
227
                    logger.info("Found end of file");
 
228
                    // Store atoms
 
229
                    IAtomContainer container = model.getBuilder().newAtomContainer();
 
230
                    for (int i = 0; i < numberOfAtoms; i++) {
 
231
                        try {
 
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);
 
241
                        }
 
242
                    }
 
243
                    
 
244
                    // Store bonds
 
245
                    for (int i = 0; i < numberOfBonds; i++) {
 
246
                        container.addBond(bondatomid1[i], bondatomid2[i], bondorder[i]);
 
247
                    }
 
248
                    
 
249
                    IMoleculeSet moleculeSet = model.getBuilder().newMoleculeSet();
 
250
                    moleculeSet.addMolecule(model.getBuilder().newMolecule(container));
 
251
                    model.setMoleculeSet(moleculeSet);
 
252
                    
 
253
                    return model;
 
254
                } else {
 
255
                    logger.warn("Skipping line: " + line);
 
256
                }
 
257
                
 
258
                line = input.readLine();
 
259
            }
 
260
        } catch (Exception exception) {
 
261
            logger.error("Error while reading file");
 
262
            logger.debug(exception);
 
263
        }
 
264
        
 
265
        // this should not happen, file is lacking !End command
 
266
        return null;
 
267
        
 
268
    }
 
269
}