+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 binnames = list->get(i);
+
+ //parse names
+ string individual = "";
+ string newNames = "";
+ int length = binnames.length();
+ for(int j=0;j<length;j++){
+ if(binnames[j] == ','){
+ if (subset.count(individual) != 0) { newNames += individual + ","; }
+ individual = "";
+ }else{
+ individual += binnames[j];
+ }
+ }
+ if (subset.count(individual) != 0) { newNames += individual + ","; }
+
+
+ //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); }
+ string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(rabundfile)) + "subsample" + m->getExtension(rabundfile);
+
+ 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); }
+ string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(sabundfile)) + "subsample" + m->getExtension(sabundfile);
+
+ 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);
+ }
+}
+//**********************************************************************************************************************