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","phylip","column","name","outputdir","inputdir"};
40 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
42 OptionParser parser(option);
43 map<string,string> parameters = parser.getParameters();
45 ValidParameters validParameter;
46 map<string,string>::iterator it;
48 //check to make sure all parameters are valid for command
49 for (it = parameters.begin(); it != parameters.end(); it++) {
50 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
54 //if the user changes the output directory command factory will send this info to us in the output parameter
55 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
57 //if the user changes the input directory command factory will send this info to us in the output parameter
58 string inputDir = validParameter.validFile(parameters, "inputdir", false);
59 if (inputDir == "not found"){ inputDir = ""; }
62 it = parameters.find("phylip");
63 //user has given a template file
64 if(it != parameters.end()){
65 path = hasPath(it->second);
66 //if the user has not given a path then, add inputdir. else leave path alone.
67 if (path == "") { parameters["phylip"] = inputDir + it->second; }
70 it = parameters.find("column");
71 //user has given a template file
72 if(it != parameters.end()){
73 path = hasPath(it->second);
74 //if the user has not given a path then, add inputdir. else leave path alone.
75 if (path == "") { parameters["column"] = inputDir + it->second; }
78 it = parameters.find("name");
79 //user has given a template file
80 if(it != parameters.end()){
81 path = hasPath(it->second);
82 //if the user has not given a path then, add inputdir. else leave path alone.
83 if (path == "") { parameters["name"] = inputDir + it->second; }
88 phylipfile = validParameter.validFile(parameters, "phylip", true);
89 if (phylipfile == "not open") { abort = true; }
90 else if (phylipfile == "not found") { phylipfile = ""; }
91 else { format = "phylip"; if (outputDir == "") { outputDir += hasPath(phylipfile); } }
93 columnfile = validParameter.validFile(parameters, "column", true);
94 if (columnfile == "not open") { abort = true; }
95 else if (columnfile == "not found") { columnfile = ""; }
96 else { format = "column"; if (outputDir == "") { outputDir += hasPath(columnfile); } }
98 namefile = validParameter.validFile(parameters, "name", true);
99 if (namefile == "not open") { abort = true; }
100 else if (namefile == "not found") { namefile = ""; }
103 //error checking on files
104 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; }
105 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; }
107 if (columnfile != "") {
108 if (namefile == "") { m->mothurOut("You need to provide a namefile if you are going to use the column format."); m->mothurOutEndLine(); abort = true; }
111 if (format == "") { format = "shared"; }
113 //check for optional parameter and set defaults
114 // ...at some point should added some additional type checking...
115 if (format == "shared") {
116 if (outputDir == "") { outputDir += hasPath(globaldata->getSharedFile()); }
118 label = validParameter.validFile(parameters, "label", false);
119 if (label == "not found") { label = ""; }
121 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
122 else { allLines = 1; }
125 //if the user has not specified any labels use the ones from read.otu
127 allLines = globaldata->allLines;
128 labels = globaldata->labels;
131 calc = validParameter.validFile(parameters, "calc", false);
132 if (calc == "not found") { calc = "jest-thetayc"; }
134 if (calc == "default") { calc = "jest-thetayc"; }
136 splitAtDash(calc, Estimators);
138 groups = validParameter.validFile(parameters, "groups", false);
139 if (groups == "not found") { groups = ""; }
141 splitAtDash(groups, Groups);
142 globaldata->Groups = Groups;
146 if (abort == false) {
147 validCalculator = new ValidCalculators();
150 for (i=0; i<Estimators.size(); i++) {
151 if (validCalculator->isValidCalculator("heat", Estimators[i]) == true) {
152 if (Estimators[i] == "jabund") {
153 heatCalculators.push_back(new JAbund());
154 }else if (Estimators[i] == "sorabund") {
155 heatCalculators.push_back(new SorAbund());
156 }else if (Estimators[i] == "jclass") {
157 heatCalculators.push_back(new Jclass());
158 }else if (Estimators[i] == "sorclass") {
159 heatCalculators.push_back(new SorClass());
160 }else if (Estimators[i] == "jest") {
161 heatCalculators.push_back(new Jest());
162 }else if (Estimators[i] == "sorest") {
163 heatCalculators.push_back(new SorEst());
164 }else if (Estimators[i] == "thetayc") {
165 heatCalculators.push_back(new ThetaYC());
166 }else if (Estimators[i] == "thetan") {
167 heatCalculators.push_back(new ThetaN());
168 }else if (Estimators[i] == "morisitahorn") {
169 heatCalculators.push_back(new MorHorn());
170 }else if (Estimators[i] == "braycurtis") {
171 heatCalculators.push_back(new BrayCurtis());
182 catch(exception& e) {
183 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
188 //**********************************************************************************************************************
190 void HeatMapSimCommand::help(){
192 m->mothurOut("The heatmap.sim command can only be executed after a successful read.otu command, or by providing a distance file.\n");
193 m->mothurOut("The heatmap.sim command parameters are phylip, column, name, groups, calc and label. No parameters are required.\n");
194 m->mothurOut("There are two ways to use the heatmap.sim command. The first is with the read.otu command. \n");
195 m->mothurOut("With the read.otu command you may use the groups, label and calc parameters. \n");
196 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n");
197 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");
198 m->mothurOut("The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, label=yourLabels).\n");
199 m->mothurOut("Example heatmap.sim(groups=A-B-C, calc=jabund).\n");
200 m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
201 validCalculator->printCalc("heat", cout);
202 m->mothurOut("The default value for calc is jclass-thetayc.\n");
203 m->mothurOut("The heatmap.sim command outputs a .svg file for each calculator you choose at each label you specify.\n");
204 m->mothurOut("The second way to use the heatmap.sim command is with a distance file representing the distance bewteen your groups. \n");
205 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");
206 m->mothurOut("The heatmap.sim command should be in the following format: heatmap.sim(phylip=yourDistanceFile).\n");
207 m->mothurOut("Example heatmap.sim(phylip=amazonGroups.dist).\n");
208 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
211 catch(exception& e) {
212 m->errorOut(e, "HeatMapSimCommand", "help");
217 //**********************************************************************************************************************
219 HeatMapSimCommand::~HeatMapSimCommand(){}
221 //**********************************************************************************************************************
223 int HeatMapSimCommand::execute(){
226 if (abort == true) { return 0; }
228 heatmap = new HeatMapSim(outputDir);
230 if (format == "shared") {
232 }else if (format == "phylip") {
233 globaldata->inputFileName = phylipfile;
235 }else if (format == "column") {
236 globaldata->inputFileName = columnfile;
241 delete validCalculator;
243 m->mothurOutEndLine();
244 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
245 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
246 m->mothurOutEndLine();
250 catch(exception& e) {
251 m->errorOut(e, "HeatMapSimCommand", "execute");
256 //**********************************************************************************************************************
257 int HeatMapSimCommand::runCommandShared() {
259 //if the users entered no valid calculators don't execute command
260 if (heatCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
263 read = new ReadOTUFile(globaldata->inputFileName);
264 read->read(&*globaldata);
266 input = globaldata->ginput;
267 lookup = input->getSharedRAbundVectors();
268 string lastLabel = lookup[0]->getLabel();
270 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups. I cannot run the command."); m->mothurOutEndLine(); return 0;}
272 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
273 set<string> processedLabels;
274 set<string> userLabels = labels;
276 //as long as you are not at the end of the file or done wih the lines you want
277 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
279 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
281 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
282 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
283 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); }
285 processedLabels.insert(lookup[0]->getLabel());
286 userLabels.erase(lookup[0]->getLabel());
289 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
290 string saveLabel = lookup[0]->getLabel();
292 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
293 lookup = input->getSharedRAbundVectors(lastLabel);
295 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
296 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
297 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); }
299 processedLabels.insert(lookup[0]->getLabel());
300 userLabels.erase(lookup[0]->getLabel());
302 //restore real lastlabel to save below
303 lookup[0]->setLabel(saveLabel);
306 //prevent memory leak
308 lastLabel = lookup[0]->getLabel();
310 //get next line to process
311 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
312 lookup = input->getSharedRAbundVectors();
315 //output error messages about any remaining user labels
316 set<string>::iterator it;
317 bool needToRun = false;
318 for (it = userLabels.begin(); it != userLabels.end(); it++) {
319 m->mothurOut("Your file does not include the label " + *it);
320 if (processedLabels.count(lastLabel) != 1) {
321 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
324 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
328 //run last label if you need to
329 if (needToRun == true) {
330 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
331 lookup = input->getSharedRAbundVectors(lastLabel);
333 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
334 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
335 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); }
337 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
341 //reset groups parameter
342 globaldata->Groups.clear();
344 delete input; globaldata->ginput = NULL;
349 catch(exception& e) {
350 m->errorOut(e, "HeatMapSimCommand", "runCommandShared");
354 //**********************************************************************************************************************
355 int HeatMapSimCommand::runCommandDist() {
358 vector< vector<double> > matrix;
359 vector<string> names;
362 //read distance file and create distance vector and names vector
363 if (format == "phylip") {
365 openInputFile(phylipfile, in);
369 in >> numSeqs >> name;
372 names.push_back(name);
374 //resize the matrix and fill with zeros
375 matrix.resize(numSeqs);
376 for(int i = 0; i < numSeqs; i++) {
377 matrix[i].resize(numSeqs, 0.0);
380 //determine if matrix is square or lower triangle
381 //if it is square read the distances for the first sequence
384 while((d=in.get()) != EOF){
386 //is d a number meaning its square
391 for(int i=0;i<numSeqs;i++){
397 //is d a line return meaning its lower triangle
404 //read rest of matrix
405 if (square == true) {
406 for(int i=1;i<numSeqs;i++){
408 names.push_back(name);
410 for(int j=0;j<numSeqs;j++) { in >> matrix[i][j]; }
415 for(int i=1;i<numSeqs;i++){
417 names.push_back(name);
419 for(int j=0;j<i;j++){
421 matrix[i][j] = dist; matrix[j][i] = dist;
429 NameAssignment* nameMap = new NameAssignment(namefile);
432 //put names in order in vector
433 for (int i = 0; i < nameMap->size(); i++) {
434 names.push_back(nameMap->get(i));
438 matrix.resize(nameMap->size());
439 for (int i = 0; i < nameMap->size(); i++) {
440 matrix[i].resize(nameMap->size(), 0.0);
444 string first, second;
446 openInputFile(columnfile, in);
449 in >> first >> second >> dist; gobble(in);
451 map<string, int>::iterator itA = nameMap->find(first);
452 map<string, int>::iterator itB = nameMap->find(second);
454 if(itA == nameMap->end()){ cerr << "AAError: Sequence '" << first << "' was not found in the names file, please correct\n"; exit(1); }
455 if(itB == nameMap->end()){ cerr << "ABError: Sequence '" << second << "' was not found in the names file, please correct\n"; exit(1); }
458 matrix[itA->second][itB->second] = dist;
459 matrix[itB->second][itA->second] = dist;
467 outputNames.push_back(heatmap->getPic(matrix, names)); //vector<vector<double>>, vector<string>
471 catch(exception& e) {
472 m->errorOut(e, "HeatMapSimCommand", "runCommandDist");
476 //**********************************************************************************************************************