+/************************************************************/
+//set the number of sequences for the seq for the group
+int CountTable::setAbund(string seqName, string groupName, int num) {
+ try {
+ if (hasGroups) {
+ map<string, int>::iterator it = indexGroupMap.find(groupName);
+ if (it == indexGroupMap.end()) {
+ m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
+ }else {
+ map<string, int>::iterator it2 = indexNameMap.find(seqName);
+ if (it2 == indexNameMap.end()) {
+ //look for it in names of groups to see if the user accidently used the wrong file
+ if (m->inUsersGroups(seqName, groups)) {
+ m->mothurOut("[WARNING]: Your group or design file contains a group named " + seqName + ". Perhaps you are used a group file instead of a design file? A common cause of this is using a tree file that relates your groups (created by the tree.shared command) with a group file that assigns sequences to a group."); m->mothurOutEndLine();
+ }
+ m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
+ }else {
+ int oldCount = counts[it2->second][it->second];
+ counts[it2->second][it->second] = num;
+ totalGroups[it->second] += (num - oldCount);
+ total += (num - oldCount);
+ totals[it2->second] += (num - oldCount);
+ }
+ }
+ }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "CountTable", "set");
+ exit(1);
+ }
+}
+/************************************************************/
+//add group
+int CountTable::addGroup(string groupName) {
+ try {
+ bool sanity = m->inUsersGroups(groupName, groups);
+ if (sanity) { m->mothurOut("[ERROR]: " + groupName + " is already in the count table, cannot add again.\n"); m->control_pressed = true; return 0; }
+
+ groups.push_back(groupName);
+ if (!hasGroups) { counts.resize(uniques); }
+
+ for (int i = 0; i < counts.size(); i++) { counts[i].push_back(0); }
+ totalGroups.push_back(0);
+ indexGroupMap[groupName] = groups.size()-1;
+ map<string, int> originalGroupMap = indexGroupMap;
+
+ //important to play well with others, :)
+ sort(groups.begin(), groups.end());
+
+ //fix indexGroupMap && totalGroups
+ vector<int> newTotals; newTotals.resize(groups.size(), 0);
+ for (int i = 0; i < groups.size(); i++) {
+ indexGroupMap[groups[i]] = i;
+ //find original spot of group[i]
+ int index = originalGroupMap[groups[i]];
+ newTotals[i] = totalGroups[index];
+ }
+ totalGroups = newTotals;
+
+ //fix counts vectors
+ for (int i = 0; i < counts.size(); i++) {
+ vector<int> newCounts; newCounts.resize(groups.size(), 0);
+ for (int j = 0; j < groups.size(); j++) {
+ //find original spot of group[i]
+ int index = originalGroupMap[groups[j]];
+ newCounts[j] = counts[i][index];
+ }
+ counts[i] = newCounts;
+ }
+ hasGroups = true;
+ m->setAllGroups(groups);
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "CountTable", "addGroup");
+ exit(1);
+ }
+}
+/************************************************************/
+//remove group
+int CountTable::removeGroup(string groupName) {
+ try {
+ if (hasGroups) {
+ //save for later in case removing a group means we need to remove a seq.
+ map<int, string> reverse;
+ for (map<string, int>::iterator it = indexNameMap.begin(); it !=indexNameMap.end(); it++) { reverse[it->second] = it->first; }
+
+ map<string, int>::iterator it = indexGroupMap.find(groupName);
+ if (it == indexGroupMap.end()) {
+ m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
+ }else {
+ int indexOfGroupToRemove = it->second;
+ map<string, int> currentGroupIndex = indexGroupMap;
+ vector<string> newGroups;
+ for (int i = 0; i < groups.size(); i++) {
+ if (groups[i] != groupName) {
+ newGroups.push_back(groups[i]);
+ indexGroupMap[groups[i]] = newGroups.size()-1;
+ }
+ }
+ indexGroupMap.erase(groupName);
+ groups = newGroups;
+ totalGroups.erase(totalGroups.begin()+indexOfGroupToRemove);
+
+ int thisIndex = 0;
+ map<string, int> newIndexNameMap;
+ for (int i = 0; i < counts.size(); i++) {
+ int num = counts[i][indexOfGroupToRemove];
+ counts[i].erase(counts[i].begin()+indexOfGroupToRemove);
+ totals[i] -= num;
+ total -= num;
+ if (totals[i] == 0) { //your sequences are only from the group we want to remove, then remove you.
+ counts.erase(counts.begin()+i);
+ totals.erase(totals.begin()+i);
+ uniques--;
+ i--;
+ }
+ newIndexNameMap[reverse[thisIndex]] = i;
+ thisIndex++;
+ }
+ indexNameMap = newIndexNameMap;
+
+ if (groups.size() == 0) { hasGroups = false; }
+ }
+ }else { m->mothurOut("[ERROR]: your count table does not contain group information, can not remove group " + groupName + ".\n"); m->control_pressed = true; }
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "CountTable", "removeGroup");
+ exit(1);
+ }
+}
+/************************************************************/
+//vector of groups for the seq
+vector<string> CountTable::getGroups(string seqName) {
+ try {
+ vector<string> thisGroups;
+ if (hasGroups) {
+ vector<int> thisCounts = getGroupCounts(seqName);
+ for (int i = 0; i < thisCounts.size(); i++) {
+ if (thisCounts[i] != 0) { thisGroups.push_back(groups[i]); }
+ }
+ }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
+
+ return thisGroups;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "CountTable", "getGroups");
+ exit(1);
+ }
+}
+/************************************************************/
+//total number of seqs represented by seq
+int CountTable::renameSeq(string oldSeqName, string newSeqName) {
+ try {
+
+ map<string, int>::iterator it = indexNameMap.find(oldSeqName);
+ if (it == indexNameMap.end()) {
+ if (hasGroupInfo()) {
+ //look for it in names of groups to see if the user accidently used the wrong file
+ if (m->inUsersGroups(oldSeqName, groups)) {
+ m->mothurOut("[WARNING]: Your group or design file contains a group named " + oldSeqName + ". Perhaps you are used a group file instead of a design file? A common cause of this is using a tree file that relates your groups (created by the tree.shared command) with a group file that assigns sequences to a group."); m->mothurOutEndLine();
+ }
+ }
+ m->mothurOut("[ERROR]: " + oldSeqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
+ }else {
+ int index = it->second;
+ indexNameMap.erase(it);
+ indexNameMap[newSeqName] = index;
+ }
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "CountTable", "renameSeq");
+ exit(1);
+ }
+}
+