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");
50 //**********************************************************************************************************************
51 string GetRAbundCommand::getOutputFileNameTag(string type, string inputName=""){
53 string outputFileName = "";
54 map<string, vector<string> >::iterator it;
56 //is this a type this command creates
57 it = outputTypes.find(type);
58 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
60 if (type == "rabund") { outputFileName = "rabund"; }
61 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
63 return outputFileName;
66 m->errorOut(e, "GetRAbundCommand", "getOutputFileNameTag");
70 //**********************************************************************************************************************
71 GetRAbundCommand::GetRAbundCommand(){
73 abort = true; calledHelp = true;
75 vector<string> tempOutNames;
76 outputTypes["rabund"] = tempOutNames;
79 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
83 //**********************************************************************************************************************
84 GetRAbundCommand::GetRAbundCommand(string option) {
86 abort = false; calledHelp = false;
89 //allow user to run help
90 if(option == "help") { help(); abort = true; calledHelp = true; }
91 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
94 vector<string> myArray = setParameters();
96 OptionParser parser(option);
97 map<string,string> parameters = parser.getParameters();
98 map<string,string>::iterator it;
100 ValidParameters validParameter;
102 //check to make sure all parameters are valid for command
103 for (it = parameters.begin(); it != parameters.end(); it++) {
104 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
107 //initialize outputTypes
108 vector<string> tempOutNames;
109 outputTypes["rabund"] = tempOutNames;
111 //if the user changes the input directory command factory will send this info to us in the output parameter
112 string inputDir = validParameter.validFile(parameters, "inputdir", false);
113 if (inputDir == "not found"){ inputDir = ""; }
116 it = parameters.find("list");
117 //user has given a template file
118 if(it != parameters.end()){
119 path = m->hasPath(it->second);
120 //if the user has not given a path then, add inputdir. else leave path alone.
121 if (path == "") { parameters["list"] = inputDir + it->second; }
124 it = parameters.find("sabund");
125 //user has given a template file
126 if(it != parameters.end()){
127 path = m->hasPath(it->second);
128 //if the user has not given a path then, add inputdir. else leave path alone.
129 if (path == "") { parameters["sabund"] = inputDir + it->second; }
134 //check for required parameters
135 listfile = validParameter.validFile(parameters, "list", true);
136 if (listfile == "not open") { listfile = ""; abort = true; }
137 else if (listfile == "not found") { listfile = ""; }
138 else { format = "list"; inputfile = listfile; m->setListFile(listfile); }
140 sabundfile = validParameter.validFile(parameters, "sabund", true);
141 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
142 else if (sabundfile == "not found") { sabundfile = ""; }
143 else { format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
146 //check for optional parameter and set defaults
147 // ...at some point should added some additional type checking...
149 temp = validParameter.validFile(parameters, "sorted", false); if (temp == "not found") { temp = "T"; }
150 sorted = m->isTrue(temp);
152 label = validParameter.validFile(parameters, "label", false);
153 if (label == "not found") { label = ""; }
155 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
156 else { allLines = 1; }
159 if ((listfile == "") && (sabundfile == "")) {
160 //is there are current file available for any of these?
161 //give priority to shared, then list, then rabund, then sabund
162 //if there is a current shared file, use it
163 listfile = m->getListFile();
164 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
166 sabundfile = m->getSabundFile();
167 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
169 m->mothurOut("No valid current files. You must provide a list or sabund file."); m->mothurOutEndLine();
176 //if the user changes the output directory command factory will send this info to us in the output parameter
177 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
183 catch(exception& e) {
184 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
188 //**********************************************************************************************************************
190 int GetRAbundCommand::execute(){
193 if (abort == true) { if (calledHelp) { return 0; } return 2; }
195 filename = outputDir + m->getRootName(m->getSimpleName(inputfile)) + getOutputFileNameTag("rabund");
196 m->openOutputFile(filename, out);
198 input = new InputData(inputfile, format);
199 rabund = input->getRAbundVector();
200 string lastLabel = rabund->getLabel();
202 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
203 set<string> processedLabels;
204 set<string> userLabels = labels;
206 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete rabund; delete input; return 0; }
208 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
210 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
211 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
213 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; delete rabund; return 0; }
215 if(sorted) { rabund->print(out); }
216 else { rabund->nonSortedPrint(out); }
218 processedLabels.insert(rabund->getLabel());
219 userLabels.erase(rabund->getLabel());
222 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
223 string saveLabel = rabund->getLabel();
226 rabund = input->getRAbundVector(lastLabel);
228 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
230 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; delete rabund; return 0; }
232 if(sorted) { rabund->print(out); }
233 else { rabund->nonSortedPrint(out); }
235 processedLabels.insert(rabund->getLabel());
236 userLabels.erase(rabund->getLabel());
238 //restore real lastlabel to save below
239 rabund->setLabel(saveLabel);
242 lastLabel = rabund->getLabel();
245 rabund = input->getRAbundVector();
248 //output error messages about any remaining user labels
249 set<string>::iterator it;
250 bool needToRun = false;
251 for (it = userLabels.begin(); it != userLabels.end(); it++) {
252 m->mothurOut("Your file does not include the label " + *it);
253 if (processedLabels.count(lastLabel) != 1) {
254 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
257 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
261 //run last label if you need to
262 if (needToRun == true) {
263 if (rabund != NULL) { delete rabund; }
264 rabund = input->getRAbundVector(lastLabel);
266 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
268 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; delete rabund; return 0; }
270 if(sorted) { rabund->print(out); }
271 else { rabund->nonSortedPrint(out); }
276 m->mothurOutEndLine();
277 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
278 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["rabund"].push_back(filename);
279 m->mothurOutEndLine();
283 //set rabund file as new current rabundfile
285 itTypes = outputTypes.find("rabund");
286 if (itTypes != outputTypes.end()) {
287 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
293 catch(exception& e) {
294 m->errorOut(e, "GetRAbundCommand", "execute");
299 //**********************************************************************************************************************