X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=fullmatrix.cpp;h=4451d6645ea8691b941ebbc991f82cc4ce8b4706;hp=66c67b7da0999281af64e9836c9a0f94aff3a698;hb=a0f1fca79d2ddfa7ad36b4485039c68b5704fe8d;hpb=8fd4a602269974e6548f4a387dddd6bd80a999ab diff --git a/fullmatrix.cpp b/fullmatrix.cpp index 66c67b7..4451d66 100644 --- a/fullmatrix.cpp +++ b/fullmatrix.cpp @@ -11,29 +11,29 @@ /**************************************************************************/ //This constructor reads a distance matrix file and stores the data in the matrix. -FullMatrix::FullMatrix(ifstream& filehandle) { +FullMatrix::FullMatrix(ifstream& filehandle, GroupMap* g, bool s) : groupmap(g), sim(s) { try{ - globaldata = GlobalData::getInstance(); - groupmap = globaldata->gGroupmap; + m = MothurOut::getInstance(); - float minSoFar = 0.0; string name, group; - filehandle >> numSeqs >> name; + filehandle >> numSeqs >> name; + //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); } - index[0].groupname = group; + if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); } + index.resize(numSeqs); 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 char d; + bool square; while((d=filehandle.get()) != EOF){ //is d a number meaning its square @@ -41,17 +41,10 @@ FullMatrix::FullMatrix(ifstream& filehandle) { square = true; filehandle.putback(d); - if (numSeqs >= 2) { - //save first distance that is not distance to itself as minimum - filehandle >> matrix[0][0] >> minSoFar; - matrix[0][1] = minSoFar; - } - - for(int i=2;i> matrix[0][i]; - if (matrix[0][i] < minSoFar) { minSoFar = matrix[0][i]; } + if (sim) { matrix[0][i] = 1.0 - matrix[0][i]; } } - index[0].minDist = minSoFar; break; } @@ -61,219 +54,230 @@ FullMatrix::FullMatrix(ifstream& filehandle) { break; } } - + //read rest of matrix - if (square == true) { readSquareMatrix(filehandle); } - else { readLTMatrix(filehandle); } + if (square == true) { readSquareMatrix(filehandle); } + else { readLTMatrix(filehandle); } - //sort sequences so they are gathered in groups for processing - sortGroups(0, numSeqs-1); - + filehandle.close(); + + if (!m->control_pressed) { sortGroups(0, numSeqs-1); } + } catch(exception& e) { - cout << "Standard Error: " << e.what() << " has occurred in the FullMatrix class Function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; - exit(1); - } - catch(...) { - cout << "An unknown error has occurred in the FullMatrix class function FullMatrix. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; + m->errorOut(e, "FullMatrix", "FullMatrix"); exit(1); } } /**************************************************************************/ -void FullMatrix::readSquareMatrix(ifstream& filehandle) { +int FullMatrix::readSquareMatrix(ifstream& filehandle) { try { Progress* reading; - reading = new Progress("Reading matrix: ", numSeqs * numSeqs); + reading = new Progress("Reading matrix: ", numSeqs * numSeqs); int count = 0; - float distance, minSoFar; - minSoFar = 0.0; - string group, name; + 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; - filehandle >> minSoFar; - matrix[i][0] = minSoFar; - - if(group == "not found") { cout << "Error: Sequence '" << name << "' was not found in the group file, please correct." << endl; exit(1); } + if(group == "not found") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); } - for(int j=1;j> distance; + for(int j=0;jcontrol_pressed) { delete reading; return 0; } + + filehandle >> matrix[i][j]; + if (sim) { matrix[i][j] = 1.0 - matrix[i][j]; } - if ((distance < minSoFar) && (i != j)) { minSoFar = distance; } - - matrix[i][j] = distance; count++; reading->update(count); } - - //save minimum value for each row - index[i].minDist = minSoFar; } + + if (m->control_pressed) { delete reading; return 0; } + reading->finish(); delete reading; + + return 0; } 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"; + m->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) { +int FullMatrix::readLTMatrix(ifstream& filehandle) { try { + Progress* reading; - reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2); + reading = new Progress("Reading matrix: ", numSeqs * (numSeqs - 1) / 2); int count = 0; - float distance, minSoFar; - minSoFar = 0.0; + float distance; string group, name; - + for(int i=1;i> name; - + group = groupmap->getGroup(name); - index[i].groupname = group; index[i].seqName = name; - - filehandle >> minSoFar; - matrix[i][0] = minSoFar; + 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") { m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine(); exit(1); } + + for(int j=0;jcontrol_pressed) { delete reading; return 0; } - for(int j=1;j> distance; + if (sim) { distance = 1.0 - distance; } - if (distance < minSoFar) { minSoFar = distance; } - matrix[i][j] = distance; matrix[j][i] = distance; + count++; reading->update(count); } - - //save minimum value for each row - index[i].minDist = minSoFar; } + + if (m->control_pressed) { delete reading; return 0; } + reading->finish(); delete reading; + + return 0; } 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"; + m->errorOut(e, "FullMatrix", "readLTMatrix"); 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(int low, int high){ 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].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"; + m->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) { + m->errorOut(e, "FullMatrix", "swapRows"); exit(1); } - } +/**************************************************************************/ + +float FullMatrix::get(int i, int j){ return matrix[i][j]; } + +/**************************************************************************/ + +vector FullMatrix::getGroups(){ return groups; } + +/**************************************************************************/ + +vector FullMatrix::getSizes(){ return sizes; } /**************************************************************************/ -int FullMatrix::getNumSeqs(){ return numSeqs; } + +int FullMatrix::getNumGroups(){ return groups.size(); } + +/**************************************************************************/ + +int FullMatrix::getNumSeqs(){ return numSeqs; } + /**************************************************************************/ -//print out matrix + void FullMatrix::printMatrix(ostream& out) { try{ for (int i = 0; i < numSeqs; i++) { - out << "row " << i << " group = " << index[i].groupname << " name = " << index[i].seqName << endl; + 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"; + m->errorOut(e, "FullMatrix", "printMatrix"); exit(1); } - } + /**************************************************************************/