+vector<string> RareFactCommand::createGroupFile(vector<string>& outputNames, map<string, string> nameMap) {
+ try {
+
+ vector<string> newFileNames;
+
+ //find different types of files
+ map<string, vector<string> > typesFiles;
+ for (int i = 0; i < outputNames.size(); i++) {
+ string extension = m->getExtension(outputNames[i]);
+
+ ifstream in;
+ m->openInputFile(outputNames[i], in);
+
+ string labels = m->getline(in);
+ string newLine = labels.substr(0, labels.find_first_of('\t'));
+
+ newLine += "\tGroup" + labels.substr(labels.find_first_of('\t'));
+
+ typesFiles[extension].push_back(outputNames[i]);
+
+ string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups" + extension;
+
+ //print headers
+ ofstream out;
+ m->openOutputFile(combineFileName, out);
+ out << newLine << endl;
+ out.close();
+
+ }
+
+ //for each type create a combo file
+ map<int, int> lineToNumber;
+ for (map<string, vector<string> >::iterator it = typesFiles.begin(); it != typesFiles.end(); it++) {
+
+ ofstream out;
+ string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups" + it->first;
+ m->openOutputFileAppend(combineFileName, out);
+ newFileNames.push_back(combineFileName);
+
+ vector<string> thisTypesFiles = it->second;
+
+ //open each type summary file
+ map<string, vector<string> > files; //maps file name to lines in file
+ int maxLines = 0;
+ int numColumns = 0;
+ for (int i=0; i<thisTypesFiles.size(); i++) {
+
+ ifstream temp;
+ m->openInputFile(thisTypesFiles[i], temp);
+
+ //read through first line - labels
+ m->getline(temp); m->gobble(temp);
+
+ vector<string> thisFilesLines;
+ string fileNameRoot = m->getRootName(thisTypesFiles[i]);
+ map<string, string>::iterator itName = nameMap.find(fileNameRoot);
+ string group = "";
+ if (itName != nameMap.end()) {
+ group = itName->second;
+ }else {
+ group = "not found" + i;
+ m->mothurOut("[ERROR]: can't parse filename."); m->mothurOutEndLine();
+ }
+
+ thisFilesLines.push_back(group);
+ int count = 1;
+ while (!temp.eof()){
+
+ string thisLine = m->getline(temp);
+
+ string numSampled = thisLine.substr(0, thisLine.find_first_of('\t'));
+ int num = 0;
+ convert(numSampled, num);
+ numColumns = m->getNumChar(thisLine, '\t');
+ lineToNumber[count] = num;
+ count++;
+
+ thisFilesLines.push_back(thisLine);
+
+ m->gobble(temp);
+ }
+
+ files[thisTypesFiles[i]] = thisFilesLines;
+
+ //save longest file for below
+ if (maxLines < thisFilesLines.size()) { maxLines = thisFilesLines.size(); }
+
+ temp.close();
+ m->mothurRemove(thisTypesFiles[i]);
+ }
+
+
+ //for each label
+ for (int k = 1; k < maxLines; k++) {
+
+ //grab data for each group
+ for (int i=0; i<thisTypesFiles.size(); i++) {
+
+ map<int, int>::iterator itLine = lineToNumber.find(k);
+ if (itLine != lineToNumber.end()) {
+ string output = toString(itLine->second);
+ if (k < files[thisTypesFiles[i]].size()) {
+ string line = files[thisTypesFiles[i]][k];
+ output = line.substr(0, line.find_first_of('\t'));
+ output += '\t' + files[thisTypesFiles[i]][0] + '\t' + line.substr(line.find_first_of('\t'));
+ }else{
+ output += '\t' + files[thisTypesFiles[i]][0] + '\t';
+ for (int h = 0; h < numColumns; h++) {
+ output += "NA\t";
+ }
+ }
+ out << output << endl;
+ }else { m->mothurOut("[ERROR]: parsing results, cant find " + toString(k)); m->mothurOutEndLine(); }
+ }
+ }
+
+ out.close();
+
+ }
+
+ //return combine file name
+ return newFileNames;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "RareFactCommand", "createGroupFile");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************