]> git.donarmstrong.com Git - mothur.git/blob - heatmapsimcommand.cpp
added [ERROR] flag if command aborts
[mothur.git] / heatmapsimcommand.cpp
1 /*
2  *  heatmapsimcommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 6/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
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"
21
22 //**********************************************************************************************************************
23 vector<string> HeatMapSimCommand::getValidParameters(){ 
24         try {
25                 string Array[] =  {"groups","label", "calc","phylip","column","name","outputdir","inputdir"};
26                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27                 return myArray;
28         }
29         catch(exception& e) {
30                 m->errorOut(e, "HeatMapSimCommand", "getValidParameters");
31                 exit(1);
32         }
33 }
34 //**********************************************************************************************************************
35 HeatMapSimCommand::HeatMapSimCommand(){ 
36         try {
37                 abort = true; calledHelp = true; 
38                 vector<string> tempOutNames;
39                 outputTypes["svg"] = tempOutNames;
40         }
41         catch(exception& e) {
42                 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
43                 exit(1);
44         }
45 }
46 //**********************************************************************************************************************
47 vector<string> HeatMapSimCommand::getRequiredParameters(){      
48         try {
49                 vector<string> myArray;
50                 return myArray;
51         }
52         catch(exception& e) {
53                 m->errorOut(e, "HeatMapSimCommand", "getRequiredParameters");
54                 exit(1);
55         }
56 }
57 //**********************************************************************************************************************
58 vector<string> HeatMapSimCommand::getRequiredFiles(){   
59         try {
60                 vector<string> myArray;
61                 return myArray;
62         }
63         catch(exception& e) {
64                 m->errorOut(e, "HeatMapSimCommand", "getRequiredFiles");
65                 exit(1);
66         }
67 }
68 //**********************************************************************************************************************
69
70 HeatMapSimCommand::HeatMapSimCommand(string option)  {
71         try {
72                 globaldata = GlobalData::getInstance();
73                 abort = false; calledHelp = false;   
74                 allLines = 1;
75                 labels.clear();
76                 Groups.clear();
77                 Estimators.clear();
78                         
79                 //allow user to run help
80                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; calledHelp = true; }
81                 
82                 else {
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)));
86                         
87                         OptionParser parser(option);
88                         map<string,string> parameters = parser.getParameters();
89                         
90                         ValidParameters validParameter;
91                         map<string,string>::iterator it;
92                         
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;  }
96                         }
97                         
98                         //initialize outputTypes
99                         vector<string> tempOutNames;
100                         outputTypes["svg"] = tempOutNames;
101                         
102                         format = "";
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 = "";         }
105                         
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 = "";          }
109                         else {
110                                 string path;
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;           }
117                                 }
118                                 
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;           }
125                                 }
126                                 
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;             }
133                                 }
134                         }
135
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); }  }
141                         
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); } }
146                         
147                         namefile = validParameter.validFile(parameters, "name", true);
148                         if (namefile == "not open") { abort = true; }   
149                         else if (namefile == "not found") { namefile = ""; }
150                         
151                         
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; }
155                         
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; }
158                         }
159                         
160                         if (format == "") { format = "shared"; }
161                         
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()); }
166                                 
167                                 label = validParameter.validFile(parameters, "label", false);                   
168                                 if (label == "not found") { label = ""; }
169                                 else { 
170                                         if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
171                                         else { allLines = 1;  }
172                                 }
173                                 
174                                 //if the user has not specified any labels use the ones from read.otu
175                                 if (label == "") {  
176                                         allLines = globaldata->allLines; 
177                                         labels = globaldata->labels; 
178                                 }
179                                 
180                                 calc = validParameter.validFile(parameters, "calc", false);                     
181                                 if (calc == "not found") { calc = "jest-thetayc";  }
182                                 else { 
183                                         if (calc == "default")  {  calc = "jest-thetayc";  }
184                                 }
185                                 m->splitAtDash(calc, Estimators);
186                                 
187                                 groups = validParameter.validFile(parameters, "groups", false);                 
188                                 if (groups == "not found") { groups = ""; }
189                                 else { 
190                                         m->splitAtDash(groups, Groups);
191                                         globaldata->Groups = Groups;
192                                 }
193                         }
194                         
195                         if (abort == false) {
196                                 validCalculator = new ValidCalculators();
197                         
198                                 int i;
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());
221                                                 }
222                                         }
223                                 }
224                                 
225                         }
226                 }
227
228                                 
229
230         }
231         catch(exception& e) {
232                 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
233                 exit(1);
234         }
235 }
236
237 //**********************************************************************************************************************
238
239 void HeatMapSimCommand::help(){
240         try {
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");
258
259         }
260         catch(exception& e) {
261                 m->errorOut(e, "HeatMapSimCommand", "help");
262                 exit(1);
263         }
264 }
265
266 //**********************************************************************************************************************
267
268 HeatMapSimCommand::~HeatMapSimCommand(){}
269
270 //**********************************************************************************************************************
271
272 int HeatMapSimCommand::execute(){
273         try {
274         
275                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
276                 
277                 heatmap = new HeatMapSim(outputDir);
278                 
279                 if (format == "shared") {
280                         runCommandShared();
281                 }else if (format == "phylip") {
282                         globaldata->inputFileName = phylipfile;
283                         runCommandDist();
284                 }else if (format == "column") {
285                         globaldata->inputFileName = columnfile;
286                         runCommandDist();
287                 }
288                 
289                 delete heatmap;
290                 delete validCalculator;
291                 
292                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } outputTypes.clear(); return 0; }
293                 
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();
298                 
299                 return 0;
300         }
301         catch(exception& e) {
302                 m->errorOut(e, "HeatMapSimCommand", "execute");
303                 exit(1);
304         }
305 }
306
307 //**********************************************************************************************************************
308 int HeatMapSimCommand::runCommandShared() {
309         try {
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; }
312                 
313                 //you have groups
314                 read = new ReadOTUFile(globaldata->inputFileName);      
315                 read->read(&*globaldata); 
316                                 
317                 input = globaldata->ginput;
318                 lookup = input->getSharedRAbundVectors();
319                 string lastLabel = lookup[0]->getLabel();
320                         
321                 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); return 0;}
322                                 
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;
326                 
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; }
328                 
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))) {
331                         
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; }
333
334                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
335         
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]); }
339                                         
340                                 processedLabels.insert(lookup[0]->getLabel());
341                                 userLabels.erase(lookup[0]->getLabel());
342                         }
343                                 
344                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
345                                 string saveLabel = lookup[0]->getLabel();
346                         
347                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
348                                 lookup = input->getSharedRAbundVectors(lastLabel);                              
349
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]);  }
353                                         
354                                 processedLabels.insert(lookup[0]->getLabel());
355                                 userLabels.erase(lookup[0]->getLabel());
356                                 
357                                 //restore real lastlabel to save below
358                                 lookup[0]->setLabel(saveLabel);
359                         }
360                                 
361                         //prevent memory leak
362                          
363                         lastLabel = lookup[0]->getLabel();                      
364
365                         //get next line to process
366                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
367                         lookup = input->getSharedRAbundVectors();
368
369                 }
370                 
371                         
372                 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; globaldata->Groups.clear();  return 0; }
373
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();
381                                 needToRun = true;
382                         }else {
383                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
384                         }
385                 }
386                 
387                 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL;  globaldata->Groups.clear(); return 0; }
388                 
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);                              
393
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]);  }
397                         
398                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
399                 }
400                 
401                 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL;  globaldata->Groups.clear(); return 0; }
402                         
403                 //reset groups parameter
404                 globaldata->Groups.clear();  
405                         
406                 delete input;  globaldata->ginput = NULL;
407                 delete read;
408         
409                 return 0;
410         }
411         catch(exception& e) {
412                 m->errorOut(e, "HeatMapSimCommand", "runCommandShared");
413                 exit(1);
414         }
415 }
416 //**********************************************************************************************************************
417 int HeatMapSimCommand::runCommandDist() {
418         try {
419         
420                 vector< vector<double> > matrix;
421                 vector<string> names;
422                 ifstream in;
423                 
424                 //read distance file and create distance vector and names vector
425                 if (format == "phylip") {
426                         //read phylip file
427                         m->openInputFile(phylipfile, in);
428                         
429                         string name;
430                         int numSeqs;
431                         in >> numSeqs >> name; 
432                         
433                         //save name
434                         names.push_back(name);
435                 
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);
440                         }
441                                         
442                         //determine if matrix is square or lower triangle
443                         //if it is square read the distances for the first sequence
444                         char d;
445                         bool square;
446                         while((d=in.get()) != EOF){
447                                 
448                                 //is d a number meaning its square
449                                 if(isalnum(d)){ 
450                                         square = true;
451                                         in.putback(d);
452                                         
453                                         for(int i=0;i<numSeqs;i++){
454                                                 in >> matrix[0][i];
455                                         }
456                                         break;
457                                 }
458                                 
459                                 //is d a line return meaning its lower triangle
460                                 if(d == '\n'){
461                                         square = false;
462                                         break;
463                                 }
464                         }
465                         
466                         //read rest of matrix
467                         if (square == true) { 
468                                 for(int i=1;i<numSeqs;i++){
469                                         in >> name;             
470                                         names.push_back(name);
471                                         
472                                         if (m->control_pressed) { return 0; }
473                                         
474                                         for(int j=0;j<numSeqs;j++) { in >> matrix[i][j];  }
475                                         m->gobble(in);
476                                 }
477                         }else { 
478                                 double dist;
479                                 for(int i=1;i<numSeqs;i++){
480                                         in >> name;     
481                                         names.push_back(name);  
482                                         
483                                         if (m->control_pressed) { return 0; }
484                                         
485                                         for(int j=0;j<i;j++){
486                                                 in >> dist;
487                                                 matrix[i][j] = dist;  matrix[j][i] = dist;
488                                         }
489                                         m->gobble(in);
490                                 }
491                         }
492                         in.close();
493                 }else {
494                         //read names file
495                         NameAssignment* nameMap = new NameAssignment(namefile);
496                         nameMap->readMap();
497                         
498                         //put names in order in vector
499                         for (int i = 0; i < nameMap->size(); i++) {
500                                 names.push_back(nameMap->get(i));
501                         }
502                         
503                         //resize matrix
504                         matrix.resize(nameMap->size());
505                         for (int i = 0; i < nameMap->size(); i++) {
506                                 matrix[i].resize(nameMap->size(), 0.0);
507                         }
508                         
509                         //read column file
510                         string first, second;
511                         double dist;
512                         m->openInputFile(columnfile, in);
513                         
514                         while (!in.eof()) {
515                                 in >> first >> second >> dist; m->gobble(in);
516                                 
517                                 if (m->control_pressed) { return 0; }
518                                 
519                                 map<string, int>::iterator itA = nameMap->find(first);
520                                 map<string, int>::iterator itB = nameMap->find(second);
521                                 
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);  }
524                                 
525                                 //save distance
526                                 matrix[itA->second][itB->second] = dist;
527                                 matrix[itB->second][itA->second] = dist;
528                         }
529                         in.close();
530                         
531                         delete nameMap;
532                 }
533                 
534                 
535                 string outputFileName = heatmap->getPic(matrix, names);
536                 outputNames.push_back(outputFileName); //vector<vector<double>>, vector<string>
537                 outputTypes["svg"].push_back(outputFileName);
538                 
539                 return 0;
540         }
541         catch(exception& e) {
542                 m->errorOut(e, "HeatMapSimCommand", "runCommandDist");
543                 exit(1);
544         }
545 }
546 //**********************************************************************************************************************
547
548
549
550
551
552