+int SubSampleCommand::getSubSampleList() {
+ try {
+
+ if (namefile != "") { m->readNames(namefile, nameMap); }
+
+ string thisOutputDir = outputDir;
+ if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
+ map<string, string> variables;
+ variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
+ variables["[extension]"] = m->getExtension(listfile);
+ string outputFileName = getOutputFileName("list", variables);
+ ofstream out;
+ m->openOutputFile(outputFileName, out);
+ outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
+
+ InputData* input = new InputData(listfile, "list");
+ ListVector* list = input->getListVector();
+ string lastLabel = list->getLabel();
+
+ //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
+ set<string> processedLabels;
+ set<string> userLabels = labels;
+
+ ofstream outGroup;
+ GroupMap groupMap;
+ if (groupfile != "") {
+ groupMap.readMap(groupfile);
+
+ //takes care of user setting groupNames that are invalid or setting groups=all
+ SharedUtil util; vector<string> namesGroups = groupMap.getNamesOfGroups(); util.setGroups(Groups, namesGroups);
+
+ //create outputfiles
+ string groupOutputDir = outputDir;
+ if (outputDir == "") { groupOutputDir += m->hasPath(groupfile); }
+ string groupOutputFileName = groupOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "subsample" + m->getExtension(groupfile);
+ m->openOutputFile(groupOutputFileName, outGroup);
+ outputTypes["group"].push_back(groupOutputFileName); outputNames.push_back(groupOutputFileName);
+
+ //file mismatch quit
+ if (list->getNumSeqs() != groupMap.getNumSeqs()) {
+ m->mothurOut("[ERROR]: your list file contains " + toString(list->getNumSeqs()) + " sequences, and your groupfile contains " + toString(groupMap.getNumSeqs()) + ", please correct.");
+ m->mothurOutEndLine(); delete list; delete input; out.close(); outGroup.close(); return 0;
+ }
+ }else if (countfile != "") {
+ if (ct.hasGroupInfo()) {
+ SharedUtil util;
+ vector<string> namesGroups = ct.getNamesOfGroups();
+ util.setGroups(Groups, namesGroups);
+ }
+
+ //file mismatch quit
+ if (list->getNumSeqs() != ct.getNumUniqueSeqs()) {
+ m->mothurOut("[ERROR]: your list file contains " + toString(list->getNumSeqs()) + " sequences, and your count file contains " + toString(ct.getNumUniqueSeqs()) + " unique sequences, please correct.");
+ m->mothurOutEndLine();
+ return 0;
+ }
+ }
+
+ //make sure that if your picked groups size is not too big
+ if (persample) {
+ if (size == 0) { //user has not set size, set size = smallest samples size
+ if (countfile == "") { size = groupMap.getNumSeqs(Groups[0]); }
+ else { size = ct.getGroupCount(Groups[0]); }
+
+ for (int i = 1; i < Groups.size(); i++) {
+ int thisSize = 0;
+ if (countfile == "") { thisSize = groupMap.getNumSeqs(Groups[i]); }
+ else { thisSize = ct.getGroupCount(Groups[i]); }
+
+ if (thisSize < size) { size = thisSize; }
+ }
+ }else { //make sure size is not too large
+ vector<string> newGroups;
+ for (int i = 0; i < Groups.size(); i++) {
+ int thisSize = 0;
+ if (countfile == "") { thisSize = groupMap.getNumSeqs(Groups[i]); }
+ else { thisSize = ct.getGroupCount(Groups[i]); }
+
+ if (thisSize >= size) { newGroups.push_back(Groups[i]); }
+ else { m->mothurOut("You have selected a size that is larger than " + Groups[i] + " number of sequences, removing " + Groups[i] + "."); m->mothurOutEndLine(); }
+ }
+ Groups = newGroups;
+ if (newGroups.size() == 0) { m->mothurOut("[ERROR]: all groups removed."); m->mothurOutEndLine(); m->control_pressed = true; }
+ }
+
+ m->mothurOut("Sampling " + toString(size) + " from each group."); m->mothurOutEndLine();
+ }else{
+ if (pickedGroups) {
+ int total = 0;
+ for(int i = 0; i < Groups.size(); i++) {
+ if (countfile == "") { total += groupMap.getNumSeqs(Groups[i]); }
+ else { total += ct.getGroupCount(Groups[i]); }
+ }
+
+ if (size == 0) { //user has not set size, set size = 10% samples size
+ size = int (total * 0.10);
+ }
+
+ if (total < size) {
+ if (size != 0) {
+ m->mothurOut("Your size is too large for the number of groups you selected. Adjusting to " + toString(int (total * 0.10)) + "."); m->mothurOutEndLine();
+ }
+ size = int (total * 0.10);
+ }
+
+ m->mothurOut("Sampling " + toString(size) + " from " + toString(total) + "."); m->mothurOutEndLine();
+ }else {
+ if (size == 0) { //user has not set size, set size = 10% samples size
+ if (countfile == "") { size = int (list->getNumSeqs() * 0.10); }
+ else { size = int (ct.getNumSeqs() * 0.10); }
+ }
+
+ int thisSize = 0;
+ if (countfile == "") { thisSize = list->getNumSeqs(); }
+ else { thisSize = ct.getNumSeqs(); }
+
+ if (size > thisSize) { m->mothurOut("Your list file only contains " + toString(thisSize) + " sequences. Setting size to " + toString(thisSize) + "."); m->mothurOutEndLine();
+ size = thisSize;
+ }
+
+ m->mothurOut("Sampling " + toString(size) + " from " + toString(thisSize) + "."); m->mothurOutEndLine();
+ }
+ }
+
+ set<string> subset; //dont want repeat sequence names added
+ if (countfile == "") {
+ //fill names
+ for (int i = 0; i < list->getNumBins(); i++) {
+ string binnames = list->get(i);
+ vector<string> thisBin;
+ m->splitAtComma(binnames, thisBin);
+
+ for(int j=0;j<thisBin.size();j++){
+ if (groupfile != "") { //if there is a groupfile given fill in group info
+ string group = groupMap.getGroup(thisBin[j]);
+ if (group == "not found") { m->mothurOut("[ERROR]: " + thisBin[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
+
+ //if hte user picked groups, we only want to keep the names of sequences from those groups
+ if (pickedGroups) { if (m->inUsersGroups(group, Groups)) { names.push_back(thisBin[j]); } }
+ else{ names.push_back(thisBin[j]); }
+ }//save everyone, group
+ else{ names.push_back(thisBin[j]); }
+ }
+ }
+
+ random_shuffle(names.begin(), names.end());
+
+ //randomly select a subset of those names to include in the subsample
+ if (persample) {
+ //initialize counts
+ map<string, int> groupCounts;
+ map<string, int>::iterator itGroupCounts;
+ for (int i = 0; i < Groups.size(); i++) { groupCounts[Groups[i]] = 0; }
+
+ for (int j = 0; j < names.size(); j++) {
+
+ if (m->control_pressed) { delete list; delete input; return 0; }
+
+ string group = groupMap.getGroup(names[j]);
+ if (group == "not found") { m->mothurOut("[ERROR]: " + names[j] + " is not in your groupfile. please correct."); m->mothurOutEndLine(); group = "NOTFOUND"; }
+ else{
+ itGroupCounts = groupCounts.find(group);
+ if (itGroupCounts != groupCounts.end()) {
+ if (groupCounts[group] < size) { subset.insert(names[j]); groupCounts[group]++; }
+ }
+ }
+ }
+ }else{
+ for (int j = 0; j < size; j++) {
+ if (m->control_pressed) { break; }
+ subset.insert(names[j]);
+ }
+ }
+
+ if (groupfile != "") {
+ //write out new groupfile
+ for (set<string>::iterator it = subset.begin(); it != subset.end(); it++) {
+ string group = groupMap.getGroup(*it);
+ if (group == "not found") { group = "NOTFOUND"; }
+ outGroup << *it << '\t' << group << endl;
+ }
+ outGroup.close();
+ }
+ }else {
+ SubSample sample; CountTable sampledCt;
+
+ if (persample) { sampledCt = sample.getSample(ct, size, Groups); }
+ else { sampledCt = sample.getSample(ct, size, Groups, pickedGroups); }
+
+ vector<string> sampledSeqs = sampledCt.getNamesOfSeqs();
+ for (int i = 0; i < sampledSeqs.size(); i++) { subset.insert(sampledSeqs[i]); }
+
+ string countOutputDir = outputDir;
+ if (outputDir == "") { countOutputDir += m->hasPath(countfile); }
+ map<string, string> variables;
+ variables["[filename]"] = countOutputDir + m->getRootName(m->getSimpleName(countfile));
+ variables["[extension]"] = m->getExtension(countfile);
+ string countOutputFileName = getOutputFileName("count", variables);
+ outputTypes["count"].push_back(countOutputFileName); outputNames.push_back(countOutputFileName);
+ sampledCt.printTable(countOutputFileName);
+ }
+
+ //as long as you are not at the end of the file or done wih the lines you want
+ while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
+
+ if (m->control_pressed) { delete list; delete input; out.close(); return 0; }
+
+ if(allLines == 1 || labels.count(list->getLabel()) == 1){
+
+ m->mothurOut(list->getLabel()); m->mothurOutEndLine();
+
+ processList(list, out, subset);
+
+ processedLabels.insert(list->getLabel());
+ userLabels.erase(list->getLabel());
+ }
+
+ if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+ string saveLabel = list->getLabel();
+
+ delete list;
+
+ list = input->getListVector(lastLabel);
+ m->mothurOut(list->getLabel()); m->mothurOutEndLine();
+
+ processList(list, out, subset);
+
+ processedLabels.insert(list->getLabel());
+ userLabels.erase(list->getLabel());
+
+ //restore real lastlabel to save below
+ list->setLabel(saveLabel);
+ }
+
+ lastLabel = list->getLabel();
+
+ delete list; list = NULL;
+
+ //get next line to process
+ list = input->getListVector();
+ }
+
+
+ if (m->control_pressed) { if (list != NULL) { delete list; } delete input; out.close(); return 0; }
+
+ //output error messages about any remaining user labels
+ set<string>::iterator it;
+ bool needToRun = false;
+ for (it = userLabels.begin(); it != userLabels.end(); it++) {
+ m->mothurOut("Your file does not include the label " + *it);
+ if (processedLabels.count(lastLabel) != 1) {
+ m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
+ needToRun = true;
+ }else {
+ m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
+ }
+ }
+
+ //run last label if you need to
+ if (needToRun == true) {
+ if (list != NULL) { delete list; }
+
+ list = input->getListVector(lastLabel);
+
+ m->mothurOut(list->getLabel()); m->mothurOutEndLine();
+
+ processList(list, out, subset);
+
+ delete list; list = NULL;
+ }
+
+ out.close();
+ if (list != NULL) { delete list; }
+ delete input;
+
+ if (taxonomyfile != "") {
+ if (namefile == "") {
+ InputData input(listfile, "list");
+ ListVector* list = input.getListVector();
+ string lastLabel = list->getLabel();
+
+ for (int i = 0; i < list->getNumBins(); i++) {
+ vector<string> temp;
+ string bin = list->get(i);
+ m->splitAtComma(bin, temp);
+ for (int j = 0; j < temp.size(); j++) { vector<string> tempFakeOut; tempFakeOut.push_back(temp[j]); nameMap[temp[j]] = tempFakeOut; }
+ }
+ delete list;
+
+ int tcount = getTax(subset);
+ if (tcount != subset.size()) { m->mothurOut("[ERROR]: subsampled list file contains " + toString(subset.size()) + " sequences, but I only found " + toString(tcount) + " in your taxonomy file, did you forget a name file? Please correct."); m->mothurOutEndLine(); }
+ }else {
+ string tempAccnos = "temp.accnos";
+ ofstream outAccnos;
+ m->openOutputFile(tempAccnos, outAccnos);
+ for (set<string>::iterator it = subset.begin(); it != subset.end(); it++) { outAccnos << *it << endl; }
+ outAccnos.close();
+
+ m->mothurOut("Sampling taxonomy and name file... "); m->mothurOutEndLine();
+ string thisNameOutputDir = outputDir;
+ if (outputDir == "") { thisNameOutputDir += m->hasPath(namefile); }
+ map<string, string> variables;
+ variables["[filename]"] = thisNameOutputDir + m->getRootName(m->getSimpleName(namefile));
+ variables["[extension]"] = m->getExtension(namefile);
+ string outputNameFileName = getOutputFileName("name", variables);
+
+ string thisTaxOutputDir = outputDir;
+ if (outputDir == "") { thisTaxOutputDir += m->hasPath(taxonomyfile); }
+ variables["[filename]"] = thisTaxOutputDir + m->getRootName(m->getSimpleName(taxonomyfile));
+ variables["[extension]"] = m->getExtension(taxonomyfile);
+ string outputTaxFileName = getOutputFileName("taxonomy", variables);
+
+
+ //use unique.seqs to create new name and fastafile
+ string inputString = "dups=f, name=" + namefile + ", taxonomy=" + taxonomyfile + ", accnos=" + tempAccnos;
+ m->mothurOut("/******************************************/"); m->mothurOutEndLine();
+ m->mothurOut("Running command: get.seqs(" + inputString + ")"); m->mothurOutEndLine();
+ m->mothurCalling = true;
+
+ Command* getCommand = new GetSeqsCommand(inputString);
+ getCommand->execute();
+
+ map<string, vector<string> > filenames = getCommand->getOutputFiles();
+
+ delete getCommand;
+ m->mothurCalling = false;
+
+ m->renameFile(filenames["name"][0], outputNameFileName);
+ m->renameFile(filenames["taxonomy"][0], outputTaxFileName);
+
+ outputTypes["name"].push_back(outputNameFileName); outputNames.push_back(outputNameFileName);
+ outputNames.push_back(outputTaxFileName); outputTypes["taxonomy"].push_back(outputTaxFileName);
+
+ m->mothurOut("/******************************************/"); m->mothurOutEndLine();
+
+ m->mothurOut("Done."); m->mothurOutEndLine();
+ }
+ }
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SubSampleCommand", "getSubSampleList");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int SubSampleCommand::processList(ListVector*& list, ofstream& out, set<string>& subset) {
+ try {
+
+ int numBins = list->getNumBins();
+
+ ListVector* temp = new ListVector();
+ temp->setLabel(list->getLabel());
+
+ for (int i = 0; i < numBins; i++) {
+
+ if (m->control_pressed) { break; }
+
+ string bin = list->get(i);
+ vector<string> binnames;
+ m->splitAtComma(bin, binnames);
+
+ string newNames = "";
+ for(int j=0;j<binnames.size();j++){ if (subset.count(binnames[j]) != 0) { newNames += binnames[j] + ","; } }
+
+ //if there are names in this bin add to new list
+ if (newNames != "") {
+ newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
+ temp->push_back(newNames);
+ }
+ }
+
+ delete list;
+ list = temp;
+
+ if (m->control_pressed) { return 0; }
+
+ list->print(out);
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SubSampleCommand", "processList");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int SubSampleCommand::getSubSampleRabund() {
+ try {
+ InputData* input = new InputData(rabundfile, "rabund");
+ RAbundVector* rabund = input->getRAbundVector();
+ string lastLabel = rabund->getLabel();
+
+ //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
+ set<string> processedLabels;
+ set<string> userLabels = labels;
+
+ if (size == 0) { //user has not set size, set size = 10%
+ size = int((rabund->getNumSeqs()) * 0.10);
+ }else if (size > rabund->getNumSeqs()) { m->mothurOut("The size you selected is too large, skipping rabund file."); m->mothurOutEndLine(); delete input; delete rabund; return 0; }
+
+ m->mothurOut("Sampling " + toString(size) + " from " + toString(rabund->getNumSeqs()) + "."); m->mothurOutEndLine();
+
+ string thisOutputDir = outputDir;
+ if (outputDir == "") { thisOutputDir += m->hasPath(rabundfile); }
+ map<string, string> variables;
+ variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile));
+ variables["[extension]"] = m->getExtension(rabundfile);
+ string outputFileName = getOutputFileName("rabund", variables);
+ ofstream out;
+ m->openOutputFile(outputFileName, out);
+ outputTypes["rabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
+
+ //as long as you are not at the end of the file or done wih the lines you want
+ while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
+ if (m->control_pressed) { delete input; delete rabund; out.close(); return 0; }
+
+ if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
+
+ m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
+
+ processRabund(rabund, out);
+
+ processedLabels.insert(rabund->getLabel());
+ userLabels.erase(rabund->getLabel());
+ }
+
+ if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+ string saveLabel = rabund->getLabel();
+
+ delete rabund;
+
+ rabund = input->getRAbundVector(lastLabel);
+ m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
+
+ processRabund(rabund, out);
+
+ processedLabels.insert(rabund->getLabel());
+ userLabels.erase(rabund->getLabel());
+
+ //restore real lastlabel to save below
+ rabund->setLabel(saveLabel);
+ }
+
+ lastLabel = rabund->getLabel();
+
+ //prevent memory leak
+ delete rabund; rabund = NULL;
+
+ //get next line to process
+ rabund = input->getRAbundVector();
+ }
+
+
+ if (m->control_pressed) { out.close(); return 0; }
+
+ //output error messages about any remaining user labels
+ set<string>::iterator it;
+ bool needToRun = false;
+ for (it = userLabels.begin(); it != userLabels.end(); it++) {
+ m->mothurOut("Your file does not include the label " + *it);
+ if (processedLabels.count(lastLabel) != 1) {
+ m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
+ needToRun = true;
+ }else {
+ m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
+ }
+ }
+
+ //run last label if you need to
+ if (needToRun == true) {
+ if (rabund != NULL) { delete rabund; }
+
+ rabund = input->getRAbundVector(lastLabel);
+
+ m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
+
+ processRabund(rabund, out);
+
+ delete rabund;
+ }
+
+ delete input;
+ out.close();
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SubSampleCommand", "getSubSampleRabund");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int SubSampleCommand::processRabund(RAbundVector*& rabund, ofstream& out) {
+ try {
+
+ int numBins = rabund->getNumBins();
+ int thisSize = rabund->getNumSeqs();
+
+ if (thisSize != size) {
+
+ OrderVector* order = new OrderVector();
+ for(int p=0;p<numBins;p++){
+ for(int j=0;j<rabund->get(p);j++){
+ order->push_back(p);
+ }
+ }
+ random_shuffle(order->begin(), order->end());
+
+ RAbundVector* temp = new RAbundVector(numBins);
+ temp->setLabel(rabund->getLabel());
+
+ delete rabund;
+ rabund = temp;
+
+ for (int j = 0; j < size; j++) {
+
+ if (m->control_pressed) { delete order; return 0; }
+
+ int bin = order->get(j);
+
+ int abund = rabund->get(bin);
+ rabund->set(bin, (abund+1));
+ }
+
+ delete order;
+ }
+
+ if (m->control_pressed) { return 0; }
+
+ rabund->print(out);
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SubSampleCommand", "processRabund");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int SubSampleCommand::getSubSampleSabund() {
+ try {
+
+ InputData* input = new InputData(sabundfile, "sabund");
+ SAbundVector* sabund = input->getSAbundVector();
+ string lastLabel = sabund->getLabel();
+
+ //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
+ set<string> processedLabels;
+ set<string> userLabels = labels;
+
+ if (size == 0) { //user has not set size, set size = 10%
+ size = int((sabund->getNumSeqs()) * 0.10);
+ }else if (size > sabund->getNumSeqs()) { m->mothurOut("The size you selected is too large, skipping sabund file."); m->mothurOutEndLine(); delete input; delete sabund; return 0; }
+
+
+ m->mothurOut("Sampling " + toString(size) + " from " + toString(sabund->getNumSeqs()) + "."); m->mothurOutEndLine();
+
+ string thisOutputDir = outputDir;
+ if (outputDir == "") { thisOutputDir += m->hasPath(sabundfile); }
+ map<string, string> variables;
+ variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile));
+ variables["[extension]"] = m->getExtension(sabundfile);
+ string outputFileName = getOutputFileName("sabund", variables);
+ ofstream out;
+ m->openOutputFile(outputFileName, out);
+ outputTypes["sabund"].push_back(outputFileName); outputNames.push_back(outputFileName);
+
+
+ //as long as you are not at the end of the file or done wih the lines you want
+ while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
+ if (m->control_pressed) { delete input; delete sabund; out.close(); return 0; }
+
+ if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
+
+ m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
+
+ processSabund(sabund, out);
+
+ processedLabels.insert(sabund->getLabel());
+ userLabels.erase(sabund->getLabel());
+ }
+
+ if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+ string saveLabel = sabund->getLabel();
+
+ delete sabund;
+
+ sabund = input->getSAbundVector(lastLabel);
+ m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
+
+ processSabund(sabund, out);
+
+ processedLabels.insert(sabund->getLabel());
+ userLabels.erase(sabund->getLabel());
+
+ //restore real lastlabel to save below
+ sabund->setLabel(saveLabel);
+ }
+
+ lastLabel = sabund->getLabel();
+
+ //prevent memory leak
+ delete sabund; sabund = NULL;
+
+ //get next line to process
+ sabund = input->getSAbundVector();
+ }
+
+
+ if (m->control_pressed) { out.close(); return 0; }
+
+ //output error messages about any remaining user labels
+ set<string>::iterator it;
+ bool needToRun = false;
+ for (it = userLabels.begin(); it != userLabels.end(); it++) {
+ m->mothurOut("Your file does not include the label " + *it);
+ if (processedLabels.count(lastLabel) != 1) {
+ m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
+ needToRun = true;
+ }else {
+ m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
+ }
+ }
+
+ //run last label if you need to
+ if (needToRun == true) {
+ if (sabund != NULL) { delete sabund; }
+
+ sabund = input->getSAbundVector(lastLabel);
+
+ m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
+
+ processSabund(sabund, out);
+
+ delete sabund;
+ }
+
+ delete input;
+ out.close();
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SubSampleCommand", "getSubSampleSabund");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int SubSampleCommand::processSabund(SAbundVector*& sabund, ofstream& out) {
+ try {
+
+ RAbundVector* rabund = new RAbundVector();
+ *rabund = sabund->getRAbundVector();
+
+ int numBins = rabund->getNumBins();
+ int thisSize = rabund->getNumSeqs();
+
+ if (thisSize != size) {
+
+ OrderVector* order = new OrderVector();
+ for(int p=0;p<numBins;p++){
+ for(int j=0;j<rabund->get(p);j++){
+ order->push_back(p);
+ }
+ }
+ random_shuffle(order->begin(), order->end());
+
+ RAbundVector* temp = new RAbundVector(numBins);
+ temp->setLabel(rabund->getLabel());
+
+ delete rabund;
+ rabund = temp;
+
+ for (int j = 0; j < size; j++) {
+
+ if (m->control_pressed) { delete order; return 0; }
+
+ int bin = order->get(j);
+
+ int abund = rabund->get(bin);
+ rabund->set(bin, (abund+1));
+ }
+
+ delete order;
+ }
+
+ if (m->control_pressed) { return 0; }
+
+ delete sabund;
+ sabund = new SAbundVector();
+ *sabund = rabund->getSAbundVector();
+ delete rabund;
+
+ sabund->print(out);
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SubSampleCommand", "processSabund");
+ exit(1);
+ }
+}
+
+//**********************************************************************************************************************
+int SubSampleCommand::getTax(set<string>& subset) {
+ try {
+
+ string thisTaxOutputDir = outputDir;
+ if (outputDir == "") { thisTaxOutputDir += m->hasPath(taxonomyfile); }
+ map<string, string> variables;
+ variables["[filename]"] = thisTaxOutputDir + m->getRootName(m->getSimpleName(taxonomyfile));
+ variables["[extension]"] = m->getExtension(taxonomyfile);
+ string outputTaxFileName = getOutputFileName("taxonomy", variables);
+ ofstream outTax;
+ m->openOutputFile(outputTaxFileName, outTax);
+ outputNames.push_back(outputTaxFileName); outputTypes["taxonomy"].push_back(outputTaxFileName);
+
+ //read through fasta file outputting only the names on the subsample list
+ ifstream inTax;
+ m->openInputFile(taxonomyfile, inTax);
+
+ string tname, tax;
+ int tcount = 0;
+ map<string, vector<string> >::iterator itNameMap;
+
+ while(!inTax.eof()){
+
+ if (m->control_pressed) { inTax.close(); outTax.close(); return 0; }
+
+ inTax >> tname; m->gobble(inTax); //read from first column
+ inTax >> tax; m->gobble(inTax); //read from second column
+
+ //does the subset contain a sequence that this sequence represents
+ itNameMap = nameMap.find(tname);
+ if (itNameMap != nameMap.end()) {
+ vector<string> nameRepresents = itNameMap->second;
+
+
+ for (int i = 0; i < nameRepresents.size(); i++){
+ if (subset.count(nameRepresents[i]) != 0) {
+ outTax << nameRepresents[i] << '\t' << tax << endl;
+ tcount++;
+
+ }
+ }
+ }else{ m->mothurOut("[ERROR]: " + tname + " is missing, please correct."); m->mothurOutEndLine(); }
+ }
+ inTax.close();
+ outTax.close();
+
+ return tcount;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "SubSampleCommand", "getTax");
+ exit(1);
+ }
+}
+
+//**********************************************************************************************************************
+