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); }
32 //determine if matrix is square or lower triangle
33 //if it is square read the distances for the first sequence
35 while((d=filehandle.get()) != EOF){
37 //is d a number meaning its square
40 filehandle.putback(d);
41 for(int i=0;i<numSeqs;i++){
42 filehandle >> matrix[0][i];
47 //is d a line return meaning its lower triangle
55 if (square == true) { readSquareMatrix(filehandle); }
56 else { readLTMatrix(filehandle); }
61 //sort sequences so they are gathered in groups for processing
63 cout << "after sort" << endl;
68 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72 cout << "An unknown error has occurred in the FullMatrix class function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
76 /**************************************************************************/
77 void FullMatrix::readSquareMatrix(ifstream& filehandle) {
81 reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
87 for(int i=1;i<numSeqs;i++){
90 group = groupmap->getGroup(name);
93 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
95 for(int j=0;j<numSeqs;j++){
96 filehandle >> distance;
98 matrix[i][j] = distance;
100 reading->update(count);
106 catch(exception& e) {
107 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
111 cout << "An unknown error has occurred in the FullMatrix class function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
116 /**************************************************************************/
117 void FullMatrix::readLTMatrix(ifstream& filehandle) {
120 reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
126 for(int i=1;i<numSeqs;i++){
129 group = groupmap->getGroup(name);
132 if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
134 for(int j=0;j<i;j++){
135 filehandle >> distance;
137 matrix[i][j] = distance; matrix[j][i] = distance;
139 reading->update(count);
145 catch(exception& e) {
146 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
150 cout << "An unknown error has occurred in the FullMatrix class function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156 /**************************************************************************/
157 void FullMatrix::sortGroups(){
159 //sort each row by group and when you do, swap rows too.
160 for (int i = 0; i < numSeqs; i++) {
161 quicksort(0, numSeqs-1, i);
164 catch(exception& e) {
165 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169 cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174 /**************************************************************************/
175 //this is a version of quicksort taken from http://www.c.happycodings.com/Sorting_Searching/code13.html
176 /* sort everything inbetween `low' <-> `high' */
177 void FullMatrix::quicksort(int low, int high, int row) {
184 //what group does this row belong to
185 string z = index[(low + high) / 2];
189 /* find member above ... */
190 while(index[i] < z) i++;
192 /* find element below ... */
193 while(index[j] > z) j--;
196 /* swap two elements in row*/
198 matrix[row][i] = matrix[row][j];
201 /* swap two elements in column*/
203 matrix[i][row] = matrix[j][row];
213 //cout << "swapping elements " << i << " " << j << endl;
214 //printMatrix(cout); cout << endl;
220 quicksort(low, j, row);
223 quicksort(i, high, row);
225 catch(exception& e) {
226 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function quicksort. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
230 cout << "An unknown error has occurred in the FullMatrix class function quicksort. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235 /**************************************************************************/
236 int FullMatrix::getNumSeqs(){ return numSeqs; }
237 /**************************************************************************/
239 void FullMatrix::printMatrix(ostream& out) {
241 for (int i = 0; i < numSeqs; i++) {
242 out << "row " << i << " group = " << index[i] << endl;
243 for (int j = 0; j < numSeqs; j++) {
244 out << matrix[i][j] << " ";
249 catch(exception& e) {
250 cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254 cout << "An unknown error has occurred in the FullMatrix class function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
259 /**************************************************************************/