+}
+//**********************************************************************************************************************
+int SplitGroupCommand::runCount(){
+ try {
+
+ CountTable ct;
+ ct.readTable(countfile);
+ if (!ct.hasGroupInfo()) { m->mothurOut("[ERROR]: your count file does not contain group info, cannot split by group.\n"); m->control_pressed = true; }
+
+ if (m->control_pressed) { return 0; }
+
+ vector<string> namesGroups = ct.getNamesOfGroups();
+ SharedUtil util; util.setGroups(Groups, namesGroups);
+
+ //fill filehandles with neccessary ofstreams
+ map<string, ofstream*> ffiles;
+ map<string, ofstream*> cfiles;
+ ofstream* temp;
+ for (int i=0; i<Groups.size(); i++) {
+ temp = new ofstream;
+ ffiles[Groups[i]] = temp;
+ string newFasta = outputDir + m->getRootName(m->getSimpleName(fastafile)) + Groups[i] + "." + getOutputFileNameTag("fasta");
+ outputNames.push_back(newFasta); outputTypes["fasta"].push_back(newFasta);
+ m->openOutputFile(newFasta, (*temp));
+ temp = new ofstream;
+ cfiles[Groups[i]] = temp;
+ string newCount = outputDir + m->getRootName(m->getSimpleName(countfile)) + Groups[i] + "." + getOutputFileNameTag("count");
+ m->openOutputFile(newCount, (*temp));
+ outputNames.push_back(newCount); outputTypes["count"].push_back(newCount);
+ (*temp) << "Representative_Sequence\ttotal\t" << Groups[i] << endl;
+ }
+
+ ifstream in;
+ m->openInputFile(fastafile, in);
+
+ while (!in.eof()) {
+ Sequence seq(in); m->gobble(in);
+
+ if (m->control_pressed) { break; }
+ if (seq.getName() != "") {
+ vector<string> thisSeqsGroups = ct.getGroups(seq.getName());
+ for (int i = 0; i < thisSeqsGroups.size(); i++) {
+ if (m->inUsersGroups(thisSeqsGroups[i], Groups)) { //if this sequence belongs to a group we want them print
+ seq.printSequence(*(ffiles[thisSeqsGroups[i]]));
+ int numSeqs = ct.getGroupCount(seq.getName(), Groups[i]);
+ (*(cfiles[thisSeqsGroups[i]])) << seq.getName() << '\t' << numSeqs << '\t' << numSeqs << endl;
+ }
+ }
+ }
+ }
+ in.close();
+
+ //close and delete ofstreams
+ for (int i=0; i<Groups.size(); i++) {
+ (*ffiles[Groups[i]]).close(); delete ffiles[Groups[i]];
+ (*cfiles[Groups[i]]).close(); delete cfiles[Groups[i]];
+ }
+
+ return 0;
+
+ }