1 /* Copyright (C) 2004-2007  Miguel Rojas <miguel.rojas@uni-koeln.de>
2  *
3  *  Contact: cdk-devel@lists.sourceforge.net
4  *
5  *  This program is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public License
7  *  as published by the Free Software Foundation; either version 2.1
8  *  of the License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 package org.openscience.cdk.qsar.descriptors.atomic;
20 
21 import org.openscience.cdk.charges.Electronegativity;
22 import org.openscience.cdk.charges.PiElectronegativity;
23 import org.openscience.cdk.exception.CDKException;
24 import org.openscience.cdk.interfaces.IAtom;
25 import org.openscience.cdk.interfaces.IAtomContainer;
26 import org.openscience.cdk.qsar.AbstractAtomicDescriptor;
27 import org.openscience.cdk.qsar.DescriptorSpecification;
28 import org.openscience.cdk.qsar.DescriptorValue;
29 import org.openscience.cdk.qsar.IAtomicDescriptor;
30 import org.openscience.cdk.qsar.result.DoubleResult;
31 import org.openscience.cdk.tools.LonePairElectronChecker;
32 import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
33 
34 /**
35  *  Pi electronegativity is given by X = a + bq + c(q*q)
36  *
37   *
38  * <table border="1"><caption>Parameters for this descriptor:</caption>
39  *   <tr>
40  *     <td>Name</td>
41  *     <td>Default</td>
42  *     <td>Description</td>
43  *   </tr>
44  *   <tr>
45  *     <td>maxIterations</td>
46  *     <td>0</td>
47  *     <td>Number of maximum iterations</td>
48  *   </tr>
49  * </table>
50  *
51  * @author      Miguel Rojas
52  * @cdk.created 2006-05-17
53  * @cdk.module  qsaratomic
54  * @cdk.githash
55  * @cdk.dictref qsar-descriptors:piElectronegativity
56  *
57  * @see Electronegativity
58  */
59 public class PiElectronegativityDescriptor extends AbstractAtomicDescriptor implements IAtomicDescriptor {
60 
61     /**Number of maximum iterations*/
62     private int                   maxIterations   = -1;
63     /**Number of maximum resonance structures*/
64     private int                   maxResonStruc   = -1;
65     /** make a lone pair electron checker. Default true*/
66     private boolean               lpeChecker      = true;
67 
68     private static final String[] NAMES = {"elecPiA"};
69     private PiElectronegativity   electronegativity;
70 
71     /**
72      *  Constructor for the PiElectronegativityDescriptor object
73      */
PiElectronegativityDescriptor()74     public PiElectronegativityDescriptor() {
75         electronegativity = new PiElectronegativity();
76     }
77 
78     /**
79      *  Gets the specification attribute of the PiElectronegativityDescriptor
80      *  object
81      *
82      *@return    The specification value
83      */
84     @Override
getSpecification()85     public DescriptorSpecification getSpecification() {
86         return new DescriptorSpecification(
87                 "http://www.blueobelisk.org/ontologies/chemoinformatics-algorithms/#piElectronegativity", this
88                         .getClass().getName(), "The Chemistry Development Kit");
89     }
90 
91     /**
92      *  Sets the parameters attribute of the PiElectronegativityDescriptor
93      *  object
94      *
95      *@param  params            The number of maximum iterations. 1= maxIterations. 2= maxResonStruc.
96      *@exception  CDKException  Description of the Exception
97      */
98     @Override
setParameters(Object[] params)99     public void setParameters(Object[] params) throws CDKException {
100         if (params.length > 3) throw new CDKException("PartialPiChargeDescriptor only expects three parameter");
101 
102         if (!(params[0] instanceof Integer)) throw new CDKException("The parameter must be of type Integer");
103         maxIterations = (Integer) params[0];
104 
105         if (params.length > 1 && params[1] != null) {
106             if (!(params[1] instanceof Boolean)) throw new CDKException("The parameter must be of type Boolean");
107             lpeChecker = (Boolean) params[1];
108         }
109 
110         if (params.length > 2 && params[2] != null) {
111             if (!(params[2] instanceof Integer)) throw new CDKException("The parameter must be of type Integer");
112             maxResonStruc = (Integer) params[2];
113         }
114     }
115 
116     /**
117      *  Gets the parameters attribute of the PiElectronegativityDescriptor
118      *  object
119      *
120      *@return    The parameters value
121      */
122     @Override
getParameters()123     public Object[] getParameters() {
124         // return the parameters as used for the descriptor calculation
125         Object[] params = new Object[3];
126         params[0] = maxIterations;
127         params[1] = lpeChecker;
128         params[2] = maxResonStruc;
129         return params;
130     }
131 
132     @Override
getDescriptorNames()133     public String[] getDescriptorNames() {
134         return NAMES;
135     }
136 
137     /**
138      *  The method calculates the pi electronegativity of a given atom
139      *  It is needed to call the addExplicitHydrogensToSatisfyValency method from the class tools.HydrogenAdder.
140      *
141      *@param  atom              The IAtom for which the DescriptorValue is requested
142      *@param  atomContainer                AtomContainer
143      *@return                   return the pi electronegativity
144      */
145     @Override
calculate(IAtom atom, IAtomContainer atomContainer)146     public DescriptorValue calculate(IAtom atom, IAtomContainer atomContainer) {
147         IAtomContainer clone;
148         IAtom localAtom;
149         try {
150             clone = (IAtomContainer) atomContainer.clone();
151             AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(clone);
152             if (lpeChecker) {
153                 LonePairElectronChecker lpcheck = new LonePairElectronChecker();
154                 lpcheck.saturate(atomContainer);
155             }
156             localAtom = clone.getAtom(atomContainer.indexOf(atom));
157         } catch (CloneNotSupportedException e) {
158             return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(
159                     Double.NaN), NAMES, null);
160         } catch (CDKException e) {
161             return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(
162                     Double.NaN), NAMES, null);
163         }
164 
165         if (maxIterations != -1 && maxIterations != 0) electronegativity.setMaxIterations(maxIterations);
166         if (maxResonStruc != -1 && maxResonStruc != 0) electronegativity.setMaxResonStruc(maxResonStruc);
167 
168         double result = electronegativity.calculatePiElectronegativity(clone, localAtom);
169 
170         return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(result),
171                                    NAMES);
172     }
173 
174     /**
175      *  Gets the parameterNames attribute of the SigmaElectronegativityDescriptor
176      *  object
177      *
178      *@return    The parameterNames value
179      */
180     @Override
getParameterNames()181     public String[] getParameterNames() {
182         String[] params = new String[3];
183         params[0] = "maxIterations";
184         params[1] = "lpeChecker";
185         params[2] = "maxResonStruc";
186         return params;
187     }
188 
189     /**
190      *  Gets the parameterType attribute of the SigmaElectronegativityDescriptor
191      *  object
192      *
193      *@param  name  Description of the Parameter
194      *@return       The parameterType value
195      */
196     @Override
getParameterType(String name)197     public Object getParameterType(String name) {
198         if ("maxIterations".equals(name)) return Integer.MAX_VALUE;
199         if ("lpeChecker".equals(name)) return Boolean.TRUE;
200         if ("maxResonStruc".equals(name)) return Integer.MAX_VALUE;
201         return null;
202     }
203 }
204