]> git.donarmstrong.com Git - mothur.git/blobdiff - nameassignment.cpp
sped up splitting of distance file by 2.5 times by buffering the read and writes.
[mothur.git] / nameassignment.cpp
index 925608659a4261fc6924d23b16bbcafb13ed3558..0c30898dd4085f5000a68c005ccbeee1bcd3650c 100644 (file)
@@ -5,7 +5,7 @@
 //**********************************************************************************************************************
 
 NameAssignment::NameAssignment(string nameMapFile){
-       
+       m = MothurOut::getInstance();
        openInputFile(nameMapFile, fileHandle);
        
 }
@@ -17,25 +17,30 @@ void NameAssignment::readMap(){
                string firstCol, secondCol, skip;
        //      int index = 0;
        
-       
-//             map<string, string> data;
+               
+               map<string, int>::iterator itData;
                int rowIndex = 0;
-
+               
                while(fileHandle){
                        fileHandle >> firstCol;                         //read from first column
                        fileHandle >> secondCol;                        //read from second column
+                                               
+                       itData = (*this).find(firstCol);
+                       if (itData == (*this).end()) {
+                       
+                               (*this)[firstCol] = rowIndex++;
+                               list.push_back(secondCol);              //adds data's value to list
+                               reverse[rowIndex] = firstCol;
+                               
+                       }else{  m->mothurOut(firstCol + " is already in namesfile. I will use first definition."); m->mothurOutEndLine();  }
                        
-//                     data[firstCol] = secondCol;                     //store data in map
-
-                       list.push_back(secondCol);              //adds data's value to list
-                       (*this)[firstCol] = rowIndex++;
                        gobble(fileHandle);
                }
                fileHandle.close();
        
        }
        catch(exception& e) {
-               errorOut(e, "NameAssignment", "readMap");
+               m->errorOut(e, "NameAssignment", "readMap");
                exit(1);
        }
 }
@@ -45,11 +50,12 @@ void NameAssignment::push_back(string name) {
        
                int num = (*this).size();
                (*this)[name] = num;
+               reverse[num] = name;
                
                list.push_back(name);
        }
        catch(exception& e) {
-               errorOut(e, "NameAssignment", "push_back");
+               m->errorOut(e, "NameAssignment", "push_back");
                exit(1);
        }
 }
@@ -64,15 +70,16 @@ ListVector NameAssignment::getListVector(void){
 
 //**********************************************************************************************************************
 
-void NameAssignment::print(void){
+void NameAssignment::print(ostream& out){
        try {
                map<string,int>::iterator it;
+//cout << (*this).size() << endl;
                for(it = (*this).begin(); it!=(*this).end(); it++){
-                       mothurOut(it->first + "\t" + toString(it->second)); mothurOutEndLine();  //prints out keys and values of the map this.
+                       out << it->first << '\t' <<  it->second << endl;  //prints out keys and values of the map this.
                }
        }
        catch(exception& e) {
-               errorOut(e, "NameAssignment", "print");
+               m->errorOut(e, "NameAssignment", "print");
                exit(1);
        }
 }
@@ -84,6 +91,12 @@ int NameAssignment::get(string key){
        return  (*this)[key];   
 
 }
+//**********************************************************************************************************************
 
+string NameAssignment::get(int key){
+       
+       return  reverse[key];   
+
+}
 //**********************************************************************************************************************