~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/graph/invariant/HuLuIndexTool.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
/* $RCSfile$
 
2
 * $Author: egonw $    
 
3
 * $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $    
 
4
 * $Revision: 7636 $
 
5
 * 
 
6
 * Copyright (C) 1997-2007  The Chemistry Development Kit (CDK) project
 
7
 * 
 
8
 * Contact: cdk-devel@lists.sourceforge.net
 
9
 * 
 
10
 * This program is free software; you can redistribute it and/or
 
11
 * modify it under the terms of the GNU Lesser General Public License
 
12
 * as published by the Free Software Foundation; either version 2.1
 
13
 * of the License, or (at your option) any later version.
 
14
 * 
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU Lesser General Public License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 
 
23
 * 
 
24
 */
 
25
package org.openscience.cdk.graph.invariant;
 
26
 
 
27
import org.openscience.cdk.AtomContainer;
 
28
import org.openscience.cdk.graph.PathTools;
 
29
import org.openscience.cdk.graph.invariant.exception.BadMatrixFormatException;
 
30
import org.openscience.cdk.graph.invariant.exception.IndexOutOfBoundsException;
 
31
import org.openscience.cdk.graph.matrix.ConnectionMatrix;
 
32
import org.openscience.cdk.interfaces.IBond;
 
33
import org.openscience.cdk.tools.LoggingTool;
 
34
 
 
35
import java.util.Iterator;
 
36
 
 
37
/**
 
38
 * Collection of methods for the calculation of topological indices of a 
 
39
 * molecular graph.
 
40
 */
 
41
public class HuLuIndexTool
 
42
{
 
43
        private final static LoggingTool logger = new LoggingTool(HuLuIndexTool.class);
 
44
        
 
45
    /**
 
46
   * Calculates the extended adjacency matrix index.
 
47
   * An implementation of the algorithm published in {@cdk.cite HU96}.
 
48
   *
 
49
   * @cdk.keyword EAID number
 
50
   */
 
51
    public static double getEAIDNumber(AtomContainer atomContainer) throws org.openscience.cdk.exception.NoSuchAtomException,
 
52
            BadMatrixFormatException,IndexOutOfBoundsException
 
53
    {
 
54
        boolean debug = false;
 
55
 
 
56
        GIMatrix matrix = new GIMatrix(getExtendedAdjacenyMatrix(atomContainer));
 
57
 
 
58
        GIMatrix tempMatrix = matrix;
 
59
        GIMatrix fixedMatrix = matrix;
 
60
        for (int i = 2; i < atomContainer.getAtomCount(); i++)
 
61
        {
 
62
            tempMatrix = tempMatrix.multiply(fixedMatrix);
 
63
            matrix = matrix.add(tempMatrix);
 
64
        }
 
65
 
 
66
        for (int i = 0; i < atomContainer.getAtomCount(); i++)
 
67
        {
 
68
            matrix.setValueAt(i,i,matrix.getValueAt(i,i)+1);
 
69
        }
 
70
        double eaid = matrix.trace();
 
71
 
 
72
        logger.debug("final matrix - the sum of the powers of EA matrix: ");
 
73
        displayMatrix(matrix.getArrayValue());
 
74
        logger.debug("eaid number: "+ eaid);
 
75
 
 
76
        return eaid;
 
77
    }
 
78
 
 
79
 
 
80
    public static double[][] getExtendedAdjacenyMatrix(AtomContainer atomContainer)
 
81
        throws org.openscience.cdk.exception.NoSuchAtomException
 
82
    {
 
83
        boolean debug = false;
 
84
        double[][] adjaMatrix = ConnectionMatrix.getMatrix(atomContainer);
 
85
 
 
86
        logger.debug("adjacency matrix: ");
 
87
        displayMatrix(adjaMatrix);
 
88
 
 
89
        double[] atomWeights = getAtomWeights(atomContainer);
 
90
 
 
91
 
 
92
        for (int i = 0; i < adjaMatrix.length; i++)
 
93
        {
 
94
            for (int j = 0; j < adjaMatrix.length; j++)
 
95
            {
 
96
                if (i==j)
 
97
                {
 
98
                    if (atomContainer.getAtom(i).getSymbol()=="O")
 
99
                    {
 
100
                        adjaMatrix[i][j] = Math.sqrt(0.74)/6;
 
101
                    }
 
102
                    else
 
103
                    {
 
104
                        adjaMatrix[i][j] = Math.sqrt(0.74)/6;
 
105
                    }
 
106
                }
 
107
                else
 
108
                {
 
109
                    adjaMatrix[i][j] = (Math.sqrt(atomWeights[i]/atomWeights[j]) + Math.sqrt(atomWeights[j]/atomWeights[i])) * Math.sqrt(adjaMatrix[i][j])/6;
 
110
                }
 
111
            }
 
112
        }
 
113
 
 
114
        logger.debug("extended adjacency matrix: ");
 
115
        displayMatrix(adjaMatrix);
 
116
 
 
117
        return adjaMatrix;
 
118
    }
 
119
 
 
120
    public static double[] getAtomWeights(AtomContainer atomContainer) throws org.openscience.cdk.exception.NoSuchAtomException
 
121
    {
 
122
        boolean debug = false;
 
123
        org.openscience.cdk.interfaces.IAtom atom,headAtom,endAtom;        
 
124
        int headAtomPosition,endAtomPosition;
 
125
 
 
126
        //int k = 0;
 
127
        double[] weightArray = new double[atomContainer.getAtomCount()];
 
128
        double[][] adjaMatrix = ConnectionMatrix.getMatrix(atomContainer);
 
129
 
 
130
        int[][] apspMatrix = PathTools.computeFloydAPSP(adjaMatrix);
 
131
        int[] atomLayers = getAtomLayers(apspMatrix);
 
132
 
 
133
        int[] valenceSum;
 
134
        int[] interLayerBondSum;
 
135
 
 
136
        logger.debug("adjacency matrix: ");
 
137
        displayMatrix(adjaMatrix);
 
138
        logger.debug("all-pairs-shortest-path matrix: ");
 
139
        displayMatrix(apspMatrix);
 
140
        logger.debug("atom layers: ");
 
141
        displayArray(atomLayers);
 
142
 
 
143
        for (int i = 0; i < atomContainer.getAtomCount(); i++)
 
144
        {
 
145
            atom = atomContainer.getAtom(i);
 
146
 
 
147
            valenceSum = new int[atomLayers[i]];
 
148
            for (int v = 0; v < valenceSum.length; v++)
 
149
            {
 
150
                valenceSum[v] = 0;
 
151
            }
 
152
 
 
153
            interLayerBondSum = new int[atomLayers[i]-1];
 
154
            for (int v = 0; v < interLayerBondSum.length; v++)
 
155
            {
 
156
                interLayerBondSum[v] = 0;
 
157
            }
 
158
 
 
159
 
 
160
            //weightArray[k] = atom.getValenceElectronsCount() - atom.getHydrogenCount(); // method unfinished
 
161
            if(atom.getSymbol()=="O")
 
162
                weightArray[i] = 6 - atom.getHydrogenCount();
 
163
            else
 
164
                weightArray[i] = 4 - atom.getHydrogenCount();
 
165
 
 
166
 
 
167
 
 
168
            for (int j = 0; j < apspMatrix.length; j++)
 
169
            {
 
170
                if(atomContainer.getAtom(j).getSymbol()=="O")
 
171
                    valenceSum[apspMatrix[j][i]] += 6 - atomContainer.getAtom(j).getHydrogenCount();
 
172
                else
 
173
                    valenceSum[apspMatrix[j][i]] += 4 - atomContainer.getAtom(j).getHydrogenCount();
 
174
            }
 
175
 
 
176
            Iterator bonds = atomContainer.bonds();
 
177
            while (bonds.hasNext()) {
 
178
                IBond bond = (IBond) bonds.next();
 
179
 
 
180
                headAtom = bond.getAtom(0);
 
181
                endAtom = bond.getAtom(1);
 
182
 
 
183
 
 
184
                headAtomPosition = atomContainer.getAtomNumber(headAtom);
 
185
                endAtomPosition = atomContainer.getAtomNumber(endAtom);
 
186
 
 
187
 
 
188
 
 
189
 
 
190
                if (Math.abs(apspMatrix[i][headAtomPosition] - apspMatrix[i][endAtomPosition]) == 1)
 
191
                {
 
192
                    int min = Math.min(apspMatrix[i][headAtomPosition],apspMatrix[i][endAtomPosition]);
 
193
                    interLayerBondSum[min] += bond.getOrder();
 
194
                }
 
195
            }
 
196
 
 
197
 
 
198
            for (int j = 0; j < interLayerBondSum.length; j++)
 
199
            {
 
200
                weightArray[i] += interLayerBondSum[j] * valenceSum[j+1] * Math.pow(10, -(j+1));
 
201
            }
 
202
 
 
203
            logger.debug("valence sum: ");
 
204
            displayArray(valenceSum);
 
205
            logger.debug("inter-layer bond sum: ");
 
206
            displayArray(interLayerBondSum);
 
207
        }
 
208
 
 
209
        logger.debug("weight array: ");
 
210
        displayArray(weightArray);
 
211
 
 
212
        return weightArray;
 
213
    }
 
214
 
 
215
    public static int[] getAtomLayers(int[][]apspMatrix)
 
216
    {
 
217
        int[] atomLayers  = new int[apspMatrix.length];
 
218
        for(int i = 0; i < apspMatrix.length; i++)
 
219
        {
 
220
            atomLayers[i] = 0;
 
221
            for(int j = 0; j < apspMatrix.length; j++)
 
222
            {
 
223
                if(atomLayers[i] < 1+ apspMatrix[j][i] )
 
224
                    atomLayers[i] = 1+ apspMatrix[j][i];
 
225
            }
 
226
 
 
227
        }
 
228
        return atomLayers;
 
229
    }
 
230
 
 
231
    /** Lists a 2D double matrix to the System console */
 
232
    public static void displayMatrix(double[][] matrix){
 
233
        String line;
 
234
        for (int f = 0; f < matrix.length; f++)
 
235
        {
 
236
            line  = "";
 
237
            for (int g = 0; g < matrix.length; g++)
 
238
            {
 
239
                line += matrix[g][f] + " | ";
 
240
            }
 
241
            logger.debug(line);
 
242
        }
 
243
    }
 
244
 
 
245
    /** Lists a 2D int matrix to the System console */
 
246
    public static void displayMatrix(int[][] matrix){
 
247
        String line;
 
248
        for (int f = 0; f < matrix.length; f++)
 
249
        {
 
250
            line  = "";
 
251
            for (int g = 0; g < matrix.length; g++)
 
252
            {
 
253
                line += matrix[g][f] + " | ";
 
254
            }
 
255
            logger.debug(line);
 
256
        }
 
257
    }
 
258
 
 
259
    /** Lists a 1D array to the System console */
 
260
    public static void displayArray(int[] array){
 
261
        String line  = "";
 
262
        for (int f = 0; f < array.length; f++)
 
263
        {
 
264
            line += array[f] + " | ";
 
265
        }
 
266
        logger.debug(line);
 
267
    }
 
268
 
 
269
    /** Lists a 1D array to the System console */
 
270
    public static void displayArray(double[] array){
 
271
        String line  = "";
 
272
        for (int f = 0; f < array.length; f++)
 
273
        {
 
274
            line += array[f] + " | ";
 
275
        }
 
276
        logger.debug(line);
 
277
    }
 
278
 
 
279
}