2 * otuassociationcommand.cpp
5 * Created by westcott on 1/19/12.
6 * Copyright 2012 Schloss Lab. All rights reserved.
10 #include "otuassociationcommand.h"
11 #include "linearalgebra.h"
13 //**********************************************************************************************************************
14 vector<string> OTUAssociationCommand::setParameters(){
16 CommandParameter pshared("shared", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none",false,false); parameters.push_back(pshared);
17 CommandParameter prelabund("relabund", "InputTypes", "", "", "SharedRelMeta", "SharedRelMeta", "none",false,false); parameters.push_back(prelabund);
18 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
19 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
20 CommandParameter pmethod("method", "Multiple", "pearson-spearman-kendall", "pearson", "", "", "",false,false); parameters.push_back(pmethod);
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, "OTUAssociationCommand", "setParameters");
33 //**********************************************************************************************************************
34 string OTUAssociationCommand::getHelpString(){
36 string helpString = "";
37 helpString += "The otu.association command reads a shared or relabund file and calculates the correlation coefficients between otus.\n";
38 helpString += "The otu.association command parameters are shared, relabund, groups, method and label. The shared or relabund parameter is required.\n";
39 helpString += "The groups parameter allows you to specify which of the groups you would like included. The group names are separated by dashes.\n";
40 helpString += "The label parameter allows you to select what distances level you would like used, and are also separated by dashes.\n";
41 helpString += "The method parameter allows you to select what method you would like to use. Options are pearson, spearman and kendall. Default=pearson.\n";
42 helpString += "The otu.association command should be in the following format: otu.association(shared=yourSharedFile, method=yourMethod).\n";
43 helpString += "Example otu.association(shared=genus.pool.shared, method=kendall).\n";
44 helpString += "The otu.association command outputs a .otu.corr file.\n";
45 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
49 m->errorOut(e, "OTUAssociationCommand", "getHelpString");
53 //**********************************************************************************************************************
54 OTUAssociationCommand::OTUAssociationCommand(){
56 abort = true; calledHelp = true;
58 vector<string> tempOutNames;
59 outputTypes["otu.corr"] = tempOutNames;
62 m->errorOut(e, "OTUAssociationCommand", "OTUAssociationCommand");
66 //**********************************************************************************************************************
67 OTUAssociationCommand::OTUAssociationCommand(string option) {
69 abort = false; calledHelp = false;
72 //allow user to run help
73 if(option == "help") { help(); abort = true; calledHelp = true; }
74 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
77 vector<string> myArray = setParameters();
79 OptionParser parser(option);
80 map<string, string> parameters = parser.getParameters();
82 ValidParameters validParameter;
83 map<string, string>::iterator it;
85 //check to make sure all parameters are valid for command
86 for (it = parameters.begin(); it != parameters.end(); it++) {
87 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
90 vector<string> tempOutNames;
91 outputTypes["otu.corr"] = tempOutNames;
93 //if the user changes the input directory command factory will send this info to us in the output parameter
94 string inputDir = validParameter.validFile(parameters, "inputdir", false);
95 if (inputDir == "not found"){ inputDir = ""; }
98 it = parameters.find("shared");
99 //user has given a template file
100 if(it != parameters.end()){
101 path = m->hasPath(it->second);
102 //if the user has not given a path then, add inputdir. else leave path alone.
103 if (path == "") { parameters["shared"] = inputDir + it->second; }
106 it = parameters.find("relabund");
107 //user has given a template file
108 if(it != parameters.end()){
109 path = m->hasPath(it->second);
110 //if the user has not given a path then, add inputdir. else leave path alone.
111 if (path == "") { parameters["relabund"] = inputDir + it->second; }
116 //check for required parameters
117 sharedfile = validParameter.validFile(parameters, "shared", true);
118 if (sharedfile == "not open") { abort = true; }
119 else if (sharedfile == "not found") { sharedfile = ""; }
120 else { inputFileName = sharedfile; m->setSharedFile(sharedfile); }
122 relabundfile = validParameter.validFile(parameters, "relabund", true);
123 if (relabundfile == "not open") { abort = true; }
124 else if (relabundfile == "not found") { relabundfile = ""; }
125 else { inputFileName = relabundfile; m->setRelAbundFile(relabundfile); }
127 groups = validParameter.validFile(parameters, "groups", false);
128 if (groups == "not found") { groups = ""; pickedGroups = false; }
131 m->splitAtDash(groups, Groups);
133 m->setGroups(Groups);
135 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFileName); }
137 label = validParameter.validFile(parameters, "label", false);
138 if (label == "not found") { label = ""; }
140 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
141 else { allLines = 1; }
144 if ((relabundfile == "") && (sharedfile == "")) {
145 //is there are current file available for any of these?
146 //give priority to shared, then relabund
147 //if there is a current shared file, use it
148 sharedfile = m->getSharedFile();
149 if (sharedfile != "") { inputFileName = sharedfile; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
151 relabundfile = m->getRelAbundFile();
152 if (relabundfile != "") { inputFileName = relabundfile; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
154 m->mothurOut("You must provide either a shared or relabund file."); m->mothurOutEndLine(); abort = true;
160 if ((relabundfile != "") && (sharedfile != "")) { m->mothurOut("You may only use one of the following : shared or relabund file."); m->mothurOutEndLine(); abort = true; }
162 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "pearson"; }
164 if ((method != "pearson") && (method != "spearman") && (method != "kendall")) { m->mothurOut(method + " is not a valid method. Valid methods are pearson, spearman, and kendall."); m->mothurOutEndLine(); abort = true; }
168 catch(exception& e) {
169 m->errorOut(e, "OTUAssociationCommand", "OTUAssociationCommand");
173 //**********************************************************************************************************************
175 int OTUAssociationCommand::execute(){
178 if (abort == true) { if (calledHelp) { return 0; } return 2; }
180 //function are identical just different datatypes
181 if (sharedfile != "") { processShared(); }
182 else if (relabundfile != "") { processRelabund(); }
184 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
186 m->mothurOutEndLine();
187 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
188 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
189 m->mothurOutEndLine();
193 catch(exception& e) {
194 m->errorOut(e, "OTUAssociationCommand", "execute");
198 //**********************************************************************************************************************
199 int OTUAssociationCommand::processShared(){
201 InputData* input = new InputData(sharedfile, "sharedfile");
202 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
203 string lastLabel = lookup[0]->getLabel();
205 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
206 set<string> processedLabels;
207 set<string> userLabels = labels;
209 //as long as you are not at the end of the file or done wih the lines you want
210 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
212 if (m->control_pressed) { delete input; return 0; }
214 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
215 processedLabels.insert(lookup[0]->getLabel());
216 userLabels.erase(lookup[0]->getLabel());
218 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
222 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
223 string saveLabel = lookup[0]->getLabel();
225 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
226 lookup = input->getSharedRAbundVectors(lastLabel);
228 processedLabels.insert(lookup[0]->getLabel());
229 userLabels.erase(lookup[0]->getLabel());
231 //restore real lastlabel to save below
232 lookup[0]->setLabel(saveLabel);
234 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
238 lastLabel = lookup[0]->getLabel();
240 //get next line to process
241 //prevent memory leak
242 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
243 lookup = input->getSharedRAbundVectors();
247 if (m->control_pressed) { delete input; return 0; }
249 //output error messages about any remaining user labels
250 set<string>::iterator it;
251 bool needToRun = false;
252 for (it = userLabels.begin(); it != userLabels.end(); it++) {
253 m->mothurOut("Your file does not include the label " + *it);
254 if (processedLabels.count(lastLabel) != 1) {
255 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
258 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
262 //run last label if you need to
263 if (needToRun == true) {
264 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
265 lookup = input->getSharedRAbundVectors(lastLabel);
267 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
275 catch(exception& e) {
276 m->errorOut(e, "OTUAssociationCommand", "processShared");
280 //**********************************************************************************************************************
281 int OTUAssociationCommand::process(vector<SharedRAbundVector*>& lookup){
284 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + "." + method + ".otu.corr";
285 outputNames.push_back(outputFileName); outputTypes["shared"].push_back(outputFileName);
288 m->openOutputFile(outputFileName, out);
289 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
292 out << "OTUA\tOTUB\t" << method << "Coef\tSignificance\n";
294 vector< vector<double> > xy; xy.resize(lookup[0]->getNumBins());
295 for (int i = 0; i < lookup[0]->getNumBins(); i++) { for (int j = 0; j < lookup.size(); j++) { xy[i].push_back(lookup[j]->getAbundance(i)); } }
297 LinearAlgebra linear;
298 for (int i = 0; i < xy.size(); i++) {
300 for (int k = 0; k < i; k++) {
302 if (m->control_pressed) { out.close(); return 0; }
306 if (method == "spearman") { coef = linear.calcSpearman(xy[i], xy[k], sig); }
307 else if (method == "pearson") { coef = linear.calcPearson(xy[i], xy[k], sig); }
308 else if (method == "kendall") { coef = linear.calcKendall(xy[i], xy[k], sig); }
309 else { m->mothurOut("[ERROR]: invalid method, choices are spearman, pearson or kendall."); m->mothurOutEndLine(); m->control_pressed = true; }
311 out << m->binLabelsInFile[i] << '\t' << m->binLabelsInFile[k] << '\t' << coef << '\t' << sig << endl;
321 catch(exception& e) {
322 m->errorOut(e, "OTUAssociationCommand", "process");
326 //**********************************************************************************************************************
327 int OTUAssociationCommand::processRelabund(){
329 InputData* input = new InputData(relabundfile, "relabund");
330 vector<SharedRAbundFloatVector*> lookup = input->getSharedRAbundFloatVectors();
331 string lastLabel = lookup[0]->getLabel();
333 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
334 set<string> processedLabels;
335 set<string> userLabels = labels;
337 //as long as you are not at the end of the file or done wih the lines you want
338 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
340 if (m->control_pressed) { delete input; return 0; }
342 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
343 processedLabels.insert(lookup[0]->getLabel());
344 userLabels.erase(lookup[0]->getLabel());
346 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
350 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
351 string saveLabel = lookup[0]->getLabel();
353 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
354 lookup = input->getSharedRAbundFloatVectors(lastLabel);
356 processedLabels.insert(lookup[0]->getLabel());
357 userLabels.erase(lookup[0]->getLabel());
359 //restore real lastlabel to save below
360 lookup[0]->setLabel(saveLabel);
362 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
366 lastLabel = lookup[0]->getLabel();
368 //get next line to process
369 //prevent memory leak
370 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
371 lookup = input->getSharedRAbundFloatVectors();
375 if (m->control_pressed) { delete input; return 0; }
377 //output error messages about any remaining user labels
378 set<string>::iterator it;
379 bool needToRun = false;
380 for (it = userLabels.begin(); it != userLabels.end(); it++) {
381 m->mothurOut("Your file does not include the label " + *it);
382 if (processedLabels.count(lastLabel) != 1) {
383 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
386 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
390 //run last label if you need to
391 if (needToRun == true) {
392 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
393 lookup = input->getSharedRAbundFloatVectors(lastLabel);
395 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
403 catch(exception& e) {
404 m->errorOut(e, "OTUAssociationCommand", "processRelabund");
408 //**********************************************************************************************************************
409 int OTUAssociationCommand::process(vector<SharedRAbundFloatVector*>& lookup){
412 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + lookup[0]->getLabel() + "." + method + ".otu.corr";
413 outputNames.push_back(outputFileName); outputTypes["shared"].push_back(outputFileName);
416 m->openOutputFile(outputFileName, out);
417 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
420 out << "OTUA\tOTUB\t" << method << "Coef\tSignificance\n";
422 vector< vector<double> > xy; xy.resize(lookup[0]->getNumBins());
423 for (int i = 0; i < lookup[0]->getNumBins(); i++) { for (int j = 0; j < lookup.size(); j++) { xy[i].push_back(lookup[j]->getAbundance(i)); } }
425 LinearAlgebra linear;
426 for (int i = 0; i < xy.size(); i++) {
428 for (int k = 0; k < i; k++) {
430 if (m->control_pressed) { out.close(); return 0; }
434 if (method == "spearman") { coef = linear.calcSpearman(xy[i], xy[k], sig); }
435 else if (method == "pearson") { coef = linear.calcPearson(xy[i], xy[k], sig); }
436 else if (method == "kendall") { coef = linear.calcKendall(xy[i], xy[k], sig); }
437 else { m->mothurOut("[ERROR]: invalid method, choices are spearman, pearson or kendall."); m->mothurOutEndLine(); m->control_pressed = true; }
439 out << m->binLabelsInFile[i] << '\t' << m->binLabelsInFile[k] << '\t' << coef << '\t' << sig << endl;
448 catch(exception& e) {
449 m->errorOut(e, "OTUAssociationCommand", "process");
453 /*****************************************************************/