2 // getcoremicrobiomcommand.cpp
5 // Created by John Westcott on 5/8/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "getcoremicrobiomcommand.h"
12 //**********************************************************************************************************************
13 vector<string> GetCoreMicroBiomCommand::setParameters(){
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);
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);
24 vector<string> myArray;
25 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
29 m->errorOut(e, "GetCoreMicroBiomCommand", "setParameters");
33 //**********************************************************************************************************************
34 string GetCoreMicroBiomCommand::getHelpString(){
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";
45 m->errorOut(e, "GetCoreMicroBiomCommand", "getHelpString");
49 //**********************************************************************************************************************
50 GetCoreMicroBiomCommand::GetCoreMicroBiomCommand(){
52 abort = true; calledHelp = true;
54 vector<string> tempOutNames;
55 outputTypes["coremicrobiom"] = tempOutNames;
58 m->errorOut(e, "GetCoreMicroBiomCommand", "GetCoreMicroBiomCommand");
62 //**********************************************************************************************************************
63 GetCoreMicroBiomCommand::GetCoreMicroBiomCommand(string option) {
65 abort = false; calledHelp = false; allLines = 1;
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;}
72 //valid paramters for this command
73 vector<string> myArray = setParameters();
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
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; }
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 = ""; }
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; }
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; }
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); }
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); }
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(); }
127 relabundfile = m->getRelAbundFile();
128 if (relabundfile != "") { inputFileName = relabundfile; format="relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
130 m->mothurOut("No valid current files. You must provide a shared or relabund."); m->mothurOutEndLine();
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
141 string groups = validParameter.validFile(parameters, "groups", false);
142 if (groups == "not found") { groups = ""; }
143 else { m->splitAtDash(groups, Groups); }
144 m->setGroups(Groups);
146 string label = validParameter.validFile(parameters, "label", false);
147 if (label == "not found") { label = ""; }
149 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
150 else { allLines = 1; }
155 catch(exception& e) {
156 m->errorOut(e, "GetCoreMicroBiomCommand", "GetCoreMicroBiomCommand");
160 //**********************************************************************************************************************
162 int GetCoreMicroBiomCommand::execute(){
165 if (abort == true) { if (calledHelp) { return 0; } return 2; }
167 InputData input(inputFileName, format);
168 vector<SharedRAbundFloatVector*> lookup = input.getSharedRAbundFloatVectors();
169 string lastLabel = lookup[0]->getLabel();
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;
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))) {
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; }
180 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
182 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
186 processedLabels.insert(lookup[0]->getLabel());
187 userLabels.erase(lookup[0]->getLabel());
190 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
191 string saveLabel = lookup[0]->getLabel();
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();
199 processedLabels.insert(lookup[0]->getLabel());
200 userLabels.erase(lookup[0]->getLabel());
202 //restore real lastlabel to save below
203 lookup[0]->setLabel(saveLabel);
206 lastLabel = lookup[0]->getLabel();
207 //prevent memory leak
208 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
210 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
212 //get next line to process
213 lookup = input.getSharedRAbundFloatVectors();
216 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
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();
227 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
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);
236 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
240 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
243 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
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();
253 catch(exception& e) {
254 m->errorOut(e, "GetCoreMicroBiomCommand", "execute");
258 //**********************************************************************************************************************
260 int GetCoreMicroBiomCommand::createTable(vector<SharedRAbundFloatVector*>& lookup){
263 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + ".core.microbiom";
264 outputNames.push_back(outputFileName); outputTypes["coremicrobiom"].push_back(outputFileName);
266 m->openOutputFile(outputFileName, out);
268 int numSamples = lookup.size();
269 int numOtus = lookup[0]->getNumBins();
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); }
277 for (int i = 0; i < numOtus; i++) {
279 if (m->control_pressed) { break; }
281 //count number of samples in this otu with a relabund >= spot in count
282 vector<int> counts; counts.resize(101, 0);
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]++; }
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]++; }
297 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
298 out << "NumSamples\t";
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; }
308 for (int i = 0; i < numSamples; i++) {
309 if (m->control_pressed) { break; }
311 for (int j = 0; j < table.size(); j++) { out << table[j][i] << '\t'; }
319 catch(exception& e) {
320 m->errorOut(e, "GetCoreMicroBiomCommand", "createTable");
325 //**********************************************************************************************************************