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);
192 if (abort == false) {
194 ValidCalculators validCalculator;
197 for (i=0; i<Estimators.size(); i++) {
198 if (validCalculator.isValidCalculator("matrix", Estimators[i]) == true) {
199 if (Estimators[i] == "sharedsobs") {
200 matrixCalculators.push_back(new SharedSobsCS());
201 }else if (Estimators[i] == "sharedchao") {
202 matrixCalculators.push_back(new SharedChao1());
203 }else if (Estimators[i] == "sharedace") {
204 matrixCalculators.push_back(new SharedAce());
205 }else if (Estimators[i] == "jabund") {
206 matrixCalculators.push_back(new JAbund());
207 }else if (Estimators[i] == "sorabund") {
208 matrixCalculators.push_back(new SorAbund());
209 }else if (Estimators[i] == "jclass") {
210 matrixCalculators.push_back(new Jclass());
211 }else if (Estimators[i] == "sorclass") {
212 matrixCalculators.push_back(new SorClass());
213 }else if (Estimators[i] == "jest") {
214 matrixCalculators.push_back(new Jest());
215 }else if (Estimators[i] == "sorest") {
216 matrixCalculators.push_back(new SorEst());
217 }else if (Estimators[i] == "thetayc") {
218 matrixCalculators.push_back(new ThetaYC());
219 }else if (Estimators[i] == "thetan") {
220 matrixCalculators.push_back(new ThetaN());
221 }else if (Estimators[i] == "kstest") {
222 matrixCalculators.push_back(new KSTest());
223 }else if (Estimators[i] == "sharednseqs") {
224 matrixCalculators.push_back(new SharedNSeqs());
225 }else if (Estimators[i] == "ochiai") {
226 matrixCalculators.push_back(new Ochiai());
227 }else if (Estimators[i] == "anderberg") {
228 matrixCalculators.push_back(new Anderberg());
229 }else if (Estimators[i] == "kulczynski") {
230 matrixCalculators.push_back(new Kulczynski());
231 }else if (Estimators[i] == "kulczynskicody") {
232 matrixCalculators.push_back(new KulczynskiCody());
233 }else if (Estimators[i] == "lennon") {
234 matrixCalculators.push_back(new Lennon());
235 }else if (Estimators[i] == "morisitahorn") {
236 matrixCalculators.push_back(new MorHorn());
237 }else if (Estimators[i] == "braycurtis") {
238 matrixCalculators.push_back(new BrayCurtis());
239 }else if (Estimators[i] == "whittaker") {
240 matrixCalculators.push_back(new Whittaker());
241 }else if (Estimators[i] == "odum") {
242 matrixCalculators.push_back(new Odum());
243 }else if (Estimators[i] == "canberra") {
244 matrixCalculators.push_back(new Canberra());
245 }else if (Estimators[i] == "structeuclidean") {
246 matrixCalculators.push_back(new StructEuclidean());
247 }else if (Estimators[i] == "structchord") {
248 matrixCalculators.push_back(new StructChord());
249 }else if (Estimators[i] == "hellinger") {
250 matrixCalculators.push_back(new Hellinger());
251 }else if (Estimators[i] == "manhattan") {
252 matrixCalculators.push_back(new Manhattan());
253 }else if (Estimators[i] == "structpearson") {
254 matrixCalculators.push_back(new StructPearson());
255 }else if (Estimators[i] == "soergel") {
256 matrixCalculators.push_back(new Soergel());
257 }else if (Estimators[i] == "spearman") {
258 matrixCalculators.push_back(new Spearman());
259 }else if (Estimators[i] == "structkulczynski") {
260 matrixCalculators.push_back(new StructKulczynski());
261 }else if (Estimators[i] == "speciesprofile") {
262 matrixCalculators.push_back(new SpeciesProfile());
263 }else if (Estimators[i] == "hamming") {
264 matrixCalculators.push_back(new Hamming());
265 }else if (Estimators[i] == "structchi2") {
266 matrixCalculators.push_back(new StructChi2());
267 }else if (Estimators[i] == "gower") {
268 matrixCalculators.push_back(new Gower());
269 }else if (Estimators[i] == "memchi2") {
270 matrixCalculators.push_back(new MemChi2());
271 }else if (Estimators[i] == "memchord") {
272 matrixCalculators.push_back(new MemChord());
273 }else if (Estimators[i] == "memeuclidean") {
274 matrixCalculators.push_back(new MemEuclidean());
275 }else if (Estimators[i] == "mempearson") {
276 matrixCalculators.push_back(new MemPearson());
285 catch(exception& e) {
286 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
291 //**********************************************************************************************************************
293 MatrixOutputCommand::~MatrixOutputCommand(){}
295 //**********************************************************************************************************************
297 int MatrixOutputCommand::execute(){
300 if (abort == true) { if (calledHelp) { return 0; } return 2; }
302 //if the users entered no valid calculators don't execute command
303 if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
305 input = new InputData(sharedfile, "sharedfile");
306 lookup = input->getSharedRAbundVectors();
307 string lastLabel = lookup[0]->getLabel();
309 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
310 set<string> processedLabels;
311 set<string> userLabels = labels;
313 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;}
315 numGroups = lookup.size();
317 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->Groups.clear(); return 0; }
319 //as long as you are not at the end of the file or done wih the lines you want
320 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
322 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; }
324 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
325 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
328 processedLabels.insert(lookup[0]->getLabel());
329 userLabels.erase(lookup[0]->getLabel());
332 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
333 string saveLabel = lookup[0]->getLabel();
335 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
336 lookup = input->getSharedRAbundVectors(lastLabel);
338 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
341 processedLabels.insert(lookup[0]->getLabel());
342 userLabels.erase(lookup[0]->getLabel());
344 //restore real lastlabel to save below
345 lookup[0]->setLabel(saveLabel);
348 lastLabel = lookup[0]->getLabel();
350 //get next line to process
351 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
352 lookup = input->getSharedRAbundVectors();
355 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; }
357 //output error messages about any remaining user labels
358 set<string>::iterator it;
359 bool needToRun = false;
360 for (it = userLabels.begin(); it != userLabels.end(); it++) {
361 m->mothurOut("Your file does not include the label " + *it);
362 if (processedLabels.count(lastLabel) != 1) {
363 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
366 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
370 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; }
372 //run last label if you need to
373 if (needToRun == true) {
374 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
375 lookup = input->getSharedRAbundVectors(lastLabel);
377 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
379 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
382 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; }
384 //reset groups parameter
387 //set phylip file as new current phylipfile
389 itTypes = outputTypes.find("phylip");
390 if (itTypes != outputTypes.end()) {
391 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
394 m->mothurOutEndLine();
395 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
396 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
397 m->mothurOutEndLine();
402 catch(exception& e) {
403 m->errorOut(e, "MatrixOutputCommand", "execute");
407 /***********************************************************/
408 void MatrixOutputCommand::printSims(ostream& out) {
411 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
414 out << simMatrix.size() << endl;
416 if (output == "lt") {
417 for (int m = 0; m < simMatrix.size(); m++) {
418 out << lookup[m]->getGroup() << '\t';
419 for (int n = 0; n < m; n++) {
420 out << simMatrix[m][n] << '\t';
425 for (int m = 0; m < simMatrix.size(); m++) {
426 out << lookup[m]->getGroup() << '\t';
427 for (int n = 0; n < simMatrix[m].size(); n++) {
428 out << simMatrix[m][n] << '\t';
434 catch(exception& e) {
435 m->errorOut(e, "MatrixOutputCommand", "printSims");
439 /***********************************************************/
440 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
444 vector<SharedRAbundVector*> subset;
446 //for each calculator
447 for(int i = 0 ; i < matrixCalculators.size(); i++) {
449 //initialize simMatrix
451 simMatrix.resize(numGroups);
452 for (int p = 0; p < simMatrix.size(); p++) {
453 for (int j = 0; j < simMatrix.size(); j++) {
454 simMatrix[p].push_back(0.0);
458 for (int k = 0; k < thisLookup.size(); k++) {
459 for (int l = k; l < thisLookup.size(); l++) {
460 if (k != l) { //we dont need to similiarity of a groups to itself
461 //get estimated similarity between 2 groups
463 if (m->control_pressed) { return 0; }
465 subset.clear(); //clear out old pair of sharedrabunds
466 //add new pair of sharedrabunds
467 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
469 //if this calc needs all groups to calculate the pair load all groups
470 if (matrixCalculators[i]->getNeedsAll()) {
471 //load subset with rest of lookup for those calcs that need everyone to calc for a pair
472 for (int w = 0; w < thisLookup.size(); w++) {
473 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
477 data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
478 //save values in similarity matrix
479 simMatrix[k][l] = 1.0 - data[0]; //convert similiarity to distance
480 simMatrix[l][k] = 1.0 - data[0]; //convert similiarity to distance
485 exportFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".dist";
486 m->openOutputFile(exportFileName, out);
487 outputNames.push_back(exportFileName); outputTypes["phylip"].push_back(exportFileName);
497 catch(exception& e) {
498 m->errorOut(e, "MatrixOutputCommand", "process");
502 /***********************************************************/