-
- string filerootFasta = outputDir + m->getRootName(m->getSimpleName(fastafile));
- string filerootName = outputDir + m->getRootName(m->getSimpleName(namefile));
- ofstream* temp;
- ofstream* temp2;
- map<string, ofstream*> filehandles;
- map<string, ofstream*>::iterator it3;
-
- for (int i=0; i<Groups.size(); i++) {
- temp = new ofstream;
- filehandles[Groups[i]+"fasta"] = temp;
- m->openOutputFile(filerootFasta + Groups[i] + ".fasta", *(filehandles[Groups[i]+"fasta"]));
- outputNames.push_back(filerootFasta + Groups[i] + ".fasta"); outputTypes["fasta"].push_back(filerootFasta + Groups[i] + ".fasta");
-
- if (namefile != "") {
- temp2 = new ofstream;
- filehandles[Groups[i]+"name"] = temp2;
- m->openOutputFile(filerootName + Groups[i] + ".names", *(filehandles[Groups[i]+"name"]));
- outputNames.push_back(filerootName + Groups[i] + ".names"); outputTypes["name"].push_back(filerootFasta + Groups[i] + ".names");
- }
- }
-
- //open input file
- ifstream in;
- m->openInputFile(fastafile, in);
-
- while (!in.eof()) {
- if (m->control_pressed) { break; }
-
- Sequence seq(in); m->gobble(in);
-
- if (seq.getName() != "") {
-
- itNames = nameMap.find(seq.getName());
-
- if (itNames == nameMap.end()) {
- if (namefile != "") { m->mothurOut(seq.getName() + " is not in your namesfile, ignoring."); m->mothurOutEndLine(); }
- else { //no names file given
- string group = groupMap->getGroup(seq.getName());
-
- if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
- seq.printSequence(*(filehandles[group+"fasta"]));
- }else if(group == "not found") {
- m->mothurOut(seq.getName() + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
- }
- }
- }else{
- vector<string> names = itNames->second;
- map<string, string> group2Names;
- map<string, string>::iterator it;
-
- for (int i = 0; i < names.size(); i++) { //build strings for new group names file, will select rep below
- string group = groupMap->getGroup(names[i]);
-
- if (m->inUsersGroups(group, Groups)) { //only add if this is in a group we want
- it = group2Names.find(group);
- if (it == group2Names.end()) {
- group2Names[group] = names[i] + ",";
- }else{
- group2Names[group] += names[i] + ",";
- }
- }else if(group == "not found") {
- m->mothurOut(names[i] + " is not in your groupfile. Ignoring."); m->mothurOutEndLine();
- }
- }
-
- for (map<string, string>::iterator itGroups = group2Names.begin(); itGroups != group2Names.end(); itGroups++) {
- //edit names string, by grabbing the first guy as the rep and removing the last comma
- string newNames = itGroups->second;
- newNames = newNames.substr(0, newNames.length()-1);
- string repName = "";
-
- int pos = newNames.find_first_of(',');
- if (pos == newNames.npos) { //only one sequence so it represents itself
- repName = newNames;
- }else{
- repName = newNames.substr(0, pos);
- }
-
- *(filehandles[itGroups->first+"name"]) << repName << '\t' << newNames << endl;
- *(filehandles[itGroups->first+"fasta"]) << ">" << repName << endl << seq.getAligned() << endl;
- }
- }
- }
- }
-
- in.close();
-
- for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
- (*(filehandles[it3->first])).close();
- delete it3->second;
- }
-
- vector<string> newOutputNames;
- //remove blank files
- for (int i = 0; i < outputNames.size(); i++) {
- if (m->isBlank(outputNames[i])) {
- remove(outputNames[i].c_str());
- }else { newOutputNames.push_back(outputNames[i]); }
- }
- outputNames = newOutputNames;
-
- return 0;
+
+ 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;
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
+ variables["[group]"] = Groups[i];
+ string newFasta = getOutputFileName("fasta",variables);
+ outputNames.push_back(newFasta); outputTypes["fasta"].push_back(newFasta);
+ m->openOutputFile(newFasta, (*temp));
+ temp = new ofstream;
+ cfiles[Groups[i]] = temp;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(countfile));
+ string newCount = getOutputFileName("count",variables);
+ 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;