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