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 "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharednseqs.h"
15 #include "sharedjabund.h"
16 #include "sharedsorabund.h"
17 #include "sharedjclass.h"
18 #include "sharedsorclass.h"
19 #include "sharedjest.h"
20 #include "sharedsorest.h"
21 #include "sharedthetayc.h"
22 #include "sharedthetan.h"
23 #include "sharedkstest.h"
24 #include "whittaker.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32 #include "sharedjackknife.h"
33 #include "whittaker.h"
36 #include "structeuclidean.h"
37 #include "structchord.h"
38 #include "hellinger.h"
39 #include "manhattan.h"
40 #include "structpearson.h"
43 #include "structkulczynski.h"
44 #include "structchi2.h"
45 #include "speciesprofile.h"
50 #include "memeuclidean.h"
51 #include "mempearson.h"
53 //**********************************************************************************************************************
54 vector<string> MatrixOutputCommand::setParameters(){
56 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pshared);
57 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
58 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
59 CommandParameter pcalc("calc", "Multiple", "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan-kstest-sharednseqs-ochiai-anderberg-kulczynski-kulczynskicody-lennon-morisitahorn-braycurtis-whittaker-odum-canberra-structeuclidean-structchord-hellinger-manhattan-structpearson-soergel-spearman-structkulczynski-speciesprofile-hamming-structchi2-gower-memchi2-memchord-memeuclidean-mempearson", "jclass-thetayc", "", "", "",true,false); parameters.push_back(pcalc);
60 CommandParameter poutput("output", "Multiple", "lt-square", "lt", "", "", "",false,false); parameters.push_back(poutput);
61 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
62 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
64 vector<string> myArray;
65 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
69 m->errorOut(e, "MatrixOutputCommand", "setParameters");
73 //**********************************************************************************************************************
74 string MatrixOutputCommand::getHelpString(){
76 string helpString = "";
77 helpString += "The dist.shared command parameters are shared, groups, calc, output and label. shared is a required, unless you have a valid current file.\n";
78 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n";
79 helpString += "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";
80 helpString += "The dist.shared command should be in the following format: dist.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n";
81 helpString += "The output parameter allows you to specify format of your distance matrix. Options are lt, and square. The default is lt.\n";
82 helpString += "Example dist.shared(groups=A-B-C, calc=jabund-sorabund).\n";
83 helpString += "The default value for groups is all the groups in your groupfile.\n";
84 helpString += "The default value for calc is jclass and thetayc.\n";
85 helpString += validCalculator->printCalc("matrix");
86 helpString += "The dist.shared command outputs a .dist file for each calculator you specify at each distance you choose.\n";
87 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n";
91 m->errorOut(e, "MatrixOutputCommand", "getHelpString");
95 //**********************************************************************************************************************
96 MatrixOutputCommand::MatrixOutputCommand(){
98 abort = true; calledHelp = true;
100 vector<string> tempOutNames;
101 outputTypes["phylip"] = tempOutNames;
103 catch(exception& e) {
104 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
108 //**********************************************************************************************************************
110 MatrixOutputCommand::MatrixOutputCommand(string option) {
112 abort = false; calledHelp = false;
115 //allow user to run help
116 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; calledHelp = true; }
119 vector<string> myArray = setParameters();
121 OptionParser parser(option);
122 map<string,string> parameters = parser.getParameters();
123 map<string,string>::iterator it;
125 ValidParameters validParameter;
127 //check to make sure all parameters are valid for command
128 for (it = parameters.begin(); it != parameters.end(); it++) {
129 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
132 //initialize outputTypes
133 vector<string> tempOutNames;
134 outputTypes["phylip"] = tempOutNames;
136 //if the user changes the input directory command factory will send this info to us in the output parameter
137 string inputDir = validParameter.validFile(parameters, "inputdir", false);
138 if (inputDir == "not found"){ inputDir = ""; }
141 it = parameters.find("shared");
142 //user has given a template file
143 if(it != parameters.end()){
144 path = m->hasPath(it->second);
145 //if the user has not given a path then, add inputdir. else leave path alone.
146 if (path == "") { parameters["shared"] = inputDir + it->second; }
150 sharedfile = validParameter.validFile(parameters, "shared", true);
151 if (sharedfile == "not found") {
152 //if there is a current shared file, use it
153 sharedfile = m->getSharedFile();
154 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
155 else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
156 }else if (sharedfile == "not open") { sharedfile = ""; abort = true; }
158 //if the user changes the output directory command factory will send this info to us in the output parameter
159 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
161 outputDir += m->hasPath(sharedfile); //if user entered a file with a path then preserve it
164 //check for optional parameter and set defaults
165 // ...at some point should added some additional type checking...
166 label = validParameter.validFile(parameters, "label", false);
167 if (label == "not found") { label = ""; }
169 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
170 else { allLines = 1; }
173 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "lt"; }
174 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"; }
176 groups = validParameter.validFile(parameters, "groups", false);
177 if (groups == "not found") { groups = ""; }
179 m->splitAtDash(groups, Groups);
183 calc = validParameter.validFile(parameters, "calc", false);
184 if (calc == "not found") { calc = "jclass-thetayc"; }
186 if (calc == "default") { calc = "jclass-thetayc"; }
188 m->splitAtDash(calc, Estimators);
190 if (abort == false) {
192 validCalculator = new ValidCalculators();
195 for (i=0; i<Estimators.size(); i++) {
196 if (validCalculator->isValidCalculator("matrix", Estimators[i]) == true) {
197 if (Estimators[i] == "sharedsobs") {
198 matrixCalculators.push_back(new SharedSobsCS());
199 }else if (Estimators[i] == "sharedchao") {
200 matrixCalculators.push_back(new SharedChao1());
201 }else if (Estimators[i] == "sharedace") {
202 matrixCalculators.push_back(new SharedAce());
203 }else if (Estimators[i] == "jabund") {
204 matrixCalculators.push_back(new JAbund());
205 }else if (Estimators[i] == "sorabund") {
206 matrixCalculators.push_back(new SorAbund());
207 }else if (Estimators[i] == "jclass") {
208 matrixCalculators.push_back(new Jclass());
209 }else if (Estimators[i] == "sorclass") {
210 matrixCalculators.push_back(new SorClass());
211 }else if (Estimators[i] == "jest") {
212 matrixCalculators.push_back(new Jest());
213 }else if (Estimators[i] == "sorest") {
214 matrixCalculators.push_back(new SorEst());
215 }else if (Estimators[i] == "thetayc") {
216 matrixCalculators.push_back(new ThetaYC());
217 }else if (Estimators[i] == "thetan") {
218 matrixCalculators.push_back(new ThetaN());
219 }else if (Estimators[i] == "kstest") {
220 matrixCalculators.push_back(new KSTest());
221 }else if (Estimators[i] == "sharednseqs") {
222 matrixCalculators.push_back(new SharedNSeqs());
223 }else if (Estimators[i] == "ochiai") {
224 matrixCalculators.push_back(new Ochiai());
225 }else if (Estimators[i] == "anderberg") {
226 matrixCalculators.push_back(new Anderberg());
227 }else if (Estimators[i] == "kulczynski") {
228 matrixCalculators.push_back(new Kulczynski());
229 }else if (Estimators[i] == "kulczynskicody") {
230 matrixCalculators.push_back(new KulczynskiCody());
231 }else if (Estimators[i] == "lennon") {
232 matrixCalculators.push_back(new Lennon());
233 }else if (Estimators[i] == "morisitahorn") {
234 matrixCalculators.push_back(new MorHorn());
235 }else if (Estimators[i] == "braycurtis") {
236 matrixCalculators.push_back(new BrayCurtis());
237 }else if (Estimators[i] == "whittaker") {
238 matrixCalculators.push_back(new Whittaker());
239 }else if (Estimators[i] == "odum") {
240 matrixCalculators.push_back(new Odum());
241 }else if (Estimators[i] == "canberra") {
242 matrixCalculators.push_back(new Canberra());
243 }else if (Estimators[i] == "structeuclidean") {
244 matrixCalculators.push_back(new StructEuclidean());
245 }else if (Estimators[i] == "structchord") {
246 matrixCalculators.push_back(new StructChord());
247 }else if (Estimators[i] == "hellinger") {
248 matrixCalculators.push_back(new Hellinger());
249 }else if (Estimators[i] == "manhattan") {
250 matrixCalculators.push_back(new Manhattan());
251 }else if (Estimators[i] == "structpearson") {
252 matrixCalculators.push_back(new StructPearson());
253 }else if (Estimators[i] == "soergel") {
254 matrixCalculators.push_back(new Soergel());
255 }else if (Estimators[i] == "spearman") {
256 matrixCalculators.push_back(new Spearman());
257 }else if (Estimators[i] == "structkulczynski") {
258 matrixCalculators.push_back(new StructKulczynski());
259 }else if (Estimators[i] == "speciesprofile") {
260 matrixCalculators.push_back(new SpeciesProfile());
261 }else if (Estimators[i] == "hamming") {
262 matrixCalculators.push_back(new Hamming());
263 }else if (Estimators[i] == "structchi2") {
264 matrixCalculators.push_back(new StructChi2());
265 }else if (Estimators[i] == "gower") {
266 matrixCalculators.push_back(new Gower());
267 }else if (Estimators[i] == "memchi2") {
268 matrixCalculators.push_back(new MemChi2());
269 }else if (Estimators[i] == "memchord") {
270 matrixCalculators.push_back(new MemChord());
271 }else if (Estimators[i] == "memeuclidean") {
272 matrixCalculators.push_back(new MemEuclidean());
273 }else if (Estimators[i] == "mempearson") {
274 matrixCalculators.push_back(new MemPearson());
283 catch(exception& e) {
284 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
289 //**********************************************************************************************************************
291 MatrixOutputCommand::~MatrixOutputCommand(){
292 if (abort == false) {
293 delete validCalculator;
297 //**********************************************************************************************************************
299 int MatrixOutputCommand::execute(){
302 if (abort == true) { if (calledHelp) { return 0; } return 2; }
304 //if the users entered no valid calculators don't execute command
305 if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
307 input = new InputData(sharedfile, "sharedfile");
308 lookup = input->getSharedRAbundVectors();
309 string lastLabel = lookup[0]->getLabel();
311 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
312 set<string> processedLabels;
313 set<string> userLabels = labels;
315 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } return 0;}
317 numGroups = lookup.size();
319 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->Groups.clear(); return 0; }
321 //as long as you are not at the end of the file or done wih the lines you want
322 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
324 if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } m->Groups.clear(); return 0; }
326 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
327 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
330 processedLabels.insert(lookup[0]->getLabel());
331 userLabels.erase(lookup[0]->getLabel());
334 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
335 string saveLabel = lookup[0]->getLabel();
337 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
338 lookup = input->getSharedRAbundVectors(lastLabel);
340 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
343 processedLabels.insert(lookup[0]->getLabel());
344 userLabels.erase(lookup[0]->getLabel());
346 //restore real lastlabel to save below
347 lookup[0]->setLabel(saveLabel);
350 lastLabel = lookup[0]->getLabel();
352 //get next line to process
353 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
354 lookup = input->getSharedRAbundVectors();
357 if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } m->Groups.clear(); return 0; }
359 //output error messages about any remaining user labels
360 set<string>::iterator it;
361 bool needToRun = false;
362 for (it = userLabels.begin(); it != userLabels.end(); it++) {
363 m->mothurOut("Your file does not include the label " + *it);
364 if (processedLabels.count(lastLabel) != 1) {
365 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
368 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
372 if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } m->Groups.clear(); return 0; }
374 //run last label if you need to
375 if (needToRun == true) {
376 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
377 lookup = input->getSharedRAbundVectors(lastLabel);
379 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
381 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
384 if (m->control_pressed) { outputTypes.clear(); delete input; for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } m->Groups.clear(); return 0; }
386 //reset groups parameter
389 //set phylip file as new current phylipfile
391 itTypes = outputTypes.find("phylip");
392 if (itTypes != outputTypes.end()) {
393 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
396 m->mothurOutEndLine();
397 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
398 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
399 m->mothurOutEndLine();
404 catch(exception& e) {
405 m->errorOut(e, "MatrixOutputCommand", "execute");
409 /***********************************************************/
410 void MatrixOutputCommand::printSims(ostream& out) {
413 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
416 out << simMatrix.size() << endl;
418 if (output == "lt") {
419 for (int m = 0; m < simMatrix.size(); m++) {
420 out << lookup[m]->getGroup() << '\t';
421 for (int n = 0; n < m; n++) {
422 out << simMatrix[m][n] << '\t';
427 for (int m = 0; m < simMatrix.size(); m++) {
428 out << lookup[m]->getGroup() << '\t';
429 for (int n = 0; n < simMatrix[m].size(); n++) {
430 out << simMatrix[m][n] << '\t';
436 catch(exception& e) {
437 m->errorOut(e, "MatrixOutputCommand", "printSims");
441 /***********************************************************/
442 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
446 vector<SharedRAbundVector*> subset;
448 //for each calculator
449 for(int i = 0 ; i < matrixCalculators.size(); i++) {
451 //initialize simMatrix
453 simMatrix.resize(numGroups);
454 for (int p = 0; p < simMatrix.size(); p++) {
455 for (int j = 0; j < simMatrix.size(); j++) {
456 simMatrix[p].push_back(0.0);
460 for (int k = 0; k < thisLookup.size(); k++) {
461 for (int l = k; l < thisLookup.size(); l++) {
462 if (k != l) { //we dont need to similiarity of a groups to itself
463 //get estimated similarity between 2 groups
465 if (m->control_pressed) { return 0; }
467 subset.clear(); //clear out old pair of sharedrabunds
468 //add new pair of sharedrabunds
469 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
471 //if this calc needs all groups to calculate the pair load all groups
472 if (matrixCalculators[i]->getNeedsAll()) {
473 //load subset with rest of lookup for those calcs that need everyone to calc for a pair
474 for (int w = 0; w < thisLookup.size(); w++) {
475 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
479 data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
480 //save values in similarity matrix
481 simMatrix[k][l] = 1.0 - data[0]; //convert similiarity to distance
482 simMatrix[l][k] = 1.0 - data[0]; //convert similiarity to distance
487 exportFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".dist";
488 m->openOutputFile(exportFileName, out);
489 outputNames.push_back(exportFileName); outputTypes["phylip"].push_back(exportFileName);
499 catch(exception& e) {
500 m->errorOut(e, "MatrixOutputCommand", "process");
504 /***********************************************************/