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 ValidCalculators validCalculator;
78 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";
79 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n";
80 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";
81 helpString += "The dist.shared command should be in the following format: dist.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n";
82 helpString += "The output parameter allows you to specify format of your distance matrix. Options are lt, and square. The default is lt.\n";
83 helpString += "Example dist.shared(groups=A-B-C, calc=jabund-sorabund).\n";
84 helpString += "The default value for groups is all the groups in your groupfile.\n";
85 helpString += "The default value for calc is jclass and thetayc.\n";
86 helpString += validCalculator.printCalc("matrix");
87 helpString += "The dist.shared command outputs a .dist file for each calculator you specify at each distance you choose.\n";
88 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
92 m->errorOut(e, "MatrixOutputCommand", "getHelpString");
96 //**********************************************************************************************************************
97 MatrixOutputCommand::MatrixOutputCommand(){
99 abort = true; calledHelp = true;
101 vector<string> tempOutNames;
102 outputTypes["phylip"] = tempOutNames;
104 catch(exception& e) {
105 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
109 //**********************************************************************************************************************
111 MatrixOutputCommand::MatrixOutputCommand(string option) {
113 abort = false; calledHelp = false;
116 //allow user to run help
117 if(option == "help") { help(); abort = true; calledHelp = true; }
118 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
121 vector<string> myArray = setParameters();
123 OptionParser parser(option);
124 map<string,string> parameters = parser.getParameters();
125 map<string,string>::iterator it;
127 ValidParameters validParameter;
129 //check to make sure all parameters are valid for command
130 for (it = parameters.begin(); it != parameters.end(); it++) {
131 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
134 //initialize outputTypes
135 vector<string> tempOutNames;
136 outputTypes["phylip"] = tempOutNames;
138 //if the user changes the input directory command factory will send this info to us in the output parameter
139 string inputDir = validParameter.validFile(parameters, "inputdir", false);
140 if (inputDir == "not found"){ inputDir = ""; }
143 it = parameters.find("shared");
144 //user has given a template file
145 if(it != parameters.end()){
146 path = m->hasPath(it->second);
147 //if the user has not given a path then, add inputdir. else leave path alone.
148 if (path == "") { parameters["shared"] = inputDir + it->second; }
152 sharedfile = validParameter.validFile(parameters, "shared", true);
153 if (sharedfile == "not found") {
154 //if there is a current shared file, use it
155 sharedfile = m->getSharedFile();
156 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
157 else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
158 }else if (sharedfile == "not open") { sharedfile = ""; abort = true; }
160 //if the user changes the output directory command factory will send this info to us in the output parameter
161 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
163 outputDir += m->hasPath(sharedfile); //if user entered a file with a path then preserve it
166 //check for optional parameter and set defaults
167 // ...at some point should added some additional type checking...
168 label = validParameter.validFile(parameters, "label", false);
169 if (label == "not found") { label = ""; }
171 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
172 else { allLines = 1; }
175 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "lt"; }
176 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"; }
178 groups = validParameter.validFile(parameters, "groups", false);
179 if (groups == "not found") { groups = ""; }
181 m->splitAtDash(groups, Groups);
185 calc = validParameter.validFile(parameters, "calc", false);
186 if (calc == "not found") { calc = "jclass-thetayc"; }
188 if (calc == "default") { calc = "jclass-thetayc"; }
190 m->splitAtDash(calc, Estimators);
191 if (m->inUsersGroups("citation", Estimators)) {
192 ValidCalculators validCalc; validCalc.printCitations(Estimators);
193 //remove citation from list of calcs
194 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
197 if (abort == false) {
199 ValidCalculators validCalculator;
202 for (i=0; i<Estimators.size(); i++) {
203 if (validCalculator.isValidCalculator("matrix", Estimators[i]) == true) {
204 if (Estimators[i] == "sharedsobs") {
205 matrixCalculators.push_back(new SharedSobsCS());
206 }else if (Estimators[i] == "sharedchao") {
207 matrixCalculators.push_back(new SharedChao1());
208 }else if (Estimators[i] == "sharedace") {
209 matrixCalculators.push_back(new SharedAce());
210 }else if (Estimators[i] == "jabund") {
211 matrixCalculators.push_back(new JAbund());
212 }else if (Estimators[i] == "sorabund") {
213 matrixCalculators.push_back(new SorAbund());
214 }else if (Estimators[i] == "jclass") {
215 matrixCalculators.push_back(new Jclass());
216 }else if (Estimators[i] == "sorclass") {
217 matrixCalculators.push_back(new SorClass());
218 }else if (Estimators[i] == "jest") {
219 matrixCalculators.push_back(new Jest());
220 }else if (Estimators[i] == "sorest") {
221 matrixCalculators.push_back(new SorEst());
222 }else if (Estimators[i] == "thetayc") {
223 matrixCalculators.push_back(new ThetaYC());
224 }else if (Estimators[i] == "thetan") {
225 matrixCalculators.push_back(new ThetaN());
226 }else if (Estimators[i] == "kstest") {
227 matrixCalculators.push_back(new KSTest());
228 }else if (Estimators[i] == "sharednseqs") {
229 matrixCalculators.push_back(new SharedNSeqs());
230 }else if (Estimators[i] == "ochiai") {
231 matrixCalculators.push_back(new Ochiai());
232 }else if (Estimators[i] == "anderberg") {
233 matrixCalculators.push_back(new Anderberg());
234 }else if (Estimators[i] == "kulczynski") {
235 matrixCalculators.push_back(new Kulczynski());
236 }else if (Estimators[i] == "kulczynskicody") {
237 matrixCalculators.push_back(new KulczynskiCody());
238 }else if (Estimators[i] == "lennon") {
239 matrixCalculators.push_back(new Lennon());
240 }else if (Estimators[i] == "morisitahorn") {
241 matrixCalculators.push_back(new MorHorn());
242 }else if (Estimators[i] == "braycurtis") {
243 matrixCalculators.push_back(new BrayCurtis());
244 }else if (Estimators[i] == "whittaker") {
245 matrixCalculators.push_back(new Whittaker());
246 }else if (Estimators[i] == "odum") {
247 matrixCalculators.push_back(new Odum());
248 }else if (Estimators[i] == "canberra") {
249 matrixCalculators.push_back(new Canberra());
250 }else if (Estimators[i] == "structeuclidean") {
251 matrixCalculators.push_back(new StructEuclidean());
252 }else if (Estimators[i] == "structchord") {
253 matrixCalculators.push_back(new StructChord());
254 }else if (Estimators[i] == "hellinger") {
255 matrixCalculators.push_back(new Hellinger());
256 }else if (Estimators[i] == "manhattan") {
257 matrixCalculators.push_back(new Manhattan());
258 }else if (Estimators[i] == "structpearson") {
259 matrixCalculators.push_back(new StructPearson());
260 }else if (Estimators[i] == "soergel") {
261 matrixCalculators.push_back(new Soergel());
262 }else if (Estimators[i] == "spearman") {
263 matrixCalculators.push_back(new Spearman());
264 }else if (Estimators[i] == "structkulczynski") {
265 matrixCalculators.push_back(new StructKulczynski());
266 }else if (Estimators[i] == "speciesprofile") {
267 matrixCalculators.push_back(new SpeciesProfile());
268 }else if (Estimators[i] == "hamming") {
269 matrixCalculators.push_back(new Hamming());
270 }else if (Estimators[i] == "structchi2") {
271 matrixCalculators.push_back(new StructChi2());
272 }else if (Estimators[i] == "gower") {
273 matrixCalculators.push_back(new Gower());
274 }else if (Estimators[i] == "memchi2") {
275 matrixCalculators.push_back(new MemChi2());
276 }else if (Estimators[i] == "memchord") {
277 matrixCalculators.push_back(new MemChord());
278 }else if (Estimators[i] == "memeuclidean") {
279 matrixCalculators.push_back(new MemEuclidean());
280 }else if (Estimators[i] == "mempearson") {
281 matrixCalculators.push_back(new MemPearson());
290 catch(exception& e) {
291 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
296 //**********************************************************************************************************************
298 MatrixOutputCommand::~MatrixOutputCommand(){}
300 //**********************************************************************************************************************
302 int MatrixOutputCommand::execute(){
305 if (abort == true) { if (calledHelp) { return 0; } return 2; }
307 //if the users entered no valid calculators don't execute command
308 if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
310 input = new InputData(sharedfile, "sharedfile");
311 lookup = input->getSharedRAbundVectors();
312 string lastLabel = lookup[0]->getLabel();
314 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
315 set<string> processedLabels;
316 set<string> userLabels = labels;
318 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;}
320 numGroups = lookup.size();
322 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->Groups.clear(); return 0; }
324 //as long as you are not at the end of the file or done wih the lines you want
325 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
327 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; }
329 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
330 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
333 processedLabels.insert(lookup[0]->getLabel());
334 userLabels.erase(lookup[0]->getLabel());
337 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
338 string saveLabel = lookup[0]->getLabel();
340 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
341 lookup = input->getSharedRAbundVectors(lastLabel);
343 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
346 processedLabels.insert(lookup[0]->getLabel());
347 userLabels.erase(lookup[0]->getLabel());
349 //restore real lastlabel to save below
350 lookup[0]->setLabel(saveLabel);
353 lastLabel = lookup[0]->getLabel();
355 //get next line to process
356 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
357 lookup = input->getSharedRAbundVectors();
360 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; }
362 //output error messages about any remaining user labels
363 set<string>::iterator it;
364 bool needToRun = false;
365 for (it = userLabels.begin(); it != userLabels.end(); it++) {
366 m->mothurOut("Your file does not include the label " + *it);
367 if (processedLabels.count(lastLabel) != 1) {
368 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
371 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
375 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; }
377 //run last label if you need to
378 if (needToRun == true) {
379 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
380 lookup = input->getSharedRAbundVectors(lastLabel);
382 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
384 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
387 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; }
389 //reset groups parameter
392 //set phylip file as new current phylipfile
394 itTypes = outputTypes.find("phylip");
395 if (itTypes != outputTypes.end()) {
396 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
399 m->mothurOutEndLine();
400 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
401 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
402 m->mothurOutEndLine();
407 catch(exception& e) {
408 m->errorOut(e, "MatrixOutputCommand", "execute");
412 /***********************************************************/
413 void MatrixOutputCommand::printSims(ostream& out) {
416 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
419 out << simMatrix.size() << endl;
421 if (output == "lt") {
422 for (int m = 0; m < simMatrix.size(); m++) {
423 out << lookup[m]->getGroup() << '\t';
424 for (int n = 0; n < m; n++) {
425 out << simMatrix[m][n] << '\t';
430 for (int m = 0; m < simMatrix.size(); m++) {
431 out << lookup[m]->getGroup() << '\t';
432 for (int n = 0; n < simMatrix[m].size(); n++) {
433 out << simMatrix[m][n] << '\t';
439 catch(exception& e) {
440 m->errorOut(e, "MatrixOutputCommand", "printSims");
444 /***********************************************************/
445 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
449 vector<SharedRAbundVector*> subset;
451 //for each calculator
452 for(int i = 0 ; i < matrixCalculators.size(); i++) {
454 //initialize simMatrix
456 simMatrix.resize(numGroups);
457 for (int p = 0; p < simMatrix.size(); p++) {
458 for (int j = 0; j < simMatrix.size(); j++) {
459 simMatrix[p].push_back(0.0);
463 for (int k = 0; k < thisLookup.size(); k++) {
464 for (int l = k; l < thisLookup.size(); l++) {
465 if (k != l) { //we dont need to similiarity of a groups to itself
466 //get estimated similarity between 2 groups
468 if (m->control_pressed) { return 0; }
470 subset.clear(); //clear out old pair of sharedrabunds
471 //add new pair of sharedrabunds
472 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
474 //if this calc needs all groups to calculate the pair load all groups
475 if (matrixCalculators[i]->getNeedsAll()) {
476 //load subset with rest of lookup for those calcs that need everyone to calc for a pair
477 for (int w = 0; w < thisLookup.size(); w++) {
478 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
482 data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
483 //save values in similarity matrix
484 simMatrix[k][l] = 1.0 - data[0]; //convert similiarity to distance
485 simMatrix[l][k] = 1.0 - data[0]; //convert similiarity to distance
490 exportFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".dist";
491 m->openOutputFile(exportFileName, out);
492 outputNames.push_back(exportFileName); outputTypes["phylip"].push_back(exportFileName);
502 catch(exception& e) {
503 m->errorOut(e, "MatrixOutputCommand", "process");
507 /***********************************************************/