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 //**********************************************************************************************************************
24 vector<string> HeatMapSimCommand::setParameters(){
26 CommandParameter pshared("shared", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none","svg",false,false,true); parameters.push_back(pshared);
27 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none","svg",false,false); parameters.push_back(pphylip);
28 CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none","",false,false); parameters.push_back(pname);
29 CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none","",false,false); parameters.push_back(pcount);
30 CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "ColumnName","svg",false,false); parameters.push_back(pcolumn);
31 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
32 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
33 CommandParameter pcalc("calc", "Multiple", "jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan-morisitahorn-braycurtis", "jest-thetayc", "", "", "","",true,false); parameters.push_back(pcalc);
34 CommandParameter pfontsize("fontsize", "Number", "", "24", "", "", "","",false,false); parameters.push_back(pfontsize);
35 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
36 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
38 vector<string> myArray;
39 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
43 m->errorOut(e, "HeatMapSimCommand", "setParameters");
47 //**********************************************************************************************************************
48 string HeatMapSimCommand::getHelpString(){
50 string helpString = "";
51 ValidCalculators validCalculator;
52 helpString += "The heatmap.sim command parameters are shared, phylip, column, name, count, groups, calc, fontsize and label. shared or phylip or column and name are required unless valid current files exist.\n";
53 helpString += "There are two ways to use the heatmap.sim command. The first is with a shared file, and you may use the groups, label and calc parameter. \n";
54 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n";
55 helpString += "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";
56 helpString += "The fontsize parameter allows you to adjust the font size of the picture created, default=24.\n";
57 helpString += "The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, label=yourLabels).\n";
58 helpString += "Example heatmap.sim(groups=A-B-C, calc=jabund).\n";
59 helpString += "The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n";
60 helpString += validCalculator.printCalc("heat");
61 helpString += "The default value for calc is jclass-thetayc.\n";
62 helpString += "The heatmap.sim command outputs a .svg file for each calculator you choose at each label you specify.\n";
63 helpString += "The second way to use the heatmap.sim command is with a distance file representing the distance bewteen your groups. \n";
64 helpString += "Using the command this way, the phylip or column parameter are required, and only one may be used. If you use a column file the name filename is required. \n";
65 helpString += "The heatmap.sim command should be in the following format: heatmap.sim(phylip=yourDistanceFile).\n";
66 helpString += "Example heatmap.sim(phylip=amazonGroups.dist).\n";
67 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
71 m->errorOut(e, "HeatMapSimCommand", "getHelpString");
75 //**********************************************************************************************************************
76 string HeatMapSimCommand::getOutputPattern(string type) {
80 if (type == "svg") { pattern = "[filename],svg"; }
81 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
86 m->errorOut(e, "HeatMapCommand", "getOutputPattern");
90 //**********************************************************************************************************************
91 HeatMapSimCommand::HeatMapSimCommand(){
93 abort = true; calledHelp = true;
95 vector<string> tempOutNames;
96 outputTypes["svg"] = tempOutNames;
99 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
103 //**********************************************************************************************************************
105 HeatMapSimCommand::HeatMapSimCommand(string option) {
107 abort = false; calledHelp = false;
110 //allow user to run help
111 if(option == "help") { help(); abort = true; calledHelp = true; }
112 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
115 vector<string> myArray = setParameters();
117 OptionParser parser(option);
118 map<string,string> parameters = parser.getParameters();
120 ValidParameters validParameter;
121 map<string,string>::iterator it;
123 //check to make sure all parameters are valid for command
124 for (it = parameters.begin(); it != parameters.end(); it++) {
125 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
128 //initialize outputTypes
129 vector<string> tempOutNames;
130 outputTypes["svg"] = tempOutNames;
134 //if the user changes the input directory command factory will send this info to us in the output parameter
135 string inputDir = validParameter.validFile(parameters, "inputdir", false);
136 if (inputDir == "not found"){ inputDir = ""; }
139 it = parameters.find("phylip");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["phylip"] = inputDir + it->second; }
147 it = parameters.find("column");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["column"] = inputDir + it->second; }
155 it = parameters.find("name");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["name"] = inputDir + it->second; }
163 it = parameters.find("shared");
164 //user has given a template file
165 if(it != parameters.end()){
166 path = m->hasPath(it->second);
167 //if the user has not given a path then, add inputdir. else leave path alone.
168 if (path == "") { parameters["shared"] = inputDir + it->second; }
171 it = parameters.find("count");
172 //user has given a template file
173 if(it != parameters.end()){
174 path = m->hasPath(it->second);
175 //if the user has not given a path then, add inputdir. else leave path alone.
176 if (path == "") { parameters["count"] = inputDir + it->second; }
180 //required parameters
181 phylipfile = validParameter.validFile(parameters, "phylip", true);
182 if (phylipfile == "not open") { abort = true; }
183 else if (phylipfile == "not found") { phylipfile = ""; }
184 else { format = "phylip"; inputfile = phylipfile; m-> setPhylipFile(phylipfile); if (outputDir == "") { outputDir += m->hasPath(phylipfile); } }
186 columnfile = validParameter.validFile(parameters, "column", true);
187 if (columnfile == "not open") { abort = true; }
188 else if (columnfile == "not found") { columnfile = ""; }
189 else { format = "column"; inputfile = columnfile; m->setColumnFile(columnfile); if (outputDir == "") { outputDir += m->hasPath(columnfile); } }
191 sharedfile = validParameter.validFile(parameters, "shared", true);
192 if (sharedfile == "not open") { abort = true; }
193 else if (sharedfile == "not found") { sharedfile = ""; }
194 else { format = "shared"; inputfile = sharedfile; m->setSharedFile(sharedfile); if (outputDir == "") { outputDir += m->hasPath(sharedfile); } }
196 namefile = validParameter.validFile(parameters, "name", true);
197 if (namefile == "not open") { abort = true; }
198 else if (namefile == "not found") { namefile = ""; }
199 else { m->setNameFile(namefile); }
201 countfile = validParameter.validFile(parameters, "count", true);
202 if (countfile == "not open") { abort = true; countfile = ""; }
203 else if (countfile == "not found") { countfile = ""; }
204 else { m->setCountTableFile(countfile); }
206 if ((countfile != "") && (namefile != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
208 //error checking on files
209 if ((sharedfile == "") && ((phylipfile == "") && (columnfile == ""))) {
210 sharedfile = m->getSharedFile();
211 if (sharedfile != "") { format = "shared"; inputfile = sharedfile; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
213 //is there are current file available for either of these?
214 //give priority to column, then phylip
215 columnfile = m->getColumnFile();
216 if (columnfile != "") { format = "column"; inputfile = columnfile; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
218 phylipfile = m->getPhylipFile();
219 if (phylipfile != "") { format = "phylip"; inputfile = phylipfile; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
221 m->mothurOut("No valid current files. You must provide a shared or phylip or column file."); m->mothurOutEndLine();
227 else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When running the heatmap.sim command with a distance file you may not use both the column and the phylip parameters."); m->mothurOutEndLine(); abort = true; }
229 if (columnfile != "") {
230 if (namefile == "") {
231 namefile = m->getNameFile();
232 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
234 countfile = m->getCountTableFile();
235 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
237 m->mothurOut("You need to provide a name or count file if you are going to use the column format."); m->mothurOutEndLine();
245 //if the user changes the output directory command factory will send this info to us in the output parameter
246 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
248 //check for optional parameter and set defaults
249 // ...at some point should added some additional type checking...
251 label = validParameter.validFile(parameters, "label", false);
252 if (label == "not found") { label = ""; }
254 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
255 else { allLines = 1; }
258 calc = validParameter.validFile(parameters, "calc", false);
259 if (calc == "not found") { calc = "jest-thetayc"; }
261 if (calc == "default") { calc = "jest-thetayc"; }
263 m->splitAtDash(calc, Estimators);
264 if (m->inUsersGroups("citation", Estimators)) {
265 ValidCalculators validCalc; validCalc.printCitations(Estimators);
266 //remove citation from list of calcs
267 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
270 groups = validParameter.validFile(parameters, "groups", false);
271 if (groups == "not found") { groups = ""; }
273 m->splitAtDash(groups, Groups);
274 m->setGroups(Groups);
277 string temp = validParameter.validFile(parameters, "fontsize", false); if (temp == "not found") { temp = "24"; }
278 m->mothurConvert(temp, fontsize);
280 if (abort == false) {
281 ValidCalculators validCalculator;
284 for (i=0; i<Estimators.size(); i++) {
285 if (validCalculator.isValidCalculator("heat", Estimators[i]) == true) {
286 if (Estimators[i] == "jabund") {
287 heatCalculators.push_back(new JAbund());
288 }else if (Estimators[i] == "sorabund") {
289 heatCalculators.push_back(new SorAbund());
290 }else if (Estimators[i] == "jclass") {
291 heatCalculators.push_back(new Jclass());
292 }else if (Estimators[i] == "sorclass") {
293 heatCalculators.push_back(new SorClass());
294 }else if (Estimators[i] == "jest") {
295 heatCalculators.push_back(new Jest());
296 }else if (Estimators[i] == "sorest") {
297 heatCalculators.push_back(new SorEst());
298 }else if (Estimators[i] == "thetayc") {
299 heatCalculators.push_back(new ThetaYC());
300 }else if (Estimators[i] == "thetan") {
301 heatCalculators.push_back(new ThetaN());
302 }else if (Estimators[i] == "morisitahorn") {
303 heatCalculators.push_back(new MorHorn());
304 }else if (Estimators[i] == "braycurtis") {
305 heatCalculators.push_back(new BrayCurtis());
316 catch(exception& e) {
317 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
321 //**********************************************************************************************************************
323 int HeatMapSimCommand::execute(){
326 if (abort == true) { if (calledHelp) { return 0; } return 2; }
328 heatmap = new HeatMapSim(outputDir, inputfile, fontsize);
330 if (format == "shared") {
332 }else{ runCommandDist(); }
336 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear(); return 0; }
338 m->mothurOutEndLine();
339 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
340 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
341 m->mothurOutEndLine();
345 catch(exception& e) {
346 m->errorOut(e, "HeatMapSimCommand", "execute");
351 //**********************************************************************************************************************
352 int HeatMapSimCommand::runCommandShared() {
354 //if the users entered no valid calculators don't execute command
355 if (heatCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
357 input = new InputData(sharedfile, "sharedfile");
358 lookup = input->getSharedRAbundVectors();
359 string lastLabel = lookup[0]->getLabel();
361 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); return 0;}
363 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
364 set<string> processedLabels;
365 set<string> userLabels = labels;
367 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0; }
369 //as long as you are not at the end of the file or done wih the lines you want
370 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
372 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0; }
374 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
376 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
377 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
378 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
380 processedLabels.insert(lookup[0]->getLabel());
381 userLabels.erase(lookup[0]->getLabel());
384 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
385 string saveLabel = lookup[0]->getLabel();
387 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
388 lookup = input->getSharedRAbundVectors(lastLabel);
390 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
391 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
392 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
394 processedLabels.insert(lookup[0]->getLabel());
395 userLabels.erase(lookup[0]->getLabel());
397 //restore real lastlabel to save below
398 lookup[0]->setLabel(saveLabel);
401 //prevent memory leak
403 lastLabel = lookup[0]->getLabel();
405 //get next line to process
406 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
407 lookup = input->getSharedRAbundVectors();
412 if (m->control_pressed) { delete input; m->clearGroups(); return 0; }
414 //output error messages about any remaining user labels
415 set<string>::iterator it;
416 bool needToRun = false;
417 for (it = userLabels.begin(); it != userLabels.end(); it++) {
418 m->mothurOut("Your file does not include the label " + *it);
419 if (processedLabels.count(lastLabel) != 1) {
420 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
423 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
427 if (m->control_pressed) { delete input; m->clearGroups(); return 0; }
429 //run last label if you need to
430 if (needToRun == true) {
431 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
432 lookup = input->getSharedRAbundVectors(lastLabel);
434 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
435 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
436 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
438 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
441 if (m->control_pressed) { delete input; m->clearGroups(); return 0; }
443 //reset groups parameter
450 catch(exception& e) {
451 m->errorOut(e, "HeatMapSimCommand", "runCommandShared");
455 //**********************************************************************************************************************
456 int HeatMapSimCommand::runCommandDist() {
459 vector< vector<double> > matrix;
460 vector<string> names;
463 //read distance file and create distance vector and names vector
464 if (format == "phylip") {
466 m->openInputFile(phylipfile, in);
470 in >> numSeqs >> name;
473 names.push_back(name);
475 //resize the matrix and fill with zeros
476 matrix.resize(numSeqs);
477 for(int i = 0; i < numSeqs; i++) {
478 matrix[i].resize(numSeqs, 0.0);
481 //determine if matrix is square or lower triangle
482 //if it is square read the distances for the first sequence
485 while((d=in.get()) != EOF){
487 //is d a number meaning its square
492 for(int i=0;i<numSeqs;i++){
498 //is d a line return meaning its lower triangle
505 //read rest of matrix
506 if (square == true) {
507 for(int i=1;i<numSeqs;i++){
509 names.push_back(name);
511 if (m->control_pressed) { return 0; }
513 for(int j=0;j<numSeqs;j++) { in >> matrix[i][j]; }
518 for(int i=1;i<numSeqs;i++){
520 names.push_back(name);
522 if (m->control_pressed) { return 0; }
524 for(int j=0;j<i;j++){
526 matrix[i][j] = dist; matrix[j][i] = dist;
534 NameAssignment* nameMap;
536 if (namefile != "") {
537 nameMap = new NameAssignment(namefile);
540 //put names in order in vector
541 for (int i = 0; i < nameMap->size(); i++) {
542 names.push_back(nameMap->get(i));
544 }else if (countfile != "") {
546 ct.readTable(countfile, true);
547 names = ct.getNamesOfSeqs();
551 matrix.resize(names.size());
552 for (int i = 0; i < names.size(); i++) {
553 matrix[i].resize(names.size(), 0.0);
557 string first, second;
559 m->openInputFile(columnfile, in);
562 in >> first >> second >> dist; m->gobble(in);
564 if (m->control_pressed) { return 0; }
566 if (namefile != "") {
567 map<string, int>::iterator itA = nameMap->find(first);
568 map<string, int>::iterator itB = nameMap->find(second);
570 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + first + "' was not found in the names file, please correct\n"); exit(1); }
571 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + second + "' was not found in the names file, please correct\n"); exit(1); }
574 matrix[itA->second][itB->second] = dist;
575 matrix[itB->second][itA->second] = dist;
576 }else if (countfile != "") {
577 int itA = ct.get(first);
578 int itB = ct.get(second);
579 matrix[itA][itB] = dist;
580 matrix[itB][itA] = dist;
585 if (namefile != "") { delete nameMap; }
589 string outputFileName = heatmap->getPic(matrix, names);
590 outputNames.push_back(outputFileName); //vector<vector<double>>, vector<string>
591 outputTypes["svg"].push_back(outputFileName);
595 catch(exception& e) {
596 m->errorOut(e, "HeatMapSimCommand", "runCommandDist");
600 //**********************************************************************************************************************