+/**************************************************************************/
+void FullMatrix::readSquareMatrix(ifstream& filehandle) {
+ try {
+
+ Progress* reading;
+ reading = new Progress("Reading matrix: ", numSeqs * numSeqs);
+
+ int count = 0;
+ float distance;
+ string group, name;
+
+ for(int i=1;i<numSeqs;i++){
+ filehandle >> name;
+
+ group = groupmap->getGroup(name);
+ index[i] = group;
+
+ if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
+
+ for(int j=0;j<numSeqs;j++){
+ filehandle >> distance;
+
+ matrix[i][j] = distance;
+ count++;
+ reading->update(count);
+ }
+ }
+ reading->finish();
+ delete reading;
+ }
+ catch(exception& e) {
+ cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+ exit(1);
+ }
+ catch(...) {
+ cout << "An unknown error has occurred in the FullMatrix class function readSquareMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+ exit(1);
+ }
+
+}
+/**************************************************************************/
+void FullMatrix::readLTMatrix(ifstream& filehandle) {
+ try {
+ Progress* reading;
+ reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2);
+
+ int count = 0;
+ float distance;
+ string group, name;
+
+ for(int i=1;i<numSeqs;i++){
+ filehandle >> name;
+
+ group = groupmap->getGroup(name);
+ index[i] = group;
+
+ if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); }
+
+ for(int j=0;j<i;j++){
+ filehandle >> distance;
+
+ matrix[i][j] = distance; matrix[j][i] = distance;
+ count++;
+ reading->update(count);
+ }
+ }
+ reading->finish();
+ delete reading;
+ }
+ catch(exception& e) {
+ cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+ exit(1);
+ }
+ catch(...) {
+ cout << "An unknown error has occurred in the FullMatrix class function readLTMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+ exit(1);
+ }
+
+}
+
+/**************************************************************************/
+void FullMatrix::sortGroups(){
+ try{
+ //sort each row by group and when you do, swap rows too.
+ for (int i = 0; i < numSeqs; i++) {
+ quicksort(0, numSeqs-1, i);
+ }
+ }
+ catch(exception& e) {
+ cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+ exit(1);
+ }
+ catch(...) {
+ cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+ exit(1);
+ }
+
+}
+/**************************************************************************/
+//this is a version of quicksort taken from http://www.c.happycodings.com/Sorting_Searching/code13.html
+/* sort everything inbetween `low' <-> `high' */
+void FullMatrix::quicksort(int low, int high, int row) {
+ try {
+ int i = low;
+ int j = high;
+ int y = 0;
+
+ /* compare value */
+ //what group does this row belong to
+ string z = index[(low + high) / 2];
+
+ /* partition */
+ do {
+ /* find member above ... */
+ while(index[i] < z) i++;
+
+ /* find element below ... */
+ while(index[j] > z) j--;
+
+ if(i <= j) {
+ /* swap two elements in row*/
+ y = matrix[row][i];
+ matrix[row][i] = matrix[row][j];
+ matrix[row][j] = y;
+
+ /* swap two elements in column*/
+ y = matrix[i][row];
+ matrix[i][row] = matrix[j][row];
+ matrix[j][row] = y;
+
+ //swap map elements
+ z = index[i];
+ index[i] = index[j];
+ index[j] = z;
+
+ i++;
+ j--;
+//cout << "swapping elements " << i << " " << j << endl;
+//printMatrix(cout); cout << endl;
+ }
+ } while(i <= j);
+
+ /* recurse */
+ if(low < j)
+ quicksort(low, j, row);
+
+ if(i < high)
+ quicksort(i, high, row);
+ }
+ catch(exception& e) {
+ cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function quicksort. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+ exit(1);
+ }
+ catch(...) {
+ cout << "An unknown error has occurred in the FullMatrix class function quicksort. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+ exit(1);
+ }
+}
+