]> git.donarmstrong.com Git - mothur.git/blob - counttable.cpp
major change to the tree class to use the count table class instead of tree map....
[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         
451         return 0;
452     }
453         catch(exception& e) {
454                 m->errorOut(e, "CountTable", "addGroup");
455                 exit(1);
456         }
457 }
458 /************************************************************/
459 //vector of groups for the seq
460 vector<string> CountTable::getGroups(string seqName) {
461     try {
462         vector<string> thisGroups;
463         if (hasGroups) {
464             vector<int> thisCounts = getGroupCounts(seqName);
465             for (int i = 0; i < thisCounts.size(); i++) {  
466                 if (thisCounts[i] != 0) {  thisGroups.push_back(groups[i]); }
467             } 
468         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n");  m->control_pressed = true; }
469         
470         return thisGroups;
471     }
472         catch(exception& e) {
473                 m->errorOut(e, "CountTable", "getGroups");
474                 exit(1);
475         }
476 }
477 /************************************************************/
478 //total number of seqs represented by seq
479 int CountTable::renameSeq(string oldSeqName, string newSeqName) {
480     try {
481         
482         map<string, int>::iterator it = indexNameMap.find(oldSeqName);
483         if (it == indexNameMap.end()) {
484             m->mothurOut("[ERROR]: " + oldSeqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
485         }else {  
486             int index = it->second;
487             indexNameMap.erase(it);
488             indexNameMap[newSeqName] = index;
489         }
490         
491         return 0;
492     }
493         catch(exception& e) {
494                 m->errorOut(e, "CountTable", "renameSeq");
495                 exit(1);
496         }
497 }
498
499 /************************************************************/
500 //total number of seqs represented by seq
501 int CountTable::getNumSeqs(string seqName) {
502     try {
503                 
504         map<string, int>::iterator it = indexNameMap.find(seqName);
505         if (it == indexNameMap.end()) {
506             m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
507         }else { 
508             return totals[it->second];
509         }
510
511         return 0;
512     }
513         catch(exception& e) {
514                 m->errorOut(e, "CountTable", "getNumSeqs");
515                 exit(1);
516         }
517 }
518 /************************************************************/
519 //returns unique index for sequence like get in NameAssignment
520 int CountTable::get(string seqName) {
521     try {
522         
523         map<string, int>::iterator it = indexNameMap.find(seqName);
524         if (it == indexNameMap.end()) {
525             m->mothurOut("[ERROR]: " + seqName + " is not in your count table. Please correct.\n"); m->control_pressed = true;
526         }else { return it->second; }
527         
528         return -1;
529     }
530         catch(exception& e) {
531                 m->errorOut(e, "CountTable", "get");
532                 exit(1);
533         }
534 }
535 /************************************************************/
536 //add seqeunce without group info
537 int CountTable::push_back(string seqName) {
538     try {
539         map<string, int>::iterator it = indexNameMap.find(seqName);
540         if (it == indexNameMap.end()) {
541             if (hasGroups) {  m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true;  }
542             indexNameMap[seqName] = uniques;
543             totals.push_back(1);
544             total++;
545             uniques++;
546         }else {
547             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;
548         }
549         
550         return 0;
551     }
552         catch(exception& e) {
553                 m->errorOut(e, "CountTable", "push_back");
554                 exit(1);
555         }
556 }
557 /************************************************************/
558 //remove sequence
559 int CountTable::remove(string seqName) {
560     try {
561         map<string, int>::iterator it = indexNameMap.find(seqName);
562         if (it == indexNameMap.end()) {
563             uniques--;
564             if (hasGroups){ //remove this sequences counts from group totals
565                 for (int i = 0; i < totalGroups.size(); i++) {  totalGroups[i] -= counts[it->second][i];  counts[it->second][i] = 0; }
566             }
567             int thisTotal = totals[it->second]; totals[it->second] = 0;
568             total -= thisTotal;
569             indexNameMap.erase(it);
570         }else {
571             m->mothurOut("[ERROR]: Your count table contains does not include " + seqName + ", cannot remove."); m->mothurOutEndLine(); m->control_pressed = true;
572         }
573         
574         return 0;
575     }
576         catch(exception& e) {
577                 m->errorOut(e, "CountTable", "push_back");
578                 exit(1);
579         }
580 }
581 /************************************************************/
582 //add seqeunce without group info
583 int CountTable::push_back(string seqName, int thisTotal) {
584     try {
585         map<string, int>::iterator it = indexNameMap.find(seqName);
586         if (it == indexNameMap.end()) {
587             if (hasGroups) {  m->mothurOut("[ERROR]: Your count table has groups and I have no group information for " + seqName + "."); m->mothurOutEndLine(); m->control_pressed = true;  }
588             indexNameMap[seqName] = uniques;
589             totals.push_back(thisTotal);
590             total+=thisTotal;
591             uniques++;
592         }else {
593             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;
594         }
595         
596         return 0;
597     }
598         catch(exception& e) {
599                 m->errorOut(e, "CountTable", "push_back");
600                 exit(1);
601         }
602 }
603 /************************************************************/
604 //add sequence with group info
605 int CountTable::push_back(string seqName, vector<int> groupCounts) {
606     try {
607         map<string, int>::iterator it = indexNameMap.find(seqName);
608         if (it == indexNameMap.end()) {
609             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;  }
610             int thisTotal = 0;
611             for (int i = 0; i < getNumGroups(); i++) {   totalGroups[i] += groupCounts[i];  thisTotal += groupCounts[i]; }
612             if (hasGroups) {  counts.push_back(groupCounts);  }
613             indexNameMap[seqName] = uniques;
614             totals.push_back(thisTotal);
615             total+= thisTotal;
616             uniques++;
617         }else {
618             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;
619         }
620         
621         return 0;
622     }
623         catch(exception& e) {
624                 m->errorOut(e, "CountTable", "push_back");
625                 exit(1);
626         }
627 }
628
629 /************************************************************/
630 //create ListVector from uniques
631 ListVector CountTable::getListVector() {
632     try {
633         ListVector list(indexNameMap.size());
634         for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) { 
635             if (m->control_pressed) { break; }
636             list.set(it->second, it->first); 
637         }
638         return list;
639     }
640         catch(exception& e) {
641                 m->errorOut(e, "CountTable", "getListVector");
642                 exit(1);
643         }
644 }
645
646 /************************************************************/
647 //returns the names of all unique sequences in file
648 vector<string> CountTable::getNamesOfSeqs() {
649     try {
650         vector<string> names;
651         for (map<string, int>::iterator it = indexNameMap.begin(); it != indexNameMap.end(); it++) {
652             names.push_back(it->first);
653         }
654                 
655         return names;
656     }
657         catch(exception& e) {
658                 m->errorOut(e, "CountTable", "getNamesOfSeqs");
659                 exit(1);
660         }
661 }
662 /************************************************************/
663 //returns the names of all unique sequences in file
664 vector<string> CountTable::getNamesOfSeqs(string group) {
665     try {
666         vector<string> names;
667         if (hasGroups) {
668             map<string, int>::iterator it = indexGroupMap.find(group);
669             if (it == indexGroupMap.end()) {
670                 m->mothurOut("[ERROR]: " + group + " is not in your count table. Please correct.\n"); m->control_pressed = true;
671             }else { 
672                 for (map<string, int>::iterator it2 = indexNameMap.begin(); it2 != indexNameMap.end(); it2++) {
673                     if (counts[it2->second][it->second] != 0) {  names.push_back(it2->first); }
674                 }
675             }
676         }else{  m->mothurOut("[ERROR]: Your count table does not have group info. Please correct.\n");  m->control_pressed = true; }
677         
678         return names;
679     }
680         catch(exception& e) {
681                 m->errorOut(e, "CountTable", "getNamesOfSeqs");
682                 exit(1);
683         }
684 }
685 /************************************************************/
686 //merges counts of seq1 and seq2, saving in seq1
687 int CountTable::mergeCounts(string seq1, string seq2) {
688     try {
689         map<string, int>::iterator it = indexNameMap.find(seq1);
690         if (it == indexNameMap.end()) {
691             m->mothurOut("[ERROR]: " + seq1 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
692         }else { 
693             map<string, int>::iterator it2 = indexNameMap.find(seq2);
694             if (it2 == indexNameMap.end()) {
695                 m->mothurOut("[ERROR]: " + seq2 + " is not in your count table. Please correct.\n"); m->control_pressed = true;
696             }else { 
697                 //merge data
698                 for (int i = 0; i < groups.size(); i++) { counts[it->second][i] += counts[it2->second][i]; }
699                 totals[it->second] += totals[it2->second];
700                 uniques--;
701                 indexNameMap.erase(it2); 
702             }
703         }
704         return 0;
705     }
706         catch(exception& e) {
707                 m->errorOut(e, "CountTable", "getNamesOfSeqs");
708                 exit(1);
709         }
710 }
711 /************************************************************/
712 int CountTable::copy(CountTable* ct) {
713     try {
714         vector<string> thisGroups = ct->getNamesOfGroups();
715         for (int i = 0; i < thisGroups.size(); i++) { addGroup(thisGroups[i]); }
716         vector<string> names = ct->getNamesOfSeqs();
717                                                                
718         for (int i = 0; i < names.size(); i++) {
719             vector<int> thisCounts = ct->getGroupCounts(names[i]);
720             push_back(names[i], thisCounts);
721         }
722                                                                
723         return 0;
724     }
725         catch(exception& e) {
726                 m->errorOut(e, "CountTable", "copy");
727                 exit(1);
728         }
729 }
730
731 /************************************************************/
732
733