+ ifstream in;
+ m->openInputFile(inFastaName, in);
+
+ 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 if (countfile != "") {
+ ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
+ sequenceStrings[seq.getAligned()] = seq.getName(); 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 if (countfile != "") {
+ int num = ct.getNumSeqs(seq.getName()); //checks to make sure seq is in table
+ if (num != 0) { //its in the table
+ ct.mergeCounts(itStrings->second, seq.getName()); //merges counts and saves in uniques name
+ }
+ }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;
+ if (countfile == "") { m->openOutputFile(outNameFile, outNames); outputNames.push_back(outNameFile); outputTypes["name"].push_back(outNameFile); }
+ else { m->openOutputFile(outCountFile, outNames); ct.printHeaders(outNames); outputTypes["count"].push_back(outCountFile); outputNames.push_back(outCountFile); }
+
+ for (int i = 0; i < nameFileOrder.size(); i++) {
+ if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outFastaFile); outNames.close(); for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); } return 0; }
+
+ itStrings = sequenceStrings.find(nameFileOrder[i]);
+
+ if (itStrings != sequenceStrings.end()) {
+ if (countfile == "") {
+ //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 { ct.printSeq(outNames, itStrings->second); }
+ }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); for (int j = 0; j < outputNames.size(); j++) { m->mothurRemove(outputNames[j]); } return 0; }
+
+ m->mothurOutEndLine();
+ m->mothurOut("Output File Names: "); m->mothurOutEndLine();
+ outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
+ for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
+ 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); }
+ }
+
+ itTypes = outputTypes.find("count");
+ if (itTypes != outputTypes.end()) {
+ if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
+ }