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; }
72 vector<string> myArray = setParameters();
74 OptionParser parser(option);
75 map<string,string> parameters = parser.getParameters();
76 map<string,string>::iterator it;
78 ValidParameters validParameter;
80 //check to make sure all parameters are valid for command
81 for (it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["sabund"] = tempOutNames;
89 //if the user changes the input directory command factory will send this info to us in the output parameter
90 string inputDir = validParameter.validFile(parameters, "inputdir", false);
91 if (inputDir == "not found"){ inputDir = ""; }
94 it = parameters.find("list");
95 //user has given a template file
96 if(it != parameters.end()){
97 path = m->hasPath(it->second);
98 //if the user has not given a path then, add inputdir. else leave path alone.
99 if (path == "") { parameters["list"] = inputDir + it->second; }
102 it = parameters.find("rabund");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["rabund"] = inputDir + it->second; }
112 //check for required parameters
113 listfile = validParameter.validFile(parameters, "list", true);
114 if (listfile == "not open") { listfile = ""; abort = true; }
115 else if (listfile == "not found") { listfile = ""; }
116 else { format = "list"; inputfile = listfile; }
118 rabundfile = validParameter.validFile(parameters, "rabund", true);
119 if (rabundfile == "not open") { rabundfile = ""; abort = true; }
120 else if (rabundfile == "not found") { rabundfile = ""; }
121 else { format = "rabund"; inputfile = rabundfile; }
124 //check for optional parameter and set defaults
125 // ...at some point should added some additional type checking...
126 label = validParameter.validFile(parameters, "label", false);
127 if (label == "not found") { label = ""; }
129 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
130 else { allLines = 1; }
133 if ((listfile == "") && (rabundfile == "")) {
134 //is there are current file available for any of these?
135 //give priority to shared, then list, then rabund, then sabund
136 //if there is a current shared file, use it
137 listfile = m->getListFile();
138 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
140 rabundfile = m->getRabundFile();
141 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
143 m->mothurOut("No valid current files. You must provide a list or rabund file."); m->mothurOutEndLine();
150 //if the user changes the output directory command factory will send this info to us in the output parameter
151 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
157 catch(exception& e) {
158 m->errorOut(e, "GetSAbundCommand", "GetSAbundCommand");
162 //**********************************************************************************************************************
164 int GetSAbundCommand::execute(){
167 if (abort == true) { if (calledHelp) { return 0; } return 2; }
168 filename = outputDir + m->getRootName(m->getSimpleName(inputfile)) + "sabund";
169 m->openOutputFile(filename, out);
171 input = new InputData(inputfile, format);
172 sabund = input->getSAbundVector();
173 string lastLabel = sabund->getLabel();
176 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
177 set<string> processedLabels;
178 set<string> userLabels = labels;
180 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete sabund; delete input; return 0; }
183 while((sabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
185 if(allLines == 1 || labels.count(sabund->getLabel()) == 1){
186 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
190 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete sabund; delete input; return 0; }
192 processedLabels.insert(sabund->getLabel());
193 userLabels.erase(sabund->getLabel());
196 if ((m->anyLabelsToProcess(sabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
197 string saveLabel = sabund->getLabel();
200 sabund = (input->getSAbundVector(lastLabel));
202 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
205 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete sabund; delete input; return 0; }
207 processedLabels.insert(sabund->getLabel());
208 userLabels.erase(sabund->getLabel());
210 //restore real lastlabel to save below
211 sabund->setLabel(saveLabel);
215 lastLabel = sabund->getLabel();
218 sabund = (input->getSAbundVector());
221 //output error messages about any remaining user labels
222 set<string>::iterator it;
223 bool needToRun = false;
224 for (it = userLabels.begin(); it != userLabels.end(); it++) {
225 m->mothurOut("Your file does not include the label " + *it);
226 if (processedLabels.count(lastLabel) != 1) {
227 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
230 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
234 //run last label if you need to
235 if (needToRun == true) {
236 if (sabund != NULL) { delete sabund; }
237 sabund = (input->getSAbundVector(lastLabel));
239 m->mothurOut(sabund->getLabel()); m->mothurOutEndLine();
243 if (m->control_pressed) { outputTypes.clear(); out.close(); remove(filename.c_str()); delete input; return 0; }
250 m->mothurOutEndLine();
251 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
252 m->mothurOut(filename); m->mothurOutEndLine(); outputNames.push_back(filename); outputTypes["sabund"].push_back(filename);
253 m->mothurOutEndLine();
255 //set sabund file as new current sabundfile
257 itTypes = outputTypes.find("sabund");
258 if (itTypes != outputTypes.end()) {
259 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setSabundFile(current); }
265 catch(exception& e) {
266 m->errorOut(e, "GetSAbundCommand", "execute");
271 //**********************************************************************************************************************