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