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"; }
78 heatmap = new HeatMap(sorted, scale, outputDir);
79 format = globaldata->getFormat();
85 m->errorOut(e, "HeatMapCommand", "HeatMapCommand");
90 //**********************************************************************************************************************
92 void HeatMapCommand::help(){
94 m->mothurOut("The heatmap.bin command can only be executed after a successful read.otu command.\n");
95 m->mothurOut("The heatmap.bin command parameters are groups, sorted, scale label. No parameters are required.\n");
96 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
97 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");
98 m->mothurOut("The scale parameter allows you to choose the range of color your bin information will be displayed with.\n");
99 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");
100 m->mothurOut("The heatmap.bin command should be in the following format: heatmap.bin(groups=yourGroups, sorted=yourSorted, label=yourLabels).\n");
101 m->mothurOut("Example heatmap.bin(groups=A-B-C, sorted=F, scale=log10).\n");
102 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
103 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");
104 m->mothurOut("The default value for scale is log10; your other options are log2 and linear.\n");
105 m->mothurOut("The heatmap.bin command outputs a .svg file for each label you specify.\n");
106 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
109 catch(exception& e) {
110 m->errorOut(e, "HeatMapCommand", "help");
115 //**********************************************************************************************************************
117 HeatMapCommand::~HeatMapCommand(){
118 if (abort == false) {
124 //**********************************************************************************************************************
126 int HeatMapCommand::execute(){
129 if (abort == true) { return 0; }
133 if (format == "sharedfile") {
135 read = new ReadOTUFile(globaldata->inputFileName);
136 read->read(&*globaldata);
138 input = globaldata->ginput;
139 lookup = input->getSharedRAbundVectors();
140 lastLabel = lookup[0]->getLabel();
142 }else if ((format == "list") || (format == "rabund") || (format == "sabund")) {
143 //you are using just a list file and have only one group
144 read = new ReadOTUFile(globaldata->inputFileName);
145 read->read(&*globaldata);
147 rabund = globaldata->rabund;
148 lastLabel = rabund->getLabel();
149 input = globaldata->ginput;
153 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
154 set<string> processedLabels;
155 set<string> userLabels = labels;
157 if ((format != "list") && (format != "rabund") && (format != "sabund")) {
159 //as long as you are not at the end of the file or done wih the lines you want
160 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
162 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
164 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
165 outputNames.push_back(heatmap->getPic(lookup));
167 processedLabels.insert(lookup[0]->getLabel());
168 userLabels.erase(lookup[0]->getLabel());
171 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
172 string saveLabel = lookup[0]->getLabel();
174 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
175 lookup = input->getSharedRAbundVectors(lastLabel);
176 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
178 outputNames.push_back(heatmap->getPic(lookup));
180 processedLabels.insert(lookup[0]->getLabel());
181 userLabels.erase(lookup[0]->getLabel());
183 //restore real lastlabel to save below
184 lookup[0]->setLabel(saveLabel);
187 lastLabel = lookup[0]->getLabel();
188 //prevent memory leak
189 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
191 //get next line to process
192 lookup = input->getSharedRAbundVectors();
195 //output error messages about any remaining user labels
196 set<string>::iterator it;
197 bool needToRun = false;
198 for (it = userLabels.begin(); it != userLabels.end(); it++) {
199 m->mothurOut("Your file does not include the label " + *it);
200 if (processedLabels.count(lastLabel) != 1) {
201 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
204 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
208 //run last label if you need to
209 if (needToRun == true) {
210 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
211 lookup = input->getSharedRAbundVectors(lastLabel);
213 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
214 outputNames.push_back(heatmap->getPic(lookup));
215 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
220 //reset groups parameter
221 globaldata->Groups.clear();
225 while((rabund != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
227 if(allLines == 1 || labels.count(rabund->getLabel()) == 1){
229 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
230 outputNames.push_back(heatmap->getPic(rabund));
232 processedLabels.insert(rabund->getLabel());
233 userLabels.erase(rabund->getLabel());
236 if ((anyLabelsToProcess(rabund->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
237 string saveLabel = rabund->getLabel();
240 rabund = input->getRAbundVector(lastLabel);
241 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
243 outputNames.push_back(heatmap->getPic(rabund));
245 processedLabels.insert(rabund->getLabel());
246 userLabels.erase(rabund->getLabel());
248 //restore real lastlabel to save below
249 rabund->setLabel(saveLabel);
254 lastLabel = rabund->getLabel();
256 rabund = input->getRAbundVector();
259 //output error messages about any remaining user labels
260 set<string>::iterator it;
261 bool needToRun = false;
262 for (it = userLabels.begin(); it != userLabels.end(); it++) {
263 m->mothurOut("Your file does not include the label " + *it);
264 if (processedLabels.count(lastLabel) != 1) {
265 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
268 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
272 //run last label if you need to
273 if (needToRun == true) {
275 if (rabund != NULL) { delete rabund; }
276 rabund = input->getRAbundVector(lastLabel);
277 m->mothurOut(rabund->getLabel()); m->mothurOutEndLine();
279 outputNames.push_back(heatmap->getPic(rabund));
280 delete rabund; globaldata->rabund = NULL;
285 globaldata->rabund = NULL;
286 delete input; globaldata->ginput = NULL;
288 m->mothurOutEndLine();
289 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
290 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
291 m->mothurOutEndLine();
295 catch(exception& e) {
296 m->errorOut(e, "HeatMapCommand", "execute");
301 //**********************************************************************************************************************