5 * Created by Sarah Westcott on 3/6/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "fullmatrix.h"
12 /**************************************************************************/
14 //This constructor reads a distance matrix file and stores the data in the matrix.
15 FullMatrix::FullMatrix(ifstream& filehandle) {
17 globaldata = GlobalData::getInstance();
18 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.resize(numSeqs);
32 index[0].groupName = group;
33 index[0].seqName = name;
35 //determine if matrix is square or lower triangle
36 //if it is square read the distances for the first sequence
39 while((d=filehandle.get()) != EOF){
41 //is d a number meaning its square
44 filehandle.putback(d);
46 for(int i=0;i<numSeqs;i++){
47 filehandle >> matrix[0][i];
52 //is d a line return meaning its lower triangle
60 if (square == true) { readSquareMatrix(filehandle); }
61 else { readLTMatrix(filehandle); }
63 //sort sequences so they are gathered in groups for processing
64 sortGroups(0, numSeqs-1);
66 groups.push_back(index[0].groupName);
70 for(int i=1;i<numSeqs;i++){
71 if(index[i].groupName == index[i-1].groupName){ sizes[groupCount]++; }
74 groups.push_back(index[i].groupName);
81 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
85 cout << "An unknown error has occurred in the FullMatrix class function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
89 /**************************************************************************/
90 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
94 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
100 for(int i=1;i<numSeqs;i++){
103 group = groupmap->getGroup(name);
104 index[i].groupName = group;
105 index[i].seqName = name;
107 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
109 for(int j=0;j<numSeqs;j++){
110 filehandle >> matrix[i][j];
113 reading->update(count);
119 catch(exception& e) {
120 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124 cout << "An unknown error has occurred in the FullMatrix class function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
129 /**************************************************************************/
130 void FullMatrix::readLTMatrix(ifstream& filehandle) {
133 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
140 for(int i=1;i<numSeqs;i++){
143 group = groupmap->getGroup(name);
144 index[i].groupName = group;
145 index[i].seqName = name;
147 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
149 for(int j=0;j<i;j++){
150 filehandle >> distance;
152 matrix[i][j] = distance; matrix[j][i] = distance;
154 reading->update(count);
161 catch(exception& e) {
162 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
166 cout << "An unknown error has occurred in the FullMatrix class function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
172 /**************************************************************************/
174 void FullMatrix::sortGroups(int low, int high){
183 //what group does this row belong to
184 string z = index[(low + high) / 2].groupName;
188 /* find member above ... */
189 while(index[i].groupName < z) i++;
191 /* find element below ... */
192 while(index[j].groupName > z) j--;
196 for (int h = 0; h < numSeqs; h++) {
198 matrix[i][h] = matrix[j][h];
203 for (int b = 0; b < numSeqs; b++) {
205 matrix[b][i] = matrix[b][j];
210 z = index[i].groupName;
211 index[i].groupName = index[j].groupName;
212 index[j].groupName = z;
214 name = index[i].seqName;
215 index[i].seqName = index[j].seqName;
216 index[j].seqName = name;
233 catch(exception& e) {
234 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
238 cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 /**************************************************************************/
246 float FullMatrix::get(int i, int j){ return matrix[i][j]; }
248 /**************************************************************************/
250 vector<string> FullMatrix::getGroups(){ return groups; }
252 /**************************************************************************/
254 vector<int> FullMatrix::getSizes(){ return sizes; }
256 /**************************************************************************/
258 int FullMatrix::getNumGroups(){ return groups.size(); }
260 /**************************************************************************/
262 int FullMatrix::getNumSeqs(){ return numSeqs; }
264 /**************************************************************************/
266 void FullMatrix::printMatrix(ostream& out) {
268 for (int i = 0; i < numSeqs; i++) {
269 out << "row " << i << " group = " << index[i].groupName << " name = " << index[i].seqName << endl;
270 for (int j = 0; j < numSeqs; j++) {
271 out << matrix[i][j] << " ";
276 catch(exception& e) {
277 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
281 cout << "An unknown error has occurred in the FullMatrix class function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 /**************************************************************************/