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(){
27 abort = true; calledHelp = true;
28 vector<string> tempOutNames;
29 outputTypes["relabund"] = tempOutNames;
32 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
36 //**********************************************************************************************************************
37 vector<string> GetRelAbundCommand::getRequiredParameters(){
39 vector<string> myArray;
43 m->errorOut(e, "GetRelAbundCommand", "getRequiredParameters");
47 //**********************************************************************************************************************
48 vector<string> GetRelAbundCommand::getRequiredFiles(){
50 string Array[] = {"shared"};
51 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55 m->errorOut(e, "GetRelAbundCommand", "getRequiredFiles");
59 //**********************************************************************************************************************
60 GetRelAbundCommand::GetRelAbundCommand(string option) {
62 globaldata = GlobalData::getInstance();
63 abort = false; calledHelp = false;
67 //allow user to run help
68 if(option == "help") { help(); abort = true; calledHelp = true; }
71 //valid paramters for this command
72 string AlignArray[] = {"groups","label","scale","outputdir","inputdir"};
73 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
80 //check to make sure all parameters are valid for command
81 for (map<string,string>::iterator 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["relabund"] = tempOutNames;
89 //if the user changes the output directory command factory will send this info to us in the output parameter
90 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
92 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
95 //make sure the user has already run the read.otu command
96 if ((globaldata->getSharedFile() == "")) {
97 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;
100 //check for optional parameter and set defaults
101 // ...at some point should added some additional type checking...
102 label = validParameter.validFile(parameters, "label", false);
103 if (label == "not found") { label = ""; }
105 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
106 else { allLines = 1; }
109 //if the user has not specified any labels use the ones from read.otu
111 allLines = globaldata->allLines;
112 labels = globaldata->labels;
115 groups = validParameter.validFile(parameters, "groups", false);
116 if (groups == "not found") { groups = ""; pickedGroups = false; }
119 m->splitAtDash(groups, Groups);
120 globaldata->Groups = Groups;
123 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "totalgroup"; }
125 if ((scale != "totalgroup") && (scale != "totalotu") && (scale != "averagegroup") && (scale != "averageotu")) {
126 m->mothurOut(scale + " is not a valid scaling option for the get.relabund command. Choices are totalgroup, totalotu, averagegroup, averageotu."); m->mothurOutEndLine(); abort = true;
131 catch(exception& e) {
132 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
137 //**********************************************************************************************************************
139 void GetRelAbundCommand::help(){
141 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");
142 m->mothurOut("The get.relabund command parameters are groups, scale and label. No parameters are required.\n");
143 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");
144 m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n");
145 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");
146 m->mothurOut("The get.relabund command should be in the following format: get.relabund(groups=yourGroups, label=yourLabels).\n");
147 m->mothurOut("Example get.relabund(groups=A-B-C, scale=averagegroup).\n");
148 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
149 m->mothurOut("The get.relabund command outputs a .relabund file.\n");
150 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
153 catch(exception& e) {
154 m->errorOut(e, "GetRelAbundCommand", "help");
159 //**********************************************************************************************************************
161 GetRelAbundCommand::~GetRelAbundCommand(){
164 //**********************************************************************************************************************
166 int GetRelAbundCommand::execute(){
169 if (abort == true) { if (calledHelp) { return 0; } return 2; }
171 string outputFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + "relabund";
173 m->openOutputFile(outputFileName, out);
174 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
176 read = new ReadOTUFile(globaldata->inputFileName);
177 read->read(&*globaldata);
178 input = globaldata->ginput;
179 lookup = input->getSharedRAbundVectors();
180 string lastLabel = lookup[0]->getLabel();
182 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
183 set<string> processedLabels;
184 set<string> userLabels = labels;
186 //as long as you are not at the end of the file or done wih the lines you want
187 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
189 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; }
191 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
193 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
194 getRelAbundance(lookup, out);
196 processedLabels.insert(lookup[0]->getLabel());
197 userLabels.erase(lookup[0]->getLabel());
200 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
201 string saveLabel = lookup[0]->getLabel();
203 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
204 lookup = input->getSharedRAbundVectors(lastLabel);
205 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
207 getRelAbundance(lookup, out);
209 processedLabels.insert(lookup[0]->getLabel());
210 userLabels.erase(lookup[0]->getLabel());
212 //restore real lastlabel to save below
213 lookup[0]->setLabel(saveLabel);
216 lastLabel = lookup[0]->getLabel();
217 //prevent memory leak
218 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
220 if (m->control_pressed) { outputTypes.clear(); globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; }
222 //get next line to process
223 lookup = input->getSharedRAbundVectors();
226 if (m->control_pressed) { outputTypes.clear(); globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; }
228 //output error messages about any remaining user labels
229 set<string>::iterator it;
230 bool needToRun = false;
231 for (it = userLabels.begin(); it != userLabels.end(); it++) {
232 m->mothurOut("Your file does not include the label " + *it);
233 if (processedLabels.count(lastLabel) != 1) {
234 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
237 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
241 //run last label if you need to
242 if (needToRun == true) {
243 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
244 lookup = input->getSharedRAbundVectors(lastLabel);
246 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
248 getRelAbundance(lookup, out);
250 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
253 //reset groups parameter
254 globaldata->Groups.clear();
255 delete input; globaldata->ginput = NULL;
259 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0;}
261 m->mothurOutEndLine();
262 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
263 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["relabund"].push_back(outputFileName);
264 m->mothurOutEndLine();
268 catch(exception& e) {
269 m->errorOut(e, "GetRelAbundCommand", "execute");
273 //**********************************************************************************************************************
275 int GetRelAbundCommand::getRelAbundance(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
278 for (int i = 0; i < thisLookUp.size(); i++) {
279 out << thisLookUp[i]->getLabel() << '\t' << thisLookUp[i]->getGroup() << '\t' << thisLookUp[i]->getNumBins() << '\t';
281 for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) {
283 if (m->control_pressed) { return 0; }
285 int abund = thisLookUp[i]->getAbundance(j);
287 float relabund = 0.0;
289 if (scale == "totalgroup") {
290 relabund = abund / (float) thisLookUp[i]->getNumSeqs();
291 }else if (scale == "totalotu") {
292 //calc the total in this otu
294 for (int l = 0; l < thisLookUp.size(); l++) { totalOtu += thisLookUp[l]->getAbundance(j); }
295 relabund = abund / (float) totalOtu;
296 }else if (scale == "averagegroup") {
297 relabund = abund / (float) (thisLookUp[i]->getNumSeqs() / (float) thisLookUp[i]->getNumBins());
298 }else if (scale == "averageotu") {
299 //calc the total in this otu
301 for (int l = 0; l < thisLookUp.size(); l++) { totalOtu += thisLookUp[l]->getAbundance(j); }
302 float averageOtu = totalOtu / (float) thisLookUp.size();
304 relabund = abund / (float) averageOtu;
305 }else{ m->mothurOut(scale + " is not a valid scaling option."); m->mothurOutEndLine(); m->control_pressed = true; return 0; }
307 out << relabund << '\t';
314 catch(exception& e) {
315 m->errorOut(e, "GetRelAbundCommand", "getRelAbundance");
319 //**********************************************************************************************************************