2 * getrelabundcommand.cpp
5 * Created by westcott on 6/21/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "getrelabundcommand.h"
12 //**********************************************************************************************************************
14 GetRelAbundCommand::GetRelAbundCommand(string option) {
16 globaldata = GlobalData::getInstance();
21 //allow user to run help
22 if(option == "help") { help(); abort = true; }
25 //valid paramters for this command
26 string AlignArray[] = {"groups","label","scale","outputdir","inputdir"};
27 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
29 OptionParser parser(option);
30 map<string,string> parameters = parser.getParameters();
32 ValidParameters validParameter;
34 //check to make sure all parameters are valid for command
35 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the output directory command factory will send this info to us in the output parameter
40 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
42 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
45 //make sure the user has already run the read.otu command
46 if ((globaldata->getSharedFile() == "")) {
47 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;
50 //check for optional parameter and set defaults
51 // ...at some point should added some additional type checking...
52 label = validParameter.validFile(parameters, "label", false);
53 if (label == "not found") { label = ""; }
55 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
56 else { allLines = 1; }
59 //if the user has not specified any labels use the ones from read.otu
61 allLines = globaldata->allLines;
62 labels = globaldata->labels;
65 groups = validParameter.validFile(parameters, "groups", false);
66 if (groups == "not found") { groups = ""; pickedGroups = false; }
69 splitAtDash(groups, Groups);
70 globaldata->Groups = Groups;
73 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "totalgroup"; }
75 if ((scale != "totalgroup") && (scale != "totalotu") && (scale != "averagegroup") && (scale != "averageotu")) {
76 m->mothurOut(scale + " is not a valid scaling option for the get.relabund command. Choices are totalgroup, totalotu, averagegroup, averageotu."); m->mothurOutEndLine(); abort = true;
82 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
87 //**********************************************************************************************************************
89 void GetRelAbundCommand::help(){
91 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");
92 m->mothurOut("The get.relabund command parameters are groups, scale and label. No parameters are required.\n");
93 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");
94 m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n");
95 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");
96 m->mothurOut("The get.relabund command should be in the following format: get.relabund(groups=yourGroups, label=yourLabels).\n");
97 m->mothurOut("Example get.relabund(groups=A-B-C, scale=averagegroup).\n");
98 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
99 m->mothurOut("The get.relabund command outputs a .relabund file.\n");
100 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
103 catch(exception& e) {
104 m->errorOut(e, "GetRelAbundCommand", "help");
109 //**********************************************************************************************************************
111 GetRelAbundCommand::~GetRelAbundCommand(){
114 //**********************************************************************************************************************
116 int GetRelAbundCommand::execute(){
119 if (abort == true) { return 0; }
121 string outputFileName = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "relabund";
123 openOutputFile(outputFileName, out);
124 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
126 read = new ReadOTUFile(globaldata->inputFileName);
127 read->read(&*globaldata);
128 input = globaldata->ginput;
129 lookup = input->getSharedRAbundVectors();
130 string lastLabel = lookup[0]->getLabel();
132 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
133 set<string> processedLabels;
134 set<string> userLabels = labels;
136 //as long as you are not at the end of the file or done wih the lines you want
137 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
139 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; }
141 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
143 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
144 getRelAbundance(lookup, out);
146 processedLabels.insert(lookup[0]->getLabel());
147 userLabels.erase(lookup[0]->getLabel());
150 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
151 string saveLabel = lookup[0]->getLabel();
153 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
154 lookup = input->getSharedRAbundVectors(lastLabel);
155 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
157 getRelAbundance(lookup, out);
159 processedLabels.insert(lookup[0]->getLabel());
160 userLabels.erase(lookup[0]->getLabel());
162 //restore real lastlabel to save below
163 lookup[0]->setLabel(saveLabel);
166 lastLabel = lookup[0]->getLabel();
167 //prevent memory leak
168 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
170 if (m->control_pressed) { globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; }
172 //get next line to process
173 lookup = input->getSharedRAbundVectors();
176 if (m->control_pressed) { globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; }
178 //output error messages about any remaining user labels
179 set<string>::iterator it;
180 bool needToRun = false;
181 for (it = userLabels.begin(); it != userLabels.end(); it++) {
182 m->mothurOut("Your file does not include the label " + *it);
183 if (processedLabels.count(lastLabel) != 1) {
184 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
187 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
191 //run last label if you need to
192 if (needToRun == true) {
193 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
194 lookup = input->getSharedRAbundVectors(lastLabel);
196 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
198 getRelAbundance(lookup, out);
200 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
203 //reset groups parameter
204 globaldata->Groups.clear();
205 delete input; globaldata->ginput = NULL;
209 if (m->control_pressed) { remove(outputFileName.c_str()); return 0;}
211 m->mothurOutEndLine();
212 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
213 m->mothurOut(outputFileName); m->mothurOutEndLine();
214 m->mothurOutEndLine();
218 catch(exception& e) {
219 m->errorOut(e, "GetRelAbundCommand", "execute");
223 //**********************************************************************************************************************
225 int GetRelAbundCommand::getRelAbundance(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
227 if (pickedGroups) { eliminateZeroOTUS(thisLookUp); }
230 for (int i = 0; i < thisLookUp.size(); i++) {
231 out << thisLookUp[i]->getLabel() << '\t' << thisLookUp[i]->getGroup() << '\t' << thisLookUp[i]->getNumBins() << '\t';
233 for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) {
235 if (m->control_pressed) { return 0; }
237 int abund = thisLookUp[i]->getAbundance(j);
239 float relabund = 0.0;
241 if (scale == "totalgroup") {
242 relabund = abund / (float) thisLookUp[i]->getNumSeqs();
243 }else if (scale == "totalotu") {
244 //calc the total in this otu
246 for (int l = 0; l < thisLookUp.size(); l++) { totalOtu += thisLookUp[l]->getAbundance(j); }
247 relabund = abund / (float) totalOtu;
248 }else if (scale == "averagegroup") {
249 relabund = abund / (float) (thisLookUp[i]->getNumSeqs() / (float) thisLookUp[i]->getNumBins());
250 }else if (scale == "averageotu") {
251 //calc the total in this otu
253 for (int l = 0; l < thisLookUp.size(); l++) { totalOtu += thisLookUp[l]->getAbundance(j); }
254 float averageOtu = totalOtu / (float) thisLookUp.size();
256 relabund = abund / (float) averageOtu;
257 }else{ m->mothurOut(scale + " is not a valid scaling option."); m->mothurOutEndLine(); m->control_pressed = true; return 0; }
259 out << relabund << '\t';
266 catch(exception& e) {
267 m->errorOut(e, "GetRelAbundCommand", "getRelAbundance");
271 //**********************************************************************************************************************
272 int GetRelAbundCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
275 vector<SharedRAbundVector*> newLookup;
276 for (int i = 0; i < thislookup.size(); i++) {
277 SharedRAbundVector* temp = new SharedRAbundVector();
278 temp->setLabel(thislookup[i]->getLabel());
279 temp->setGroup(thislookup[i]->getGroup());
280 newLookup.push_back(temp);
284 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
285 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
287 //look at each sharedRabund and make sure they are not all zero
289 for (int j = 0; j < thislookup.size(); j++) {
290 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
293 //if they are not all zero add this bin
295 for (int j = 0; j < thislookup.size(); j++) {
296 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
301 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
303 thislookup = newLookup;
308 catch(exception& e) {
309 m->errorOut(e, "GetRelAbundCommand", "eliminateZeroOTUS");
314 //**********************************************************************************************************************