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 += 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() == "")) {
48 m->mothurOut("You must read a list, rabund, sabund, or a list and a group, or a shared 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") { 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 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=F, 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();
157 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
158 set<string> processedLabels;
159 set<string> userLabels = labels;
161 if ((format != "list") && (format != "rabund") && (format != "sabund")) {
163 //as long as you are not at the end of the file or done wih the lines you want
164 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
165 if (m->control_pressed) {
166 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
167 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
168 globaldata->Groups.clear();
169 delete read; delete heatmap; return 0;
172 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
174 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
175 outputNames.push_back(heatmap->getPic(lookup));
177 processedLabels.insert(lookup[0]->getLabel());
178 userLabels.erase(lookup[0]->getLabel());
181 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
182 string saveLabel = lookup[0]->getLabel();
184 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
185 lookup = input->getSharedRAbundVectors(lastLabel);
186 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
188 outputNames.push_back(heatmap->getPic(lookup));
190 processedLabels.insert(lookup[0]->getLabel());
191 userLabels.erase(lookup[0]->getLabel());
193 //restore real lastlabel to save below
194 lookup[0]->setLabel(saveLabel);
197 lastLabel = lookup[0]->getLabel();
198 //prevent memory leak
199 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
201 //get next line to process
202 lookup = input->getSharedRAbundVectors();
206 if (m->control_pressed) {
207 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
208 globaldata->Groups.clear();
209 delete read; delete heatmap; return 0;
212 //output error messages about any remaining user labels
213 set<string>::iterator it;
214 bool needToRun = false;
215 for (it = userLabels.begin(); it != userLabels.end(); it++) {
216 m->mothurOut("Your file does not include the label " + *it);
217 if (processedLabels.count(lastLabel) != 1) {
218 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
221 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
225 //run last label if you need to
226 if (needToRun == true) {
227 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
228 lookup = input->getSharedRAbundVectors(lastLabel);
230 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
231 outputNames.push_back(heatmap->getPic(lookup));
232 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
235 //reset groups parameter
236 globaldata->Groups.clear();
240 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
241 if (m->control_pressed) {
242 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
243 delete rabund; delete read; delete heatmap; return 0;
246 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
248 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
249 outputNames.push_back(heatmap->getPic(rabund));
251 processedLabels.insert(rabund->getLabel());
252 userLabels.erase(rabund->getLabel());
255 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
256 string saveLabel = rabund->getLabel();
259 rabund = input->getRAbundVector(lastLabel);
260 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
262 outputNames.push_back(heatmap->getPic(rabund));
264 processedLabels.insert(rabund->getLabel());
265 userLabels.erase(rabund->getLabel());
267 //restore real lastlabel to save below
268 rabund->setLabel(saveLabel);
273 lastLabel = rabund->getLabel();
275 rabund = input->getRAbundVector();
278 if (m->control_pressed) {
279 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
280 delete read; delete heatmap; return 0;
283 //output error messages about any remaining user labels
284 set<string>::iterator it;
285 bool needToRun = false;
286 for (it = userLabels.begin(); it != userLabels.end(); it++) {
287 m->mothurOut("Your file does not include the label " + *it);
288 if (processedLabels.count(lastLabel) != 1) {
289 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
292 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
296 //run last label if you need to
297 if (needToRun == true) {
299 if (rabund != NULL) { delete rabund; }
300 rabund = input->getRAbundVector(lastLabel);
301 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
303 outputNames.push_back(heatmap->getPic(rabund));
304 delete rabund; globaldata->rabund = NULL;
309 globaldata->rabund = NULL;
310 delete input; globaldata->ginput = NULL;
312 if (m->control_pressed) {
313 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
314 delete read; delete heatmap; return 0;
317 m->mothurOutEndLine();
318 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
319 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
320 m->mothurOutEndLine();
327 catch(exception& e) {
328 m->errorOut(e, "HeatMapCommand", "execute");
333 //**********************************************************************************************************************