4 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.
7 * The SymMatrix class is used to store the distance matrix. It stores it in a double
9 * This class throws an out_of_range exception if we try to access an element outside
10 * of the array bounds. It will throw a bad_alloc if we cannot allocate enough memory for
11 * the distance matrix.
31 SymMatrix() : elements(0), numSeqs(0), subElements(0), firstSeq(0), numSeqsInSub(0){;}
39 // Size is the numSeqs + 1
41 sizeElements = ((numSeqs + 1) * (numSeqs + 2)) >> 1;
44 elements = new double[sizeElements];
45 setAllArrayToVal(elements, sizeElements, 0.0);
49 cout << "Could not allocate a distance matrix for " << numSeqs
63 delete [] subElements;
67 inline void SetAt(int nRow, int nCol, const double& value)
69 index = getIndex(nRow, nCol, numSeqs);
70 elements[index] = value;
73 inline double GetAt(int nRow, int nCol)
75 index = getIndex(nRow, nCol, numSeqs);
76 return elements[index];
79 inline void ResizeRect(int size, double val = 0.0)
82 sizeElements = ((numSeqs + 1) * (numSeqs + 2)) >> 1;
89 elements = new double[sizeElements];
90 setAllArrayToVal(elements, sizeElements, val);
94 cout << "Could not allocate a distance matrix for " << numSeqs
95 << " seqs. Need to terminate program.\n";
100 inline void setAllArrayToVal(double* array, int size, double val)
102 for(int i = 0; i < size; i++)
113 inline void clearArray()
128 delete [] subElements;
135 printArray(elements, sizeElements);
140 printArray(subElements, sizeSubElements);
143 void printArray(double* array, int size)
147 for(int i = 0; i < size; i++)
152 if(numSoFar == numThisTime)
154 cout << " " << setw(4) << array[i] << "\n";
160 cout << " " << setw(4) << array[i];
166 inline int getIndex(const int &i, const int &j, const int &nSeqs) const
173 int _i = i - 1, _j = j - 1;
176 if ((_i >= nSeqs) || (_i < 0))
178 throw out_of_range("index out of range\n");
180 return (_i * (_i + 3)) >> 1;
185 if ((_i >= nSeqs) || (_j < 0))
187 throw out_of_range("index out of range\n");
189 return ((_i * (_i + 1)) >> 1) + _j;
192 if ((_j >= nSeqs) || (_i < 0))
194 throw out_of_range("index out of range\n");
196 return ((_j * (_j + 1)) >> 1) + _i;
200 inline double operator() (unsigned row, unsigned col) const
202 return elements[getIndex(row, col, numSeqs)];
205 inline double& operator() (unsigned row, unsigned col)
207 return elements[getIndex(row, col, numSeqs)];
210 inline double* getElements()
215 inline double* getDistMatrix(int fSeq, int nSeqsInSub)
217 if(fSeq == 1 && numSeqs == nSeqsInSub)
219 return elements; // We want the whole array.
223 // Calculate the subMatrix and return it!!!
228 delete [] subElements;
230 sizeSubElements = ((nSeqsInSub + 1) * (nSeqsInSub + 2)) >> 1;
231 numSeqsInSub = nSeqsInSub;
233 subElements = new double[sizeSubElements];
234 setAllArrayToVal(subElements, sizeSubElements, 0.0);
237 subElements[0] = 0.0;
238 int lSeq = fSeq + numSeqsInSub - 1; // NOTE this is wrong!!!!! Need to fix
240 for(int i = fSeq; i <= lSeq; i++)
242 for(int j = i + 1; j <= lSeq; j++)
244 currIndex = getIndex(i - fSeq + 1, j - fSeq + 1, numSeqsInSub);
245 subElements[currIndex] = elements[getIndex(i, j, numSeqs)];
253 cout << "Out of Memory!\n";
263 delete [] subElements;
270 void makeSimilarityMatrix()
273 for (int i = 0; i < numSeqs; i++)
275 SetAt(i + 1, i + 1, 0.0);
276 for (int j = 0; j < i; j++)
278 value = 100.0 - (GetAt(i + 1, j + 1)) * 100.0;
279 SetAt(i + 1, j + 1, value);
280 //distMat->SetAt(j + 1, i + 1, value);
290 double* subElements; // To be used to return a sub matrix.
291 int firstSeq, numSeqsInSub;