X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=counttable.cpp;h=d5720acd386c36cf8cd713dcbc6d1bd5e507609b;hp=a79047d0af943e1c2bbef6d169aeefa884c1e164;hb=b206f634aae1b4ce13978d203247fb64757d5482;hpb=96dbe925073caefaed6e6db85659c144a806aeb1 diff --git a/counttable.cpp b/counttable.cpp index a79047d..d5720ac 100644 --- a/counttable.cpp +++ b/counttable.cpp @@ -40,7 +40,13 @@ int CountTable::createTable(set& n, map& g, set& if (itGroup != g.end()) { groupCounts[indexGroupMap[itGroup->second]] = 1; totalGroups[indexGroupMap[itGroup->second]]++; - }else { m->mothurOut("[ERROR]: Your group file does not contain " + seqName + ". Please correct."); m->mothurOutEndLine(); } + }else { + //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]: Your group file does not contain " + seqName + ". Please correct."); m->mothurOutEndLine(); + } map::iterator it2 = indexNameMap.find(seqName); if (it2 == indexNameMap.end()) { @@ -131,6 +137,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, ','); @@ -202,7 +211,7 @@ int CountTable::createTable(string namefile, string groupfile, bool createGroup) } } /************************************************************/ -int CountTable::readTable(string file) { +int CountTable::readTable(string file, bool readGroups, bool mothurRunning) { try { filename = file; ifstream in; @@ -218,7 +227,7 @@ int CountTable::readTable(string file) { indexNameMap.clear(); counts.clear(); map originalGroupIndexes; - if (columnHeaders.size() > 2) { hasGroups = true; numGroups = columnHeaders.size() - 2; } + if ((columnHeaders.size() > 2) && readGroups) { hasGroups = true; numGroups = columnHeaders.size() - 2; } for (int i = 2; i < columnHeaders.size(); i++) { groups.push_back(columnHeaders[i]); originalGroupIndexes[i-2] = columnHeaders[i]; totalGroups.push_back(0); } //sort groups to keep consistent with how we store the groups in groupmap sort(groups.begin(), groups.end()); @@ -237,9 +246,18 @@ int CountTable::readTable(string file) { in >> name; m->gobble(in); in >> thisTotal; m->gobble(in); if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + toString(thisTotal) + "\n"); } + if ((thisTotal == 0) && !mothurRunning) { error=true; m->mothurOut("[ERROR]: Your count table contains a sequence named " + name + " with a total=0. Please correct."); m->mothurOutEndLine(); + } + //if group info, then read it vector groupCounts; groupCounts.resize(numGroups, 0); - for (int i = 0; i < numGroups; i++) { int thisIndex = indexGroupMap[originalGroupIndexes[i]]; in >> groupCounts[thisIndex]; m->gobble(in); totalGroups[thisIndex] += groupCounts[thisIndex]; } + if (columnHeaders.size() > 2) { //file contains groups + if (readGroups) { //user wants to save them + for (int i = 0; i < numGroups; i++) { int thisIndex = indexGroupMap[originalGroupIndexes[i]]; in >> groupCounts[thisIndex]; m->gobble(in); totalGroups[thisIndex] += groupCounts[thisIndex]; } + }else { //read and discard + m->getline(in); m->gobble(in); + } + } map::iterator it = indexNameMap.find(name); if (it == indexNameMap.end()) { @@ -280,7 +298,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) { @@ -289,7 +323,7 @@ int CountTable::printTable(string file) { } } out << endl; - } + }*/ out.close(); return 0; } @@ -341,6 +375,10 @@ vector CountTable::getGroupCounts(string seqName) { if (hasGroups) { map::iterator it = indexNameMap.find(seqName); if (it == 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 { temp = counts[it->second]; @@ -361,7 +399,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]; } @@ -381,11 +419,15 @@ 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; + //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]: seq " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true; }else { return counts[it2->second][it->second]; } @@ -410,6 +452,10 @@ int CountTable::setAbund(string seqName, string groupName, int num) { }else { map::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]; @@ -481,6 +527,10 @@ int CountTable::addGroup(string groupName) { 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; @@ -491,13 +541,15 @@ int CountTable::removeGroup(string groupName) { for (int i = 0; i < groups.size(); i++) { if (groups[i] != groupName) { newGroups.push_back(groups[i]); - indexGroupMap[groups[i]] = 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); @@ -509,7 +561,11 @@ int CountTable::removeGroup(string groupName) { 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; } @@ -547,6 +603,12 @@ int CountTable::renameSeq(string oldSeqName, string newSeqName) { map::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; @@ -569,6 +631,12 @@ int CountTable::getNumSeqs(string seqName) { map::iterator it = indexNameMap.find(seqName); 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(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 { return totals[it->second]; @@ -588,6 +656,12 @@ int CountTable::get(string seqName) { map::iterator it = indexNameMap.find(seqName); 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(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 { return it->second; } @@ -634,6 +708,12 @@ int CountTable::remove(string seqName) { total -= thisTotal; indexNameMap.erase(it); }else { + if (hasGroupInfo()) { + //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]: Your count table contains does not include " + seqName + ", cannot remove."); m->mothurOutEndLine(); m->control_pressed = true; } @@ -770,10 +850,22 @@ int CountTable::mergeCounts(string seq1, string seq2) { try { map::iterator it = indexNameMap.find(seq1); 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(seq1, groups)) { + m->mothurOut("[WARNING]: Your group or design file contains a group named " + seq1 + ". 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]: " + seq1 + " is not in your count table. Please correct.\n"); m->control_pressed = true; }else { map::iterator it2 = indexNameMap.find(seq2); if (it2 == indexNameMap.end()) { + if (hasGroupInfo()) { + //look for it in names of groups to see if the user accidently used the wrong file + if (m->inUsersGroups(seq2, groups)) { + m->mothurOut("[WARNING]: Your group or design file contains a group named " + seq2 + ". 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]: " + seq2 + " is not in your count table. Please correct.\n"); m->control_pressed = true; }else { //merge data