+
+ //read through fasta file outputting only the names on the subsample list
+ ifstream in;
+ m->openInputFile(fastafile, in);
+
+ string thisname;
+ int count = 0;
+ map<string, vector<string> >::iterator itNameMap;
+
+ while(!in.eof()){
+
+ if (m->control_pressed) { in.close(); out.close(); return 0; }
+
+ Sequence currSeq(in);
+ thisname = currSeq.getName();
+
+ if (thisname != "") {
+
+ //does the subset contain a sequence that this sequence represents
+ itNameMap = nameMap.find(thisname);
+ if (itNameMap != nameMap.end()) {
+ vector<string> nameRepresents = itNameMap->second;
+
+ for (int i = 0; i < nameRepresents.size(); i++){
+ if (subset.count(nameRepresents[i]) != 0) {
+ out << ">" << nameRepresents[i] << endl << currSeq.getAligned() << endl;
+ count++;
+ }
+ }
+ }else{
+ m->mothurOut("[ERROR]: " + thisname + " is not in your namefile, please correct."); m->mothurOutEndLine();
+ }
+ }
+ m->gobble(in);
+ }
+ in.close();
+ out.close();
+
+ if (count != subset.size()) {
+ m->mothurOut("[ERROR]: The subset selected contained " + toString(subset.size()) + " sequences, but I only found " + toString(count) + " of those in the fastafile."); m->mothurOutEndLine();
+ }
+
+ if (namefile != "") {
+ m->mothurOut("Deconvoluting subsampled fasta file... "); m->mothurOutEndLine();
+ map<string, string> variables;
+ variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(namefile));
+ variables["[extension]"] = m->getExtension(namefile);
+ string outputNameFileName = getOutputFileName("name", variables);
+ //use unique.seqs to create new name and fastafile
+ string inputString = "fasta=" + outputFileName;
+ m->mothurOut("/******************************************/"); m->mothurOutEndLine();
+ m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine();
+ m->mothurCalling = true;
+
+ Command* uniqueCommand = new DeconvoluteCommand(inputString);
+ uniqueCommand->execute();
+
+ map<string, vector<string> > filenames = uniqueCommand->getOutputFiles();
+
+ delete uniqueCommand;
+ m->mothurCalling = false;
+
+ m->renameFile(filenames["name"][0], outputNameFileName);
+ m->renameFile(filenames["fasta"][0], outputFileName);
+
+ outputTypes["name"].push_back(outputNameFileName); outputNames.push_back(outputNameFileName);
+
+ m->mothurOut("/******************************************/"); m->mothurOutEndLine();
+
+ m->mothurOut("Done."); m->mothurOutEndLine();
+ }
+
+ outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
+
+ //if a groupfile is provided read through the group file only outputting the names on the subsample list
+ if (groupfile != "") {
+
+ string groupOutputDir = outputDir;
+ if (outputDir == "") { groupOutputDir += m->hasPath(groupfile); }
+ map<string, string> variables;
+ variables["[filename]"] = groupOutputDir + m->getRootName(m->getSimpleName(groupfile));
+ variables["[extension]"] = m->getExtension(groupfile);
+ string groupOutputFileName = getOutputFileName("group", variables);
+
+ ofstream outGroup;
+ m->openOutputFile(groupOutputFileName, outGroup);
+ outputTypes["group"].push_back(groupOutputFileName); outputNames.push_back(groupOutputFileName);
+
+ ifstream inGroup;
+ m->openInputFile(groupfile, inGroup);
+ string name, group;
+
+ while(!inGroup.eof()){
+
+ if (m->control_pressed) { inGroup.close(); outGroup.close(); return 0; }
+
+ inGroup >> name; m->gobble(inGroup); //read from first column
+ inGroup >> group; //read from second column
+
+ //if this name is in the accnos file
+ if (subset.count(name) != 0) {
+ outGroup << name << '\t' << group << endl;
+ subset.erase(name);
+ }
+
+ m->gobble(inGroup);
+ }
+ inGroup.close();
+ outGroup.close();
+
+ //sanity check
+ if (subset.size() != 0) {
+ m->mothurOut("Your groupfile does not match your fasta file."); m->mothurOutEndLine();
+ for (set<string>::iterator it = subset.begin(); it != subset.end(); it++) {
+ m->mothurOut("[ERROR]: " + *it + " is missing from your groupfile."); m->mothurOutEndLine();
+ }
+ }
+ }
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SubSampleCommand", "getSubSampleFasta");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int SubSampleCommand::getNames() {
+ try {
+
+ ifstream in;
+ m->openInputFile(fastafile, in);
+
+ string thisname;
+ while(!in.eof()){
+
+ if (m->control_pressed) { in.close(); return 0; }
+
+ Sequence currSeq(in);
+ thisname = currSeq.getName();
+
+ if (thisname != "") {
+ vector<string> temp; temp.push_back(thisname);
+ nameMap[thisname] = temp;
+ names.push_back(thisname);
+ }
+ m->gobble(in);
+ }
+ in.close();
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SubSampleCommand", "getNames");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int SubSampleCommand::readNames() {
+ try {
+
+ nameMap.clear();
+ m->readNames(namefile, nameMap);
+
+ //save names of all sequences
+ map<string, vector<string> >::iterator it;
+ for (it = nameMap.begin(); it != nameMap.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { names.push_back((it->second)[i]); } }
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SubSampleCommand", "readNames");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int SubSampleCommand::getSubSampleShared() {
+ try {