Source code: joelib/process/types/DescSelectionWriter.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: DescSelectionWriter.java,v $
3 // Purpose: Counts the number of descriptors and molecules in a molecule file.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.6 $
8 // $Date: 2003/08/22 15:56:20 $
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.process.types;
23
24 import joelib.data.JOEDataType;
25 import joelib.data.JOEGenericData;
26 import joelib.data.JOEPairData;
27
28 import joelib.desc.NativeValue;
29
30 import joelib.io.IOType;
31 import joelib.io.IOTypeHolder;
32 import joelib.io.JOEFileFormat;
33 import joelib.io.MoleculeFileType;
34 import joelib.io.MoleculeIOException;
35 import joelib.io.PropertyWriter;
36
37 import joelib.molecule.JOEMol;
38
39 import joelib.process.JOEProcessException;
40 import joelib.process.SimpleProcess;
41
42 import joelib.util.JOEHelper;
43 import joelib.util.JOEProperty;
44
45 import joelib.util.iterator.GenericDataIterator;
46
47 /*==========================================================================*
48 * IMPORTS
49 *========================================================================== */
50 import java.io.FileOutputStream;
51 import java.io.IOException;
52 import java.io.OutputStream;
53 import java.io.PrintStream;
54
55 import java.util.Map;
56 import java.util.Vector;
57
58 import org.apache.log4j.Category;
59
60
61 /*==========================================================================*
62 * CLASS DECLARATION
63 *========================================================================== */
64
65 /**
66 * Calling processor classes if the filter rule fits.
67 *
68 * @author wegnerj
69 * @license GPL
70 * @cvsversion $Revision: 1.6 $, $Date: 2003/08/22 15:56:20 $
71 */
72 public class DescSelectionWriter extends SimpleProcess
73 {
74 //~ Static fields/initializers /////////////////////////////////////////////
75
76 /*-------------------------------------------------------------------------*
77 * public static member variables
78 *------------------------------------------------------------------------- */
79
80 /**
81 * Obtain a suitable logger.
82 */
83 private static Category logger = Category.getInstance(
84 "joelib.process.types.DescSelectionWriter");
85
86 /**
87 * Description of the Field
88 */
89 public final static int DESCRIPTORS = 0;
90
91 /**
92 * Description of the Field
93 */
94 public final static int MOL_AND_DESCRIPTORS = 1;
95
96 /**
97 * Description of the Field
98 */
99 private final static int DEFAULT_DESC_OTYPE = MOL_AND_DESCRIPTORS;
100
101 //~ Instance fields ////////////////////////////////////////////////////////
102
103 private IOType outType;
104 private IOType skipType;
105 private MoleculeFileType outWriter;
106 private MoleculeFileType skipWriter;
107 private PrintStream outStream;
108 private PrintStream skipStream;
109 private String commentString;
110 private String delimiterString;
111 private Vector descriptorNames;
112 private boolean descNamesChecked;
113 private boolean firstLineWritten;
114
115 // private final static JOEProperty[] ACCEPTED_PROPERTIES = new JOEProperty[]{
116 // new JOEProperty("SKIP_WRITER", "joelib.io.MoleculeFileType", "Writer for skipped molecule entries.", true),
117 // new JOEProperty("DELIMITER", "java.lang.String", "Delimiter between descriptors in flat mode.", true),
118 // new JOEProperty("COMMENT", "java.lang.String", "Comment character of the first line in flat mode.", true)
119 // };
120 private int descOutputType = DEFAULT_DESC_OTYPE;
121 private int molCounter;
122 private int skipCounter;
123
124 //~ Constructors ///////////////////////////////////////////////////////////
125
126 /*-------------------------------------------------------------------------*
127 * constructor
128 *------------------------------------------------------------------------- */
129
130 /**
131 * Constructor for the DescSelectionWriter object
132 */
133 public DescSelectionWriter()
134 {
135 clear();
136 }
137
138 //~ Methods ////////////////////////////////////////////////////////////////
139
140 /**
141 * Sets the comment attribute of the DescSelectionWriter object
142 *
143 * @param _comment The new comment value
144 */
145 public void setComment(String _comment)
146 {
147 commentString = _comment;
148 }
149
150 /**
151 * Gets the comment attribute of the DescSelectionWriter object
152 *
153 * @return The comment value
154 */
155 public String getComment()
156 {
157 return commentString;
158 }
159
160 /*-------------------------------------------------------------------------*
161 * public methods
162 *------------------------------------------------------------------------- */
163
164 /**
165 * Sets the delimiter attribute of the DescSelectionWriter object
166 *
167 * @param _delimiter The new delimiter value
168 */
169 public void setDelimiter(String _delimiter)
170 {
171 delimiterString = _delimiter;
172 }
173
174 /**
175 * Gets the delimiter attribute of the DescSelectionWriter object
176 *
177 * @return The delimiter value
178 */
179 public String getDelimiter()
180 {
181 return delimiterString;
182 }
183
184 /**
185 * Sets the skipStream attribute of the DescSelectionWriter object
186 *
187 * @param _skipStream The new skipStream value
188 * @param _skipType The new skipStream value
189 * @exception Exception Description of the Exception
190 */
191 public void setSkipStream(OutputStream _skipStream, IOType _skipType)
192 throws Exception
193 {
194 if (_skipStream instanceof PrintStream)
195 {
196 skipStream = (PrintStream) _skipStream;
197 }
198 else
199 {
200 skipStream = new PrintStream(_skipStream);
201 }
202
203 skipType = _skipType;
204
205 // initialize molecule skip writer
206 try
207 {
208 skipWriter = JOEFileFormat.getMolWriter(skipStream, skipType);
209 }
210 catch (Exception ex)
211 {
212 throw ex;
213 }
214
215 if (!skipWriter.writeable())
216 {
217 throw new Exception(skipType.getRepresentation() +
218 " is not writeable.\n" + "You're invited to write one !;-)");
219 }
220 }
221
222 /**
223 * Description of the Method
224 *
225 * @return Description of the Return Value
226 */
227 public boolean clear()
228 {
229 descriptorNames = null;
230 firstLineWritten = false;
231 descNamesChecked = false;
232 molCounter = 0;
233 skipCounter = 0;
234 commentString = "";
235 delimiterString = " ";
236
237 return true;
238 }
239
240 /**
241 * Description of the Method
242 *
243 * @param os Description of the Parameter
244 * @param _outType Description of the Parameter
245 * @exception Exception Description of the Exception
246 */
247 public void init(OutputStream os, IOType _outType)
248 throws Exception
249 {
250 init(os, _outType, null, DEFAULT_DESC_OTYPE);
251 }
252
253 /**
254 * Description of the Method
255 *
256 * @param _outputFile Description of the Parameter
257 * @param _outType Description of the Parameter
258 * @exception Exception Description of the Exception
259 */
260 public void init(String _outputFile, IOType _outType)
261 throws Exception
262 {
263 init(_outputFile, _outType, null, DEFAULT_DESC_OTYPE);
264 }
265
266 /**
267 * Description of the Method
268 *
269 * @param _outputFile Description of the Parameter
270 * @param _outType Description of the Parameter
271 * @param _descriptorNames Description of the Parameter
272 * @param _descOutputType Description of the Parameter
273 * @exception Exception Description of the Exception
274 */
275 public void init(String _outputFile, IOType _outType,
276 Vector _descriptorNames, int _descOutputType) throws Exception
277 {
278 // initialize output stream
279 init(new FileOutputStream(_outputFile), _outType, _descriptorNames,
280 _descOutputType);
281
282 // set file where skipped entries should be stored.
283 String skipFile;
284 int index = _outputFile.lastIndexOf(".");
285
286 if (index == -1)
287 {
288 skipFile = _outputFile + "_skip";
289 }
290 else
291 {
292 skipFile = _outputFile.substring(0, index) + "_skip.sdf";
293 }
294
295 setSkipStream(new FileOutputStream(skipFile),
296 IOTypeHolder.instance().getIOType("SDF"));
297 }
298
299 /**
300 * Description of the Method
301 *
302 * @param os Description of the Parameter
303 * @param _outType Description of the Parameter
304 * @param _descriptorNames Description of the Parameter
305 * @param _descOutputType Description of the Parameter
306 * @exception Exception Description of the Exception
307 */
308 public void init(OutputStream os, IOType _outType, Vector _descriptorNames,
309 int _descOutputType) throws Exception
310 {
311 if (os instanceof PrintStream)
312 {
313 outStream = (PrintStream) os;
314 }
315 else
316 {
317 outStream = new PrintStream(os);
318 }
319
320 outType = _outType;
321
322 if ((_descriptorNames != null) && (_descriptorNames.size() == 0))
323 {
324 logger.warn("No descriptors for writing defined in " +
325 this.getClass().getName());
326 }
327
328 descriptorNames = _descriptorNames;
329 descOutputType = _descOutputType;
330
331 // initialize molecle writer
332 try
333 {
334 outWriter = JOEFileFormat.getMolWriter(outStream, outType);
335 }
336 catch (Exception ex)
337 {
338 throw ex;
339 }
340
341 if (!outWriter.writeable())
342 {
343 throw new Exception(outType.getRepresentation() +
344 " is not writeable.\n" + "You're invited to write one !;-)");
345 }
346 }
347
348 /**
349 * Description of the Method
350 *
351 * @return Description of the Return Value
352 */
353 public JOEProperty[] neededProperties()
354 {
355 // return ACCEPTED_PROPERTIES;
356 return null;
357 }
358
359 /**
360 * Description of the Method
361 *
362 * @param mol Description of the Parameter
363 * @param properties Description of the Parameter
364 * @return Description of the Return Value
365 * @exception JOEProcessException Description of the Exception
366 */
367 public boolean process(JOEMol mol, Map properties)
368 throws JOEProcessException
369 {
370 try
371 {
372 super.process(mol, properties);
373 }
374 catch (JOEProcessException e)
375 {
376 throw new JOEProcessException("Properties for " +
377 this.getClass().getName() + " not correct.");
378 }
379
380 // System.out.println("processing:::"+mol.getTitle());
381 if (!descNamesChecked)
382 {
383 checkDescriptorNames(mol);
384 logger.debug("Descriptor names were checked.");
385 }
386
387 JOEGenericData genericData;
388
389 try
390 {
391 // write a descriptor name line at the beginning
392 if (!firstLineWritten)
393 {
394 firstLineWritten = true;
395
396 if (descOutputType == DESCRIPTORS)
397 {
398 writeFirstLine();
399 }
400 }
401
402 if (descOutputType == DESCRIPTORS)
403 {
404 int size = descriptorNames.size();
405
406 for (int i = 0; i < size; i++)
407 {
408 // get parsed data
409 genericData = mol.getData((String) descriptorNames.get(i),
410 true);
411
412 // System.out.println("write:::"+genericData);
413 if (genericData == null)
414 {
415 logger.warn((String) descriptorNames.get(i) +
416 " data entry don't exist in molecule (#" +
417 (molCounter + 1) + "): " + mol.getTitle());
418
419 try
420 {
421 skipWriter.write(mol);
422 skipCounter++;
423 }
424 catch (MoleculeIOException ex)
425 {
426 ex.printStackTrace();
427 logger.error("Could not write skipped files.");
428 }
429
430 break;
431 }
432 else
433 {
434 if (genericData.getDataType() == JOEDataType.JOE_PAIR_DATA)
435 {
436 JOEPairData data = (JOEPairData) genericData;
437
438 // if(JOEHelper.hasInterface(data, "joelib.desc.NativeValue"))
439 if (JOEHelper.hasInterface(data, "NativeValue"))
440 {
441 outStream.print(((NativeValue) data).getStringNV());
442 outStream.print(delimiterString);
443 }
444 else
445 {
446 outStream.print((String) genericData.toString());
447 outStream.print(delimiterString);
448 logger.warn("Descriptor " +
449 descriptorNames.get(i) +
450 " seems not to be a native type (int, double, atom property int, ...).");
451 }
452
453 // System.out.println(""+( String ) genericData.toString()+delimiterString);
454 }
455 }
456 }
457
458 outStream.println();
459
460 // increase counter for succesfull written molecules
461 molCounter++;
462 }
463 else if (descOutputType == MOL_AND_DESCRIPTORS)
464 {
465 // write molecule with descriptors
466 boolean success = false;
467
468 // System.out.println("write mol and desc: "+JOEHelper.hasInterface(outWriter, "PropertyWriter"));
469 if (JOEHelper.hasInterface(outWriter, "PropertyWriter"))
470 {
471 try
472 {
473 success = ((PropertyWriter) outWriter).write(mol, null,
474 true, descriptorNames);
475 }
476 catch (MoleculeIOException ex)
477 {
478 //ex.printStackTrace();
479 throw new JOEProcessException(
480 "Could not write skipped files. " + ex.toString());
481 }
482 }
483
484 if (success)
485 {
486 // increase counter for succesfull written molecules
487 molCounter++;
488 }
489 else
490 {
491 try
492 {
493 skipWriter.write(mol);
494 skipCounter++;
495 }
496 catch (MoleculeIOException ex)
497 {
498 // ex.printStackTrace();
499 // logger.error("Could not write skipped files.");
500 throw new JOEProcessException(
501 "Could not write skipped files. " + ex.toString());
502 }
503 }
504 }
505 }
506 catch (IOException ex)
507 {
508 // ex.printStackTrace();
509 throw new JOEProcessException(ex.toString());
510
511 // return false;
512 }
513
514 return true;
515 }
516
517 /**
518 * Description of the Method
519 *
520 * @param mol Description of the Parameter
521 */
522 private void checkDescriptorNames(JOEMol mol)
523 {
524 if (!descNamesChecked)
525 {
526 descNamesChecked = true;
527 }
528 else
529 {
530 return;
531 }
532
533 if (descriptorNames == null)
534 {
535 descriptorNames = new Vector(20);
536
537 GenericDataIterator gdit = mol.genericDataIterator();
538 JOEGenericData genericData;
539
540 while (gdit.hasNext())
541 {
542 genericData = gdit.nextGenericData();
543
544 if (genericData.getDataType() == JOEDataType.JOE_PAIR_DATA)
545 {
546 descriptorNames.add(genericData.getAttribute());
547 }
548 }
549 }
550 }
551
552 /*-------------------------------------------------------------------------*
553 * private methods
554 *------------------------------------------------------------------------- */
555
556 /**
557 * Description of the Method
558 *
559 * @param mol Description of the Parameter
560 * @exception IOException Description of the Exception
561 */
562 private void writeFirstLine() throws IOException
563 {
564 //already called
565 // if(!descNamesChecked) checkDescriptorNames(mol);
566 // write first line
567 // write only defined descriptors
568 outStream.print(commentString);
569
570 // outStream.print( delimiterString );
571 for (int i = 0; i < descriptorNames.size(); i++)
572 {
573 outStream.print((String) descriptorNames.get(i));
574 outStream.print(delimiterString);
575 }
576
577 outStream.println();
578 }
579 }
580 ///////////////////////////////////////////////////////////////////////////////
581 // END OF FILE.
582 ///////////////////////////////////////////////////////////////////////////////