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",false,false); parameters.push_back(plist);
16 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); 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::getOutputFileNameTag(string type, string inputName=""){
51 string outputFileName = "";
52 map<string, vector<string> >::iterator it;
54 //is this a type this command creates
55 it = outputTypes.find(type);
56 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
58 if (type == "sabund") { outputFileName = "sabund"; }
59 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
61 return outputFileName;
64 m->errorOut(e, "GetSAbundCommand", "getOutputFileNameTag");
68 //**********************************************************************************************************************
69 GetSAbundCommand::GetSAbundCommand(){
71 abort = true; calledHelp = true;
73 vector<string> tempOutNames;
74 outputTypes["sabund"] = tempOutNames;
77 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
81 //**********************************************************************************************************************
82 GetSAbundCommand::GetSAbundCommand(string option) {
84 abort = false; calledHelp = false;
87 //allow user to run help
88 if(option == "help") { help(); abort = true; calledHelp = true; }
89 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
92 vector<string> myArray = setParameters();
94 OptionParser parser(option);
95 map<string,string> parameters = parser.getParameters();
96 map<string,string>::iterator it;
98 ValidParameters validParameter;
100 //check to make sure all parameters are valid for command
101 for (it = parameters.begin(); it != parameters.end(); it++) {
102 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
105 //initialize outputTypes
106 vector<string> tempOutNames;
107 outputTypes["sabund"] = tempOutNames;
109 //if the user changes the input directory command factory will send this info to us in the output parameter
110 string inputDir = validParameter.validFile(parameters, "inputdir", false);
111 if (inputDir == "not found"){ inputDir = ""; }
114 it = parameters.find("list");
115 //user has given a template file
116 if(it != parameters.end()){
117 path = m->hasPath(it->second);
118 //if the user has not given a path then, add inputdir. else leave path alone.
119 if (path == "") { parameters["list"] = inputDir + it->second; }
122 it = parameters.find("rabund");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["rabund"] = inputDir + it->second; }
132 //check for required parameters
133 listfile = validParameter.validFile(parameters, "list", true);
134 if (listfile == "not open") { listfile = ""; abort = true; }
135 else if (listfile == "not found") { listfile = ""; }
136 else { format = "list"; inputfile = listfile; m->setListFile(listfile); }
138 rabundfile = validParameter.validFile(parameters, "rabund", true);
139 if (rabundfile == "not open") { rabundfile = ""; abort = true; }
140 else if (rabundfile == "not found") { rabundfile = ""; }
141 else { format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
144 //check for optional parameter and set defaults
145 // ...at some point should added some additional type checking...
146 label = validParameter.validFile(parameters, "label", false);
147 if (label == "not found") { label = ""; }
149 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
150 else { allLines = 1; }
153 if ((listfile == "") && (rabundfile == "")) {
154 //is there are current file available for any of these?
155 //give priority to shared, then list, then rabund, then sabund
156 //if there is a current shared file, use it
157 listfile = m->getListFile();
158 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
160 rabundfile = m->getRabundFile();
161 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
163 m->mothurOut("No valid current files. You must provide a list or rabund file."); m->mothurOutEndLine();
170 //if the user changes the output directory command factory will send this info to us in the output parameter
171 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
177 catch(exception& e) {
178 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
182 //**********************************************************************************************************************
184 int GetSAbundCommand::execute(){
187 if (abort == true) { if (calledHelp) { return 0; } return 2; }
188 filename = outputDir + m->getRootName(m->getSimpleName(inputfile)) + getOutputFileNameTag("sabund");
189 m->openOutputFile(filename, out);
191 input = new InputData(inputfile, format);
192 sabund = input->getSAbundVector();
193 string lastLabel = sabund->getLabel();
196 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
197 set<string> processedLabels;
198 set<string> userLabels = labels;
200 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete sabund; delete input; return 0; }
203 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
205 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
206 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
210 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete sabund; delete input; return 0; }
212 processedLabels.insert(sabund->getLabel());
213 userLabels.erase(sabund->getLabel());
216 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
217 string saveLabel = sabund->getLabel();
220 sabund = (input->getSAbundVector(lastLabel));
222 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
225 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete sabund; delete input; return 0; }
227 processedLabels.insert(sabund->getLabel());
228 userLabels.erase(sabund->getLabel());
230 //restore real lastlabel to save below
231 sabund->setLabel(saveLabel);
235 lastLabel = sabund->getLabel();
238 sabund = (input->getSAbundVector());
241 //output error messages about any remaining user labels
242 set<string>::iterator it;
243 bool needToRun = false;
244 for (it = userLabels.begin(); it != userLabels.end(); it++) {
245 m->mothurOut("Your file does not include the label " + *it);
246 if (processedLabels.count(lastLabel) != 1) {
247 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
250 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
254 //run last label if you need to
255 if (needToRun == true) {
256 if (sabund != NULL) { delete sabund; }
257 sabund = (input->getSAbundVector(lastLabel));
259 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
263 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; return 0; }
270 m->mothurOutEndLine();
271 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
272 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
273 m->mothurOutEndLine();
275 //set sabund file as new current sabundfile
277 itTypes = outputTypes.find("sabund");
278 if (itTypes != outputTypes.end()) {
279 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
285 catch(exception& e) {
286 m->errorOut(e, "GetSAbundCommand", "execute");
291 //**********************************************************************************************************************