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(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["svg"] = tempOutNames;
33 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
37 //**********************************************************************************************************************
38 vector<string> HeatMapCommand::getRequiredParameters(){
40 vector<string> myArray;
44 m->errorOut(e, "HeatMapCommand", "getRequiredParameters");
48 //**********************************************************************************************************************
49 vector<string> HeatMapCommand::getRequiredFiles(){
51 string Array[] = {"list","sabund","rabund","shared","relabund","or"};
52 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
56 m->errorOut(e, "HeatMapCommand", "getRequiredFiles");
60 //**********************************************************************************************************************
62 HeatMapCommand::HeatMapCommand(string option) {
64 globaldata = GlobalData::getInstance();
69 //allow user to run help
70 if(option == "help") { help(); abort = true; }
73 //valid paramters for this command
74 string AlignArray[] = {"groups","label","sorted","scale","fontsize","numotu","outputdir","inputdir"};
75 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
77 OptionParser parser(option);
78 map<string,string> parameters = parser.getParameters();
80 ValidParameters validParameter;
82 //check to make sure all parameters are valid for command
83 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //initialize outputTypes
88 vector<string> tempOutNames;
89 outputTypes["svg"] = tempOutNames;
91 //if the user changes the output directory command factory will send this info to us in the output parameter
92 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
94 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
97 //make sure the user has already run the read.otu command
98 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "") && (globaldata->getSharedFile() == "") && (globaldata->getRelAbundFile() == "")) {
99 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;
102 //check for optional parameter and set defaults
103 // ...at some point should added some additional type checking...
104 label = validParameter.validFile(parameters, "label", false);
105 if (label == "not found") { label = ""; }
107 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
108 else { allLines = 1; }
111 //if the user has not specified any labels use the ones from read.otu
113 allLines = globaldata->allLines;
114 labels = globaldata->labels;
117 groups = validParameter.validFile(parameters, "groups", false);
118 if (groups == "not found") { groups = ""; }
120 m->splitAtDash(groups, Groups);
121 globaldata->Groups = Groups;
124 string temp = validParameter.validFile(parameters, "numotu", false); if (temp == "not found") { temp = "0"; }
125 convert(temp, numOTU);
127 temp = validParameter.validFile(parameters, "fontsize", false); if (temp == "not found") { temp = "24"; }
128 convert(temp, fontSize);
130 sorted = validParameter.validFile(parameters, "sorted", false);
131 if (sorted == "not found") {
132 //if numOTU is used change default
133 if (numOTU != 0) { sorted = "topotu"; }
134 else { sorted = "shared"; }
137 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "log10"; }
139 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; }
143 catch(exception& e) {
144 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
149 //**********************************************************************************************************************
151 void HeatMapCommand::help(){
153 m->mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
154 m->mothurOut("The heatmap.bin command parameters are groups, sorted, scale, numotu, fontsize and label. No parameters are required.\n");
155 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
156 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");
157 m->mothurOut("topotu, meaning the otus with the greatest abundance when totaled across groups, topgroup, meaning the top otus for each group. \n");
158 m->mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
159 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");
160 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");
161 m->mothurOut("The fontsize parameter allows you to adjust the font size of the picture created, default=24.\n");
162 m->mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, label=yourLabels).\n");
163 m->mothurOut("Example heatmap.bin(groups=A-B-C, sorted=none, scale=log10).\n");
164 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
165 m->mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
166 m->mothurOut("The heatmap.bin command outputs a .svg file for each label you specify.\n");
167 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
170 catch(exception& e) {
171 m->errorOut(e, "HeatMapCommand", "help");
176 //**********************************************************************************************************************
178 HeatMapCommand::~HeatMapCommand(){
181 //**********************************************************************************************************************
183 int HeatMapCommand::execute(){
186 if (abort == true) { return 0; }
188 heatmap = new HeatMap(sorted, scale, numOTU, fontSize, outputDir);
189 format = globaldata->getFormat();
193 read = new ReadOTUFile(globaldata->inputFileName);
194 read->read(&*globaldata);
195 input = globaldata->ginput;
197 if (format == "sharedfile") {
199 lookup = input->getSharedRAbundVectors();
200 lastLabel = lookup[0]->getLabel();
202 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
203 //you are using just a list file and have only one group
204 rabund = globaldata->rabund;
205 lastLabel = rabund->getLabel();
206 }else if (format == "relabund") {
208 lookupFloat = input->getSharedRAbundFloatVectors();
209 lastLabel = lookupFloat[0]->getLabel();
212 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
213 set<string> processedLabels;
214 set<string> userLabels = labels;
216 if (format == "sharedfile") {
218 //as long as you are not at the end of the file or done wih the lines you want
219 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
220 if (m->control_pressed) {
221 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
222 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
223 globaldata->Groups.clear();
224 delete read; delete heatmap; return 0;
227 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
229 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
230 string outputFileName = heatmap->getPic(lookup);
231 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
233 processedLabels.insert(lookup[0]->getLabel());
234 userLabels.erase(lookup[0]->getLabel());
237 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
238 string saveLabel = lookup[0]->getLabel();
240 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
242 lookup = input->getSharedRAbundVectors(lastLabel);
243 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
245 string outputFileName = heatmap->getPic(lookup);
246 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
248 processedLabels.insert(lookup[0]->getLabel());
249 userLabels.erase(lookup[0]->getLabel());
251 //restore real lastlabel to save below
252 lookup[0]->setLabel(saveLabel);
255 lastLabel = lookup[0]->getLabel();
256 //prevent memory leak
257 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
259 //get next line to process
260 lookup = input->getSharedRAbundVectors();
264 if (m->control_pressed) {
265 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
266 globaldata->Groups.clear();
267 delete read; delete heatmap; return 0;
270 //output error messages about any remaining user labels
271 set<string>::iterator it;
272 bool needToRun = false;
273 for (it = userLabels.begin(); it != userLabels.end(); it++) {
274 m->mothurOut("Your file does not include the label " + *it);
275 if (processedLabels.count(lastLabel) != 1) {
276 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
279 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
283 //run last label if you need to
284 if (needToRun == true) {
285 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
286 lookup = input->getSharedRAbundVectors(lastLabel);
288 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
289 string outputFileName = heatmap->getPic(lookup);
290 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
291 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
294 //reset groups parameter
295 globaldata->Groups.clear();
297 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
299 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
300 if (m->control_pressed) {
301 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
302 delete rabund; delete read; delete heatmap; return 0;
305 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
307 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
308 string outputFileName = heatmap->getPic(rabund);
309 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
311 processedLabels.insert(rabund->getLabel());
312 userLabels.erase(rabund->getLabel());
315 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
316 string saveLabel = rabund->getLabel();
319 rabund = input->getRAbundVector(lastLabel);
320 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
322 string outputFileName = heatmap->getPic(rabund);
323 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
325 processedLabels.insert(rabund->getLabel());
326 userLabels.erase(rabund->getLabel());
328 //restore real lastlabel to save below
329 rabund->setLabel(saveLabel);
334 lastLabel = rabund->getLabel();
336 rabund = input->getRAbundVector();
339 if (m->control_pressed) {
340 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
341 delete read; delete heatmap; return 0;
344 //output error messages about any remaining user labels
345 set<string>::iterator it;
346 bool needToRun = false;
347 for (it = userLabels.begin(); it != userLabels.end(); it++) {
348 m->mothurOut("Your file does not include the label " + *it);
349 if (processedLabels.count(lastLabel) != 1) {
350 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
353 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
357 //run last label if you need to
358 if (needToRun == true) {
360 if (rabund != NULL) { delete rabund; }
361 rabund = input->getRAbundVector(lastLabel);
362 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
364 string outputFileName = heatmap->getPic(rabund);
365 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
366 delete rabund; globaldata->rabund = NULL;
371 //as long as you are not at the end of the file or done wih the lines you want
372 while((lookupFloat[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
373 if (m->control_pressed) {
374 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
375 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
376 globaldata->Groups.clear();
377 delete read; delete heatmap; return 0;
380 if(allLines == 1 || labels.count(lookupFloat[0]->getLabel()) == 1){
382 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
383 string outputFileName = heatmap->getPic(lookupFloat);
384 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
386 processedLabels.insert(lookupFloat[0]->getLabel());
387 userLabels.erase(lookupFloat[0]->getLabel());
390 if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
391 string saveLabel = lookupFloat[0]->getLabel();
393 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
394 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
395 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
397 string outputFileName = heatmap->getPic(lookupFloat);
398 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
400 processedLabels.insert(lookupFloat[0]->getLabel());
401 userLabels.erase(lookupFloat[0]->getLabel());
403 //restore real lastlabel to save below
404 lookupFloat[0]->setLabel(saveLabel);
407 lastLabel = lookupFloat[0]->getLabel();
408 //prevent memory leak
409 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; lookupFloat[i] = NULL; }
411 //get next line to process
412 lookupFloat = input->getSharedRAbundFloatVectors();
416 if (m->control_pressed) {
417 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
418 globaldata->Groups.clear();
419 delete read; delete heatmap; return 0;
422 //output error messages about any remaining user labels
423 set<string>::iterator it;
424 bool needToRun = false;
425 for (it = userLabels.begin(); it != userLabels.end(); it++) {
426 m->mothurOut("Your file does not include the label " + *it);
427 if (processedLabels.count(lastLabel) != 1) {
428 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
431 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
435 //run last label if you need to
436 if (needToRun == true) {
437 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }
438 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
440 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
441 string outputFileName = heatmap->getPic(lookupFloat);
442 outputNames.push_back(outputFileName); outputTypes["svg"].push_back(outputFileName);
443 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
446 //reset groups parameter
447 globaldata->Groups.clear();
451 globaldata->rabund = NULL;
452 delete input; globaldata->ginput = NULL;
454 if (m->control_pressed) {
455 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } } outputTypes.clear();
456 delete read; delete heatmap; return 0;
459 m->mothurOutEndLine();
460 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
461 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
462 m->mothurOutEndLine();
469 catch(exception& e) {
470 m->errorOut(e, "HeatMapCommand", "execute");
475 //**********************************************************************************************************************