+ ofstream outFasta;
+ m->openOutputFile(outFastaFile, outFasta);
+
+ map<string, string> sequenceStrings; //sequenceString -> list of names. "atgc...." -> seq1,seq2,seq3.
+ map<string, string>::iterator itStrings;
+ set<string> nameInFastaFile; //for sanity checking
+ set<string>::iterator itname;
+ vector<string> nameFileOrder;
+ int count = 0;
+ while (!in.eof()) {
+
+ if (m->control_pressed) { in.close(); outFasta.close(); m->mothurRemove(outFastaFile); return 0; }
+
+ Sequence seq(in);
+
+ if (seq.getName() != "") {
+
+ //sanity checks
+ itname = nameInFastaFile.find(seq.getName());
+ if (itname == nameInFastaFile.end()) { nameInFastaFile.insert(seq.getName()); }
+ else { m->mothurOut("[ERROR]: You already have a sequence named " + seq.getName() + " in your fasta file, sequence names must be unique, please correct."); m->mothurOutEndLine(); }
+
+ itStrings = sequenceStrings.find(seq.getAligned());
+
+ if (itStrings == sequenceStrings.end()) { //this is a new unique sequence
+ //output to unique fasta file
+ seq.printSequence(outFasta);
+
+ if (oldNameMapFName != "") {
+ itNames = nameMap.find(seq.getName());
+
+ if (itNames == nameMap.end()) { //namefile and fastafile do not match
+ m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
+ }else {
+ sequenceStrings[seq.getAligned()] = itNames->second;
+ nameFileOrder.push_back(seq.getAligned());
+ }
+ }else { sequenceStrings[seq.getAligned()] = seq.getName(); nameFileOrder.push_back(seq.getAligned()); }
+ }else { //this is a dup
+ if (oldNameMapFName != "") {
+ itNames = nameMap.find(seq.getName());
+
+ if (itNames == nameMap.end()) { //namefile and fastafile do not match
+ m->mothurOut("[ERROR]: " + seq.getName() + " is in your fasta file, and not in your namefile, please correct."); m->mothurOutEndLine();
+ }else {
+ sequenceStrings[seq.getAligned()] += "," + itNames->second;
+ }
+ }else { sequenceStrings[seq.getAligned()] += "," + seq.getName(); }
+ }
+
+ count++;
+ }
+
+ m->gobble(in);
+
+ if(count % 1000 == 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
+ }
+
+ if(count % 1000 != 0) { m->mothurOut(toString(count) + "\t" + toString(sequenceStrings.size())); m->mothurOutEndLine(); }
+
+ in.close();
+ outFasta.close();
+
+ if (m->control_pressed) { m->mothurRemove(outFastaFile); return 0; }
+
+ //print new names file
+ ofstream outNames;
+ m->openOutputFile(outNameFile, outNames);
+
+ for (int i = 0; i < nameFileOrder.size(); i++) {
+ //for (itStrings = sequenceStrings.begin(); itStrings != sequenceStrings.end(); itStrings++) {
+ if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); outNames.close(); m->mothurRemove(outNameFile); return 0; }
+
+ itStrings = sequenceStrings.find(nameFileOrder[i]);
+
+ if (itStrings != sequenceStrings.end()) {
+ //get rep name
+ int pos = (itStrings->second).find_first_of(',');
+
+ if (pos == string::npos) { // only reps itself
+ outNames << itStrings->second << '\t' << itStrings->second << endl;
+ }else {
+ outNames << (itStrings->second).substr(0, pos) << '\t' << itStrings->second << endl;
+ }
+ }else{ m->mothurOut("[ERROR]: mismatch in namefile print."); m->mothurOutEndLine(); m->control_pressed = true; }
+ }
+ outNames.close();
+
+ if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); m->mothurRemove(outNameFile); return 0; }
+
+ m->mothurOutEndLine();
+ m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+ m->mothurOut(outFastaFile); m->mothurOutEndLine();
+ m->mothurOut(outNameFile); m->mothurOutEndLine();
+ outputNames.push_back(outFastaFile); outputNames.push_back(outNameFile); outputTypes["fasta"].push_back(outFastaFile); outputTypes["name"].push_back(outNameFile);
+ m->mothurOutEndLine();
+
+ //set fasta file as new current fastafile
+ string current = "";
+ itTypes = outputTypes.find("fasta");
+ if (itTypes != outputTypes.end()) {
+ if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
+ }
+
+ itTypes = outputTypes.find("name");
+ if (itTypes != outputTypes.end()) {
+ if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
+ }