]> git.donarmstrong.com Git - mothur.git/blobdiff - readcolumn.cpp
working on pam
[mothur.git] / readcolumn.cpp
index 59c3bd5520929f40086279d744c666ab18fe6d8b..e14125cc6d94f563e37d5ea1cea8235a5c8df04b 100644 (file)
 
 ReadColumnMatrix::ReadColumnMatrix(string df) : distFile(df){
        
-       successOpen = openInputFile(distFile, fileHandle);
+       successOpen = m->openInputFile(distFile, fileHandle);
+       sim = false;
        
 }
+/***********************************************************************/
+
+ReadColumnMatrix::ReadColumnMatrix(string df, bool s) : distFile(df){
+       
+       successOpen = m->openInputFile(distFile, fileHandle);
+       sim = s;
+}
 
 /***********************************************************************/
 
-void ReadColumnMatrix::read(NameAssignment* nameMap){
+int ReadColumnMatrix::read(NameAssignment* nameMap){
        try {           
+
+               string firstName, secondName;
+               float distance;
+               int nseqs = nameMap->size();
+        DMatrix->resize(nseqs);
+               list = new ListVector(nameMap->getListVector());
        
-                       string firstName, secondName;
-                       float distance;
-                       int nseqs = nameMap->size();
+               Progress* reading = new Progress("Reading matrix:     ", nseqs * nseqs);
 
-                       list = new ListVector(nameMap->getListVector());
-               
-                       Progress* reading = new Progress("Reading matrix:     ", nseqs * nseqs);
+               int lt = 1;
+               int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose
+               int refCol = 0; //shows up later - Cell(refCol,refRow).  If it does, then its a square matrix
+
+               //need to see if this is a square or a triangular matrix...
        
-                       int lt = 1;
-                       int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose
-                       int refCol = 0; //shows up later - Cell(refCol,refRow).  If it does, then its a square matrix
+               while(fileHandle && lt == 1){  //let's assume it's a triangular matrix...
+
+               
+                       fileHandle >> firstName; m->gobble(fileHandle);
+            fileHandle >> secondName; m->gobble(fileHandle);
+            fileHandle >> distance;    // get the row and column names and distance
+            
+            if (m->debug) { cout << firstName << '\t' << secondName << '\t' << distance << endl; }
+                       
+                       if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
        
-                       //need to see if this is a square or a triangular matrix...
-                       while(fileHandle && lt == 1){  //let's assume it's a triangular matrix...
+                       map<string,int>::iterator itA = nameMap->find(firstName);
+                       map<string,int>::iterator itB = nameMap->find(secondName);
+
+                       if(itA == nameMap->end()){  m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1);  }
+                       if(itB == nameMap->end()){  m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1);  }
+
+                       if (distance == -1) { distance = 1000000; }
+                       else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
                        
-                               fileHandle >> firstName >> secondName >> distance;      // get the row and column names and distance
-               
-                               if(nameMap->count(firstName)==0){
-                                       cerr << "AError: Sequence '" << firstName << "' was not found in the names file, please correct\n";
+                       if(distance < cutoff && itA != itB){
+                               if(itA->second > itB->second){
+                    PDistCell value(itA->second, distance);
+                    
+                    
+                                       if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
+                                               refRow = itA->second;
+                                               refCol = itB->second;
+                                               DMatrix->addCell(itB->second, value);
+                                       }
+                                       else if(refRow == itA->second && refCol == itB->second){
+                                               lt = 0;
+                                       }
+                                       else{
+                                               DMatrix->addCell(itB->second, value);
+                                       }
                                }
-                               if(nameMap->count(secondName)==0){
-                                       cerr << "AError: Sequence '" << secondName << "' was not found in the names file, please correct\n";
+                               else if(itA->second < itB->second){
+                                       PDistCell value(itB->second, distance);
+                       
+                                       if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
+                                               refRow = itA->second;
+                                               refCol = itB->second;
+                                               DMatrix->addCell(itA->second, value);
+                                       }
+                                       else if(refRow == itB->second && refCol == itA->second){
+                                               lt = 0;
+                                       }
+                                       else{
+                                               DMatrix->addCell(itA->second, value);
+                                       }
                                }
+                               reading->update(itA->second * nseqs);
+                       }
+                       m->gobble(fileHandle);
+               }
+
+               if(lt == 0){  // oops, it was square
+       
+                       fileHandle.close();  //let's start over
+                       DMatrix->clear();  //let's start over
+                  
+                       m->openInputFile(distFile, fileHandle);  //let's start over
+
+                       while(fileHandle){
+                               fileHandle >> firstName; m->gobble(fileHandle);
+                fileHandle >> secondName; m->gobble(fileHandle);
+                fileHandle >> distance;        // get the row and column names and distance
                                
-                               if (distance == -1) { distance = 1000000; }
+                               if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
+               
+                               map<string,int>::iterator itA = nameMap->find(firstName);
+                               map<string,int>::iterator itB = nameMap->find(secondName);
                                
-                               if(distance < cutoff && nameMap->get(firstName) != nameMap->get(secondName)){
-                                       if(nameMap->get(firstName) > nameMap->get(secondName)){
-                                               PCell value(nameMap->get(firstName), nameMap->get(secondName), distance);
+                               if(itA == nameMap->end()){  m->mothurOut("AAError: Sequence '" + firstName + "' was not found in the names file, please correct\n"); exit(1);  }
+                               if(itB == nameMap->end()){  m->mothurOut("ABError: Sequence '" + secondName + "' was not found in the names file, please correct\n"); exit(1);  }
                                
-                                               if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
-                                                       refRow = nameMap->get(firstName);
-                                                       refCol = nameMap->get(secondName);
-                                                       D->addCell(value);
-                                               }
-                                               else if(refRow == nameMap->get(firstName) && refCol == nameMap->get(secondName)){
-                                                       lt = 0;
-                                               }
-                                               else{
-                                                       D->addCell(value);
-                                               }
-                                       }
-                                       else if(nameMap->get(firstName) < nameMap->get(secondName)){
-                                               PCell value(nameMap->get(secondName), nameMap->get(firstName), distance);
+                               if (distance == -1) { distance = 1000000; }
+                               else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
                                
-                                               if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
-                                                       refRow = nameMap->get(firstName);
-                                                       refCol = nameMap->get(secondName);
-                                                       D->addCell(value);
-                                               }
-                                               else if(refRow == nameMap->get(secondName) && refCol == nameMap->get(firstName)){
-                                                       lt = 0;
-                                               }
-                                               else{
-                                                       D->addCell(value);
-                                               }
-                                       }
-                                       reading->update(nameMap->get(firstName) * nseqs);
+                               if(distance < cutoff && itA->second > itB->second){
+                    PDistCell value(itA->second, distance);
+                                       DMatrix->addCell(itB->second, value);
+                                       reading->update(itA->second * nseqs);
                                }
-                               gobble(fileHandle);
+               
+                               m->gobble(fileHandle);
                        }
+               }
+               
+               if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
+               
+               reading->finish();
+               fileHandle.close();
 
-                       if(lt == 0){  // oops, it was square
-                               fileHandle.close();  //let's start over
-                               D->clear();  //let's start over
-                          
-                               openInputFile(distFile, fileHandle);  //let's start over
+               list->setLabel("0");
+               
+               return 1;
+
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ReadColumnMatrix", "read");
+               exit(1);
+       }
+}
+/***********************************************************************/
 
-                               while(fileHandle){
-                                       fileHandle >> firstName >> secondName >> distance;
+int ReadColumnMatrix::read(CountTable* countTable){
+       try {           
+        
+               string firstName, secondName;
+               float distance;
+               int nseqs = countTable->size();
+        
+        DMatrix->resize(nseqs);
+               list = new ListVector(countTable->getListVector());
+        
+               Progress* reading = new Progress("Reading matrix:     ", nseqs * nseqs);
+        
+               int lt = 1;
+               int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose
+               int refCol = 0; //shows up later - Cell(refCol,refRow).  If it does, then its a square matrix
+        
+               //need to see if this is a square or a triangular matrix...
+               
+               while(fileHandle && lt == 1){  //let's assume it's a triangular matrix...
+            
+            
+                       fileHandle >> firstName; m->gobble(fileHandle);
+            fileHandle >> secondName; m->gobble(fileHandle);
+            fileHandle >> distance;    // get the row and column names and distance
+            
+                       if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
+            
+                       int itA = countTable->get(firstName);
+                       int itB = countTable->get(secondName);
+            
+            if (m->control_pressed) { exit(1); }
+            
+                       if (distance == -1) { distance = 1000000; }
+                       else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
                        
-                                       if(nameMap->count(firstName)==0){
-                                               cerr << "BError: Sequence '" << firstName << "' was not found in the names file, please correct\n";
+                       if(distance < cutoff && itA != itB){
+                               if(itA > itB){
+                    PDistCell value(itA, distance);
+                    
+                    
+                                       if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
+                                               refRow = itA;
+                                               refCol = itB;
+                                               DMatrix->addCell(itB, value);
                                        }
-                                       if(nameMap->count(secondName)==0){
-                                               cerr << "BError: Sequence '" << secondName << "' was not found in the names file, please correct\n";
+                                       else if(refRow == itA && refCol == itB){
+                                               lt = 0;
                                        }
-                                       
-                                       if (distance == -1) { distance = 1000000; }
-                                       
-                                       if(distance < cutoff && nameMap->get(firstName) > nameMap->get(secondName)){
-                                               PCell value(nameMap->get(firstName), nameMap->get(secondName), distance);
-                                               D->addCell(value);
-                                               reading->update(nameMap->get(firstName) * nseqs);
+                                       else{
+                                               DMatrix->addCell(itB, value);
+                                       }
+                               }
+                               else if(itA < itB){
+                                       PDistCell value(itB, distance);
+                    
+                                       if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...
+                                               refRow = itA;
+                                               refCol = itB;
+                                               DMatrix->addCell(itA, value);
+                                       }
+                                       else if(refRow == itB && refCol == itA){
+                                               lt = 0;
+                                       }
+                                       else{
+                                               DMatrix->addCell(itA, value);
                                        }
-                       
-                                       gobble(fileHandle);
                                }
+                               reading->update(itA * nseqs);
                        }
-               //      else if(lt == 0){
-               //              while(fileHandle){
-               //                      fileHandle >> firstName >> secondName >> distance;
-               //                      
-               //                      if(nameMap->count(firstName)==0){
-               //                              cerr << "CError: Sequence '" << firstName << "' was not found in the names file, please correct\n";
-               //                      }
-               //                      if(nameMap->count(secondName)==0){
-               //                              cerr << "CError: Sequence '" << secondName << "' was not found in the names file, please correct\n";
-               //                      }
-               //                      if (distance == -1) { distance = 1000000; }
+                       m->gobble(fileHandle);
+               }
+        
+               if(lt == 0){  // oops, it was square
+            
+                       fileHandle.close();  //let's start over
+                       DMatrix->clear();  //let's start over
+            
+                       m->openInputFile(distFile, fileHandle);  //let's start over
+            
+                       while(fileHandle){
+                               fileHandle >> firstName; m->gobble(fileHandle);
+                fileHandle >> secondName; m->gobble(fileHandle);
+                fileHandle >> distance;        // get the row and column names and distance
+                               
+                               if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
+                
+                               int itA = countTable->get(firstName);
+                int itB = countTable->get(secondName);
+                
+                
+                if (m->control_pressed) { exit(1); }
+                               
+                               if (distance == -1) { distance = 1000000; }
+                               else if (sim) { distance = 1.0 - distance;  }  //user has entered a sim matrix that we need to convert.
+                               
+                               if(distance < cutoff && itA > itB){
+                    PDistCell value(itA, distance);
+                                       DMatrix->addCell(itB, value);
+                                       reading->update(itA * nseqs);
+                               }
+                
+                               m->gobble(fileHandle);
+                       }
+               }
                
-               //                      if(distance < cutoff && (*nameMap)[firstName].second < (*nameMap)[secondName].second){
-               ////                            cout << (*nameMap)[secondName] << ' ' << (*nameMap)[firstName] << ' ' << distance << endl;
-               //                              D->addCell(Cell((*nameMap)[secondName].second, (*nameMap)[firstName].second, distance));
-               //                              reading->update((*nameMap)[secondName].second * nseqs);
-               //                      }
-               //
-               //                      gobble(fileHandle);
-               //              }
-               //      }       
-                       reading->finish();
-                       fileHandle.close();
-       
-                       list->setLabel("0");
-       
+               if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
+               
+               reading->finish();
+               fileHandle.close();
+        
+               list->setLabel("0");
+               
+               return 1;
+        
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the ReadColumnMatrix class Function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "ReadColumnMatrix", "read");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the ReadColumnMatrix class function read. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-
 }
 
 /***********************************************************************/
-
-ReadColumnMatrix::~ReadColumnMatrix(){
-       //delete D;
-       //delete list;
-}
-
+ReadColumnMatrix::~ReadColumnMatrix(){}
+/***********************************************************************/