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::getValidParameters(){
15 string Array[] = {"groups","label","sorted","scale","fontsize","numotu","outputdir","inputdir"};
16 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
20 m->errorOut(e, "HeatMapCommand", "getValidParameters");
24 //**********************************************************************************************************************
25 HeatMapCommand::HeatMapCommand(){
27 //initialize outputTypes
28 vector<string> tempOutNames;
29 outputTypes["svg"] = tempOutNames;
32 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
36 //**********************************************************************************************************************
37 vector<string> HeatMapCommand::getRequiredParameters(){
39 vector<string> myArray;
43 m->errorOut(e, "HeatMapCommand", "getRequiredParameters");
47 //**********************************************************************************************************************
48 vector<string> HeatMapCommand::getRequiredFiles(){
50 string Array[] = {"list","sabund","rabund","shared","relabund","or"};
51 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55 m->errorOut(e, "HeatMapCommand", "getRequiredFiles");
59 //**********************************************************************************************************************
61 HeatMapCommand::HeatMapCommand(string option) {
63 globaldata = GlobalData::getInstance();
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string AlignArray[] = {"groups","label","sorted","scale","fontsize","numotu","outputdir","inputdir"};
74 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
81 //check to make sure all parameters are valid for command
82 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["svg"] = tempOutNames;
90 //if the user changes the output directory command factory will send this info to us in the output parameter
91 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
93 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
96 //make sure the user has already run the read.otu command
97 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "") && (globaldata->getSharedFile() == "") && (globaldata->getRelAbundFile() == "")) {
98 m->mothurOut("You must read a list, rabund, sabund, or a list and a group, shared, or relabund file before you can use the heatmap.bin command."); m->mothurOutEndLine(); abort = true;
101 //check for optional parameter and set defaults
102 // ...at some point should added some additional type checking...
103 label = validParameter.validFile(parameters, "label", false);
104 if (label == "not found") { label = ""; }
106 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
107 else { allLines = 1; }
110 //if the user has not specified any labels use the ones from read.otu
112 allLines = globaldata->allLines;
113 labels = globaldata->labels;
116 groups = validParameter.validFile(parameters, "groups", false);
117 if (groups == "not found") { groups = ""; }
119 m->splitAtDash(groups, Groups);
120 globaldata->Groups = Groups;
123 string temp = validParameter.validFile(parameters, "numotu", false); if (temp == "not found") { temp = "0"; }
124 convert(temp, numOTU);
126 temp = validParameter.validFile(parameters, "fontsize", false); if (temp == "not found") { temp = "24"; }
127 convert(temp, fontSize);
129 sorted = validParameter.validFile(parameters, "sorted", false);
130 if (sorted == "not found") {
131 //if numOTU is used change default
132 if (numOTU != 0) { sorted = "topotu"; }
133 else { sorted = "shared"; }
136 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "log10"; }
138 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; }
142 catch(exception& e) {
143 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
148 //**********************************************************************************************************************
150 void HeatMapCommand::help(){
152 m->mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
153 m->mothurOut("The heatmap.bin command parameters are groups, sorted, scale, numotu, fontsize and label. No parameters are required.\n");
154 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
155 m->mothurOut("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");
156 m->mothurOut("topotu, meaning the otus with the greatest abundance when totaled across groups, topgroup, meaning the top otus for each group. \n");
157 m->mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
158 m->mothurOut("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");
159 m->mothurOut("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");
160 m->mothurOut("The fontsize parameter allows you to adjust the font size of the picture created, default=24.\n");
161 m->mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, label=yourLabels).\n");
162 m->mothurOut("Example heatmap.bin(groups=A-B-C, sorted=none, scale=log10).\n");
163 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
164 m->mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
165 m->mothurOut("The heatmap.bin command outputs a .svg file for each label you specify.\n");
166 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
169 catch(exception& e) {
170 m->errorOut(e, "HeatMapCommand", "help");
175 //**********************************************************************************************************************
177 HeatMapCommand::~HeatMapCommand(){
180 //**********************************************************************************************************************
182 int HeatMapCommand::execute(){
185 if (abort == true) { return 0; }
187 heatmap = new HeatMap(sorted, scale, numOTU, fontSize, outputDir);
188 format = globaldata->getFormat();
192 read = new ReadOTUFile(globaldata->inputFileName);
193 read->read(&*globaldata);
194 input = globaldata->ginput;
196 if (format == "sharedfile") {
198 lookup = input->getSharedRAbundVectors();
199 lastLabel = lookup[0]->getLabel();
201 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
202 //you are using just a list file and have only one group
203 rabund = globaldata->rabund;
204 lastLabel = rabund->getLabel();
205 }else if (format == "relabund") {
207 lookupFloat = input->getSharedRAbundFloatVectors();
208 lastLabel = lookupFloat[0]->getLabel();
211 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
212 set<string> processedLabels;
213 set<string> userLabels = labels;
215 if (format == "sharedfile") {
217 //as long as you are not at the end of the file or done wih the lines you want
218 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
219 if (m->control_pressed) {
220 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
221 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
222 globaldata->Groups.clear();
223 delete read; delete heatmap; return 0;
226 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
228 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
229 string outputFileName = heatmap->getPic(lookup);
230 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
232 processedLabels.insert(lookup[0]->getLabel());
233 userLabels.erase(lookup[0]->getLabel());
236 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
237 string saveLabel = lookup[0]->getLabel();
239 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
241 lookup = input->getSharedRAbundVectors(lastLabel);
242 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
244 string outputFileName = heatmap->getPic(lookup);
245 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
247 processedLabels.insert(lookup[0]->getLabel());
248 userLabels.erase(lookup[0]->getLabel());
250 //restore real lastlabel to save below
251 lookup[0]->setLabel(saveLabel);
254 lastLabel = lookup[0]->getLabel();
255 //prevent memory leak
256 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
258 //get next line to process
259 lookup = input->getSharedRAbundVectors();
263 if (m->control_pressed) {
264 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
265 globaldata->Groups.clear();
266 delete read; delete heatmap; return 0;
269 //output error messages about any remaining user labels
270 set<string>::iterator it;
271 bool needToRun = false;
272 for (it = userLabels.begin(); it != userLabels.end(); it++) {
273 m->mothurOut("Your file does not include the label " + *it);
274 if (processedLabels.count(lastLabel) != 1) {
275 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
278 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
282 //run last label if you need to
283 if (needToRun == true) {
284 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
285 lookup = input->getSharedRAbundVectors(lastLabel);
287 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
288 string outputFileName = heatmap->getPic(lookup);
289 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
290 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
293 //reset groups parameter
294 globaldata->Groups.clear();
296 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
298 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
299 if (m->control_pressed) {
300 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
301 delete rabund; delete read; delete heatmap; return 0;
304 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
306 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
307 string outputFileName = heatmap->getPic(rabund);
308 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
310 processedLabels.insert(rabund->getLabel());
311 userLabels.erase(rabund->getLabel());
314 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
315 string saveLabel = rabund->getLabel();
318 rabund = input->getRAbundVector(lastLabel);
319 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
321 string outputFileName = heatmap->getPic(rabund);
322 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
324 processedLabels.insert(rabund->getLabel());
325 userLabels.erase(rabund->getLabel());
327 //restore real lastlabel to save below
328 rabund->setLabel(saveLabel);
333 lastLabel = rabund->getLabel();
335 rabund = input->getRAbundVector();
338 if (m->control_pressed) {
339 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
340 delete read; delete heatmap; return 0;
343 //output error messages about any remaining user labels
344 set<string>::iterator it;
345 bool needToRun = false;
346 for (it = userLabels.begin(); it != userLabels.end(); it++) {
347 m->mothurOut("Your file does not include the label " + *it);
348 if (processedLabels.count(lastLabel) != 1) {
349 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
352 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
356 //run last label if you need to
357 if (needToRun == true) {
359 if (rabund != NULL) { delete rabund; }
360 rabund = input->getRAbundVector(lastLabel);
361 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
363 string outputFileName = heatmap->getPic(rabund);
364 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
365 delete rabund; globaldata->rabund = NULL;
370 //as long as you are not at the end of the file or done wih the lines you want
371 while((lookupFloat[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
372 if (m->control_pressed) {
373 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
374 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
375 globaldata->Groups.clear();
376 delete read; delete heatmap; return 0;
379 if(allLines == 1 || labels.count(lookupFloat[0]->getLabel()) == 1){
381 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
382 string outputFileName = heatmap->getPic(lookupFloat);
383 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
385 processedLabels.insert(lookupFloat[0]->getLabel());
386 userLabels.erase(lookupFloat[0]->getLabel());
389 if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
390 string saveLabel = lookupFloat[0]->getLabel();
392 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
393 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
394 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
396 string outputFileName = heatmap->getPic(lookupFloat);
397 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
399 processedLabels.insert(lookupFloat[0]->getLabel());
400 userLabels.erase(lookupFloat[0]->getLabel());
402 //restore real lastlabel to save below
403 lookupFloat[0]->setLabel(saveLabel);
406 lastLabel = lookupFloat[0]->getLabel();
407 //prevent memory leak
408 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; lookupFloat[i] = NULL; }
410 //get next line to process
411 lookupFloat = input->getSharedRAbundFloatVectors();
415 if (m->control_pressed) {
416 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
417 globaldata->Groups.clear();
418 delete read; delete heatmap; return 0;
421 //output error messages about any remaining user labels
422 set<string>::iterator it;
423 bool needToRun = false;
424 for (it = userLabels.begin(); it != userLabels.end(); it++) {
425 m->mothurOut("Your file does not include the label " + *it);
426 if (processedLabels.count(lastLabel) != 1) {
427 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
430 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
434 //run last label if you need to
435 if (needToRun == true) {
436 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }
437 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
439 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
440 string outputFileName = heatmap->getPic(lookupFloat);
441 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
442 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
445 //reset groups parameter
446 globaldata->Groups.clear();
450 globaldata->rabund = NULL;
451 delete input; globaldata->ginput = NULL;
453 if (m->control_pressed) {
454 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
455 delete read; delete heatmap; return 0;
458 m->mothurOutEndLine();
459 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
460 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
461 m->mothurOutEndLine();
468 catch(exception& e) {
469 m->errorOut(e, "HeatMapCommand", "execute");
474 //**********************************************************************************************************************