From 3659285c05daa94306a7b61dd97ec945b23d472b Mon Sep 17 00:00:00 2001 From: westcott Date: Mon, 9 Mar 2009 16:36:18 +0000 Subject: [PATCH] fixed bug with line and label parameters and added all as option for both --- fullmatrix.cpp | 89 ++++++++++++++++++++++++-------------------------- fullmatrix.h | 11 +++++-- globaldata.cpp | 13 ++++---- 3 files changed, 57 insertions(+), 56 deletions(-) diff --git a/fullmatrix.cpp b/fullmatrix.cpp index c61109c..64d7b6a 100644 --- a/fullmatrix.cpp +++ b/fullmatrix.cpp @@ -27,7 +27,8 @@ FullMatrix::FullMatrix(ifstream& filehandle) { group = groupmap->getGroup(name); if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); } - index[0] = group; + index[0].groupname = group; + index[0].seqName = name; //determine if matrix is square or lower triangle //if it is square read the distances for the first sequence @@ -59,7 +60,7 @@ FullMatrix::FullMatrix(ifstream& filehandle) { printMatrix(cout); //sort sequences so they are gathered in groups for processing - sortGroups(); + sortGroups(0, numSeqs-1); cout << "after sort" << endl; printMatrix(cout); @@ -88,7 +89,8 @@ void FullMatrix::readSquareMatrix(ifstream& filehandle) { filehandle >> name; group = groupmap->getGroup(name); - index[i] = group; + index[i].groupname = group; + index[i].seqName = name; if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); } @@ -127,7 +129,8 @@ void FullMatrix::readLTMatrix(ifstream& filehandle) { filehandle >> name; group = groupmap->getGroup(name); - index[i] = group; + index[i].groupname = group; + index[i].seqName = name; if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); } @@ -154,82 +157,76 @@ void FullMatrix::readLTMatrix(ifstream& filehandle) { } /**************************************************************************/ -void FullMatrix::sortGroups(){ +void FullMatrix::sortGroups(int low, int high){ 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; + string name; /* compare value */ //what group does this row belong to - string z = index[(low + high) / 2]; + string z = index[(low + high) / 2].groupname; /* partition */ do { /* find member above ... */ - while(index[i] < z) i++; + while(index[i].groupname < z) i++; /* find element below ... */ - while(index[j] > z) j--; + while(index[j].groupname > 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 rows*/ + for (int h = 0; h < numSeqs; h++) { + y = matrix[i][h]; + matrix[i][h] = matrix[j][h]; + matrix[j][h] = y; + } - /* swap two elements in column*/ - y = matrix[i][row]; - matrix[i][row] = matrix[j][row]; - matrix[j][row] = 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]; - index[i] = index[j]; - index[j] = z; + 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--; -//cout << "swapping elements " << i << " " << j << endl; -//printMatrix(cout); cout << endl; +cout << "swapping rows " << i << " " << j << endl; +printMatrix(cout); cout << endl; } } while(i <= j); /* recurse */ if(low < j) - quicksort(low, j, row); + sortGroups(low, j); if(i < high) - quicksort(i, high, row); + sortGroups(i, high); + + } 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"; + 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 quicksort. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + cout << "An unknown error has occurred in the FullMatrix class function sortGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; exit(1); } + } /**************************************************************************/ @@ -239,7 +236,7 @@ int FullMatrix::getNumSeqs(){ return numSeqs; } void FullMatrix::printMatrix(ostream& out) { try{ for (int i = 0; i < numSeqs; i++) { - out << "row " << i << " group = " << index[i] << endl; + out << "row " << i << " group = " << index[i].groupname << " name = " << index[i].seqName << endl; for (int j = 0; j < numSeqs; j++) { out << matrix[i][j] << " "; } diff --git a/fullmatrix.h b/fullmatrix.h index f3ad0e7..0ab1d71 100644 --- a/fullmatrix.h +++ b/fullmatrix.h @@ -16,6 +16,12 @@ using namespace std; +struct Names { + string groupname; + string seqName; +}; + + class FullMatrix { public: @@ -27,12 +33,11 @@ class FullMatrix { void printMatrix(ostream&); private: - void sortGroups(); //this function sorts the sequences within the matrix. - void quicksort(int, int, int);//row of matrix, low, high and row number + void sortGroups(int, int); //this function sorts the sequences within the matrix. void readSquareMatrix(ifstream&); void readLTMatrix(ifstream&); vector< vector > matrix; //a 2D distance matrix of all the sequences and their distances to eachother. - map index; // row in vector, sequence group. need to know this so when we sort it can be updated. + map index; // row in vector, sequence group. need to know this so when we sort it can be updated. GroupMap* groupmap; //maps sequences to groups they belong to. GlobalData* globaldata; int numSeqs; diff --git a/globaldata.cpp b/globaldata.cpp index 3ddd02e..17b4908 100644 --- a/globaldata.cpp +++ b/globaldata.cpp @@ -20,7 +20,6 @@ GlobalData* GlobalData::getInstance() { //This function parses through the option string of the command to remove its parameters void GlobalData::parseGlobalData(string commandString, string optionText){ try { - allLines = 1; commandName = commandString; //save command name to be used by other classes //set all non filename paramters to default @@ -32,6 +31,7 @@ void GlobalData::parseGlobalData(string commandString, string optionText){ gGroupmap = NULL; gTree.clear(); labels.clear(); lines.clear(); groups.clear(); + allLines = 1; } //saves help request @@ -122,16 +122,17 @@ void GlobalData::parseGlobalData(string commandString, string optionText){ lines.clear(); line = value; label = ""; - splitAtDash(value, lines); - allLines = 0; + if (line != "all") { splitAtDash(value, lines); allLines = 0; } + else { allLines = 1; } } if (key == "label") {//stores lines to be used in a vector labels.clear(); label = value; line = ""; - splitAtDash(value, labels); - allLines = 0; + if (label != "all") { splitAtDash(value, labels); allLines = 0; } + else { allLines = 1; } } + if (key == "groups") {//stores groups to be used in a vector Groups.clear(); groups = value; @@ -274,8 +275,6 @@ void GlobalData::reset() { cutoff = "10.00"; precision = "100"; iters = "1000"; - line = ""; - label = ""; groups = ""; jumble = "1"; //0 means don't jumble, 1 means jumble. randomtree = ""; //"" means user will enter some user trees, "outputfile" means they just want the random tree distribution to be outputted to outputfile. -- 2.39.2