]> git.donarmstrong.com Git - mothur.git/blobdiff - readcolumn.cpp
working on pam
[mothur.git] / readcolumn.cpp
index d58b0377cb48d1964b21479ca64278919dbf8cca..e14125cc6d94f563e37d5ea1cea8235a5c8df04b 100644 (file)
-/*\r
- *  readcolumn.cpp\r
- *  Mothur\r
- *\r
- *  Created by Sarah Westcott on 4/21/09.\r
- *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.\r
- *\r
- */\r
-\r
-#include "readcolumn.h"\r
-#include "progress.hpp"\r
-\r
-/***********************************************************************/\r
-\r
-ReadColumnMatrix::ReadColumnMatrix(string df) : distFile(df){\r
-       \r
-       successOpen = openInputFile(distFile, fileHandle);\r
-       \r
-}\r
-\r
-/***********************************************************************/\r
-\r
-int ReadColumnMatrix::read(NameAssignment* nameMap){\r
-       try {           \r
-\r
-               string firstName, secondName;\r
-               float distance;\r
-               int nseqs = nameMap->size();\r
-\r
-               list = new ListVector(nameMap->getListVector());\r
-       \r
-               Progress* reading = new Progress("Reading matrix:     ", nseqs * nseqs);\r
-\r
-               int lt = 1;\r
-               int refRow = 0; //we'll keep track of one cell - Cell(refRow,refCol) - and see if it's transpose\r
-               int refCol = 0; //shows up later - Cell(refCol,refRow).  If it does, then its a square matrix\r
-\r
-               //need to see if this is a square or a triangular matrix...\r
-       \r
-               while(fileHandle && lt == 1){  //let's assume it's a triangular matrix...\r
-\r
-               \r
-                       fileHandle >> firstName >> secondName >> distance;      // get the row and column names and distance\r
-                       \r
-                       if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }\r
-       \r
-                       map<string,int>::iterator itA = nameMap->find(firstName);\r
-                       map<string,int>::iterator itB = nameMap->find(secondName);\r
-                       \r
-                       if(itA == nameMap->end()){\r
-                               cerr << "AAError: Sequence '" << firstName << "' was not found in the names file, please correct\n"; exit(1);\r
-                       }\r
-                       if(itB == nameMap->end()){\r
-                               cerr << "ABError: Sequence '" << secondName << "' was not found in the names file, please correct\n"; exit(1);\r
-                       }\r
-\r
-                       if (distance == -1) { distance = 1000000; }\r
-                       \r
-                       if(distance < cutoff && itA != itB){\r
-                               if(itA->second > itB->second){\r
-                                       PCell value(itA->second, itB->second, distance);\r
-                       \r
-                                       if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...\r
-                                               refRow = itA->second;\r
-                                               refCol = itB->second;\r
-                                               D->addCell(value);\r
-                                       }\r
-                                       else if(refRow == itA->second && refCol == itB->second){\r
-                                               lt = 0;\r
-                                       }\r
-                                       else{\r
-                                               D->addCell(value);\r
-                                       }\r
-                               }\r
-                               else if(itA->second < itB->second){\r
-                                       PCell value(itB->second, itA->second, distance);\r
-                       \r
-                                       if(refRow == refCol){           // in other words, if we haven't loaded refRow and refCol...\r
-                                               refRow = itA->second;\r
-                                               refCol = itB->second;\r
-                                               D->addCell(value);\r
-                                       }\r
-                                       else if(refRow == itB->second && refCol == itA->second){\r
-                                               lt = 0;\r
-                                       }\r
-                                       else{\r
-                                               D->addCell(value);\r
-                                       }\r
-                               }\r
-                               reading->update(itA->second * nseqs);\r
-                       }\r
-                       gobble(fileHandle);\r
-               }\r
-\r
-               if(lt == 0){  // oops, it was square\r
-                       fileHandle.close();  //let's start over\r
-                       D->clear();  //let's start over\r
-                  \r
-                       openInputFile(distFile, fileHandle);  //let's start over\r
-\r
-                       while(fileHandle){\r
-                               fileHandle >> firstName >> secondName >> distance;\r
-                               \r
-                               if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }\r
-               \r
-                               map<string,int>::iterator itA = nameMap->find(firstName);\r
-                               map<string,int>::iterator itB = nameMap->find(secondName);\r
-                               \r
-                               if(itA == nameMap->end()){\r
-                                       cerr << "BError: Sequence '" << firstName << "' was not found in the names file, please correct\n";\r
-                               }\r
-                               if(itB == nameMap->end()){\r
-                                       cerr << "BError: Sequence '" << secondName << "' was not found in the names file, please correct\n";\r
-                               }\r
-                               \r
-                               if (distance == -1) { distance = 1000000; }\r
-                               \r
-                               if(distance < cutoff && itA->second > itB->second){\r
-                                       PCell value(itA->second, itB->second, distance);\r
-                                       D->addCell(value);\r
-                                       reading->update(itA->second * nseqs);\r
-                               }\r
-               \r
-                               gobble(fileHandle);\r
-                       }\r
-               }\r
-               \r
-               if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }\r
-               \r
-               reading->finish();\r
-               fileHandle.close();\r
-\r
-               list->setLabel("0");\r
-               \r
-               return 1;\r
-\r
-       }\r
-       catch(exception& e) {\r
-               m->errorOut(e, "ReadColumnMatrix", "read");\r
-               exit(1);\r
-       }\r
-}\r
-\r
-/***********************************************************************/\r
-\r
-ReadColumnMatrix::~ReadColumnMatrix(){\r
-       //delete D;\r
-       //delete list;\r
-}\r
-\r
-\r
+/*
+ *  readcolumn.cpp
+ *  Mothur
+ *
+ *  Created by Sarah Westcott on 4/21/09.
+ *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
+ *
+ */
+
+#include "readcolumn.h"
+#include "progress.hpp"
+
+/***********************************************************************/
+
+ReadColumnMatrix::ReadColumnMatrix(string df) : distFile(df){
+       
+       successOpen = m->openInputFile(distFile, fileHandle);
+       sim = false;
+       
+}
+/***********************************************************************/
+
+ReadColumnMatrix::ReadColumnMatrix(string df, bool s) : distFile(df){
+       
+       successOpen = m->openInputFile(distFile, fileHandle);
+       sim = s;
+}
+
+/***********************************************************************/
+
+int ReadColumnMatrix::read(NameAssignment* nameMap){
+       try {           
+
+               string firstName, secondName;
+               float distance;
+               int nseqs = nameMap->size();
+        DMatrix->resize(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...
+       
+               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; }
+       
+                       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.
+                       
+                       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);
+                                       }
+                               }
+                               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 (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(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.
+                               
+                               if(distance < cutoff && itA->second > itB->second){
+                    PDistCell value(itA->second, distance);
+                                       DMatrix->addCell(itB->second, value);
+                                       reading->update(itA->second * nseqs);
+                               }
+               
+                               m->gobble(fileHandle);
+                       }
+               }
+               
+               if (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
+               
+               reading->finish();
+               fileHandle.close();
+
+               list->setLabel("0");
+               
+               return 1;
+
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ReadColumnMatrix", "read");
+               exit(1);
+       }
+}
+/***********************************************************************/
+
+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(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);
+                                       }
+                                       else if(refRow == itA && refCol == itB){
+                                               lt = 0;
+                                       }
+                                       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);
+                                       }
+                               }
+                               reading->update(itA * 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 (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 (m->control_pressed) {  fileHandle.close();  delete reading; return 0; }
+               
+               reading->finish();
+               fileHandle.close();
+        
+               list->setLabel("0");
+               
+               return 1;
+        
+       }
+       catch(exception& e) {
+               m->errorOut(e, "ReadColumnMatrix", "read");
+               exit(1);
+       }
+}
+
+/***********************************************************************/
+ReadColumnMatrix::~ReadColumnMatrix(){}
+/***********************************************************************/
+