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(){
27 //initialize outputTypes
28 vector<string> tempOutNames;
29 outputTypes["sabund"] = tempOutNames;
32 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
36 //**********************************************************************************************************************
37 vector<string> GetSAbundCommand::getRequiredParameters(){
39 vector<string> myArray;
43 m->errorOut(e, "GetSAbundCommand", "getRequiredParameters");
47 //**********************************************************************************************************************
48 vector<string> GetSAbundCommand::getRequiredFiles(){
50 string Array[] = {"list","rabund"};
51 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55 m->errorOut(e, "GetSAbundCommand", "getRequiredFiles");
59 //**********************************************************************************************************************
60 GetSAbundCommand::GetSAbundCommand(string option) {
62 globaldata = GlobalData::getInstance();
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
71 //valid paramters for this command
72 string Array[] = {"label","outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
80 //check to make sure all parameters are valid for command
81 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["sabund"] = tempOutNames;
89 //if the user changes the output directory command factory will send this info to us in the output parameter
90 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
92 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
95 //make sure the user has already run the read.otu command
96 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; }
98 //check for optional parameter and set defaults
99 // ...at some point should added some additional type checking...
100 label = validParameter.validFile(parameters, "label", false);
101 if (label == "not found") { label = ""; }
103 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
104 else { allLines = 1; }
107 //if the user has not specified any labels use the ones from read.otu
109 allLines = globaldata->allLines;
110 labels = globaldata->labels;
113 if (abort == false) {
114 filename = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "sabund";
115 m->openOutputFile(filename, out);
120 catch(exception& e) {
121 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
125 //**********************************************************************************************************************
127 void GetSAbundCommand::help(){
129 m->mothurOut("The get.sabund command can only be executed after a successful read.otu of a listfile or rabundfile.\n");
130 m->mothurOut("The get.sabund command parameters is label. No parameters are required.\n");
131 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");
132 m->mothurOut("The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n");
133 m->mothurOut("Example get.sabund().\n");
134 m->mothurOut("The default value for label is all labels in your inputfile.\n");
135 m->mothurOut("The get.sabund command outputs a .sabund file containing the labels you selected.\n");
136 m->mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
138 catch(exception& e) {
139 m->errorOut(e, "GetSAbundCommand", "help");
144 //**********************************************************************************************************************
146 GetSAbundCommand::~GetSAbundCommand(){
149 //**********************************************************************************************************************
151 int GetSAbundCommand::execute(){
154 if (abort == true) { return 0; }
156 //using order vector so you don't have to distinguish between the list and rabund files
157 read = new ReadOTUFile(globaldata->inputFileName);
158 read->read(&*globaldata);
160 order = globaldata->gorder;
161 string lastLabel = order->getLabel();
162 input = globaldata->ginput;
164 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
165 set<string> processedLabels;
166 set<string> userLabels = labels;
168 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
171 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
173 if(allLines == 1 || labels.count(order->getLabel()) == 1){
174 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
175 sabund = new SAbundVector();
176 *sabund = (order->getSAbundVector());
180 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
182 processedLabels.insert(order->getLabel());
183 userLabels.erase(order->getLabel());
186 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
187 string saveLabel = order->getLabel();
190 order = (input->getOrderVector(lastLabel));
192 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
193 sabund = new SAbundVector();
194 *sabund = (order->getSAbundVector());
198 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
200 processedLabels.insert(order->getLabel());
201 userLabels.erase(order->getLabel());
203 //restore real lastlabel to save below
204 order->setLabel(saveLabel);
208 lastLabel = order->getLabel();
211 order = (input->getOrderVector());
214 //output error messages about any remaining user labels
215 set<string>::iterator it;
216 bool needToRun = false;
217 for (it = userLabels.begin(); it != userLabels.end(); it++) {
218 m->mothurOut("Your file does not include the label " + *it);
219 if (processedLabels.count(lastLabel) != 1) {
220 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
223 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
227 //run last label if you need to
228 if (needToRun == true) {
229 if (order != NULL) { delete order; }
230 order = (input->getOrderVector(lastLabel));
232 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
233 sabund = new SAbundVector();
234 *sabund = (order->getSAbundVector());
238 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete order; globaldata->gorder = NULL; return 0; }
242 globaldata->gorder = NULL;
246 m->mothurOutEndLine();
247 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
248 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
249 m->mothurOutEndLine();
254 catch(exception& e) {
255 m->errorOut(e, "GetSAbundCommand", "execute");
260 //**********************************************************************************************************************