X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=counttable.cpp;h=c16bf9227a51580cac00de78a215b89e2a93e001;hb=67ea6ccd74dbd64828d31b952808255f206364ff;hp=5307beee8379d8e758b946b3847dfa1be2d5a7b2;hpb=6c2b1e530a5c0bb87040e58a3e410097acdfcc3d;p=mothur.git diff --git a/counttable.cpp b/counttable.cpp index 5307bee..c16bf92 100644 --- a/counttable.cpp +++ b/counttable.cpp @@ -52,6 +52,12 @@ int CountTable::createTable(set& n, map& g, set& } } + if (hasGroups) { + for (int i = 0; i < totalGroups.size(); i++) { + if (totalGroups[i] == 0) { m->mothurOut("\nRemoving group: " + groups[i] + " because all sequences have been removed.\n"); removeGroup(groups[i]); i--; } + } + } + return 0; } catch(exception& e) { @@ -125,6 +131,9 @@ int CountTable::createTable(string namefile, string groupfile, bool createGroup) string firstCol, secondCol; in >> firstCol; m->gobble(in); in >> secondCol; m->gobble(in); + m->checkName(firstCol); + m->checkName(secondCol); + vector names; m->splitAtChar(secondCol, names, ','); @@ -179,7 +188,14 @@ int CountTable::createTable(string namefile, string groupfile, bool createGroup) in.close(); if (error) { m->control_pressed = true; } - if (groupfile != "") { delete groupMap; } + else { //check for zero groups + if (hasGroups) { + for (int i = 0; i < totalGroups.size(); i++) { + if (totalGroups[i] == 0) { m->mothurOut("\nRemoving group: " + groups[i] + " because all sequences have been removed.\n"); removeGroup(groups[i]); i--; } + } + } + } + if (groupfile != "") { delete groupMap; } return 0; } @@ -243,6 +259,13 @@ int CountTable::readTable(string file) { in.close(); if (error) { m->control_pressed = true; } + else { //check for zero groups + if (hasGroups) { + for (int i = 0; i < totalGroups.size(); i++) { + if (totalGroups[i] == 0) { m->mothurOut("\nRemoving group: " + groups[i] + " because all sequences have been removed.\n"); removeGroup(groups[i]); i--; } + } + } + } return 0; } @@ -260,7 +283,23 @@ int CountTable::printTable(string file) { for (int i = 0; i < groups.size(); i++) { out << groups[i] << '\t'; } out << endl; - for (map::iterator itNames = indexNameMap.begin(); itNames != indexNameMap.end(); itNames++) { + map reverse; //use this to preserve order + for (map::iterator it = indexNameMap.begin(); it !=indexNameMap.end(); it++) { reverse[it->second] = it->first; } + + for (int i = 0; i < totals.size(); i++) { + map::iterator itR = reverse.find(i); + + if (itR != reverse.end()) { //will equal end if seqs were removed because remove just removes from indexNameMap + out << itR->second << '\t' << totals[i] << '\t'; + if (hasGroups) { + for (int j = 0; j < groups.size(); j++) { + out << counts[i][j] << '\t'; + } + } + out << endl; + } + } + /*for (map::iterator itNames = indexNameMap.begin(); itNames != indexNameMap.end(); itNames++) { out << itNames->first << '\t' << totals[itNames->second] << '\t'; if (hasGroups) { @@ -269,7 +308,7 @@ int CountTable::printTable(string file) { } } out << endl; - } + }*/ out.close(); return 0; } @@ -341,7 +380,7 @@ int CountTable::getGroupCount(string groupName) { if (hasGroups) { map::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; + m->mothurOut("[ERROR]: group " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true; }else { return totalGroups[it->second]; } @@ -361,11 +400,11 @@ int CountTable::getGroupCount(string seqName, string groupName) { if (hasGroups) { map::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; + m->mothurOut("[ERROR]: group " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true; }else { map::iterator it2 = indexNameMap.find(seqName); if (it2 == indexNameMap.end()) { - m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true; + m->mothurOut("[ERROR]: seq " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true; }else { return counts[it2->second][it->second]; } @@ -447,6 +486,7 @@ int CountTable::addGroup(string groupName) { counts[i] = newCounts; } hasGroups = true; + m->setAllGroups(groups); return 0; } @@ -456,6 +496,61 @@ int CountTable::addGroup(string groupName) { } } /************************************************************/ +//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 reverse; + for (map::iterator it = indexNameMap.begin(); it !=indexNameMap.end(); it++) { reverse[it->second] = it->first; } + + map::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 currentGroupIndex = indexGroupMap; + vector 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 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 CountTable::getGroups(string seqName) { try { @@ -559,7 +654,7 @@ int CountTable::push_back(string seqName) { int CountTable::remove(string seqName) { try { map::iterator it = indexNameMap.find(seqName); - if (it == indexNameMap.end()) { + if (it != indexNameMap.end()) { uniques--; if (hasGroups){ //remove this sequences counts from group totals for (int i = 0; i < totalGroups.size(); i++) { totalGroups[i] -= counts[it->second][i]; counts[it->second][i] = 0; } @@ -660,6 +755,22 @@ vector CountTable::getNamesOfSeqs() { } } /************************************************************/ +//returns the names of all unique sequences in file mapped to their seqCounts +map CountTable::getNameMap() { + try { + map names; + for (map::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) { + names[it->first] = totals[it->second]; + } + + return names; + } + catch(exception& e) { + m->errorOut(e, "CountTable", "getNameMap"); + exit(1); + } +} +/************************************************************/ //returns the names of all unique sequences in file vector CountTable::getNamesOfSeqs(string group) { try {