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