2 * getrelabundcommand.cpp
5 * Created by westcott on 6/21/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "getrelabundcommand.h"
12 //**********************************************************************************************************************
13 vector<string> GetRelAbundCommand::setParameters(){
15 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none","relabund",false,true, true); parameters.push_back(pshared);
16 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
17 CommandParameter pscale("scale", "Multiple", "totalgroup-totalotu-averagegroup-averageotu", "totalgroup", "", "", "","",false,false); parameters.push_back(pscale);
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, "GetRelAbundCommand", "setParameters");
31 //**********************************************************************************************************************
32 string GetRelAbundCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The get.relabund command parameters are shared, groups, scale and label. shared is required, unless you have a valid current file.\n";
36 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included. The group names are separated by dashes.\n";
37 helpString += "The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n";
38 helpString += "The scale parameter allows you to select what scale you would like to use. Choices are totalgroup, totalotu, averagegroup, averageotu, default is totalgroup.\n";
39 helpString += "The get.relabund command should be in the following format: get.relabund(groups=yourGroups, label=yourLabels).\n";
40 helpString += "Example get.relabund(groups=A-B-C, scale=averagegroup).\n";
41 helpString += "The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n";
42 helpString += "The get.relabund command outputs a .relabund file.\n";
43 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
47 m->errorOut(e, "GetRelAbundCommand", "getHelpString");
51 //**********************************************************************************************************************
52 string GetRelAbundCommand::getOutputPattern(string type) {
56 if (type == "relabund") { pattern = "[filename],relabund"; }
57 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
62 m->errorOut(e, "GetRelAbundCommand", "getOutputPattern");
66 //**********************************************************************************************************************
67 GetRelAbundCommand::GetRelAbundCommand(){
69 abort = true; calledHelp = true;
71 vector<string> tempOutNames;
72 outputTypes["relabund"] = tempOutNames;
75 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
79 //**********************************************************************************************************************
80 GetRelAbundCommand::GetRelAbundCommand(string option) {
82 abort = false; calledHelp = false;
85 //allow user to run help
86 if(option == "help") { help(); abort = true; calledHelp = true; }
87 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
90 vector<string> myArray = setParameters();
92 OptionParser parser(option);
93 map<string,string> parameters = parser.getParameters();
94 map<string,string>::iterator it;
96 ValidParameters validParameter;
98 //check to make sure all parameters are valid for command
99 for (it = parameters.begin(); it != parameters.end(); it++) {
100 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
103 //initialize outputTypes
104 vector<string> tempOutNames;
105 outputTypes["relabund"] = tempOutNames;
107 //if the user changes the input directory command factory will send this info to us in the output parameter
108 string inputDir = validParameter.validFile(parameters, "inputdir", false);
109 if (inputDir == "not found"){ inputDir = ""; }
112 it = parameters.find("shared");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["shared"] = inputDir + it->second; }
122 sharedfile = validParameter.validFile(parameters, "shared", true);
123 if (sharedfile == "not open") { sharedfile = ""; abort = true; }
124 else if (sharedfile == "not found") {
125 //if there is a current shared file, use it
126 sharedfile = m->getSharedFile();
127 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
128 else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
129 }else { m->setSharedFile(sharedfile); }
132 //if the user changes the output directory command factory will send this info to us in the output parameter
133 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(sharedfile); }
135 //check for optional parameter and set defaults
136 // ...at some point should added some additional type checking...
137 label = validParameter.validFile(parameters, "label", false);
138 if (label == "not found") { label = ""; }
140 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
141 else { allLines = 1; }
144 groups = validParameter.validFile(parameters, "groups", false);
145 if (groups == "not found") { groups = ""; pickedGroups = false; }
148 m->splitAtDash(groups, Groups);
149 m->setGroups(Groups);
152 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "totalgroup"; }
154 if ((scale != "totalgroup") && (scale != "totalotu") && (scale != "averagegroup") && (scale != "averageotu")) {
155 m->mothurOut(scale + " is not a valid scaling option for the get.relabund command. Choices are totalgroup, totalotu, averagegroup, averageotu."); m->mothurOutEndLine(); abort = true;
160 catch(exception& e) {
161 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
165 //**********************************************************************************************************************
167 int GetRelAbundCommand::execute(){
170 if (abort == true) { if (calledHelp) { return 0; } return 2; }
172 map<string, string> variables;
173 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
174 string outputFileName = getOutputFileName("relabund", variables);
176 m->openOutputFile(outputFileName, out);
177 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
179 input = new InputData(sharedfile, "sharedfile");
180 lookup = input->getSharedRAbundVectors();
181 string lastLabel = lookup[0]->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 //as long as you are not at the end of the file or done wih the lines you want
188 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
190 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); delete input; out.close(); m->mothurRemove(outputFileName); return 0; }
192 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
194 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
195 if (!m->printedSharedHeaders) { lookup[0]->printHeaders(out); }
196 getRelAbundance(lookup, out);
198 processedLabels.insert(lookup[0]->getLabel());
199 userLabels.erase(lookup[0]->getLabel());
202 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
203 string saveLabel = lookup[0]->getLabel();
205 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
206 lookup = input->getSharedRAbundVectors(lastLabel);
207 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
208 if (!m->printedSharedHeaders) { lookup[0]->printHeaders(out); }
209 getRelAbundance(lookup, out);
211 processedLabels.insert(lookup[0]->getLabel());
212 userLabels.erase(lookup[0]->getLabel());
214 //restore real lastlabel to save below
215 lookup[0]->setLabel(saveLabel);
218 lastLabel = lookup[0]->getLabel();
219 //prevent memory leak
220 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
222 if (m->control_pressed) { outputTypes.clear(); m->clearGroups(); delete input; out.close(); m->mothurRemove(outputFileName); return 0; }
224 //get next line to process
225 lookup = input->getSharedRAbundVectors();
228 if (m->control_pressed) { outputTypes.clear(); m->clearGroups(); delete input; out.close(); m->mothurRemove(outputFileName); return 0; }
230 //output error messages about any remaining user labels
231 set<string>::iterator it;
232 bool needToRun = false;
233 for (it = userLabels.begin(); it != userLabels.end(); it++) {
234 m->mothurOut("Your file does not include the label " + *it);
235 if (processedLabels.count(lastLabel) != 1) {
236 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
239 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
243 //run last label if you need to
244 if (needToRun == true) {
245 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
246 lookup = input->getSharedRAbundVectors(lastLabel);
248 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
249 if (!m->printedSharedHeaders) { lookup[0]->printHeaders(out); }
250 getRelAbundance(lookup, out);
252 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
255 //reset groups parameter
260 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFileName); return 0;}
262 m->mothurOutEndLine();
263 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
264 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["relabund"].push_back(outputFileName);
265 m->mothurOutEndLine();
267 //set relabund file as new current relabundfile
269 itTypes = outputTypes.find("relabund");
270 if (itTypes != outputTypes.end()) {
271 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRelAbundFile(current); }
276 catch(exception& e) {
277 m->errorOut(e, "GetRelAbundCommand", "execute");
281 //**********************************************************************************************************************
283 int GetRelAbundCommand::getRelAbundance(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
286 for (int i = 0; i < thisLookUp.size(); i++) {
287 out << thisLookUp[i]->getLabel() << '\t' << thisLookUp[i]->getGroup() << '\t' << thisLookUp[i]->getNumBins() << '\t';
289 for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) {
291 if (m->control_pressed) { return 0; }
293 int abund = thisLookUp[i]->getAbundance(j);
295 float relabund = 0.0;
297 if (scale == "totalgroup") {
298 relabund = abund / (float) thisLookUp[i]->getNumSeqs();
299 }else if (scale == "totalotu") {
300 //calc the total in this otu
302 for (int l = 0; l < thisLookUp.size(); l++) { totalOtu += thisLookUp[l]->getAbundance(j); }
303 relabund = abund / (float) totalOtu;
304 }else if (scale == "averagegroup") {
305 relabund = abund / (float) (thisLookUp[i]->getNumSeqs() / (float) thisLookUp[i]->getNumBins());
306 }else if (scale == "averageotu") {
307 //calc the total in this otu
309 for (int l = 0; l < thisLookUp.size(); l++) { totalOtu += thisLookUp[l]->getAbundance(j); }
310 float averageOtu = totalOtu / (float) thisLookUp.size();
312 relabund = abund / (float) averageOtu;
313 }else{ m->mothurOut(scale + " is not a valid scaling option."); m->mothurOutEndLine(); m->control_pressed = true; return 0; }
315 out << relabund << '\t';
322 catch(exception& e) {
323 m->errorOut(e, "GetRelAbundCommand", "getRelAbundance");
327 //**********************************************************************************************************************