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"};
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 //make sure the user has already run the read.otu command
41 if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "") && (globaldata->getSharedFile() == "")) {
42 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;
45 //check for optional parameter and set defaults
46 // ...at some point should added some additional type checking...
47 label = validParameter.validFile(parameters, "label", false);
48 if (label == "not found") { label = ""; }
50 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
51 else { allLines = 1; }
54 //if the user has not specified any labels use the ones from read.otu
56 allLines = globaldata->allLines;
57 labels = globaldata->labels;
60 groups = validParameter.validFile(parameters, "groups", false);
61 if (groups == "not found") { groups = ""; }
63 splitAtDash(groups, Groups);
64 globaldata->Groups = Groups;
67 sorted = validParameter.validFile(parameters, "sorted", false); if (sorted == "not found") { sorted = "T"; }
69 scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "log10"; }
72 heatmap = new HeatMap(sorted, scale);
73 format = globaldata->getFormat();
79 errorOut(e, "HeatMapCommand", "HeatMapCommand");
84 //**********************************************************************************************************************
86 void HeatMapCommand::help(){
88 mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
89 mothurOut("The heatmap.bin command parameters are groups, sorted, scale label. No parameters are required.\n");
90 mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
91 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");
92 mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
93 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");
94 mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, label=yourLabels).\n");
95 mothurOut("Example heatmap.bin(groups=A-B-C, sorted=F, scale=log10).\n");
96 mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
97 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");
98 mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
99 mothurOut("The heatmap.bin command outputs a .svg file for each label you specify.\n");
100 mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
103 catch(exception& e) {
104 errorOut(e, "HeatMapCommand", "help");
109 //**********************************************************************************************************************
111 HeatMapCommand::~HeatMapCommand(){
112 if (abort == false) {
118 //**********************************************************************************************************************
120 int HeatMapCommand::execute(){
123 if (abort == true) { return 0; }
127 if (format == "sharedfile") {
129 read = new ReadOTUFile(globaldata->inputFileName);
130 read->read(&*globaldata);
132 input = globaldata->ginput;
133 lookup = input->getSharedRAbundVectors();
134 lastLabel = lookup[0]->getLabel();
136 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
137 //you are using just a list file and have only one group
138 read = new ReadOTUFile(globaldata->inputFileName);
139 read->read(&*globaldata);
141 rabund = globaldata->rabund;
142 lastLabel = rabund->getLabel();
143 input = globaldata->ginput;
147 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
148 set<string> processedLabels;
149 set<string> userLabels = labels;
151 if ((format != "list") && (format != "rabund") && (format != "sabund")) {
153 //as long as you are not at the end of the file or done wih the lines you want
154 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
156 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
158 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
159 heatmap->getPic(lookup);
161 processedLabels.insert(lookup[0]->getLabel());
162 userLabels.erase(lookup[0]->getLabel());
165 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
166 string saveLabel = lookup[0]->getLabel();
168 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
169 lookup = input->getSharedRAbundVectors(lastLabel);
170 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
172 heatmap->getPic(lookup);
174 processedLabels.insert(lookup[0]->getLabel());
175 userLabels.erase(lookup[0]->getLabel());
177 //restore real lastlabel to save below
178 lookup[0]->setLabel(saveLabel);
181 lastLabel = lookup[0]->getLabel();
182 //prevent memory leak
183 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
185 //get next line to process
186 lookup = input->getSharedRAbundVectors();
189 //output error messages about any remaining user labels
190 set<string>::iterator it;
191 bool needToRun = false;
192 for (it = userLabels.begin(); it != userLabels.end(); it++) {
193 mothurOut("Your file does not include the label " + *it);
194 if (processedLabels.count(lastLabel) != 1) {
195 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
198 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
202 //run last label if you need to
203 if (needToRun == true) {
204 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
205 lookup = input->getSharedRAbundVectors(lastLabel);
207 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
208 heatmap->getPic(lookup);
209 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
214 //reset groups parameter
215 globaldata->Groups.clear();
219 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
221 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
223 mothurOut(rabund->getLabel()); mothurOutEndLine();
224 heatmap->getPic(rabund);
226 processedLabels.insert(rabund->getLabel());
227 userLabels.erase(rabund->getLabel());
230 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
231 string saveLabel = rabund->getLabel();
234 rabund = input->getRAbundVector(lastLabel);
235 mothurOut(rabund->getLabel()); mothurOutEndLine();
237 heatmap->getPic(rabund);
239 processedLabels.insert(rabund->getLabel());
240 userLabels.erase(rabund->getLabel());
242 //restore real lastlabel to save below
243 rabund->setLabel(saveLabel);
248 lastLabel = rabund->getLabel();
250 rabund = input->getRAbundVector();
253 //output error messages about any remaining user labels
254 set<string>::iterator it;
255 bool needToRun = false;
256 for (it = userLabels.begin(); it != userLabels.end(); it++) {
257 mothurOut("Your file does not include the label " + *it);
258 if (processedLabels.count(lastLabel) != 1) {
259 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
262 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
266 //run last label if you need to
267 if (needToRun == true) {
269 if (rabund != NULL) { delete rabund; }
270 rabund = input->getRAbundVector(lastLabel);
271 mothurOut(rabund->getLabel()); mothurOutEndLine();
273 heatmap->getPic(rabund);
274 delete rabund; globaldata->rabund = NULL;
279 globaldata->rabund = NULL;
280 delete input; globaldata->ginput = NULL;
283 catch(exception& e) {
284 errorOut(e, "HeatMapCommand", "execute");
289 //**********************************************************************************************************************