~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/qsar/descriptors/molecular/BondCountDescriptor.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  $Revision: 7894 $ $Author: egonw $ $Date: 2007-02-07 23:15:58 +0100 (Wed, 07 Feb 2007) $
 
2
 *
 
3
 *  Copyright (C) 2004-2007  Matteo Floris <mfe4@users.sf.net>
 
4
 *
 
5
 *  Contact: cdk-devel@lists.sourceforge.net
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or
 
8
 *  modify it under the terms of the GNU Lesser General Public License
 
9
 *  as published by the Free Software Foundation; either version 2.1
 
10
 *  of the License, or (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU Lesser General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU Lesser General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 */
 
21
package org.openscience.cdk.qsar.descriptors.molecular;
 
22
 
 
23
import org.openscience.cdk.exception.CDKException;
 
24
import org.openscience.cdk.interfaces.IAtomContainer;
 
25
import org.openscience.cdk.interfaces.IBond;
 
26
import org.openscience.cdk.qsar.DescriptorSpecification;
 
27
import org.openscience.cdk.qsar.DescriptorValue;
 
28
import org.openscience.cdk.qsar.IMolecularDescriptor;
 
29
import org.openscience.cdk.qsar.result.IDescriptorResult;
 
30
import org.openscience.cdk.qsar.result.IntegerResult;
 
31
 
 
32
import java.util.Iterator;
 
33
 
 
34
/**
 
35
 *  IDescriptor based on the number of bonds of a certain bond order.
 
36
 *
 
37
 * <p>This descriptor uses these parameters:
 
38
 * <table border="1">
 
39
 *   <tr>
 
40
 *     <td>Name</td>
 
41
 *     <td>Default</td>
 
42
 *     <td>Description</td>
 
43
 *   </tr>
 
44
 *   <tr>
 
45
 *     <td>order</td>
 
46
 *     <td>any</td>
 
47
 *     <td>The bond order</td>
 
48
 *   </tr>
 
49
 * </table>
 
50
 *
 
51
 * Returns a single value with name <i>nBX</i> where <i>X</i> can be
 
52
 * <ul>
 
53
 * <li>s for single bonds
 
54
 * <li>d for double bonds
 
55
 * <li>t for triple bonds
 
56
 * <li>a for aromatic bonds
 
57
 * <li>"" for all bonds
 
58
 * </ul>
 
59
 *
 
60
 * @author      mfe4
 
61
 * @cdk.created 2004-11-13
 
62
 * @cdk.module  qsar
 
63
 * @cdk.set     qsar-descriptors
 
64
 * @cdk.dictref qsar-descriptors:bondCount
 
65
 */
 
66
public class BondCountDescriptor implements IMolecularDescriptor {
 
67
 
 
68
        /** defaults to -1.0, which means: count all bonds **/
 
69
    private double order = -1.0;
 
70
 
 
71
    /**
 
72
     *  Constructor for the BondCountDescriptor object
 
73
     */
 
74
    public BondCountDescriptor() { }
 
75
 
 
76
 
 
77
    /**
 
78
     *  Gets the specification attribute of the BondCountDescriptor object
 
79
     *
 
80
     *@return    The specification value
 
81
     */
 
82
    public DescriptorSpecification getSpecification() {
 
83
        return new DescriptorSpecification(
 
84
            "http://www.blueobelisk.org/ontologies/chemoinformatics-algorithms/#bondCount",
 
85
            this.getClass().getName(),
 
86
            "$Id: BondCountDescriptor.java 7894 2007-02-07 22:15:58Z egonw $",
 
87
            "The Chemistry Development Kit");
 
88
    }
 
89
 
 
90
 
 
91
    /**
 
92
     *  Sets the parameters attribute of the BondCountDescriptor object
 
93
     *
 
94
     *@param  params            The new parameters value
 
95
     *@exception  CDKException  Description of the Exception
 
96
     */
 
97
    public void setParameters(Object[] params) throws CDKException {
 
98
        if (params.length > 1) {
 
99
            throw new CDKException("BondCount only expects one parameter");
 
100
        }
 
101
        if (!(params[0] instanceof Double)) {
 
102
            throw new CDKException("The parameter must be of type Double");
 
103
        }
 
104
        // ok, all should be fine
 
105
        order = ((Double) params[0]).doubleValue();
 
106
    }
 
107
 
 
108
 
 
109
    /**
 
110
     *  Gets the parameters attribute of the BondCountDescriptor object
 
111
     *
 
112
     *@return    The parameters value
 
113
     */
 
114
    public Object[] getParameters() {
 
115
        // return the parameters as used for the descriptor calculation
 
116
        Object[] params = new Object[1];
 
117
        params[0] = new Double(order);
 
118
        return params;
 
119
    }
 
120
 
 
121
 
 
122
    /**
 
123
     *  This method calculate the number of bonds of a given type in an atomContainer
 
124
     *
 
125
     *@param  container  AtomContainer
 
126
     *@return            The number of bonds of a certain type.
 
127
     */
 
128
    public DescriptorValue calculate(IAtomContainer container) {
 
129
        if (order < 0) {
 
130
                // the special case: just count them all
 
131
                return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(),
 
132
                    new IntegerResult(container.getBondCount()), new String[]{"nB"});
 
133
        }
 
134
        
 
135
        int bondCount = 0;
 
136
        Iterator bonds = container.bonds();
 
137
        while (bonds.hasNext()) {
 
138
            IBond bond = (IBond) bonds.next();
 
139
            if (bond.getOrder() == order) {
 
140
                bondCount += 1;
 
141
            }
 
142
        }
 
143
 
 
144
        String name = "nB";
 
145
        if (order == 1) name += "s";
 
146
        else if (order == 2) name += "d";
 
147
        else if (order == 3) name += "t";
 
148
        else if (order == 1.5) name += "a";
 
149
 
 
150
        return new DescriptorValue(getSpecification(), getParameterNames(), getParameters(),
 
151
                new IntegerResult(bondCount), new String[]{name});
 
152
    }
 
153
 
 
154
    /**
 
155
     * Returns the specific type of the DescriptorResult object.
 
156
     * <p/>
 
157
     * The return value from this method really indicates what type of result will
 
158
     * be obtained from the {@link org.openscience.cdk.qsar.DescriptorValue} object. Note that the same result
 
159
     * can be achieved by interrogating the {@link org.openscience.cdk.qsar.DescriptorValue} object; this method
 
160
     * allows you to do the same thing, without actually calculating the descriptor.
 
161
     *
 
162
     * @return an object that implements the {@link org.openscience.cdk.qsar.result.IDescriptorResult} interface indicating
 
163
     *         the actual type of values returned by the descriptor in the {@link org.openscience.cdk.qsar.DescriptorValue} object
 
164
     */
 
165
    public IDescriptorResult getDescriptorResultType() {
 
166
        return new IntegerResult(1);
 
167
    }
 
168
 
 
169
 
 
170
    /**
 
171
     *  Gets the parameterNames attribute of the BondCountDescriptor object
 
172
     *
 
173
     *@return    The parameterNames value
 
174
     */
 
175
    public String[] getParameterNames() {
 
176
        String[] params = new String[1];
 
177
        params[0] = "order";
 
178
        return params;
 
179
    }
 
180
 
 
181
 
 
182
    /**
 
183
     *  Gets the parameterType attribute of the BondCountDescriptor object
 
184
     *
 
185
     *@param  name  Description of the Parameter
 
186
     *@return       The parameterType value
 
187
     */
 
188
    public Object getParameterType(String name) {
 
189
        return new Double(0.0);
 
190
    }
 
191
}
 
192