]> git.donarmstrong.com Git - mothur.git/blob - nmdscommand.cpp
fixes while testing 1.33.0
[mothur.git] / nmdscommand.cpp
1 /*
2  *  nmdscommand.cpp
3  *  mothur
4  *
5  *  Created by westcott on 1/11/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "nmdscommand.h"
11 #include "readphylipvector.h"
12
13 //**********************************************************************************************************************
14 vector<string> NMDSCommand::setParameters(){    
15         try {
16                 CommandParameter paxes("axes", "InputTypes", "", "", "none", "none", "none","",false,false,true); parameters.push_back(paxes);
17                 CommandParameter pphylip("phylip", "InputTypes", "", "", "none", "none", "none","nmds-stress",false,true,true); parameters.push_back(pphylip);
18                 CommandParameter pmaxdim("maxdim", "Number", "", "2", "", "", "","",false,false); parameters.push_back(pmaxdim);
19                 CommandParameter pmindim("mindim", "Number", "", "2", "", "", "","",false,false); parameters.push_back(pmindim);
20                 CommandParameter piters("iters", "Number", "", "10", "", "", "","",false,false); parameters.push_back(piters);
21                 CommandParameter pmaxiters("maxiters", "Number", "", "500", "", "", "","",false,false); parameters.push_back(pmaxiters);
22                 CommandParameter pepsilon("epsilon", "Number", "", "0.000000000001", "", "", "","",false,false); parameters.push_back(pepsilon);
23                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
24                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
25                 
26                 vector<string> myArray;
27                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
28                 return myArray;
29         }
30         catch(exception& e) {
31                 m->errorOut(e, "NMDSCommand", "setParameters");
32                 exit(1);
33         }
34 }
35 //**********************************************************************************************************************
36 string NMDSCommand::getHelpString(){    
37         try {
38                 string helpString = "";
39                 helpString += "The nmds command is modelled after the nmds code written in R by Sarah Goslee, using Non-metric multidimensional scaling function using the majorization algorithm from Borg & Groenen 1997, Modern Multidimensional Scaling.\n";
40                 helpString += "The nmds command parameters are phylip, axes, mindim, maxdim, maxiters, iters and epsilon.\n"; 
41                 helpString += "The phylip parameter allows you to enter your distance file.\n"; 
42                 helpString += "The axes parameter allows you to enter a file containing a starting configuration.\n";
43                 helpString += "The maxdim parameter allows you to select the maximum dimensions to use. Default=2\n"; 
44                 helpString += "The mindim parameter allows you to select the minimum dimensions to use. Default=2\n";
45                 helpString += "The maxiters parameter allows you to select the maximum number of iters to try with each random configuration. Default=500\n"; 
46                 helpString += "The iters parameter allows you to select the number of random configuration to try. Default=10\n"; 
47                 helpString += "The epsilon parameter allows you to select set an acceptable stopping point. Default=1e-12.\n"; 
48                 helpString += "Example nmds(phylip=yourDistanceFile).\n";
49                 helpString += "Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourDistanceFile).\n";
50                 return helpString;
51         }
52         catch(exception& e) {
53                 m->errorOut(e, "NMDSCommand", "getHelpString");
54                 exit(1);
55         }
56 }
57 //**********************************************************************************************************************
58 string NMDSCommand::getOutputPattern(string type) {
59     try {
60         string pattern = "";
61         
62         if (type == "nmds") {  pattern = "[filename],nmds.axes"; } 
63         else if (type == "stress") {  pattern = "[filename],nmds.stress"; } 
64         else if (type == "iters") {  pattern = "[filename],nmds.iters"; } 
65         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
66         
67         return pattern;
68     }
69     catch(exception& e) {
70         m->errorOut(e, "NMDSCommand", "getOutputPattern");
71         exit(1);
72     }
73 }
74
75 //**********************************************************************************************************************
76 NMDSCommand::NMDSCommand(){     
77         try {
78                 abort = true; calledHelp = true; 
79                 setParameters();
80                 vector<string> tempOutNames;
81                 outputTypes["nmds"] = tempOutNames;
82                 outputTypes["stress"] = tempOutNames;
83                 outputTypes["iters"] = tempOutNames;
84         }
85         catch(exception& e) {
86                 m->errorOut(e, "NMDSCommand", "NMDSCommand");
87                 exit(1);
88         }
89 }
90 //**********************************************************************************************************************
91
92 NMDSCommand::NMDSCommand(string option)  {
93         try {
94                 abort = false; calledHelp = false;   
95                 
96                 //allow user to run help
97                 if(option == "help") { help(); abort = true; calledHelp = true; }
98                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
99                 
100                 else {
101                         vector<string> myArray = setParameters();
102                         
103                         OptionParser parser(option);
104                         map<string, string> parameters = parser. getParameters();
105                         
106                         ValidParameters validParameter;
107                         map<string, string>::iterator it;
108                         
109                         //check to make sure all parameters are valid for command
110                         for (it = parameters.begin(); it != parameters.end(); it++) { 
111                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
112                         }
113                         //if the user changes the input directory command factory will send this info to us in the output parameter 
114                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
115                         if (inputDir == "not found"){   inputDir = "";          }
116                         else {
117                                 string path;
118                                 it = parameters.find("phylip");
119                                 //user has given a template file
120                                 if(it != parameters.end()){ 
121                                         path = m->hasPath(it->second);
122                                         //if the user has not given a path then, add inputdir. else leave path alone.
123                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
124                                 }
125                                 
126                                 it = parameters.find("axes");
127                                 //user has given a template file
128                                 if(it != parameters.end()){ 
129                                         path = m->hasPath(it->second);
130                                         //if the user has not given a path then, add inputdir. else leave path alone.
131                                         if (path == "") {       parameters["axes"] = inputDir + it->second;             }
132                                 }
133                         }
134                         
135                         //initialize outputTypes
136                         vector<string> tempOutNames;
137                         outputTypes["nmds"] = tempOutNames;
138                         outputTypes["iters"] = tempOutNames;
139                         outputTypes["stress"] = tempOutNames;
140                         
141                         //required parameters
142                         phylipfile = validParameter.validFile(parameters, "phylip", true);
143                         if (phylipfile == "not open") { phylipfile = ""; abort = true; }
144                         else if (phylipfile == "not found") {                           
145                                 //if there is a current phylip file, use it
146                                 phylipfile = m->getPhylipFile(); 
147                                 if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
148                                 else {  m->mothurOut("You have no current phylip file and the phylip parameter is required."); m->mothurOutEndLine(); abort = true; }
149                         }else { m->setPhylipFile(phylipfile); } 
150                         
151                         axesfile = validParameter.validFile(parameters, "axes", true);
152                         if (axesfile == "not open") { axesfile = ""; abort = true; }
153                         else if (axesfile == "not found") { axesfile = "";  }                           
154                         
155                         //if the user changes the output directory command factory will send this info to us in the output parameter 
156                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
157                                 outputDir = ""; 
158                                 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it      
159                         }
160                         
161                         string temp = validParameter.validFile(parameters, "mindim", false);    if (temp == "not found") {      temp = "2";     }
162                         m->mothurConvert(temp, mindim);
163                         
164                         temp = validParameter.validFile(parameters, "maxiters", false); if (temp == "not found") {      temp = "500";   }
165                         m->mothurConvert(temp, maxIters);
166                         
167                         temp = validParameter.validFile(parameters, "iters", false);    if (temp == "not found") {      temp = "10";    }
168                         m->mothurConvert(temp, iters);
169                         
170                         temp = validParameter.validFile(parameters, "maxdim", false);   if (temp == "not found") {      temp = "2";     }
171                         m->mothurConvert(temp, maxdim);
172                         
173                         temp = validParameter.validFile(parameters, "epsilon", false);  if (temp == "not found") {      temp = "0.000000000001";        }
174                         m->mothurConvert(temp, epsilon); 
175                         
176                         if (mindim < 1) { m->mothurOut("mindim must be at least 1."); m->mothurOutEndLine(); abort = true; }
177                         if (maxdim < mindim) { maxdim = mindim; }
178                 }
179                 
180         }
181         catch(exception& e) {
182                 m->errorOut(e, "NMDSCommand", "NMDSCommand");
183                 exit(1);
184         }
185 }
186 //**********************************************************************************************************************
187 int NMDSCommand::execute(){
188         try {
189                 
190                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
191                 
192                 cout.setf(ios::fixed, ios::floatfield);
193                 cout.setf(ios::showpoint);
194                 
195                 vector<string> names;
196                 vector< vector< double> > matrix; 
197                 
198                 //read in phylip file
199                 ReadPhylipVector readFile(phylipfile);
200                 names = readFile.read(matrix);
201                 if (m->control_pressed) { return 0; }
202                 
203                 //read axes
204                 vector< vector<double> > axes;
205                 if (axesfile != "") {  axes = readAxes(names);          }
206                 
207         map<string, string> variables; 
208         variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(phylipfile));
209                 string outputFileName = getOutputFileName("iters",variables);
210                 string stressFileName = getOutputFileName("stress",variables);
211                 outputNames.push_back(outputFileName); outputTypes["iters"].push_back(outputFileName);
212                 outputNames.push_back(stressFileName); outputTypes["stress"].push_back(stressFileName);
213                 
214                 ofstream out, out2;
215                 m->openOutputFile(outputFileName, out);
216                 m->openOutputFile(stressFileName, out2);
217                 
218                 out2.setf(ios::fixed, ios::floatfield);
219                 out2.setf(ios::showpoint);
220                 out.setf(ios::fixed, ios::floatfield);
221                 out.setf(ios::showpoint);
222                 
223                 out2 << "Dimension\tIter\tStress\tRsq" << endl;
224                 
225                 double bestStress = 10000000;
226                 double bestR2 = 10000000;
227                 vector< vector<double> > bestConfig;
228                 int bestDim = 0;
229                 
230                 for (int i = mindim; i <= maxdim; i++) {
231                         m->mothurOut("Processing Dimension: " + toString(i)); m->mothurOutEndLine();
232                         
233                         for (int j = 0; j < iters; j++) {
234                                 m->mothurOut(toString(j+1)); m->mothurOutEndLine(); 
235                                 
236                                 //get configuration - either randomly generate or resize to this dimension
237                                 vector< vector<double> > thisConfig;
238                                 if (axesfile == "") {   thisConfig = generateStartingConfiguration(names.size(), i);            }
239                                 else                            {       thisConfig = getConfiguration(axes, i);                                                         }
240                                 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) {     m->mothurRemove(outputNames[k]);        } return 0; }
241                                 
242                                 //calc nmds for this dimension
243                                 double stress;
244                                 vector< vector<double> > endConfig = nmdsCalc(matrix, thisConfig, stress);
245                                 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) {     m->mothurRemove(outputNames[k]);        } return 0; }
246                                 
247                                 //calc euclid distances for new config
248                                 vector< vector<double> > newEuclid = linearCalc.calculateEuclidianDistance(endConfig);
249                                 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) {     m->mothurRemove(outputNames[k]);        } return 0; }
250                                 
251                                 //calc correlation between original distances and euclidean distances from this config
252                                 double rsquared = linearCalc.calcPearson(newEuclid, matrix);
253                                 rsquared *= rsquared;
254                                 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) {     m->mothurRemove(outputNames[k]);        } return 0; }
255                                 
256                                 //output results
257                                 out << "Config" << (j+1) << '\t';
258                                 for (int k = 0; k < i; k++) { out << "axis" << (k+1) << '\t'; }
259                                 out << endl;
260                                 out2 << i << '\t' << (j+1) << '\t' << stress << '\t' << rsquared << endl;
261                                 
262                                 output(endConfig, names, out);
263                                 
264                                 //save best
265                                 if (stress < bestStress) {
266                                         bestDim = i;
267                                         bestStress = stress;
268                                         bestR2 = rsquared;
269                                         bestConfig = endConfig;
270                                 }
271                                 
272                                 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) {     m->mothurRemove(outputNames[k]);        } return 0; }
273                         }
274                 }
275                 
276                 out.close(); out2.close();
277                 
278                 //output best config
279                 string BestFileName = getOutputFileName("nmds",variables);
280                 outputNames.push_back(BestFileName); outputTypes["nmds"].push_back(BestFileName);
281                 
282                 m->mothurOut("\nNumber of dimensions:\t" + toString(bestDim) + "\n");
283                 m->mothurOut("Lowest stress :\t" + toString(bestStress) + "\n");
284                 m->mothurOut("R-squared for configuration:\t" + toString(bestR2) + "\n");
285                 
286                 ofstream outBest;
287                 m->openOutputFile(BestFileName, outBest);
288                 outBest.setf(ios::fixed, ios::floatfield);
289                 outBest.setf(ios::showpoint);
290                 
291                 outBest << "group" << '\t';
292                 for (int k = 0; k < bestConfig.size(); k++) { outBest << "axis" << (k+1) << '\t'; }
293                 outBest << endl;
294                 
295                 output(bestConfig, names, outBest);
296                 
297                 outBest.close();
298                 
299                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);        } return 0; }
300                 
301                 m->mothurOutEndLine();
302                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
303                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
304                 m->mothurOutEndLine();
305                 
306                 return 0;
307         }
308         catch(exception& e) {
309                 m->errorOut(e, "NMDSCommand", "execute");
310                 exit(1);
311         }
312 }
313 //**********************************************************************************************************************
314 vector< vector<double> > NMDSCommand::nmdsCalc(vector< vector<double> >& matrix, vector< vector<double> >& config, double& stress1) {
315         try {
316                 
317                 vector< vector<double> > newConfig = config;
318                 
319                 //calc euclid distances
320                 vector< vector<double> > euclid = linearCalc.calculateEuclidianDistance(newConfig);
321                 if (m->control_pressed) { return newConfig; }           
322                 
323                 double stress2 = calculateStress(matrix, euclid);
324                 stress1 = stress2 + 1.0 + epsilon;
325                 
326                 int count = 0;
327                 while ((count < maxIters) && (abs(stress1 - stress2) > epsilon)) {
328                         count++;
329                         
330                         stress1 = stress2;
331                         
332                         if (m->control_pressed) { return newConfig; }
333                         
334                         vector< vector<double> > b; b.resize(euclid.size());
335                         for (int i = 0; i < b.size(); i++) { b[i].resize(euclid[i].size(), 0.0); }
336                         
337                         vector<double> columnSums; columnSums.resize(euclid.size(), 0.0);
338                         for (int i = 0; i < euclid.size(); i++) {
339                                 for (int j = 0; j < euclid[i].size(); j++) {
340                                         //eliminate divide by zero error
341                                         if (euclid[i][j] != 0) { 
342                                                 b[i][j] = matrix[i][j] / euclid[i][j];
343                                                 columnSums[j] += b[i][j];
344                                                 b[i][j] *= -1.0;
345                                         }
346                                 }
347                         }
348                         
349                         //put in diagonal sums
350                         for (int i = 0; i < euclid.size(); i++) {  b[i][i] = columnSums[i]; }
351                         
352                         int numInLowerTriangle = matrix.size() * (matrix.size()-1) / 2.0;
353                         double n = (1.0 + sqrt(1.0 + 8.0 * numInLowerTriangle)) / 2.0;
354                         
355                         //matrix mult
356                         newConfig = linearCalc.matrix_mult(newConfig, b);
357                         for (int i = 0; i < newConfig.size(); i++) {
358                                 for (int j = 0; j < newConfig[i].size(); j++) {
359                                         newConfig[i][j] *= (1.0 / n);
360                                 }
361                         }
362                         
363                         euclid = linearCalc.calculateEuclidianDistance(newConfig);
364                         
365                         stress2 = calculateStress(matrix, euclid);
366                 }
367                 
368                 return newConfig;
369         }
370         catch(exception& e) {
371                 m->errorOut(e, "NMDSCommand", "generateStartingConfiguration");
372                 exit(1);
373         }
374 }
375
376 //**********************************************************************************************************************
377 //generate random config
378 vector< vector<double> > NMDSCommand::generateStartingConfiguration(int numNames, int dimension) {
379         try {
380                 vector< vector<double> > axes;  axes.resize(dimension);
381                 for (int i = 0; i < axes.size(); i++) {  axes[i].resize(numNames); }
382                 
383                 //generate random number between -1 and 1, precision 6
384                 for (int i = 0; i < axes.size(); i++) {
385                         for (int j = 0; j < axes[i].size(); j++) {
386                                 
387                                 if (m->control_pressed) { return axes; }
388                                 
389                                 //generate random int between 0 and 99999
390                                 int myrand = (int)((float)(rand()) / ((RAND_MAX / 99998) + 1));
391                                 
392                                 //generate random sign
393                                 int mysign = (int)((float)(rand()) / ((RAND_MAX / 99998) + 1));
394                                 
395                                 //if mysign is even then sign = positive, else sign = negative
396                                 if ((mysign % 2) == 0) { mysign = 1.0; }
397                                 else { mysign = -1.0; }
398                                 
399                                 axes[i][j] = mysign * myrand / (float) 100000;
400                         }
401                 }
402
403                 return axes;
404         }
405         catch(exception& e) {
406                 m->errorOut(e, "NMDSCommand", "generateStartingConfiguration");
407                 exit(1);
408         }
409 }
410 //**********************************************************************************************************************
411 //normalize configuration
412 int NMDSCommand::normalizeConfiguration(vector< vector<double> >& axes, int numNames, int dimension) {
413         try {
414                 vector<double> averageAxes; averageAxes.resize(dimension, 0.0);
415                 
416                 //find average
417                 for (int i = 0; i < axes.size(); i++) {
418                         for (int j = 0; j < axes[i].size(); j++) {      averageAxes[i] += axes[i][j];   }
419                         
420                         averageAxes[i] /= (float) numNames;
421                 }
422                 
423                 //normalize axes
424                 double sumDenom = 0.0;
425                 for (int i = 0; i < axes.size(); i++) {
426                         for (int j = 0; j < axes[i].size(); j++) {
427                                 sumDenom += ((axes[i][j] - averageAxes[i]) * (axes[i][j] - averageAxes[i]));
428                         }
429                 }
430                 
431                 double denom = sqrt((sumDenom / (float) (axes.size() * numNames)));
432                 
433                 for (int i = 0; i < axes.size(); i++) {
434                         for (int j = 0; j < axes[i].size(); j++) {
435                                 axes[i][j] = (axes[i][j] - averageAxes[i]) / denom;
436                         }
437                 }
438                 
439                 return 0;
440         }
441         catch(exception& e) {
442                 m->errorOut(e, "NMDSCommand", "normalizeConfiguration");
443                 exit(1);
444         }
445 }
446 //**********************************************************************************************************************
447 //get configuration
448 vector< vector<double> > NMDSCommand::getConfiguration(vector< vector<double> >& axes, int dimension) {
449         try {
450                 vector< vector<double> > newAxes; newAxes.resize(dimension);
451                 
452                 for (int i = 0; i < dimension; i++) {
453                         newAxes[i] = axes[i];
454                 }
455                                 
456                 return newAxes;
457         }
458         catch(exception& e) {
459                 m->errorOut(e, "NMDSCommand", "getConfiguration");
460                 exit(1);
461         }
462 }
463 //**********************************************************************************************************************
464 //find raw stress, and normalize using
465 double NMDSCommand::calculateStress(vector< vector<double> >& matrix, vector< vector<double> >& config) {
466         try {
467                 double normStress = 0.0;
468                 double denom = 0.0;
469                 double rawStress = 0.0;
470                 
471                 //find raw stress
472                 for (int i = 0; i < matrix.size(); i++) {
473                         for (int j = 0; j < matrix[i].size(); j++) {
474                                 if (m->control_pressed) { return normStress; }
475                                 
476                                 rawStress += ((matrix[i][j] - config[i][j]) * (matrix[i][j] - config[i][j]));
477                                 denom += (config[i][j] * config[i][j]);
478                         }
479                 }
480                 
481                 //normalize stress
482                 if ((rawStress != 0.0) && (denom != 0.0)) {
483                         normStress = sqrt((rawStress / denom));
484                 }
485
486                 return normStress;
487         }
488         catch(exception& e) {
489                 m->errorOut(e, "NMDSCommand", "calculateStress");
490                 exit(1);
491         }
492 }
493
494 //**********************************************************************************************************************
495 int NMDSCommand::output(vector< vector<double> >& config, vector<string>& names, ofstream& out) {
496         try {
497                 
498                 for (int i = 0; i < names.size(); i++) {
499                         
500                         out << names[i] << '\t';
501                         
502                         for (int j = 0; j < config.size(); j++) {
503                                 out << config[j][i] << '\t';
504                         }
505                         
506                         out << endl;
507                 }
508                 
509                 out << endl << endl;
510                         
511                 return 0;
512         }
513         catch(exception& e) {
514                 m->errorOut(e, "NMDSCommand", "output");
515                 exit(1);
516         }
517 }
518 /*****************************************************************/
519 vector< vector<double> > NMDSCommand::readAxes(vector<string> names){
520         try {
521                 ifstream in;
522                 m->openInputFile(axesfile, in);
523                 
524                 string headerLine = m->getline(in); m->gobble(in);
525                 
526                 //count the number of axis you are reading
527                 bool done = false;
528                 int count = 0;
529                 while (!done) {
530                         int pos = headerLine.find("axis");
531                         if (pos != string::npos) {
532                                 count++;
533                                 headerLine = headerLine.substr(pos+4);
534                         }else { done = true; }
535                 }
536                 
537                 if (maxdim > count) { 
538                         m->mothurOut("You requested maxdim = " + toString(maxdim) + ", but your file only includes " + toString(count) + ". Using " + toString(count) + "."); m->mothurOutEndLine(); 
539                         maxdim = count; 
540                         if (maxdim < mindim) { m->mothurOut("Also adjusting mindim to " + toString(maxdim-1) + "."); m->mothurOutEndLine(); }
541                 }
542                 
543                 vector< vector<double> > axes;  axes.resize(maxdim);
544                 for (int i = 0; i < axes.size(); i++) { axes[i].resize(names.size(), 0.0); }
545                 
546                 map <string, vector<double> > orderedAxes;
547                 map     <string, vector<double> >::iterator it;
548                 
549                 while (!in.eof()) {
550                         
551                         if (m->control_pressed) { in.close(); return axes; }
552                         
553                         string group = "";
554                         in >> group; m->gobble(in);
555                         
556                         bool ignore = false;
557                         if (!m->inUsersGroups(group, names)) { ignore = true; m->mothurOut(group + " is in your axes file and not in your distance file, ignoring."); m->mothurOutEndLine(); }
558                         
559                         vector<double> thisGroupsAxes;
560                         for (int i = 0; i < count; i++) {
561                                 float temp = 0.0;
562                                 in >> temp; 
563                                 
564                                 //only save the axis we want
565                                 if (i < maxdim) {  thisGroupsAxes.push_back(temp); }
566                         }
567                         
568                         if (!ignore) {  orderedAxes[group] = thisGroupsAxes; }
569                         
570                         m->gobble(in);
571                 }
572                 in.close();
573                                 
574                 //sanity check
575                 if (names.size() != orderedAxes.size()) { m->mothurOut("[ERROR]: your axes file does not match your distance file, aborting."); m->mothurOutEndLine(); m->control_pressed = true; return axes; }
576                 
577                 //put axes info in same order as distance file, just in case
578                 for (int i = 0; i < names.size(); i++) {
579                         it = orderedAxes.find(names[i]);
580                         
581                         if (it != orderedAxes.end()) {
582                                 vector<double> thisGroupsAxes = it->second;
583                                 
584                                 for (int j = 0; j < thisGroupsAxes.size(); j++) {
585                                         axes[j][i] = thisGroupsAxes[j];
586                                 }
587                                 
588                         }else { m->mothurOut("[ERROR]: your axes file does not match your distance file, aborting."); m->mothurOutEndLine(); m->control_pressed = true; return axes; }
589                 }
590                 
591                 return axes;
592         }
593         catch(exception& e) {
594                 m->errorOut(e, "NMDSCommand", "readAxes");      
595                 exit(1);
596         }
597 }
598 /**********************************************************************************************************************/
599
600
601