X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=fullmatrix.cpp;h=5612041528a00e9e0e441422662c6d3738bcb07c;hb=9354b70bb84f6dd52ff4a1955754bcbf7edeedbf;hp=bc53b7db926cca543449fe58b5182d7d1ed6af67;hpb=197c6d3303439582502840980d6a85cf3aab2314;p=mothur.git diff --git a/fullmatrix.cpp b/fullmatrix.cpp index bc53b7d..5612041 100644 --- a/fullmatrix.cpp +++ b/fullmatrix.cpp @@ -10,7 +10,6 @@ #include "fullmatrix.h" /**************************************************************************/ - //This constructor reads a distance matrix file and stores the data in the matrix. FullMatrix::FullMatrix(ifstream& filehandle) { try{ @@ -23,14 +22,13 @@ FullMatrix::FullMatrix(ifstream& filehandle) { //make the matrix filled with zeros matrix.resize(numSeqs); for(int i = 0; i < numSeqs; i++) { - matrix[i].resize(numSeqs, 0); + matrix[i].resize(numSeqs, 0.0); } - group = groupmap->getGroup(name); - if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); } + if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); } index.resize(numSeqs); - index[0].groupName = group; index[0].seqName = name; + index[0].groupName = group; //determine if matrix is square or lower triangle //if it is square read the distances for the first sequence @@ -60,29 +58,11 @@ FullMatrix::FullMatrix(ifstream& filehandle) { if (square == true) { readSquareMatrix(filehandle); } else { readLTMatrix(filehandle); } - //sort sequences so they are gathered in groups for processing - sortGroups(0, numSeqs-1); - - groups.push_back(index[0].groupName); - sizes.push_back(1); - int groupCount = 0; - - for(int i=1;i> name; group = groupmap->getGroup(name); - index[i].groupName = group; index[i].seqName = name; + index[i].groupName = group; - if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); } + if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); } for(int j=0;j> matrix[i][j]; @@ -117,14 +97,9 @@ void FullMatrix::readSquareMatrix(ifstream& filehandle) { 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"; + errorOut(e, "FullMatrix", "readSquareMatrix"); 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) { @@ -136,111 +111,113 @@ void FullMatrix::readLTMatrix(ifstream& filehandle) { float distance; string group, name; - + for(int i=1;i> name; - + group = groupmap->getGroup(name); - index[i].groupName = group; index[i].seqName = name; + index[i].groupName = group; - if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); } + if(group == "not found") { mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); mothurOutEndLine(); exit(1); } for(int j=0;j> 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"; + errorOut(e, "FullMatrix", "readLTMatrix"); exit(1); } - } /**************************************************************************/ void FullMatrix::sortGroups(int low, int high){ try{ - - int i = low; - int j = high; - float y = 0; - string name; - /* compare value */ - //what group does this row belong to - string z = index[(low + high) / 2].groupName; - - /* partition */ - do { - /* find member above ... */ - while(index[i].groupName < z) i++; - - /* find element below ... */ - while(index[j].groupName > z) j--; + if (low < high) { + int i = low+1; + int j = high; + int pivot = (low+high) / 2; - if(i <= j) { - /* swap rows*/ - for (int h = 0; h < numSeqs; h++) { - y = matrix[i][h]; - matrix[i][h] = matrix[j][h]; - matrix[j][h] = y; - } + swapRows(low, pivot); //puts pivot in final spot + + /* compare value */ + //what group does this row belong to + string key = index[low].groupName; + + /* partition */ + while(i <= j) { + /* find member above ... */ + while((i <= high) && (index[i].groupName <= key)) { i++; } - /* swap columns*/ - for (int b = 0; b < numSeqs; b++) { - y = matrix[b][i]; - matrix[b][i] = matrix[b][j]; - matrix[b][j] = y; + /* find element below ... */ + while((j >= low) && (index[j].groupName > key)) { j--; } + + if(i < j) { + swapRows(i, j); } - - //swap map elements - z = index[i].groupName; - index[i].groupName = index[j].groupName; - index[j].groupName = z; - - name = index[i].seqName; - index[i].seqName = index[j].seqName; - index[j].seqName = name; - - - i++; - j--; - } - } while(i <= j); - - /* recurse */ - if(low < j) - sortGroups(low, j); - - if(i < high) - sortGroups(i, high); - + } + + swapRows(low, j); + + /* recurse */ + sortGroups(low, j-1); + sortGroups(j+1, high); + } } 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"; + errorOut(e, "FullMatrix", "sortGroups"); 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"; +} + +/**************************************************************************/ +void FullMatrix::swapRows(int i, int j) { + try { + + float y; + string z, name; + + /* swap rows*/ + for (int h = 0; h < numSeqs; h++) { + y = matrix[i][h]; + matrix[i][h] = matrix[j][h]; + matrix[j][h] = y; + } + + /* swap columns*/ + for (int b = 0; b < numSeqs; b++) { + y = matrix[b][i]; + matrix[b][i] = matrix[b][j]; + matrix[b][j] = y; + } + + //swap map elements + z = index[i].groupName; + index[i].groupName = index[j].groupName; + index[j].groupName = z; + + name = index[i].seqName; + index[i].seqName = index[j].seqName; + index[j].seqName = name; + + + } + catch(exception& e) { + errorOut(e, "FullMatrix", "swapRows"); exit(1); } - } - /**************************************************************************/ float FullMatrix::get(int i, int j){ return matrix[i][j]; } @@ -268,20 +245,17 @@ void FullMatrix::printMatrix(ostream& out) { for (int i = 0; i < numSeqs; i++) { out << "row " << i << " group = " << index[i].groupName << " name = " << index[i].seqName << endl; for (int j = 0; j < numSeqs; j++) { - out << matrix[i][j] << " "; + out << i << '\t' << j << '\t' << matrix[i][j] << endl; } out << endl; } + + for (int i = 0; i < numSeqs; i++) { out << i << '\t' << index[i].seqName << endl; } } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the FullMatrix class function printMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + errorOut(e, "FullMatrix", "printMatrix"); exit(1); } - } /**************************************************************************/