5 // Created by Sarah Westcott on 6/26/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "counttable.h"
11 /************************************************************/
12 int CountTable::createTable(set<string>& n, map<string, string>& g, set<string>& gs) {
17 indexGroupMap.clear();
20 for (set<string>::iterator it = gs.begin(); it != gs.end(); it++) { groups.push_back(*it); hasGroups = true; }
21 numGroups = groups.size();
22 totalGroups.resize(numGroups, 0);
24 //sort groups to keep consistent with how we store the groups in groupmap
25 sort(groups.begin(), groups.end());
26 for (int i = 0; i < groups.size(); i++) { indexGroupMap[groups[i]] = i; }
27 m->setAllGroups(groups);
31 for (set<string>::iterator it = n.begin(); it != n.end(); it++) {
33 if (m->control_pressed) { break; }
37 vector<int> groupCounts; groupCounts.resize(numGroups, 0);
38 map<string, string>::iterator itGroup = g.find(seqName);
40 if (itGroup != g.end()) {
41 groupCounts[indexGroupMap[itGroup->second]] = 1;
42 totalGroups[indexGroupMap[itGroup->second]]++;
44 //look for it in names of groups to see if the user accidently used the wrong file
45 if (m->inUsersGroups(seqName, groups)) {
46 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();
48 m->mothurOut("[ERROR]: Your group file does not contain " + seqName + ". Please correct."); m->mothurOutEndLine();
51 map<string, int>::iterator it2 = indexNameMap.find(seqName);
52 if (it2 == indexNameMap.end()) {
53 if (hasGroups) { counts.push_back(groupCounts); }
54 indexNameMap[seqName] = uniques;
62 for (int i = 0; i < totalGroups.size(); i++) {
63 if (totalGroups[i] == 0) { m->mothurOut("\nRemoving group: " + groups[i] + " because all sequences have been removed.\n"); removeGroup(groups[i]); i--; }
70 m->errorOut(e, "CountTable", "createTable");
74 /************************************************************/
75 bool CountTable::testGroups(string file) {
77 m = MothurOut::getInstance(); hasGroups = false; total = 0;
79 m->openInputFile(file, in);
81 string headers = m->getline(in); m->gobble(in);
82 vector<string> columnHeaders = m->splitWhiteSpace(headers);
83 if (columnHeaders.size() > 2) { hasGroups = true; }
87 m->errorOut(e, "CountTable", "readTable");
91 /************************************************************/
92 int CountTable::createTable(string namefile, string groupfile, bool createGroup) {
95 if (namefile == "") { m->mothurOut("[ERROR]: namefile cannot be blank when creating a count table.\n"); m->control_pressed = true; }
101 indexGroupMap.clear();
102 indexNameMap.clear();
104 map<int, string> originalGroupIndexes;
106 if (groupfile != "") {
108 groupMap = new GroupMap(groupfile); groupMap->readMap();
109 numGroups = groupMap->getNumGroups();
110 groups = groupMap->getNamesOfGroups();
111 totalGroups.resize(numGroups, 0);
112 }else if(createGroup) {
115 groups.push_back("Group1");
116 totalGroups.resize(numGroups, 0);
118 //sort groups to keep consistent with how we store the groups in groupmap
119 sort(groups.begin(), groups.end());
120 for (int i = 0; i < groups.size(); i++) { indexGroupMap[groups[i]] = i; }
121 m->setAllGroups(groups);
131 m->openInputFile(namefile, in);
135 if (m->control_pressed) { break; }
137 string firstCol, secondCol;
138 in >> firstCol; m->gobble(in); in >> secondCol; m->gobble(in);
140 m->checkName(firstCol);
141 m->checkName(secondCol);
143 vector<string> names;
144 m->splitAtChar(secondCol, names, ',');
146 map<string, int> groupCounts;
148 if (groupfile != "") {
150 for (int i = 0; i < groups.size(); i++) { groupCounts[groups[i]] = 0; }
152 //get counts for each of the users groups
153 for (int i = 0; i < names.size(); i++) {
154 string group = groupMap->getGroup(names[i]);
156 if (group == "not found") { m->mothurOut("[ERROR]: " + names[i] + " is not in your groupfile, please correct."); m->mothurOutEndLine(); error=true; }
158 map<string, int>::iterator it = groupCounts.find(group);
160 //if not found, then this sequence is not from a group we care about
161 if (it != groupCounts.end()) {
167 }else if (createGroup) {
168 groupCounts["Group1"]=0;
169 for (int i = 0; i < names.size(); i++) {
170 string group = "Group1";
171 groupCounts["Group1"]++; thisTotal++;
173 }else { thisTotal = names.size(); }
175 //if group info, then read it
176 vector<int> thisGroupsCount; thisGroupsCount.resize(numGroups, 0);
177 for (int i = 0; i < numGroups; i++) {
178 thisGroupsCount[i] = groupCounts[groups[i]];
179 totalGroups[i] += thisGroupsCount[i];
182 map<string, int>::iterator it = indexNameMap.find(firstCol);
183 if (it == indexNameMap.end()) {
184 if (hasGroups) { counts.push_back(thisGroupsCount); }
185 indexNameMap[firstCol] = uniques;
186 totals.push_back(thisTotal);
191 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + firstCol + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();
196 if (error) { m->control_pressed = true; }
197 else { //check for zero groups
199 for (int i = 0; i < totalGroups.size(); i++) {
200 if (totalGroups[i] == 0) { m->mothurOut("\nRemoving group: " + groups[i] + " because all sequences have been removed.\n"); removeGroup(groups[i]); i--; }
204 if (groupfile != "") { delete groupMap; }
208 catch(exception& e) {
209 m->errorOut(e, "CountTable", "createTable");
213 /************************************************************/
214 int CountTable::readTable(string file) {
218 m->openInputFile(filename, in);
220 string headers = m->getline(in); m->gobble(in);
221 vector<string> columnHeaders = m->splitWhiteSpace(headers);
226 indexGroupMap.clear();
227 indexNameMap.clear();
229 map<int, string> originalGroupIndexes;
230 if (columnHeaders.size() > 2) { hasGroups = true; numGroups = columnHeaders.size() - 2; }
231 for (int i = 2; i < columnHeaders.size(); i++) { groups.push_back(columnHeaders[i]); originalGroupIndexes[i-2] = columnHeaders[i]; totalGroups.push_back(0); }
232 //sort groups to keep consistent with how we store the groups in groupmap
233 sort(groups.begin(), groups.end());
234 for (int i = 0; i < groups.size(); i++) { indexGroupMap[groups[i]] = i; }
235 m->setAllGroups(groups);
244 if (m->control_pressed) { break; }
246 in >> name; m->gobble(in); in >> thisTotal; m->gobble(in);
247 if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + toString(thisTotal) + "\n"); }
249 //if group info, then read it
250 vector<int> groupCounts; groupCounts.resize(numGroups, 0);
251 for (int i = 0; i < numGroups; i++) { int thisIndex = indexGroupMap[originalGroupIndexes[i]]; in >> groupCounts[thisIndex]; m->gobble(in); totalGroups[thisIndex] += groupCounts[thisIndex]; }
253 map<string, int>::iterator it = indexNameMap.find(name);
254 if (it == indexNameMap.end()) {
255 if (hasGroups) { counts.push_back(groupCounts); }
256 indexNameMap[name] = uniques;
257 totals.push_back(thisTotal);
262 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + name + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();
267 if (error) { m->control_pressed = true; }
268 else { //check for zero groups
270 for (int i = 0; i < totalGroups.size(); i++) {
271 if (totalGroups[i] == 0) { m->mothurOut("\nRemoving group: " + groups[i] + " because all sequences have been removed.\n"); removeGroup(groups[i]); i--; }
278 catch(exception& e) {
279 m->errorOut(e, "CountTable", "readTable");
283 /************************************************************/
284 int CountTable::printTable(string file) {
287 m->openOutputFile(file, out);
288 out << "Representative_Sequence\ttotal\t";
289 for (int i = 0; i < groups.size(); i++) { out << groups[i] << '\t'; }
292 map<int, string> reverse; //use this to preserve order
293 for (map<string, int>::iterator it = indexNameMap.begin(); it !=indexNameMap.end(); it++) { reverse[it->second] = it->first; }
295 for (int i = 0; i < totals.size(); i++) {
296 map<int, string>::iterator itR = reverse.find(i);
298 if (itR != reverse.end()) { //will equal end if seqs were removed because remove just removes from indexNameMap
299 out << itR->second << '\t' << totals[i] << '\t';
301 for (int j = 0; j < groups.size(); j++) {
302 out << counts[i][j] << '\t';
308 /*for (map<string, int>::iterator itNames = indexNameMap.begin(); itNames != indexNameMap.end(); itNames++) {
309 out << itNames->first << '\t' << totals[itNames->second] << '\t';
312 for (int i = 0; i < groups.size(); i++) {
313 out << counts[itNames->second][i] << '\t';
321 catch(exception& e) {
322 m->errorOut(e, "CountTable", "printTable");
326 /************************************************************/
327 int CountTable::printHeaders(ofstream& out) {
329 out << "Representative_Sequence\ttotal\t";
330 for (int i = 0; i < groups.size(); i++) { out << groups[i] << '\t'; }
334 catch(exception& e) {
335 m->errorOut(e, "CountTable", "printHeaders");
339 /************************************************************/
340 int CountTable::printSeq(ofstream& out, string seqName) {
342 map<string, int>::iterator it = indexNameMap.find(seqName);
343 if (it == indexNameMap.end()) {
344 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
346 out << it->first << '\t' << totals[it->second] << '\t';
348 for (int i = 0; i < groups.size(); i++) {
349 out << counts[it->second][i] << '\t';
356 catch(exception& e) {
357 m->errorOut(e, "CountTable", "printSeq");
361 /************************************************************/
362 //group counts for a seq
363 vector<int> CountTable::getGroupCounts(string seqName) {
367 map<string, int>::iterator it = indexNameMap.find(seqName);
368 if (it == indexNameMap.end()) {
369 //look for it in names of groups to see if the user accidently used the wrong file
370 if (m->inUsersGroups(seqName, groups)) {
371 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();
373 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
375 temp = counts[it->second];
377 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
381 catch(exception& e) {
382 m->errorOut(e, "CountTable", "getGroupCounts");
386 /************************************************************/
387 //total number of sequences for the group
388 int CountTable::getGroupCount(string groupName) {
391 map<string, int>::iterator it = indexGroupMap.find(groupName);
392 if (it == indexGroupMap.end()) {
393 m->mothurOut("[ERROR]: group " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
395 return totalGroups[it->second];
397 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
401 catch(exception& e) {
402 m->errorOut(e, "CountTable", "getGroupCount");
406 /************************************************************/
407 //total number of sequences for the seq for the group
408 int CountTable::getGroupCount(string seqName, string groupName) {
411 map<string, int>::iterator it = indexGroupMap.find(groupName);
412 if (it == indexGroupMap.end()) {
413 m->mothurOut("[ERROR]: group " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
415 map<string, int>::iterator it2 = indexNameMap.find(seqName);
416 if (it2 == indexNameMap.end()) {
417 //look for it in names of groups to see if the user accidently used the wrong file
418 if (m->inUsersGroups(seqName, groups)) {
419 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();
421 m->mothurOut("[ERROR]: seq " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
423 return counts[it2->second][it->second];
426 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
430 catch(exception& e) {
431 m->errorOut(e, "CountTable", "getGroupCount");
435 /************************************************************/
436 //set the number of sequences for the seq for the group
437 int CountTable::setAbund(string seqName, string groupName, int num) {
440 map<string, int>::iterator it = indexGroupMap.find(groupName);
441 if (it == indexGroupMap.end()) {
442 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
444 map<string, int>::iterator it2 = indexNameMap.find(seqName);
445 if (it2 == indexNameMap.end()) {
446 //look for it in names of groups to see if the user accidently used the wrong file
447 if (m->inUsersGroups(seqName, groups)) {
448 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();
450 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
452 int oldCount = counts[it2->second][it->second];
453 counts[it2->second][it->second] = num;
454 totalGroups[it->second] += (num - oldCount);
455 total += (num - oldCount);
456 totals[it2->second] += (num - oldCount);
459 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
463 catch(exception& e) {
464 m->errorOut(e, "CountTable", "set");
468 /************************************************************/
470 int CountTable::addGroup(string groupName) {
472 bool sanity = m->inUsersGroups(groupName, groups);
473 if (sanity) { m->mothurOut("[ERROR]: " + groupName + " is already in the count table, cannot add again.\n"); m->control_pressed = true; return 0; }
475 groups.push_back(groupName);
476 if (!hasGroups) { counts.resize(uniques); }
478 for (int i = 0; i < counts.size(); i++) { counts[i].push_back(0); }
479 totalGroups.push_back(0);
480 indexGroupMap[groupName] = groups.size()-1;
481 map<string, int> originalGroupMap = indexGroupMap;
483 //important to play well with others, :)
484 sort(groups.begin(), groups.end());
486 //fix indexGroupMap && totalGroups
487 vector<int> newTotals; newTotals.resize(groups.size(), 0);
488 for (int i = 0; i < groups.size(); i++) {
489 indexGroupMap[groups[i]] = i;
490 //find original spot of group[i]
491 int index = originalGroupMap[groups[i]];
492 newTotals[i] = totalGroups[index];
494 totalGroups = newTotals;
497 for (int i = 0; i < counts.size(); i++) {
498 vector<int> newCounts; newCounts.resize(groups.size(), 0);
499 for (int j = 0; j < groups.size(); j++) {
500 //find original spot of group[i]
501 int index = originalGroupMap[groups[j]];
502 newCounts[j] = counts[i][index];
504 counts[i] = newCounts;
507 m->setAllGroups(groups);
511 catch(exception& e) {
512 m->errorOut(e, "CountTable", "addGroup");
516 /************************************************************/
518 int CountTable::removeGroup(string groupName) {
521 //save for later in case removing a group means we need to remove a seq.
522 map<int, string> reverse;
523 for (map<string, int>::iterator it = indexNameMap.begin(); it !=indexNameMap.end(); it++) { reverse[it->second] = it->first; }
525 map<string, int>::iterator it = indexGroupMap.find(groupName);
526 if (it == indexGroupMap.end()) {
527 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
529 int indexOfGroupToRemove = it->second;
530 map<string, int> currentGroupIndex = indexGroupMap;
531 vector<string> newGroups;
532 for (int i = 0; i < groups.size(); i++) {
533 if (groups[i] != groupName) {
534 newGroups.push_back(groups[i]);
535 indexGroupMap[groups[i]] = newGroups.size()-1;
538 indexGroupMap.erase(groupName);
540 totalGroups.erase(totalGroups.begin()+indexOfGroupToRemove);
543 map<string, int> newIndexNameMap;
544 for (int i = 0; i < counts.size(); i++) {
545 int num = counts[i][indexOfGroupToRemove];
546 counts[i].erase(counts[i].begin()+indexOfGroupToRemove);
549 if (totals[i] == 0) { //your sequences are only from the group we want to remove, then remove you.
550 counts.erase(counts.begin()+i);
551 totals.erase(totals.begin()+i);
555 newIndexNameMap[reverse[thisIndex]] = i;
558 indexNameMap = newIndexNameMap;
560 if (groups.size() == 0) { hasGroups = false; }
562 }else { m->mothurOut("[ERROR]: your count table does not contain group information, can not remove group " + groupName + ".\n"); m->control_pressed = true; }
566 catch(exception& e) {
567 m->errorOut(e, "CountTable", "removeGroup");
571 /************************************************************/
572 //vector of groups for the seq
573 vector<string> CountTable::getGroups(string seqName) {
575 vector<string> thisGroups;
577 vector<int> thisCounts = getGroupCounts(seqName);
578 for (int i = 0; i < thisCounts.size(); i++) {
579 if (thisCounts[i] != 0) { thisGroups.push_back(groups[i]); }
581 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
585 catch(exception& e) {
586 m->errorOut(e, "CountTable", "getGroups");
590 /************************************************************/
591 //total number of seqs represented by seq
592 int CountTable::renameSeq(string oldSeqName, string newSeqName) {
595 map<string, int>::iterator it = indexNameMap.find(oldSeqName);
596 if (it == indexNameMap.end()) {
597 if (hasGroupInfo()) {
598 //look for it in names of groups to see if the user accidently used the wrong file
599 if (m->inUsersGroups(oldSeqName, groups)) {
600 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();
603 m->mothurOut("[ERROR]: " + oldSeqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
605 int index = it->second;
606 indexNameMap.erase(it);
607 indexNameMap[newSeqName] = index;
612 catch(exception& e) {
613 m->errorOut(e, "CountTable", "renameSeq");
618 /************************************************************/
619 //total number of seqs represented by seq
620 int CountTable::getNumSeqs(string seqName) {
623 map<string, int>::iterator it = indexNameMap.find(seqName);
624 if (it == indexNameMap.end()) {
625 if (hasGroupInfo()) {
626 //look for it in names of groups to see if the user accidently used the wrong file
627 if (m->inUsersGroups(seqName, groups)) {
628 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();
631 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
633 return totals[it->second];
638 catch(exception& e) {
639 m->errorOut(e, "CountTable", "getNumSeqs");
643 /************************************************************/
644 //returns unique index for sequence like get in NameAssignment
645 int CountTable::get(string seqName) {
648 map<string, int>::iterator it = indexNameMap.find(seqName);
649 if (it == indexNameMap.end()) {
650 if (hasGroupInfo()) {
651 //look for it in names of groups to see if the user accidently used the wrong file
652 if (m->inUsersGroups(seqName, groups)) {
653 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();
656 m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
657 }else { return it->second; }
661 catch(exception& e) {
662 m->errorOut(e, "CountTable", "get");
666 /************************************************************/
667 //add seqeunce without group info
668 int CountTable::push_back(string seqName) {
670 map<string, int>::iterator it = indexNameMap.find(seqName);
671 if (it == indexNameMap.end()) {
672 if (hasGroups) { m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true; }
673 indexNameMap[seqName] = uniques;
678 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); m->control_pressed = true;
683 catch(exception& e) {
684 m->errorOut(e, "CountTable", "push_back");
688 /************************************************************/
690 int CountTable::remove(string seqName) {
692 map<string, int>::iterator it = indexNameMap.find(seqName);
693 if (it != indexNameMap.end()) {
695 if (hasGroups){ //remove this sequences counts from group totals
696 for (int i = 0; i < totalGroups.size(); i++) { totalGroups[i] -= counts[it->second][i]; counts[it->second][i] = 0; }
698 int thisTotal = totals[it->second]; totals[it->second] = 0;
700 indexNameMap.erase(it);
702 if (hasGroupInfo()) {
703 //look for it in names of groups to see if the user accidently used the wrong file
704 if (m->inUsersGroups(seqName, groups)) {
705 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();
708 m->mothurOut("[ERROR]: Your count table contains does not include " + seqName + ", cannot remove."); m->mothurOutEndLine(); m->control_pressed = true;
713 catch(exception& e) {
714 m->errorOut(e, "CountTable", "push_back");
718 /************************************************************/
719 //add seqeunce without group info
720 int CountTable::push_back(string seqName, int thisTotal) {
722 map<string, int>::iterator it = indexNameMap.find(seqName);
723 if (it == indexNameMap.end()) {
724 if (hasGroups) { m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true; }
725 indexNameMap[seqName] = uniques;
726 totals.push_back(thisTotal);
730 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); m->control_pressed = true;
735 catch(exception& e) {
736 m->errorOut(e, "CountTable", "push_back");
740 /************************************************************/
741 //add sequence with group info
742 int CountTable::push_back(string seqName, vector<int> groupCounts) {
744 map<string, int>::iterator it = indexNameMap.find(seqName);
745 if (it == indexNameMap.end()) {
746 if ((hasGroups) && (groupCounts.size() != getNumGroups())) { m->mothurOut("[ERROR]: Your count table has a " + toString(getNumGroups()) + " groups and " + seqName + " has " + toString(groupCounts.size()) + ", please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
748 for (int i = 0; i < getNumGroups(); i++) { totalGroups[i] += groupCounts[i]; thisTotal += groupCounts[i]; }
749 if (hasGroups) { counts.push_back(groupCounts); }
750 indexNameMap[seqName] = uniques;
751 totals.push_back(thisTotal);
755 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); m->control_pressed = true;
760 catch(exception& e) {
761 m->errorOut(e, "CountTable", "push_back");
766 /************************************************************/
767 //create ListVector from uniques
768 ListVector CountTable::getListVector() {
770 ListVector list(indexNameMap.size());
771 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
772 if (m->control_pressed) { break; }
773 list.set(it->second, it->first);
777 catch(exception& e) {
778 m->errorOut(e, "CountTable", "getListVector");
783 /************************************************************/
784 //returns the names of all unique sequences in file
785 vector<string> CountTable::getNamesOfSeqs() {
787 vector<string> names;
788 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
789 names.push_back(it->first);
794 catch(exception& e) {
795 m->errorOut(e, "CountTable", "getNamesOfSeqs");
799 /************************************************************/
800 //returns the names of all unique sequences in file mapped to their seqCounts
801 map<string, int> CountTable::getNameMap() {
803 map<string, int> names;
804 for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
805 names[it->first] = totals[it->second];
810 catch(exception& e) {
811 m->errorOut(e, "CountTable", "getNameMap");
815 /************************************************************/
816 //returns the names of all unique sequences in file
817 vector<string> CountTable::getNamesOfSeqs(string group) {
819 vector<string> names;
821 map<string, int>::iterator it = indexGroupMap.find(group);
822 if (it == indexGroupMap.end()) {
823 m->mothurOut("[ERROR]: " + group + " is not in your count table. Please correct.\n"); m->control_pressed = true;
825 for (map<string, int>::iterator it2 = indexNameMap.begin(); it2 != indexNameMap.end(); it2++) {
826 if (counts[it2->second][it->second] != 0) { names.push_back(it2->first); }
829 }else{ m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
833 catch(exception& e) {
834 m->errorOut(e, "CountTable", "getNamesOfSeqs");
838 /************************************************************/
839 //merges counts of seq1 and seq2, saving in seq1
840 int CountTable::mergeCounts(string seq1, string seq2) {
842 map<string, int>::iterator it = indexNameMap.find(seq1);
843 if (it == indexNameMap.end()) {
844 if (hasGroupInfo()) {
845 //look for it in names of groups to see if the user accidently used the wrong file
846 if (m->inUsersGroups(seq1, groups)) {
847 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();
850 m->mothurOut("[ERROR]: " + seq1 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
852 map<string, int>::iterator it2 = indexNameMap.find(seq2);
853 if (it2 == indexNameMap.end()) {
854 if (hasGroupInfo()) {
855 //look for it in names of groups to see if the user accidently used the wrong file
856 if (m->inUsersGroups(seq2, groups)) {
857 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();
860 m->mothurOut("[ERROR]: " + seq2 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
863 for (int i = 0; i < groups.size(); i++) { counts[it->second][i] += counts[it2->second][i]; }
864 totals[it->second] += totals[it2->second];
866 indexNameMap.erase(it2);
871 catch(exception& e) {
872 m->errorOut(e, "CountTable", "getNamesOfSeqs");
876 /************************************************************/
877 int CountTable::copy(CountTable* ct) {
879 vector<string> thisGroups = ct->getNamesOfGroups();
880 for (int i = 0; i < thisGroups.size(); i++) { addGroup(thisGroups[i]); }
881 vector<string> names = ct->getNamesOfSeqs();
883 for (int i = 0; i < names.size(); i++) {
884 vector<int> thisCounts = ct->getGroupCounts(names[i]);
885 push_back(names[i], thisCounts);
890 catch(exception& e) {
891 m->errorOut(e, "CountTable", "copy");
896 /************************************************************/