5 * Created by Sarah Westcott on 6/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "getsabundcommand.h"
12 //**********************************************************************************************************************
13 vector<string> GetSAbundCommand::setParameters(){
15 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none","sabund",false,false, true); parameters.push_back(plist);
16 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none","sabund",false,false, true); parameters.push_back(prabund);
17 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
18 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
19 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "GetSAbundCommand", "setParameters");
30 //**********************************************************************************************************************
31 string GetSAbundCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The get.sabund command parameters is list, rabund and label. list or rabund is required unless a valid current file exists.\n";
35 helpString += "The label parameter allows you to select what distance levels you would like included in your .sabund file, and are separated by dashes.\n";
36 helpString += "The get.sabund command should be in the following format: get.sabund(label=yourLabels).\n";
37 helpString += "Example get.sabund().\n";
38 helpString += "The default value for label is all labels in your inputfile.\n";
39 helpString += "The get.sabund command outputs a .sabund file containing the labels you selected.\n";
40 helpString += "Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n";
44 m->errorOut(e, "GetSAbundCommand", "getHelpString");
48 //**********************************************************************************************************************
49 string GetSAbundCommand::getOutputPattern(string type) {
53 if (type == "sabund") { pattern = "[filename],sabund"; }
54 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
59 m->errorOut(e, "GetRAbundCommand", "getOutputPattern");
63 //**********************************************************************************************************************
64 GetSAbundCommand::GetSAbundCommand(){
66 abort = true; calledHelp = true;
68 vector<string> tempOutNames;
69 outputTypes["sabund"] = tempOutNames;
72 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
76 //**********************************************************************************************************************
77 GetSAbundCommand::GetSAbundCommand(string option) {
79 abort = false; calledHelp = false;
82 //allow user to run help
83 if(option == "help") { help(); abort = true; calledHelp = true; }
84 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
87 vector<string> myArray = setParameters();
89 OptionParser parser(option);
90 map<string,string> parameters = parser.getParameters();
91 map<string,string>::iterator it;
93 ValidParameters validParameter;
95 //check to make sure all parameters are valid for command
96 for (it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["sabund"] = tempOutNames;
104 //if the user changes the input directory command factory will send this info to us in the output parameter
105 string inputDir = validParameter.validFile(parameters, "inputdir", false);
106 if (inputDir == "not found"){ inputDir = ""; }
109 it = parameters.find("list");
110 //user has given a template file
111 if(it != parameters.end()){
112 path = m->hasPath(it->second);
113 //if the user has not given a path then, add inputdir. else leave path alone.
114 if (path == "") { parameters["list"] = inputDir + it->second; }
117 it = parameters.find("rabund");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["rabund"] = inputDir + it->second; }
127 //check for required parameters
128 listfile = validParameter.validFile(parameters, "list", true);
129 if (listfile == "not open") { listfile = ""; abort = true; }
130 else if (listfile == "not found") { listfile = ""; }
131 else { format = "list"; inputfile = listfile; m->setListFile(listfile); }
133 rabundfile = validParameter.validFile(parameters, "rabund", true);
134 if (rabundfile == "not open") { rabundfile = ""; abort = true; }
135 else if (rabundfile == "not found") { rabundfile = ""; }
136 else { format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
139 //check for optional parameter and set defaults
140 // ...at some point should added some additional type checking...
141 label = validParameter.validFile(parameters, "label", false);
142 if (label == "not found") { label = ""; }
144 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
145 else { allLines = 1; }
148 if ((listfile == "") && (rabundfile == "")) {
149 //is there are current file available for any of these?
150 //give priority to shared, then list, then rabund, then sabund
151 //if there is a current shared file, use it
152 listfile = m->getListFile();
153 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
155 rabundfile = m->getRabundFile();
156 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
158 m->mothurOut("No valid current files. You must provide a list or rabund file."); m->mothurOutEndLine();
165 //if the user changes the output directory command factory will send this info to us in the output parameter
166 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
172 catch(exception& e) {
173 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
177 //**********************************************************************************************************************
179 int GetSAbundCommand::execute(){
182 if (abort == true) { if (calledHelp) { return 0; } return 2; }
183 map<string, string> variables;
184 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(inputfile));
185 filename = getOutputFileName("sabund", variables);
186 m->openOutputFile(filename, out);
188 input = new InputData(inputfile, format);
189 sabund = input->getSAbundVector();
190 string lastLabel = sabund->getLabel();
193 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
194 set<string> processedLabels;
195 set<string> userLabels = labels;
197 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete sabund; delete input; return 0; }
200 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
202 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
203 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
207 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete sabund; delete input; return 0; }
209 processedLabels.insert(sabund->getLabel());
210 userLabels.erase(sabund->getLabel());
213 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
214 string saveLabel = sabund->getLabel();
217 sabund = (input->getSAbundVector(lastLabel));
219 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
222 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete sabund; delete input; return 0; }
224 processedLabels.insert(sabund->getLabel());
225 userLabels.erase(sabund->getLabel());
227 //restore real lastlabel to save below
228 sabund->setLabel(saveLabel);
232 lastLabel = sabund->getLabel();
235 sabund = (input->getSAbundVector());
238 //output error messages about any remaining user labels
239 set<string>::iterator it;
240 bool needToRun = false;
241 for (it = userLabels.begin(); it != userLabels.end(); it++) {
242 m->mothurOut("Your file does not include the label " + *it);
243 if (processedLabels.count(lastLabel) != 1) {
244 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
247 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
251 //run last label if you need to
252 if (needToRun == true) {
253 if (sabund != NULL) { delete sabund; }
254 sabund = (input->getSAbundVector(lastLabel));
256 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
260 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; return 0; }
267 m->mothurOutEndLine();
268 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
269 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
270 m->mothurOutEndLine();
272 //set sabund file as new current sabundfile
274 itTypes = outputTypes.find("sabund");
275 if (itTypes != outputTypes.end()) {
276 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
282 catch(exception& e) {
283 m->errorOut(e, "GetSAbundCommand", "execute");
288 //**********************************************************************************************************************