Source code: joelib/io/types/MolconnZ.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: MolconnZ.java,v $
3 // Purpose: Molconn-Z file format support.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.6 $
8 // $Date: 2003/08/19 13:11:26 $
9 // $Author: wegner $
10 //
11 // Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation version 2 of the License.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 ///////////////////////////////////////////////////////////////////////////////
22 package joelib.io.types;
23
24 import cformat.PrintfStream;
25
26 import joelib.data.JOEPairData;
27
28 import joelib.io.MoleculeFileType;
29 import joelib.io.MoleculeIOException;
30
31 import joelib.molecule.JOEMol;
32
33 import joelib.util.JHM;
34
35 import wsi.ra.tool.PropertyHolder;
36 import wsi.ra.tool.ResourceLoader;
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
47 import java.util.Vector;
48
49 import org.apache.log4j.Category;
50
51
52 /*==========================================================================*
53 * CLASS DECLARATION
54 *========================================================================== */
55
56 /**
57 * <a href="http://www.edusoft-lc.com/molconn/">Molconn-Z</a> file format support.
58 *
59 * @author wegnerj
60 * @license GPL
61 * @cvsversion $Revision: 1.6 $, $Date: 2003/08/19 13:11:26 $
62 */
63 public class MolconnZ implements MoleculeFileType
64 {
65 //~ Static fields/initializers /////////////////////////////////////////////
66
67 /*-------------------------------------------------------------------------*
68 * private static member variables
69 *------------------------------------------------------------------------- */
70
71 /**
72 * Obtain a suitable logger.
73 */
74 private static Category logger = Category.getInstance(
75 "joelib.io.types.MolconnZ");
76 private final static String description = new String("MolconnZ result file");
77 private final static String[] extensions = new String[]{"s"};
78
79 //~ Instance fields ////////////////////////////////////////////////////////
80
81 /*-------------------------------------------------------------------------*
82 * private member variables
83 *------------------------------------------------------------------------- */
84 private LineNumberReader lnr;
85 private PrintfStream ps;
86 private Vector descLines;
87 private int linesRemaining;
88 private long lineCounter;
89
90 //~ Constructors ///////////////////////////////////////////////////////////
91
92 /*-------------------------------------------------------------------------*
93 * constructor
94 *------------------------------------------------------------------------- */
95
96 /**
97 * Constructor for the Smiles object
98 */
99 public MolconnZ()
100 {
101 }
102
103 //~ Methods ////////////////////////////////////////////////////////////////
104
105 /**
106 * Description of the Method
107 *
108 * @exception IOException Description of the Exception
109 */
110 public void closeReader() throws IOException
111 {
112 lnr.close();
113 }
114
115 /**
116 * Description of the Method
117 *
118 * @exception IOException Description of the Exception
119 */
120 public void closeWriter() throws IOException
121 {
122 ps.close();
123 }
124
125 /**
126 * Description of the Method
127 *
128 * @param is Description of the Parameter
129 * @exception IOException Description of the Exception
130 */
131 public void initReader(InputStream is) throws IOException
132 {
133 lnr = new LineNumberReader(new InputStreamReader(is));
134
135 if (!initializeParser())
136 {
137 throw new IOException("Could not open parser information.");
138 }
139
140 lineCounter = 0;
141 }
142
143 /**
144 * Description of the Method
145 *
146 * @param os Description of the Parameter
147 * @exception IOException Description of the Exception
148 */
149 public void initWriter(OutputStream os) throws IOException
150 {
151 ps = new PrintfStream(os);
152 }
153
154 public boolean initializeParser()
155 {
156 String value;
157
158 if ((value = PropertyHolder.instance().getProperty(this,
159 "parserDefinition")) == null)
160 {
161 logger.error("Parser description for Molconn-Z file not defined.");
162
163 return false;
164 }
165
166 descLines = ResourceLoader.readLines(value);
167
168 if (descLines == null)
169 {
170 logger.error("File with parser description could not be found.");
171
172 return false;
173 }
174
175 int size = descLines.size();
176 Vector descs;
177
178 for (int i = 0; i < size; i++)
179 {
180 // parse descriptor lines
181 descs = new Vector();
182
183 // System.out.println("line "+i+"("+size+"):"+descLines.get(i));
184 JHM.tokenize(descs, (String) descLines.get(i), " \t\n\r");
185 descLines.setElementAt(descs, i);
186 }
187
188 return true;
189 }
190
191 /*-------------------------------------------------------------------------*
192 * public static methods
193 *------------------------------------------------------------------------- */
194
195 /**
196 * Description of the Method
197 *
198 * @return Description of the Return Value
199 */
200 public String inputDescription()
201 {
202 return description;
203 }
204
205 /**
206 * Description of the Method
207 *
208 * @return Description of the Return Value
209 */
210 public String[] inputFileExtensions()
211 {
212 return extensions;
213 }
214
215 /**
216 * Description of the Method
217 *
218 * @return Description of the Return Value
219 */
220 public String outputDescription()
221 {
222 return null;
223 }
224
225 /**
226 * Description of the Method
227 *
228 * @return Description of the Return Value
229 */
230 public String[] outputFileExtensions()
231 {
232 return null;
233 }
234
235 /**
236 * Reads an molecule entry as (unparsed) <tt>String</tt> representation.
237 *
238 * @param mol the molecule to store the data
239 * @return <tt>null</tt> if the reader contains no
240 * more relevant data. Otherwise the <tt>String</tt> representation
241 * of the whole molecule entry is returned.
242 * @exception IOException typical IOException
243 */
244 public String read() throws IOException
245 {
246 int s = descLines.size();
247 StringBuffer sb = new StringBuffer(s * 100);
248 linesRemaining = s;
249
250 String line;
251
252 for (int i = 1; i <= s; i++, linesRemaining--)
253 {
254 if ((line = lnr.readLine()) == null)
255 {
256 linesRemaining--;
257 skipReaderEntry();
258
259 return null;
260 }
261
262 sb.append(line);
263 sb.append('\n');
264 }
265
266 return sb.toString();
267 }
268
269 /**
270 * Description of the Method
271 *
272 * @param mol Description of the Parameter
273 * @return Description of the Return Value
274 * @exception IOException Description of the Exception
275 */
276 public boolean read(JOEMol mol) throws IOException, MoleculeIOException
277 {
278 return read(mol, null);
279 }
280
281 /**
282 * Loads an molecule in SMILES format and sets the title. If <tt>title</tt>
283 * is <tt>null</tt> the title line in the molecule file is used.
284 *
285 * @param mol Description of the Parameter
286 * @param title Description of the Parameter
287 * @return Description of the Return Value
288 * @exception IOException Description of the Exception
289 */
290 public synchronized boolean read(JOEMol mol, String title)
291 throws IOException, MoleculeIOException
292 {
293 String line;
294
295 int s = descLines.size();
296 linesRemaining = s;
297
298 Vector descs;
299 Vector diLine;
300 int ds;
301
302 for (int i = 1; i <= s; i++, linesRemaining--)
303 {
304 if ((line = lnr.readLine()) == null)
305 {
306 return (false);
307 }
308
309 lineCounter++;
310 descs = (Vector) descLines.get(i - 1);
311 diLine = new Vector(30);
312 JHM.tokenize(diLine, line, " \t\n\r");
313
314 //System.out.println(""+lnr.getLineNumber()+":"+line);
315 ds = descs.size();
316
317 if ((i < 49) && (ds != diLine.size()))
318 {
319 linesRemaining--;
320 skipReaderEntry();
321 throw new MoleculeIOException("Line " + lineCounter + "(" + i +
322 ")" + " should contain " + ds + " descriptor values not " +
323 diLine.size() + ": " + line);
324 }
325
326 // take the real size of available descriptors
327 // only needed for line 49 and 50
328 ds = diLine.size();
329
330 for (int n = 0; n < ds; n++)
331 {
332 JOEPairData dp = new JOEPairData();
333 dp.setAttribute((String) descs.get(n));
334 dp.setValue((String) diLine.get(n));
335 mol.addData(dp);
336 }
337
338 diLine = null;
339 line = null;
340 }
341
342 return (true);
343 }
344
345 /**
346 * Description of the Method
347 *
348 * @return Description of the Return Value
349 */
350 public boolean readable()
351 {
352 return true;
353 }
354
355 /**
356 * Description of the Method
357 *
358 * @return Description of the Return Value
359 * @exception IOException Description of the Exception
360 */
361 public boolean skipReaderEntry() throws IOException
362 {
363 String line;
364
365 for (int i = linesRemaining; i > 0; i--)
366 {
367 if ((line = lnr.readLine()) == null)
368 {
369 lineCounter++;
370
371 return (false);
372 }
373
374 line = null;
375 }
376
377 return true;
378 }
379
380 /**
381 * Description of the Method
382 *
383 * @param mol Description of the Parameter
384 * @return Description of the Return Value
385 * @exception IOException Description of the Exception
386 */
387 public boolean write(JOEMol mol) throws IOException
388 {
389 return write(mol, null);
390 }
391
392 /**
393 * Description of the Method
394 *
395 * @param mol Description of the Parameter
396 * @param title Description of the Parameter
397 * @return Description of the Return Value
398 * @exception IOException Description of the Exception
399 */
400 public boolean write(JOEMol mol, String title) throws IOException
401 {
402 return (true);
403 }
404
405 /**
406 * Description of the Method
407 *
408 * @return Description of the Return Value
409 */
410 public boolean writeable()
411 {
412 return false;
413 }
414 }
415 ///////////////////////////////////////////////////////////////////////////////
416 // END OF FILE.
417 ///////////////////////////////////////////////////////////////////////////////