]> git.donarmstrong.com Git - mothur.git/blob - getrelabundcommand.cpp
1.12.0
[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
14 GetRelAbundCommand::GetRelAbundCommand(string option) {
15         try {
16                 globaldata = GlobalData::getInstance();
17                 abort = false;
18                 allLines = 1;
19                 labels.clear();
20                 
21                 //allow user to run help
22                 if(option == "help") { help(); abort = true; }
23                 
24                 else {
25                         //valid paramters for this command
26                         string AlignArray[] =  {"groups","label","scale","outputdir","inputdir"};
27                         vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
28                         
29                         OptionParser parser(option);
30                         map<string,string> parameters = parser.getParameters();
31                         
32                         ValidParameters validParameter;
33                         
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;  }
37                         }
38                         
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"){  
41                                 outputDir = ""; 
42                                 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it  
43                         }
44                         
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; 
48                         }
49
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 = ""; }
54                         else { 
55                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
56                                 else { allLines = 1;  }
57                         }
58                         
59                         //if the user has not specified any labels use the ones from read.otu
60                         if (label == "") {  
61                                 allLines = globaldata->allLines; 
62                                 labels = globaldata->labels; 
63                         }
64                         
65                         groups = validParameter.validFile(parameters, "groups", false);                 
66                         if (groups == "not found") { groups = ""; pickedGroups = false; }
67                         else { 
68                                 pickedGroups = true;
69                                 splitAtDash(groups, Groups);
70                                 globaldata->Groups = Groups;
71                         }
72                         
73                         scale = validParameter.validFile(parameters, "scale", false);                           if (scale == "not found") { scale = "totalgroup"; }
74                         
75                         if ((scale != "totalgroup") && (scale != "totalotu") && (scale != "averagegroup") && (scale != "averageotu")) {
76                                 m->mothurOut(scale + " is not a valid scaling option for the get.relabund command. Choices are totalgroup, totalotu, averagegroup, averageotu."); m->mothurOutEndLine(); abort = true; 
77                         }
78                 }
79
80         }
81         catch(exception& e) {
82                 m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand");
83                 exit(1);
84         }
85 }
86
87 //**********************************************************************************************************************
88
89 void GetRelAbundCommand::help(){
90         try {
91                 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");
92                 m->mothurOut("The get.relabund command parameters are groups, scale and label.  No parameters are required.\n");
93                 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");
94                 m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n");
95                 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");
96                 m->mothurOut("The get.relabund command should be in the following format: get.relabund(groups=yourGroups, label=yourLabels).\n");
97                 m->mothurOut("Example get.relabund(groups=A-B-C, scale=averagegroup).\n");
98                 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
99                 m->mothurOut("The get.relabund command outputs a .relabund file.\n");
100                 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
101
102         }
103         catch(exception& e) {
104                 m->errorOut(e, "GetRelAbundCommand", "help");
105                 exit(1);
106         }
107 }
108
109 //**********************************************************************************************************************
110
111 GetRelAbundCommand::~GetRelAbundCommand(){
112 }
113
114 //**********************************************************************************************************************
115
116 int GetRelAbundCommand::execute(){
117         try {
118         
119                 if (abort == true) { return 0; }
120                 
121                 string outputFileName = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "relabund";
122                 ofstream out;
123                 openOutputFile(outputFileName, out);
124                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
125                 
126                 read = new ReadOTUFile(globaldata->inputFileName);      
127                 read->read(&*globaldata); 
128                 input = globaldata->ginput;
129                 lookup = input->getSharedRAbundVectors();
130                 string lastLabel = lookup[0]->getLabel();
131                 
132                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
133                 set<string> processedLabels;
134                 set<string> userLabels = labels;
135
136                 //as long as you are not at the end of the file or done wih the lines you want
137                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
138                         
139                         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; }
140         
141                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
142
143                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
144                                 getRelAbundance(lookup, out);
145                                 
146                                 processedLabels.insert(lookup[0]->getLabel());
147                                 userLabels.erase(lookup[0]->getLabel());
148                         }
149                         
150                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
151                                 string saveLabel = lookup[0]->getLabel();
152                         
153                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
154                                 lookup = input->getSharedRAbundVectors(lastLabel);
155                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
156                                 
157                                 getRelAbundance(lookup, out);
158                                 
159                                 processedLabels.insert(lookup[0]->getLabel());
160                                 userLabels.erase(lookup[0]->getLabel());
161                                 
162                                 //restore real lastlabel to save below
163                                 lookup[0]->setLabel(saveLabel);
164                         }
165                         
166                         lastLabel = lookup[0]->getLabel();
167                         //prevent memory leak
168                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
169                         
170                         if (m->control_pressed) {  globaldata->Groups.clear(); delete read;  out.close(); remove(outputFileName.c_str()); return 0; }
171
172                         //get next line to process
173                         lookup = input->getSharedRAbundVectors();                               
174                 }
175                 
176                 if (m->control_pressed) { globaldata->Groups.clear(); delete read;  out.close(); remove(outputFileName.c_str());  return 0; }
177
178                 //output error messages about any remaining user labels
179                 set<string>::iterator it;
180                 bool needToRun = false;
181                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
182                         m->mothurOut("Your file does not include the label " + *it); 
183                         if (processedLabels.count(lastLabel) != 1) {
184                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
185                                 needToRun = true;
186                         }else {
187                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
188                         }
189                 }
190         
191                 //run last label if you need to
192                 if (needToRun == true)  {
193                         for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }  
194                         lookup = input->getSharedRAbundVectors(lastLabel);
195                         
196                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
197                         
198                         getRelAbundance(lookup, out);
199                         
200                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
201                 }
202         
203                 //reset groups parameter
204                 globaldata->Groups.clear();  
205                 delete input; globaldata->ginput = NULL;
206                 delete read;
207                 out.close();
208                 
209                 if (m->control_pressed) { remove(outputFileName.c_str()); return 0;}
210                 
211                 m->mothurOutEndLine();
212                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
213                 m->mothurOut(outputFileName); m->mothurOutEndLine();
214                 m->mothurOutEndLine();
215                 
216                 return 0;
217         }
218         catch(exception& e) {
219                 m->errorOut(e, "GetRelAbundCommand", "execute");
220                 exit(1);
221         }
222 }
223 //**********************************************************************************************************************
224
225 int GetRelAbundCommand::getRelAbundance(vector<SharedRAbundVector*>& thisLookUp, ofstream& out){
226         try {
227                 if (pickedGroups) { eliminateZeroOTUS(thisLookUp); }
228
229                 
230                  for (int i = 0; i < thisLookUp.size(); i++) {
231                         out << thisLookUp[i]->getLabel() << '\t' << thisLookUp[i]->getGroup() << '\t' << thisLookUp[i]->getNumBins() << '\t';
232                         
233                         for (int j = 0; j < thisLookUp[i]->getNumBins(); j++) {
234                         
235                                 if (m->control_pressed) { return 0; }
236                         
237                                 int abund = thisLookUp[i]->getAbundance(j);
238                                 
239                                 float relabund = 0.0;
240                                 
241                                 if (scale == "totalgroup") { 
242                                         relabund = abund / (float) thisLookUp[i]->getNumSeqs();
243                                 }else if (scale == "totalotu") {
244                                         //calc the total in this otu
245                                         int totalOtu = 0;
246                                         for (int l = 0; l < thisLookUp.size(); l++) {  totalOtu += thisLookUp[l]->getAbundance(j); }
247                                         
248                                         relabund = abund / (float) totalOtu;
249                                 }else if (scale == "averagegroup") {
250                                         relabund = abund / (float) (thisLookUp[i]->getNumSeqs() / (float) thisLookUp[i]->getNumBins());
251                                 }else if (scale == "averageotu") {
252                                         //calc the total in this otu
253                                         int totalOtu = 0;
254                                         for (int l = 0; l < thisLookUp.size(); l++) {  totalOtu += thisLookUp[l]->getAbundance(j); }
255                                         float averageOtu = totalOtu / (float) thisLookUp.size();
256                                         
257                                         relabund = abund / (float) averageOtu;
258                                 }else{ m->mothurOut(scale + " is not a valid scaling option."); m->mothurOutEndLine(); m->control_pressed = true; return 0; }
259                                 
260                                 out << relabund << '\t';
261                         }
262                         out << endl;
263                  }
264         
265                  return 0;
266         }
267         catch(exception& e) {
268                 m->errorOut(e, "GetRelAbundCommand", "getRelAbundance");
269                 exit(1);
270         }
271 }
272 //**********************************************************************************************************************
273 int GetRelAbundCommand::eliminateZeroOTUS(vector<SharedRAbundVector*>& thislookup) {
274         try {
275                 
276                 vector<SharedRAbundVector*> newLookup;
277                 for (int i = 0; i < thislookup.size(); i++) {
278                         SharedRAbundVector* temp = new SharedRAbundVector();
279                         temp->setLabel(thislookup[i]->getLabel());
280                         temp->setGroup(thislookup[i]->getGroup());
281                         newLookup.push_back(temp);
282                 }
283                 
284                 //for each bin
285                 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
286                         if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) {  delete newLookup[j];  } return 0; }
287                 
288                         //look at each sharedRabund and make sure they are not all zero
289                         bool allZero = true;
290                         for (int j = 0; j < thislookup.size(); j++) {
291                                 if (thislookup[j]->getAbundance(i) != 0) { allZero = false;  break;  }
292                         }
293                         
294                         //if they are not all zero add this bin
295                         if (!allZero) {
296                                 for (int j = 0; j < thislookup.size(); j++) {
297                                         newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
298                                 }
299                         }
300                 }
301
302                 for (int j = 0; j < thislookup.size(); j++) {  delete thislookup[j];  }
303
304                 thislookup = newLookup;
305                 
306                 return 0;
307  
308         }
309         catch(exception& e) {
310                 m->errorOut(e, "GetRelAbundCommand", "eliminateZeroOTUS");
311                 exit(1);
312         }
313 }
314
315 //**********************************************************************************************************************
316
317