2 * matrixoutputcommand.cpp
5 * Created by Sarah Westcott on 5/20/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "matrixoutputcommand.h"
11 #include "sharedjabund.h"
12 #include "sharedsorabund.h"
13 #include "sharedjclass.h"
14 #include "sharedsorclass.h"
15 #include "sharedjest.h"
16 #include "sharedsorest.h"
17 #include "sharedthetayc.h"
18 #include "sharedthetan.h"
19 #include "sharedmorisitahorn.h"
20 #include "sharedbraycurtis.h"
22 //**********************************************************************************************************************
23 vector<string> MatrixOutputCommand::getValidParameters(){
25 string Array[] = {"label","calc","groups","outputdir","inputdir", "output"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
30 m->errorOut(e, "MatrixOutputCommand", "getValidParameters");
34 //**********************************************************************************************************************
35 MatrixOutputCommand::MatrixOutputCommand(){
38 //initialize outputTypes
39 vector<string> tempOutNames;
40 outputTypes["phylip"] = tempOutNames;
43 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
47 //**********************************************************************************************************************
48 vector<string> MatrixOutputCommand::getRequiredParameters(){
50 vector<string> myArray;
54 m->errorOut(e, "MatrixOutputCommand", "getRequiredParameters");
58 //**********************************************************************************************************************
59 vector<string> MatrixOutputCommand::getRequiredFiles(){
61 string Array[] = {"shared"};
62 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
66 m->errorOut(e, "MatrixOutputCommand", "getRequiredFiles");
70 //**********************************************************************************************************************
72 MatrixOutputCommand::MatrixOutputCommand(string option) {
74 globaldata = GlobalData::getInstance();
81 //allow user to run help
82 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
85 //valid paramters for this command
86 string Array[] = {"label","calc","groups","outputdir","inputdir", "output"};
87 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
89 OptionParser parser(option);
90 map<string,string> parameters = parser.getParameters();
92 ValidParameters validParameter;
94 //check to make sure all parameters are valid for command
95 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
96 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
99 //initialize outputTypes
100 vector<string> tempOutNames;
101 outputTypes["phylip"] = tempOutNames;
103 //if the user changes the output directory command factory will send this info to us in the output parameter
104 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
106 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
109 //make sure the user has already run the read.otu command
110 if (globaldata->getSharedFile() == "") {
111 if (globaldata->getListFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the dist.shared command."); m->mothurOutEndLine(); abort = true; }
112 else if (globaldata->getGroupFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the dist.shared command."); m->mothurOutEndLine(); abort = true; }
115 //check for optional parameter and set defaults
116 // ...at some point should added some additional type checking...
117 label = validParameter.validFile(parameters, "label", false);
118 if (label == "not found") { label = ""; }
120 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
121 else { allLines = 1; }
124 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "lt"; }
125 if ((output != "lt") && (output != "square")) { m->mothurOut(output + " is not a valid output form. Options are lt and square. I will use lt."); m->mothurOutEndLine(); output = "lt"; }
127 //if the user has not specified any labels use the ones from read.otu
129 allLines = globaldata->allLines;
130 labels = globaldata->labels;
133 groups = validParameter.validFile(parameters, "groups", false);
134 if (groups == "not found") { groups = ""; }
136 m->splitAtDash(groups, Groups);
137 globaldata->Groups = Groups;
140 calc = validParameter.validFile(parameters, "calc", false);
141 if (calc == "not found") { calc = "jclass-thetayc"; }
143 if (calc == "default") { calc = "jclass-thetayc"; }
145 m->splitAtDash(calc, Estimators);
147 if (abort == false) {
149 validCalculator = new ValidCalculators();
152 for (i=0; i<Estimators.size(); i++) {
153 if (validCalculator->isValidCalculator("matrix", Estimators[i]) == true) {
154 if (Estimators[i] == "jabund") {
155 matrixCalculators.push_back(new JAbund());
156 }else if (Estimators[i] == "sorabund") {
157 matrixCalculators.push_back(new SorAbund());
158 }else if (Estimators[i] == "jclass") {
159 matrixCalculators.push_back(new Jclass());
160 }else if (Estimators[i] == "sorclass") {
161 matrixCalculators.push_back(new SorClass());
162 }else if (Estimators[i] == "jest") {
163 matrixCalculators.push_back(new Jest());
164 }else if (Estimators[i] == "sorest") {
165 matrixCalculators.push_back(new SorEst());
166 }else if (Estimators[i] == "thetayc") {
167 matrixCalculators.push_back(new ThetaYC());
168 }else if (Estimators[i] == "thetan") {
169 matrixCalculators.push_back(new ThetaN());
170 }else if (Estimators[i] == "morisitahorn") {
171 matrixCalculators.push_back(new MorHorn());
172 }else if (Estimators[i] == "braycurtis") {
173 matrixCalculators.push_back(new BrayCurtis());
182 catch(exception& e) {
183 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
188 //**********************************************************************************************************************
190 void MatrixOutputCommand::help(){
192 m->mothurOut("The dist.shared command can only be executed after a successful read.otu command.\n");
193 m->mothurOut("The dist.shared command parameters are groups, calc, output and label. No parameters are required.\n");
194 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n");
195 m->mothurOut("The group names are separated by dashes. The label parameter allows you to select what distance levels you would like distance matrices created for, and is also separated by dashes.\n");
196 m->mothurOut("The dist.shared command should be in the following format: dist.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n");
197 m->mothurOut("The output parameter allows you to specify format of your distance matrix. Options are lt, and square. The default is lt.\n");
198 m->mothurOut("Example dist.shared(groups=A-B-C, calc=jabund-sorabund).\n");
199 m->mothurOut("The default value for groups is all the groups in your groupfile.\n");
200 m->mothurOut("The default value for calc is jclass and thetayc.\n");
201 validCalculator->printCalc("matrix", cout);
202 m->mothurOut("The dist.shared command outputs a .dist file for each calculator you specify at each distance you choose.\n");
203 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
205 catch(exception& e) {
206 m->errorOut(e, "MatrixOutputCommand", "help");
212 //**********************************************************************************************************************
214 MatrixOutputCommand::~MatrixOutputCommand(){
215 if (abort == false) {
216 delete input; globaldata->ginput = NULL;
218 delete validCalculator;
222 //**********************************************************************************************************************
224 int MatrixOutputCommand::execute(){
227 if (abort == true) { return 0; }
229 //if the users entered no valid calculators don't execute command
230 if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
233 read = new ReadOTUFile(globaldata->inputFileName);
234 read->read(&*globaldata);
236 input = globaldata->ginput;
237 lookup = input->getSharedRAbundVectors();
238 string lastLabel = lookup[0]->getLabel();
240 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
241 set<string> processedLabels;
242 set<string> userLabels = labels;
244 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); return 0;}
246 numGroups = lookup.size();
248 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } globaldata->Groups.clear(); return 0; }
250 //as long as you are not at the end of the file or done wih the lines you want
251 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
253 if (m->control_pressed) { outputTypes.clear(); delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0; }
255 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
256 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
259 processedLabels.insert(lookup[0]->getLabel());
260 userLabels.erase(lookup[0]->getLabel());
263 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
264 string saveLabel = lookup[0]->getLabel();
266 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
267 lookup = input->getSharedRAbundVectors(lastLabel);
269 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
272 processedLabels.insert(lookup[0]->getLabel());
273 userLabels.erase(lookup[0]->getLabel());
275 //restore real lastlabel to save below
276 lookup[0]->setLabel(saveLabel);
279 lastLabel = lookup[0]->getLabel();
281 //get next line to process
282 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
283 lookup = input->getSharedRAbundVectors();
286 if (m->control_pressed) { outputTypes.clear(); delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0; }
288 //output error messages about any remaining user labels
289 set<string>::iterator it;
290 bool needToRun = false;
291 for (it = userLabels.begin(); it != userLabels.end(); it++) {
292 m->mothurOut("Your file does not include the label " + *it);
293 if (processedLabels.count(lastLabel) != 1) {
294 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
297 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
301 if (m->control_pressed) { outputTypes.clear(); delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0; }
303 //run last label if you need to
304 if (needToRun == true) {
305 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
306 lookup = input->getSharedRAbundVectors(lastLabel);
308 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
310 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
313 if (m->control_pressed) { outputTypes.clear(); delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } globaldata->Groups.clear(); return 0; }
315 //reset groups parameter
316 globaldata->Groups.clear();
318 m->mothurOutEndLine();
319 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
320 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
321 m->mothurOutEndLine();
326 catch(exception& e) {
327 m->errorOut(e, "MatrixOutputCommand", "execute");
331 /***********************************************************/
332 void MatrixOutputCommand::printSims(ostream& out) {
335 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
338 out << simMatrix.size() << endl;
340 if (output == "lt") {
341 for (int m = 0; m < simMatrix.size(); m++) {
342 out << lookup[m]->getGroup() << '\t';
343 for (int n = 0; n < m; n++) {
344 out << simMatrix[m][n] << '\t';
349 for (int m = 0; m < simMatrix.size(); m++) {
350 out << lookup[m]->getGroup() << '\t';
351 for (int n = 0; n < simMatrix[m].size(); n++) {
352 out << simMatrix[m][n] << '\t';
358 catch(exception& e) {
359 m->errorOut(e, "MatrixOutputCommand", "printSims");
363 /***********************************************************/
364 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
368 vector<SharedRAbundVector*> subset;
370 //for each calculator
371 for(int i = 0 ; i < matrixCalculators.size(); i++) {
373 //initialize simMatrix
375 simMatrix.resize(numGroups);
376 for (int p = 0; p < simMatrix.size(); p++) {
377 for (int j = 0; j < simMatrix.size(); j++) {
378 simMatrix[p].push_back(0.0);
382 for (int k = 0; k < thisLookup.size(); k++) {
383 for (int l = k; l < thisLookup.size(); l++) {
384 if (k != l) { //we dont need to similiarity of a groups to itself
385 //get estimated similarity between 2 groups
387 if (m->control_pressed) { return 0; }
389 subset.clear(); //clear out old pair of sharedrabunds
390 //add new pair of sharedrabunds
391 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
393 data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
394 //save values in similarity matrix
395 simMatrix[k][l] = 1.0 - data[0]; //convert similiarity to distance
396 simMatrix[l][k] = 1.0 - data[0]; //convert similiarity to distance
401 exportFileName = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".dist";
402 m->openOutputFile(exportFileName, out);
403 outputNames.push_back(exportFileName); outputTypes["phylip"].push_back(exportFileName);
413 catch(exception& e) {
414 m->errorOut(e, "MatrixOutputCommand", "process");
418 /***********************************************************/