Source code: joelib/desc/types/GeomDistanceMatrix.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: GeomDistanceMatrix.java,v $
3 // Purpose: Geometrical distance matrix.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.7 $
8 // $Date: 2003/08/19 13:11:25 $
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.DoubleMatrixResult;
33
34 import joelib.molecule.JOEMol;
35
36 import joelib.util.JOEProperty;
37
38 /*==========================================================================*
39 * IMPORTS
40 *========================================================================== */
41 import java.util.Map;
42
43 import org.apache.log4j.Category;
44
45
46 /*==========================================================================*
47 * CLASS DECLARATION
48 *========================================================================== */
49
50 /**
51 * Geometrical distance matrix.
52 *
53 * @author wegnerj
54 * @license GPL
55 * @cvsversion $Revision: 1.7 $, $Date: 2003/08/19 13:11:25 $
56 */
57 public class GeomDistanceMatrix implements Descriptor
58 {
59 //~ Static fields/initializers /////////////////////////////////////////////
60
61 /*-------------------------------------------------------------------------*
62 * public static member variables
63 *------------------------------------------------------------------------- */
64
65 /**
66 * Obtain a suitable logger.
67 */
68 private static Category logger = Category.getInstance(
69 "joelib.desc.types.GeomDistanceMatrix");
70 public static final String DESC_KEY = "Geometrical_distance_matrix";
71
72 //~ Instance fields ////////////////////////////////////////////////////////
73
74 /*-------------------------------------------------------------------------*
75 * private variables
76 *------------------------------------------------------------------------- */
77 private DescriptorInfo descInfo;
78
79 //~ Constructors ///////////////////////////////////////////////////////////
80
81 /*-------------------------------------------------------------------------*
82 * constructor
83 *------------------------------------------------------------------------- */
84
85 /**
86 * Constructor for the DistanceMatrix object
87 */
88 public GeomDistanceMatrix()
89 {
90 if (logger.isDebugEnabled())
91 {
92 logger.debug("Initialize " + this.getClass().getName());
93 }
94
95 descInfo = DescriptorHelper.generateDescInfo(DESC_KEY, this.getClass(),
96 DescriptorInfo.TYPE_GEOMETRICAL, null,
97 "joelib.desc.result.DoubleMatrixResult");
98 }
99
100 //~ Methods ////////////////////////////////////////////////////////////////
101
102 /*-------------------------------------------------------------------------*
103 * public methods
104 *------------------------------------------------------------------------- */
105
106 /**
107 * Description of the Method
108 *
109 * @return Description of the Return Value
110 */
111 public DescriptorInfo getDescInfo()
112 {
113 return descInfo;
114 }
115
116 /**
117 * Sets the descriptionFile attribute of the Descriptor object
118 *
119 * @param _descInfo The new descInfo value
120 */
121
122 // public void setDescInfo(DescriptorInfo _descInfo)
123 // {
124 // descInfo = _descInfo;
125 // }
126
127 /**
128 * Gets the description attribute of the Descriptor object
129 *
130 * @return The description value
131 */
132 public DescDescription getDescription()
133 {
134 return new DescDescription(descInfo.getDescriptionFile());
135 }
136
137 public JOEProperty[] acceptedProperties()
138 {
139 return null;
140 }
141
142 /**
143 * Description of the Method
144 *
145 * @param mol Description of the Parameter
146 * @return Description of the Return Value
147 * @exception DescriptorException Description of the Exception
148 */
149 public DescResult calculate(JOEMol mol) throws DescriptorException
150 {
151 DescResult result = ResultFactory.instance().getDescResult(descInfo.getName());
152
153 return calculate(mol, result, null);
154 }
155
156 /**
157 * Description of the Method
158 *
159 * @param mol Description of the Parameter
160 * @param initData Description of the Parameter
161 * @return Description of the Return Value
162 * @exception DescriptorException Description of the Exception
163 */
164 public DescResult calculate(JOEMol mol, Map properties)
165 throws DescriptorException
166 {
167 DescResult result = ResultFactory.instance().getDescResult(descInfo.getName());
168
169 return calculate(mol, result, properties);
170 }
171
172 /**
173 * Description of the Method
174 *
175 * @param mol Description of the Parameter
176 * @param descResult Description of the Parameter
177 * @return Description of the Return Value
178 * @exception DescriptorException Description of the Exception
179 */
180 public DescResult calculate(JOEMol mol, DescResult descResult)
181 throws DescriptorException
182 {
183 return calculate(mol, descResult, null);
184 }
185
186 /**
187 * Description of the Method
188 *
189 * @param mol Description of the Parameter
190 * @param initData Description of the Parameter
191 * @param descResult Description of the Parameter
192 * @return Description of the Return Value
193 * @exception DescriptorException Description of the Exception
194 */
195 public DescResult calculate(JOEMol mol, DescResult descResult,
196 Map properties) throws DescriptorException
197 {
198 if (mol.empty())
199 {
200 logger.error("Empty molecule '" + mol.getTitle() + "'.");
201
202 return null;
203 }
204
205 if (!(descResult instanceof DoubleMatrixResult))
206 {
207 logger.error(descInfo.getName() + " result should be of type " +
208 DoubleMatrixResult.class.getName() + " but it's of type " +
209 descResult.getClass().toString());
210 }
211
212 // check if the init type is correct
213 if (!initialize(properties))
214 {
215 return null;
216 }
217
218 double dist;
219 double[][] matrix = new double[mol.numAtoms()][mol.numAtoms()];
220
221 for (int i = 1; i < mol.numAtoms(); i++)
222 {
223 for (int k = 0; k < i; k++)
224 {
225 dist = Math.sqrt(Math.pow(mol.getAtom(i + 1).getX() -
226 mol.getAtom(k + 1).getX(), 2.0) +
227 Math.pow(mol.getAtom(i + 1).getY() -
228 mol.getAtom(k + 1).getY(), 2.0) +
229 Math.pow(mol.getAtom(i + 1).getZ() -
230 mol.getAtom(k + 1).getZ(), 2.0));
231 matrix[i][k] = dist;
232 matrix[k][i] = dist;
233 }
234 }
235
236 DoubleMatrixResult result = (DoubleMatrixResult) descResult;
237 result.value = matrix;
238
239 return result;
240 }
241
242 /**
243 * Description of the Method
244 */
245 public void clear()
246 {
247 }
248
249 /**
250 * Description of the Method
251 *
252 * @param initData Description of the Parameter
253 */
254 public boolean initialize(Map properties)
255 {
256 return true;
257 }
258
259 /**
260 * Test the implementation of this descriptor.
261 *
262 * @return <tt>true</tt> if the implementation is correct
263 */
264 public boolean testDescriptor()
265 {
266 return true;
267 }
268 }
269 ///////////////////////////////////////////////////////////////////////////////
270 // END OF FILE.
271 ///////////////////////////////////////////////////////////////////////////////