Source code: joelib/io/types/XYZ.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: XYZ.java,v $
3 // Purpose: Reader/Writer for XYZ files.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.14 $
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 import cformat.ScanfReader;
29
30 import joelib.data.JOEElementTable;
31 import joelib.data.JOETypeTable;
32
33 import joelib.io.MoleculeFileType;
34
35 import joelib.molecule.JOEAtom;
36 import joelib.molecule.JOEMol;
37
38 /*==========================================================================*
39 * IMPORTS
40 *========================================================================== */
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.io.InputStreamReader;
44 import java.io.LineNumberReader;
45 import java.io.OutputStream;
46 import java.io.StringReader;
47
48 import org.apache.log4j.Category;
49
50
51 /*==========================================================================*
52 * CLASS DECLARATION
53 *========================================================================== */
54
55 /**
56 * Reader/Writer for XYZ files.
57 *
58 * @author wegnerj
59 * @license GPL
60 * @cvsversion $Revision: 1.14 $, $Date: 2003/08/22 15:56:18 $
61 */
62 public class XYZ implements MoleculeFileType
63 {
64 //~ Static fields/initializers /////////////////////////////////////////////
65
66 /*-------------------------------------------------------------------------*
67 * private static member variables
68 *------------------------------------------------------------------------- */
69
70 /**
71 * Obtain a suitable logger.
72 */
73 private static Category logger = Category.getInstance("joelib.io.types.XYZ");
74 private final static String description = new String("XYZ");
75 private final static String[] extensions = new String[]{"xyz"};
76
77 //~ Instance fields ////////////////////////////////////////////////////////
78
79 /*-------------------------------------------------------------------------*
80 * private member variables
81 *------------------------------------------------------------------------- */
82 private LineNumberReader lnr;
83 private PrintfStream ps;
84
85 // helper variable for skipReaderEntry
86 private int linesRemaining;
87 private long lineCounter;
88
89 //~ Constructors ///////////////////////////////////////////////////////////
90
91 /*-------------------------------------------------------------------------*
92 * constructor
93 *------------------------------------------------------------------------- */
94
95 /**
96 * Constructor for the XYZ object
97 */
98 public XYZ()
99 {
100 if (logger.isDebugEnabled())
101 {
102 logger.debug("Initialize " + this.getClass().getName());
103 }
104 }
105
106 //~ Methods ////////////////////////////////////////////////////////////////
107
108 public void closeReader() throws IOException
109 {
110 lnr.close();
111 }
112
113 public void closeWriter() throws IOException
114 {
115 ps.close();
116 }
117
118 /**
119 * Description of the Method
120 *
121 * @param is Description of the Parameter
122 * @exception IOException Description of the Exception
123 */
124 public void initReader(InputStream is) throws IOException
125 {
126 lnr = new LineNumberReader(new InputStreamReader(is));
127 }
128
129 /**
130 * Description of the Method
131 *
132 * @param os Description of the Parameter
133 * @exception IOException Description of the Exception
134 */
135 public void initWriter(OutputStream os) throws IOException
136 {
137 ps = new PrintfStream(os);
138 }
139
140 /*-------------------------------------------------------------------------*
141 * public static methods
142 *------------------------------------------------------------------------- */
143
144 /**
145 * Description of the Method
146 *
147 * @return Description of the Return Value
148 */
149 public String inputDescription()
150 {
151 return description;
152 }
153
154 /**
155 * Description of the Method
156 *
157 * @return Description of the Return Value
158 */
159 public String[] inputFileExtensions()
160 {
161 return extensions;
162 }
163
164 /**
165 * Description of the Method
166 *
167 * @return Description of the Return Value
168 */
169 public String outputDescription()
170 {
171 return description;
172 }
173
174 /**
175 * Description of the Method
176 *
177 * @return Description of the Return Value
178 */
179 public String[] outputFileExtensions()
180 {
181 return extensions;
182 }
183
184 /**
185 * Reads an molecule entry as (unparsed) <tt>String</tt> representation.
186 *
187 * @return <tt>null</tt> if the reader contains no more
188 * relevant data. Otherwise the <tt>String</tt> representation of the
189 * whole molecule entry is returned.
190 * @exception IOException typical IOException
191 */
192 public String read() throws IOException
193 {
194 String line;
195
196 if ((line = lnr.readLine()) == null)
197 {
198 return null;
199 }
200
201 int s;
202
203 try
204 {
205 s = Integer.parseInt(line);
206 }
207 catch (NumberFormatException ex)
208 {
209 return null;
210 }
211
212 StringBuffer sb = new StringBuffer(s * 100);
213
214 // set number of atoms
215 sb.append(line);
216 sb.append('\n');
217
218 // number of atoms
219 linesRemaining = s;
220
221 // set title
222 if ((line = lnr.readLine()) == null)
223 {
224 return null;
225 }
226
227 sb.append(line);
228 sb.append('\n');
229
230 // set element and coordinates
231 for (int i = 1; i <= s; i++, linesRemaining--)
232 {
233 if ((line = lnr.readLine()) == null)
234 {
235 linesRemaining--;
236 skipReaderEntry();
237
238 return null;
239 }
240
241 //sb.append(i-1);
242 sb.append(line);
243 sb.append('\n');
244 }
245
246 //System.out.println(sb);
247 return sb.toString();
248 }
249
250 /**
251 * Description of the Method
252 *
253 * @param mol Description of the Parameter
254 * @return Description of the Return Value
255 * @exception IOException Description of the Exception
256 */
257 public boolean read(JOEMol mol) throws IOException
258 {
259 return read(mol, null);
260 }
261
262 /**
263 * Description of the Method
264 *
265 * @param mol Description of the Parameter
266 * @param title Description of the Parameter
267 * @return Description of the Return Value
268 * @exception IOException Description of the Exception
269 */
270 public boolean read(JOEMol mol, String title) throws IOException
271 {
272 int i;
273 int natoms;
274
275 String line;
276
277 ScanfReader scanf;
278
279 // delete molecule data
280 mol.clear();
281
282 if ((line = lnr.readLine()) == null)
283 {
284 return (false);
285 }
286
287 scanf = new ScanfReader(new StringReader(line));
288 natoms = scanf.scanInt();
289
290 if (natoms == 0)
291 {
292 return (false);
293 }
294
295 mol.reserveAtoms(natoms);
296 JOETypeTable.instance().setFromType("XYZ");
297
298 String str;
299 double x;
300 double y;
301 double z;
302 JOEAtom atom;
303 String elemString;
304
305 if ((line = lnr.readLine()) == null)
306 {
307 return (false);
308 }
309
310 // set molecule title
311 if (title == null)
312 {
313 mol.setTitle(line);
314 }
315 else
316 {
317 mol.setTitle(title);
318 }
319
320 JOETypeTable.instance().setToType("INT");
321
322 // get all atoms
323 linesRemaining = natoms;
324
325 for (i = 1; i <= natoms; i++, linesRemaining--)
326 {
327 if ((line = lnr.readLine()) == null)
328 {
329 return (false);
330 }
331
332 scanf = new ScanfReader(new StringReader(line));
333
334 //tokenize(vs,buffer);
335 //if (vs.size() != 4) return(false);
336 //x = atof((char*)vs[1].c_str());
337 //y = atof((char*)vs[2].c_str());
338 //z = atof((char*)vs[3].c_str());
339 atom = mol.newAtom();
340
341 //set atomic number
342 elemString = scanf.scanString();
343 atom.setAtomicNum(JOEElementTable.instance().getAtomicNum(elemString));
344
345 //set coordinates
346 x = scanf.scanFloat();
347 y = scanf.scanFloat();
348 z = scanf.scanFloat();
349 atom.setVector(x, y, z);
350
351 //set type
352 str = JOETypeTable.instance().translate(elemString);
353 atom.setType(str);
354 }
355
356 // connect the atoms with bonds
357 mol.connectTheDots();
358
359 return (true);
360 }
361
362 /**
363 * Description of the Method
364 *
365 * @return Description of the Return Value
366 */
367 public boolean readable()
368 {
369 return true;
370 }
371
372 public boolean skipReaderEntry() throws IOException
373 {
374 String line;
375
376 for (int i = linesRemaining; i > 0; i--)
377 {
378 if ((line = lnr.readLine()) == null)
379 {
380 lineCounter++;
381
382 return (false);
383 }
384
385 line = null;
386 }
387
388 return true;
389 }
390
391 /**
392 * Description of the Method
393 *
394 * @param mol Description of the Parameter
395 * @return Description of the Return Value
396 * @exception IOException Description of the Exception
397 */
398 public boolean write(JOEMol mol) throws IOException
399 {
400 return write(mol, null);
401 }
402
403 /**
404 * Description of the Method
405 *
406 * @param mol Description of the Parameter
407 * @param title Description of the Parameter
408 * @return Description of the Return Value
409 * @exception IOException Description of the Exception
410 */
411 public boolean write(JOEMol mol, String title) throws IOException
412 {
413 int i;
414 PrintfFormat f15_5 = new PrintfFormat("%15.5f");
415 PrintfFormat s3 = new PrintfFormat("%3s");
416
417 ps.printf("%d", mol.numAtoms());
418 ps.println();
419
420 // System.out.println("TITLE:"+mol.getTitle());
421 if (title == null)
422 {
423 ps.print(mol.getTitle());
424 }
425 else
426 {
427 ps.print(title);
428 }
429
430 ps.printf("\t%15.7f", mol.getEnergy());
431 ps.println();
432 JOETypeTable.instance().setFromType("INT");
433 JOETypeTable.instance().setToType("XYZ");
434
435 JOEAtom atom;
436
437 for (i = 1; i <= mol.numAtoms(); i++)
438 {
439 atom = mol.getAtom(i);
440
441 ps.printf(s3,
442 JOEElementTable.instance().getSymbol(atom.getAtomicNum()));
443 ps.printf(f15_5, atom.getX());
444 ps.printf(f15_5, atom.getY());
445 ps.printf(f15_5, atom.getZ());
446 ps.println();
447 }
448
449 return (true);
450 }
451
452 /**
453 * Description of the Method
454 *
455 * @return Description of the Return Value
456 */
457 public boolean writeable()
458 {
459 return true;
460 }
461 }
462 ///////////////////////////////////////////////////////////////////////////////
463 // END OF FILE.
464 ///////////////////////////////////////////////////////////////////////////////