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 = ""; }
68 splitAtDash(groups, Groups);
69 globaldata->Groups = Groups;
72 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "totalgroup"; }
74 if ((scale != "totalgroup") && (scale != "totalgroup") && (scale != "totalgroup") && (scale != "totalgroup")) {
75 m->mothurOut(scale + " is not a valid scaling option for the get.relabund command. Choices are totalgroup, totalotu, averagegroup, averageotu."); m->mothurOutEndLine(); abort = true;
81 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
86 //**********************************************************************************************************************
88 void GetRelAbundCommand::help(){
90 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");
91 m->mothurOut("The get.relabund command parameters are groups, scale and label. No parameters are required.\n");
92 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");
93 m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n");
94 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");
95 m->mothurOut("The get.relabund command should be in the following format: get.relabund(groups=yourGroups, label=yourLabels).\n");
96 m->mothurOut("Example get.relabund(groups=A-B-C, scale=log10).\n");
97 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
98 m->mothurOut("The get.relabund command outputs a .relabund file.\n");
99 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
102 catch(exception& e) {
103 m->errorOut(e, "GetRelAbundCommand", "help");
108 //**********************************************************************************************************************
110 GetRelAbundCommand::~GetRelAbundCommand(){
113 //**********************************************************************************************************************
115 int GetRelAbundCommand::execute(){
118 if (abort == true) { return 0; }
120 string outputFileName = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "relabund";
122 openOutputFile(outputFileName, out);
124 read = new ReadOTUFile(globaldata->inputFileName);
125 read->read(&*globaldata);
126 input = globaldata->ginput;
127 lookup = input->getSharedRAbundVectors();
128 string lastLabel = lookup[0]->getLabel();
130 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
131 set<string> processedLabels;
132 set<string> userLabels = labels;
134 //as long as you are not at the end of the file or done wih the lines you want
135 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
137 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; }
139 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
141 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
142 getRelAbundance(lookup, out);
144 processedLabels.insert(lookup[0]->getLabel());
145 userLabels.erase(lookup[0]->getLabel());
148 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
149 string saveLabel = lookup[0]->getLabel();
151 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
152 lookup = input->getSharedRAbundVectors(lastLabel);
153 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
155 getRelAbundance(lookup, out);
157 processedLabels.insert(lookup[0]->getLabel());
158 userLabels.erase(lookup[0]->getLabel());
160 //restore real lastlabel to save below
161 lookup[0]->setLabel(saveLabel);
164 lastLabel = lookup[0]->getLabel();
165 //prevent memory leak
166 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
168 //get next line to process
169 lookup = input->getSharedRAbundVectors();
172 if (m->control_pressed) { globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; }
174 //output error messages about any remaining user labels
175 set<string>::iterator it;
176 bool needToRun = false;
177 for (it = userLabels.begin(); it != userLabels.end(); it++) {
178 m->mothurOut("Your file does not include the label " + *it);
179 if (processedLabels.count(lastLabel) != 1) {
180 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
183 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
187 //run last label if you need to
188 if (needToRun == true) {
189 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
190 lookup = input->getSharedRAbundVectors(lastLabel);
192 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
194 getRelAbundance(lookup, out);
196 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
199 //reset groups parameter
200 globaldata->Groups.clear();
201 delete input; globaldata->ginput = NULL;
205 if (m->control_pressed) { remove(outputFileName.c_str()); return 0;}
207 m->mothurOutEndLine();
208 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
209 m->mothurOut(outputFileName); m->mothurOutEndLine();
210 m->mothurOutEndLine();
214 catch(exception& e) {
215 m->errorOut(e, "GetRelAbundCommand", "execute");
219 //**********************************************************************************************************************
221 int GetRelAbundCommand::getRelAbundance(vector<SharedRAbundVector*> thisLookUp, ofstream& out){
226 catch(exception& e) {
227 m->errorOut(e, "GetRelAbundCommand", "getRelAbundance");
231 //**********************************************************************************************************************