]> git.donarmstrong.com Git - mothur.git/blob - getcoremicrobiomcommand.cpp
added abundance and sample parameters to get.coremicrobiome command.
[mothur.git] / getcoremicrobiomcommand.cpp
1 //
2 //  getcoremicrobiomcommand.cpp
3 //  Mothur
4 //
5 //  Created by John Westcott on 5/8/12.
6 //  Copyright (c) 2012 Schloss Lab. All rights reserved.
7 //
8
9 #include "getcoremicrobiomcommand.h"
10
11
12 //**********************************************************************************************************************
13 vector<string> GetCoreMicroBiomCommand::setParameters(){        
14         try {
15         CommandParameter pshared("shared", "InputTypes", "", "", "SharedRel", "SharedRel", "none",false,false); parameters.push_back(pshared);
16                 CommandParameter prelabund("relabund", "InputTypes", "", "", "SharedRel", "SharedRel", "none",false,false); parameters.push_back(prelabund);
17         CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
18                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
19                 
20         //every command must have inputdir and outputdir.  This allows mothur users to redirect input and output files.
21                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
23                 
24                 vector<string> myArray;
25                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
26                 return myArray;
27         }
28         catch(exception& e) {
29                 m->errorOut(e, "GetCoreMicroBiomCommand", "setParameters");
30                 exit(1);
31         }
32 }
33 //**********************************************************************************************************************
34 string GetCoreMicroBiomCommand::getHelpString(){        
35         try {
36                 string helpString = "";
37                 helpString += "The new command allows you to ....\n";
38                 helpString += "The new command parameters are: ....\n";
39                 helpString += "The whatever parameter is used to ....\n";
40                 helpString += "The new command should be in the following format: \n";
41                 helpString += "new(...)\n";
42                 return helpString;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "GetCoreMicroBiomCommand", "getHelpString");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 GetCoreMicroBiomCommand::GetCoreMicroBiomCommand(){     
51         try {
52                 abort = true; calledHelp = true;
53                 setParameters();
54         vector<string> tempOutNames;
55                 outputTypes["coremicrobiom"] = tempOutNames; 
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "GetCoreMicroBiomCommand", "GetCoreMicroBiomCommand");
59                 exit(1);
60         }
61 }
62 //**********************************************************************************************************************
63 GetCoreMicroBiomCommand::GetCoreMicroBiomCommand(string option)  {
64         try {
65                 abort = false; calledHelp = false;   allLines = 1;
66                 
67                 //allow user to run help
68                 if(option == "help") { help(); abort = true; calledHelp = true; }
69                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
70                 
71                 else {
72                         //valid paramters for this command
73                         vector<string> myArray = setParameters();
74                         
75                         OptionParser parser(option);
76                         map<string,string> parameters = parser.getParameters();
77                         
78                         ValidParameters validParameter;
79                         map<string,string>::iterator it;
80                         //check to make sure all parameters are valid for command
81                         for (it = parameters.begin(); it != parameters.end(); it++) { 
82                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
83                         }
84                         
85                         
86                         //if the user changes the input directory command factory will send this info to us in the output parameter 
87                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
88                         if (inputDir == "not found"){   inputDir = "";          }
89                         else {
90                 
91                                 string path;
92                                 it = parameters.find("relabund");
93                                 //user has given a template file
94                                 if(it != parameters.end()){ 
95                                         path = m->hasPath(it->second);
96                                         //if the user has not given a path then, add inputdir. else leave path alone.
97                                         if (path == "") {       parameters["relabund"] = inputDir + it->second;         }
98                                 }
99                 
100                 it = parameters.find("shared");
101                                 //user has given a template file
102                                 if(it != parameters.end()){ 
103                                         path = m->hasPath(it->second);
104                                         //if the user has not given a path then, add inputdir. else leave path alone.
105                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
106                                 }
107             }
108            
109         
110                         //check for parameters
111             sharedfile = validParameter.validFile(parameters, "shared", true);
112                         if (sharedfile == "not open") { abort = true; }
113                         else if (sharedfile == "not found") { sharedfile = ""; }
114                         else { inputFileName = sharedfile; format = "sharedfile"; m->setSharedFile(sharedfile); }
115                         
116                         relabundfile = validParameter.validFile(parameters, "relabund", true);
117                         if (relabundfile == "not open") { abort = true; }
118                         else if (relabundfile == "not found") { relabundfile = ""; }
119                         else { inputFileName = relabundfile; format = "relabund"; m->setRelAbundFile(relabundfile); }
120             
121             if ((relabundfile == "") && (sharedfile == "")) { 
122                                 //is there are current file available for either of these?
123                                 //give priority to shared, then relabund
124                                 sharedfile = m->getSharedFile(); 
125                                 if (sharedfile != "") {  inputFileName = sharedfile; format="sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
126                                 else { 
127                                         relabundfile = m->getRelAbundFile(); 
128                                         if (relabundfile != "") {  inputFileName = relabundfile; format="relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
129                                         else { 
130                                                 m->mothurOut("No valid current files. You must provide a shared or relabund."); m->mothurOutEndLine(); 
131                                                 abort = true;
132                                         }
133                                 }
134                         }
135             
136             //if the user changes the output directory command factory will send this info to us in the output parameter 
137                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
138                                 outputDir = m->hasPath(inputFileName); //if user entered a file with a path then preserve it    
139                         }
140             
141             string groups = validParameter.validFile(parameters, "groups", false);                      
142                         if (groups == "not found") { groups = ""; }
143                         else { m->splitAtDash(groups, Groups); }
144                         m->setGroups(Groups);
145             
146             string label = validParameter.validFile(parameters, "label", false);                        
147                         if (label == "not found") { label = ""; }
148                         else { 
149                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
150                                 else { allLines = 1;  }
151                         }
152                 }
153                 
154         }
155         catch(exception& e) {
156                 m->errorOut(e, "GetCoreMicroBiomCommand", "GetCoreMicroBiomCommand");
157                 exit(1);
158         }
159 }
160 //**********************************************************************************************************************
161
162 int GetCoreMicroBiomCommand::execute(){
163         try {
164                 
165                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
166         
167         InputData input(inputFileName, format);
168         vector<SharedRAbundFloatVector*> lookup = input.getSharedRAbundFloatVectors();
169         string lastLabel = lookup[0]->getLabel();
170         
171         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
172         set<string> processedLabels;
173         set<string> userLabels = labels;
174         
175         //as long as you are not at the end of the file or done wih the lines you want
176         while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
177             
178             if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
179             
180             if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                      
181                 
182                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
183                 
184                 createTable(lookup);
185                 
186                 processedLabels.insert(lookup[0]->getLabel());
187                 userLabels.erase(lookup[0]->getLabel());
188             }
189             
190             if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
191                 string saveLabel = lookup[0]->getLabel();
192                 
193                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  
194                 lookup = input.getSharedRAbundFloatVectors(lastLabel);
195                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
196                 
197                 createTable(lookup);
198                 
199                 processedLabels.insert(lookup[0]->getLabel());
200                 userLabels.erase(lookup[0]->getLabel());
201                 
202                 //restore real lastlabel to save below
203                 lookup[0]->setLabel(saveLabel);
204             }
205             
206             lastLabel = lookup[0]->getLabel();
207             //prevent memory leak
208             for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
209             
210             if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
211             
212             //get next line to process
213             lookup = input.getSharedRAbundFloatVectors();                               
214         }
215         
216         if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
217         
218         //output error messages about any remaining user labels
219         set<string>::iterator it;
220         bool needToRun = false;
221         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
222             m->mothurOut("Your file does not include the label " + *it); 
223             if (processedLabels.count(lastLabel) != 1) {
224                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
225                 needToRun = true;
226             }else {
227                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
228             }
229         }
230         
231         //run last label if you need to
232         if (needToRun == true)  {
233             for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }  
234             lookup = input.getSharedRAbundFloatVectors(lastLabel);
235             
236             m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
237             
238             createTable(lookup);
239             
240             for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
241         }
242         
243         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }  return 0; }
244         
245         //output files created by command
246                 m->mothurOutEndLine();
247                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
248                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
249                 m->mothurOutEndLine();
250         return 0;
251                 
252     }
253         catch(exception& e) {
254                 m->errorOut(e, "GetCoreMicroBiomCommand", "execute");
255                 exit(1);
256         }
257 }
258 //**********************************************************************************************************************
259
260 int GetCoreMicroBiomCommand::createTable(vector<SharedRAbundFloatVector*>& lookup){
261         try {
262         
263         string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + ".core.microbiom";
264         outputNames.push_back(outputFileName);  outputTypes["coremicrobiom"].push_back(outputFileName);
265                 ofstream out;
266                 m->openOutputFile(outputFileName, out);
267         
268         int numSamples = lookup.size();
269         int numOtus = lookup[0]->getNumBins();
270         
271         //table is 100 by numsamples
272         //question we are answering is: what fraction of OTUs in a study have a relative abundance at or above %X
273         //in at least %Y samples. x goes from 0 to 100, y from 1 to numSamples
274         vector< vector<double> > table; table.resize(101);
275         for (int i = 0; i < table.size(); i++) { table[i].resize(numSamples, 0.0); }
276         
277         for (int i = 0; i < numOtus; i++) {
278             
279             if (m->control_pressed) { break; }
280             
281             //count number of samples in this otu with a relabund >= spot in count
282             vector<int> counts; counts.resize(101, 0);
283             
284             for (int j = 0; j < lookup.size(); j++) {
285                 double relabund = lookup[j]->getAbundance(i);
286                 int wholeRelabund = (int) (floor(relabund*100));
287                 for (int k = 0; k < wholeRelabund+1; k++) { counts[k]++; }
288             }
289             
290             //add this otus info to table
291             for (int j = 0; j < table.size(); j++) {
292                 for (int k = 0; k < counts[j]; k++) { table[j][k]++; }
293             }
294         }
295                 
296         //format output
297                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
298         out << "NumSamples\t";
299         
300         //convert table counts to percents
301         for (int i = 0; i < table.size(); i++) {
302             out << "Relabund-" << i << "%\t";
303             if (m->control_pressed) { break; }
304             for (int j = 0; j < table[i].size(); j++) {  table[i][j] /= (double) numOtus; }
305         }
306         out << endl;
307         
308         for (int i = 0; i < numSamples; i++) {
309             if (m->control_pressed) { break; }
310             out << i+1 << '\t';
311             for (int j = 0; j < table.size(); j++) {  out << table[j][i] << '\t'; }
312             out << endl;
313         }
314
315         out.close();
316         
317         return 0;
318     }
319         catch(exception& e) {
320                 m->errorOut(e, "GetCoreMicroBiomCommand", "createTable");
321                 exit(1);
322         }
323 }
324
325 //**********************************************************************************************************************
326
327