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");
49 //**********************************************************************************************************************
50 GetSAbundCommand::GetSAbundCommand(){
52 abort = true; calledHelp = true;
54 vector<string> tempOutNames;
55 outputTypes["sabund"] = tempOutNames;
58 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
62 //**********************************************************************************************************************
63 GetSAbundCommand::GetSAbundCommand(string option) {
65 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
70 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
73 vector<string> myArray = setParameters();
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
77 map<string,string>::iterator it;
79 ValidParameters validParameter;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["sabund"] = tempOutNames;
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("list");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["list"] = inputDir + it->second; }
103 it = parameters.find("rabund");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["rabund"] = inputDir + it->second; }
113 //check for required parameters
114 listfile = validParameter.validFile(parameters, "list", true);
115 if (listfile == "not open") { listfile = ""; abort = true; }
116 else if (listfile == "not found") { listfile = ""; }
117 else { format = "list"; inputfile = listfile; m->setListFile(listfile); }
119 rabundfile = validParameter.validFile(parameters, "rabund", true);
120 if (rabundfile == "not open") { rabundfile = ""; abort = true; }
121 else if (rabundfile == "not found") { rabundfile = ""; }
122 else { format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
125 //check for optional parameter and set defaults
126 // ...at some point should added some additional type checking...
127 label = validParameter.validFile(parameters, "label", false);
128 if (label == "not found") { label = ""; }
130 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
131 else { allLines = 1; }
134 if ((listfile == "") && (rabundfile == "")) {
135 //is there are current file available for any of these?
136 //give priority to shared, then list, then rabund, then sabund
137 //if there is a current shared file, use it
138 listfile = m->getListFile();
139 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
141 rabundfile = m->getRabundFile();
142 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
144 m->mothurOut("No valid current files. You must provide a list or rabund file."); m->mothurOutEndLine();
151 //if the user changes the output directory command factory will send this info to us in the output parameter
152 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
158 catch(exception& e) {
159 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
163 //**********************************************************************************************************************
165 int GetSAbundCommand::execute(){
168 if (abort == true) { if (calledHelp) { return 0; } return 2; }
169 filename = outputDir + m->getRootName(m->getSimpleName(inputfile)) + "sabund";
170 m->openOutputFile(filename, out);
172 input = new InputData(inputfile, format);
173 sabund = input->getSAbundVector();
174 string lastLabel = sabund->getLabel();
177 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
178 set<string> processedLabels;
179 set<string> userLabels = labels;
181 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete sabund; delete input; return 0; }
184 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
186 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
187 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
191 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete sabund; delete input; return 0; }
193 processedLabels.insert(sabund->getLabel());
194 userLabels.erase(sabund->getLabel());
197 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
198 string saveLabel = sabund->getLabel();
201 sabund = (input->getSAbundVector(lastLabel));
203 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
206 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete sabund; delete input; return 0; }
208 processedLabels.insert(sabund->getLabel());
209 userLabels.erase(sabund->getLabel());
211 //restore real lastlabel to save below
212 sabund->setLabel(saveLabel);
216 lastLabel = sabund->getLabel();
219 sabund = (input->getSAbundVector());
222 //output error messages about any remaining user labels
223 set<string>::iterator it;
224 bool needToRun = false;
225 for (it = userLabels.begin(); it != userLabels.end(); it++) {
226 m->mothurOut("Your file does not include the label " + *it);
227 if (processedLabels.count(lastLabel) != 1) {
228 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
231 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
235 //run last label if you need to
236 if (needToRun == true) {
237 if (sabund != NULL) { delete sabund; }
238 sabund = (input->getSAbundVector(lastLabel));
240 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
244 if (m->control_pressed) { outputTypes.clear(); out.close(); m->mothurRemove(filename); delete input; return 0; }
251 m->mothurOutEndLine();
252 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
253 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
254 m->mothurOutEndLine();
256 //set sabund file as new current sabundfile
258 itTypes = outputTypes.find("sabund");
259 if (itTypes != outputTypes.end()) {
260 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
266 catch(exception& e) {
267 m->errorOut(e, "GetSAbundCommand", "execute");
272 //**********************************************************************************************************************