1 /* Copyright (C) 2006-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 modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 package org.openscience.cdk.qsar.descriptors.molecular;
20 
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 
24 import org.openscience.cdk.exception.CDKException;
25 import org.openscience.cdk.interfaces.IAtom;
26 import org.openscience.cdk.interfaces.IAtomContainer;
27 import org.openscience.cdk.interfaces.IBond;
28 import org.openscience.cdk.qsar.AbstractMolecularDescriptor;
29 import org.openscience.cdk.qsar.DescriptorSpecification;
30 import org.openscience.cdk.qsar.DescriptorValue;
31 import org.openscience.cdk.qsar.IMolecularDescriptor;
32 import org.openscience.cdk.qsar.descriptors.bond.IPBondLearningDescriptor;
33 import org.openscience.cdk.qsar.result.DoubleArrayResult;
34 import org.openscience.cdk.qsar.result.DoubleResult;
35 import org.openscience.cdk.qsar.result.DoubleResultType;
36 import org.openscience.cdk.qsar.result.IDescriptorResult;
37 import org.openscience.cdk.tools.IonizationPotentialTool;
38 import org.openscience.cdk.tools.LonePairElectronChecker;
39 
40 /**
41  *  This class returns the ionization potential of a molecule. Up to now is
42  *  only possible for atomContainers which contain; see IPAtomicDescriptor and
43  *  IPBondDescriptor.
44  *
45  * The descriptor assumes that explicit hydrogens have been added to the molecule
46  *
47  * <table border="1"><caption>Parameters for this descriptor:</caption>
48  *   <tr>
49  *     <td>Name</td>
50  *     <td>Default</td>
51  *     <td>Description</td>
52  *   </tr>
53  *   <tr>
54  *     <td>addlp</td>
55  *     <td>true</td>
56  *     <td>If true lone pairs are added to the molecule</td>
57  *   </tr>
58  * </table>
59  *
60  * @author           Miguel Rojas
61  * @cdk.created      2006-05-26
62  * @cdk.module       qsarionpot
63  * @cdk.githash
64  * @cdk.dictref      qsar-descriptors:ionizationPotential
65  * @cdk.keyword      ionization potential
66  *
67  * @see org.openscience.cdk.qsar.descriptors.atomic.IPAtomicHOSEDescriptor
68  * @see IPBondLearningDescriptor
69  */
70 @Deprecated
71 public class IPMolecularLearningDescriptor extends AbstractMolecularDescriptor implements IMolecularDescriptor {
72 
73     private boolean               addlp = true;
74     private static final String[] names = {"MolIP"};
75 
76     /**
77      *  Constructor for the IPMolecularLearningDescriptor object
78      */
IPMolecularLearningDescriptor()79     public IPMolecularLearningDescriptor() {}
80 
81     /**
82      *  Gets the specification attribute of the IPMolecularLearningDescriptor object
83      *
84      *@return    The specification value
85      */
86     @Override
getSpecification()87     public DescriptorSpecification getSpecification() {
88         return new DescriptorSpecification("http://www.blueobelisk.org/ontologies/chemoinformatics-algorithms/#ip",
89                 this.getClass().getName(), "The Chemistry Development Kit");
90     }
91 
92     /**
93      *  Sets the parameters attribute of the IPMolecularLearningDescriptor object
94      *
95      *@param  params            The new parameters value
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 != 1) throw new CDKException("One parameter expected");
101         if (!(params[0] instanceof Boolean)) throw new CDKException("Boolean parameter expected");
102         addlp = (Boolean) params[0];
103     }
104 
105     /**
106      * Gets the parameters attribute of the IPMolecularLearningDescriptor object
107      *
108      * @return The parameters value
109      */
110     @Override
getParameters()111     public Object[] getParameters() {
112         return new Object[]{addlp};
113     }
114 
115     @Override
getDescriptorNames()116     public String[] getDescriptorNames() {
117         return names;
118     }
119 
120     /**
121      *  It calculates the first ionization energy of a molecule.
122      *
123      *@param  atomContainer     AtomContainer
124      *@return                   The first ionization energy
125      */
126     @Override
calculate(IAtomContainer atomContainer)127     public DescriptorValue calculate(IAtomContainer atomContainer) {
128         IAtomContainer local;
129         if (addlp) {
130             try {
131                 local = (IAtomContainer) atomContainer.clone();
132                 LonePairElectronChecker lpcheck = new LonePairElectronChecker();
133                 lpcheck.saturate(local);
134             } catch (CloneNotSupportedException e) {
135                 return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(
136                         Double.NaN), getDescriptorNames(), e);
137             } catch (CDKException e) {
138                 return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(
139                         Double.NaN), getDescriptorNames(), e);
140             }
141 
142         } else
143             local = atomContainer;
144 
145         DoubleResult value;
146         try {
147             value = new DoubleResult(((DoubleArrayResult) calculatePlus(local).getValue()).get(0));
148         } catch (CDKException e) {
149             return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), new DoubleResult(
150                     Double.NaN), getDescriptorNames(), e);
151         }
152         return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), value,
153                 getDescriptorNames());
154     }
155 
156     /**
157      *  It calculates the 1,2,.. ionization energies of a molecule.
158      *
159      *@param  container         AtomContainer
160      *@return                   The 1, 2, .. ionization energies
161      *@exception  CDKException  Possible Exceptions
162      */
calculatePlus(IAtomContainer container)163     public DescriptorValue calculatePlus(IAtomContainer container) throws CDKException {
164 
165         ArrayList<Double> dar = new ArrayList<Double>();
166         for (Iterator<IAtom> itA = container.atoms().iterator(); itA.hasNext();) {
167             IAtom atom = itA.next();
168             double value = IonizationPotentialTool.predictIP(container, atom);
169             if (value != 0) dar.add(value);
170         }
171         for (Iterator<IBond> itB = container.bonds().iterator(); itB.hasNext();) {
172             IBond bond = itB.next();
173             if (bond.getOrder() == IBond.Order.DOUBLE & bond.getBegin().getSymbol().equals("C")
174                     & bond.getEnd().getSymbol().equals("C")) {
175                 double value = IonizationPotentialTool.predictIP(container, bond);
176                 if (value != 0) dar.add(value);
177 
178             }
179         }
180 
181         DoubleArrayResult results = arrangingEnergy(dar);
182 
183         return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(), results,
184                 getDescriptorNames(), null);
185 
186     }
187 
188     /**
189      * put in increasing order the ArrayList
190      *
191      * @param array The ArrayList to order
192      * @return      The DoubleArrayResult ordered
193      */
arrangingEnergy(ArrayList<Double> array)194     private DoubleArrayResult arrangingEnergy(ArrayList<Double> array) {
195 
196         DoubleArrayResult results = new DoubleArrayResult();
197         int count = array.size();
198         for (int i = 0; i < count; i++) {
199             double min = array.get(0);
200             int pos = 0;
201             for (int j = 0; j < array.size(); j++) {
202                 double value = array.get(j);
203                 if (value < min) {
204                     min = value;
205                     pos = j;
206                 }
207             }
208             array.remove(pos);
209             results.add(min);
210         }
211         return results;
212     }
213 
214     /**
215      * Returns the specific type of the DescriptorResult object.
216      *
217      * The return value from this method really indicates what type of result will
218      * be obtained from the {@link org.openscience.cdk.qsar.DescriptorValue} object. Note that the same result
219      * can be achieved by interrogating the {@link org.openscience.cdk.qsar.DescriptorValue} object; this method
220      * allows you to do the same thing, without actually calculating the descriptor.
221      *
222      * @return an object that implements the {@link org.openscience.cdk.qsar.result.IDescriptorResult} interface indicating
223      *         the actual type of values returned by the descriptor in the {@link org.openscience.cdk.qsar.DescriptorValue} object
224      */
225     @Override
getDescriptorResultType()226     public IDescriptorResult getDescriptorResultType() {
227         return new DoubleResultType();
228     }
229 
230     /**
231      *  Gets the parameterNames attribute of the IPMolecularLearningDescriptor object
232      *
233      *@return    The parameterNames value
234      */
235     @Override
getParameterNames()236     public String[] getParameterNames() {
237         return new String[]{"addlp"};
238     }
239 
240     /**
241      *  Gets the parameterType attribute of the IPMolecularLearningDescriptor object
242      *
243      *@param  name  Description of the Parameter
244      *@return       The parameterType value
245      */
246     @Override
getParameterType(String name)247     public Object getParameterType(String name) {
248         return addlp;
249     }
250 
251 }
252