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; }
72 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
75 vector<string> myArray = setParameters();
77 OptionParser parser(option);
78 map<string,string> parameters = parser.getParameters();
79 map<string,string>::iterator it;
81 ValidParameters validParameter;
83 //check to make sure all parameters are valid for command
84 for (it = parameters.begin(); it != parameters.end(); it++) {
85 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
88 //initialize outputTypes
89 vector<string> tempOutNames;
90 outputTypes["rabund"] = tempOutNames;
92 //if the user changes the input directory command factory will send this info to us in the output parameter
93 string inputDir = validParameter.validFile(parameters, "inputdir", false);
94 if (inputDir == "not found"){ inputDir = ""; }
97 it = parameters.find("list");
98 //user has given a template file
99 if(it != parameters.end()){
100 path = m->hasPath(it->second);
101 //if the user has not given a path then, add inputdir. else leave path alone.
102 if (path == "") { parameters["list"] = inputDir + it->second; }
105 it = parameters.find("sabund");
106 //user has given a template file
107 if(it != parameters.end()){
108 path = m->hasPath(it->second);
109 //if the user has not given a path then, add inputdir. else leave path alone.
110 if (path == "") { parameters["sabund"] = inputDir + it->second; }
115 //check for required parameters
116 listfile = validParameter.validFile(parameters, "list", true);
117 if (listfile == "not open") { listfile = ""; abort = true; }
118 else if (listfile == "not found") { listfile = ""; }
119 else { format = "list"; inputfile = listfile; m->setListFile(listfile); }
121 sabundfile = validParameter.validFile(parameters, "sabund", true);
122 if (sabundfile == "not open") { sabundfile = ""; abort = true; }
123 else if (sabundfile == "not found") { sabundfile = ""; }
124 else { format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
127 //check for optional parameter and set defaults
128 // ...at some point should added some additional type checking...
130 temp = validParameter.validFile(parameters, "sorted", false); if (temp == "not found") { temp = "T"; }
131 sorted = m->isTrue(temp);
133 label = validParameter.validFile(parameters, "label", false);
134 if (label == "not found") { label = ""; }
136 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
137 else { allLines = 1; }
140 if ((listfile == "") && (sabundfile == "")) {
141 //is there are current file available for any of these?
142 //give priority to shared, then list, then rabund, then sabund
143 //if there is a current shared file, use it
144 listfile = m->getListFile();
145 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
147 sabundfile = m->getSabundFile();
148 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
150 m->mothurOut("No valid current files. You must provide a list or sabund file."); m->mothurOutEndLine();
157 //if the user changes the output directory command factory will send this info to us in the output parameter
158 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
164 catch(exception& e) {
165 m->errorOut(e, "GetRAbundCommand", "GetRAbundCommand");
169 //**********************************************************************************************************************
171 int GetRAbundCommand::execute(){
174 if (abort == true) { if (calledHelp) { return 0; } return 2; }
176 filename = outputDir + m->getRootName(m->getSimpleName(inputfile)) + "rabund";
177 m->openOutputFile(filename, out);
179 input = new InputData(inputfile, format);
180 rabund = input->getRAbundVector();
181 string lastLabel = rabund->getLabel();
183 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
184 set<string> processedLabels;
185 set<string> userLabels = labels;
187 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete rabund; delete input; return 0; }
189 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
191 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
192 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
194 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; delete rabund; return 0; }
196 if(sorted) { rabund->print(out); }
197 else { rabund->nonSortedPrint(out); }
199 processedLabels.insert(rabund->getLabel());
200 userLabels.erase(rabund->getLabel());
203 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
204 string saveLabel = rabund->getLabel();
207 rabund = input->getRAbundVector(lastLabel);
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());
219 //restore real lastlabel to save below
220 rabund->setLabel(saveLabel);
223 lastLabel = rabund->getLabel();
226 rabund = input->getRAbundVector();
229 //output error messages about any remaining user labels
230 set<string>::iterator it;
231 bool needToRun = false;
232 for (it = userLabels.begin(); it != userLabels.end(); it++) {
233 m->mothurOut("Your file does not include the label " + *it);
234 if (processedLabels.count(lastLabel) != 1) {
235 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
238 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
242 //run last label if you need to
243 if (needToRun == true) {
244 if (rabund != NULL) { delete rabund; }
245 rabund = input->getRAbundVector(lastLabel);
247 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
249 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; delete rabund; return 0; }
251 if(sorted) { rabund->print(out); }
252 else { rabund->nonSortedPrint(out); }
257 m->mothurOutEndLine();
258 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
259 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["rabund"].push_back(filename);
260 m->mothurOutEndLine();
264 //set rabund file as new current rabundfile
266 itTypes = outputTypes.find("rabund");
267 if (itTypes != outputTypes.end()) {
268 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRabundFile(current); }
274 catch(exception& e) {
275 m->errorOut(e, "GetRAbundCommand", "execute");
280 //**********************************************************************************************************************