2 * heatmapsimcommand.cpp
5 * Created by Sarah Westcott on 6/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "heatmapsimcommand.h"
11 #include "sharedjabund.h"
12 #include "sharedsorabund.h"
13 #include "sharedjclass.h"
14 #include "sharedsorclass.h"
15 #include "sharedjest.h"
16 #include "sharedsorest.h"
17 #include "sharedthetayc.h"
18 #include "sharedthetan.h"
19 #include "sharedmorisitahorn.h"
20 #include "sharedbraycurtis.h"
23 //**********************************************************************************************************************
25 HeatMapSimCommand::HeatMapSimCommand(string option){
27 globaldata = GlobalData::getInstance();
35 //allow user to run help
36 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
39 //valid paramters for this command
40 string AlignArray[] = {"groups","line","label", "calc"};
41 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
43 OptionParser parser(option);
44 map<string,string> parameters = parser.getParameters();
46 ValidParameters validParameter;
48 //check to make sure all parameters are valid for command
49 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
50 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
53 //make sure the user has already run the read.otu command
54 if (globaldata->getSharedFile() == "") {
55 mothurOut("You must read a list and a group, or a shared before you can use the heatmap.sim command."); mothurOutEndLine(); abort = true;
58 //check for optional parameter and set defaults
59 // ...at some point should added some additional type checking...
60 line = validParameter.validFile(parameters, "line", false);
61 if (line == "not found") { line = ""; }
63 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
64 else { allLines = 1; }
67 label = validParameter.validFile(parameters, "label", false);
68 if (label == "not found") { label = ""; }
70 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
71 else { allLines = 1; }
74 //make sure user did not use both the line and label parameters
75 if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
76 //if the user has not specified any line or labels use the ones from read.otu
77 else if ((line == "") && (label == "")) {
78 allLines = globaldata->allLines;
79 labels = globaldata->labels;
80 lines = globaldata->lines;
83 calc = validParameter.validFile(parameters, "calc", false);
84 if (calc == "not found") { calc = "jest-thetayc"; }
86 if (calc == "default") { calc = "jest-thetayc"; }
88 splitAtDash(calc, Estimators);
90 groups = validParameter.validFile(parameters, "groups", false);
91 if (groups == "not found") { groups = ""; }
93 splitAtDash(groups, Groups);
94 globaldata->Groups = Groups;
98 validCalculator = new ValidCalculators();
99 heatmap = new HeatMapSim();
102 for (i=0; i<Estimators.size(); i++) {
103 if (validCalculator->isValidCalculator("heat", Estimators[i]) == true) {
104 if (Estimators[i] == "jabund") {
105 heatCalculators.push_back(new JAbund());
106 }else if (Estimators[i] == "sorabund") {
107 heatCalculators.push_back(new SorAbund());
108 }else if (Estimators[i] == "jclass") {
109 heatCalculators.push_back(new Jclass());
110 }else if (Estimators[i] == "sorclass") {
111 heatCalculators.push_back(new SorClass());
112 }else if (Estimators[i] == "jest") {
113 heatCalculators.push_back(new Jest());
114 }else if (Estimators[i] == "sorest") {
115 heatCalculators.push_back(new SorEst());
116 }else if (Estimators[i] == "thetayc") {
117 heatCalculators.push_back(new ThetaYC());
118 }else if (Estimators[i] == "thetan") {
119 heatCalculators.push_back(new ThetaN());
120 }else if (Estimators[i] == "morisitahorn") {
121 heatCalculators.push_back(new MorHorn());
122 }else if (Estimators[i] == "braycurtis") {
123 heatCalculators.push_back(new BrayCurtis());
134 catch(exception& e) {
135 errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
140 //**********************************************************************************************************************
142 void HeatMapSimCommand::help(){
144 mothurOut("The heatmap.sim command can only be executed after a successful read.otu command.\n");
145 mothurOut("The heatmap.sim command parameters are groups, calc, line and label. No parameters are required, but you may not use line and label at the same time.\n");
146 mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
147 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");
148 mothurOut("The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, line=yourLines, label=yourLabels).\n");
149 mothurOut("Example heatmap.sim(groups=A-B-C, line=1-3-5, calc=jabund).\n");
150 mothurOut("The default value for groups is all the groups in your groupfile, and all lines in your inputfile will be used.\n");
151 validCalculator->printCalc("heat", cout);
152 mothurOut("The default value for calc is jclass-thetayc.\n");
153 mothurOut("The heatmap.sim command outputs a .svg file for each calculator you choose at each line or label you specify.\n");
154 mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
157 catch(exception& e) {
158 errorOut(e, "HeatMapSimCommand", "help");
163 //**********************************************************************************************************************
165 HeatMapSimCommand::~HeatMapSimCommand(){
166 if (abort == false) {
167 delete input; globaldata->ginput = NULL;
170 delete validCalculator;
174 //**********************************************************************************************************************
176 int HeatMapSimCommand::execute(){
179 if (abort == true) { return 0; }
183 //if the users entered no valid calculators don't execute command
184 if (heatCalculators.size() == 0) { mothurOut("No valid calculators."); mothurOutEndLine(); return 0; }
187 read = new ReadOTUFile(globaldata->inputFileName);
188 read->read(&*globaldata);
190 input = globaldata->ginput;
191 lookup = input->getSharedRAbundVectors();
192 string lastLabel = lookup[0]->getLabel();
194 if (lookup.size() < 2) { mothurOut("You have not provided enough valid groups. I cannot run the command."); mothurOutEndLine(); return 0;}
196 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
197 set<string> processedLabels;
198 set<string> userLabels = labels;
199 set<int> userLines = lines;
202 //as long as you are not at the end of the file or done wih the lines you want
203 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
205 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){
207 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
208 heatmap->getPic(lookup, heatCalculators);
210 processedLabels.insert(lookup[0]->getLabel());
211 userLabels.erase(lookup[0]->getLabel());
212 userLines.erase(count);
215 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
217 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
218 lookup = input->getSharedRAbundVectors(lastLabel);
220 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
221 heatmap->getPic(lookup, heatCalculators);
223 processedLabels.insert(lookup[0]->getLabel());
224 userLabels.erase(lookup[0]->getLabel());
227 //prevent memory leak
229 lastLabel = lookup[0]->getLabel();
231 //get next line to process
232 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
233 lookup = input->getSharedRAbundVectors();
237 //output error messages about any remaining user labels
238 set<string>::iterator it;
239 bool needToRun = false;
240 for (it = userLabels.begin(); it != userLabels.end(); it++) {
241 mothurOut("Your file does not include the label " + *it);
242 if (processedLabels.count(lastLabel) != 1) {
243 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
246 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
250 //run last line if you need to
251 if (needToRun == true) {
252 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
253 lookup = input->getSharedRAbundVectors(lastLabel);
255 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
256 heatmap->getPic(lookup, heatCalculators);
257 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
261 //reset groups parameter
262 globaldata->Groups.clear();
266 catch(exception& e) {
267 errorOut(e, "HeatMapSimCommand", "execute");
272 //**********************************************************************************************************************