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::getValidParameters(){
15 string Array[] = {"groups","label","scale","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "GetRelAbundCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 GetRelAbundCommand::GetRelAbundCommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["relabund"] = tempOutNames;
33 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
37 //**********************************************************************************************************************
38 vector<string> GetRelAbundCommand::getRequiredParameters(){
40 vector<string> myArray;
44 m->errorOut(e, "GetRelAbundCommand", "getRequiredParameters");
48 //**********************************************************************************************************************
49 vector<string> GetRelAbundCommand::getRequiredFiles(){
51 string Array[] = {"shared"};
52 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
56 m->errorOut(e, "GetRelAbundCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
61 GetRelAbundCommand::GetRelAbundCommand(string option) {
63 globaldata = GlobalData::getInstance();
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string AlignArray[] = {"groups","label","scale","outputdir","inputdir"};
74 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
81 //check to make sure all parameters are valid for command
82 for (map<string,string>::iterator 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["relabund"] = tempOutNames;
90 //if the user changes the output directory command factory will send this info to us in the output parameter
91 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
93 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
96 //make sure the user has already run the read.otu command
97 if ((globaldata->getSharedFile() == "")) {
98 m->mothurOut("You must read a list and a group, or a shared file before you can use the get.relabund command."); m->mothurOutEndLine(); abort = true;
101 //check for optional parameter and set defaults
102 // ...at some point should added some additional type checking...
103 label = validParameter.validFile(parameters, "label", false);
104 if (label == "not found") { label = ""; }
106 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
107 else { allLines = 1; }
110 //if the user has not specified any labels use the ones from read.otu
112 allLines = globaldata->allLines;
113 labels = globaldata->labels;
116 groups = validParameter.validFile(parameters, "groups", false);
117 if (groups == "not found") { groups = ""; pickedGroups = false; }
120 m->splitAtDash(groups, Groups);
121 globaldata->Groups = Groups;
124 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "totalgroup"; }
126 if ((scale != "totalgroup") && (scale != "totalotu") && (scale != "averagegroup") && (scale != "averageotu")) {
127 m->mothurOut(scale + " is not a valid scaling option for the get.relabund command. Choices are totalgroup, totalotu, averagegroup, averageotu."); m->mothurOutEndLine(); abort = true;
132 catch(exception& e) {
133 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
138 //**********************************************************************************************************************
140 void GetRelAbundCommand::help(){
142 m->mothurOut("The get.relabund command can only be executed after a successful read.otu command of a list and group or shared file.\n");
143 m->mothurOut("The get.relabund command parameters are groups, scale and label. No parameters are required.\n");
144 m->mothurOut("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");
145 m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n");
146 m->mothurOut("The scale parameter allows you to select what scale you would like to use. Choices are totalgroup, totalotu, averagegroup, averageotu, default is totalgroup.\n");
147 m->mothurOut("The get.relabund command should be in the following format: get.relabund(groups=yourGroups, label=yourLabels).\n");
148 m->mothurOut("Example get.relabund(groups=A-B-C, scale=averagegroup).\n");
149 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
150 m->mothurOut("The get.relabund command outputs a .relabund file.\n");
151 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
154 catch(exception& e) {
155 m->errorOut(e, "GetRelAbundCommand", "help");
160 //**********************************************************************************************************************
162 GetRelAbundCommand::~GetRelAbundCommand(){
165 //**********************************************************************************************************************
167 int GetRelAbundCommand::execute(){
170 if (abort == true) { return 0; }
172 string outputFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "relabund";
174 m->openOutputFile(outputFileName, out);
175 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
177 read = new ReadOTUFile(globaldata->inputFileName);
178 read->read(&*globaldata);
179 input = globaldata->ginput;
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]; } globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; }
192 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
194 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
195 getRelAbundance(lookup, out);
197 processedLabels.insert(lookup[0]->getLabel());
198 userLabels.erase(lookup[0]->getLabel());
201 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
202 string saveLabel = lookup[0]->getLabel();
204 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
205 lookup = input->getSharedRAbundVectors(lastLabel);
206 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
208 getRelAbundance(lookup, out);
210 processedLabels.insert(lookup[0]->getLabel());
211 userLabels.erase(lookup[0]->getLabel());
213 //restore real lastlabel to save below
214 lookup[0]->setLabel(saveLabel);
217 lastLabel = lookup[0]->getLabel();
218 //prevent memory leak
219 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
221 if (m->control_pressed) { outputTypes.clear(); globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; }
223 //get next line to process
224 lookup = input->getSharedRAbundVectors();
227 if (m->control_pressed) { outputTypes.clear(); globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; }
229 //output error messages about any remaining user labels
230 set<string>::iterator it;
231 bool needToRun = false;
232 for (it = userLabels.begin(); it != userLabels.end(); it++) {
233 m->mothurOut("Your file does not include the label " + *it);
234 if (processedLabels.count(lastLabel) != 1) {
235 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
238 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
242 //run last label if you need to
243 if (needToRun == true) {
244 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
245 lookup = input->getSharedRAbundVectors(lastLabel);
247 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
249 getRelAbundance(lookup, out);
251 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
254 //reset groups parameter
255 globaldata->Groups.clear();
256 delete input; globaldata->ginput = NULL;
260 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); 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();
269 catch(exception& e) {
270 m->errorOut(e, "GetRelAbundCommand", "execute");
274 //**********************************************************************************************************************
276 int GetRelAbundCommand::getRelAbundance(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
278 if (pickedGroups) { eliminateZeroOTUS(thisLookUp); }
281 for (int i = 0; i < thisLookUp.size(); i++) {
282 out << thisLookUp[i]->getLabel() << '\t' << thisLookUp[i]->getGroup() << '\t' << thisLookUp[i]->getNumBins() << '\t';
284 for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) {
286 if (m->control_pressed) { return 0; }
288 int abund = thisLookUp[i]->getAbundance(j);
290 float relabund = 0.0;
292 if (scale == "totalgroup") {
293 relabund = abund / (float) thisLookUp[i]->getNumSeqs();
294 }else if (scale == "totalotu") {
295 //calc the total in this otu
297 for (int l = 0; l < thisLookUp.size(); l++) { totalOtu += thisLookUp[l]->getAbundance(j); }
298 relabund = abund / (float) totalOtu;
299 }else if (scale == "averagegroup") {
300 relabund = abund / (float) (thisLookUp[i]->getNumSeqs() / (float) thisLookUp[i]->getNumBins());
301 }else if (scale == "averageotu") {
302 //calc the total in this otu
304 for (int l = 0; l < thisLookUp.size(); l++) { totalOtu += thisLookUp[l]->getAbundance(j); }
305 float averageOtu = totalOtu / (float) thisLookUp.size();
307 relabund = abund / (float) averageOtu;
308 }else{ m->mothurOut(scale + " is not a valid scaling option."); m->mothurOutEndLine(); m->control_pressed = true; return 0; }
310 out << relabund << '\t';
317 catch(exception& e) {
318 m->errorOut(e, "GetRelAbundCommand", "getRelAbundance");
322 //**********************************************************************************************************************
323 int GetRelAbundCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
326 vector<SharedRAbundVector*> newLookup;
327 for (int i = 0; i < thislookup.size(); i++) {
328 SharedRAbundVector* temp = new SharedRAbundVector();
329 temp->setLabel(thislookup[i]->getLabel());
330 temp->setGroup(thislookup[i]->getGroup());
331 newLookup.push_back(temp);
335 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
336 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
338 //look at each sharedRabund and make sure they are not all zero
340 for (int j = 0; j < thislookup.size(); j++) {
341 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
344 //if they are not all zero add this bin
346 for (int j = 0; j < thislookup.size(); j++) {
347 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
352 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
354 thislookup = newLookup;
359 catch(exception& e) {
360 m->errorOut(e, "GetRelAbundCommand", "eliminateZeroOTUS");
365 //**********************************************************************************************************************