5 * Created by Sarah Westcott on 3/25/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "heatmapcommand.h"
13 //**********************************************************************************************************************
15 HeatMapCommand::HeatMapCommand(string option) {
17 globaldata = GlobalData::getInstance();
22 //allow user to run help
23 if(option == "help") { help(); abort = true; }
26 //valid paramters for this command
27 string AlignArray[] = {"groups","label","sorted","scale","fontsize","numotu","outputdir","inputdir"};
28 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
30 OptionParser parser(option);
31 map<string,string> parameters = parser.getParameters();
33 ValidParameters validParameter;
35 //check to make sure all parameters are valid for command
36 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
37 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
40 //if the user changes the output directory command factory will send this info to us in the output parameter
41 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
43 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
46 //make sure the user has already run the read.otu command
47 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "") && (globaldata->getSharedFile() == "") && (globaldata->getRelAbundFile() == "")) {
48 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;
51 //check for optional parameter and set defaults
52 // ...at some point should added some additional type checking...
53 label = validParameter.validFile(parameters, "label", false);
54 if (label == "not found") { label = ""; }
56 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
57 else { allLines = 1; }
60 //if the user has not specified any labels use the ones from read.otu
62 allLines = globaldata->allLines;
63 labels = globaldata->labels;
66 groups = validParameter.validFile(parameters, "groups", false);
67 if (groups == "not found") { groups = ""; }
69 m->splitAtDash(groups, Groups);
70 globaldata->Groups = Groups;
73 string temp = validParameter.validFile(parameters, "numotu", false); if (temp == "not found") { temp = "0"; }
74 convert(temp, numOTU);
76 temp = validParameter.validFile(parameters, "fontsize", false); if (temp == "not found") { temp = "24"; }
77 convert(temp, fontSize);
79 sorted = validParameter.validFile(parameters, "sorted", false);
80 if (sorted == "not found") {
81 //if numOTU is used change default
82 if (numOTU != 0) { sorted = "topotu"; }
83 else { sorted = "shared"; }
86 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "log10"; }
88 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; }
93 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
98 //**********************************************************************************************************************
100 void HeatMapCommand::help(){
102 m->mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
103 m->mothurOut("The heatmap.bin command parameters are groups, sorted, scale, numotu, fontsize and label. No parameters are required.\n");
104 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
105 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");
106 m->mothurOut("topotu, meaning the otus with the greatest abundance when totaled across groups, topgroup, meaning the top otus for each group. \n");
107 m->mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
108 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");
109 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");
110 m->mothurOut("The fontsize parameter allows you to adjust the font size of the picture created, default=24.\n");
111 m->mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, label=yourLabels).\n");
112 m->mothurOut("Example heatmap.bin(groups=A-B-C, sorted=none, scale=log10).\n");
113 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
114 m->mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
115 m->mothurOut("The heatmap.bin command outputs a .svg file for each label you specify.\n");
116 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
119 catch(exception& e) {
120 m->errorOut(e, "HeatMapCommand", "help");
125 //**********************************************************************************************************************
127 HeatMapCommand::~HeatMapCommand(){
130 //**********************************************************************************************************************
132 int HeatMapCommand::execute(){
135 if (abort == true) { return 0; }
137 heatmap = new HeatMap(sorted, scale, numOTU, fontSize, outputDir);
138 format = globaldata->getFormat();
142 read = new ReadOTUFile(globaldata->inputFileName);
143 read->read(&*globaldata);
144 input = globaldata->ginput;
146 if (format == "sharedfile") {
148 lookup = input->getSharedRAbundVectors();
149 lastLabel = lookup[0]->getLabel();
151 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
152 //you are using just a list file and have only one group
153 rabund = globaldata->rabund;
154 lastLabel = rabund->getLabel();
155 }else if (format == "relabund") {
157 lookupFloat = input->getSharedRAbundFloatVectors();
158 lastLabel = lookupFloat[0]->getLabel();
161 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
162 set<string> processedLabels;
163 set<string> userLabels = labels;
165 if (format == "sharedfile") {
167 //as long as you are not at the end of the file or done wih the lines you want
168 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
169 if (m->control_pressed) {
170 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
171 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
172 globaldata->Groups.clear();
173 delete read; delete heatmap; return 0;
176 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
178 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
179 outputNames.push_back(heatmap->getPic(lookup));
181 processedLabels.insert(lookup[0]->getLabel());
182 userLabels.erase(lookup[0]->getLabel());
185 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
186 string saveLabel = lookup[0]->getLabel();
188 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
190 lookup = input->getSharedRAbundVectors(lastLabel);
191 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
193 outputNames.push_back(heatmap->getPic(lookup));
195 processedLabels.insert(lookup[0]->getLabel());
196 userLabels.erase(lookup[0]->getLabel());
198 //restore real lastlabel to save below
199 lookup[0]->setLabel(saveLabel);
202 lastLabel = lookup[0]->getLabel();
203 //prevent memory leak
204 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
206 //get next line to process
207 lookup = input->getSharedRAbundVectors();
211 if (m->control_pressed) {
212 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
213 globaldata->Groups.clear();
214 delete read; delete heatmap; return 0;
217 //output error messages about any remaining user labels
218 set<string>::iterator it;
219 bool needToRun = false;
220 for (it = userLabels.begin(); it != userLabels.end(); it++) {
221 m->mothurOut("Your file does not include the label " + *it);
222 if (processedLabels.count(lastLabel) != 1) {
223 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
226 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
230 //run last label if you need to
231 if (needToRun == true) {
232 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
233 lookup = input->getSharedRAbundVectors(lastLabel);
235 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
236 outputNames.push_back(heatmap->getPic(lookup));
237 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
240 //reset groups parameter
241 globaldata->Groups.clear();
243 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
245 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
246 if (m->control_pressed) {
247 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
248 delete rabund; delete read; delete heatmap; return 0;
251 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
253 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
254 outputNames.push_back(heatmap->getPic(rabund));
256 processedLabels.insert(rabund->getLabel());
257 userLabels.erase(rabund->getLabel());
260 if ((m->anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
261 string saveLabel = rabund->getLabel();
264 rabund = input->getRAbundVector(lastLabel);
265 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
267 outputNames.push_back(heatmap->getPic(rabund));
269 processedLabels.insert(rabund->getLabel());
270 userLabels.erase(rabund->getLabel());
272 //restore real lastlabel to save below
273 rabund->setLabel(saveLabel);
278 lastLabel = rabund->getLabel();
280 rabund = input->getRAbundVector();
283 if (m->control_pressed) {
284 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
285 delete read; delete heatmap; return 0;
288 //output error messages about any remaining user labels
289 set<string>::iterator it;
290 bool needToRun = false;
291 for (it = userLabels.begin(); it != userLabels.end(); it++) {
292 m->mothurOut("Your file does not include the label " + *it);
293 if (processedLabels.count(lastLabel) != 1) {
294 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
297 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
301 //run last label if you need to
302 if (needToRun == true) {
304 if (rabund != NULL) { delete rabund; }
305 rabund = input->getRAbundVector(lastLabel);
306 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
308 outputNames.push_back(heatmap->getPic(rabund));
309 delete rabund; globaldata->rabund = NULL;
314 //as long as you are not at the end of the file or done wih the lines you want
315 while((lookupFloat[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
316 if (m->control_pressed) {
317 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
318 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
319 globaldata->Groups.clear();
320 delete read; delete heatmap; return 0;
323 if(allLines == 1 || labels.count(lookupFloat[0]->getLabel()) == 1){
325 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
326 outputNames.push_back(heatmap->getPic(lookupFloat));
328 processedLabels.insert(lookupFloat[0]->getLabel());
329 userLabels.erase(lookupFloat[0]->getLabel());
332 if ((m->anyLabelsToProcess(lookupFloat[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
333 string saveLabel = lookupFloat[0]->getLabel();
335 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
336 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
337 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
339 outputNames.push_back(heatmap->getPic(lookupFloat));
341 processedLabels.insert(lookupFloat[0]->getLabel());
342 userLabels.erase(lookupFloat[0]->getLabel());
344 //restore real lastlabel to save below
345 lookupFloat[0]->setLabel(saveLabel);
348 lastLabel = lookupFloat[0]->getLabel();
349 //prevent memory leak
350 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; lookupFloat[i] = NULL; }
352 //get next line to process
353 lookupFloat = input->getSharedRAbundFloatVectors();
357 if (m->control_pressed) {
358 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
359 globaldata->Groups.clear();
360 delete read; delete heatmap; return 0;
363 //output error messages about any remaining user labels
364 set<string>::iterator it;
365 bool needToRun = false;
366 for (it = userLabels.begin(); it != userLabels.end(); it++) {
367 m->mothurOut("Your file does not include the label " + *it);
368 if (processedLabels.count(lastLabel) != 1) {
369 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
372 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
376 //run last label if you need to
377 if (needToRun == true) {
378 for (int i = 0; i < lookupFloat.size(); i++) { if (lookupFloat[i] != NULL) { delete lookupFloat[i]; } }
379 lookupFloat = input->getSharedRAbundFloatVectors(lastLabel);
381 m->mothurOut(lookupFloat[0]->getLabel()); m->mothurOutEndLine();
382 outputNames.push_back(heatmap->getPic(lookupFloat));
383 for (int i = 0; i < lookupFloat.size(); i++) { delete lookupFloat[i]; }
386 //reset groups parameter
387 globaldata->Groups.clear();
391 globaldata->rabund = NULL;
392 delete input; globaldata->ginput = NULL;
394 if (m->control_pressed) {
395 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
396 delete read; delete heatmap; return 0;
399 m->mothurOutEndLine();
400 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
401 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
402 m->mothurOutEndLine();
409 catch(exception& e) {
410 m->errorOut(e, "HeatMapCommand", "execute");
415 //**********************************************************************************************************************