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();
34 //allow user to run help
35 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
38 //valid paramters for this command
39 string AlignArray[] = {"groups","label", "calc"};
40 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
42 OptionParser parser(option);
43 map<string,string> parameters = parser.getParameters();
45 ValidParameters validParameter;
47 //check to make sure all parameters are valid for command
48 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
49 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
52 //make sure the user has already run the read.otu command
53 if (globaldata->getSharedFile() == "") {
54 mothurOut("You must read a list and a group, or a shared before you can use the heatmap.sim command."); mothurOutEndLine(); abort = true;
57 //check for optional parameter and set defaults
58 // ...at some point should added some additional type checking...
59 label = validParameter.validFile(parameters, "label", false);
60 if (label == "not found") { label = ""; }
62 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
63 else { allLines = 1; }
66 //if the user has not specified any labels use the ones from read.otu
68 allLines = globaldata->allLines;
69 labels = globaldata->labels;
72 calc = validParameter.validFile(parameters, "calc", false);
73 if (calc == "not found") { calc = "jest-thetayc"; }
75 if (calc == "default") { calc = "jest-thetayc"; }
77 splitAtDash(calc, Estimators);
79 groups = validParameter.validFile(parameters, "groups", false);
80 if (groups == "not found") { groups = ""; }
82 splitAtDash(groups, Groups);
83 globaldata->Groups = Groups;
87 validCalculator = new ValidCalculators();
88 heatmap = new HeatMapSim();
91 for (i=0; i<Estimators.size(); i++) {
92 if (validCalculator->isValidCalculator("heat", Estimators[i]) == true) {
93 if (Estimators[i] == "jabund") {
94 heatCalculators.push_back(new JAbund());
95 }else if (Estimators[i] == "sorabund") {
96 heatCalculators.push_back(new SorAbund());
97 }else if (Estimators[i] == "jclass") {
98 heatCalculators.push_back(new Jclass());
99 }else if (Estimators[i] == "sorclass") {
100 heatCalculators.push_back(new SorClass());
101 }else if (Estimators[i] == "jest") {
102 heatCalculators.push_back(new Jest());
103 }else if (Estimators[i] == "sorest") {
104 heatCalculators.push_back(new SorEst());
105 }else if (Estimators[i] == "thetayc") {
106 heatCalculators.push_back(new ThetaYC());
107 }else if (Estimators[i] == "thetan") {
108 heatCalculators.push_back(new ThetaN());
109 }else if (Estimators[i] == "morisitahorn") {
110 heatCalculators.push_back(new MorHorn());
111 }else if (Estimators[i] == "braycurtis") {
112 heatCalculators.push_back(new BrayCurtis());
123 catch(exception& e) {
124 errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
129 //**********************************************************************************************************************
131 void HeatMapSimCommand::help(){
133 mothurOut("The heatmap.sim command can only be executed after a successful read.otu command.\n");
134 mothurOut("The heatmap.sim command parameters are groups, calc and label. No parameters are required.\n");
135 mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
136 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 is also separated by dashes.\n");
137 mothurOut("The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, label=yourLabels).\n");
138 mothurOut("Example heatmap.sim(groups=A-B-C, calc=jabund).\n");
139 mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
140 validCalculator->printCalc("heat", cout);
141 mothurOut("The default value for calc is jclass-thetayc.\n");
142 mothurOut("The heatmap.sim command outputs a .svg file for each calculator you choose at each label you specify.\n");
143 mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
146 catch(exception& e) {
147 errorOut(e, "HeatMapSimCommand", "help");
152 //**********************************************************************************************************************
154 HeatMapSimCommand::~HeatMapSimCommand(){
155 if (abort == false) {
156 delete input; globaldata->ginput = NULL;
159 delete validCalculator;
163 //**********************************************************************************************************************
165 int HeatMapSimCommand::execute(){
168 if (abort == true) { return 0; }
170 //if the users entered no valid calculators don't execute command
171 if (heatCalculators.size() == 0) { mothurOut("No valid calculators."); mothurOutEndLine(); return 0; }
174 read = new ReadOTUFile(globaldata->inputFileName);
175 read->read(&*globaldata);
177 input = globaldata->ginput;
178 lookup = input->getSharedRAbundVectors();
179 string lastLabel = lookup[0]->getLabel();
181 if (lookup.size() < 2) { mothurOut("You have not provided enough valid groups. I cannot run the command."); mothurOutEndLine(); return 0;}
183 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
184 set<string> processedLabels;
185 set<string> userLabels = labels;
187 //as long as you are not at the end of the file or done wih the lines you want
188 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
190 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
192 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
193 heatmap->getPic(lookup, heatCalculators);
195 processedLabels.insert(lookup[0]->getLabel());
196 userLabels.erase(lookup[0]->getLabel());
199 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
201 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
202 lookup = input->getSharedRAbundVectors(lastLabel);
204 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
205 heatmap->getPic(lookup, heatCalculators);
207 processedLabels.insert(lookup[0]->getLabel());
208 userLabels.erase(lookup[0]->getLabel());
211 //prevent memory leak
213 lastLabel = lookup[0]->getLabel();
215 //get next line to process
216 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
217 lookup = input->getSharedRAbundVectors();
220 //output error messages about any remaining user labels
221 set<string>::iterator it;
222 bool needToRun = false;
223 for (it = userLabels.begin(); it != userLabels.end(); it++) {
224 mothurOut("Your file does not include the label " + *it);
225 if (processedLabels.count(lastLabel) != 1) {
226 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
229 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
233 //run last label if you need to
234 if (needToRun == true) {
235 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
236 lookup = input->getSharedRAbundVectors(lastLabel);
238 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
239 heatmap->getPic(lookup, heatCalculators);
240 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
244 //reset groups parameter
245 globaldata->Groups.clear();
249 catch(exception& e) {
250 errorOut(e, "HeatMapSimCommand", "execute");
255 //**********************************************************************************************************************