- int numGroups = groups.size();
-
- ifstream bigNameFile(namefile.c_str());
- if(!bigNameFile){
- cerr << "Error: We can't open the name file\n";
- exit(1);
- }
-
- map<string, string> nameMap;
- string name, nameList;
- while(bigNameFile){
- bigNameFile >> name >> nameList;
- nameMap[name] = nameList;
- gobble(bigNameFile);
- }
- bigNameFile.close();
-
- for(int i=0;i<numGroups;i++){ //parse names file to match distance files
- int numSeqsInGroup = groups[i].size();
-
- if(numSeqsInGroup > 0){
- string fileName = namefile + "." + toString(i) + ".temp";
- ofstream smallNameFile(fileName.c_str(), ios::ate);
-
- for(set<string>::iterator gIt=groups[i].begin();gIt!=groups[i].end();gIt++){
- map<string,string>::iterator nIt = nameMap.find(*gIt);
- if (nIt != nameMap.end()) {
- smallNameFile << nIt->first << '\t' << nIt->second << endl;
- nameMap.erase(nIt);
- }else{
- m->mothurOut((*gIt) + " is in your distance file and not in your namefile. Please correct."); m->mothurOutEndLine(); exit(1);
- }
- }
- smallNameFile.close();
- }
- }
-
- //names of singletons
- if (nameMap.size() != 0) {
- singleton = namefile + ".extra.temp";
- ofstream remainingNames(singleton.c_str(), ios::ate);
- for(map<string,string>::iterator nIt=nameMap.begin();nIt!=nameMap.end();nIt++){
- remainingNames << nIt->first << '\t' << nIt->second << endl;
- }
- remainingNames.close();
- }else { singleton = "none"; }
-
+ ofstream outFile;
+ map<string, int>::iterator it;
+
+ string inputFile = namefile;
+ if (countfile != "") { inputFile = countfile; }
+
+ for(int i=0;i<numGroups;i++){ m->mothurRemove((inputFile + "." + toString(i) + ".temp")); }
+
+ singleton = inputFile + ".extra.temp";
+ ofstream remainingNames;
+ m->openOutputFile(singleton, remainingNames);
+
+ bool wroteExtra = false;
+
+ ifstream bigNameFile;
+ m->openInputFile(inputFile, bigNameFile);
+
+ //grab header line
+ string headers = "";
+ if (countfile != "") { headers = m->getline(bigNameFile); m->gobble(bigNameFile); }
+
+ string name, nameList;
+ while(!bigNameFile.eof()){
+ bigNameFile >> name >> nameList;
+ m->getline(bigNameFile); m->gobble(bigNameFile); //extra getline is for rest of countfile line if groups are given.
+
+ //did this sequence get assigned a group
+ it = seqGroup.find(name);
+
+ if (it != seqGroup.end()) {
+ m->openOutputFileAppend((inputFile + "." + toString(it->second) + ".temp"), outFile);
+ outFile << name << '\t' << nameList << endl;
+ outFile.close();
+ }else{
+ wroteExtra = true;
+ remainingNames << name << '\t' << nameList << endl;
+ }
+ }
+ bigNameFile.close();
+