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::setParameters(){
15 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
16 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(psabund);
17 CommandParameter psorted("sorted", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(psorted);
18 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
19 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
20 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
22 vector<string> myArray;
23 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
27 m->errorOut(e, "GetRAbundCommand", "setParameters");
31 //**********************************************************************************************************************
32 string GetRAbundCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The get.rabund command parameters are list, sabund, label and sorted. list or sabund parameters are required, unless you have valid current files.\n";
36 helpString += "The label parameter allows you to select what distance levels you would like included in your .rabund file, and are separated by dashes.\n";
37 helpString += "The sorted parameters allows you to print the rabund results sorted by abundance or not. The default is sorted.\n";
38 helpString += "The get.rabund command should be in the following format: get.rabund(label=yourLabels, sorted=yourSorted).\n";
39 helpString += "Example get.rabund(sorted=F).\n";
40 helpString += "The default value for label is all labels in your inputfile.\n";
41 helpString += "The get.rabund command outputs a .rabund file containing the lines you selected.\n";
42 helpString += "Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabels).\n";
46 m->errorOut(e, "GetRAbundCommand", "getHelpString");
51 //**********************************************************************************************************************
52 GetRAbundCommand::GetRAbundCommand(){
54 abort = true; calledHelp = true;
56 vector<string> tempOutNames;
57 outputTypes["rabund"] = tempOutNames;
60 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
64 //**********************************************************************************************************************
65 GetRAbundCommand::GetRAbundCommand(string option) {
67 abort = false; calledHelp = false;
70 //allow user to run help
71 if(option == "help") { help(); abort = true; calledHelp = true; }
74 vector<string> myArray = setParameters();
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
78 map<string,string>::iterator it;
80 ValidParameters validParameter;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["rabund"] = tempOutNames;
91 //if the user changes the input directory command factory will send this info to us in the output parameter
92 string inputDir = validParameter.validFile(parameters, "inputdir", false);
93 if (inputDir == "not found"){ inputDir = ""; }
96 it = parameters.find("list");
97 //user has given a template file
98 if(it != parameters.end()){
99 path = m->hasPath(it->second);
100 //if the user has not given a path then, add inputdir. else leave path alone.
101 if (path == "") { parameters["list"] = inputDir + it->second; }
104 it = parameters.find("sabund");
105 //user has given a template file
106 if(it != parameters.end()){
107 path = m->hasPath(it->second);
108 //if the user has not given a path then, add inputdir. else leave path alone.
109 if (path == "") { parameters["sabund"] = inputDir + it->second; }
114 //check for required parameters
115 listfile = validParameter.validFile(parameters, "list", true);
116 if (listfile == "not open") { listfile = ""; abort = true; }
117 else if (listfile == "not found") { listfile = ""; }
118 else { format = "list"; inputfile = listfile; }
120 sabundfile = validParameter.validFile(parameters, "sabund", true);
121 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
122 else if (sabundfile == "not found") { sabundfile = ""; }
123 else { format = "sabund"; inputfile = sabundfile; }
126 //check for optional parameter and set defaults
127 // ...at some point should added some additional type checking...
129 temp = validParameter.validFile(parameters, "sorted", false); if (temp == "not found") { temp = "T"; }
130 sorted = m->isTrue(temp);
132 label = validParameter.validFile(parameters, "label", false);
133 if (label == "not found") { label = ""; }
135 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
136 else { allLines = 1; }
139 if ((listfile == "") && (sabundfile == "")) {
140 //is there are current file available for any of these?
141 //give priority to shared, then list, then rabund, then sabund
142 //if there is a current shared file, use it
143 listfile = m->getListFile();
144 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
146 sabundfile = m->getSabundFile();
147 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
149 m->mothurOut("No valid current files. You must provide a list or sabund file."); m->mothurOutEndLine();
156 //if the user changes the output directory command factory will send this info to us in the output parameter
157 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
163 catch(exception& e) {
164 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
168 //**********************************************************************************************************************
170 int GetRAbundCommand::execute(){
173 if (abort == true) { if (calledHelp) { return 0; } return 2; }
175 filename = outputDir + m->getRootName(m->getSimpleName(inputfile)) + "rabund";
176 m->openOutputFile(filename, out);
178 input = new InputData(inputfile, format);
179 rabund = input->getRAbundVector();
180 string lastLabel = rabund->getLabel();
182 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
183 set<string> processedLabels;
184 set<string> userLabels = labels;
186 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete rabund; delete input; return 0; }
188 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
190 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
191 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
193 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete input; delete rabund; return 0; }
195 if(sorted) { rabund->print(out); }
196 else { rabund->nonSortedPrint(out); }
200 processedLabels.insert(rabund->getLabel());
201 userLabels.erase(rabund->getLabel());
204 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
205 string saveLabel = rabund->getLabel();
208 rabund = input->getRAbundVector(lastLabel);
210 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
212 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete input; delete rabund; return 0; }
214 if(sorted) { rabund->print(out); }
215 else { rabund->nonSortedPrint(out); }
219 processedLabels.insert(rabund->getLabel());
220 userLabels.erase(rabund->getLabel());
222 //restore real lastlabel to save below
223 rabund->setLabel(saveLabel);
226 lastLabel = rabund->getLabel();
229 rabund = input->getRAbundVector();
232 //output error messages about any remaining user labels
233 set<string>::iterator it;
234 bool needToRun = false;
235 for (it = userLabels.begin(); it != userLabels.end(); it++) {
236 m->mothurOut("Your file does not include the label " + *it);
237 if (processedLabels.count(lastLabel) != 1) {
238 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
241 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
245 //run last label if you need to
246 if (needToRun == true) {
247 if (rabund != NULL) { delete rabund; }
248 rabund = input->getRAbundVector(lastLabel);
250 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
252 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete input; delete rabund; return 0; }
254 if(sorted) { rabund->print(out); }
255 else { rabund->nonSortedPrint(out); }
260 m->mothurOutEndLine();
261 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
262 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["rabund"].push_back(filename);
263 m->mothurOutEndLine();
267 //set rabund file as new current rabundfile
269 itTypes = outputTypes.find("rabund");
270 if (itTypes != outputTypes.end()) {
271 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
277 catch(exception& e) {
278 m->errorOut(e, "GetRAbundCommand", "execute");
283 //**********************************************************************************************************************