5 * Created by Sarah Westcott on 3/6/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "fullmatrix.h"
12 /**************************************************************************/
13 //This constructor reads a distance matrix file and stores the data in the matrix.
14 FullMatrix::FullMatrix(ifstream& filehandle) {
16 globaldata = GlobalData::getInstance();
17 groupmap = globaldata->gGroupmap;
21 filehandle >> numSeqs >> name;
23 //make the matrix filled with zeros
24 matrix.resize(numSeqs);
25 for(int i = 0; i < numSeqs; i++) {
26 matrix[i].resize(numSeqs, 0);
29 group = groupmap->getGroup(name);
30 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
31 index[0].groupname = group;
32 index[0].seqName = name;
34 //determine if matrix is square or lower triangle
35 //if it is square read the distances for the first sequence
37 while((d=filehandle.get()) != EOF){
39 //is d a number meaning its square
42 filehandle.putback(d);
45 //save first distance that is not distance to itself as minimum
46 filehandle >> matrix[0][0] >> minSoFar;
47 matrix[0][1] = minSoFar;
50 for(int i=2;i<numSeqs;i++){
51 filehandle >> matrix[0][i];
52 if (matrix[0][i] < minSoFar) { minSoFar = matrix[0][i]; }
54 index[0].minDist = minSoFar;
58 //is d a line return meaning its lower triangle
66 if (square == true) { readSquareMatrix(filehandle); }
67 else { readLTMatrix(filehandle); }
69 //sort sequences so they are gathered in groups for processing
70 sortGroups(0, numSeqs-1);
74 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
78 cout << "An unknown error has occurred in the FullMatrix class function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
82 /**************************************************************************/
83 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
87 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
90 float distance, minSoFar;
94 for(int i=1;i<numSeqs;i++){
97 group = groupmap->getGroup(name);
98 index[i].groupname = group;
99 index[i].seqName = name;
101 filehandle >> minSoFar;
102 matrix[i][0] = minSoFar;
104 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
106 for(int j=1;j<numSeqs;j++){
107 filehandle >> distance;
109 if ((distance < minSoFar) && (i != j)) { minSoFar = distance; }
111 matrix[i][j] = distance;
113 reading->update(count);
116 //save minimum value for each row
117 index[i].minDist = minSoFar;
122 catch(exception& e) {
123 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
127 cout << "An unknown error has occurred in the FullMatrix class function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
132 /**************************************************************************/
133 void FullMatrix::readLTMatrix(ifstream& filehandle) {
136 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
139 float distance, minSoFar;
144 for(int i=1;i<numSeqs;i++){
147 group = groupmap->getGroup(name);
148 index[i].groupname = group;
149 index[i].seqName = name;
151 filehandle >> minSoFar;
152 matrix[i][0] = minSoFar;
154 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
156 for(int j=1;j<i;j++){
157 filehandle >> distance;
159 if (distance < minSoFar) { minSoFar = distance; }
161 matrix[i][j] = distance; matrix[j][i] = distance;
163 reading->update(count);
166 //save minimum value for each row
167 index[i].minDist = minSoFar;
172 catch(exception& e) {
173 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177 cout << "An unknown error has occurred in the FullMatrix class function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183 /**************************************************************************/
184 void FullMatrix::sortGroups(int low, int high){
193 //what group does this row belong to
194 string z = index[(low + high) / 2].groupname;
198 /* find member above ... */
199 while(index[i].groupname < z) i++;
201 /* find element below ... */
202 while(index[j].groupname > z) j--;
206 for (int h = 0; h < numSeqs; h++) {
208 matrix[i][h] = matrix[j][h];
213 for (int b = 0; b < numSeqs; b++) {
215 matrix[b][i] = matrix[b][j];
220 z = index[i].groupname;
221 index[i].groupname = index[j].groupname;
222 index[j].groupname = z;
224 name = index[i].seqName;
225 index[i].seqName = index[j].seqName;
226 index[j].seqName = name;
243 catch(exception& e) {
244 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248 cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254 /**************************************************************************/
255 int FullMatrix::getNumSeqs(){ return numSeqs; }
256 /**************************************************************************/
258 void FullMatrix::printMatrix(ostream& out) {
260 for (int i = 0; i < numSeqs; i++) {
261 out << "row " << i << " group = " << index[i].groupname << " name = " << index[i].seqName << endl;
262 for (int j = 0; j < numSeqs; j++) {
263 out << matrix[i][j] << " ";
268 catch(exception& e) {
269 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
273 cout << "An unknown error has occurred in the FullMatrix class function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
278 /**************************************************************************/