5 * Created by Sarah Westcott on 3/25/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "heatmapcommand.h"
12 //**********************************************************************************************************************
13 vector<string> HeatMapCommand::setParameters(){
15 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
16 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prabund);
17 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(psabund);
18 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(pshared);
19 CommandParameter prelabund("relabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prelabund);
20 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
21 CommandParameter pscale("scale", "Multiple", "log10-log2-linear", "log10", "", "", "",false,false); parameters.push_back(pscale);
22 CommandParameter psorted("sorted", "Multiple", "none-shared-topotu-topgroup", "shared", "", "", "",false,false); parameters.push_back(psorted);
23 CommandParameter pnumotu("numotu", "Number", "", "0", "", "", "",false,false); parameters.push_back(pnumotu);
24 CommandParameter pfontsize("fontsize", "Number", "", "24", "", "", "",false,false); parameters.push_back(pfontsize);
25 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
26 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
27 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
29 vector<string> myArray;
30 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
34 m->errorOut(e, "HeatMapCommand", "setParameters");
38 //**********************************************************************************************************************
39 string HeatMapCommand::getHelpString(){
41 string helpString = "";
42 helpString += "The heatmap.bin command parameters are shared, relabund, list, rabund, sabund, groups, sorted, scale, numotu, fontsize and label. shared, relabund, list, rabund or sabund is required unless you have a valid current file.\n";
43 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n";
44 helpString += "The sorted parameter allows you to order the otus displayed, default=shared, meaning display the shared otus first. Other options for sorted are none, meaning the exact representation of your otus, \n";
45 helpString += "topotu, meaning the otus with the greatest abundance when totaled across groups, topgroup, meaning the top otus for each group. \n";
46 helpString += "The scale parameter allows you to choose the range of color your bin information will be displayed with.\n";
47 helpString += "The numotu parameter allows you to display only the top N otus, by default all the otus are displayed. You could choose to look at the top 10, by setting numotu=10. The default for sorted is topotu when numotu is used.\n";
48 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 are also separated by dashes.\n";
49 helpString += "The fontsize parameter allows you to adjust the font size of the picture created, default=24.\n";
50 helpString += "The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, label=yourLabels).\n";
51 helpString += "Example heatmap.bin(groups=A-B-C, sorted=none, scale=log10).\n";
52 helpString += "The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n";
53 helpString += "The default value for scale is log10; your other options are log2 and linear.\n";
54 helpString += "The heatmap.bin command outputs a .svg file for each label you specify.\n";
55 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
59 m->errorOut(e, "HeatMapCommand", "getHelpString");
64 //**********************************************************************************************************************
66 string HeatMapCommand::getOutputFileNameTag(string type, string inputName=""){
68 string outputFileName = "";
69 map<string, vector<string> >::iterator it;
71 //is this a type this command creates
72 it = outputTypes.find(type);
73 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
75 if (type == "svg") { outputFileName = "svg"; }
76 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
78 return outputFileName;
81 m->errorOut(e, "HeatMapCommand", "getOutputFileNameTag");
86 //**********************************************************************************************************************
87 HeatMapCommand::HeatMapCommand(){
89 abort = true; calledHelp = true;
91 vector<string> tempOutNames;
92 outputTypes["svg"] = tempOutNames;
95 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
99 //**********************************************************************************************************************
101 HeatMapCommand::HeatMapCommand(string option) {
103 abort = false; calledHelp = false;
106 //allow user to run help
107 if(option == "help") { help(); abort = true; calledHelp = true; }
108 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
111 vector<string> myArray = setParameters();
113 OptionParser parser(option);
114 map<string,string> parameters = parser.getParameters();
115 map<string,string>::iterator it;
117 ValidParameters validParameter;
119 //check to make sure all parameters are valid for command
120 for (it = parameters.begin(); it != parameters.end(); it++) {
121 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
124 //initialize outputTypes
125 vector<string> tempOutNames;
126 outputTypes["svg"] = tempOutNames;
128 //if the user changes the input directory command factory will send this info to us in the output parameter
129 string inputDir = validParameter.validFile(parameters, "inputdir", false);
130 if (inputDir == "not found"){ inputDir = ""; }
133 it = parameters.find("shared");
134 //user has given a template file
135 if(it != parameters.end()){
136 path = m->hasPath(it->second);
137 //if the user has not given a path then, add inputdir. else leave path alone.
138 if (path == "") { parameters["shared"] = inputDir + it->second; }
141 it = parameters.find("rabund");
142 //user has given a template file
143 if(it != parameters.end()){
144 path = m->hasPath(it->second);
145 //if the user has not given a path then, add inputdir. else leave path alone.
146 if (path == "") { parameters["rabund"] = inputDir + it->second; }
149 it = parameters.find("sabund");
150 //user has given a template file
151 if(it != parameters.end()){
152 path = m->hasPath(it->second);
153 //if the user has not given a path then, add inputdir. else leave path alone.
154 if (path == "") { parameters["sabund"] = inputDir + it->second; }
157 it = parameters.find("list");
158 //user has given a template file
159 if(it != parameters.end()){
160 path = m->hasPath(it->second);
161 //if the user has not given a path then, add inputdir. else leave path alone.
162 if (path == "") { parameters["list"] = inputDir + it->second; }
165 it = parameters.find("relabund");
166 //user has given a template file
167 if(it != parameters.end()){
168 path = m->hasPath(it->second);
169 //if the user has not given a path then, add inputdir. else leave path alone.
170 if (path == "") { parameters["relabund"] = inputDir + it->second; }
174 //check for required parameters
175 listfile = validParameter.validFile(parameters, "list", true);
176 if (listfile == "not open") { abort = true; }
177 else if (listfile == "not found") { listfile = ""; }
178 else { format = "list"; inputfile = listfile; m->setListFile(listfile); }
180 sabundfile = validParameter.validFile(parameters, "sabund", true);
181 if (sabundfile == "not open") { abort = true; }
182 else if (sabundfile == "not found") { sabundfile = ""; }
183 else { format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
185 rabundfile = validParameter.validFile(parameters, "rabund", true);
186 if (rabundfile == "not open") { abort = true; }
187 else if (rabundfile == "not found") { rabundfile = ""; }
188 else { format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
190 sharedfile = validParameter.validFile(parameters, "shared", true);
191 if (sharedfile == "not open") { abort = true; }
192 else if (sharedfile == "not found") { sharedfile = ""; }
193 else { format = "sharedfile"; inputfile = sharedfile; m->setSharedFile(sharedfile); }
195 relabundfile = validParameter.validFile(parameters, "relabund", true);
196 if (relabundfile == "not open") { abort = true; }
197 else if (relabundfile == "not found") { relabundfile = ""; }
198 else { format = "relabund"; inputfile = relabundfile; m->setRelAbundFile(relabundfile); }
201 if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "") && (relabundfile == "")) {
202 //is there are current file available for any of these?
203 //give priority to shared, then list, then rabund, then sabund
204 //if there is a current shared file, use it
205 sharedfile = m->getSharedFile();
206 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
208 listfile = m->getListFile();
209 if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
211 rabundfile = m->getRabundFile();
212 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
214 sabundfile = m->getSabundFile();
215 if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
217 relabundfile = m->getRelAbundFile();
218 if (relabundfile != "") { inputfile = relabundfile; format = "relabund"; m->mothurOut("Using " + relabundfile + " as input file for the relabund parameter."); m->mothurOutEndLine(); }
220 m->mothurOut("No valid current files. You must provide a list, sabund, rabund, relabund or shared file."); m->mothurOutEndLine();
230 //if the user changes the output directory command factory will send this info to us in the output parameter
231 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
233 //check for optional parameter and set defaults
234 // ...at some point should added some additional type checking...
235 label = validParameter.validFile(parameters, "label", false);
236 if (label == "not found") { label = ""; }
238 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
239 else { allLines = 1; }
243 groups = validParameter.validFile(parameters, "groups", false);
244 if (groups == "not found") { groups = ""; }
246 m->splitAtDash(groups, Groups);
247 m->setGroups(Groups);
250 string temp = validParameter.validFile(parameters, "numotu", false); if (temp == "not found") { temp = "0"; }
251 m->mothurConvert(temp, numOTU);
253 temp = validParameter.validFile(parameters, "fontsize", false); if (temp == "not found") { temp = "24"; }
254 m->mothurConvert(temp, fontSize);
256 sorted = validParameter.validFile(parameters, "sorted", false);
257 if (sorted == "not found") {
258 //if numOTU is used change default
259 if (numOTU != 0) { sorted = "topotu"; }
260 else { sorted = "shared"; }
263 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "log10"; }
265 if ((sorted != "none") && (sorted != "shared") && (sorted != "topotu") && (sorted != "topgroup")) { m->mothurOut(sorted + " is not a valid sorting option. Sorted options are: none, shared, topotu, topgroup"); m->mothurOutEndLine(); abort=true; }
269 catch(exception& e) {
270 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
274 //**********************************************************************************************************************
276 int HeatMapCommand::execute(){
279 if (abort == true) { if (calledHelp) { return 0; } return 2; }
281 heatmap = new HeatMap(sorted, scale, numOTU, fontSize, outputDir, inputfile);
284 input = new InputData(inputfile, format);
286 if (format == "sharedfile") {
288 lookup = input->getSharedRAbundVectors();
289 lastLabel = lookup[0]->getLabel();
291 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
292 //you are using just a list file and have only one group
293 rabund = input->getRAbundVector();
294 lastLabel = rabund->getLabel();
295 }else if (format == "relabund") {
297 lookupFloat = input->getSharedRAbundFloatVectors();
298 lastLabel = lookupFloat[0]->getLabel();
301 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
302 set<string> processedLabels;
303 set<string> userLabels = labels;
305 if (format == "sharedfile") {
307 //as long as you are not at the end of the file or done wih the lines you want
308 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
309 if (m->control_pressed) {
310 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
311 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { m->mothurRemove(outputNames[i]); } } outputTypes.clear();
313 delete input; delete heatmap; return 0;
316 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
318 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
319 string outputFileName = heatmap->getPic(lookup);
320 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
322 processedLabels.insert(lookup[0]->getLabel());
323 userLabels.erase(lookup[0]->getLabel());
326 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
327 string saveLabel = lookup[0]->getLabel();
329 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
331 lookup = input->getSharedRAbundVectors(lastLabel);
332 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
334 string outputFileName = heatmap->getPic(lookup);
335 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
337 processedLabels.insert(lookup[0]->getLabel());
338 userLabels.erase(lookup[0]->getLabel());
340 //restore real lastlabel to save below
341 lookup[0]->setLabel(saveLabel);
344 lastLabel = lookup[0]->getLabel();
345 //prevent memory leak
346 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
348 //get next line to process
349 lookup = input->getSharedRAbundVectors();
353 if (m->control_pressed) {
354 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { m->mothurRemove(outputNames[i]); } } outputTypes.clear();
356 delete input; delete heatmap; return 0;
359 //output error messages about any remaining user labels
360 set<string>::iterator it;
361 bool needToRun = false;
362 for (it = userLabels.begin(); it != userLabels.end(); it++) {
363 m->mothurOut("Your file does not include the label " + *it);
364 if (processedLabels.count(lastLabel) != 1) {
365 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
368 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
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();
378 string outputFileName = heatmap->getPic(lookup);
379 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
380 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
383 //reset groups parameter
386 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
388 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
389 if (m->control_pressed) {
390 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { m->mothurRemove(outputNames[i]); } } outputTypes.clear();
391 delete rabund; delete input; delete heatmap; return 0;
394 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
396 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
397 string outputFileName = heatmap->getPic(rabund);
398 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
400 processedLabels.insert(rabund->getLabel());
401 userLabels.erase(rabund->getLabel());
404 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
405 string saveLabel = rabund->getLabel();
408 rabund = input->getRAbundVector(lastLabel);
409 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
411 string outputFileName = heatmap->getPic(rabund);
412 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
414 processedLabels.insert(rabund->getLabel());
415 userLabels.erase(rabund->getLabel());
417 //restore real lastlabel to save below
418 rabund->setLabel(saveLabel);
423 lastLabel = rabund->getLabel();
425 rabund = input->getRAbundVector();
428 if (m->control_pressed) {
429 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { m->mothurRemove(outputNames[i]); } } outputTypes.clear();
430 delete input; delete heatmap; return 0;
433 //output error messages about any remaining user labels
434 set<string>::iterator it;
435 bool needToRun = false;
436 for (it = userLabels.begin(); it != userLabels.end(); it++) {
437 m->mothurOut("Your file does not include the label " + *it);
438 if (processedLabels.count(lastLabel) != 1) {
439 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
442 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
446 //run last label if you need to
447 if (needToRun == true) {
449 if (rabund != NULL) { delete rabund; }
450 rabund = input->getRAbundVector(lastLabel);
451 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
453 string outputFileName = heatmap->getPic(rabund);
454 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
460 //as long as you are not at the end of the file or done wih the lines you want
461 while((lookupFloat[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
462 if (m->control_pressed) {
463 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
464 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { m->mothurRemove(outputNames[i]); } } outputTypes.clear();
466 delete input; delete heatmap; return 0;
469 if(allLines == 1 || labels.count(lookupFloat[0]->getLabel()) == 1){
471 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
472 string outputFileName = heatmap->getPic(lookupFloat);
473 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
475 processedLabels.insert(lookupFloat[0]->getLabel());
476 userLabels.erase(lookupFloat[0]->getLabel());
479 if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
480 string saveLabel = lookupFloat[0]->getLabel();
482 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
483 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
484 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
486 string outputFileName = heatmap->getPic(lookupFloat);
487 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
489 processedLabels.insert(lookupFloat[0]->getLabel());
490 userLabels.erase(lookupFloat[0]->getLabel());
492 //restore real lastlabel to save below
493 lookupFloat[0]->setLabel(saveLabel);
496 lastLabel = lookupFloat[0]->getLabel();
497 //prevent memory leak
498 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; lookupFloat[i] = NULL; }
500 //get next line to process
501 lookupFloat = input->getSharedRAbundFloatVectors();
505 if (m->control_pressed) {
506 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { m->mothurRemove(outputNames[i]); } } outputTypes.clear();
508 delete input; delete heatmap; return 0;
511 //output error messages about any remaining user labels
512 set<string>::iterator it;
513 bool needToRun = false;
514 for (it = userLabels.begin(); it != userLabels.end(); it++) {
515 m->mothurOut("Your file does not include the label " + *it);
516 if (processedLabels.count(lastLabel) != 1) {
517 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
520 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
524 //run last label if you need to
525 if (needToRun == true) {
526 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }
527 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
529 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
530 string outputFileName = heatmap->getPic(lookupFloat);
531 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
532 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
535 //reset groups parameter
543 if (m->control_pressed) {
544 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { m->mothurRemove(outputNames[i]); } } outputTypes.clear(); return 0;
547 m->mothurOutEndLine();
548 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
549 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
550 m->mothurOutEndLine();
554 catch(exception& e) {
555 m->errorOut(e, "HeatMapCommand", "execute");
560 //**********************************************************************************************************************