+/**************************************************************************************************/
+
+int PreClusterCommand::mergeGroupCounts(string newcount, string newname, string newfasta){
+ try {
+ ifstream inNames;
+ m->openInputFile(newname, inNames);
+
+ string group, first, second;
+ set<string> uniqueNames;
+ while (!inNames.eof()) {
+ if (m->control_pressed) { break; }
+ inNames >> group; m->gobble(inNames);
+ inNames >> first; m->gobble(inNames);
+ inNames >> second; m->gobble(inNames);
+
+ vector<string> names;
+ m->splitAtComma(second, names);
+
+ uniqueNames.insert(first);
+
+ int total = ct.getGroupCount(first, group);
+ for (int i = 1; i < names.size(); i++) {
+ total += ct.getGroupCount(names[i], group);
+ ct.setAbund(names[i], group, 0);
+ }
+ ct.setAbund(first, group, total);
+ }
+ inNames.close();
+
+ vector<string> namesOfSeqs = ct.getNamesOfSeqs();
+ for (int i = 0; i < namesOfSeqs.size(); i++) {
+ if (ct.getNumSeqs(namesOfSeqs[i]) == 0) {
+ ct.remove(namesOfSeqs[i]);
+ }
+ }
+
+ ct.printTable(newcount);
+ m->mothurRemove(newname);
+
+ if (bygroup) { //if by group, must remove the duplicate seqs that are named the same
+ ifstream in;
+ m->openInputFile(newfasta, in);
+
+ ofstream out;
+ m->openOutputFile(newfasta+"temp", out);
+
+ int count = 0;
+ set<string> already;
+ while(!in.eof()) {
+ if (m->control_pressed) { break; }
+
+ Sequence seq(in); m->gobble(in);
+
+ if (seq.getName() != "") {
+ count++;
+ if (already.count(seq.getName()) == 0) {
+ seq.printSequence(out);
+ already.insert(seq.getName());
+ }
+ }
+ }
+ in.close();
+ out.close();
+ m->mothurRemove(newfasta);
+ m->renameFile(newfasta+"temp", newfasta);
+ }
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "PreClusterCommand", "mergeGroupCounts");
+ exit(1);
+ }
+}