]> git.donarmstrong.com Git - mothur.git/blob - heatmapsimcommand.cpp
Merge remote-tracking branch 'mothur/master'
[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 vector<string> HeatMapSimCommand::setParameters(){      
25         try {
26                 CommandParameter pshared("shared", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none",false,false); parameters.push_back(pshared);        
27                 CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "none",false,false); parameters.push_back(pphylip);
28                 CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none",false,false); parameters.push_back(pname);
29         CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none",false,false); parameters.push_back(pcount);
30                 CommandParameter pcolumn("column", "InputTypes", "", "", "PhylipColumnShared", "PhylipColumnShared", "ColumnName",false,false); parameters.push_back(pcolumn);          
31                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
32                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
33                 CommandParameter pcalc("calc", "Multiple", "jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan-morisitahorn-braycurtis", "jest-thetayc", "", "", "",true,false); parameters.push_back(pcalc);
34                 CommandParameter pfontsize("fontsize", "Number", "", "24", "", "", "",false,false); parameters.push_back(pfontsize);
35                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
36                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
37                 
38                 vector<string> myArray;
39                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
40                 return myArray;
41         }
42         catch(exception& e) {
43                 m->errorOut(e, "HeatMapSimCommand", "setParameters");
44                 exit(1);
45         }
46 }
47 //**********************************************************************************************************************
48 string HeatMapSimCommand::getHelpString(){      
49         try {
50                 string helpString = "";
51                 ValidCalculators validCalculator;
52                 helpString += "The heatmap.sim command parameters are shared, phylip, column, name, count, groups, calc, fontsize and label.  shared or phylip or column and name are required unless valid current files exist.\n";
53                 helpString += "There are two ways to use the heatmap.sim command. The first is with a shared file, and you may use the groups, label and calc parameter. \n";
54                 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like included in your heatmap.\n";
55                 helpString += "The group names are separated by dashes. The label parameter allows you to select what distance levels you would like a heatmap created for, and is also separated by dashes.\n";
56                 helpString += "The fontsize parameter allows you to adjust the font size of the picture created, default=24.\n";
57                 helpString += "The heatmap.sim command should be in the following format: heatmap.sim(groups=yourGroups, calc=yourCalc, label=yourLabels).\n";
58                 helpString += "Example heatmap.sim(groups=A-B-C, calc=jabund).\n";
59                 helpString += "The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n";
60                 helpString +=  validCalculator.printCalc("heat");
61                 helpString += "The default value for calc is jclass-thetayc.\n";
62                 helpString += "The heatmap.sim command outputs a .svg file for each calculator you choose at each label you specify.\n";
63                 helpString += "The second way to use the heatmap.sim command is with a distance file representing the distance bewteen your groups. \n";
64                 helpString += "Using the command this way, the phylip or column parameter are required, and only one may be used.  If you use a column file the name filename is required. \n";
65                 helpString += "The heatmap.sim command should be in the following format: heatmap.sim(phylip=yourDistanceFile).\n";
66                 helpString += "Example heatmap.sim(phylip=amazonGroups.dist).\n";
67                 helpString += "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n";
68                 return helpString;
69         }
70         catch(exception& e) {
71                 m->errorOut(e, "HeatMapSimCommand", "getHelpString");
72                 exit(1);
73         }
74 }
75 //**********************************************************************************************************************
76
77 string HeatMapSimCommand::getOutputFileNameTag(string type, string inputName=""){       
78         try {
79         string outputFileName = "";
80                 map<string, vector<string> >::iterator it;
81         
82         //is this a type this command creates
83         it = outputTypes.find(type);
84         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
85         else {
86             if (type == "svg")            {   outputFileName =  "svg";   }
87             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
88         }
89         return outputFileName;
90         }
91         catch(exception& e) {
92                 m->errorOut(e, "HeatMapSimCommand", "getOutputFileNameTag");
93                 exit(1);
94         }
95 }
96
97 //**********************************************************************************************************************
98 HeatMapSimCommand::HeatMapSimCommand(){ 
99         try {
100                 abort = true; calledHelp = true; 
101                 setParameters();
102                 vector<string> tempOutNames;
103                 outputTypes["svg"] = tempOutNames;
104         }
105         catch(exception& e) {
106                 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
107                 exit(1);
108         }
109 }
110 //**********************************************************************************************************************
111
112 HeatMapSimCommand::HeatMapSimCommand(string option)  {
113         try {
114                 abort = false; calledHelp = false;   
115                 allLines = 1;
116                         
117                 //allow user to run help
118                 if(option == "help") {  help(); abort = true; calledHelp = true; }
119                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
120                 
121                 else {
122                         vector<string> myArray = setParameters();
123                         
124                         OptionParser parser(option);
125                         map<string,string> parameters = parser.getParameters();
126                         
127                         ValidParameters validParameter;
128                         map<string,string>::iterator it;
129                         
130                         //check to make sure all parameters are valid for command
131                         for (it = parameters.begin(); it != parameters.end(); it++) { 
132                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
133                         }
134                         
135                         //initialize outputTypes
136                         vector<string> tempOutNames;
137                         outputTypes["svg"] = tempOutNames;
138                         
139                         format = "";
140                                 
141                         //if the user changes the input directory command factory will send this info to us in the output parameter 
142                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
143                         if (inputDir == "not found"){   inputDir = "";          }
144                         else {
145                                 string path;
146                                 it = parameters.find("phylip");
147                                 //user has given a template file
148                                 if(it != parameters.end()){ 
149                                         path = m->hasPath(it->second);
150                                         //if the user has not given a path then, add inputdir. else leave path alone.
151                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
152                                 }
153                                 
154                                 it = parameters.find("column");
155                                 //user has given a template file
156                                 if(it != parameters.end()){ 
157                                         path = m->hasPath(it->second);
158                                         //if the user has not given a path then, add inputdir. else leave path alone.
159                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
160                                 }
161                                 
162                                 it = parameters.find("name");
163                                 //user has given a template file
164                                 if(it != parameters.end()){ 
165                                         path = m->hasPath(it->second);
166                                         //if the user has not given a path then, add inputdir. else leave path alone.
167                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
168                                 }
169                                 
170                                 it = parameters.find("shared");
171                                 //user has given a template file
172                                 if(it != parameters.end()){ 
173                                         path = m->hasPath(it->second);
174                                         //if the user has not given a path then, add inputdir. else leave path alone.
175                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
176                                 }
177                 
178                 it = parameters.find("count");
179                                 //user has given a template file
180                                 if(it != parameters.end()){ 
181                                         path = m->hasPath(it->second);
182                                         //if the user has not given a path then, add inputdir. else leave path alone.
183                                         if (path == "") {       parameters["count"] = inputDir + it->second;            }
184                                 }
185                         }
186
187                         //required parameters
188                         phylipfile = validParameter.validFile(parameters, "phylip", true);
189                         if (phylipfile == "not open") { abort = true; }
190                         else if (phylipfile == "not found") { phylipfile = ""; }        
191                         else {  format = "phylip";      inputfile = phylipfile; m-> setPhylipFile(phylipfile); if (outputDir == "") { outputDir += m->hasPath(phylipfile); }  }
192                         
193                         columnfile = validParameter.validFile(parameters, "column", true);
194                         if (columnfile == "not open") { abort = true; } 
195                         else if (columnfile == "not found") { columnfile = ""; }
196                         else {  format = "column";      inputfile = columnfile; m->setColumnFile(columnfile); if (outputDir == "") { outputDir += m->hasPath(columnfile); } }
197                         
198                         sharedfile = validParameter.validFile(parameters, "shared", true);
199                         if (sharedfile == "not open") { abort = true; } 
200                         else if (sharedfile == "not found") { sharedfile = ""; }
201                         else {  format = "shared";      inputfile = sharedfile; m->setSharedFile(sharedfile); if (outputDir == "") { outputDir += m->hasPath(sharedfile); } }
202                         
203                         namefile = validParameter.validFile(parameters, "name", true);
204                         if (namefile == "not open") { abort = true; }   
205                         else if (namefile == "not found") { namefile = ""; }
206                         else { m->setNameFile(namefile); }
207                         
208             countfile = validParameter.validFile(parameters, "count", true);
209                         if (countfile == "not open") { abort = true; countfile = ""; }  
210                         else if (countfile == "not found") { countfile = ""; }
211                         else { m->setCountTableFile(countfile); }
212                         
213             if ((countfile != "") && (namefile != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
214                         
215                         //error checking on files                       
216                         if ((sharedfile == "") && ((phylipfile == "") && (columnfile == "")))   { 
217                                 sharedfile = m->getSharedFile(); 
218                                 if (sharedfile != "") { format = "shared"; inputfile = sharedfile; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
219                                 else { 
220                                         //is there are current file available for either of these?
221                                         //give priority to column, then phylip
222                                         columnfile = m->getColumnFile(); 
223                                         if (columnfile != "") {  format = "column"; inputfile = columnfile; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
224                                         else { 
225                                                 phylipfile = m->getPhylipFile(); 
226                                                 if (phylipfile != "") { format = "phylip";  inputfile = phylipfile; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
227                                                 else { 
228                                                         m->mothurOut("No valid current files. You must provide a shared or phylip or column file."); m->mothurOutEndLine(); 
229                                                         abort = true;
230                                                 }
231                                         }
232                                 }
233                         }
234                         else if ((phylipfile != "") && (columnfile != "")) { m->mothurOut("When running the heatmap.sim command with a distance file you may not use both the column and the phylip parameters."); m->mothurOutEndLine(); abort = true; }
235                         
236                         if (columnfile != "") {
237                                 if (namefile == "") { 
238                                         namefile = m->getNameFile(); 
239                                         if (namefile != "") {  m->mothurOut("Using " + namefile + " as input file for the name parameter."); m->mothurOutEndLine(); }
240                                         else { 
241                         countfile = m->getCountTableFile(); 
242                                                 if (countfile != "") {  m->mothurOut("Using " + countfile + " as input file for the count parameter."); m->mothurOutEndLine(); }
243                         else { 
244                             m->mothurOut("You need to provide a name or count file if you are going to use the column format."); m->mothurOutEndLine(); 
245                             abort = true; 
246                         }       
247                                         }       
248                                 }
249                         }
250                         
251                         
252                         //if the user changes the output directory command factory will send this info to us in the output parameter 
253                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);              }
254
255                         //check for optional parameter and set defaults
256                         // ...at some point should added some additional type checking...
257                                                         
258                         label = validParameter.validFile(parameters, "label", false);                   
259                         if (label == "not found") { label = ""; }
260                         else { 
261                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
262                                 else { allLines = 1;  }
263                         }
264                                 
265                         calc = validParameter.validFile(parameters, "calc", false);                     
266                         if (calc == "not found") { calc = "jest-thetayc";  }
267                         else { 
268                                 if (calc == "default")  {  calc = "jest-thetayc";  }
269                         }
270                         m->splitAtDash(calc, Estimators);
271                         if (m->inUsersGroups("citation", Estimators)) { 
272                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
273                                 //remove citation from list of calcs
274                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
275                         }
276                                 
277                         groups = validParameter.validFile(parameters, "groups", false);                 
278                         if (groups == "not found") { groups = ""; }
279                         else { 
280                                 m->splitAtDash(groups, Groups);
281                                 m->setGroups(Groups);
282                         }
283                         
284                         string temp = validParameter.validFile(parameters, "fontsize", false);                          if (temp == "not found") { temp = "24"; }
285                         m->mothurConvert(temp, fontsize);
286                         
287                         if (abort == false) {
288                                 ValidCalculators validCalculator;
289                         
290                                 int i;
291                                 for (i=0; i<Estimators.size(); i++) {
292                                         if (validCalculator.isValidCalculator("heat", Estimators[i]) == true) { 
293                                                 if (Estimators[i] == "jabund") {        
294                                                         heatCalculators.push_back(new JAbund());
295                                                 }else if (Estimators[i] == "sorabund") { 
296                                                         heatCalculators.push_back(new SorAbund());
297                                                 }else if (Estimators[i] == "jclass") { 
298                                                         heatCalculators.push_back(new Jclass());
299                                                 }else if (Estimators[i] == "sorclass") { 
300                                                         heatCalculators.push_back(new SorClass());
301                                                 }else if (Estimators[i] == "jest") { 
302                                                         heatCalculators.push_back(new Jest());
303                                                 }else if (Estimators[i] == "sorest") { 
304                                                         heatCalculators.push_back(new SorEst());
305                                                 }else if (Estimators[i] == "thetayc") { 
306                                                         heatCalculators.push_back(new ThetaYC());
307                                                 }else if (Estimators[i] == "thetan") { 
308                                                         heatCalculators.push_back(new ThetaN());
309                                                 }else if (Estimators[i] == "morisitahorn") { 
310                                                         heatCalculators.push_back(new MorHorn());
311                                                 }else if (Estimators[i] == "braycurtis") { 
312                                                         heatCalculators.push_back(new BrayCurtis());
313                                                 }
314                                         }
315                                 }
316                                 
317                         }
318                 }
319
320                                 
321
322         }
323         catch(exception& e) {
324                 m->errorOut(e, "HeatMapSimCommand", "HeatMapSimCommand");
325                 exit(1);
326         }
327 }
328 //**********************************************************************************************************************
329
330 int HeatMapSimCommand::execute(){
331         try {
332         
333                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
334                 
335                 heatmap = new HeatMapSim(outputDir, inputfile, fontsize);
336                 
337                 if (format == "shared") {
338                         runCommandShared();
339                 }else{  runCommandDist();       }
340                 
341                 delete heatmap;
342                 
343                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);  } outputTypes.clear(); return 0; }
344                 
345                 m->mothurOutEndLine();
346                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
347                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
348                 m->mothurOutEndLine();
349                 
350                 return 0;
351         }
352         catch(exception& e) {
353                 m->errorOut(e, "HeatMapSimCommand", "execute");
354                 exit(1);
355         }
356 }
357
358 //**********************************************************************************************************************
359 int HeatMapSimCommand::runCommandShared() {
360         try {
361                 //if the users entered no valid calculators don't execute command
362                 if (heatCalculators.size() == 0) { m->mothurOut("No valid calculators."); m->mothurOutEndLine(); return 0; }
363                 
364                 input = new InputData(sharedfile, "sharedfile");
365                 lookup = input->getSharedRAbundVectors();
366                 string lastLabel = lookup[0]->getLabel();
367                         
368                 if (lookup.size() < 2) { m->mothurOut("You have not provided enough valid groups.  I cannot run the command."); m->mothurOutEndLine(); return 0;}
369                                 
370                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
371                 set<string> processedLabels;
372                 set<string> userLabels = labels;
373                 
374                 if (m->control_pressed) {  delete input;  for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }  m->clearGroups(); return 0; }
375                 
376                 //as long as you are not at the end of the file or done wih the lines you want
377                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
378                         
379                         if (m->control_pressed) { delete input;  for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } m->clearGroups(); return 0; }
380
381                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
382         
383                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
384                                 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
385                                 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]);  outputTypes["svg"].push_back(outfilenames[i]); }
386                                         
387                                 processedLabels.insert(lookup[0]->getLabel());
388                                 userLabels.erase(lookup[0]->getLabel());
389                         }
390                                 
391                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
392                                 string saveLabel = lookup[0]->getLabel();
393                         
394                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
395                                 lookup = input->getSharedRAbundVectors(lastLabel);                              
396
397                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
398                                 vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
399                                 for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]);  }
400                                         
401                                 processedLabels.insert(lookup[0]->getLabel());
402                                 userLabels.erase(lookup[0]->getLabel());
403                                 
404                                 //restore real lastlabel to save below
405                                 lookup[0]->setLabel(saveLabel);
406                         }
407                                 
408                         //prevent memory leak
409                          
410                         lastLabel = lookup[0]->getLabel();                      
411
412                         //get next line to process
413                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
414                         lookup = input->getSharedRAbundVectors();
415
416                 }
417                 
418                         
419                 if (m->control_pressed) {  delete input;  m->clearGroups();  return 0; }
420
421                 //output error messages about any remaining user labels
422                 set<string>::iterator it;
423                 bool needToRun = false;
424                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
425                         m->mothurOut("Your file does not include the label " + *it); 
426                         if (processedLabels.count(lastLabel) != 1) {
427                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
428                                 needToRun = true;
429                         }else {
430                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
431                         }
432                 }
433                 
434                 if (m->control_pressed) {  delete input;  m->clearGroups(); return 0; }
435                 
436                 //run last label if you need to
437                 if (needToRun == true)  {
438                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) { delete lookup[i]; } } 
439                         lookup = input->getSharedRAbundVectors(lastLabel);                              
440
441                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
442                         vector<string> outfilenames = heatmap->getPic(lookup, heatCalculators);
443                         for(int i = 0; i < outfilenames.size(); i++) { outputNames.push_back(outfilenames[i]); outputTypes["svg"].push_back(outfilenames[i]);  }
444                         
445                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
446                 }
447                 
448                 if (m->control_pressed) {  delete input;  m->clearGroups(); return 0; }
449                         
450                 //reset groups parameter
451                 m->clearGroups();  
452                         
453                 delete input;  
454         
455                 return 0;
456         }
457         catch(exception& e) {
458                 m->errorOut(e, "HeatMapSimCommand", "runCommandShared");
459                 exit(1);
460         }
461 }
462 //**********************************************************************************************************************
463 int HeatMapSimCommand::runCommandDist() {
464         try {
465         
466                 vector< vector<double> > matrix;
467                 vector<string> names;
468                 ifstream in;
469                 
470                 //read distance file and create distance vector and names vector
471                 if (format == "phylip") {
472                         //read phylip file
473                         m->openInputFile(phylipfile, in);
474                         
475                         string name;
476                         int numSeqs;
477                         in >> numSeqs >> name; 
478                         
479                         //save name
480                         names.push_back(name);
481                 
482                         //resize the matrix and fill with zeros
483                         matrix.resize(numSeqs); 
484                         for(int i = 0; i < numSeqs; i++) {
485                                 matrix[i].resize(numSeqs, 0.0);
486                         }
487                                         
488                         //determine if matrix is square or lower triangle
489                         //if it is square read the distances for the first sequence
490                         char d;
491                         bool square;
492                         while((d=in.get()) != EOF){
493                                 
494                                 //is d a number meaning its square
495                                 if(isalnum(d)){ 
496                                         square = true;
497                                         in.putback(d);
498                                         
499                                         for(int i=0;i<numSeqs;i++){
500                                                 in >> matrix[0][i];
501                                         }
502                                         break;
503                                 }
504                                 
505                                 //is d a line return meaning its lower triangle
506                                 if(d == '\n'){
507                                         square = false;
508                                         break;
509                                 }
510                         }
511                         
512                         //read rest of matrix
513                         if (square == true) { 
514                                 for(int i=1;i<numSeqs;i++){
515                                         in >> name;             
516                                         names.push_back(name);
517                                         
518                                         if (m->control_pressed) { return 0; }
519                                         
520                                         for(int j=0;j<numSeqs;j++) { in >> matrix[i][j];  }
521                                         m->gobble(in);
522                                 }
523                         }else { 
524                                 double dist;
525                                 for(int i=1;i<numSeqs;i++){
526                                         in >> name;     
527                                         names.push_back(name);  
528                                         
529                                         if (m->control_pressed) { return 0; }
530                                         
531                                         for(int j=0;j<i;j++){
532                                                 in >> dist;
533                                                 matrix[i][j] = dist;  matrix[j][i] = dist;
534                                         }
535                                         m->gobble(in);
536                                 }
537                         }
538                         in.close();
539                 }else {
540                         //read names file
541                         NameAssignment* nameMap;
542             CountTable ct; 
543             if (namefile != "") { 
544                 nameMap = new NameAssignment(namefile);
545                 nameMap->readMap();
546                 
547                 //put names in order in vector
548                 for (int i = 0; i < nameMap->size(); i++) {
549                     names.push_back(nameMap->get(i));
550                 }
551              }else if (countfile != "") {
552                 nameMap = NULL;
553                 ct.readTable(countfile);
554                 names = ct.getNamesOfSeqs();
555             }
556                         
557             //resize matrix
558             matrix.resize(names.size());
559             for (int i = 0; i < names.size(); i++) {
560                 matrix[i].resize(names.size(), 0.0);
561             }
562                                                 
563                         //read column file
564                         string first, second;
565                         double dist;
566                         m->openInputFile(columnfile, in);
567                         
568                         while (!in.eof()) {
569                                 in >> first >> second >> dist; m->gobble(in);
570                                 
571                                 if (m->control_pressed) { return 0; }
572                                 
573                 if (namefile != "") {
574                     map<string, int>::iterator itA = nameMap->find(first);
575                     map<string, int>::iterator itB = nameMap->find(second);
576                     
577                     if(itA == nameMap->end()){  m->mothurOut("AAError: Sequence '" + first + "' was not found in the names file, please correct\n"); exit(1);  }
578                     if(itB == nameMap->end()){  m->mothurOut("ABError: Sequence '" + second + "' was not found in the names file, please correct\n"); exit(1);  }
579                     
580                     //save distance
581                     matrix[itA->second][itB->second] = dist;
582                     matrix[itB->second][itA->second] = dist;
583                 }else if (countfile != "") {
584                     int itA = ct.get(first);
585                     int itB = ct.get(second);
586                     matrix[itA][itB] = dist;
587                     matrix[itB][itA] = dist;
588                 }
589                         }
590                         in.close();
591                         
592                         if (namefile != "") { delete nameMap; }
593                 }
594                 
595                 
596                 string outputFileName = heatmap->getPic(matrix, names);
597                 outputNames.push_back(outputFileName); //vector<vector<double>>, vector<string>
598                 outputTypes["svg"].push_back(outputFileName);
599                 
600                 return 0;
601         }
602         catch(exception& e) {
603                 m->errorOut(e, "HeatMapSimCommand", "runCommandDist");
604                 exit(1);
605         }
606 }
607 //**********************************************************************************************************************
608
609
610
611
612
613