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); }
59 //sort sequences so they are gathered in groups for processing
60 sortGroups(0, numSeqs-1);
64 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
68 cout << "An unknown error has occurred in the FullMatrix class function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72 /**************************************************************************/
73 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
77 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
83 for(int i=1;i<numSeqs;i++){
86 group = groupmap->getGroup(name);
87 index[i].groupname = group;
88 index[i].seqName = name;
90 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
92 for(int j=0;j<numSeqs;j++){
93 filehandle >> distance;
95 matrix[i][j] = distance;
97 reading->update(count);
103 catch(exception& e) {
104 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
108 cout << "An unknown error has occurred in the FullMatrix class function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
113 /**************************************************************************/
114 void FullMatrix::readLTMatrix(ifstream& filehandle) {
117 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
123 for(int i=1;i<numSeqs;i++){
126 group = groupmap->getGroup(name);
127 index[i].groupname = group;
128 index[i].seqName = name;
130 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
132 for(int j=0;j<i;j++){
133 filehandle >> distance;
135 matrix[i][j] = distance; matrix[j][i] = distance;
137 reading->update(count);
143 catch(exception& e) {
144 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148 cout << "An unknown error has occurred in the FullMatrix class function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
154 /**************************************************************************/
155 void FullMatrix::sortGroups(int low, int high){
164 //what group does this row belong to
165 string z = index[(low + high) / 2].groupname;
169 /* find member above ... */
170 while(index[i].groupname < z) i++;
172 /* find element below ... */
173 while(index[j].groupname > z) j--;
177 for (int h = 0; h < numSeqs; h++) {
179 matrix[i][h] = matrix[j][h];
184 for (int b = 0; b < numSeqs; b++) {
186 matrix[b][i] = matrix[b][j];
191 z = index[i].groupname;
192 index[i].groupname = index[j].groupname;
193 index[j].groupname = z;
195 name = index[i].seqName;
196 index[i].seqName = index[j].seqName;
197 index[j].seqName = name;
214 catch(exception& e) {
215 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
219 cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
225 /**************************************************************************/
226 int FullMatrix::getNumSeqs(){ return numSeqs; }
227 /**************************************************************************/
229 void FullMatrix::printMatrix(ostream& out) {
231 for (int i = 0; i < numSeqs; i++) {
232 out << "row " << i << " group = " << index[i].groupname << " name = " << index[i].seqName << endl;
233 for (int j = 0; j < numSeqs; j++) {
234 out << matrix[i][j] << " ";
239 catch(exception& e) {
240 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 cout << "An unknown error has occurred in the FullMatrix class function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
249 /**************************************************************************/