]> git.donarmstrong.com Git - mothur.git/blobdiff - fullmatrix.cpp
final fixes for 1.7
[mothur.git] / fullmatrix.cpp
index 6ea2318d740003f8cf46ec7e63815c175e1893ca..5bb2d2336d6ea263a1b0596511abf5f66cb7a248 100644 (file)
@@ -10,6 +10,7 @@
 #include "fullmatrix.h"
 
 /**************************************************************************/
+
 //This constructor reads a distance matrix file and stores the data in the matrix.
 FullMatrix::FullMatrix(ifstream& filehandle) {
        try{
@@ -26,19 +27,22 @@ 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].groupname = group; 
+               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;
                
                //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
                        if(isalnum(d)){ 
                                square = true;
                                filehandle.putback(d);
+                               
                                for(int i=0;i<numSeqs;i++){
                                        filehandle >> matrix[0][i];
                                }
@@ -58,14 +62,23 @@ FullMatrix::FullMatrix(ifstream& 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<numSeqs;i++){
+                       if(index[i].groupName == index[i-1].groupName){ sizes[groupCount]++;    }
+                       else{
+                               sizes.push_back(1);
+                               groups.push_back(index[i].groupName);
+                               groupCount++;
+                       }                               
+               }
+               
        }
        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";
+               errorOut(e, "FullMatrix", "FullMatrix");
                exit(1);
        }
 }
@@ -74,25 +87,24 @@ void 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;
+               
                string group, name;
                
                for(int i=1;i<numSeqs;i++){
                        filehandle >> name;             
                        
                        group = groupmap->getGroup(name);
-                       index[i].groupname = 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); }
+                       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<numSeqs;j++){
-                               filehandle >> distance;
-                                       
-                               matrix[i][j] = distance;
+                               filehandle >> matrix[i][j];
+                               
                                count++;
                                reading->update(count);
                        }
@@ -101,33 +113,29 @@ 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) {
        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;
+
                string group, name;
                
                for(int i=1;i<numSeqs;i++){
                        filehandle >> name;             
                                                
                        group = groupmap->getGroup(name);
-                       index[i].groupname = 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); }
+                       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<i;j++){
                                filehandle >> distance;
@@ -136,41 +144,38 @@ void FullMatrix::readLTMatrix(ifstream& filehandle) {
                                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";
+               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;
+               float y = 0;
                string name;
                
                /* compare value */
                //what group does this row belong to
-               string z = index[(low + high) / 2].groupname;
+               string z = index[(low + high) / 2].groupName;
 
                /* partition */
                do {
                        /* find member above ... */
-                       while(index[i].groupname < z) i++;
+                       while(index[i].groupName < z) i++;
 
                        /* find element below ... */
-                       while(index[j].groupname > z) j--;
+                       while(index[j].groupName > z) j--;
                        
                        if(i <= j) {
                                /* swap rows*/
@@ -188,9 +193,9 @@ void FullMatrix::sortGroups(int low, int high){
                                }
                                
                                //swap map elements
-                               z = index[i].groupname;
-                               index[i].groupname = index[j].groupname;
-                               index[j].groupname = 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;
@@ -212,24 +217,37 @@ void FullMatrix::sortGroups(int low, int 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";
-               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";
+               errorOut(e, "FullMatrix", "sortGroups");
                exit(1);
        }
-
 }
 
 /**************************************************************************/   
-int FullMatrix::getNumSeqs(){ return numSeqs; }
+
+float FullMatrix::get(int i, int j){   return matrix[i][j];            }
+
+/**************************************************************************/   
+
+vector<string> FullMatrix::getGroups(){        return groups;          }
+
+/**************************************************************************/   
+
+vector<int> FullMatrix::getSizes(){    return sizes;           }
+
+/**************************************************************************/   
+
+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] << " ";
                        }
@@ -237,14 +255,10 @@ void FullMatrix::printMatrix(ostream& out) {
                }
        }
        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);
        }
-
 }
+
 /**************************************************************************/