]> git.donarmstrong.com Git - mothur.git/blob - heatmapsimcommand.cpp
testing 1.13.0
[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 //**********************************************************************************************************************
24
25 HeatMapSimCommand::HeatMapSimCommand(string option)  {
26         try {
27                 globaldata = GlobalData::getInstance();
28                 abort = false;
29                 allLines = 1;
30                 labels.clear();
31                 Groups.clear();
32                 Estimators.clear();
33                         
34                 //allow user to run help
35                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
36                 
37                 else {
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)));
41                         
42                         OptionParser parser(option);
43                         map<string,string> parameters = parser.getParameters();
44                         
45                         ValidParameters validParameter;
46                         map<string,string>::iterator it;
47                         
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;  }
51                         }
52                         
53                         format = "";
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 = "";         }
56                         
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 = "";          }
60                         else {
61                                 string path;
62                                 it = parameters.find("phylip");
63                                 //user has given a template file
64                                 if(it != parameters.end()){ 
65                                         path = m->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;           }
68                                 }
69                                 
70                                 it = parameters.find("column");
71                                 //user has given a template file
72                                 if(it != parameters.end()){ 
73                                         path = m->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;           }
76                                 }
77                                 
78                                 it = parameters.find("name");
79                                 //user has given a template file
80                                 if(it != parameters.end()){ 
81                                         path = m->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;             }
84                                 }
85                         }
86
87                         //required parameters
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 += m->hasPath(phylipfile); }  }
92                         
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 += m->hasPath(columnfile); } }
97                         
98                         namefile = validParameter.validFile(parameters, "name", true);
99                         if (namefile == "not open") { abort = true; }   
100                         else if (namefile == "not found") { namefile = ""; }
101                         
102                         
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; }
106                         
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; }
109                         }
110                         
111                         if (format == "") { format = "shared"; }
112                         
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 += m->hasPath(globaldata->getSharedFile()); }
117                                 
118                                 label = validParameter.validFile(parameters, "label", false);                   
119                                 if (label == "not found") { label = ""; }
120                                 else { 
121                                         if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
122                                         else { allLines = 1;  }
123                                 }
124                                 
125                                 //if the user has not specified any labels use the ones from read.otu
126                                 if (label == "") {  
127                                         allLines = globaldata->allLines; 
128                                         labels = globaldata->labels; 
129                                 }
130                                 
131                                 calc = validParameter.validFile(parameters, "calc", false);                     
132                                 if (calc == "not found") { calc = "jest-thetayc";  }
133                                 else { 
134                                         if (calc == "default")  {  calc = "jest-thetayc";  }
135                                 }
136                                 m->splitAtDash(calc, Estimators);
137                                 
138                                 groups = validParameter.validFile(parameters, "groups", false);                 
139                                 if (groups == "not found") { groups = ""; }
140                                 else { 
141                                         m->splitAtDash(groups, Groups);
142                                         globaldata->Groups = Groups;
143                                 }
144                         }
145                         
146                         if (abort == false) {
147                                 validCalculator = new ValidCalculators();
148                         
149                                 int i;
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());
172                                                 }
173                                         }
174                                 }
175                                 
176                         }
177                 }
178
179                                 
180
181         }
182         catch(exception& e) {
183                 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
184                 exit(1);
185         }
186 }
187
188 //**********************************************************************************************************************
189
190 void HeatMapSimCommand::help(){
191         try {
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");
209
210         }
211         catch(exception& e) {
212                 m->errorOut(e, "HeatMapSimCommand", "help");
213                 exit(1);
214         }
215 }
216
217 //**********************************************************************************************************************
218
219 HeatMapSimCommand::~HeatMapSimCommand(){}
220
221 //**********************************************************************************************************************
222
223 int HeatMapSimCommand::execute(){
224         try {
225         
226                 if (abort == true)  { return 0; }
227                 
228                 heatmap = new HeatMapSim(outputDir);
229                 
230                 if (format == "shared") {
231                         runCommandShared();
232                 }else if (format == "phylip") {
233                         globaldata->inputFileName = phylipfile;
234                         runCommandDist();
235                 }else if (format == "column") {
236                         globaldata->inputFileName = columnfile;
237                         runCommandDist();
238                 }
239                 
240                 delete heatmap;
241                 delete validCalculator;
242                 
243                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());  } return 0; }
244                 
245                 m->mothurOutEndLine();
246                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
247                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
248                 m->mothurOutEndLine();
249                 
250                 return 0;
251         }
252         catch(exception& e) {
253                 m->errorOut(e, "HeatMapSimCommand", "execute");
254                 exit(1);
255         }
256 }
257
258 //**********************************************************************************************************************
259 int HeatMapSimCommand::runCommandShared() {
260         try {
261                 //if the users entered no valid calculators don't execute command
262                 if (heatCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
263                 
264                 //you have groups
265                 read = new ReadOTUFile(globaldata->inputFileName);      
266                 read->read(&*globaldata); 
267                                 
268                 input = globaldata->ginput;
269                 lookup = input->getSharedRAbundVectors();
270                 string lastLabel = lookup[0]->getLabel();
271                         
272                 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); return 0;}
273                                 
274                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
275                 set<string> processedLabels;
276                 set<string> userLabels = labels;
277                 
278                 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; }
279                 
280                 //as long as you are not at the end of the file or done wih the lines you want
281                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
282                         
283                         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; }
284
285                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
286         
287                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
288                                 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
289                                 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); }
290                                         
291                                 processedLabels.insert(lookup[0]->getLabel());
292                                 userLabels.erase(lookup[0]->getLabel());
293                         }
294                                 
295                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
296                                 string saveLabel = lookup[0]->getLabel();
297                         
298                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
299                                 lookup = input->getSharedRAbundVectors(lastLabel);                              
300
301                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
302                                 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
303                                 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); }
304                                         
305                                 processedLabels.insert(lookup[0]->getLabel());
306                                 userLabels.erase(lookup[0]->getLabel());
307                                 
308                                 //restore real lastlabel to save below
309                                 lookup[0]->setLabel(saveLabel);
310                         }
311                                 
312                         //prevent memory leak
313                          
314                         lastLabel = lookup[0]->getLabel();                      
315
316                         //get next line to process
317                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
318                         lookup = input->getSharedRAbundVectors();
319
320                 }
321                 
322                         
323                 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL; globaldata->Groups.clear();  return 0; }
324
325                 //output error messages about any remaining user labels
326                 set<string>::iterator it;
327                 bool needToRun = false;
328                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
329                         m->mothurOut("Your file does not include the label " + *it); 
330                         if (processedLabels.count(lastLabel) != 1) {
331                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
332                                 needToRun = true;
333                         }else {
334                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
335                         }
336                 }
337                 
338                 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL;  globaldata->Groups.clear(); return 0; }
339                 
340                 //run last label if you need to
341                 if (needToRun == true)  {
342                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) { delete lookup[i]; } } 
343                         lookup = input->getSharedRAbundVectors(lastLabel);                              
344
345                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
346                         vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
347                         for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); }
348                         
349                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
350                 }
351                 
352                 if (m->control_pressed) { delete read; delete input; globaldata->ginput = NULL;  globaldata->Groups.clear(); return 0; }
353                         
354                 //reset groups parameter
355                 globaldata->Groups.clear();  
356                         
357                 delete input;  globaldata->ginput = NULL;
358                 delete read;
359         
360                 return 0;
361         }
362         catch(exception& e) {
363                 m->errorOut(e, "HeatMapSimCommand", "runCommandShared");
364                 exit(1);
365         }
366 }
367 //**********************************************************************************************************************
368 int HeatMapSimCommand::runCommandDist() {
369         try {
370         
371                 vector< vector<double> > matrix;
372                 vector<string> names;
373                 ifstream in;
374                 
375                 //read distance file and create distance vector and names vector
376                 if (format == "phylip") {
377                         //read phylip file
378                         m->openInputFile(phylipfile, in);
379                         
380                         string name;
381                         int numSeqs;
382                         in >> numSeqs >> name; 
383                         
384                         //save name
385                         names.push_back(name);
386                 
387                         //resize the matrix and fill with zeros
388                         matrix.resize(numSeqs); 
389                         for(int i = 0; i < numSeqs; i++) {
390                                 matrix[i].resize(numSeqs, 0.0);
391                         }
392                                         
393                         //determine if matrix is square or lower triangle
394                         //if it is square read the distances for the first sequence
395                         char d;
396                         bool square;
397                         while((d=in.get()) != EOF){
398                                 
399                                 //is d a number meaning its square
400                                 if(isalnum(d)){ 
401                                         square = true;
402                                         in.putback(d);
403                                         
404                                         for(int i=0;i<numSeqs;i++){
405                                                 in >> matrix[0][i];
406                                         }
407                                         break;
408                                 }
409                                 
410                                 //is d a line return meaning its lower triangle
411                                 if(d == '\n'){
412                                         square = false;
413                                         break;
414                                 }
415                         }
416                         
417                         //read rest of matrix
418                         if (square == true) { 
419                                 for(int i=1;i<numSeqs;i++){
420                                         in >> name;             
421                                         names.push_back(name);
422                                         
423                                         if (m->control_pressed) { return 0; }
424                                         
425                                         for(int j=0;j<numSeqs;j++) { in >> matrix[i][j];  }
426                                         m->gobble(in);
427                                 }
428                         }else { 
429                                 double dist;
430                                 for(int i=1;i<numSeqs;i++){
431                                         in >> name;     
432                                         names.push_back(name);  
433                                         
434                                         if (m->control_pressed) { return 0; }
435                                         
436                                         for(int j=0;j<i;j++){
437                                                 in >> dist;
438                                                 matrix[i][j] = dist;  matrix[j][i] = dist;
439                                         }
440                                         m->gobble(in);
441                                 }
442                         }
443                         in.close();
444                 }else {
445                         //read names file
446                         NameAssignment* nameMap = new NameAssignment(namefile);
447                         nameMap->readMap();
448                         
449                         //put names in order in vector
450                         for (int i = 0; i < nameMap->size(); i++) {
451                                 names.push_back(nameMap->get(i));
452                         }
453                         
454                         //resize matrix
455                         matrix.resize(nameMap->size());
456                         for (int i = 0; i < nameMap->size(); i++) {
457                                 matrix[i].resize(nameMap->size(), 0.0);
458                         }
459                         
460                         //read column file
461                         string first, second;
462                         double dist;
463                         m->openInputFile(columnfile, in);
464                         
465                         while (!in.eof()) {
466                                 in >> first >> second >> dist; m->gobble(in);
467                                 
468                                 if (m->control_pressed) { return 0; }
469                                 
470                                 map<string, int>::iterator itA = nameMap->find(first);
471                                 map<string, int>::iterator itB = nameMap->find(second);
472                                 
473                                 if(itA == nameMap->end()){  cerr << "AAError: Sequence '" << first << "' was not found in the names file, please correct\n"; exit(1);  }
474                                 if(itB == nameMap->end()){  cerr << "ABError: Sequence '" << second << "' was not found in the names file, please correct\n"; exit(1);  }
475                                 
476                                 //save distance
477                                 matrix[itA->second][itB->second] = dist;
478                                 matrix[itB->second][itA->second] = dist;
479                         }
480                         in.close();
481                         
482                         delete nameMap;
483                 }
484                 
485
486                 outputNames.push_back(heatmap->getPic(matrix, names)); //vector<vector<double>>, vector<string>
487                 
488                 return 0;
489         }
490         catch(exception& e) {
491                 m->errorOut(e, "HeatMapSimCommand", "runCommandDist");
492                 exit(1);
493         }
494 }
495 //**********************************************************************************************************************
496
497
498
499
500
501