Source code: joelib/desc/types/Autocorrelation.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: Autocorrelation.java,v $
3 // Purpose: Moreau-Broto autocorrelation.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.11 $
8 // $Date: 2003/08/22 15:56:16 $
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.desc.types;
23
24 import joelib.desc.DescDescription;
25 import joelib.desc.DescResult;
26 import joelib.desc.Descriptor;
27 import joelib.desc.DescriptorException;
28 import joelib.desc.DescriptorHelper;
29 import joelib.desc.DescriptorInfo;
30 import joelib.desc.ResultFactory;
31
32 import joelib.desc.result.APropDoubleArrResult;
33 import joelib.desc.result.IntMatrixResult;
34
35 import joelib.molecule.JOEMol;
36
37 import joelib.molecule.types.AtomProperties;
38
39 import joelib.util.JOEHelper;
40 import joelib.util.JOEProperty;
41 import joelib.util.JOEPropertyHelper;
42
43 /*==========================================================================*
44 * IMPORTS
45 *==========================================================================*/
46 import java.util.Map;
47
48 import org.apache.log4j.Category;
49
50
51 /*==========================================================================*
52 * CLASS DECLARATION
53 *==========================================================================*/
54
55 /**
56 * Moreau-Broto autocorrelation.
57 *
58 * @author wegnerj
59 * @license GPL
60 * @cvsversion $Revision: 1.11 $, $Date: 2003/08/22 15:56:16 $
61 * @cite bmv84
62 */
63 public class Autocorrelation implements Descriptor
64 {
65 //~ Static fields/initializers /////////////////////////////////////////////
66
67 /**
68 * Obtain a suitable logger.
69 */
70 private static Category logger = Category.getInstance(
71 "joelib.desc.types.Autocorrelation");
72 public final static String ATOM_PROPERTY = "ATOM_PROPERTY";
73 private final static JOEProperty[] ACCEPTED_PROPERTIES = new JOEProperty[]
74 {
75 new JOEProperty(ATOM_PROPERTY, "java.lang.String",
76 "Atom property to use.", true, "Gasteiger_Marsili"),
77 };
78 public static final String DESC_KEY = "Auto_correlation";
79
80 //~ Instance fields ////////////////////////////////////////////////////////
81
82 /*-------------------------------------------------------------------------*
83 * private variables
84 *-------------------------------------------------------------------------*/
85 private DescriptorInfo descInfo;
86 private String propertyName;
87
88 //~ Constructors ///////////////////////////////////////////////////////////
89
90 /*-------------------------------------------------------------------------*
91 * constructor
92 *------------------------------------------------------------------------- */
93 public Autocorrelation()
94 {
95 if (logger.isDebugEnabled())
96 {
97 logger.debug("Initialize " + this.getClass().getName());
98 }
99
100 descInfo = DescriptorHelper.generateDescInfo(DESC_KEY, this.getClass(),
101 DescriptorInfo.TYPE_NO_COORDINATES, null,
102 "joelib.desc.result.APropDoubleArrResult");
103 }
104
105 //~ Methods ////////////////////////////////////////////////////////////////
106
107 /*-------------------------------------------------------------------------*
108 * public methods
109 *------------------------------------------------------------------------- */
110
111 /**
112 * Description of the Method
113 *
114 * @return Description of the Return Value
115 */
116 public DescriptorInfo getDescInfo()
117 {
118 return descInfo;
119 }
120
121 /**
122 * Sets the descriptionFile attribute of the Descriptor object
123 *
124 * @param _descInfo The new descInfo value
125 */
126
127 // public void setDescInfo(DescriptorInfo _descInfo)
128 // {
129 // descInfo = _descInfo;
130 // }
131
132 /**
133 * Gets the description attribute of the Descriptor object
134 *
135 * @return The description value
136 */
137 public DescDescription getDescription()
138 {
139 return new DescDescription(descInfo.getDescriptionFile());
140 }
141
142 public JOEProperty[] acceptedProperties()
143 {
144 return ACCEPTED_PROPERTIES;
145 }
146
147 /**
148 * Description of the Method
149 *
150 * @param mol Description of the Parameter
151 * @return Description of the Return Value
152 * @exception DescriptorException Description of the Exception
153 */
154 public DescResult calculate(JOEMol mol) throws DescriptorException
155 {
156 DescResult result = ResultFactory.instance().getDescResult(descInfo.getName());
157
158 return calculate(mol, result, null);
159 }
160
161 /**
162 * Description of the Method
163 *
164 * @param mol Description of the Parameter
165 * @param initData Description of the Parameter
166 * @return Description of the Return Value
167 * @exception DescriptorException Description of the Exception
168 */
169 public DescResult calculate(JOEMol mol, Map properties)
170 throws DescriptorException
171 {
172 DescResult result = ResultFactory.instance().getDescResult(descInfo.getName());
173
174 return calculate(mol, result, properties);
175 }
176
177 /**
178 * Description of the Method
179 *
180 * @param mol Description of the Parameter
181 * @param descResult Description of the Parameter
182 * @return Description of the Return Value
183 * @exception DescriptorException Description of the Exception
184 */
185 public DescResult calculate(JOEMol mol, DescResult descResult)
186 throws DescriptorException
187 {
188 return calculate(mol, descResult, null);
189 }
190
191 /**
192 * Description of the Method
193 *
194 * @param mol Description of the Parameter
195 * @param initData Description of the Parameter
196 * @param descResult Description of the Parameter
197 * @return Description of the Return Value
198 * @exception DescriptorException Description of the Exception
199 */
200 public DescResult calculate(JOEMol molOriginal, DescResult descResult,
201 Map properties) throws DescriptorException
202 {
203 APropDoubleArrResult result = null;
204
205 // check if the result type is correct
206 if (!(descResult instanceof APropDoubleArrResult))
207 {
208 logger.error(descInfo.getName() + " result should be of type " +
209 APropDoubleArrResult.class.getName() + " but it's of type " +
210 descResult.getClass().toString());
211
212 return null;
213 }
214
215 // initialize result type, if not already initialized
216 else
217 {
218 result = (APropDoubleArrResult) descResult;
219 }
220
221 // check if the init type is correct
222 if (!initialize(properties))
223 {
224 return null;
225 }
226
227 if (molOriginal.empty())
228 {
229 result.value = new double[1];
230 result.atomProperty = propertyName;
231 logger.warn("Empty molecule '" + molOriginal.getTitle() + "'. " +
232 DESC_KEY + " was set to ac[0]=0.");
233
234 return result;
235 }
236
237 JOEMol mol = (JOEMol) molOriginal.clone(true, new String[]{propertyName});
238 mol.deleteHydrogens();
239
240 //System.out.print("propertyName: "+propertyName);
241 // get distance matrix or calculate if not already available
242 DescResult tmpResult;
243 String distanceMatrixKey = "Distance_matrix";
244 tmpResult = DescriptorHelper.instance().descFromMol(mol,
245 distanceMatrixKey);
246
247 if (!(tmpResult instanceof IntMatrixResult))
248 {
249 logger.error("Needed descriptor '" + distanceMatrixKey +
250 "' should be of type " + IntMatrixResult.class.getName() +
251 ". " + DESC_KEY + " can not be calculated.");
252
253 return null;
254 }
255
256 IntMatrixResult distResult = (IntMatrixResult) tmpResult;
257 int[][] distances = distResult.value;
258
259 // get maximum distance value
260 int maxDistance = -Integer.MAX_VALUE;
261
262 for (int i = 0; i < distances.length; i++)
263 {
264 for (int ii = 0; ii < i; ii++)
265 {
266 if (maxDistance < distances[i][ii])
267 {
268 maxDistance = distances[i][ii];
269 }
270 }
271 }
272
273 // get atom properties or calculate if not already available
274 DescResult tmpPropResult;
275 tmpPropResult = DescriptorHelper.instance().descFromMol(mol,
276 propertyName);
277
278 AtomProperties atomProperties;
279
280 if (JOEHelper.hasInterface(tmpPropResult, "AtomProperties"))
281 {
282 atomProperties = (AtomProperties) tmpPropResult;
283 }
284 else
285 {
286 logger.error("Property '" + propertyName +
287 "' must be an atom type to calculate the " + DESC_KEY + ".");
288
289 return null;
290 }
291
292 // calculate autocorrelation
293 // System.out.println("maxDistance:"+maxDistance);
294 if (maxDistance < 0)
295 {
296 logger.warn("Possibly invalid molecule or only one atom in " +
297 mol.getTitle());
298 maxDistance = 0;
299
300 //return null;
301 }
302
303 if (maxDistance == Integer.MAX_VALUE)
304 {
305 logger.warn("Some atoms which where never visited by BFS in " +
306 mol.getTitle());
307
308 //maxDistance=0;
309 return null;
310 }
311
312 double[] acValues = new double[maxDistance + 1];
313 double value;
314
315 // System.out.println("atoms:"+mol.numAtoms()+" distances:"+distances.length);
316 for (int i = 0; i < distances.length; i++)
317 {
318 for (int ii = 0; ii <= i; ii++)
319 {
320 value = atomProperties.getDoubleValue(i + 1) * atomProperties.getDoubleValue(ii +
321 1);
322 acValues[distances[i][ii]] += value;
323 acValues[distances[ii][i]] += value;
324 }
325
326 //System.out.print(" "+atomProperties.getDoubleValue(i + 1));
327 }
328
329 //System.out.println("");
330 // for (int i = 0; i < acValues.length; i++) {
331 // System.out.print(" "+acValues[i]);
332 // }
333 // System.out.println("");
334 // save result
335 result.value = acValues;
336 result.atomProperty = propertyName;
337
338 return result;
339 }
340
341 /**
342 * Description of the Method
343 */
344 public void clear()
345 {
346 }
347
348 /**
349 * Description of the Method
350 *
351 * @param initData Description of the Parameter
352 * @return Description of the Return Value
353 */
354 public boolean initialize(Map properties)
355 {
356 if (!JOEPropertyHelper.checkProperties(this, properties))
357 {
358 logger.error("Empty property definition or missing property entry.");
359
360 return false;
361 }
362
363 String property = (String) JOEPropertyHelper.getProperty(this,
364 ATOM_PROPERTY, properties);
365
366 if (property == null)
367 {
368 propertyName = "Gasteiger_Marsili";
369 }
370 else
371 {
372 propertyName = property;
373 }
374
375 return true;
376 }
377
378 /**
379 * Test the implementation of this descriptor.
380 *
381 * @return <tt>true</tt> if the implementation is correct
382 */
383 public boolean testDescriptor()
384 {
385 return true;
386 }
387 }
388 ///////////////////////////////////////////////////////////////////////////////
389 // END OF FILE.
390 ///////////////////////////////////////////////////////////////////////////////