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