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 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
167 lookup = input->getSharedRAbundVectors(lastLabel);
168 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
170 heatmap->getPic(lookup);
172 processedLabels.insert(lookup[0]->getLabel());
173 userLabels.erase(lookup[0]->getLabel());
176 lastLabel = lookup[0]->getLabel();
177 //prevent memory leak
178 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
180 //get next line to process
181 lookup = input->getSharedRAbundVectors();
184 //output error messages about any remaining user labels
185 set<string>::iterator it;
186 bool needToRun = false;
187 for (it = userLabels.begin(); it != userLabels.end(); it++) {
188 mothurOut("Your file does not include the label " + *it);
189 if (processedLabels.count(lastLabel) != 1) {
190 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
193 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
197 //run last label if you need to
198 if (needToRun == true) {
199 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
200 lookup = input->getSharedRAbundVectors(lastLabel);
202 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
203 heatmap->getPic(lookup);
204 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
209 //reset groups parameter
210 globaldata->Groups.clear();
214 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
216 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
218 mothurOut(rabund->getLabel()); mothurOutEndLine();
219 heatmap->getPic(rabund);
221 processedLabels.insert(rabund->getLabel());
222 userLabels.erase(rabund->getLabel());
225 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
228 rabund = input->getRAbundVector(lastLabel);
229 mothurOut(rabund->getLabel()); mothurOutEndLine();
231 heatmap->getPic(rabund);
233 processedLabels.insert(rabund->getLabel());
234 userLabels.erase(rabund->getLabel());
239 lastLabel = rabund->getLabel();
241 rabund = input->getRAbundVector();
244 //output error messages about any remaining user labels
245 set<string>::iterator it;
246 bool needToRun = false;
247 for (it = userLabels.begin(); it != userLabels.end(); it++) {
248 mothurOut("Your file does not include the label " + *it);
249 if (processedLabels.count(lastLabel) != 1) {
250 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
253 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
257 //run last label if you need to
258 if (needToRun == true) {
260 if (rabund != NULL) { delete rabund; }
261 rabund = input->getRAbundVector(lastLabel);
262 mothurOut(rabund->getLabel()); mothurOutEndLine();
264 heatmap->getPic(rabund);
265 delete rabund; globaldata->rabund = NULL;
270 globaldata->rabund = NULL;
271 delete input; globaldata->ginput = NULL;
274 catch(exception& e) {
275 errorOut(e, "HeatMapCommand", "execute");
280 //**********************************************************************************************************************