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