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"
22 //**********************************************************************************************************************
23 vector<string> HeatMapSimCommand::getValidParameters(){
25 string Array[] = {"groups","label", "calc","phylip","column","name","outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
30 m->errorOut(e, "HeatMapSimCommand", "getValidParameters");
34 //**********************************************************************************************************************
35 HeatMapSimCommand::HeatMapSimCommand(){
37 //initialize outputTypes
38 vector<string> tempOutNames;
39 outputTypes["svg"] = tempOutNames;
42 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
46 //**********************************************************************************************************************
47 vector<string> HeatMapSimCommand::getRequiredParameters(){
49 vector<string> myArray;
53 m->errorOut(e, "HeatMapSimCommand", "getRequiredParameters");
57 //**********************************************************************************************************************
58 vector<string> HeatMapSimCommand::getRequiredFiles(){
60 vector<string> myArray;
64 m->errorOut(e, "HeatMapSimCommand", "getRequiredFiles");
68 //**********************************************************************************************************************
70 HeatMapSimCommand::HeatMapSimCommand(string option) {
72 globaldata = GlobalData::getInstance();
79 //allow user to run help
80 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
83 //valid paramters for this command
84 string AlignArray[] = {"groups","label", "calc","phylip","column","name","outputdir","inputdir"};
85 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
87 OptionParser parser(option);
88 map<string,string> parameters = parser.getParameters();
90 ValidParameters validParameter;
91 map<string,string>::iterator it;
93 //check to make sure all parameters are valid for command
94 for (it = parameters.begin(); it != parameters.end(); it++) {
95 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
98 //initialize outputTypes
99 vector<string> tempOutNames;
100 outputTypes["svg"] = tempOutNames;
103 //if the user changes the output directory command factory will send this info to us in the output parameter
104 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
106 //if the user changes the input directory command factory will send this info to us in the output parameter
107 string inputDir = validParameter.validFile(parameters, "inputdir", false);
108 if (inputDir == "not found"){ inputDir = ""; }
111 it = parameters.find("phylip");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["phylip"] = inputDir + it->second; }
119 it = parameters.find("column");
120 //user has given a template file
121 if(it != parameters.end()){
122 path = m->hasPath(it->second);
123 //if the user has not given a path then, add inputdir. else leave path alone.
124 if (path == "") { parameters["column"] = inputDir + it->second; }
127 it = parameters.find("name");
128 //user has given a template file
129 if(it != parameters.end()){
130 path = m->hasPath(it->second);
131 //if the user has not given a path then, add inputdir. else leave path alone.
132 if (path == "") { parameters["name"] = inputDir + it->second; }
136 //required parameters
137 phylipfile = validParameter.validFile(parameters, "phylip", true);
138 if (phylipfile == "not open") { abort = true; }
139 else if (phylipfile == "not found") { phylipfile = ""; }
140 else { format = "phylip"; if (outputDir == "") { outputDir += m->hasPath(phylipfile); } }
142 columnfile = validParameter.validFile(parameters, "column", true);
143 if (columnfile == "not open") { abort = true; }
144 else if (columnfile == "not found") { columnfile = ""; }
145 else { format = "column"; if (outputDir == "") { outputDir += m->hasPath(columnfile); } }
147 namefile = validParameter.validFile(parameters, "name", true);
148 if (namefile == "not open") { abort = true; }
149 else if (namefile == "not found") { namefile = ""; }
152 //error checking on files
153 if ((globaldata->getSharedFile() == "") && ((phylipfile == "") && (columnfile == ""))) { m->mothurOut("You must run the read.otu command or provide a distance file before running the heatmap.sim command."); m->mothurOutEndLine(); abort = true; }
154 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; }
156 if (columnfile != "") {
157 if (namefile == "") { m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine(); abort = true; }
160 if (format == "") { format = "shared"; }
162 //check for optional parameter and set defaults
163 // ...at some point should added some additional type checking...
164 if (format == "shared") {
165 if (outputDir == "") { outputDir += m->hasPath(globaldata->getSharedFile()); }
167 label = validParameter.validFile(parameters, "label", false);
168 if (label == "not found") { label = ""; }
170 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
171 else { allLines = 1; }
174 //if the user has not specified any labels use the ones from read.otu
176 allLines = globaldata->allLines;
177 labels = globaldata->labels;
180 calc = validParameter.validFile(parameters, "calc", false);
181 if (calc == "not found") { calc = "jest-thetayc"; }
183 if (calc == "default") { calc = "jest-thetayc"; }
185 m->splitAtDash(calc, Estimators);
187 groups = validParameter.validFile(parameters, "groups", false);
188 if (groups == "not found") { groups = ""; }
190 m->splitAtDash(groups, Groups);
191 globaldata->Groups = Groups;
195 if (abort == false) {
196 validCalculator = new ValidCalculators();
199 for (i=0; i<Estimators.size(); i++) {
200 if (validCalculator->isValidCalculator("heat", Estimators[i]) == true) {
201 if (Estimators[i] == "jabund") {
202 heatCalculators.push_back(new JAbund());
203 }else if (Estimators[i] == "sorabund") {
204 heatCalculators.push_back(new SorAbund());
205 }else if (Estimators[i] == "jclass") {
206 heatCalculators.push_back(new Jclass());
207 }else if (Estimators[i] == "sorclass") {
208 heatCalculators.push_back(new SorClass());
209 }else if (Estimators[i] == "jest") {
210 heatCalculators.push_back(new Jest());
211 }else if (Estimators[i] == "sorest") {
212 heatCalculators.push_back(new SorEst());
213 }else if (Estimators[i] == "thetayc") {
214 heatCalculators.push_back(new ThetaYC());
215 }else if (Estimators[i] == "thetan") {
216 heatCalculators.push_back(new ThetaN());
217 }else if (Estimators[i] == "morisitahorn") {
218 heatCalculators.push_back(new MorHorn());
219 }else if (Estimators[i] == "braycurtis") {
220 heatCalculators.push_back(new BrayCurtis());
231 catch(exception& e) {
232 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
237 //**********************************************************************************************************************
239 void HeatMapSimCommand::help(){
241 m->mothurOut("The heatmap.sim command can only be executed after a successful read.otu command, or by providing a distance file.\n");
242 m->mothurOut("The heatmap.sim command parameters are phylip, column, name, groups, calc and label. No parameters are required.\n");
243 m->mothurOut("There are two ways to use the heatmap.sim command. The first is with the read.otu command. \n");
244 m->mothurOut("With the read.otu command you may use the groups, label and calc parameters. \n");
245 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
246 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 is also separated by dashes.\n");
247 m->mothurOut("The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, label=yourLabels).\n");
248 m->mothurOut("Example heatmap.sim(groups=A-B-C, calc=jabund).\n");
249 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
250 validCalculator->printCalc("heat", cout);
251 m->mothurOut("The default value for calc is jclass-thetayc.\n");
252 m->mothurOut("The heatmap.sim command outputs a .svg file for each calculator you choose at each label you specify.\n");
253 m->mothurOut("The second way to use the heatmap.sim command is with a distance file representing the distance bewteen your groups. \n");
254 m->mothurOut("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");
255 m->mothurOut("The heatmap.sim command should be in the following format: heatmap.sim(phylip=yourDistanceFile).\n");
256 m->mothurOut("Example heatmap.sim(phylip=amazonGroups.dist).\n");
257 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
260 catch(exception& e) {
261 m->errorOut(e, "HeatMapSimCommand", "help");
266 //**********************************************************************************************************************
268 HeatMapSimCommand::~HeatMapSimCommand(){}
270 //**********************************************************************************************************************
272 int HeatMapSimCommand::execute(){
275 if (abort == true) { return 0; }
277 heatmap = new HeatMapSim(outputDir);
279 if (format == "shared") {
281 }else if (format == "phylip") {
282 globaldata->inputFileName = phylipfile;
284 }else if (format == "column") {
285 globaldata->inputFileName = columnfile;
290 delete validCalculator;
292 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } outputTypes.clear(); return 0; }
294 m->mothurOutEndLine();
295 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
296 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
297 m->mothurOutEndLine();
301 catch(exception& e) {
302 m->errorOut(e, "HeatMapSimCommand", "execute");
307 //**********************************************************************************************************************
308 int HeatMapSimCommand::runCommandShared() {
310 //if the users entered no valid calculators don't execute command
311 if (heatCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
314 read = new ReadOTUFile(globaldata->inputFileName);
315 read->read(&*globaldata);
317 input = globaldata->ginput;
318 lookup = input->getSharedRAbundVectors();
319 string lastLabel = lookup[0]->getLabel();
321 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); return 0;}
323 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
324 set<string> processedLabels;
325 set<string> userLabels = labels;
327 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } globaldata->Groups.clear(); return 0; }
329 //as long as you are not at the end of the file or done wih the lines you want
330 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
332 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } globaldata->Groups.clear(); return 0; }
334 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
336 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
337 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
338 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
340 processedLabels.insert(lookup[0]->getLabel());
341 userLabels.erase(lookup[0]->getLabel());
344 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
345 string saveLabel = lookup[0]->getLabel();
347 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
348 lookup = input->getSharedRAbundVectors(lastLabel);
350 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
351 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
352 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
354 processedLabels.insert(lookup[0]->getLabel());
355 userLabels.erase(lookup[0]->getLabel());
357 //restore real lastlabel to save below
358 lookup[0]->setLabel(saveLabel);
361 //prevent memory leak
363 lastLabel = lookup[0]->getLabel();
365 //get next line to process
366 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
367 lookup = input->getSharedRAbundVectors();
372 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; globaldata->Groups.clear(); return 0; }
374 //output error messages about any remaining user labels
375 set<string>::iterator it;
376 bool needToRun = false;
377 for (it = userLabels.begin(); it != userLabels.end(); it++) {
378 m->mothurOut("Your file does not include the label " + *it);
379 if (processedLabels.count(lastLabel) != 1) {
380 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
383 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
387 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; globaldata->Groups.clear(); return 0; }
389 //run last label if you need to
390 if (needToRun == true) {
391 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
392 lookup = input->getSharedRAbundVectors(lastLabel);
394 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
395 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
396 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]); }
398 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
401 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; globaldata->Groups.clear(); return 0; }
403 //reset groups parameter
404 globaldata->Groups.clear();
406 delete input; globaldata->ginput = NULL;
411 catch(exception& e) {
412 m->errorOut(e, "HeatMapSimCommand", "runCommandShared");
416 //**********************************************************************************************************************
417 int HeatMapSimCommand::runCommandDist() {
420 vector< vector<double> > matrix;
421 vector<string> names;
424 //read distance file and create distance vector and names vector
425 if (format == "phylip") {
427 m->openInputFile(phylipfile, in);
431 in >> numSeqs >> name;
434 names.push_back(name);
436 //resize the matrix and fill with zeros
437 matrix.resize(numSeqs);
438 for(int i = 0; i < numSeqs; i++) {
439 matrix[i].resize(numSeqs, 0.0);
442 //determine if matrix is square or lower triangle
443 //if it is square read the distances for the first sequence
446 while((d=in.get()) != EOF){
448 //is d a number meaning its square
453 for(int i=0;i<numSeqs;i++){
459 //is d a line return meaning its lower triangle
466 //read rest of matrix
467 if (square == true) {
468 for(int i=1;i<numSeqs;i++){
470 names.push_back(name);
472 if (m->control_pressed) { return 0; }
474 for(int j=0;j<numSeqs;j++) { in >> matrix[i][j]; }
479 for(int i=1;i<numSeqs;i++){
481 names.push_back(name);
483 if (m->control_pressed) { return 0; }
485 for(int j=0;j<i;j++){
487 matrix[i][j] = dist; matrix[j][i] = dist;
495 NameAssignment* nameMap = new NameAssignment(namefile);
498 //put names in order in vector
499 for (int i = 0; i < nameMap->size(); i++) {
500 names.push_back(nameMap->get(i));
504 matrix.resize(nameMap->size());
505 for (int i = 0; i < nameMap->size(); i++) {
506 matrix[i].resize(nameMap->size(), 0.0);
510 string first, second;
512 m->openInputFile(columnfile, in);
515 in >> first >> second >> dist; m->gobble(in);
517 if (m->control_pressed) { return 0; }
519 map<string, int>::iterator itA = nameMap->find(first);
520 map<string, int>::iterator itB = nameMap->find(second);
522 if(itA == nameMap->end()){ cerr << "AAError: Sequence '" << first << "' was not found in the names file, please correct\n"; exit(1); }
523 if(itB == nameMap->end()){ cerr << "ABError: Sequence '" << second << "' was not found in the names file, please correct\n"; exit(1); }
526 matrix[itA->second][itB->second] = dist;
527 matrix[itB->second][itA->second] = dist;
535 string outputFileName = heatmap->getPic(matrix, names);
536 outputNames.push_back(outputFileName); //vector<vector<double>>, vector<string>
537 outputTypes["svg"].push_back(outputFileName);
541 catch(exception& e) {
542 m->errorOut(e, "HeatMapSimCommand", "runCommandDist");
546 //**********************************************************************************************************************