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;
20 filehandle >> numSeqs >> name;
22 //make the matrix filled with zeros
23 matrix.resize(numSeqs);
24 for(int i = 0; i < numSeqs; i++) {
25 matrix[i].resize(numSeqs, 0);
28 group = groupmap->getGroup(name);
29 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
30 index[0].groupname = group;
31 index[0].seqName = name;
33 //determine if matrix is square or lower triangle
34 //if it is square read the distances for the first sequence
36 while((d=filehandle.get()) != EOF){
38 //is d a number meaning its square
41 filehandle.putback(d);
42 for(int i=0;i<numSeqs;i++){
43 filehandle >> matrix[0][i];
48 //is d a line return meaning its lower triangle
56 if (square == true) { readSquareMatrix(filehandle); }
57 else { readLTMatrix(filehandle); }
62 //sort sequences so they are gathered in groups for processing
63 sortGroups(0, numSeqs-1);
64 cout << "after sort" << endl;
69 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
73 cout << "An unknown error has occurred in the FullMatrix class function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
77 /**************************************************************************/
78 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
82 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
88 for(int i=1;i<numSeqs;i++){
91 group = groupmap->getGroup(name);
92 index[i].groupname = group;
93 index[i].seqName = name;
95 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
97 for(int j=0;j<numSeqs;j++){
98 filehandle >> distance;
100 matrix[i][j] = distance;
102 reading->update(count);
108 catch(exception& e) {
109 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
113 cout << "An unknown error has occurred in the FullMatrix class function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
118 /**************************************************************************/
119 void FullMatrix::readLTMatrix(ifstream& filehandle) {
122 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
128 for(int i=1;i<numSeqs;i++){
131 group = groupmap->getGroup(name);
132 index[i].groupname = group;
133 index[i].seqName = name;
135 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
137 for(int j=0;j<i;j++){
138 filehandle >> distance;
140 matrix[i][j] = distance; matrix[j][i] = distance;
142 reading->update(count);
148 catch(exception& e) {
149 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
153 cout << "An unknown error has occurred in the FullMatrix class function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
159 /**************************************************************************/
160 void FullMatrix::sortGroups(int low, int high){
169 //what group does this row belong to
170 string z = index[(low + high) / 2].groupname;
174 /* find member above ... */
175 while(index[i].groupname < z) i++;
177 /* find element below ... */
178 while(index[j].groupname > z) j--;
182 for (int h = 0; h < numSeqs; h++) {
184 matrix[i][h] = matrix[j][h];
189 for (int b = 0; b < numSeqs; b++) {
191 matrix[b][i] = matrix[b][j];
196 z = index[i].groupname;
197 index[i].groupname = index[j].groupname;
198 index[j].groupname = z;
200 name = index[i].seqName;
201 index[i].seqName = index[j].seqName;
202 index[j].seqName = name;
207 cout << "swapping rows " << i << " " << j << endl;
208 printMatrix(cout); cout << endl;
221 catch(exception& e) {
222 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
226 cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232 /**************************************************************************/
233 int FullMatrix::getNumSeqs(){ return numSeqs; }
234 /**************************************************************************/
236 void FullMatrix::printMatrix(ostream& out) {
238 for (int i = 0; i < numSeqs; i++) {
239 out << "row " << i << " group = " << index[i].groupname << " name = " << index[i].seqName << endl;
240 for (int j = 0; j < numSeqs; j++) {
241 out << matrix[i][j] << " ";
246 catch(exception& e) {
247 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
251 cout << "An unknown error has occurred in the FullMatrix class function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
256 /**************************************************************************/