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","rabund",false,false, true); parameters.push_back(plist);
16 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none","rabund",false,false, true); 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");
50 //**********************************************************************************************************************
51 string GetRAbundCommand::getOutputPattern(string type) {
55 if (type == "rabund") { pattern = "[filename],rabund"; }
56 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
61 m->errorOut(e, "GetRAbundCommand", "getOutputPattern");
66 //**********************************************************************************************************************
67 GetRAbundCommand::GetRAbundCommand(){
69 abort = true; calledHelp = true;
71 vector<string> tempOutNames;
72 outputTypes["rabund"] = tempOutNames;
75 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
79 //**********************************************************************************************************************
80 GetRAbundCommand::GetRAbundCommand(string option) {
82 abort = false; calledHelp = false;
85 //allow user to run help
86 if(option == "help") { help(); abort = true; calledHelp = true; }
87 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
90 vector<string> myArray = setParameters();
92 OptionParser parser(option);
93 map<string,string> parameters = parser.getParameters();
94 map<string,string>::iterator it;
96 ValidParameters validParameter;
98 //check to make sure all parameters are valid for command
99 for (it = parameters.begin(); it != parameters.end(); it++) {
100 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
103 //initialize outputTypes
104 vector<string> tempOutNames;
105 outputTypes["rabund"] = tempOutNames;
107 //if the user changes the input directory command factory will send this info to us in the output parameter
108 string inputDir = validParameter.validFile(parameters, "inputdir", false);
109 if (inputDir == "not found"){ inputDir = ""; }
112 it = parameters.find("list");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["list"] = inputDir + it->second; }
120 it = parameters.find("sabund");
121 //user has given a template file
122 if(it != parameters.end()){
123 path = m->hasPath(it->second);
124 //if the user has not given a path then, add inputdir. else leave path alone.
125 if (path == "") { parameters["sabund"] = inputDir + it->second; }
130 //check for required parameters
131 listfile = validParameter.validFile(parameters, "list", true);
132 if (listfile == "not open") { listfile = ""; abort = true; }
133 else if (listfile == "not found") { listfile = ""; }
134 else { format = "list"; inputfile = listfile; m->setListFile(listfile); }
136 sabundfile = validParameter.validFile(parameters, "sabund", true);
137 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
138 else if (sabundfile == "not found") { sabundfile = ""; }
139 else { format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
142 //check for optional parameter and set defaults
143 // ...at some point should added some additional type checking...
145 temp = validParameter.validFile(parameters, "sorted", false); if (temp == "not found") { temp = "T"; }
146 sorted = m->isTrue(temp);
148 label = validParameter.validFile(parameters, "label", false);
149 if (label == "not found") { label = ""; }
151 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
152 else { allLines = 1; }
155 if ((listfile == "") && (sabundfile == "")) {
156 //is there are current file available for any of these?
157 //give priority to shared, then list, then rabund, then sabund
158 //if there is a current shared file, use it
159 listfile = m->getListFile();
160 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
162 sabundfile = m->getSabundFile();
163 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
165 m->mothurOut("No valid current files. You must provide a list or sabund file."); m->mothurOutEndLine();
172 //if the user changes the output directory command factory will send this info to us in the output parameter
173 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
179 catch(exception& e) {
180 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
184 //**********************************************************************************************************************
186 int GetRAbundCommand::execute(){
189 if (abort == true) { if (calledHelp) { return 0; } return 2; }
191 map<string, string> variables;
192 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
193 filename = getOutputFileName("rabund", variables);
194 m->openOutputFile(filename, out);
196 input = new InputData(inputfile, format);
197 rabund = input->getRAbundVector();
198 string lastLabel = rabund->getLabel();
200 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
201 set<string> processedLabels;
202 set<string> userLabels = labels;
204 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete rabund; delete input; return 0; }
206 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
208 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
209 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
211 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; delete rabund; return 0; }
213 if(sorted) { rabund->print(out); }
214 else { rabund->nonSortedPrint(out); }
216 processedLabels.insert(rabund->getLabel());
217 userLabels.erase(rabund->getLabel());
220 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
221 string saveLabel = rabund->getLabel();
224 rabund = input->getRAbundVector(lastLabel);
226 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
228 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; delete rabund; return 0; }
230 if(sorted) { rabund->print(out); }
231 else { rabund->nonSortedPrint(out); }
233 processedLabels.insert(rabund->getLabel());
234 userLabels.erase(rabund->getLabel());
236 //restore real lastlabel to save below
237 rabund->setLabel(saveLabel);
240 lastLabel = rabund->getLabel();
243 rabund = input->getRAbundVector();
246 //output error messages about any remaining user labels
247 set<string>::iterator it;
248 bool needToRun = false;
249 for (it = userLabels.begin(); it != userLabels.end(); it++) {
250 m->mothurOut("Your file does not include the label " + *it);
251 if (processedLabels.count(lastLabel) != 1) {
252 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
255 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
259 //run last label if you need to
260 if (needToRun == true) {
261 if (rabund != NULL) { delete rabund; }
262 rabund = input->getRAbundVector(lastLabel);
264 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
266 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; delete rabund; return 0; }
268 if(sorted) { rabund->print(out); }
269 else { rabund->nonSortedPrint(out); }
274 m->mothurOutEndLine();
275 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
276 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["rabund"].push_back(filename);
277 m->mothurOutEndLine();
281 //set rabund file as new current rabundfile
283 itTypes = outputTypes.find("rabund");
284 if (itTypes != outputTypes.end()) {
285 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
291 catch(exception& e) {
292 m->errorOut(e, "GetRAbundCommand", "execute");
297 //**********************************************************************************************************************