]> git.donarmstrong.com Git - mothur.git/blob - counttable.cpp
added count file to get.oturep, pre.cluster, screen.seqs, tree.shared. made remove...
[mothur.git] / counttable.cpp
1 //
2 //  counttable.cpp
3 //  Mothur
4 //
5 //  Created by Sarah Westcott on 6/26/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "counttable.h"
10
11 /************************************************************/
12 int CountTable::createTable(set<string>& n, map<string, string>& g, set<string>& gs) {
13     try {
14         int numGroups = 0;
15         groups.clear();
16         totalGroups.clear();
17         indexGroupMap.clear();
18         indexNameMap.clear();
19         counts.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);
23         
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);
28         
29         uniques = 0;
30         total = 0;
31         for (set<string>::iterator it = n.begin(); it != n.end(); it++) {
32             
33             if (m->control_pressed) { break; }
34             
35             string seqName = *it;
36             
37             vector<int> groupCounts; groupCounts.resize(numGroups, 0);
38             map<string, string>::iterator itGroup = g.find(seqName);
39             
40             if (itGroup != g.end()) {   
41                 groupCounts[indexGroupMap[itGroup->second]] = 1; 
42                 totalGroups[indexGroupMap[itGroup->second]]++;
43             }else { m->mothurOut("[ERROR]: Your group file does not contain " + seqName + ". Please correct."); m->mothurOutEndLine(); }
44             
45             map<string, int>::iterator it2 = indexNameMap.find(seqName);
46             if (it2 == indexNameMap.end()) {
47                 if (hasGroups) {  counts.push_back(groupCounts);  }
48                 indexNameMap[seqName] = uniques;
49                 totals.push_back(1);
50                 total++;
51                 uniques++;
52             }
53         }
54         
55         if (hasGroups) {
56             for (int i = 0; i < totalGroups.size(); i++) {
57                 if (totalGroups[i] == 0) { m->mothurOut("\nRemoving group: " + groups[i] + " because all sequences have been removed.\n"); removeGroup(groups[i]); i--; }
58             }
59         }
60
61         return 0;
62     }
63         catch(exception& e) {
64                 m->errorOut(e, "CountTable", "createTable");
65                 exit(1);
66         }
67 }
68 /************************************************************/
69 bool CountTable::testGroups(string file) {
70     try {
71         m = MothurOut::getInstance(); hasGroups = false; total = 0;
72         ifstream in;
73         m->openInputFile(file, in);
74     
75         string headers = m->getline(in); m->gobble(in);
76         vector<string> columnHeaders = m->splitWhiteSpace(headers);
77         if (columnHeaders.size() > 2) { hasGroups = true;   }
78         return hasGroups;
79     }
80         catch(exception& e) {
81                 m->errorOut(e, "CountTable", "readTable");
82                 exit(1);
83         }
84 }
85 /************************************************************/
86 int CountTable::createTable(string namefile, string groupfile, bool createGroup) {
87     try {
88         
89         if (namefile == "") { m->mothurOut("[ERROR]: namefile cannot be blank when creating a count table.\n"); m->control_pressed = true; }
90                                            
91         GroupMap* groupMap;
92         int numGroups = 0;
93         groups.clear();
94         totalGroups.clear();
95         indexGroupMap.clear();
96         indexNameMap.clear();
97         counts.clear();
98         map<int, string> originalGroupIndexes;
99         
100         if (groupfile != "") { 
101             hasGroups = true;
102             groupMap = new GroupMap(groupfile); groupMap->readMap();
103             numGroups = groupMap->getNumGroups();
104             groups = groupMap->getNamesOfGroups();
105             totalGroups.resize(numGroups, 0);
106         }else if(createGroup) {
107             hasGroups = true;
108             numGroups = 1;
109             groups.push_back("Group1");
110             totalGroups.resize(numGroups, 0);
111         }
112                 //sort groups to keep consistent with how we store the groups in groupmap
113         sort(groups.begin(), groups.end());
114         for (int i = 0; i < groups.size(); i++) {  indexGroupMap[groups[i]] = i; }
115         m->setAllGroups(groups);
116         
117         bool error = false;
118         string name;
119         uniques = 0;
120         total = 0;
121         
122         
123         //open input file
124         ifstream in;
125         m->openInputFile(namefile, in);
126         
127         int total = 0;
128         while (!in.eof()) {
129             if (m->control_pressed) { break; }
130             
131             string firstCol, secondCol;
132             in >> firstCol; m->gobble(in); in >> secondCol; m->gobble(in);
133             
134             vector<string> names;
135             m->splitAtChar(secondCol, names, ',');
136             
137             map<string, int> groupCounts;
138             int thisTotal = 0;
139             if (groupfile != "") {
140                 //set to 0
141                 for (int i = 0; i < groups.size(); i++) { groupCounts[groups[i]] = 0; }
142                 
143                 //get counts for each of the users groups
144                 for (int i = 0; i < names.size(); i++) {
145                     string group = groupMap->getGroup(names[i]);
146                     
147                     if (group == "not found") { m->mothurOut("[ERROR]: " + names[i] + " is not in your groupfile, please correct."); m->mothurOutEndLine(); error=true; }
148                     else {
149                         map<string, int>::iterator it = groupCounts.find(group);
150                         
151                         //if not found, then this sequence is not from a group we care about
152                         if (it != groupCounts.end()) {
153                             it->second++;
154                             thisTotal++;
155                         }
156                     }
157                 }
158             }else if (createGroup) {
159                 groupCounts["Group1"]=0;
160                 for (int i = 0; i < names.size(); i++) {
161                     string group = "Group1";
162                     groupCounts["Group1"]++; thisTotal++;
163                 }
164             }else { thisTotal = names.size();  }
165             
166             //if group info, then read it
167             vector<int> thisGroupsCount; thisGroupsCount.resize(numGroups, 0);
168             for (int i = 0; i < numGroups; i++) {  
169                 thisGroupsCount[i] = groupCounts[groups[i]]; 
170                 totalGroups[i] += thisGroupsCount[i]; 
171             }
172             
173             map<string, int>::iterator it = indexNameMap.find(firstCol);
174             if (it == indexNameMap.end()) {
175                 if (hasGroups) {  counts.push_back(thisGroupsCount);  }
176                 indexNameMap[firstCol] = uniques;
177                 totals.push_back(thisTotal);
178                 total += thisTotal;
179                 uniques++;
180             }else {
181                 error = true;
182                 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + firstCol + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); 
183             }
184         }
185         in.close();
186                 
187         if (error) { m->control_pressed = true; }
188         else { //check for zero groups
189             if (hasGroups) {
190                 for (int i = 0; i < totalGroups.size(); i++) {
191                     if (totalGroups[i] == 0) { m->mothurOut("\nRemoving group: " + groups[i] + " because all sequences have been removed.\n"); removeGroup(groups[i]); i--; }
192                 }
193             }
194         }
195         if (groupfile != "") { delete groupMap; }
196         
197         return 0;
198     }
199         catch(exception& e) {
200                 m->errorOut(e, "CountTable", "createTable");
201                 exit(1);
202         }
203 }
204 /************************************************************/
205 int CountTable::readTable(string file) {
206     try {
207         filename = file;
208         ifstream in;
209         m->openInputFile(filename, in);
210         
211         string headers = m->getline(in); m->gobble(in);
212         vector<string> columnHeaders = m->splitWhiteSpace(headers);
213         
214         int numGroups = 0;
215         groups.clear();
216         totalGroups.clear();
217         indexGroupMap.clear();
218         indexNameMap.clear();
219         counts.clear();
220         map<int, string> originalGroupIndexes;
221         if (columnHeaders.size() > 2) { hasGroups = true; numGroups = columnHeaders.size() - 2;  }
222         for (int i = 2; i < columnHeaders.size(); i++) {  groups.push_back(columnHeaders[i]);  originalGroupIndexes[i-2] = columnHeaders[i]; totalGroups.push_back(0); }
223         //sort groups to keep consistent with how we store the groups in groupmap
224         sort(groups.begin(), groups.end());
225         for (int i = 0; i < groups.size(); i++) {  indexGroupMap[groups[i]] = i; }
226         m->setAllGroups(groups);
227         
228         bool error = false;
229         string name;
230         int thisTotal;
231         uniques = 0;
232         total = 0;
233         while (!in.eof()) {
234             
235             if (m->control_pressed) { break; }
236             
237             in >> name; m->gobble(in); in >> thisTotal; m->gobble(in);
238             if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + toString(thisTotal) + "\n"); }
239             
240             //if group info, then read it
241             vector<int> groupCounts; groupCounts.resize(numGroups, 0);
242             for (int i = 0; i < numGroups; i++) {  int thisIndex = indexGroupMap[originalGroupIndexes[i]]; in >> groupCounts[thisIndex]; m->gobble(in); totalGroups[thisIndex] += groupCounts[thisIndex];  }
243             
244             map<string, int>::iterator it = indexNameMap.find(name);
245             if (it == indexNameMap.end()) {
246                 if (hasGroups) {  counts.push_back(groupCounts);  }
247                 indexNameMap[name] = uniques;
248                 totals.push_back(thisTotal);
249                 total += thisTotal;
250                 uniques++;
251             }else {
252                 error = true;
253                 m->mothurOut("[ERROR]: Your count table contains more than 1 sequence named " + name + ", sequence names must be unique. Please correct."); m->mothurOutEndLine(); 
254             }
255         }
256         in.close();
257         
258         if (error) { m->control_pressed = true; }
259         else { //check for zero groups
260             if (hasGroups) {
261                 for (int i = 0; i < totalGroups.size(); i++) {
262                     if (totalGroups[i] == 0) { m->mothurOut("\nRemoving group: " + groups[i] + " because all sequences have been removed.\n"); removeGroup(groups[i]); i--; }
263                 }
264             }
265         }
266         
267         return 0;
268     }
269         catch(exception& e) {
270                 m->errorOut(e, "CountTable", "readTable");
271                 exit(1);
272         }
273 }
274 /************************************************************/
275 int CountTable::printTable(string file) {
276     try {
277         ofstream out;
278         m->openOutputFile(file, out); 
279                 out << "Representative_Sequence\ttotal\t";
280         for (int i = 0; i < groups.size(); i++) { out << groups[i] << '\t'; }
281         out << endl;
282         
283         for (map<string, int>::iterator itNames = indexNameMap.begin(); itNames != indexNameMap.end(); itNames++) {
284             out << itNames->first << '\t' << totals[itNames->second] << '\t';
285             if (hasGroups) {
286                 
287                 for (int i = 0; i < groups.size(); i++) {
288                     out << counts[itNames->second][i] << '\t';
289                 }
290             }
291             out << endl;
292         }
293         out.close();
294         return 0;
295     }
296         catch(exception& e) {
297                 m->errorOut(e, "CountTable", "printTable");
298                 exit(1);
299         }
300 }
301 /************************************************************/
302 int CountTable::printHeaders(ofstream& out) {
303     try {
304                 out << "Representative_Sequence\ttotal\t";
305         for (int i = 0; i < groups.size(); i++) { out << groups[i] << '\t'; }
306         out << endl;
307         return 0;
308     }
309         catch(exception& e) {
310                 m->errorOut(e, "CountTable", "printHeaders");
311                 exit(1);
312         }
313 }
314 /************************************************************/
315 int CountTable::printSeq(ofstream& out, string seqName) {
316     try {
317                 map<string, int>::iterator it = indexNameMap.find(seqName);
318         if (it == indexNameMap.end()) {
319             m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
320         }else { 
321             out << it->first << '\t' << totals[it->second] << '\t';
322             if (hasGroups) {
323                 for (int i = 0; i < groups.size(); i++) {
324                     out << counts[it->second][i] << '\t';
325                 }
326             }
327             out << endl;
328         }
329         return 0;
330     }
331         catch(exception& e) {
332                 m->errorOut(e, "CountTable", "printSeq");
333                 exit(1);
334         }
335 }
336 /************************************************************/
337 //group counts for a seq
338 vector<int> CountTable::getGroupCounts(string seqName) {
339     try {
340         vector<int> temp;
341         if (hasGroups) {
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;
345             }else { 
346                 temp = counts[it->second];
347             }
348         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n"); m->control_pressed = true; }
349         
350         return temp;
351     }
352         catch(exception& e) {
353                 m->errorOut(e, "CountTable", "getGroupCounts");
354                 exit(1);
355         }
356 }
357 /************************************************************/
358 //total number of sequences for the group
359 int CountTable::getGroupCount(string groupName) {
360     try {
361         if (hasGroups) {
362             map<string, int>::iterator it = indexGroupMap.find(groupName);
363             if (it == indexGroupMap.end()) {
364                 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
365             }else { 
366                 return totalGroups[it->second];
367             }
368         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n");  m->control_pressed = true; }
369
370         return 0;
371     }
372         catch(exception& e) {
373                 m->errorOut(e, "CountTable", "getGroupCount");
374                 exit(1);
375         }
376 }
377 /************************************************************/
378 //total number of sequences for the seq for the group
379 int CountTable::getGroupCount(string seqName, string groupName) {
380     try {
381         if (hasGroups) {
382             map<string, int>::iterator it = indexGroupMap.find(groupName);
383             if (it == indexGroupMap.end()) {
384                 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
385             }else { 
386                 map<string, int>::iterator it2 = indexNameMap.find(seqName);
387                 if (it2 == indexNameMap.end()) {
388                     m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
389                 }else { 
390                     return counts[it2->second][it->second];
391                 }
392             }
393         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n");  m->control_pressed = true; }
394         
395         return 0;
396     }
397         catch(exception& e) {
398                 m->errorOut(e, "CountTable", "getGroupCount");
399                 exit(1);
400         }
401 }
402 /************************************************************/
403 //set the number of sequences for the seq for the group
404 int CountTable::setAbund(string seqName, string groupName, int num) {
405     try {
406         if (hasGroups) {
407             map<string, int>::iterator it = indexGroupMap.find(groupName);
408             if (it == indexGroupMap.end()) {
409                 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
410             }else { 
411                 map<string, int>::iterator it2 = indexNameMap.find(seqName);
412                 if (it2 == indexNameMap.end()) {
413                     m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
414                 }else { 
415                     int oldCount = counts[it2->second][it->second];
416                     counts[it2->second][it->second] = num;
417                     totalGroups[it->second] += (num - oldCount);
418                     total += (num - oldCount);
419                     totals[it2->second] += (num - oldCount);
420                 }
421             }
422         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n");  m->control_pressed = true; }
423         
424         return 0;
425     }
426         catch(exception& e) {
427                 m->errorOut(e, "CountTable", "set");
428                 exit(1);
429         }
430 }
431 /************************************************************/
432 //add group
433 int CountTable::addGroup(string groupName) {
434     try {        
435         bool sanity = m->inUsersGroups(groupName, groups);
436         if (sanity) { m->mothurOut("[ERROR]: " + groupName + " is already in the count table, cannot add again.\n"); m->control_pressed = true;  return 0; }
437         
438         groups.push_back(groupName);
439         if (!hasGroups) { counts.resize(uniques);  }
440         
441         for (int i = 0; i < counts.size(); i++) { counts[i].push_back(0); }
442         totalGroups.push_back(0);
443         indexGroupMap[groupName] = groups.size()-1;
444         map<string, int> originalGroupMap = indexGroupMap;
445         
446         //important to play well with others, :)
447         sort(groups.begin(), groups.end());
448         
449         //fix indexGroupMap && totalGroups
450         vector<int> newTotals; newTotals.resize(groups.size(), 0);
451         for (int i = 0; i < groups.size(); i++) {  
452             indexGroupMap[groups[i]] = i;  
453             //find original spot of group[i]
454             int index = originalGroupMap[groups[i]];
455             newTotals[i] = totalGroups[index];
456         }
457         totalGroups = newTotals;
458         
459         //fix counts vectors
460         for (int i = 0; i < counts.size(); i++) {
461             vector<int> newCounts; newCounts.resize(groups.size(), 0);
462             for (int j = 0; j < groups.size(); j++) {  
463                 //find original spot of group[i]
464                 int index = originalGroupMap[groups[j]];
465                 newCounts[j] = counts[i][index];
466             }
467             counts[i] = newCounts;
468         }
469         hasGroups = true;
470         m->setAllGroups(groups);
471         
472         return 0;
473     }
474         catch(exception& e) {
475                 m->errorOut(e, "CountTable", "addGroup");
476                 exit(1);
477         }
478 }
479 /************************************************************/
480 //remove group
481 int CountTable::removeGroup(string groupName) {
482     try {        
483         if (hasGroups) {
484             map<string, int>::iterator it = indexGroupMap.find(groupName);
485             if (it == indexGroupMap.end()) {
486                 m->mothurOut("[ERROR]: " + groupName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
487             }else { 
488                 int indexOfGroupToRemove = it->second;
489                 map<string, int> currentGroupIndex = indexGroupMap;
490                 vector<string> newGroups;
491                 for (int i = 0; i < groups.size(); i++) {
492                     if (groups[i] != groupName) { 
493                         newGroups.push_back(groups[i]);
494                         indexGroupMap[groups[i]] = i;
495                     }
496                 }
497                 indexGroupMap.erase(groupName);
498                 groups = newGroups;
499                 totalGroups.erase(totalGroups.begin()+indexOfGroupToRemove);
500                  
501                 for (int i = 0; i < counts.size(); i++) {
502                     int num = counts[i][indexOfGroupToRemove];
503                     counts[i].erase(counts[i].begin()+indexOfGroupToRemove);
504                     totals[i] -= num;
505                     total -= num;
506                     if (totals[i] == 0) { //your sequences are only from the group we want to remove, then remove you.
507                         counts.erase(counts.begin()+i);
508                         totals.erase(totals.begin()+i);
509                         uniques--;
510                         i--;
511                     }
512                 }
513                 if (groups.size() == 0) { hasGroups = false; }
514             }
515         }else { m->mothurOut("[ERROR]: your count table does not contain group information, can not remove group " + groupName + ".\n"); m->control_pressed = true; }
516     
517         return 0;
518     }
519         catch(exception& e) {
520                 m->errorOut(e, "CountTable", "removeGroup");
521                 exit(1);
522         }
523 }
524 /************************************************************/
525 //vector of groups for the seq
526 vector<string> CountTable::getGroups(string seqName) {
527     try {
528         vector<string> thisGroups;
529         if (hasGroups) {
530             vector<int> thisCounts = getGroupCounts(seqName);
531             for (int i = 0; i < thisCounts.size(); i++) {  
532                 if (thisCounts[i] != 0) {  thisGroups.push_back(groups[i]); }
533             } 
534         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n");  m->control_pressed = true; }
535         
536         return thisGroups;
537     }
538         catch(exception& e) {
539                 m->errorOut(e, "CountTable", "getGroups");
540                 exit(1);
541         }
542 }
543 /************************************************************/
544 //total number of seqs represented by seq
545 int CountTable::renameSeq(string oldSeqName, string newSeqName) {
546     try {
547         
548         map<string, int>::iterator it = indexNameMap.find(oldSeqName);
549         if (it == indexNameMap.end()) {
550             m->mothurOut("[ERROR]: " + oldSeqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
551         }else {  
552             int index = it->second;
553             indexNameMap.erase(it);
554             indexNameMap[newSeqName] = index;
555         }
556         
557         return 0;
558     }
559         catch(exception& e) {
560                 m->errorOut(e, "CountTable", "renameSeq");
561                 exit(1);
562         }
563 }
564
565 /************************************************************/
566 //total number of seqs represented by seq
567 int CountTable::getNumSeqs(string seqName) {
568     try {
569                 
570         map<string, int>::iterator it = indexNameMap.find(seqName);
571         if (it == indexNameMap.end()) {
572             m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
573         }else { 
574             return totals[it->second];
575         }
576
577         return 0;
578     }
579         catch(exception& e) {
580                 m->errorOut(e, "CountTable", "getNumSeqs");
581                 exit(1);
582         }
583 }
584 /************************************************************/
585 //returns unique index for sequence like get in NameAssignment
586 int CountTable::get(string seqName) {
587     try {
588         
589         map<string, int>::iterator it = indexNameMap.find(seqName);
590         if (it == indexNameMap.end()) {
591             m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
592         }else { return it->second; }
593         
594         return -1;
595     }
596         catch(exception& e) {
597                 m->errorOut(e, "CountTable", "get");
598                 exit(1);
599         }
600 }
601 /************************************************************/
602 //add seqeunce without group info
603 int CountTable::push_back(string seqName) {
604     try {
605         map<string, int>::iterator it = indexNameMap.find(seqName);
606         if (it == indexNameMap.end()) {
607             if (hasGroups) {  m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true;  }
608             indexNameMap[seqName] = uniques;
609             totals.push_back(1);
610             total++;
611             uniques++;
612         }else {
613             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;
614         }
615         
616         return 0;
617     }
618         catch(exception& e) {
619                 m->errorOut(e, "CountTable", "push_back");
620                 exit(1);
621         }
622 }
623 /************************************************************/
624 //remove sequence
625 int CountTable::remove(string seqName) {
626     try {
627         map<string, int>::iterator it = indexNameMap.find(seqName);
628         if (it != indexNameMap.end()) {
629             uniques--;
630             if (hasGroups){ //remove this sequences counts from group totals
631                 for (int i = 0; i < totalGroups.size(); i++) {  totalGroups[i] -= counts[it->second][i];  counts[it->second][i] = 0; }
632             }
633             int thisTotal = totals[it->second]; totals[it->second] = 0;
634             total -= thisTotal;
635             indexNameMap.erase(it);
636         }else {
637             m->mothurOut("[ERROR]: Your count table contains does not include " + seqName + ", cannot remove."); m->mothurOutEndLine(); m->control_pressed = true;
638         }
639         
640         return 0;
641     }
642         catch(exception& e) {
643                 m->errorOut(e, "CountTable", "push_back");
644                 exit(1);
645         }
646 }
647 /************************************************************/
648 //add seqeunce without group info
649 int CountTable::push_back(string seqName, int thisTotal) {
650     try {
651         map<string, int>::iterator it = indexNameMap.find(seqName);
652         if (it == indexNameMap.end()) {
653             if (hasGroups) {  m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true;  }
654             indexNameMap[seqName] = uniques;
655             totals.push_back(thisTotal);
656             total+=thisTotal;
657             uniques++;
658         }else {
659             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;
660         }
661         
662         return 0;
663     }
664         catch(exception& e) {
665                 m->errorOut(e, "CountTable", "push_back");
666                 exit(1);
667         }
668 }
669 /************************************************************/
670 //add sequence with group info
671 int CountTable::push_back(string seqName, vector<int> groupCounts) {
672     try {
673         map<string, int>::iterator it = indexNameMap.find(seqName);
674         if (it == indexNameMap.end()) {
675             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;  }
676             int thisTotal = 0;
677             for (int i = 0; i < getNumGroups(); i++) {   totalGroups[i] += groupCounts[i];  thisTotal += groupCounts[i]; }
678             if (hasGroups) {  counts.push_back(groupCounts);  }
679             indexNameMap[seqName] = uniques;
680             totals.push_back(thisTotal);
681             total+= thisTotal;
682             uniques++;
683         }else {
684             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;
685         }
686         
687         return 0;
688     }
689         catch(exception& e) {
690                 m->errorOut(e, "CountTable", "push_back");
691                 exit(1);
692         }
693 }
694
695 /************************************************************/
696 //create ListVector from uniques
697 ListVector CountTable::getListVector() {
698     try {
699         ListVector list(indexNameMap.size());
700         for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) { 
701             if (m->control_pressed) { break; }
702             list.set(it->second, it->first); 
703         }
704         return list;
705     }
706         catch(exception& e) {
707                 m->errorOut(e, "CountTable", "getListVector");
708                 exit(1);
709         }
710 }
711
712 /************************************************************/
713 //returns the names of all unique sequences in file
714 vector<string> CountTable::getNamesOfSeqs() {
715     try {
716         vector<string> names;
717         for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
718             names.push_back(it->first);
719         }
720                 
721         return names;
722     }
723         catch(exception& e) {
724                 m->errorOut(e, "CountTable", "getNamesOfSeqs");
725                 exit(1);
726         }
727 }
728 /************************************************************/
729 //returns the names of all unique sequences in file mapped to their seqCounts
730 map<string, int> CountTable::getNameMap() {
731     try {
732         map<string, int> names;
733         for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
734             names[it->first] = totals[it->second];
735         }
736         
737         return names;
738     }
739         catch(exception& e) {
740                 m->errorOut(e, "CountTable", "getNameMap");
741                 exit(1);
742         }
743 }
744 /************************************************************/
745 //returns the names of all unique sequences in file
746 vector<string> CountTable::getNamesOfSeqs(string group) {
747     try {
748         vector<string> names;
749         if (hasGroups) {
750             map<string, int>::iterator it = indexGroupMap.find(group);
751             if (it == indexGroupMap.end()) {
752                 m->mothurOut("[ERROR]: " + group + " is not in your count table. Please correct.\n"); m->control_pressed = true;
753             }else { 
754                 for (map<string, int>::iterator it2 = indexNameMap.begin(); it2 != indexNameMap.end(); it2++) {
755                     if (counts[it2->second][it->second] != 0) {  names.push_back(it2->first); }
756                 }
757             }
758         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n");  m->control_pressed = true; }
759         
760         return names;
761     }
762         catch(exception& e) {
763                 m->errorOut(e, "CountTable", "getNamesOfSeqs");
764                 exit(1);
765         }
766 }
767 /************************************************************/
768 //merges counts of seq1 and seq2, saving in seq1
769 int CountTable::mergeCounts(string seq1, string seq2) {
770     try {
771         map<string, int>::iterator it = indexNameMap.find(seq1);
772         if (it == indexNameMap.end()) {
773             m->mothurOut("[ERROR]: " + seq1 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
774         }else { 
775             map<string, int>::iterator it2 = indexNameMap.find(seq2);
776             if (it2 == indexNameMap.end()) {
777                 m->mothurOut("[ERROR]: " + seq2 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
778             }else { 
779                 //merge data
780                 for (int i = 0; i < groups.size(); i++) { counts[it->second][i] += counts[it2->second][i]; }
781                 totals[it->second] += totals[it2->second];
782                 uniques--;
783                 indexNameMap.erase(it2); 
784             }
785         }
786         return 0;
787     }
788         catch(exception& e) {
789                 m->errorOut(e, "CountTable", "getNamesOfSeqs");
790                 exit(1);
791         }
792 }
793 /************************************************************/
794 int CountTable::copy(CountTable* ct) {
795     try {
796         vector<string> thisGroups = ct->getNamesOfGroups();
797         for (int i = 0; i < thisGroups.size(); i++) { addGroup(thisGroups[i]); }
798         vector<string> names = ct->getNamesOfSeqs();
799                                                                
800         for (int i = 0; i < names.size(); i++) {
801             vector<int> thisCounts = ct->getGroupCounts(names[i]);
802             push_back(names[i], thisCounts);
803         }
804                                                                
805         return 0;
806     }
807         catch(exception& e) {
808                 m->errorOut(e, "CountTable", "copy");
809                 exit(1);
810         }
811 }
812
813 /************************************************************/
814
815