2 * heatmapsimcommand.cpp
5 * Created by Sarah Westcott on 6/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "heatmapsimcommand.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"
23 //**********************************************************************************************************************
24 vector<string> HeatMapSimCommand::setParameters(){
26 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(pshared);
27 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none",false,false); parameters.push_back(pphylip);
28 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "ColumnName",false,false); parameters.push_back(pname);
29 CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "ColumnName",false,false); parameters.push_back(pcolumn);
30 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
31 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
32 CommandParameter pcalc("calc", "Multiple", "jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan-morisitahorn-braycurtis", "jest-thetayc", "", "", "",true,false); parameters.push_back(pcalc);
33 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
34 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
36 vector<string> myArray;
37 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
41 m->errorOut(e, "HeatMapSimCommand", "setParameters");
45 //**********************************************************************************************************************
46 string HeatMapSimCommand::getHelpString(){
48 string helpString = "";
49 helpString += "The heatmap.sim command parameters are shared, phylip, column, name, groups, calc and label. shared or phylip or column and name are required unless valid current files exist.\n";
50 helpString += "There are two ways to use the heatmap.sim command. The first is with the read.otu command. \n";
51 helpString += "With the read.otu command you may use the groups, label and calc parameters. \n";
52 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n";
53 helpString += "The group names are separated by dashes. The label parameter allows you to select what distance levels you would like a heatmap created for, and is also separated by dashes.\n";
54 helpString += "The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, label=yourLabels).\n";
55 helpString += "Example heatmap.sim(groups=A-B-C, calc=jabund).\n";
56 helpString += "The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n";
57 helpString += validCalculator->printCalc("heat");
58 helpString += "The default value for calc is jclass-thetayc.\n";
59 helpString += "The heatmap.sim command outputs a .svg file for each calculator you choose at each label you specify.\n";
60 helpString += "The second way to use the heatmap.sim command is with a distance file representing the distance bewteen your groups. \n";
61 helpString += "Using the command this way, the phylip or column parameter are required, and only one may be used. If you use a column file the name filename is required. \n";
62 helpString += "The heatmap.sim command should be in the following format: heatmap.sim(phylip=yourDistanceFile).\n";
63 helpString += "Example heatmap.sim(phylip=amazonGroups.dist).\n";
64 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n";
68 m->errorOut(e, "HeatMapSimCommand", "getHelpString");
72 //**********************************************************************************************************************
73 HeatMapSimCommand::HeatMapSimCommand(){
75 abort = true; calledHelp = true;
77 vector<string> tempOutNames;
78 outputTypes["svg"] = tempOutNames;
81 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
85 //**********************************************************************************************************************
87 HeatMapSimCommand::HeatMapSimCommand(string option) {
89 abort = false; calledHelp = false;
92 //allow user to run help
93 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; calledHelp = true; }
96 vector<string> myArray = setParameters();
98 OptionParser parser(option);
99 map<string,string> parameters = parser.getParameters();
101 ValidParameters validParameter;
102 map<string,string>::iterator it;
104 //check to make sure all parameters are valid for command
105 for (it = parameters.begin(); it != parameters.end(); it++) {
106 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
109 //initialize outputTypes
110 vector<string> tempOutNames;
111 outputTypes["svg"] = tempOutNames;
114 //if the user changes the output directory command factory will send this info to us in the output parameter
115 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
117 //if the user changes the input directory command factory will send this info to us in the output parameter
118 string inputDir = validParameter.validFile(parameters, "inputdir", false);
119 if (inputDir == "not found"){ inputDir = ""; }
122 it = parameters.find("phylip");
123 //user has given a template file
124 if(it != parameters.end()){
125 path = m->hasPath(it->second);
126 //if the user has not given a path then, add inputdir. else leave path alone.
127 if (path == "") { parameters["phylip"] = inputDir + it->second; }
130 it = parameters.find("column");
131 //user has given a template file
132 if(it != parameters.end()){
133 path = m->hasPath(it->second);
134 //if the user has not given a path then, add inputdir. else leave path alone.
135 if (path == "") { parameters["column"] = inputDir + it->second; }
138 it = parameters.find("name");
139 //user has given a template file
140 if(it != parameters.end()){
141 path = m->hasPath(it->second);
142 //if the user has not given a path then, add inputdir. else leave path alone.
143 if (path == "") { parameters["name"] = inputDir + it->second; }
146 it = parameters.find("shared");
147 //user has given a template file
148 if(it != parameters.end()){
149 path = m->hasPath(it->second);
150 //if the user has not given a path then, add inputdir. else leave path alone.
151 if (path == "") { parameters["shared"] = inputDir + it->second; }
155 //required parameters
156 phylipfile = validParameter.validFile(parameters, "phylip", true);
157 if (phylipfile == "not open") { abort = true; }
158 else if (phylipfile == "not found") { phylipfile = ""; }
159 else { format = "phylip"; inputfile = phylipfile; if (outputDir == "") { outputDir += m->hasPath(phylipfile); } }
161 columnfile = validParameter.validFile(parameters, "column", true);
162 if (columnfile == "not open") { abort = true; }
163 else if (columnfile == "not found") { columnfile = ""; }
164 else { format = "column"; inputfile = columnfile; if (outputDir == "") { outputDir += m->hasPath(columnfile); } }
166 sharedfile = validParameter.validFile(parameters, "shared", true);
167 if (sharedfile == "not open") { abort = true; }
168 else if (sharedfile == "not found") { sharedfile = ""; }
169 else { format = "shared"; inputfile = sharedfile; if (outputDir == "") { outputDir += m->hasPath(sharedfile); } }
171 namefile = validParameter.validFile(parameters, "name", true);
172 if (namefile == "not open") { abort = true; }
173 else if (namefile == "not found") { namefile = ""; }
176 //error checking on files
177 if ((sharedfile == "") && ((phylipfile == "") && (columnfile == ""))) {
178 sharedfile = m->getSharedFile();
179 if (sharedfile != "") { format = "shared"; inputfile = sharedfile; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
181 //is there are current file available for either of these?
182 //give priority to column, then phylip
183 columnfile = m->getColumnFile();
184 if (columnfile != "") { format = "column"; inputfile = columnfile; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
186 phylipfile = m->getPhylipFile();
187 if (phylipfile != "") { format = "phylip"; inputfile = phylipfile; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
189 m->mothurOut("No valid current files. You must provide a shared or phylip or column file."); m->mothurOutEndLine();
195 else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When running the heatmap.sim command with a distance file you may not use both the column and the phylip parameters."); m->mothurOutEndLine(); abort = true; }
197 if (columnfile != "") {
198 if (namefile == "") {
199 namefile = m->getNameFile();
200 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
202 m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine();
208 //check for optional parameter and set defaults
209 // ...at some point should added some additional type checking...
211 label = validParameter.validFile(parameters, "label", false);
212 if (label == "not found") { label = ""; }
214 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
215 else { allLines = 1; }
218 calc = validParameter.validFile(parameters, "calc", false);
219 if (calc == "not found") { calc = "jest-thetayc"; }
221 if (calc == "default") { calc = "jest-thetayc"; }
223 m->splitAtDash(calc, Estimators);
225 groups = validParameter.validFile(parameters, "groups", false);
226 if (groups == "not found") { groups = ""; }
228 m->splitAtDash(groups, Groups);
233 if (abort == false) {
234 validCalculator = new ValidCalculators();
237 for (i=0; i<Estimators.size(); i++) {
238 if (validCalculator->isValidCalculator("heat", Estimators[i]) == true) {
239 if (Estimators[i] == "jabund") {
240 heatCalculators.push_back(new JAbund());
241 }else if (Estimators[i] == "sorabund") {
242 heatCalculators.push_back(new SorAbund());
243 }else if (Estimators[i] == "jclass") {
244 heatCalculators.push_back(new Jclass());
245 }else if (Estimators[i] == "sorclass") {
246 heatCalculators.push_back(new SorClass());
247 }else if (Estimators[i] == "jest") {
248 heatCalculators.push_back(new Jest());
249 }else if (Estimators[i] == "sorest") {
250 heatCalculators.push_back(new SorEst());
251 }else if (Estimators[i] == "thetayc") {
252 heatCalculators.push_back(new ThetaYC());
253 }else if (Estimators[i] == "thetan") {
254 heatCalculators.push_back(new ThetaN());
255 }else if (Estimators[i] == "morisitahorn") {
256 heatCalculators.push_back(new MorHorn());
257 }else if (Estimators[i] == "braycurtis") {
258 heatCalculators.push_back(new BrayCurtis());
269 catch(exception& e) {
270 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
274 //**********************************************************************************************************************
276 int HeatMapSimCommand::execute(){
279 if (abort == true) { if (calledHelp) { return 0; } return 2; }
281 heatmap = new HeatMapSim(outputDir, inputfile);
283 if (format == "shared") {
285 }else{ runCommandDist(); }
288 delete validCalculator;
290 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear(); return 0; }
292 m->mothurOutEndLine();
293 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
294 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
295 m->mothurOutEndLine();
299 catch(exception& e) {
300 m->errorOut(e, "HeatMapSimCommand", "execute");
305 //**********************************************************************************************************************
306 int HeatMapSimCommand::runCommandShared() {
308 //if the users entered no valid calculators don't execute command
309 if (heatCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
311 input = new InputData(sharedfile, "sharedfile");
312 lookup = input->getSharedRAbundVectors();
313 string lastLabel = lookup[0]->getLabel();
315 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); return 0;}
317 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
318 set<string> processedLabels;
319 set<string> userLabels = labels;
321 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->Groups.clear(); return 0; }
323 //as long as you are not at the end of the file or done wih the lines you want
324 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
326 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->Groups.clear(); return 0; }
328 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
330 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
331 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
332 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
334 processedLabels.insert(lookup[0]->getLabel());
335 userLabels.erase(lookup[0]->getLabel());
338 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
339 string saveLabel = lookup[0]->getLabel();
341 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
342 lookup = input->getSharedRAbundVectors(lastLabel);
344 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
345 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
346 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
348 processedLabels.insert(lookup[0]->getLabel());
349 userLabels.erase(lookup[0]->getLabel());
351 //restore real lastlabel to save below
352 lookup[0]->setLabel(saveLabel);
355 //prevent memory leak
357 lastLabel = lookup[0]->getLabel();
359 //get next line to process
360 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
361 lookup = input->getSharedRAbundVectors();
366 if (m->control_pressed) { delete input; m->Groups.clear(); return 0; }
368 //output error messages about any remaining user labels
369 set<string>::iterator it;
370 bool needToRun = false;
371 for (it = userLabels.begin(); it != userLabels.end(); it++) {
372 m->mothurOut("Your file does not include the label " + *it);
373 if (processedLabels.count(lastLabel) != 1) {
374 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
377 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
381 if (m->control_pressed) { delete input; m->Groups.clear(); return 0; }
383 //run last label if you need to
384 if (needToRun == true) {
385 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
386 lookup = input->getSharedRAbundVectors(lastLabel);
388 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
389 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
390 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
392 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
395 if (m->control_pressed) { delete input; m->Groups.clear(); return 0; }
397 //reset groups parameter
404 catch(exception& e) {
405 m->errorOut(e, "HeatMapSimCommand", "runCommandShared");
409 //**********************************************************************************************************************
410 int HeatMapSimCommand::runCommandDist() {
413 vector< vector<double> > matrix;
414 vector<string> names;
417 //read distance file and create distance vector and names vector
418 if (format == "phylip") {
420 m->openInputFile(phylipfile, in);
424 in >> numSeqs >> name;
427 names.push_back(name);
429 //resize the matrix and fill with zeros
430 matrix.resize(numSeqs);
431 for(int i = 0; i < numSeqs; i++) {
432 matrix[i].resize(numSeqs, 0.0);
435 //determine if matrix is square or lower triangle
436 //if it is square read the distances for the first sequence
439 while((d=in.get()) != EOF){
441 //is d a number meaning its square
446 for(int i=0;i<numSeqs;i++){
452 //is d a line return meaning its lower triangle
459 //read rest of matrix
460 if (square == true) {
461 for(int i=1;i<numSeqs;i++){
463 names.push_back(name);
465 if (m->control_pressed) { return 0; }
467 for(int j=0;j<numSeqs;j++) { in >> matrix[i][j]; }
472 for(int i=1;i<numSeqs;i++){
474 names.push_back(name);
476 if (m->control_pressed) { return 0; }
478 for(int j=0;j<i;j++){
480 matrix[i][j] = dist; matrix[j][i] = dist;
488 NameAssignment* nameMap = new NameAssignment(namefile);
491 //put names in order in vector
492 for (int i = 0; i < nameMap->size(); i++) {
493 names.push_back(nameMap->get(i));
497 matrix.resize(nameMap->size());
498 for (int i = 0; i < nameMap->size(); i++) {
499 matrix[i].resize(nameMap->size(), 0.0);
503 string first, second;
505 m->openInputFile(columnfile, in);
508 in >> first >> second >> dist; m->gobble(in);
510 if (m->control_pressed) { return 0; }
512 map<string, int>::iterator itA = nameMap->find(first);
513 map<string, int>::iterator itB = nameMap->find(second);
515 if(itA == nameMap->end()){ cerr << "AAError: Sequence '" << first << "' was not found in the names file, please correct\n"; exit(1); }
516 if(itB == nameMap->end()){ cerr << "ABError: Sequence '" << second << "' was not found in the names file, please correct\n"; exit(1); }
519 matrix[itA->second][itB->second] = dist;
520 matrix[itB->second][itA->second] = dist;
528 string outputFileName = heatmap->getPic(matrix, names);
529 outputNames.push_back(outputFileName); //vector<vector<double>>, vector<string>
530 outputTypes["svg"].push_back(outputFileName);
534 catch(exception& e) {
535 m->errorOut(e, "HeatMapSimCommand", "runCommandDist");
539 //**********************************************************************************************************************