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",false,false); parameters.push_back(pshared);
27 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none",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",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 //**********************************************************************************************************************
77 string HeatMapSimCommand::getOutputFileNameTag(string type, string inputName=""){
79 string outputFileName = "";
80 map<string, vector<string> >::iterator it;
82 //is this a type this command creates
83 it = outputTypes.find(type);
84 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
86 if (type == "svg") { outputFileName = "svg"; }
87 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
89 return outputFileName;
92 m->errorOut(e, "HeatMapSimCommand", "getOutputFileNameTag");
97 //**********************************************************************************************************************
98 HeatMapSimCommand::HeatMapSimCommand(){
100 abort = true; calledHelp = true;
102 vector<string> tempOutNames;
103 outputTypes["svg"] = tempOutNames;
105 catch(exception& e) {
106 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
110 //**********************************************************************************************************************
112 HeatMapSimCommand::HeatMapSimCommand(string option) {
114 abort = false; calledHelp = false;
117 //allow user to run help
118 if(option == "help") { help(); abort = true; calledHelp = true; }
119 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
122 vector<string> myArray = setParameters();
124 OptionParser parser(option);
125 map<string,string> parameters = parser.getParameters();
127 ValidParameters validParameter;
128 map<string,string>::iterator it;
130 //check to make sure all parameters are valid for command
131 for (it = parameters.begin(); it != parameters.end(); it++) {
132 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
135 //initialize outputTypes
136 vector<string> tempOutNames;
137 outputTypes["svg"] = tempOutNames;
141 //if the user changes the input directory command factory will send this info to us in the output parameter
142 string inputDir = validParameter.validFile(parameters, "inputdir", false);
143 if (inputDir == "not found"){ inputDir = ""; }
146 it = parameters.find("phylip");
147 //user has given a template file
148 if(it != parameters.end()){
149 path = m->hasPath(it->second);
150 //if the user has not given a path then, add inputdir. else leave path alone.
151 if (path == "") { parameters["phylip"] = inputDir + it->second; }
154 it = parameters.find("column");
155 //user has given a template file
156 if(it != parameters.end()){
157 path = m->hasPath(it->second);
158 //if the user has not given a path then, add inputdir. else leave path alone.
159 if (path == "") { parameters["column"] = inputDir + it->second; }
162 it = parameters.find("name");
163 //user has given a template file
164 if(it != parameters.end()){
165 path = m->hasPath(it->second);
166 //if the user has not given a path then, add inputdir. else leave path alone.
167 if (path == "") { parameters["name"] = inputDir + it->second; }
170 it = parameters.find("shared");
171 //user has given a template file
172 if(it != parameters.end()){
173 path = m->hasPath(it->second);
174 //if the user has not given a path then, add inputdir. else leave path alone.
175 if (path == "") { parameters["shared"] = inputDir + it->second; }
178 it = parameters.find("count");
179 //user has given a template file
180 if(it != parameters.end()){
181 path = m->hasPath(it->second);
182 //if the user has not given a path then, add inputdir. else leave path alone.
183 if (path == "") { parameters["count"] = inputDir + it->second; }
187 //required parameters
188 phylipfile = validParameter.validFile(parameters, "phylip", true);
189 if (phylipfile == "not open") { abort = true; }
190 else if (phylipfile == "not found") { phylipfile = ""; }
191 else { format = "phylip"; inputfile = phylipfile; m-> setPhylipFile(phylipfile); if (outputDir == "") { outputDir += m->hasPath(phylipfile); } }
193 columnfile = validParameter.validFile(parameters, "column", true);
194 if (columnfile == "not open") { abort = true; }
195 else if (columnfile == "not found") { columnfile = ""; }
196 else { format = "column"; inputfile = columnfile; m->setColumnFile(columnfile); if (outputDir == "") { outputDir += m->hasPath(columnfile); } }
198 sharedfile = validParameter.validFile(parameters, "shared", true);
199 if (sharedfile == "not open") { abort = true; }
200 else if (sharedfile == "not found") { sharedfile = ""; }
201 else { format = "shared"; inputfile = sharedfile; m->setSharedFile(sharedfile); if (outputDir == "") { outputDir += m->hasPath(sharedfile); } }
203 namefile = validParameter.validFile(parameters, "name", true);
204 if (namefile == "not open") { abort = true; }
205 else if (namefile == "not found") { namefile = ""; }
206 else { m->setNameFile(namefile); }
208 countfile = validParameter.validFile(parameters, "count", true);
209 if (countfile == "not open") { abort = true; countfile = ""; }
210 else if (countfile == "not found") { countfile = ""; }
211 else { m->setCountTableFile(countfile); }
213 if ((countfile != "") && (namefile != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
215 //error checking on files
216 if ((sharedfile == "") && ((phylipfile == "") && (columnfile == ""))) {
217 sharedfile = m->getSharedFile();
218 if (sharedfile != "") { format = "shared"; inputfile = sharedfile; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
220 //is there are current file available for either of these?
221 //give priority to column, then phylip
222 columnfile = m->getColumnFile();
223 if (columnfile != "") { format = "column"; inputfile = columnfile; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
225 phylipfile = m->getPhylipFile();
226 if (phylipfile != "") { format = "phylip"; inputfile = phylipfile; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
228 m->mothurOut("No valid current files. You must provide a shared or phylip or column file."); m->mothurOutEndLine();
234 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; }
236 if (columnfile != "") {
237 if (namefile == "") {
238 namefile = m->getNameFile();
239 if (namefile != "") { m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
241 countfile = m->getCountTableFile();
242 if (countfile != "") { m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
244 m->mothurOut("You need to provide a name or count file if you are going to use the column format."); m->mothurOutEndLine();
252 //if the user changes the output directory command factory will send this info to us in the output parameter
253 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(inputfile); }
255 //check for optional parameter and set defaults
256 // ...at some point should added some additional type checking...
258 label = validParameter.validFile(parameters, "label", false);
259 if (label == "not found") { label = ""; }
261 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
262 else { allLines = 1; }
265 calc = validParameter.validFile(parameters, "calc", false);
266 if (calc == "not found") { calc = "jest-thetayc"; }
268 if (calc == "default") { calc = "jest-thetayc"; }
270 m->splitAtDash(calc, Estimators);
271 if (m->inUsersGroups("citation", Estimators)) {
272 ValidCalculators validCalc; validCalc.printCitations(Estimators);
273 //remove citation from list of calcs
274 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") { Estimators.erase(Estimators.begin()+i); break; } }
277 groups = validParameter.validFile(parameters, "groups", false);
278 if (groups == "not found") { groups = ""; }
280 m->splitAtDash(groups, Groups);
281 m->setGroups(Groups);
284 string temp = validParameter.validFile(parameters, "fontsize", false); if (temp == "not found") { temp = "24"; }
285 m->mothurConvert(temp, fontsize);
287 if (abort == false) {
288 ValidCalculators validCalculator;
291 for (i=0; i<Estimators.size(); i++) {
292 if (validCalculator.isValidCalculator("heat", Estimators[i]) == true) {
293 if (Estimators[i] == "jabund") {
294 heatCalculators.push_back(new JAbund());
295 }else if (Estimators[i] == "sorabund") {
296 heatCalculators.push_back(new SorAbund());
297 }else if (Estimators[i] == "jclass") {
298 heatCalculators.push_back(new Jclass());
299 }else if (Estimators[i] == "sorclass") {
300 heatCalculators.push_back(new SorClass());
301 }else if (Estimators[i] == "jest") {
302 heatCalculators.push_back(new Jest());
303 }else if (Estimators[i] == "sorest") {
304 heatCalculators.push_back(new SorEst());
305 }else if (Estimators[i] == "thetayc") {
306 heatCalculators.push_back(new ThetaYC());
307 }else if (Estimators[i] == "thetan") {
308 heatCalculators.push_back(new ThetaN());
309 }else if (Estimators[i] == "morisitahorn") {
310 heatCalculators.push_back(new MorHorn());
311 }else if (Estimators[i] == "braycurtis") {
312 heatCalculators.push_back(new BrayCurtis());
323 catch(exception& e) {
324 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
328 //**********************************************************************************************************************
330 int HeatMapSimCommand::execute(){
333 if (abort == true) { if (calledHelp) { return 0; } return 2; }
335 heatmap = new HeatMapSim(outputDir, inputfile, fontsize);
337 if (format == "shared") {
339 }else{ runCommandDist(); }
343 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } outputTypes.clear(); return 0; }
345 m->mothurOutEndLine();
346 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
347 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
348 m->mothurOutEndLine();
352 catch(exception& e) {
353 m->errorOut(e, "HeatMapSimCommand", "execute");
358 //**********************************************************************************************************************
359 int HeatMapSimCommand::runCommandShared() {
361 //if the users entered no valid calculators don't execute command
362 if (heatCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
364 input = new InputData(sharedfile, "sharedfile");
365 lookup = input->getSharedRAbundVectors();
366 string lastLabel = lookup[0]->getLabel();
368 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); return 0;}
370 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
371 set<string> processedLabels;
372 set<string> userLabels = labels;
374 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0; }
376 //as long as you are not at the end of the file or done wih the lines you want
377 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
379 if (m->control_pressed) { delete input; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } m->clearGroups(); return 0; }
381 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
383 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
384 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
385 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
387 processedLabels.insert(lookup[0]->getLabel());
388 userLabels.erase(lookup[0]->getLabel());
391 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
392 string saveLabel = lookup[0]->getLabel();
394 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
395 lookup = input->getSharedRAbundVectors(lastLabel);
397 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
398 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
399 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
401 processedLabels.insert(lookup[0]->getLabel());
402 userLabels.erase(lookup[0]->getLabel());
404 //restore real lastlabel to save below
405 lookup[0]->setLabel(saveLabel);
408 //prevent memory leak
410 lastLabel = lookup[0]->getLabel();
412 //get next line to process
413 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
414 lookup = input->getSharedRAbundVectors();
419 if (m->control_pressed) { delete input; m->clearGroups(); return 0; }
421 //output error messages about any remaining user labels
422 set<string>::iterator it;
423 bool needToRun = false;
424 for (it = userLabels.begin(); it != userLabels.end(); it++) {
425 m->mothurOut("Your file does not include the label " + *it);
426 if (processedLabels.count(lastLabel) != 1) {
427 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
430 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
434 if (m->control_pressed) { delete input; m->clearGroups(); return 0; }
436 //run last label if you need to
437 if (needToRun == true) {
438 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
439 lookup = input->getSharedRAbundVectors(lastLabel);
441 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
442 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
443 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
445 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
448 if (m->control_pressed) { delete input; m->clearGroups(); return 0; }
450 //reset groups parameter
457 catch(exception& e) {
458 m->errorOut(e, "HeatMapSimCommand", "runCommandShared");
462 //**********************************************************************************************************************
463 int HeatMapSimCommand::runCommandDist() {
466 vector< vector<double> > matrix;
467 vector<string> names;
470 //read distance file and create distance vector and names vector
471 if (format == "phylip") {
473 m->openInputFile(phylipfile, in);
477 in >> numSeqs >> name;
480 names.push_back(name);
482 //resize the matrix and fill with zeros
483 matrix.resize(numSeqs);
484 for(int i = 0; i < numSeqs; i++) {
485 matrix[i].resize(numSeqs, 0.0);
488 //determine if matrix is square or lower triangle
489 //if it is square read the distances for the first sequence
492 while((d=in.get()) != EOF){
494 //is d a number meaning its square
499 for(int i=0;i<numSeqs;i++){
505 //is d a line return meaning its lower triangle
512 //read rest of matrix
513 if (square == true) {
514 for(int i=1;i<numSeqs;i++){
516 names.push_back(name);
518 if (m->control_pressed) { return 0; }
520 for(int j=0;j<numSeqs;j++) { in >> matrix[i][j]; }
525 for(int i=1;i<numSeqs;i++){
527 names.push_back(name);
529 if (m->control_pressed) { return 0; }
531 for(int j=0;j<i;j++){
533 matrix[i][j] = dist; matrix[j][i] = dist;
541 NameAssignment* nameMap;
543 if (namefile != "") {
544 nameMap = new NameAssignment(namefile);
547 //put names in order in vector
548 for (int i = 0; i < nameMap->size(); i++) {
549 names.push_back(nameMap->get(i));
551 }else if (countfile != "") {
553 ct.readTable(countfile);
554 names = ct.getNamesOfSeqs();
558 matrix.resize(names.size());
559 for (int i = 0; i < names.size(); i++) {
560 matrix[i].resize(names.size(), 0.0);
564 string first, second;
566 m->openInputFile(columnfile, in);
569 in >> first >> second >> dist; m->gobble(in);
571 if (m->control_pressed) { return 0; }
573 if (namefile != "") {
574 map<string, int>::iterator itA = nameMap->find(first);
575 map<string, int>::iterator itB = nameMap->find(second);
577 if(itA == nameMap->end()){ m->mothurOut("AAError: Sequence '" + first + "' was not found in the names file, please correct\n"); exit(1); }
578 if(itB == nameMap->end()){ m->mothurOut("ABError: Sequence '" + second + "' was not found in the names file, please correct\n"); exit(1); }
581 matrix[itA->second][itB->second] = dist;
582 matrix[itB->second][itA->second] = dist;
583 }else if (countfile != "") {
584 int itA = ct.get(first);
585 int itB = ct.get(second);
586 matrix[itA][itB] = dist;
587 matrix[itB][itA] = dist;
592 if (namefile != "") { delete nameMap; }
596 string outputFileName = heatmap->getPic(matrix, names);
597 outputNames.push_back(outputFileName); //vector<vector<double>>, vector<string>
598 outputTypes["svg"].push_back(outputFileName);
602 catch(exception& e) {
603 m->errorOut(e, "HeatMapSimCommand", "runCommandDist");
607 //**********************************************************************************************************************