File: SymMatrix.h

package info (click to toggle)
clustalx 2.1%2Blgpl-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,868 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 15
file content (297 lines) | stat: -rw-r--r-- 8,237 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
 * Author: Mark Larkin
 * 
 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
 */
/**
 * The SymMatrix class is used to store the distance matrix. It stores it in a double  
 * array.
 * This class throws an out_of_range exception if we try to access an element outside 
 * of the array bounds. It will throw a bad_alloc if we cannot allocate enough memory for 
 * the distance matrix.
 */
#ifndef SYMMATRIX_H
#define SYMMATRIX_H

#include<iostream>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdexcept>
#include <new>
#include <cstdlib>

namespace clustalw
{
using namespace std;

class SymMatrix
{
    public:
        SymMatrix() : elements(0), numSeqs(0), subElements(0), firstSeq(0), numSeqsInSub(0){;}
        SymMatrix(int size)
         : elements(0),
           numSeqs(0),
           subElements(0),
           firstSeq(0),
           numSeqsInSub(0) 
        {
            // Size is the numSeqs + 1
            numSeqs = size - 1;
            sizeElements = ((numSeqs + 1) * (numSeqs + 2)) >> 1;
            try
            {
                elements = new double[sizeElements];
                setAllArrayToVal(elements, sizeElements, 0.0);
            }
            catch(bad_alloc& e)
            {
                cout << "Could not allocate a distance matrix for " << numSeqs 
                     << " seqs.\n";
                throw e;
            }
            
        }
        ~SymMatrix()
        {
            if(elements)
            {
                delete [] elements;
            }
            if(subElements)
            {
                delete [] subElements;
            }
        }
        
        inline void SetAt(int nRow, int nCol, const double& value)
        {
            index = getIndex(nRow, nCol, numSeqs);
            elements[index] = value; 
        }
        
        inline double GetAt(int nRow, int nCol) 
        {
            index = getIndex(nRow, nCol, numSeqs);
            return elements[index];
        }
   
        inline void ResizeRect(int size, double val = 0.0)
        {
            numSeqs = size - 1;
            sizeElements = ((numSeqs + 1) * (numSeqs + 2)) >> 1;
            if(elements)
            {
                delete [] elements;
            }
            try
            {
                elements = new double[sizeElements];
                setAllArrayToVal(elements, sizeElements, val);
            }
            catch(bad_alloc& e)
            {
                cout << "Could not allocate a distance matrix for " << numSeqs 
                     << " seqs. Need to terminate program.\n";
                throw e;
            }            
        }
        
        inline void setAllArrayToVal(double* array, int size, double val)
        {
            for(int i = 0; i < size; i++)
            {
                array[i] = val;
            }
        } 
        
        inline int getSize()
        {
            return numSeqs + 1;
        }

        inline void clearArray()
        {
            sizeElements = 0;
            numSeqs = 0;
            if(elements)
            {
                delete [] elements;
                elements = 0;
            }
            
            numSeqsInSub = 0;
            sizeSubElements = 0;
            
            if(subElements)
            {
                delete [] subElements;
                subElements = 0;                
            }
        }
         
        void printArray()
        {
            printArray(elements, sizeElements);
        }
        
        void printSubArray()
        {
            printArray(subElements, sizeSubElements);
        }
        
        void printArray(double* array, int size)
        {
            int numThisTime = 1;
            int numSoFar = 0;
            for(int i = 0; i < size; i++)
            {
                
                numSoFar++;
                
                if(numSoFar == numThisTime)
                {
                    cout << " " << setw(4) << array[i] << "\n";
                    numSoFar = 0;
                    numThisTime++;
                }
                else
                {
                    cout << " " << setw(4) << array[i];
                }    
            }
            cout << "\n";
        }
        
        inline int getIndex(const int &i, const int &j, const int &nSeqs) const
        {
            if(i == 0 || j == 0)
            {
                return 0;
            }
            
            int _i = i - 1, _j = j - 1;
            if (_i == _j) 
            {
                if ((_i >= nSeqs) || (_i < 0))
                {
                    throw out_of_range("index out of range\n");
                }
                return (_i * (_i + 3)) >> 1;
            }

            if (_i > _j) 
            {
                if ((_i >= nSeqs) || (_j < 0))
                {
                    throw out_of_range("index out of range\n");
                }
                return ((_i * (_i + 1)) >> 1) + _j;
            }

            if ((_j >= nSeqs) || (_i < 0))
            {
                throw out_of_range("index out of range\n");
            }
            return  ((_j * (_j + 1)) >> 1) + _i;            
        }
        
        //inline
        inline double operator() (unsigned row, unsigned col) const
        {
            return elements[getIndex(row, col, numSeqs)];
        }
        
        inline double& operator() (unsigned row, unsigned col)
        {
            return elements[getIndex(row, col, numSeqs)];
        }
        
        inline double* getElements()
        {
            return elements;
        }
        
        inline double* getDistMatrix(int fSeq, int nSeqsInSub)
        {
            if(fSeq == 1 && numSeqs == nSeqsInSub)
            {
                return elements; // We want the whole array.
            }
            else
            {
                // Calculate the subMatrix and return it!!!
                try
                {
                    if(subElements)
                    {
                        delete [] subElements;
                    }
                    sizeSubElements = ((nSeqsInSub + 1) * (nSeqsInSub + 2)) >> 1;
                    numSeqsInSub = nSeqsInSub;                    
                    
                    subElements = new double[sizeSubElements];
                    setAllArrayToVal(subElements, sizeSubElements, 0.0);
                    
                    int currIndex = 0;
                    subElements[0] = 0.0;
                    int lSeq = fSeq + numSeqsInSub - 1; // NOTE this is wrong!!!!! Need to fix

                    for(int i = fSeq; i <= lSeq; i++)
                    {
                        for(int j = i + 1; j <= lSeq; j++)
                        {
                            currIndex = getIndex(i - fSeq + 1, j - fSeq + 1, numSeqsInSub);
                            subElements[currIndex] = elements[getIndex(i, j, numSeqs)];    
                        }
                    }

                    return subElements;
                }
                catch(bad_alloc& e)
                {
                    cout << "Out of Memory!\n";
                    throw e;
                }                
            }
        }
        
        void clearSubArray()
        {
            if(subElements)
            {
                delete [] subElements;
            }
            subElements = 0; 
            sizeSubElements = 0;
            numSeqsInSub = 0;       
        }
        
        void makeSimilarityMatrix()
        {
            double value;
            for (int i = 0; i < numSeqs; i++)
            {
                SetAt(i + 1, i + 1, 0.0);
                for (int j = 0; j < i; j++)
                {
                    value = 100.0 - (GetAt(i + 1, j + 1)) * 100.0;
                    SetAt(i + 1, j + 1, value);
                    //distMat->SetAt(j + 1, i + 1, value);
                }
            }
        }
        
    private:
        double* elements;
        int sizeElements;
        int numSeqs;
        int index;
        double* subElements; // To be used to return a sub matrix.
        int firstSeq, numSeqsInSub;
        int sizeSubElements;
};

}

#endif