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", "", "", "PhylipColumnShared", "PhylipColumnShared", "none",false,false); parameters.push_back(pshared);
27 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "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", "", "", "PhylipColumnShared", "PhylipColumnShared", "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 pfontsize("fontsize", "Number", "", "24", "", "", "",false,false); parameters.push_back(pfontsize);
34 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
35 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
37 vector<string> myArray;
38 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
42 m->errorOut(e, "HeatMapSimCommand", "setParameters");
46 //**********************************************************************************************************************
47 string HeatMapSimCommand::getHelpString(){
49 string helpString = "";
50 ValidCalculators validCalculator;
51 helpString += "The heatmap.sim command parameters are shared, phylip, column, name, groups, calc, fontsize and label. shared or phylip or column and name are required unless valid current files exist.\n";
52 helpString += "There are two ways to use the heatmap.sim command. The first is with the read.otu command. \n";
53 helpString += "With the read.otu command you may use the groups, label and calc parameters. \n";
54 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n";
55 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";
56 helpString += "The fontsize parameter allows you to adjust the font size of the picture created, default=24.\n";
57 helpString += "The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, label=yourLabels).\n";
58 helpString += "Example heatmap.sim(groups=A-B-C, calc=jabund).\n";
59 helpString += "The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n";
60 helpString += validCalculator.printCalc("heat");
61 helpString += "The default value for calc is jclass-thetayc.\n";
62 helpString += "The heatmap.sim command outputs a .svg file for each calculator you choose at each label you specify.\n";
63 helpString += "The second way to use the heatmap.sim command is with a distance file representing the distance bewteen your groups. \n";
64 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";
65 helpString += "The heatmap.sim command should be in the following format: heatmap.sim(phylip=yourDistanceFile).\n";
66 helpString += "Example heatmap.sim(phylip=amazonGroups.dist).\n";
67 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
71 m->errorOut(e, "HeatMapSimCommand", "getHelpString");
75 //**********************************************************************************************************************
77 string HeatMapSimCommand::getOutputFileNameTag(string type, string inputName=""){
79 string outputFileName = "";
80 map<string, vector<string> >::iterator it;
82 //is this a type this command creates
83 it = outputTypes.find(type);
84 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
86 if (type == "svg") { outputFileName = "svg"; }
87 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
89 return outputFileName;
92 m->errorOut(e, "HeatMapSimCommand", "getOutputFileNameTag");
97 //**********************************************************************************************************************
98 HeatMapSimCommand::HeatMapSimCommand(){
100 abort = true; calledHelp = true;
102 vector<string> tempOutNames;
103 outputTypes["svg"] = tempOutNames;
105 catch(exception& e) {
106 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
110 //**********************************************************************************************************************
112 HeatMapSimCommand::HeatMapSimCommand(string option) {
114 abort = false; calledHelp = false;
117 //allow user to run help
118 if(option == "help") { help(); abort = true; calledHelp = true; }
119 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
122 vector<string> myArray = setParameters();
124 OptionParser parser(option);
125 map<string,string> parameters = parser.getParameters();
127 ValidParameters validParameter;
128 map<string,string>::iterator it;
130 //check to make sure all parameters are valid for command
131 for (it = parameters.begin(); it != parameters.end(); it++) {
132 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
135 //initialize outputTypes
136 vector<string> tempOutNames;
137 outputTypes["svg"] = tempOutNames;
141 //if the user changes the input directory command factory will send this info to us in the output parameter
142 string inputDir = validParameter.validFile(parameters, "inputdir", false);
143 if (inputDir == "not found"){ inputDir = ""; }
146 it = parameters.find("phylip");
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["phylip"] = inputDir + it->second; }
154 it = parameters.find("column");
155 //user has given a template file
156 if(it != parameters.end()){
157 path = m->hasPath(it->second);
158 //if the user has not given a path then, add inputdir. else leave path alone.
159 if (path == "") { parameters["column"] = inputDir + it->second; }
162 it = parameters.find("name");
163 //user has given a template file
164 if(it != parameters.end()){
165 path = m->hasPath(it->second);
166 //if the user has not given a path then, add inputdir. else leave path alone.
167 if (path == "") { parameters["name"] = inputDir + it->second; }
170 it = parameters.find("shared");
171 //user has given a template file
172 if(it != parameters.end()){
173 path = m->hasPath(it->second);
174 //if the user has not given a path then, add inputdir. else leave path alone.
175 if (path == "") { parameters["shared"] = inputDir + it->second; }
179 //required parameters
180 phylipfile = validParameter.validFile(parameters, "phylip", true);
181 if (phylipfile == "not open") { abort = true; }
182 else if (phylipfile == "not found") { phylipfile = ""; }
183 else { format = "phylip"; inputfile = phylipfile; m-> setPhylipFile(phylipfile); if (outputDir == "") { outputDir += m->hasPath(phylipfile); } }
185 columnfile = validParameter.validFile(parameters, "column", true);
186 if (columnfile == "not open") { abort = true; }
187 else if (columnfile == "not found") { columnfile = ""; }
188 else { format = "column"; inputfile = columnfile; m->setColumnFile(columnfile); if (outputDir == "") { outputDir += m->hasPath(columnfile); } }
190 sharedfile = validParameter.validFile(parameters, "shared", true);
191 if (sharedfile == "not open") { abort = true; }
192 else if (sharedfile == "not found") { sharedfile = ""; }
193 else { format = "shared"; inputfile = sharedfile; m->setSharedFile(sharedfile); if (outputDir == "") { outputDir += m->hasPath(sharedfile); } }
195 namefile = validParameter.validFile(parameters, "name", true);
196 if (namefile == "not open") { abort = true; }
197 else if (namefile == "not found") { namefile = ""; }
198 else { m->setNameFile(namefile); }
201 //error checking on files
202 if ((sharedfile == "") && ((phylipfile == "") && (columnfile == ""))) {
203 sharedfile = m->getSharedFile();
204 if (sharedfile != "") { format = "shared"; inputfile = sharedfile; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
206 //is there are current file available for either of these?
207 //give priority to column, then phylip
208 columnfile = m->getColumnFile();
209 if (columnfile != "") { format = "column"; inputfile = columnfile; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
211 phylipfile = m->getPhylipFile();
212 if (phylipfile != "") { format = "phylip"; inputfile = phylipfile; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
214 m->mothurOut("No valid current files. You must provide a shared or phylip or column file."); m->mothurOutEndLine();
220 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; }
222 if (columnfile != "") {
223 if (namefile == "") {
224 namefile = m->getNameFile();
225 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
227 m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine();
234 //if the user changes the output directory command factory will send this info to us in the output parameter
235 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
237 //check for optional parameter and set defaults
238 // ...at some point should added some additional type checking...
240 label = validParameter.validFile(parameters, "label", false);
241 if (label == "not found") { label = ""; }
243 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
244 else { allLines = 1; }
247 calc = validParameter.validFile(parameters, "calc", false);
248 if (calc == "not found") { calc = "jest-thetayc"; }
250 if (calc == "default") { calc = "jest-thetayc"; }
252 m->splitAtDash(calc, Estimators);
253 if (m->inUsersGroups("citation", Estimators)) {
254 ValidCalculators validCalc; validCalc.printCitations(Estimators);
255 //remove citation from list of calcs
256 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
259 groups = validParameter.validFile(parameters, "groups", false);
260 if (groups == "not found") { groups = ""; }
262 m->splitAtDash(groups, Groups);
263 m->setGroups(Groups);
266 string temp = validParameter.validFile(parameters, "fontsize", false); if (temp == "not found") { temp = "24"; }
267 m->mothurConvert(temp, fontsize);
269 if (abort == false) {
270 ValidCalculators validCalculator;
273 for (i=0; i<Estimators.size(); i++) {
274 if (validCalculator.isValidCalculator("heat", Estimators[i]) == true) {
275 if (Estimators[i] == "jabund") {
276 heatCalculators.push_back(new JAbund());
277 }else if (Estimators[i] == "sorabund") {
278 heatCalculators.push_back(new SorAbund());
279 }else if (Estimators[i] == "jclass") {
280 heatCalculators.push_back(new Jclass());
281 }else if (Estimators[i] == "sorclass") {
282 heatCalculators.push_back(new SorClass());
283 }else if (Estimators[i] == "jest") {
284 heatCalculators.push_back(new Jest());
285 }else if (Estimators[i] == "sorest") {
286 heatCalculators.push_back(new SorEst());
287 }else if (Estimators[i] == "thetayc") {
288 heatCalculators.push_back(new ThetaYC());
289 }else if (Estimators[i] == "thetan") {
290 heatCalculators.push_back(new ThetaN());
291 }else if (Estimators[i] == "morisitahorn") {
292 heatCalculators.push_back(new MorHorn());
293 }else if (Estimators[i] == "braycurtis") {
294 heatCalculators.push_back(new BrayCurtis());
305 catch(exception& e) {
306 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
310 //**********************************************************************************************************************
312 int HeatMapSimCommand::execute(){
315 if (abort == true) { if (calledHelp) { return 0; } return 2; }
317 heatmap = new HeatMapSim(outputDir, inputfile, fontsize);
319 if (format == "shared") {
321 }else{ runCommandDist(); }
325 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear(); return 0; }
327 m->mothurOutEndLine();
328 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
329 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
330 m->mothurOutEndLine();
334 catch(exception& e) {
335 m->errorOut(e, "HeatMapSimCommand", "execute");
340 //**********************************************************************************************************************
341 int HeatMapSimCommand::runCommandShared() {
343 //if the users entered no valid calculators don't execute command
344 if (heatCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
346 input = new InputData(sharedfile, "sharedfile");
347 lookup = input->getSharedRAbundVectors();
348 string lastLabel = lookup[0]->getLabel();
350 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); return 0;}
352 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
353 set<string> processedLabels;
354 set<string> userLabels = labels;
356 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0; }
358 //as long as you are not at the end of the file or done wih the lines you want
359 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
361 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0; }
363 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
365 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
366 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
367 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
369 processedLabels.insert(lookup[0]->getLabel());
370 userLabels.erase(lookup[0]->getLabel());
373 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
374 string saveLabel = lookup[0]->getLabel();
376 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
377 lookup = input->getSharedRAbundVectors(lastLabel);
379 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
380 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
381 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
383 processedLabels.insert(lookup[0]->getLabel());
384 userLabels.erase(lookup[0]->getLabel());
386 //restore real lastlabel to save below
387 lookup[0]->setLabel(saveLabel);
390 //prevent memory leak
392 lastLabel = lookup[0]->getLabel();
394 //get next line to process
395 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
396 lookup = input->getSharedRAbundVectors();
401 if (m->control_pressed) { delete input; m->clearGroups(); return 0; }
403 //output error messages about any remaining user labels
404 set<string>::iterator it;
405 bool needToRun = false;
406 for (it = userLabels.begin(); it != userLabels.end(); it++) {
407 m->mothurOut("Your file does not include the label " + *it);
408 if (processedLabels.count(lastLabel) != 1) {
409 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
412 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
416 if (m->control_pressed) { delete input; m->clearGroups(); return 0; }
418 //run last label if you need to
419 if (needToRun == true) {
420 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
421 lookup = input->getSharedRAbundVectors(lastLabel);
423 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
424 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
425 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
427 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
430 if (m->control_pressed) { delete input; m->clearGroups(); return 0; }
432 //reset groups parameter
439 catch(exception& e) {
440 m->errorOut(e, "HeatMapSimCommand", "runCommandShared");
444 //**********************************************************************************************************************
445 int HeatMapSimCommand::runCommandDist() {
448 vector< vector<double> > matrix;
449 vector<string> names;
452 //read distance file and create distance vector and names vector
453 if (format == "phylip") {
455 m->openInputFile(phylipfile, in);
459 in >> numSeqs >> name;
462 names.push_back(name);
464 //resize the matrix and fill with zeros
465 matrix.resize(numSeqs);
466 for(int i = 0; i < numSeqs; i++) {
467 matrix[i].resize(numSeqs, 0.0);
470 //determine if matrix is square or lower triangle
471 //if it is square read the distances for the first sequence
474 while((d=in.get()) != EOF){
476 //is d a number meaning its square
481 for(int i=0;i<numSeqs;i++){
487 //is d a line return meaning its lower triangle
494 //read rest of matrix
495 if (square == true) {
496 for(int i=1;i<numSeqs;i++){
498 names.push_back(name);
500 if (m->control_pressed) { return 0; }
502 for(int j=0;j<numSeqs;j++) { in >> matrix[i][j]; }
507 for(int i=1;i<numSeqs;i++){
509 names.push_back(name);
511 if (m->control_pressed) { return 0; }
513 for(int j=0;j<i;j++){
515 matrix[i][j] = dist; matrix[j][i] = dist;
523 NameAssignment* nameMap = new NameAssignment(namefile);
526 //put names in order in vector
527 for (int i = 0; i < nameMap->size(); i++) {
528 names.push_back(nameMap->get(i));
532 matrix.resize(nameMap->size());
533 for (int i = 0; i < nameMap->size(); i++) {
534 matrix[i].resize(nameMap->size(), 0.0);
538 string first, second;
540 m->openInputFile(columnfile, in);
543 in >> first >> second >> dist; m->gobble(in);
545 if (m->control_pressed) { return 0; }
547 map<string, int>::iterator itA = nameMap->find(first);
548 map<string, int>::iterator itB = nameMap->find(second);
550 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + first + "' was not found in the names file, please correct\n"); exit(1); }
551 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + second + "' was not found in the names file, please correct\n"); exit(1); }
554 matrix[itA->second][itB->second] = dist;
555 matrix[itB->second][itA->second] = dist;
563 string outputFileName = heatmap->getPic(matrix, names);
564 outputNames.push_back(outputFileName); //vector<vector<double>>, vector<string>
565 outputTypes["svg"].push_back(outputFileName);
569 catch(exception& e) {
570 m->errorOut(e, "HeatMapSimCommand", "runCommandDist");
574 //**********************************************************************************************************************