5 * Created by westcott on 12/22/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "corraxescommand.h"
12 //**********************************************************************************************************************
13 vector<string> CorrAxesCommand::getValidParameters(){
15 string Array[] = {"axes","shared","relabund","numaxes","label","groups","method","metastats","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "CorrAxesCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 vector<string> CorrAxesCommand::getRequiredParameters(){
27 string Array[] = {"axes"};
28 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
32 m->errorOut(e, "CorrAxesCommand", "getRequiredParameters");
36 //**********************************************************************************************************************
37 CorrAxesCommand::CorrAxesCommand(){
40 //initialize outputTypes
41 vector<string> tempOutNames;
42 outputTypes["corr.axes"] = tempOutNames;
45 m->errorOut(e, "CorrAxesCommand", "CorrAxesCommand");
50 //**********************************************************************************************************************
51 vector<string> CorrAxesCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "CorrAxesCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
62 CorrAxesCommand::CorrAxesCommand(string option) {
65 globaldata = GlobalData::getInstance();
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
71 //valid paramters for this command
72 string Array[] = {"axes","shared","relabund","numaxes","label","groups","method","metastats","outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string, string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string, string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 vector<string> tempOutNames;
87 outputTypes["corr.axes"] = tempOutNames;
89 //if the user changes the input directory command factory will send this info to us in the output parameter
90 string inputDir = validParameter.validFile(parameters, "inputdir", false);
91 if (inputDir == "not found"){ inputDir = ""; }
94 it = parameters.find("axes");
95 //user has given a template file
96 if(it != parameters.end()){
97 path = m->hasPath(it->second);
98 //if the user has not given a path then, add inputdir. else leave path alone.
99 if (path == "") { parameters["axes"] = inputDir + it->second; }
102 it = parameters.find("shared");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["shared"] = inputDir + it->second; }
110 it = parameters.find("relabund");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["relabund"] = inputDir + it->second; }
120 //check for required parameters
121 axesfile = validParameter.validFile(parameters, "axes", true);
122 if (axesfile == "not open") { abort = true; }
123 else if (axesfile == "not found") { axesfile = ""; m->mothurOut("axes is a required parameter for the corr.axes command."); m->mothurOutEndLine(); abort = true; }
125 sharedfile = validParameter.validFile(parameters, "shared", true);
126 if (sharedfile == "not open") { abort = true; }
127 else if (sharedfile == "not found") { sharedfile = ""; }
128 else { inputFileName = sharedfile; }
130 relabundfile = validParameter.validFile(parameters, "relabund", true);
131 if (relabundfile == "not open") { abort = true; }
132 else if (relabundfile == "not found") { relabundfile = ""; }
133 else { inputFileName = relabundfile; }
135 groups = validParameter.validFile(parameters, "groups", false);
136 if (groups == "not found") { groups = ""; pickedGroups = false; }
139 m->splitAtDash(groups, Groups);
141 globaldata->Groups = Groups;
143 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputFileName); }
145 label = validParameter.validFile(parameters, "label", false);
146 if (label == "not found") { label = ""; m->mothurOut("You did not provide a label, I will use the first label in your inputfile."); m->mothurOutEndLine(); label=""; }
148 if ((relabundfile == "") && (sharedfile == "")) { m->mothurOut("You must provide either a shared or relabund file."); m->mothurOutEndLine(); abort = true; }
150 if ((relabundfile != "") && (sharedfile != "")) { m->mothurOut("You may not use both a shared and relabund file."); m->mothurOutEndLine(); abort = true; }
153 temp = validParameter.validFile(parameters, "numaxes", false); if (temp == "not found"){ temp = "3"; }
154 convert(temp, numaxes);
156 method = validParameter.validFile(parameters, "method", false); if (method == "not found"){ method = "pearson"; }
158 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; }
162 catch(exception& e) {
163 m->errorOut(e, "CorrAxesCommand", "CorrAxesCommand");
167 //**********************************************************************************************************************
169 void CorrAxesCommand::help(){
173 catch(exception& e) {
174 m->errorOut(e, "CorrAxesCommand", "help");
179 //**********************************************************************************************************************
181 CorrAxesCommand::~CorrAxesCommand(){}
183 //**********************************************************************************************************************
185 int CorrAxesCommand::execute(){
188 if (abort == true) { return 0; }
190 /*************************************************************************************/
191 // use smart distancing to get right sharedRabund and convert to relabund if needed //
192 /************************************************************************************/
193 if (sharedfile != "") {
195 if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0; }
196 if (lookup[0] == NULL) { m->mothurOut("[ERROR] reading shared file."); m->mothurOutEndLine(); return 0; }
198 //fills lookupFloat with relative abundance values from lookup
201 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
204 if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
205 if (lookupFloat[0] == NULL) { m->mothurOut("[ERROR] reading relabund file."); m->mothurOutEndLine(); return 0; }
207 if (pickedGroups) { eliminateZeroOTUS(lookupFloat); }
210 if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
212 //this is for a sanity check to make sure the axes file and shared file match
213 for (int i = 0; i < lookupFloat.size(); i++) { names.insert(lookupFloat[i]->getGroup()); }
215 /*************************************************************************************/
216 // read axes file and check for file mismatches with shared or relabund file //
217 /************************************************************************************/
220 map<string, vector<float> > axes = readAxes();
222 if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
224 //sanity check, the read only adds groups that are in the shared or relabund file, but we want to make sure the axes file isn't missing anyone
225 if (axes.size() != lookupFloat.size()) {
226 map<string, vector<float> >::iterator it;
227 for (int i = 0; i < lookupFloat.size(); i++) {
228 it = axes.find(lookupFloat[i]->getGroup());
229 if (it == axes.end()) { m->mothurOut(lookupFloat[i]->getGroup() + " is in your shared of relabund file but not in your axes file, please correct."); m->mothurOutEndLine(); }
231 m->control_pressed = true;
234 if (m->control_pressed) { for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; } return 0; }
236 string outputFileName = outputDir + m->getRootName(m->getSimpleName(inputFileName)) + "corr.axes";
237 outputNames.push_back(outputFileName); outputTypes["corr.axes"].push_back(outputFileName);
239 m->openOutputFile(outputFileName, out);
241 if (method == "pearson") { calcPearson(axes, out); }
242 //else if (method == "spearman") { calcSpearman(axes, out); }
243 //else if (method == "kendall") { calcKendal(axes, out); }
244 //else { m->mothurOut("[ERROR]: Invalid method."); m->mothurOutEndLine(); }
247 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
249 if (m->control_pressed) { return 0; }
251 m->mothurOutEndLine();
252 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
253 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
254 m->mothurOutEndLine();
258 catch(exception& e) {
259 m->errorOut(e, "CorrAxesCommand", "execute");
263 //**********************************************************************************************************************
264 int CorrAxesCommand::calcPearson(map<string, vector<float> >& axes, ofstream& out) {
267 //find average of each axis - X
268 vector<float> averageAxes; averageAxes.resize(numaxes, 0.0);
269 for (map<string, vector<float> >::iterator it = axes.begin(); it != axes.end(); it++) {
270 vector<float> temp = it->second;
271 for (int i = 0; i < temp.size(); i++) {
272 averageAxes[i] += temp[i];
276 for (int i = 0; i < averageAxes.size(); i++) { averageAxes[i] = averageAxes[i] / (float) axes.size(); }
279 for (int i = 0; i < lookupFloat[0]->getNumBins(); i++) {
281 //find the averages this otu - Y
283 for (int j = 0; j < lookupFloat.size(); j++) {
284 sumOtu += lookupFloat[j]->getAbundance(i);
286 float Ybar = sumOtu / (float) lookupFloat.size();
293 catch(exception& e) {
294 m->errorOut(e, "CorrAxesCommand", "calcPearson");
298 //**********************************************************************************************************************
299 int CorrAxesCommand::getShared(){
301 InputData* input = new InputData(sharedfile, "sharedfile");
302 lookup = input->getSharedRAbundVectors();
303 string lastLabel = lookup[0]->getLabel();
305 if (label == "") { label = lastLabel; delete input; return 0; }
307 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
308 set<string> labels; labels.insert(label);
309 set<string> processedLabels;
310 set<string> userLabels = labels;
312 //as long as you are not at the end of the file or done wih the lines you want
313 while((lookup[0] != NULL) && (userLabels.size() != 0)) {
314 if (m->control_pressed) { delete input; return 0; }
316 if(labels.count(lookup[0]->getLabel()) == 1){
317 processedLabels.insert(lookup[0]->getLabel());
318 userLabels.erase(lookup[0]->getLabel());
322 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
323 string saveLabel = lookup[0]->getLabel();
325 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
326 lookup = input->getSharedRAbundVectors(lastLabel);
328 processedLabels.insert(lookup[0]->getLabel());
329 userLabels.erase(lookup[0]->getLabel());
331 //restore real lastlabel to save below
332 lookup[0]->setLabel(saveLabel);
336 lastLabel = lookup[0]->getLabel();
338 //get next line to process
339 //prevent memory leak
340 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
341 lookup = input->getSharedRAbundVectors();
345 if (m->control_pressed) { delete input; return 0; }
347 //output error messages about any remaining user labels
348 set<string>::iterator it;
349 bool needToRun = false;
350 for (it = userLabels.begin(); it != userLabels.end(); it++) {
351 m->mothurOut("Your file does not include the label " + *it);
352 if (processedLabels.count(lastLabel) != 1) {
353 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
356 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
360 //run last label if you need to
361 if (needToRun == true) {
362 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
363 lookup = input->getSharedRAbundVectors(lastLabel);
369 catch(exception& e) {
370 m->errorOut(e, "CorrAxesCommand", "getShared");
374 //**********************************************************************************************************************
375 int CorrAxesCommand::getSharedFloat(){
377 InputData* input = new InputData(relabundfile, "relabund");
378 lookupFloat = input->getSharedRAbundFloatVectors();
379 string lastLabel = lookupFloat[0]->getLabel();
381 if (label == "") { label = lastLabel; delete input; return 0; }
383 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
384 set<string> labels; labels.insert(label);
385 set<string> processedLabels;
386 set<string> userLabels = labels;
388 //as long as you are not at the end of the file or done wih the lines you want
389 while((lookupFloat[0] != NULL) && (userLabels.size() != 0)) {
391 if (m->control_pressed) { delete input; return 0; }
393 if(labels.count(lookupFloat[0]->getLabel()) == 1){
394 processedLabels.insert(lookupFloat[0]->getLabel());
395 userLabels.erase(lookupFloat[0]->getLabel());
399 if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
400 string saveLabel = lookupFloat[0]->getLabel();
402 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
403 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
405 processedLabels.insert(lookupFloat[0]->getLabel());
406 userLabels.erase(lookupFloat[0]->getLabel());
408 //restore real lastlabel to save below
409 lookupFloat[0]->setLabel(saveLabel);
413 lastLabel = lookupFloat[0]->getLabel();
415 //get next line to process
416 //prevent memory leak
417 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
418 lookupFloat = input->getSharedRAbundFloatVectors();
422 if (m->control_pressed) { delete input; return 0; }
424 //output error messages about any remaining user labels
425 set<string>::iterator it;
426 bool needToRun = false;
427 for (it = userLabels.begin(); it != userLabels.end(); it++) {
428 m->mothurOut("Your file does not include the label " + *it);
429 if (processedLabels.count(lastLabel) != 1) {
430 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
433 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
437 //run last label if you need to
438 if (needToRun == true) {
439 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }
440 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
446 catch(exception& e) {
447 m->errorOut(e, "CorrAxesCommand", "getSharedFloat");
451 /*****************************************************************/
452 int CorrAxesCommand::convertToRelabund(){
455 vector<SharedRAbundFloatVector*> newLookup;
456 for (int i = 0; i < lookup.size(); i++) {
457 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
458 temp->setLabel(lookup[i]->getLabel());
459 temp->setGroup(lookup[i]->getGroup());
460 newLookup.push_back(temp);
463 for (int i = 0; i < lookup.size(); i++) {
465 for (int j = 0; j < lookup[i]->getNumBins(); j++) {
467 if (m->control_pressed) { return 0; }
469 int abund = lookup[i]->getAbundance(j);
471 float relabund = abund / (float) lookup[i]->getNumSeqs();
473 newLookup[i]->push_back(relabund, lookup[i]->getGroup());
477 if (pickedGroups) { eliminateZeroOTUS(newLookup); }
479 lookupFloat = newLookup;
483 catch(exception& e) {
484 m->errorOut(e, "CorrAxesCommand", "convertToRelabund");
488 //**********************************************************************************************************************
489 int CorrAxesCommand::eliminateZeroOTUS(vector<SharedRAbundFloatVector*>& thislookup) {
492 vector<SharedRAbundFloatVector*> newLookup;
493 for (int i = 0; i < thislookup.size(); i++) {
494 SharedRAbundFloatVector* temp = new SharedRAbundFloatVector();
495 temp->setLabel(thislookup[i]->getLabel());
496 temp->setGroup(thislookup[i]->getGroup());
497 newLookup.push_back(temp);
501 for (int i = 0; i < thislookup[0]->getNumBins(); i++) {
502 if (m->control_pressed) { for (int j = 0; j < newLookup.size(); j++) { delete newLookup[j]; } return 0; }
504 //look at each sharedRabund and make sure they are not all zero
506 for (int j = 0; j < thislookup.size(); j++) {
507 if (thislookup[j]->getAbundance(i) != 0) { allZero = false; break; }
510 //if they are not all zero add this bin
512 for (int j = 0; j < thislookup.size(); j++) {
513 newLookup[j]->push_back(thislookup[j]->getAbundance(i), thislookup[j]->getGroup());
518 for (int j = 0; j < thislookup.size(); j++) { delete thislookup[j]; }
520 thislookup = newLookup;
525 catch(exception& e) {
526 m->errorOut(e, "CorrAxesCommand", "eliminateZeroOTUS");
530 /*****************************************************************/
531 map<string, vector<float> > CorrAxesCommand::readAxes(){
533 map<string, vector<float> > axes;
536 m->openInputFile(axesfile, in);
538 string headerLine = m->getline(in); m->gobble(in);
540 //count the number of axis you are reading
544 int pos = headerLine.find("axis");
545 if (pos != string::npos) {
547 headerLine = headerLine.substr(pos+4);
548 }else { done = true; }
550 cout << "count = " << count << endl;
554 if (m->control_pressed) { in.close(); return axes; }
557 in >> group; m->gobble(in);
559 vector<float> thisGroupsAxes;
560 for (int i = 0; i < count; i++) {
564 //only save the axis we want
565 if (i < numaxes) { thisGroupsAxes.push_back(temp); }
568 //save group if its one the user selected
569 if (names.count(group) != 0) {
570 map<string, vector<float> >::iterator it = axes.find(group);
572 if (it == axes.end()) {
573 axes[group] = thisGroupsAxes;
575 m->mothurOut(group + " is already in your axes file, using first definition."); m->mothurOutEndLine();
585 catch(exception& e) {
586 m->errorOut(e, "CorrAxesCommand", "convertToRelabund");
590 /*****************************************************************/