5 * Created by Sarah Westcott on 6/2/09.
6 * Copyright 2009 Schloss Lab Umass Amherst. All rights reserved.
10 #include "getrabundcommand.h"
12 //**********************************************************************************************************************
13 vector<string> GetRAbundCommand::getValidParameters(){
15 string Array[] = {"label","sorted","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "GetRAbundCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 GetRAbundCommand::GetRAbundCommand(){
27 abort = true; calledHelp = true;
28 vector<string> tempOutNames;
29 outputTypes["rabund"] = tempOutNames;
32 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
36 //**********************************************************************************************************************
37 vector<string> GetRAbundCommand::getRequiredParameters(){
39 vector<string> myArray;
43 m->errorOut(e, "GetRAbundCommand", "getRequiredParameters");
47 //**********************************************************************************************************************
48 vector<string> GetRAbundCommand::getRequiredFiles(){
50 string Array[] = {"list"};
51 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
56 m->errorOut(e, "GetRAbundCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
61 GetRAbundCommand::GetRAbundCommand(string option) {
63 globaldata = GlobalData::getInstance();
64 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
72 //valid paramters for this command
73 string Array[] = {"label","sorted","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 //if the user changes the output directory command factory will send this info to us in the output parameter
87 string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
89 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
92 //initialize outputTypes
93 vector<string> tempOutNames;
94 outputTypes["rabund"] = tempOutNames;
96 //make sure the user has already run the read.otu command
97 if (globaldata->getListFile() == "") { m->mothurOut("You must read a listfile before you can use the get.rabund command."); m->mothurOutEndLine(); abort = true; }
99 //check for optional parameter and set defaults
100 // ...at some point should added some additional type checking...
103 temp = validParameter.validFile(parameters, "sorted", false); if (temp == "not found") { temp = "T"; }
104 sorted = m->isTrue(temp);
106 label = validParameter.validFile(parameters, "label", false);
107 if (label == "not found") { label = ""; }
109 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
110 else { allLines = 1; }
113 //if the user has not specified any labels use the ones from read.otu
115 allLines = globaldata->allLines;
116 labels = globaldata->labels;
119 if (abort == false) {
120 filename = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "rabund";
121 m->openOutputFile(filename, out);
126 catch(exception& e) {
127 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
131 //**********************************************************************************************************************
133 void GetRAbundCommand::help(){
135 m->mothurOut("The get.rabund command can only be executed after a successful read.otu of a listfile.\n");
136 m->mothurOut("The get.rabund command parameters are label and sorted. No parameters are required.\n");
137 m->mothurOut("The label parameter allows you to select what distance levels you would like included in your .rabund file, and are separated by dashes.\n");
138 m->mothurOut("The sorted parameters allows you to print the rabund results sorted by abundance or not. The default is sorted.\n");
139 m->mothurOut("The get.rabund command should be in the following format: get.rabund(label=yourLabels, sorted=yourSorted).\n");
140 m->mothurOut("Example get.rabund(sorted=F).\n");
141 m->mothurOut("The default value for label is all labels in your inputfile.\n");
142 m->mothurOut("The get.rabund command outputs a .rabund file containing the lines you selected.\n");
143 m->mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n\n");
145 catch(exception& e) {
146 m->errorOut(e, "GetRAbundCommand", "help");
151 //**********************************************************************************************************************
153 GetRAbundCommand::~GetRAbundCommand(){}
155 //**********************************************************************************************************************
157 int GetRAbundCommand::execute(){
160 if (abort == true) { if (calledHelp) { return 0; } return 2; }
163 read = new ReadOTUFile(globaldata->inputFileName);
164 read->read(&*globaldata);
166 input = globaldata->ginput;
167 list = globaldata->gListVector;
168 string lastLabel = list->getLabel();
170 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
171 set<string> processedLabels;
172 set<string> userLabels = labels;
174 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete list; globaldata->gListVector = NULL; return 0; }
176 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
178 if(allLines == 1 || labels.count(list->getLabel()) == 1){
179 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
180 rabund = new RAbundVector();
181 *rabund = (list->getRAbundVector());
183 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete list; delete rabund; globaldata->gListVector = NULL; return 0; }
186 if(sorted) { rabund->print(out); }
187 else { rabund->nonSortedPrint(out); }
191 processedLabels.insert(list->getLabel());
192 userLabels.erase(list->getLabel());
195 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
196 string saveLabel = list->getLabel();
199 list = input->getListVector(lastLabel);
201 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
202 rabund = new RAbundVector();
203 *rabund = (list->getRAbundVector());
205 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete list; delete rabund; globaldata->gListVector = NULL; return 0; }
207 if(sorted) { rabund->print(out); }
208 else { rabund->nonSortedPrint(out); }
212 processedLabels.insert(list->getLabel());
213 userLabels.erase(list->getLabel());
215 //restore real lastlabel to save below
216 list->setLabel(saveLabel);
219 lastLabel = list->getLabel();
222 list = input->getListVector();
225 //output error messages about any remaining user labels
226 set<string>::iterator it;
227 bool needToRun = false;
228 for (it = userLabels.begin(); it != userLabels.end(); it++) {
229 m->mothurOut("Your file does not include the label " + *it);
230 if (processedLabels.count(lastLabel) != 1) {
231 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
234 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
238 //run last label if you need to
239 if (needToRun == true) {
240 if (list != NULL) { delete list; }
241 list = input->getListVector(lastLabel);
243 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
244 rabund = new RAbundVector();
245 *rabund = (list->getRAbundVector());
247 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete list; delete rabund; globaldata->gListVector = NULL; return 0; }
249 if(sorted) { rabund->print(out); }
250 else { rabund->nonSortedPrint(out); }
256 m->mothurOutEndLine();
257 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
258 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["rabund"].push_back(filename);
259 m->mothurOutEndLine();
263 globaldata->gListVector = NULL;
268 catch(exception& e) {
269 m->errorOut(e, "GetRAbundCommand", "execute");
274 //**********************************************************************************************************************