5 * Created by Sarah Westcott on 6/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "getsabundcommand.h"
12 //**********************************************************************************************************************
14 GetSAbundCommand::GetSAbundCommand(string option) {
16 globaldata = GlobalData::getInstance();
21 //allow user to run help
22 if(option == "help") { help(); abort = true; }
25 //valid paramters for this command
26 string Array[] = {"label","outputdir","inputdir"};
27 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
29 OptionParser parser(option);
30 map<string,string> parameters = parser.getParameters();
32 ValidParameters validParameter;
34 //check to make sure all parameters are valid for command
35 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the output directory command factory will send this info to us in the output parameter
40 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
42 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
45 //make sure the user has already run the read.otu command
46 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "")) { m->mothurOut("You must read a list or rabund before you can use the get.sabund command."); m->mothurOutEndLine(); abort = true; }
48 //check for optional parameter and set defaults
49 // ...at some point should added some additional type checking...
50 label = validParameter.validFile(parameters, "label", false);
51 if (label == "not found") { label = ""; }
53 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
54 else { allLines = 1; }
57 //if the user has not specified any labels use the ones from read.otu
59 allLines = globaldata->allLines;
60 labels = globaldata->labels;
64 filename = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "sabund";
65 m->openOutputFile(filename, out);
71 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
75 //**********************************************************************************************************************
77 void GetSAbundCommand::help(){
79 m->mothurOut("The get.sabund command can only be executed after a successful read.otu of a listfile or rabundfile.\n");
80 m->mothurOut("The get.sabund command parameters is label. No parameters are required.\n");
81 m->mothurOut("The label parameter allows you to select what distance levels you would like included in your .sabund file, and are separated by dashes.\n");
82 m->mothurOut("The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n");
83 m->mothurOut("Example get.sabund().\n");
84 m->mothurOut("The default value for label is all labels in your inputfile.\n");
85 m->mothurOut("The get.sabund command outputs a .sabund file containing the labels you selected.\n");
86 m->mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
89 m->errorOut(e, "GetSAbundCommand", "help");
94 //**********************************************************************************************************************
96 GetSAbundCommand::~GetSAbundCommand(){
99 //**********************************************************************************************************************
101 int GetSAbundCommand::execute(){
104 if (abort == true) { return 0; }
106 //using order vector so you don't have to distinguish between the list and rabund files
107 read = new ReadOTUFile(globaldata->inputFileName);
108 read->read(&*globaldata);
110 order = globaldata->gorder;
111 string lastLabel = order->getLabel();
112 input = globaldata->ginput;
114 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
115 set<string> processedLabels;
116 set<string> userLabels = labels;
118 if (m->control_pressed) { out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
121 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
123 if(allLines == 1 || labels.count(order->getLabel()) == 1){
124 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
125 sabund = new SAbundVector();
126 *sabund = (order->getSAbundVector());
130 if (m->control_pressed) { out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
132 processedLabels.insert(order->getLabel());
133 userLabels.erase(order->getLabel());
136 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
137 string saveLabel = order->getLabel();
140 order = (input->getOrderVector(lastLabel));
142 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
143 sabund = new SAbundVector();
144 *sabund = (order->getSAbundVector());
148 if (m->control_pressed) { out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
150 processedLabels.insert(order->getLabel());
151 userLabels.erase(order->getLabel());
153 //restore real lastlabel to save below
154 order->setLabel(saveLabel);
158 lastLabel = order->getLabel();
161 order = (input->getOrderVector());
164 //output error messages about any remaining user labels
165 set<string>::iterator it;
166 bool needToRun = false;
167 for (it = userLabels.begin(); it != userLabels.end(); it++) {
168 m->mothurOut("Your file does not include the label " + *it);
169 if (processedLabels.count(lastLabel) != 1) {
170 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
173 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
177 //run last label if you need to
178 if (needToRun == true) {
179 if (order != NULL) { delete order; }
180 order = (input->getOrderVector(lastLabel));
182 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
183 sabund = new SAbundVector();
184 *sabund = (order->getSAbundVector());
188 if (m->control_pressed) { out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
192 globaldata->gorder = NULL;
196 m->mothurOutEndLine();
197 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
198 m->mothurOut(filename); m->mothurOutEndLine();
199 m->mothurOutEndLine();
204 catch(exception& e) {
205 m->errorOut(e, "GetSAbundCommand", "execute");
210 //**********************************************************************************************************************