Source code: joelib/io/types/Tinker.java
1 /**
2 * Filename: $RCSfile: Tinker.java,v $
3 * Purpose: Atom representation.
4 * Language: Java
5 * Compiler: JDK 1.4
6 * Authors: Joerg K. Wegner
7 * Version: $Revision: 1.15 $
8 * $Date: 2003/08/22 15:56:18 $
9 * $Author: wegner $
10 * Original Author: ???, OpenEye Scientific Software
11 * Original Version: babel 2.0a1
12 *
13 * Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation version 2 of the License.
18 *
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 General Public License for more details.
23 */
24 package joelib.io.types;
25
26 import cformat.PrintfFormat;
27 import cformat.PrintfStream;
28
29 import joelib.data.JOEElementTable;
30 import joelib.data.JOETypeTable;
31
32 import joelib.io.MoleculeFileType;
33
34 import joelib.molecule.JOEAtom;
35 import joelib.molecule.JOEBond;
36 import joelib.molecule.JOEMol;
37
38 import joelib.util.iterator.AtomIterator;
39 import joelib.util.iterator.BondIterator;
40
41 /*==========================================================================*
42 * IMPORTS
43 *========================================================================== */
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.InputStreamReader;
47 import java.io.LineNumberReader;
48 import java.io.OutputStream;
49
50 import org.apache.log4j.Category;
51
52
53 /*==========================================================================*
54 * CLASS DECLARATION
55 *========================================================================== */
56
57 /**
58 * Reader/Writer for Tinker files.
59 *
60 * @author wegnerj
61 * @license GPL
62 * @cvsversion $Revision: 1.15 $, $Date: 2003/08/22 15:56:18 $
63 */
64 public class Tinker implements MoleculeFileType
65 {
66 //~ Static fields/initializers /////////////////////////////////////////////
67
68 /*-------------------------------------------------------------------------*
69 * private static member variables
70 *------------------------------------------------------------------------- */
71
72 /**
73 * Obtain a suitable logger.
74 */
75 private static Category logger = Category.getInstance(
76 "joelib.io.types.Tinker");
77 private final static String description = new String("Tinker XYZ");
78 private final static String[] extensions = new String[]{"txyz"};
79
80 //~ Instance fields ////////////////////////////////////////////////////////
81
82 private LineNumberReader lnr;
83 private PrintfFormat d5 = new PrintfFormat("%5d");
84
85 /*-------------------------------------------------------------------------*
86 * private member variables
87 *------------------------------------------------------------------------- */
88 private PrintfFormat d6 = new PrintfFormat("%6d");
89 private PrintfFormat f12_6 = new PrintfFormat("%12.6f");
90 private PrintfFormat s2 = new PrintfFormat("%2s");
91 private PrintfFormat s20 = new PrintfFormat("%-20s");
92 private PrintfStream ps;
93 private boolean forceUnixStyle = true;
94
95 //~ Constructors ///////////////////////////////////////////////////////////
96
97 /*-------------------------------------------------------------------------*
98 * constructor
99 *------------------------------------------------------------------------- */
100 public Tinker()
101 {
102 }
103
104 //~ Methods ////////////////////////////////////////////////////////////////
105
106 public void setUseUnixStyle(boolean _flag)
107 {
108 forceUnixStyle = _flag;
109 }
110
111 public boolean getUseUnixStyle()
112 {
113 return forceUnixStyle;
114 }
115
116 public void closeReader() throws IOException
117 {
118 lnr.close();
119 }
120
121 public void closeWriter() throws IOException
122 {
123 ps.close();
124 }
125
126 /**
127 * Description of the Method
128 *
129 * @param is Description of the Parameter
130 * @exception IOException Description of the Exception
131 */
132 public void initReader(InputStream is) throws IOException
133 {
134 lnr = new LineNumberReader(new InputStreamReader(is));
135 }
136
137 /**
138 * Description of the Method
139 *
140 * @param os Description of the Parameter
141 * @exception IOException Description of the Exception
142 */
143 public void initWriter(OutputStream os) throws IOException
144 {
145 ps = new PrintfStream(os);
146 }
147
148 /*-------------------------------------------------------------------------*
149 * public static methods
150 *------------------------------------------------------------------------- */
151
152 /**
153 * Description of the Method
154 *
155 * @return Description of the Return Value
156 */
157 public String inputDescription()
158 {
159 return null;
160 }
161
162 /**
163 * Description of the Method
164 *
165 * @return Description of the Return Value
166 */
167 public String[] inputFileExtensions()
168 {
169 return null;
170 }
171
172 /**
173 * Description of the Method
174 *
175 * @return Description of the Return Value
176 */
177 public String outputDescription()
178 {
179 return description;
180 }
181
182 /**
183 * Description of the Method
184 *
185 * @return Description of the Return Value
186 */
187 public String[] outputFileExtensions()
188 {
189 return extensions;
190 }
191
192 /**
193 * Reads an molecule entry as (unparsed) <tt>String</tt> representation.
194 *
195 * @return <tt>null</tt> if the reader contains no more
196 * relevant data. Otherwise the <tt>String</tt> representation of the
197 * whole molecule entry is returned.
198 * @exception IOException typical IOException
199 */
200 public String read() throws IOException
201 {
202 logger.error(
203 "Reading Tinker data as String representation is not implemented yet !!!");
204
205 return null;
206 }
207
208 /**
209 * Description of the Method
210 *
211 * @param mol Description of the Parameter
212 * @return Description of the Return Value
213 * @exception IOException Description of the Exception
214 */
215 public boolean read(JOEMol mol) throws IOException
216 {
217 return read(mol, "Undefined");
218 }
219
220 /**
221 * Description of the Method
222 *
223 * @param mol Description of the Parameter
224 * @param title Description of the Parameter
225 * @return Description of the Return Value
226 * @exception IOException Description of the Exception
227 */
228 public synchronized boolean read(JOEMol mol, String title)
229 throws IOException
230 {
231 return false;
232 }
233
234 /**
235 * Description of the Method
236 *
237 * @return Description of the Return Value
238 */
239 public boolean readable()
240 {
241 return false;
242 }
243
244 public boolean skipReaderEntry() throws IOException
245 {
246 return true;
247 }
248
249 /**
250 * Description of the Method
251 *
252 * @param mol Description of the Parameter
253 * @return Description of the Return Value
254 * @exception IOException Description of the Exception
255 */
256 public synchronized boolean write(JOEMol mol) throws IOException
257 {
258 return write(mol, "Undefined");
259 }
260
261 /**
262 * Description of the Method
263 *
264 * @param mol Description of the Parameter
265 * @param title Description of the Parameter
266 * @return Description of the Return Value
267 * @exception IOException Description of the Exception
268 */
269 public synchronized boolean write(JOEMol mol, String title)
270 throws IOException
271 {
272 ps.printf(d6, mol.numAtoms());
273 ps.print(' ');
274 ps.printf(s20, mol.getTitle());
275
276 if (forceUnixStyle)
277 {
278 ps.print('\n');
279 }
280 else
281 {
282 ps.println();
283 }
284
285 JOETypeTable ttab = JOETypeTable.instance();
286 ttab.setFromType("INT");
287 ttab.setToType("MM2");
288
289 JOEAtom atom;
290 AtomIterator ait = mol.atomIterator();
291 String str;
292 String str1;
293 int index = 1;
294
295 while (ait.hasNext())
296 {
297 atom = ait.nextAtom();
298 str = atom.getType();
299 str1 = ttab.translate(str);
300 ps.printf(d6, index);
301 ps.print(' ');
302 ps.printf(s2,
303 JOEElementTable.instance().getSymbol(atom.getAtomicNum()));
304 ps.print(" ");
305 ps.printf(f12_6, atom.getX());
306 ps.printf(f12_6, atom.getY());
307 ps.printf(f12_6, atom.getZ());
308 ps.print(' ');
309
310 // System.out.println("str1:"+str1);
311 ps.printf(d5, Integer.parseInt(str1));
312
313 BondIterator bit = atom.bondIterator();
314 JOEBond bond;
315
316 while (bit.hasNext())
317 {
318 bond = bit.nextBond();
319 ps.printf(d6, (bond.getNbrAtom(atom)).getIdx());
320 }
321
322 if (forceUnixStyle)
323 {
324 ps.print('\n');
325 }
326 else
327 {
328 ps.println();
329 }
330
331 index++;
332 }
333
334 return true;
335 }
336
337 /**
338 * Description of the Method
339 *
340 * @return Description of the Return Value
341 */
342 public boolean writeable()
343 {
344 return true;
345 }
346 }
347 ///////////////////////////////////////////////////////////////////////////////
348 // END OF FILE.
349 ///////////////////////////////////////////////////////////////////////////////