5 * Created by Sarah Westcott on 6/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "getsabundcommand.h"
12 //**********************************************************************************************************************
13 vector<string> GetSAbundCommand::getValidParameters(){
15 string Array[] = {"label","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "GetSAbundCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 GetSAbundCommand::GetSAbundCommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["sabund"] = tempOutNames;
33 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
37 //**********************************************************************************************************************
38 vector<string> GetSAbundCommand::getRequiredParameters(){
40 vector<string> myArray;
44 m->errorOut(e, "GetSAbundCommand", "getRequiredParameters");
48 //**********************************************************************************************************************
49 vector<string> GetSAbundCommand::getRequiredFiles(){
51 string Array[] = {"list","rabund"};
52 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
56 m->errorOut(e, "GetSAbundCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
61 GetSAbundCommand::GetSAbundCommand(string option) {
63 globaldata = GlobalData::getInstance();
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string Array[] = {"label","outputdir","inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
81 //check to make sure all parameters are valid for command
82 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["sabund"] = tempOutNames;
90 //if the user changes the output directory command factory will send this info to us in the output parameter
91 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
93 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
96 //make sure the user has already run the read.otu command
97 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; }
99 //check for optional parameter and set defaults
100 // ...at some point should added some additional type checking...
101 label = validParameter.validFile(parameters, "label", false);
102 if (label == "not found") { label = ""; }
104 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
105 else { allLines = 1; }
108 //if the user has not specified any labels use the ones from read.otu
110 allLines = globaldata->allLines;
111 labels = globaldata->labels;
114 if (abort == false) {
115 filename = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "sabund";
116 m->openOutputFile(filename, out);
121 catch(exception& e) {
122 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
126 //**********************************************************************************************************************
128 void GetSAbundCommand::help(){
130 m->mothurOut("The get.sabund command can only be executed after a successful read.otu of a listfile or rabundfile.\n");
131 m->mothurOut("The get.sabund command parameters is label. No parameters are required.\n");
132 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");
133 m->mothurOut("The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n");
134 m->mothurOut("Example get.sabund().\n");
135 m->mothurOut("The default value for label is all labels in your inputfile.\n");
136 m->mothurOut("The get.sabund command outputs a .sabund file containing the labels you selected.\n");
137 m->mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
139 catch(exception& e) {
140 m->errorOut(e, "GetSAbundCommand", "help");
145 //**********************************************************************************************************************
147 GetSAbundCommand::~GetSAbundCommand(){
150 //**********************************************************************************************************************
152 int GetSAbundCommand::execute(){
155 if (abort == true) { return 0; }
157 //using order vector so you don't have to distinguish between the list and rabund files
158 read = new ReadOTUFile(globaldata->inputFileName);
159 read->read(&*globaldata);
161 order = globaldata->gorder;
162 string lastLabel = order->getLabel();
163 input = globaldata->ginput;
165 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
166 set<string> processedLabels;
167 set<string> userLabels = labels;
169 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
172 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
174 if(allLines == 1 || labels.count(order->getLabel()) == 1){
175 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
176 sabund = new SAbundVector();
177 *sabund = (order->getSAbundVector());
181 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
183 processedLabels.insert(order->getLabel());
184 userLabels.erase(order->getLabel());
187 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
188 string saveLabel = order->getLabel();
191 order = (input->getOrderVector(lastLabel));
193 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
194 sabund = new SAbundVector();
195 *sabund = (order->getSAbundVector());
199 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
201 processedLabels.insert(order->getLabel());
202 userLabels.erase(order->getLabel());
204 //restore real lastlabel to save below
205 order->setLabel(saveLabel);
209 lastLabel = order->getLabel();
212 order = (input->getOrderVector());
215 //output error messages about any remaining user labels
216 set<string>::iterator it;
217 bool needToRun = false;
218 for (it = userLabels.begin(); it != userLabels.end(); it++) {
219 m->mothurOut("Your file does not include the label " + *it);
220 if (processedLabels.count(lastLabel) != 1) {
221 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
224 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
228 //run last label if you need to
229 if (needToRun == true) {
230 if (order != NULL) { delete order; }
231 order = (input->getOrderVector(lastLabel));
233 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
234 sabund = new SAbundVector();
235 *sabund = (order->getSAbundVector());
239 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
243 globaldata->gorder = NULL;
247 m->mothurOutEndLine();
248 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
249 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
250 m->mothurOutEndLine();
255 catch(exception& e) {
256 m->errorOut(e, "GetSAbundCommand", "execute");
261 //**********************************************************************************************************************