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();
23 //allow user to run help
24 if(option == "help") { help(); abort = true; }
27 //valid paramters for this command
28 string AlignArray[] = {"groups","line","label","sorted","scale"};
29 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
31 OptionParser parser(option);
32 map<string,string> parameters = parser.getParameters();
34 ValidParameters validParameter;
36 //check to make sure all parameters are valid for command
37 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
38 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
41 //make sure the user has already run the read.otu command
42 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "") && (globaldata->getSharedFile() == "")) {
43 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."); mothurOutEndLine(); abort = true;
46 //check for optional parameter and set defaults
47 // ...at some point should added some additional type checking...
48 line = validParameter.validFile(parameters, "line", false);
49 if (line == "not found") { line = ""; }
51 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
52 else { allLines = 1; }
55 label = validParameter.validFile(parameters, "label", false);
56 if (label == "not found") { label = ""; }
58 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
59 else { allLines = 1; }
62 //make sure user did not use both the line and label parameters
63 if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
64 //if the user has not specified any line or labels use the ones from read.otu
65 else if ((line == "") && (label == "")) {
66 allLines = globaldata->allLines;
67 labels = globaldata->labels;
68 lines = globaldata->lines;
71 groups = validParameter.validFile(parameters, "groups", false);
72 if (groups == "not found") { groups = ""; }
74 splitAtDash(groups, Groups);
75 globaldata->Groups = Groups;
78 sorted = validParameter.validFile(parameters, "sorted", false); if (sorted == "not found") { sorted = "T"; }
80 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "log10"; }
83 heatmap = new HeatMap(sorted, scale);
84 format = globaldata->getFormat();
90 errorOut(e, "HeatMapCommand", "HeatMapCommand");
95 //**********************************************************************************************************************
97 void HeatMapCommand::help(){
99 mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
100 mothurOut("The heatmap.bin command parameters are groups, sorted, scale, line and label. No parameters are required, but you may not use line and label at the same time.\n");
101 mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
102 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");
103 mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
104 mothurOut("The group names are separated by dashes. The line and label allow you to select what distance levels you would like a heatmap created for, and are also separated by dashes.\n");
105 mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, line=yourLines, label=yourLabels).\n");
106 mothurOut("Example heatmap.bin(groups=A-B-C, line=1-3-5, sorted=F, scale=log10).\n");
107 mothurOut("The default value for groups is all the groups in your groupfile, and all lines in your inputfile will be used.\n");
108 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");
109 mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
110 mothurOut("The heatmap.bin command outputs a .svg file for each line or label you specify.\n");
111 mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
114 catch(exception& e) {
115 errorOut(e, "HeatMapCommand", "help");
120 //**********************************************************************************************************************
122 HeatMapCommand::~HeatMapCommand(){
123 if (abort == false) {
129 //**********************************************************************************************************************
131 int HeatMapCommand::execute(){
134 if (abort == true) { return 0; }
139 if (format == "sharedfile") {
141 read = new ReadOTUFile(globaldata->inputFileName);
142 read->read(&*globaldata);
144 input = globaldata->ginput;
145 lookup = input->getSharedRAbundVectors();
146 lastLabel = lookup[0]->getLabel();
148 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
149 //you are using just a list file and have only one group
150 read = new ReadOTUFile(globaldata->inputFileName);
151 read->read(&*globaldata);
153 rabund = globaldata->rabund;
154 lastLabel = rabund->getLabel();
155 input = globaldata->ginput;
159 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
160 set<string> processedLabels;
161 set<string> userLabels = labels;
162 set<int> userLines = lines;
164 if ((format != "list") && (format != "rabund") && (format != "sabund")) {
166 //as long as you are not at the end of the file or done wih the lines you want
167 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
169 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){
171 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
172 heatmap->getPic(lookup);
174 processedLabels.insert(lookup[0]->getLabel());
175 userLabels.erase(lookup[0]->getLabel());
176 userLines.erase(count);
179 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
180 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
181 lookup = input->getSharedRAbundVectors(lastLabel);
182 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
184 heatmap->getPic(lookup);
186 processedLabels.insert(lookup[0]->getLabel());
187 userLabels.erase(lookup[0]->getLabel());
190 lastLabel = lookup[0]->getLabel();
191 //prevent memory leak
192 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
194 //get next line to process
195 lookup = input->getSharedRAbundVectors();
199 //output error messages about any remaining user labels
200 set<string>::iterator it;
201 bool needToRun = false;
202 for (it = userLabels.begin(); it != userLabels.end(); it++) {
203 mothurOut("Your file does not include the label " + *it);
204 if (processedLabels.count(lastLabel) != 1) {
205 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
208 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
212 //run last line if you need to
213 if (needToRun == true) {
214 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
215 lookup = input->getSharedRAbundVectors(lastLabel);
217 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
218 heatmap->getPic(lookup);
219 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
224 //reset groups parameter
225 globaldata->Groups.clear();
229 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
231 if(allLines == 1 || lines.count(count) == 1 || labels.count(rabund->getLabel()) == 1){
233 mothurOut(rabund->getLabel()); mothurOutEndLine();
234 heatmap->getPic(rabund);
236 processedLabels.insert(rabund->getLabel());
237 userLabels.erase(rabund->getLabel());
238 userLines.erase(count);
241 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
244 rabund = input->getRAbundVector(lastLabel);
245 mothurOut(rabund->getLabel()); mothurOutEndLine();
247 heatmap->getPic(rabund);
249 processedLabels.insert(rabund->getLabel());
250 userLabels.erase(rabund->getLabel());
255 lastLabel = rabund->getLabel();
257 rabund = input->getRAbundVector();
261 //output error messages about any remaining user labels
262 set<string>::iterator it;
263 bool needToRun = false;
264 for (it = userLabels.begin(); it != userLabels.end(); it++) {
265 mothurOut("Your file does not include the label " + *it);
266 if (processedLabels.count(lastLabel) != 1) {
267 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
270 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
274 //run last line if you need to
275 if (needToRun == true) {
278 rabund = input->getRAbundVector(lastLabel);
279 mothurOut(rabund->getLabel()); mothurOutEndLine();
281 heatmap->getPic(rabund);
282 delete rabund; globaldata->rabund = NULL;
287 globaldata->rabund = NULL;
288 delete input; globaldata->ginput = NULL;
291 catch(exception& e) {
292 errorOut(e, "HeatMapCommand", "execute");
297 //**********************************************************************************************************************