Source code: joelib/data/JOEPhModel.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: JOEPhModel.java,v $
3 // Purpose: Model for PH values.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.21 $
8 // $Date: 2003/12/03 18:15:32 $
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.data;
25
26 import joelib.molecule.JOEAtom;
27 import joelib.molecule.JOEMol;
28
29 import joelib.smarts.JOESmartsPattern;
30
31 import joelib.smarts.types.SMARTSPatternFVec;
32
33 import joelib.util.JHM;
34 import joelib.util.JOEHelper;
35
36 import joelib.util.iterator.AtomIterator;
37
38 import wsi.ra.tool.PropertyHolder;
39
40 /*==========================================================================*
41 * IMPORTS
42 *========================================================================== */
43 import java.util.Properties;
44 import java.util.Vector;
45
46 import org.apache.log4j.Category;
47
48
49 /*==========================================================================*
50 * CLASS DECLARATION
51 *========================================================================== */
52
53 /**
54 * Model for the protonation/deprotonation of molecules.
55 * The definition file can be defined in the
56 * <tt>joelib.data.JOEPhModel.resourceFile</tt> property in the {@link wsi.ra.tool.PropertyHolder}.
57 * The {@link wsi.ra.tool.ResourceLoader} loads the <tt>joelib.properties</tt> file for default.
58 *
59 * <p>
60 * Default:<br>
61 * joelib.data.JOEPhModel.resourceFile=<a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/joelib/joelib/src/joelib/data/plain/phmodel.txt?rev=HEAD&content-type=text/vnd.viewcvs-markup">joelib/data/plain/phmodel.txt</a>
62 *
63 * @author wegnerj
64 * @license GPL
65 * @cvsversion $Revision: 1.21 $, $Date: 2003/12/03 18:15:32 $
66 * @see wsi.ra.tool.PropertyHolder
67 * @see wsi.ra.tool.ResourceLoader
68 * @see joelib.data.JOEChemTransformation
69 */
70 public class JOEPhModel extends JOEGlobalDataBase
71 {
72 //~ Static fields/initializers /////////////////////////////////////////////
73
74 /*-------------------------------------------------------------------------*
75 * public static member variables
76 *------------------------------------------------------------------------- */
77
78 /**
79 * Obtain a suitable logger.
80 */
81 private static Category logger = Category.getInstance(
82 "joelib.data.JOEPhModel");
83 private static JOEPhModel phmodel;
84 private static final String DEFAULT_RESOURCE = "joelib/data/plain/phmodel.txt";
85
86 //~ Instance fields ////////////////////////////////////////////////////////
87
88 /*-------------------------------------------------------------------------*
89 * public member variables
90 *------------------------------------------------------------------------- */
91
92 /**
93 * Match list.
94 * {@link java.util.Vector} of <tt>int[1]</tt>{@link java.util.Vector}
95 */
96 private Vector matchList;
97
98 /**
99 * Seed charges for the Gasteiger-Marsili partial charges.
100 * {@link java.util.Vector} of <tt>SMARTSPatternFVec</tt>
101 */
102 private Vector seedChargeGM;
103
104 /**
105 * SMARTS based transformation patterns.
106 * {@link java.util.Vector} of <tt>JOEChemTransformation</tt>
107 */
108 private Vector transformation;
109
110 //~ Constructors ///////////////////////////////////////////////////////////
111
112 /*-------------------------------------------------------------------------*
113 * constructor
114 *------------------------------------------------------------------------- */
115
116 /**
117 * Constructor for the JOEPhModel object
118 */
119 private JOEPhModel()
120 {
121 initialized = false;
122
123 Properties prop = PropertyHolder.instance().getProperties();
124 resourceFile = prop.getProperty(this.getClass().getName() +
125 ".resourceFile", DEFAULT_RESOURCE);
126
127 matchList = new Vector();
128 transformation = new Vector();
129 seedChargeGM = new Vector();
130
131 logger.info("Using pH value correction model: " + resourceFile);
132 }
133
134 //~ Methods ////////////////////////////////////////////////////////////////
135
136 /**
137 * Description of the Method
138 */
139 public void finalize()
140 {
141 for (int k = 0; k < transformation.size(); k++)
142 {
143 Object obj = transformation.get(k);
144 obj = null;
145 }
146
147 for (int m = 0; m < seedChargeGM.size(); m++)
148 {
149 ((SMARTSPatternFVec) seedChargeGM.get(m)).sp = null;
150 }
151 }
152
153 /*-------------------------------------------------------------------------*
154 * public methods
155 *------------------------------------------------------------------------- */
156
157 /**
158 * Description of the Method
159 *
160 * @return Description of the Return Value
161 */
162 public static synchronized JOEPhModel instance()
163 {
164 if (phmodel == null)
165 {
166 phmodel = new JOEPhModel();
167 }
168
169 return phmodel;
170 }
171
172 /**
173 * Description of the Method
174 *
175 * @param mol Description of the Parameter
176 */
177 public void assignSeedPartialCharge(JOEMol mol)
178 {
179 if (!initialized)
180 {
181 init();
182 }
183
184 mol.setPartialChargesPerceived();
185
186 if (!mol.automaticPartialCharge())
187 {
188 return;
189 }
190
191 SMARTSPatternFVec sfvec;
192
193 for (int i = 0; i < seedChargeGM.size(); i++)
194 {
195 sfvec = (SMARTSPatternFVec) seedChargeGM.get(i);
196
197 if (sfvec.sp.match(mol))
198 {
199 matchList = sfvec.sp.getUMapList();
200
201 int k;
202 int[] iarr;
203
204 for (int j = 0; j < matchList.size(); j++)
205 {
206 iarr = (int[]) matchList.get(j);
207
208 for (k = 0; k < iarr.length; k++)
209 {
210 mol.getAtom(iarr[k]).setPartialCharge(sfvec.seedCharge[k]);
211 }
212 }
213 }
214 }
215 }
216
217 /**
218 * Corrects the molecule for PH.
219 * Changes the state of oxygen and nitrogen atoms, if it
220 * is allowed to change the formal charges of the atoms, that means
221 * if <tt>JOEMol.automaticFormalCharge()</tt> returns <tt>true</tt>
222 *
223 * @param mol Description of the Parameter
224 */
225 public void correctForPH(JOEMol mol)
226 {
227 //System.out.print("a_");
228 if (!initialized)
229 {
230 init();
231 }
232
233 if (mol.isCorrectedForPH())
234 {
235 return;
236 }
237
238 if (!mol.automaticFormalCharge())
239 {
240 return;
241 }
242
243 mol.setCorrectedForPH();
244
245 // set only the atoms to 0 which where considered by
246 // the PH value correction, leave all other unchanged
247 // Set O,N to zero
248 // JOEAtom atom;
249 // AtomIterator ait = mol.atomIterator();
250 // while (ait.hasNext())
251 // {
252 // atom = ait.nextAtom();
253 // atom.setFormalCharge(0);
254 // }
255 JOEChemTransformation ctsfm;
256
257 //System.out.print("b_");
258 for (int i = 0; i < transformation.size(); i++)
259 {
260 ctsfm = (JOEChemTransformation) transformation.get(i);
261 ctsfm.apply(mol);
262 }
263
264 JOEAtomTyper.instance().correctAromaticNitrogens(mol);
265
266 /*AtomIterator ait = mol.atomIterator();
267 JOEAtom atom;
268 System.out.println("PH: atoms=" + mol.numAtoms());
269 while (ait.hasNext())
270 {
271 atom = ait.nextAtom();
272 System.out.println("idx: " + atom.getIdx());
273 }*/
274 }
275
276 /**
277 * Description of the Method
278 *
279 * @param buffer Description of the Parameter
280 */
281 public void parseLine(String buffer)
282 {
283 Vector vs = new Vector();
284
285 // of type String
286 JOESmartsPattern sp;
287
288 if (buffer.trim().equals("") || (buffer.charAt(0) == '#'))
289 {
290 return;
291 }
292
293 if (JOEHelper.EQn(buffer, "TRANSFORM", 7))
294 {
295 JHM.tokenize(vs, buffer);
296
297 if ((vs.size() == 0) || (vs.size() < 4))
298 {
299 return;
300 }
301
302 JOEChemTransformation tsfm = new JOEChemTransformation();
303
304 // System.out.println("vs1: "+(String) vs.get(1));
305 // System.out.println("vs2: "+(String) vs.get(2));
306 // System.out.println("vs3: "+(String) vs.get(3));
307 if (!tsfm.init((String) vs.get(1), (String) vs.get(3)))
308 {
309 tsfm = null;
310
311 return;
312 }
313
314 transformation.add(tsfm);
315 }
316 else if (JOEHelper.EQn(buffer, "SEEDCHARGE", 10))
317 {
318 JHM.tokenize(vs, buffer);
319
320 if ((vs.size() == 0) || (vs.size() < 2))
321 {
322 return;
323 }
324
325 sp = new JOESmartsPattern();
326
327 if (!sp.init((String) vs.get(1)) ||
328 ((vs.size() - 2) != sp.numAtoms()))
329 {
330 sp = null;
331
332 return;
333 }
334
335 double[] seedCharge = new double[vs.size() - 2];
336 int index = 0;
337
338 for (int i = 2; i < vs.size(); i++, index++)
339 {
340 seedCharge[index] = Double.parseDouble((String) vs.get(i));
341 }
342
343 seedChargeGM.add(new SMARTSPatternFVec(sp, seedCharge));
344 }
345 }
346 }
347 ///////////////////////////////////////////////////////////////////////////////
348 // END OF FILE.
349 ///////////////////////////////////////////////////////////////////////////////