]> git.donarmstrong.com Git - mothur.git/blobdiff - treemap.cpp
removing chime source files from mother project.
[mothur.git] / treemap.cpp
index fee309c5877138039c2ecdff8f54428a8060bc24..c228162aab1d511af3fc5318aaf3e5eb5b39610c 100644 (file)
 /************************************************************/
 
  TreeMap::TreeMap(string filename) {
+       m = MothurOut::getInstance();
        groupFileName = filename;
-       openInputFile(filename, fileHandle);
+       m->openInputFile(filename, fileHandle);
 }
 
 /************************************************************/
- TreeMap::~TreeMap(){};
+ TreeMap::~TreeMap(){}
+/************************************************************/
+int TreeMap::readMap(string gf) {
+    
+    groupFileName = gf;
+       m->openInputFile(gf, fileHandle);
+    
+    string seqName, seqGroup;
+    int error = 0;
+    
+    while(fileHandle){
+        fileHandle >> seqName;       m->gobble(fileHandle);    //read from first column
+        fileHandle >> seqGroup;                        //read from second column
+        
+        if (m->control_pressed) {  fileHandle.close();  return 1; }
+        
+        setNamesOfGroups(seqGroup);
+        
+        map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
+        if (itCheck != treemap.end()) { error = 1; m->mothurOut("[WARNING]: Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
+        else {
+            namesOfSeqs.push_back(seqName);
+            treemap[seqName].groupname = seqGroup;     //store data in map
+            
+            it2 = seqsPerGroup.find(seqGroup);
+            if (it2 == seqsPerGroup.end()) { //if it's a new group
+                seqsPerGroup[seqGroup] = 1;
+            }else {//it's a group we already have
+                seqsPerGroup[seqGroup]++;
+            }                          
+        }
+        
+        m->gobble(fileHandle);
+    }
+    fileHandle.close();
+    
+    return error;
+}
 
 /************************************************************/
-void TreeMap::readMap() {
+int TreeMap::readMap() {
                string seqName, seqGroup;
-       
+               int error = 0;
+               
                while(fileHandle){
-                       fileHandle >> seqName;                  //read from first column
+                       fileHandle >> seqName;           m->gobble(fileHandle); //read from first column
                        fileHandle >> seqGroup;                 //read from second column
                        
-                       namesOfSeqs.push_back(seqName);
-                       setNamesOfGroups(seqGroup);
-                                               
-                       treemap[seqName].groupname = seqGroup;  //store data in map
+                       if (m->control_pressed) {  fileHandle.close();  return 1; }
                        
-                       it2 = seqsPerGroup.find(seqGroup);
-                       if (it2 == seqsPerGroup.end()) { //if it's a new group
-                               seqsPerGroup[seqGroup] = 1;
-                       }else {//it's a group we already have
-                               seqsPerGroup[seqGroup]++;
+                       setNamesOfGroups(seqGroup);
+                                       
+                       map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
+                       if (itCheck != treemap.end()) { error = 1; m->mothurOut("[WARNING]: Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
+                       else {
+                               namesOfSeqs.push_back(seqName);
+                               treemap[seqName].groupname = seqGroup;  //store data in map
+                               
+                               it2 = seqsPerGroup.find(seqGroup);
+                               if (it2 == seqsPerGroup.end()) { //if it's a new group
+                                       seqsPerGroup[seqGroup] = 1;
+                               }else {//it's a group we already have
+                                       seqsPerGroup[seqGroup]++;
+                               }                               
                        }
-
-                       gobble(fileHandle);
+                       
+                       m->gobble(fileHandle);
                }
                fileHandle.close();
+       
+
+               return error;
+}
+/************************************************************/
+void TreeMap::addSeq(string seqName, string seqGroup) {
+       
+               namesOfSeqs.push_back(seqName);
+               setNamesOfGroups(seqGroup);
+                                       
+               treemap[seqName].groupname = seqGroup;  //store data in map
+                       
+               it2 = seqsPerGroup.find(seqGroup);
+               if (it2 == seqsPerGroup.end()) { //if it's a new group
+                       seqsPerGroup[seqGroup] = 1;
+               }else {//it's a group we already have
+                       seqsPerGroup[seqGroup]++;
+               }
+}
+/************************************************************/
+void TreeMap::removeSeq(string seqName) {
+       
+       //erase name from namesOfSeqs
+       for (int i = 0; i < namesOfSeqs.size(); i++) {
+               if (namesOfSeqs[i] == seqName)  {
+                       namesOfSeqs.erase(namesOfSeqs.begin()+i);
+                       break;
+               }
+       }
+       
+       //decrement sequences in this group
+       string group = treemap[seqName].groupname;
+       seqsPerGroup[group]--;
+       
+       //remove seq from treemap
+       it = treemap.find(seqName);
+       treemap.erase(it);
 }
 /************************************************************/
 
@@ -116,11 +198,7 @@ bool TreeMap::isValidGroup(string groupname) {
                return false;
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TreeMap class Function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }
-       catch(...) {
-               cout << "An unknown error has occurred in the TreeMap class function isValidGroup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "TreeMap", "isValidGroup");
                exit(1);
        }
 }
@@ -134,13 +212,114 @@ void TreeMap::print(ostream& output){
                }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the TreeMap class Function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "TreeMap", "print");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the TreeMap class function print. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+}
+
+/************************************************************/
+void TreeMap::makeSim(vector<string> ThisnamesOfGroups) {
+       try {
+               //set names of groups
+               namesOfGroups = ThisnamesOfGroups;
+               
+               //set names of seqs to names of groups
+               namesOfSeqs = ThisnamesOfGroups;
+               
+               // make map where key and value are both the group name since that what the tree.shared command wants
+               for (int i = 0; i < namesOfGroups.size(); i++) {
+                       treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
+                       seqsPerGroup[namesOfGroups[i]] = 1;
+               }
+               
+               numGroups = namesOfGroups.size();
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeMap", "makeSim");
+               exit(1);
+       }
+}
+/************************************************************/
+void TreeMap::makeSim(ListVector* list) {
+       try {
+               //set names of groups
+               namesOfGroups.clear();
+               for(int i = 0; i < list->size(); i++) {
+                       namesOfGroups.push_back(list->get(i));
+               }
+               
+               //set names of seqs to names of groups
+               namesOfSeqs = namesOfGroups;
+               
+               // make map where key and value are both the group name since that what the tree.shared command wants
+               for (int i = 0; i < namesOfGroups.size(); i++) {
+                       treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
+                       seqsPerGroup[namesOfGroups[i]] = 1;
+               }
+               
+               numGroups = namesOfGroups.size();
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeMap", "makeSim");
+               exit(1);
+       }
+}
+/************************************************************/
+int TreeMap::getCopy(TreeMap& copy){
+       try {
+         
+        namesOfGroups = copy.getNamesOfGroups();
+               numGroups = copy.getNumGroups();
+        namesOfSeqs = copy.namesOfSeqs;
+        seqsPerGroup = copy.seqsPerGroup;
+        treemap = copy.treemap;
+        
+        return 0;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeMap", "getCopy");
+               exit(1);
+       }
+}
+/************************************************************/
+vector<string> TreeMap::getNamesSeqs(){
+       try {
+        
+               vector<string> names;
+               
+        for(it = treemap.begin(); it != treemap.end(); it++){
+            names.push_back(it->first);
+               }
+               
+               return names;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeMap", "getNamesSeqs");
+               exit(1);
+       }
+}
+/************************************************************/
+vector<string> TreeMap::getNamesSeqs(vector<string> picked){
+       try {
+               
+               vector<string> names;
+               
+               for(it = treemap.begin(); it != treemap.end(); it++){
+                       //if you are belong to one the the groups in the picked vector add you
+                       if (m->inUsersGroups(it->second.groupname, picked)) {
+                               names.push_back(it->first);
+                       }
+               }
+               
+               return names;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TreeMap", "getNamesSeqs");
                exit(1);
        }
 }
 
 /************************************************************/
+