]> git.donarmstrong.com Git - mothur.git/blob - getrelabundcommand.cpp
added getCommandInfoCommand for gui
[mothur.git] / getrelabundcommand.cpp
1 /*
2  *  getrelabundcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 6/21/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "getrelabundcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> GetRelAbundCommand::setParameters(){     
14         try {
15                 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,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);
21                 
22                 vector<string> myArray;
23                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
24                 return myArray;
25         }
26         catch(exception& e) {
27                 m->errorOut(e, "GetRelAbundCommand", "setParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 string GetRelAbundCommand::getHelpString(){     
33         try {
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";
44                 return helpString;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "GetRelAbundCommand", "getHelpString");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 GetRelAbundCommand::GetRelAbundCommand(){       
53         try {
54                 abort = true; calledHelp = true; 
55                 setParameters();
56                 vector<string> tempOutNames;
57                 outputTypes["relabund"] = tempOutNames;
58         }
59         catch(exception& e) {
60                 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
61                 exit(1);
62         }
63 }
64 //**********************************************************************************************************************
65 GetRelAbundCommand::GetRelAbundCommand(string option) {
66         try {
67                 abort = false; calledHelp = false;   
68                 allLines = 1;
69                                 
70                 //allow user to run help
71                 if(option == "help") { help(); abort = true; calledHelp = true; }
72                 
73                 else {
74                         vector<string> myArray = setParameters();
75                         
76                         OptionParser parser(option);
77                         map<string,string> parameters = parser.getParameters();
78                         map<string,string>::iterator it;
79                         
80                         ValidParameters validParameter;
81                         
82                         //check to make sure all parameters are valid for command
83                         for (it = parameters.begin(); it != parameters.end(); it++) { 
84                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
85                         }
86                         
87                         //initialize outputTypes
88                         vector<string> tempOutNames;
89                         outputTypes["relabund"] = tempOutNames;
90                         
91                         //if the user changes the input directory command factory will send this info to us in the output parameter 
92                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
93                         if (inputDir == "not found"){   inputDir = "";          }
94                         else {
95                                 string path;
96                                 it = parameters.find("shared");
97                                 //user has given a template file
98                                 if(it != parameters.end()){ 
99                                         path = m->hasPath(it->second);
100                                         //if the user has not given a path then, add inputdir. else leave path alone.
101                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
102                                 }
103                         }
104                 
105                         //get shared file
106                         sharedfile = validParameter.validFile(parameters, "shared", true);
107                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
108                         else if (sharedfile == "not found") { 
109                                 //if there is a current shared file, use it
110                                 sharedfile = m->getSharedFile(); 
111                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
112                                 else {  m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
113                         }
114                         
115                         
116                         //if the user changes the output directory command factory will send this info to us in the output parameter 
117                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(sharedfile);             }
118
119                         //check for optional parameter and set defaults
120                         // ...at some point should added some additional type checking...
121                         label = validParameter.validFile(parameters, "label", false);                   
122                         if (label == "not found") { label = ""; }
123                         else { 
124                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
125                                 else { allLines = 1;  }
126                         }
127                         
128                         groups = validParameter.validFile(parameters, "groups", false);                 
129                         if (groups == "not found") { groups = ""; pickedGroups = false; }
130                         else { 
131                                 pickedGroups = true;
132                                 m->splitAtDash(groups, Groups);
133                                 m->Groups = Groups;
134                         }
135                         
136                         scale = validParameter.validFile(parameters, "scale", false);                           if (scale == "not found") { scale = "totalgroup"; }
137                         
138                         if ((scale != "totalgroup") && (scale != "totalotu") && (scale != "averagegroup") && (scale != "averageotu")) {
139                                 m->mothurOut(scale + " is not a valid scaling option for the get.relabund command. Choices are totalgroup, totalotu, averagegroup, averageotu."); m->mothurOutEndLine(); abort = true; 
140                         }
141                 }
142
143         }
144         catch(exception& e) {
145                 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
146                 exit(1);
147         }
148 }
149 //**********************************************************************************************************************
150
151 int GetRelAbundCommand::execute(){
152         try {
153         
154                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
155                 
156                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "relabund";
157                 ofstream out;
158                 m->openOutputFile(outputFileName, out);
159                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
160                 
161                 input = new InputData(sharedfile, "sharedfile");
162                 lookup = input->getSharedRAbundVectors();
163                 string lastLabel = lookup[0]->getLabel();
164                 
165                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
166                 set<string> processedLabels;
167                 set<string> userLabels = labels;
168
169                 //as long as you are not at the end of the file or done wih the lines you want
170                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
171                         
172                         if (m->control_pressed) {  outputTypes.clear();  for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } m->Groups.clear(); delete input;  out.close(); remove(outputFileName.c_str()); return 0; }
173         
174                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
175
176                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
177                                 getRelAbundance(lookup, out);
178                                 
179                                 processedLabels.insert(lookup[0]->getLabel());
180                                 userLabels.erase(lookup[0]->getLabel());
181                         }
182                         
183                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
184                                 string saveLabel = lookup[0]->getLabel();
185                         
186                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
187                                 lookup = input->getSharedRAbundVectors(lastLabel);
188                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
189                                 
190                                 getRelAbundance(lookup, out);
191                                 
192                                 processedLabels.insert(lookup[0]->getLabel());
193                                 userLabels.erase(lookup[0]->getLabel());
194                                 
195                                 //restore real lastlabel to save below
196                                 lookup[0]->setLabel(saveLabel);
197                         }
198                         
199                         lastLabel = lookup[0]->getLabel();
200                         //prevent memory leak
201                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
202                         
203                         if (m->control_pressed) {  outputTypes.clear();  m->Groups.clear(); delete input;  out.close(); remove(outputFileName.c_str()); return 0; }
204
205                         //get next line to process
206                         lookup = input->getSharedRAbundVectors();                               
207                 }
208                 
209                 if (m->control_pressed) { outputTypes.clear(); m->Groups.clear(); delete input;  out.close(); remove(outputFileName.c_str());  return 0; }
210
211                 //output error messages about any remaining user labels
212                 set<string>::iterator it;
213                 bool needToRun = false;
214                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
215                         m->mothurOut("Your file does not include the label " + *it); 
216                         if (processedLabels.count(lastLabel) != 1) {
217                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
218                                 needToRun = true;
219                         }else {
220                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
221                         }
222                 }
223         
224                 //run last label if you need to
225                 if (needToRun == true)  {
226                         for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }  
227                         lookup = input->getSharedRAbundVectors(lastLabel);
228                         
229                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
230                         
231                         getRelAbundance(lookup, out);
232                         
233                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
234                 }
235         
236                 //reset groups parameter
237                 m->Groups.clear();  
238                 delete input; 
239                 out.close();
240                 
241                 if (m->control_pressed) { outputTypes.clear(); remove(outputFileName.c_str()); return 0;}
242                 
243                 m->mothurOutEndLine();
244                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
245                 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["relabund"].push_back(outputFileName);
246                 m->mothurOutEndLine();
247                 
248                 //set relabund file as new current relabundfile
249                 string current = "";
250                 itTypes = outputTypes.find("relabund");
251                 if (itTypes != outputTypes.end()) {
252                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setRelAbundFile(current); }
253                 }
254                 
255                 return 0;
256         }
257         catch(exception& e) {
258                 m->errorOut(e, "GetRelAbundCommand", "execute");
259                 exit(1);
260         }
261 }
262 //**********************************************************************************************************************
263
264 int GetRelAbundCommand::getRelAbundance(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
265         try {
266                 
267                  for (int i = 0; i < thisLookUp.size(); i++) {
268                         out << thisLookUp[i]->getLabel() << '\t' << thisLookUp[i]->getGroup() << '\t' << thisLookUp[i]->getNumBins() << '\t';
269                         
270                         for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) {
271                         
272                                 if (m->control_pressed) { return 0; }
273                         
274                                 int abund = thisLookUp[i]->getAbundance(j);
275                                 
276                                 float relabund = 0.0;
277                                 
278                                 if (scale == "totalgroup") { 
279                                         relabund = abund / (float) thisLookUp[i]->getNumSeqs();
280                                 }else if (scale == "totalotu") {
281                                         //calc the total in this otu
282                                         int totalOtu = 0;
283                                         for (int l = 0; l < thisLookUp.size(); l++) {  totalOtu += thisLookUp[l]->getAbundance(j); }
284                                         relabund = abund / (float) totalOtu;
285                                 }else if (scale == "averagegroup") {
286                                         relabund = abund / (float) (thisLookUp[i]->getNumSeqs() / (float) thisLookUp[i]->getNumBins());
287                                 }else if (scale == "averageotu") {
288                                         //calc the total in this otu
289                                         int totalOtu = 0;
290                                         for (int l = 0; l < thisLookUp.size(); l++) {  totalOtu += thisLookUp[l]->getAbundance(j); }
291                                         float averageOtu = totalOtu / (float) thisLookUp.size();
292                                         
293                                         relabund = abund / (float) averageOtu;
294                                 }else{ m->mothurOut(scale + " is not a valid scaling option."); m->mothurOutEndLine(); m->control_pressed = true; return 0; }
295                                 
296                                 out << relabund << '\t';
297                         }
298                         out << endl;
299                  }
300         
301                  return 0;
302         }
303         catch(exception& e) {
304                 m->errorOut(e, "GetRelAbundCommand", "getRelAbundance");
305                 exit(1);
306         }
307 }
308 //**********************************************************************************************************************
309
310