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; }
120 vector<string> myArray = setParameters();
122 OptionParser parser(option);
123 map<string,string> parameters = parser.getParameters();
124 map<string,string>::iterator it;
126 ValidParameters validParameter;
128 //check to make sure all parameters are valid for command
129 for (it = parameters.begin(); it != parameters.end(); it++) {
130 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
133 //initialize outputTypes
134 vector<string> tempOutNames;
135 outputTypes["phylip"] = tempOutNames;
137 //if the user changes the input directory command factory will send this info to us in the output parameter
138 string inputDir = validParameter.validFile(parameters, "inputdir", false);
139 if (inputDir == "not found"){ inputDir = ""; }
142 it = parameters.find("shared");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["shared"] = inputDir + it->second; }
151 sharedfile = validParameter.validFile(parameters, "shared", true);
152 if (sharedfile == "not found") {
153 //if there is a current shared file, use it
154 sharedfile = m->getSharedFile();
155 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
156 else { m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
157 }else if (sharedfile == "not open") { sharedfile = ""; abort = true; }
159 //if the user changes the output directory command factory will send this info to us in the output parameter
160 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
162 outputDir += m->hasPath(sharedfile); //if user entered a file with a path then preserve it
165 //check for optional parameter and set defaults
166 // ...at some point should added some additional type checking...
167 label = validParameter.validFile(parameters, "label", false);
168 if (label == "not found") { label = ""; }
170 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
171 else { allLines = 1; }
174 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "lt"; }
175 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"; }
177 groups = validParameter.validFile(parameters, "groups", false);
178 if (groups == "not found") { groups = ""; }
180 m->splitAtDash(groups, Groups);
184 calc = validParameter.validFile(parameters, "calc", false);
185 if (calc == "not found") { calc = "jclass-thetayc"; }
187 if (calc == "default") { calc = "jclass-thetayc"; }
189 m->splitAtDash(calc, Estimators);
191 if (abort == false) {
193 ValidCalculators validCalculator;
196 for (i=0; i<Estimators.size(); i++) {
197 if (validCalculator.isValidCalculator("matrix", Estimators[i]) == true) {
198 if (Estimators[i] == "sharedsobs") {
199 matrixCalculators.push_back(new SharedSobsCS());
200 }else if (Estimators[i] == "sharedchao") {
201 matrixCalculators.push_back(new SharedChao1());
202 }else if (Estimators[i] == "sharedace") {
203 matrixCalculators.push_back(new SharedAce());
204 }else if (Estimators[i] == "jabund") {
205 matrixCalculators.push_back(new JAbund());
206 }else if (Estimators[i] == "sorabund") {
207 matrixCalculators.push_back(new SorAbund());
208 }else if (Estimators[i] == "jclass") {
209 matrixCalculators.push_back(new Jclass());
210 }else if (Estimators[i] == "sorclass") {
211 matrixCalculators.push_back(new SorClass());
212 }else if (Estimators[i] == "jest") {
213 matrixCalculators.push_back(new Jest());
214 }else if (Estimators[i] == "sorest") {
215 matrixCalculators.push_back(new SorEst());
216 }else if (Estimators[i] == "thetayc") {
217 matrixCalculators.push_back(new ThetaYC());
218 }else if (Estimators[i] == "thetan") {
219 matrixCalculators.push_back(new ThetaN());
220 }else if (Estimators[i] == "kstest") {
221 matrixCalculators.push_back(new KSTest());
222 }else if (Estimators[i] == "sharednseqs") {
223 matrixCalculators.push_back(new SharedNSeqs());
224 }else if (Estimators[i] == "ochiai") {
225 matrixCalculators.push_back(new Ochiai());
226 }else if (Estimators[i] == "anderberg") {
227 matrixCalculators.push_back(new Anderberg());
228 }else if (Estimators[i] == "kulczynski") {
229 matrixCalculators.push_back(new Kulczynski());
230 }else if (Estimators[i] == "kulczynskicody") {
231 matrixCalculators.push_back(new KulczynskiCody());
232 }else if (Estimators[i] == "lennon") {
233 matrixCalculators.push_back(new Lennon());
234 }else if (Estimators[i] == "morisitahorn") {
235 matrixCalculators.push_back(new MorHorn());
236 }else if (Estimators[i] == "braycurtis") {
237 matrixCalculators.push_back(new BrayCurtis());
238 }else if (Estimators[i] == "whittaker") {
239 matrixCalculators.push_back(new Whittaker());
240 }else if (Estimators[i] == "odum") {
241 matrixCalculators.push_back(new Odum());
242 }else if (Estimators[i] == "canberra") {
243 matrixCalculators.push_back(new Canberra());
244 }else if (Estimators[i] == "structeuclidean") {
245 matrixCalculators.push_back(new StructEuclidean());
246 }else if (Estimators[i] == "structchord") {
247 matrixCalculators.push_back(new StructChord());
248 }else if (Estimators[i] == "hellinger") {
249 matrixCalculators.push_back(new Hellinger());
250 }else if (Estimators[i] == "manhattan") {
251 matrixCalculators.push_back(new Manhattan());
252 }else if (Estimators[i] == "structpearson") {
253 matrixCalculators.push_back(new StructPearson());
254 }else if (Estimators[i] == "soergel") {
255 matrixCalculators.push_back(new Soergel());
256 }else if (Estimators[i] == "spearman") {
257 matrixCalculators.push_back(new Spearman());
258 }else if (Estimators[i] == "structkulczynski") {
259 matrixCalculators.push_back(new StructKulczynski());
260 }else if (Estimators[i] == "speciesprofile") {
261 matrixCalculators.push_back(new SpeciesProfile());
262 }else if (Estimators[i] == "hamming") {
263 matrixCalculators.push_back(new Hamming());
264 }else if (Estimators[i] == "structchi2") {
265 matrixCalculators.push_back(new StructChi2());
266 }else if (Estimators[i] == "gower") {
267 matrixCalculators.push_back(new Gower());
268 }else if (Estimators[i] == "memchi2") {
269 matrixCalculators.push_back(new MemChi2());
270 }else if (Estimators[i] == "memchord") {
271 matrixCalculators.push_back(new MemChord());
272 }else if (Estimators[i] == "memeuclidean") {
273 matrixCalculators.push_back(new MemEuclidean());
274 }else if (Estimators[i] == "mempearson") {
275 matrixCalculators.push_back(new MemPearson());
284 catch(exception& e) {
285 m->errorOut(e, "MatrixOutputCommand", "MatrixOutputCommand");
290 //**********************************************************************************************************************
292 MatrixOutputCommand::~MatrixOutputCommand(){}
294 //**********************************************************************************************************************
296 int MatrixOutputCommand::execute(){
299 if (abort == true) { if (calledHelp) { return 0; } return 2; }
301 //if the users entered no valid calculators don't execute command
302 if (matrixCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
304 input = new InputData(sharedfile, "sharedfile");
305 lookup = input->getSharedRAbundVectors();
306 string lastLabel = lookup[0]->getLabel();
308 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
309 set<string> processedLabels;
310 set<string> userLabels = labels;
312 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;}
314 numGroups = lookup.size();
316 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->Groups.clear(); return 0; }
318 //as long as you are not at the end of the file or done wih the lines you want
319 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
321 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; }
323 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
324 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
327 processedLabels.insert(lookup[0]->getLabel());
328 userLabels.erase(lookup[0]->getLabel());
331 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
332 string saveLabel = lookup[0]->getLabel();
334 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
335 lookup = input->getSharedRAbundVectors(lastLabel);
337 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
340 processedLabels.insert(lookup[0]->getLabel());
341 userLabels.erase(lookup[0]->getLabel());
343 //restore real lastlabel to save below
344 lookup[0]->setLabel(saveLabel);
347 lastLabel = lookup[0]->getLabel();
349 //get next line to process
350 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
351 lookup = input->getSharedRAbundVectors();
354 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; }
356 //output error messages about any remaining user labels
357 set<string>::iterator it;
358 bool needToRun = false;
359 for (it = userLabels.begin(); it != userLabels.end(); it++) {
360 m->mothurOut("Your file does not include the label " + *it);
361 if (processedLabels.count(lastLabel) != 1) {
362 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
365 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
369 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; }
371 //run last label if you need to
372 if (needToRun == true) {
373 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
374 lookup = input->getSharedRAbundVectors(lastLabel);
376 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
378 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
381 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; }
383 //reset groups parameter
386 //set phylip file as new current phylipfile
388 itTypes = outputTypes.find("phylip");
389 if (itTypes != outputTypes.end()) {
390 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
393 m->mothurOutEndLine();
394 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
395 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
396 m->mothurOutEndLine();
401 catch(exception& e) {
402 m->errorOut(e, "MatrixOutputCommand", "execute");
406 /***********************************************************/
407 void MatrixOutputCommand::printSims(ostream& out) {
410 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
413 out << simMatrix.size() << endl;
415 if (output == "lt") {
416 for (int m = 0; m < simMatrix.size(); m++) {
417 out << lookup[m]->getGroup() << '\t';
418 for (int n = 0; n < m; n++) {
419 out << simMatrix[m][n] << '\t';
424 for (int m = 0; m < simMatrix.size(); m++) {
425 out << lookup[m]->getGroup() << '\t';
426 for (int n = 0; n < simMatrix[m].size(); n++) {
427 out << simMatrix[m][n] << '\t';
433 catch(exception& e) {
434 m->errorOut(e, "MatrixOutputCommand", "printSims");
438 /***********************************************************/
439 int MatrixOutputCommand::process(vector<SharedRAbundVector*> thisLookup){
443 vector<SharedRAbundVector*> subset;
445 //for each calculator
446 for(int i = 0 ; i < matrixCalculators.size(); i++) {
448 //initialize simMatrix
450 simMatrix.resize(numGroups);
451 for (int p = 0; p < simMatrix.size(); p++) {
452 for (int j = 0; j < simMatrix.size(); j++) {
453 simMatrix[p].push_back(0.0);
457 for (int k = 0; k < thisLookup.size(); k++) {
458 for (int l = k; l < thisLookup.size(); l++) {
459 if (k != l) { //we dont need to similiarity of a groups to itself
460 //get estimated similarity between 2 groups
462 if (m->control_pressed) { return 0; }
464 subset.clear(); //clear out old pair of sharedrabunds
465 //add new pair of sharedrabunds
466 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]);
468 //if this calc needs all groups to calculate the pair load all groups
469 if (matrixCalculators[i]->getNeedsAll()) {
470 //load subset with rest of lookup for those calcs that need everyone to calc for a pair
471 for (int w = 0; w < thisLookup.size(); w++) {
472 if ((w != k) && (w != l)) { subset.push_back(thisLookup[w]); }
476 data = matrixCalculators[i]->getValues(subset); //saves the calculator outputs
477 //save values in similarity matrix
478 simMatrix[k][l] = 1.0 - data[0]; //convert similiarity to distance
479 simMatrix[l][k] = 1.0 - data[0]; //convert similiarity to distance
484 exportFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + "." + output + ".dist";
485 m->openOutputFile(exportFileName, out);
486 outputNames.push_back(exportFileName); outputTypes["phylip"].push_back(exportFileName);
496 catch(exception& e) {
497 m->errorOut(e, "MatrixOutputCommand", "process");
501 /***********************************************************/