]> git.donarmstrong.com Git - mothur.git/blobdiff - groupmap.cpp
created mothurOut class to handle logfiles
[mothur.git] / groupmap.cpp
index 27302dbce43f02a008fa54aaeb5e84bda8a8b003..8d0662f9c404cb05d4cfed74596a33284d5235d9 100644 (file)
 /************************************************************/
 
  GroupMap::GroupMap(string filename) {
+       m = MothurOut::getInstance();
        groupFileName = filename;
        openInputFile(filename, fileHandle);
        index = 0;
 }
 
 /************************************************************/
- GroupMap::~GroupMap(){};
+ GroupMap::~GroupMap(){}
 
 /************************************************************/
-void GroupMap::readMap() {
+int GroupMap::readMap() {
                string seqName, seqGroup;
+               int error = 0;
        
                while(fileHandle){
                        fileHandle >> seqName;                  //read from first column
                        fileHandle >> seqGroup;                 //read from second column
                        
                        setNamesOfGroups(seqGroup);
-                                               
-                       groupmap[seqName] = seqGroup;   //store data in map
-               
+                       
+                       it = groupmap.find(seqName);
+                       
+                       if (it != groupmap.end()) { error = 1; m->mothurOut("Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
+                       else {
+                               groupmap[seqName] = seqGroup;   //store data in map
+                               seqsPerGroup[seqGroup]++;  //increment number of seqs in that group
+                       }
                        gobble(fileHandle);
                }
                fileHandle.close();
+               return error;
 }
 /************************************************************/
-
-int GroupMap::getNumGroups() {
-                       
-       return namesOfGroups.size();    
-               
-}
+int GroupMap::getNumGroups() { return namesOfGroups.size();    }
 /************************************************************/
 
 string GroupMap::getGroup(string sequenceName) {
@@ -53,11 +56,13 @@ string GroupMap::getGroup(string sequenceName) {
        }else {
                return "not found";
        }
-               
 }
 
 /************************************************************/
-
+void GroupMap::setGroup(string sequenceName, string groupN) {
+       groupmap[sequenceName] = groupN;
+}
+/************************************************************/
 void GroupMap::setNamesOfGroups(string seqGroup) {
                        int i, count;
                        count = 0;
@@ -70,7 +75,60 @@ void GroupMap::setNamesOfGroups(string seqGroup) {
                        }
                        if (count == namesOfGroups.size()) {
                                namesOfGroups.push_back(seqGroup); //new group
+                               seqsPerGroup[seqGroup] = 0;
                                groupIndex[seqGroup] = index;
                                index++;
                        }
 }
+/************************************************************/
+bool GroupMap::isValidGroup(string groupname) {
+       try {
+               for (int i = 0; i < namesOfGroups.size(); i++) {
+                       if (groupname == namesOfGroups[i]) { return true; }
+               }
+               
+               return false;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GroupMap", "isValidGroup");
+               exit(1);
+       }
+}
+/************************************************************/
+int GroupMap::getNumSeqs(string group) {
+       try {
+               
+               map<string, int>::iterator itNum;
+               
+               itNum = seqsPerGroup.find(group);
+               
+               if (itNum == seqsPerGroup.end()) { return 0; }
+               
+               return seqsPerGroup[group];
+               
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GroupMap", "getNumSeqs");
+               exit(1);
+       }
+}
+
+/************************************************************/
+vector<string> GroupMap::getNamesSeqs(){
+       try {
+       
+               vector<string> names;
+               
+               for (it = groupmap.begin(); it != groupmap.end(); it++) {
+                       names.push_back(it->first);
+               }
+               
+               return names;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GroupMap", "getNamesSeqs");
+               exit(1);
+       }
+}
+/************************************************************/
+