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","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 sorted = validParameter.validFile(parameters, "sorted", false); if (sorted == "not found") { sorted = "T"; }
75 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "log10"; }
81 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
86 //**********************************************************************************************************************
88 void HeatMapCommand::help(){
90 m->mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
91 m->mothurOut("The heatmap.bin command parameters are groups, sorted, scale label. No parameters are required.\n");
92 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
93 m->mothurOut("The sorted parameter allows you to choose to see the file with the shared otus at the top or the otus in the order they appear in your input file. \n");
94 m->mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
95 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");
96 m->mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, label=yourLabels).\n");
97 m->mothurOut("Example heatmap.bin(groups=A-B-C, sorted=F, scale=log10).\n");
98 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
99 m->mothurOut("The default value for sorted is T meaning you want the shared otus on top, you may change it to F meaning the exact representation of your input file.\n");
100 m->mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
101 m->mothurOut("The heatmap.bin command outputs a .svg file for each label you specify.\n");
102 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
105 catch(exception& e) {
106 m->errorOut(e, "HeatMapCommand", "help");
111 //**********************************************************************************************************************
113 HeatMapCommand::~HeatMapCommand(){
116 //**********************************************************************************************************************
118 int HeatMapCommand::execute(){
121 if (abort == true) { return 0; }
123 heatmap = new HeatMap(sorted, scale, outputDir);
124 format = globaldata->getFormat();
128 read = new ReadOTUFile(globaldata->inputFileName);
129 read->read(&*globaldata);
130 input = globaldata->ginput;
132 if (format == "sharedfile") {
134 lookup = input->getSharedRAbundVectors();
135 lastLabel = lookup[0]->getLabel();
137 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
138 //you are using just a list file and have only one group
139 rabund = globaldata->rabund;
140 lastLabel = rabund->getLabel();
143 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
144 set<string> processedLabels;
145 set<string> userLabels = labels;
147 if ((format != "list") && (format != "rabund") && (format != "sabund")) {
149 //as long as you are not at the end of the file or done wih the lines you want
150 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
151 if (m->control_pressed) {
152 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
153 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
154 globaldata->Groups.clear();
155 delete read; delete heatmap; return 0;
158 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
160 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
161 outputNames.push_back(heatmap->getPic(lookup));
163 processedLabels.insert(lookup[0]->getLabel());
164 userLabels.erase(lookup[0]->getLabel());
167 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
168 string saveLabel = lookup[0]->getLabel();
170 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
171 lookup = input->getSharedRAbundVectors(lastLabel);
172 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
174 outputNames.push_back(heatmap->getPic(lookup));
176 processedLabels.insert(lookup[0]->getLabel());
177 userLabels.erase(lookup[0]->getLabel());
179 //restore real lastlabel to save below
180 lookup[0]->setLabel(saveLabel);
183 lastLabel = lookup[0]->getLabel();
184 //prevent memory leak
185 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; }
187 //get next line to process
188 lookup = input->getSharedRAbundVectors();
192 if (m->control_pressed) {
193 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
194 globaldata->Groups.clear();
195 delete read; delete heatmap; return 0;
198 //output error messages about any remaining user labels
199 set<string>::iterator it;
200 bool needToRun = false;
201 for (it = userLabels.begin(); it != userLabels.end(); it++) {
202 m->mothurOut("Your file does not include the label " + *it);
203 if (processedLabels.count(lastLabel) != 1) {
204 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
207 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
211 //run last label if you need to
212 if (needToRun == true) {
213 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
214 lookup = input->getSharedRAbundVectors(lastLabel);
216 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
217 outputNames.push_back(heatmap->getPic(lookup));
218 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
221 //reset groups parameter
222 globaldata->Groups.clear();
226 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
227 if (m->control_pressed) {
228 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
229 delete rabund; delete read; delete heatmap; return 0;
232 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
234 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
235 outputNames.push_back(heatmap->getPic(rabund));
237 processedLabels.insert(rabund->getLabel());
238 userLabels.erase(rabund->getLabel());
241 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
242 string saveLabel = rabund->getLabel();
245 rabund = input->getRAbundVector(lastLabel);
246 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
248 outputNames.push_back(heatmap->getPic(rabund));
250 processedLabels.insert(rabund->getLabel());
251 userLabels.erase(rabund->getLabel());
253 //restore real lastlabel to save below
254 rabund->setLabel(saveLabel);
259 lastLabel = rabund->getLabel();
261 rabund = input->getRAbundVector();
264 if (m->control_pressed) {
265 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
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) {
285 if (rabund != NULL) { delete rabund; }
286 rabund = input->getRAbundVector(lastLabel);
287 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
289 outputNames.push_back(heatmap->getPic(rabund));
290 delete rabund; globaldata->rabund = NULL;
295 globaldata->rabund = NULL;
296 delete input; globaldata->ginput = NULL;
298 if (m->control_pressed) {
299 for (int i = 0; i < outputNames.size(); i++) { if (outputNames[i] != "control") { remove(outputNames[i].c_str()); } }
300 delete read; delete heatmap; return 0;
303 m->mothurOutEndLine();
304 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
305 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
306 m->mothurOutEndLine();
313 catch(exception& e) {
314 m->errorOut(e, "HeatMapCommand", "execute");
319 //**********************************************************************************************************************