+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inFastaName));
+ string outNameFile = getOutputFileName("name", variables);
+ string outCountFile = getOutputFileName("count", variables);
+ variables["[extension]"] = m->getExtension(inFastaName);
+ string outFastaFile = getOutputFileName("fasta", variables);
+
+ map<string, string> nameMap;
+ map<string, string>::iterator itNames;
+ if (oldNameMapFName != "") {
+ m->readNames(oldNameMapFName, nameMap);
+ if (oldNameMapFName == outNameFile){
+ variables["[tag]"] = "unique";
+ outNameFile = getOutputFileName("name", variables); }
+ }
+ CountTable ct;
+ if (countfile != "") {
+ ct.readTable(countfile);
+ if (countfile == outCountFile){
+ variables["[tag]"] = "unique";
+ outCountFile = getOutputFileName("count", variables); }
+ }
+
+ if (m->control_pressed) { return 0; }
+
+ 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; }