]> git.donarmstrong.com Git - mothur.git/blob - nmdscommand.cpp
9984247427d5fde267532d2ce2e96a2087afdf0c
[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::getValidParameters(){       
15         try {
16                 string Array[] =  {"phylip","axes","mindim","maxdim","iters","maxiters","trace","epsilon","outputdir","inputdir"};
17                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "NMDSCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 NMDSCommand::NMDSCommand(){     
27         try {
28                 abort = true;
29                 //initialize outputTypes
30                 vector<string> tempOutNames;
31                 outputTypes["nmds"] = tempOutNames;
32                 outputTypes["stress"] = tempOutNames;
33         }
34         catch(exception& e) {
35                 m->errorOut(e, "NMDSCommand", "NMDSCommand");
36                 exit(1);
37         }
38 }
39 //**********************************************************************************************************************
40 vector<string> NMDSCommand::getRequiredParameters(){    
41         try {
42                 string Array[] =  {"phylip"};
43                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
44                 return myArray;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "NMDSCommand", "getRequiredParameters");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 vector<string> NMDSCommand::getRequiredFiles(){ 
53         try {
54                 vector<string> myArray;
55                 return myArray;
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "NMDSCommand", "getRequiredFiles");
59                 exit(1);
60         }
61 }
62 //**********************************************************************************************************************
63
64 NMDSCommand::NMDSCommand(string option)  {
65         try {
66                 abort = false;
67                 
68                 //allow user to run help
69                 if(option == "help") { help(); abort = true; }
70                 
71                 else {
72                         //valid paramters for this command
73                         string Array[] =  {"phylip","axes","mindim","maxdim","iters","maxiters","trace","epsilon","outputdir", "inputdir"};
74                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75                         
76                         OptionParser parser(option);
77                         map<string, string> parameters = parser. getParameters();
78                         
79                         ValidParameters validParameter;
80                         map<string, string>::iterator it;
81                         
82                         //check to make sure all parameters are valid for command
83                         for (it = parameters.begin(); it != parameters.end(); it++) { 
84                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
85                         }
86                         //if the user changes the input directory command factory will send this info to us in the output parameter 
87                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
88                         if (inputDir == "not found"){   inputDir = "";          }
89                         else {
90                                 string path;
91                                 it = parameters.find("phylip");
92                                 //user has given a template file
93                                 if(it != parameters.end()){ 
94                                         path = m->hasPath(it->second);
95                                         //if the user has not given a path then, add inputdir. else leave path alone.
96                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
97                                 }
98                                 
99                                 it = parameters.find("axes");
100                                 //user has given a template file
101                                 if(it != parameters.end()){ 
102                                         path = m->hasPath(it->second);
103                                         //if the user has not given a path then, add inputdir. else leave path alone.
104                                         if (path == "") {       parameters["axes"] = inputDir + it->second;             }
105                                 }
106                         }
107                         
108                         //initialize outputTypes
109                         vector<string> tempOutNames;
110                         outputTypes["nmds"] = tempOutNames;
111                         outputTypes["stress"] = tempOutNames;
112                         
113                         //required parameters
114                         phylipfile = validParameter.validFile(parameters, "phylip", true);
115                         if (phylipfile == "not open") { phylipfile = ""; abort = true; }
116                         else if (phylipfile == "not found") { phylipfile = ""; m->mothurOut("You must provide a distance file before running the nmds command."); m->mothurOutEndLine(); abort = true; }        
117                         
118                         axesfile = validParameter.validFile(parameters, "axes", true);
119                         if (axesfile == "not open") { axesfile = ""; abort = true; }
120                         else if (axesfile == "not found") { axesfile = "";  }                           
121                         
122                         //if the user changes the output directory command factory will send this info to us in the output parameter 
123                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
124                                 outputDir = ""; 
125                                 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it      
126                         }
127                         
128                         string temp = validParameter.validFile(parameters, "mindim", false);    if (temp == "not found") {      temp = "1";     }
129                         convert(temp, mindim);
130                         
131                         temp = validParameter.validFile(parameters, "maxiters", false); if (temp == "not found") {      temp = "500";   }
132                         convert(temp, maxIters);
133                         
134                         temp = validParameter.validFile(parameters, "iters", false);    if (temp == "not found") {      temp = "10";    }
135                         convert(temp, iters);
136                         
137                         temp = validParameter.validFile(parameters, "maxdim", false);   if (temp == "not found") {      temp = "2";     }
138                         convert(temp, maxdim);
139                         
140                         temp = validParameter.validFile(parameters, "epsilon", false);  if (temp == "not found") {      temp = "0.000000000001";        }
141                         convert(temp, epsilon); 
142                         
143                         temp = validParameter.validFile(parameters, "trace", false);    if (temp == "not found") {      temp = "F";     }
144                         trace = m->isTrue(temp);
145                         
146                         if (mindim < 1) { m->mothurOut("mindim must be at least 1."); m->mothurOutEndLine(); abort = true; }
147                         if (maxdim < mindim) { m->mothurOut("maxdim must be greater than mindim."); m->mothurOutEndLine(); abort = true; }
148                 }
149                 
150         }
151         catch(exception& e) {
152                 m->errorOut(e, "NMDSCommand", "NMDSCommand");
153                 exit(1);
154         }
155 }
156 //**********************************************************************************************************************
157 void NMDSCommand::help(){
158         try {
159                 m->mothurOut("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."); m->mothurOutEndLine();
160                 m->mothurOut("The nmds command parameters are phylip, axes, mindim, maxdim, maxiters, iters, epsilon and trace."); m->mothurOutEndLine();
161                 m->mothurOut("The phylip parameter allows you to enter your distance file."); m->mothurOutEndLine();
162                 m->mothurOut("The axes parameter allows you to enter a file containing a starting configuration."); m->mothurOutEndLine();
163                 m->mothurOut("The maxdim parameter allows you to select how maximum dimensions to use. Default=2"); m->mothurOutEndLine();
164                 m->mothurOut("The mindim parameter allows you to select how minimum dimensions to use. Default=1"); m->mothurOutEndLine();
165                 m->mothurOut("The maxiters parameter allows you to select the maximum number of iters to try with each random configuration. Default=500"); m->mothurOutEndLine();
166                 m->mothurOut("The iters parameter allows you to select the number of random configuration to try. Default=10"); m->mothurOutEndLine();
167                 m->mothurOut("The epsilon parameter allows you to select set an acceptable stopping point. Default=1e-12."); m->mothurOutEndLine();
168                 m->mothurOut("The trace parameter allows you to see the output after each iter. Default=F"); m->mothurOutEndLine();
169                 m->mothurOut("Example nmds(phylip=yourDistanceFile).\n");
170                 m->mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourDistanceFile).\n\n");
171         }
172         catch(exception& e) {
173                 m->errorOut(e, "NMDSCommand", "help");
174                 exit(1);
175         }
176 }
177 //**********************************************************************************************************************
178 NMDSCommand::~NMDSCommand(){}
179 //**********************************************************************************************************************
180 int NMDSCommand::execute(){
181         try {
182                 
183                 if (abort == true) { return 0; }
184                 
185                 cout.setf(ios::fixed, ios::floatfield);
186                 cout.setf(ios::showpoint);
187                 
188                 vector<string> names;
189                 vector< vector< double> > matrix; 
190                 
191                 //read in phylip file
192                 ReadPhylipVector readFile(phylipfile);
193                 names = readFile.read(matrix);
194                 if (m->control_pressed) { return 0; }
195                 
196                 //read axes
197                 vector< vector<double> > axes;
198                 if (axesfile != "") {  axes = readAxes(names);          }
199                 
200                 for (int i = mindim; i <= maxdim; i++) {
201                         m->mothurOut("Processing Dimension: " + toString(i)); m->mothurOutEndLine();
202                         
203                         string outputFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "dim" + toString(i) + ".nmds";
204                         string stressFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "dim" + toString(i) + ".stress.nmds";
205                         outputNames.push_back(outputFileName); outputTypes["nmds"].push_back(outputFileName);
206                         outputNames.push_back(stressFileName); outputTypes["stress"].push_back(stressFileName);
207                         
208                         ofstream out, out2;
209                         m->openOutputFile(outputFileName, out);
210                         m->openOutputFile(stressFileName, out2);
211                         
212                         out2.setf(ios::fixed, ios::floatfield);
213                         out2.setf(ios::showpoint);
214                         out.setf(ios::fixed, ios::floatfield);
215                         out.setf(ios::showpoint);
216                         
217                         out2 << "Iter\tStress\tCorr" << endl;
218                         
219                         for (int j = 0; j < iters; j++) {
220                                 if (trace) { m->mothurOut(toString(j+1)); m->mothurOutEndLine(); }
221                                 
222                                 //get configuration - either randomly generate or resize to this dimension
223                                 vector< vector<double> > thisConfig;
224                                 if (axesfile == "") {   thisConfig = generateStartingConfiguration(names.size(), i);            }
225                                 else                            {       thisConfig = getConfiguration(axes, i);                                                         }
226                                 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) {     remove(outputNames[k].c_str()); } return 0; }
227                                 
228                                 //calc nmds for this dimension
229                                 double stress;
230                                 vector< vector<double> > endConfig = nmdsCalc(matrix, thisConfig, stress);
231                                 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) {     remove(outputNames[k].c_str()); } return 0; }
232                                 
233                                 //calc euclid distances for new config
234                                 vector< vector<double> > newEuclid = linearCalc.calculateEuclidianDistance(endConfig);
235                                 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) {     remove(outputNames[k].c_str()); } return 0; }
236                                 
237                                 //calc correlation between original distances and euclidean distances from this config
238                                 double corr = linearCalc.calcPearson(matrix, newEuclid);
239                                 corr *= corr;
240                                 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) {     remove(outputNames[k].c_str()); } return 0; }
241                                 
242                                 //output results
243                                 out << "Config" << (j+1) << '\t';
244                                 for (int k = 0; k < i; k++) { out << "X" << (k+1) << '\t'; }
245                                 out << endl;
246                                 out2 << (j+1) << '\t' << stress << '\t' << corr << endl;
247                                 
248                                 output(endConfig, names, out);
249                                 
250                                 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) {     remove(outputNames[k].c_str()); } return 0; }
251
252                         }
253                         
254                         out.close(); out2.close();
255                 }
256                 
257                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
258                 
259                 m->mothurOutEndLine();
260                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
261                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
262                 m->mothurOutEndLine();
263                 
264                 return 0;
265         }
266         catch(exception& e) {
267                 m->errorOut(e, "NMDSCommand", "execute");
268                 exit(1);
269         }
270 }
271 //**********************************************************************************************************************
272 vector< vector<double> > NMDSCommand::nmdsCalc(vector< vector<double> >& matrix, vector< vector<double> >& config, double& stress1) {
273         try {
274                 
275                 vector< vector<double> > newConfig = config;
276                 
277                 //calc euclid distances
278                 vector< vector<double> > euclid = linearCalc.calculateEuclidianDistance(newConfig);
279                 if (m->control_pressed) { return newConfig; }           
280                 
281                 double stress2 = calculateStress(matrix, euclid);
282                 stress1 = stress2 + 1.0 + epsilon;
283                 
284                 if (trace) { m->mothurOutEndLine(); m->mothurOut("Iter\tStress"); m->mothurOutEndLine(); }
285                 
286                 int count = 0;
287                 while ((count < maxIters) && (abs(stress1 - stress2) > epsilon)) {
288                         count++;
289                         
290                         stress1 = stress2;
291                         
292                         if (m->control_pressed) { return newConfig; }
293                         
294                         vector< vector<double> > b; b.resize(euclid.size());
295                         for (int i = 0; i < b.size(); i++) { b[i].resize(euclid[i].size(), 0.0); }
296                         
297                         vector<double> columnSums; columnSums.resize(euclid.size(), 0.0);
298                         for (int i = 0; i < euclid.size(); i++) {
299                                 for (int j = 0; j < euclid[i].size(); j++) {
300                                         //eliminate divide by zero error
301                                         if (euclid[i][j] != 0) { 
302                                                 b[i][j] = matrix[i][j] / euclid[i][j];
303                                                 columnSums[j] += b[i][j];
304                                                 b[i][j] *= -1.0;
305                                         }
306                                 }
307                         }
308                         
309                         //put in diagonal sums
310                         for (int i = 0; i < euclid.size(); i++) {  b[i][i] = columnSums[i]; }
311                         
312                         int numInLowerTriangle = matrix.size() * (matrix.size()-1) / 2.0;
313                         double n = (1.0 + sqrt(1.0 + 8.0 * numInLowerTriangle)) / 2.0;
314                         
315                         //matrix mult
316                         newConfig = linearCalc.matrix_mult(newConfig, b);
317                         for (int i = 0; i < newConfig.size(); i++) {
318                                 for (int j = 0; j < newConfig[i].size(); j++) {
319                                         newConfig[i][j] *= (1.0 / n);
320                                 }
321                         }
322                         
323                         euclid = linearCalc.calculateEuclidianDistance(newConfig);
324                         
325                         stress2 = calculateStress(matrix, euclid);
326                         
327                         if (trace) { m->mothurOut(count + "\t" + toString(stress1)); m->mothurOutEndLine(); }
328                         
329                 }
330                 
331                 return newConfig;
332         }
333         catch(exception& e) {
334                 m->errorOut(e, "NMDSCommand", "generateStartingConfiguration");
335                 exit(1);
336         }
337 }
338
339 //**********************************************************************************************************************
340 //generate random config
341 vector< vector<double> > NMDSCommand::generateStartingConfiguration(int numNames, int dimension) {
342         try {
343                 vector< vector<double> > axes;  axes.resize(dimension);
344                 for (int i = 0; i < axes.size(); i++) {  axes[i].resize(numNames); }
345                 
346                 //generate random number between -1 and 1, precision 6
347                 for (int i = 0; i < axes.size(); i++) {
348                         for (int j = 0; j < axes[i].size(); j++) {
349                                 
350                                 if (m->control_pressed) { return axes; }
351                                 
352                                 //generate random int between 0 and 99999
353                                 int myrand = (int)((float)(rand()) / ((RAND_MAX / 99998) + 1));
354                                 
355                                 //generate random sign
356                                 int mysign = (int)((float)(rand()) / ((RAND_MAX / 99998) + 1));
357                                 
358                                 //if mysign is even then sign = positive, else sign = negative
359                                 if ((mysign % 2) == 0) { mysign = 1.0; }
360                                 else { mysign = -1.0; }
361                                 
362                                 axes[i][j] = mysign * myrand / (float) 100000;
363                         }
364                 }
365
366                 return axes;
367         }
368         catch(exception& e) {
369                 m->errorOut(e, "NMDSCommand", "generateStartingConfiguration");
370                 exit(1);
371         }
372 }
373 //**********************************************************************************************************************
374 //normalize configuration
375 int NMDSCommand::normalizeConfiguration(vector< vector<double> >& axes, int numNames, int dimension) {
376         try {
377                 vector<double> averageAxes; averageAxes.resize(dimension, 0.0);
378                 
379                 //find average
380                 for (int i = 0; i < axes.size(); i++) {
381                         for (int j = 0; j < axes[i].size(); j++) {      averageAxes[i] += axes[i][j];   }
382                         
383                         averageAxes[i] /= (float) numNames;
384                 }
385                 
386                 //normalize axes
387                 double sumDenom = 0.0;
388                 for (int i = 0; i < axes.size(); i++) {
389                         for (int j = 0; j < axes[i].size(); j++) {
390                                 sumDenom += ((axes[i][j] - averageAxes[i]) * (axes[i][j] - averageAxes[i]));
391                         }
392                 }
393                 
394                 double denom = sqrt((sumDenom / (float) (axes.size() * numNames)));
395                 
396                 for (int i = 0; i < axes.size(); i++) {
397                         for (int j = 0; j < axes[i].size(); j++) {
398                                 axes[i][j] = (axes[i][j] - averageAxes[i]) / denom;
399                         }
400                 }
401                 
402                 return 0;
403         }
404         catch(exception& e) {
405                 m->errorOut(e, "NMDSCommand", "normalizeConfiguration");
406                 exit(1);
407         }
408 }
409 //**********************************************************************************************************************
410 //get configuration
411 vector< vector<double> > NMDSCommand::getConfiguration(vector< vector<double> >& axes, int dimension) {
412         try {
413                 vector< vector<double> > newAxes; newAxes.resize(dimension);
414                 
415                 for (int i = 0; i < dimension; i++) {
416                         newAxes[i] = axes[i];
417                 }
418                                 
419                 return newAxes;
420         }
421         catch(exception& e) {
422                 m->errorOut(e, "NMDSCommand", "getConfiguration");
423                 exit(1);
424         }
425 }
426 //**********************************************************************************************************************
427 //find raw stress, and normalize using
428 double NMDSCommand::calculateStress(vector< vector<double> >& matrix, vector< vector<double> >& config) {
429         try {
430                 double normStress = 0.0;
431                 double denom = 0.0;
432                 double rawStress = 0.0;
433                 
434                 //find raw stress
435                 for (int i = 0; i < matrix.size(); i++) {
436                         for (int j = 0; j < matrix[i].size(); j++) {
437                                 if (m->control_pressed) { return normStress; }
438                                 
439                                 rawStress += ((matrix[i][j] - config[i][j]) * (matrix[i][j] - config[i][j]));
440                                 denom += (config[i][j] * config[i][j]);
441                         }
442                 }
443                 
444                 //normalize stress
445                 if ((rawStress != 0.0) && (denom != 0.0)) {
446                         normStress = sqrt((rawStress / denom));
447                 }
448
449                 return normStress;
450         }
451         catch(exception& e) {
452                 m->errorOut(e, "NMDSCommand", "calculateStress");
453                 exit(1);
454         }
455 }
456
457 //**********************************************************************************************************************
458 int NMDSCommand::output(vector< vector<double> >& config, vector<string>& names, ofstream& out) {
459         try {
460                 
461                 for (int i = 0; i < names.size(); i++) {
462                         
463                         out << names[i] << '\t';
464                         
465                         for (int j = 0; j < config.size(); j++) {
466                                 out << config[j][i] << '\t';
467                         }
468                         
469                         out << endl;
470                 }
471                 
472                 out << endl << endl;
473                         
474                 return 0;
475         }
476         catch(exception& e) {
477                 m->errorOut(e, "NMDSCommand", "output");
478                 exit(1);
479         }
480 }
481 /*****************************************************************/
482 vector< vector<double> > NMDSCommand::readAxes(vector<string> names){
483         try {
484                 ifstream in;
485                 m->openInputFile(axesfile, in);
486                 
487                 string headerLine = m->getline(in); m->gobble(in);
488                 
489                 //count the number of axis you are reading
490                 bool done = false;
491                 int count = 0;
492                 while (!done) {
493                         int pos = headerLine.find("axis");
494                         if (pos != string::npos) {
495                                 count++;
496                                 headerLine = headerLine.substr(pos+4);
497                         }else { done = true; }
498                 }
499                 
500                 if (maxdim > count) { 
501                         m->mothurOut("You requested maxdim = " + toString(maxdim) + ", but your file only includes " + toString(count) + ". Using " + toString(count) + "."); m->mothurOutEndLine(); 
502                         maxdim = count; 
503                         if (maxdim < mindim) { m->mothurOut("Also adjusting mindim to " + toString(maxdim-1) + "."); m->mothurOutEndLine(); }
504                 }
505                 
506                 vector< vector<double> > axes;  axes.resize(maxdim);
507                 for (int i = 0; i < axes.size(); i++) { axes[i].resize(names.size(), 0.0); }
508                 
509                 map <string, vector<double> > orderedAxes;
510                 map     <string, vector<double> >::iterator it;
511                 
512                 while (!in.eof()) {
513                         
514                         if (m->control_pressed) { in.close(); return axes; }
515                         
516                         string group = "";
517                         in >> group; m->gobble(in);
518                         
519                         bool ignore = false;
520                         if (!m->inUsersGroups(group, names)) { ignore = true; m->mothurOut(group + " is in your axes file and not in your distance file, ignoring."); m->mothurOutEndLine(); }
521                         
522                         vector<double> thisGroupsAxes;
523                         for (int i = 0; i < count; i++) {
524                                 float temp = 0.0;
525                                 in >> temp; 
526                                 
527                                 //only save the axis we want
528                                 if (i < maxdim) {  thisGroupsAxes.push_back(temp); }
529                         }
530                         
531                         if (!ignore) {  orderedAxes[group] = thisGroupsAxes; }
532                         
533                         m->gobble(in);
534                 }
535                 in.close();
536                                 
537                 //sanity check
538                 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; }
539                 
540                 //put axes info in same order as distance file, just in case
541                 for (int i = 0; i < names.size(); i++) {
542                         it = orderedAxes.find(names[i]);
543                         
544                         if (it != orderedAxes.end()) {
545                                 vector<double> thisGroupsAxes = it->second;
546                                 
547                                 for (int j = 0; j < thisGroupsAxes.size(); j++) {
548                                         axes[j][i] = thisGroupsAxes[j];
549                                 }
550                                 
551                         }else { m->mothurOut("[ERROR]: your axes file does not match your distance file, aborting."); m->mothurOutEndLine(); m->control_pressed = true; return axes; }
552                 }
553                 
554                 return axes;
555         }
556         catch(exception& e) {
557                 m->errorOut(e, "NMDSCommand", "readAxes");      
558                 exit(1);
559         }
560 }
561 /**********************************************************************************************************************
562  vector< vector<double> > NMDSCommand::calculateStressGradientVector(vector<seqDist>& eDists, vector<seqDist>& D, double rawStress, double stress, vector< vector<double> >& axes) {
563  try {
564  vector< vector<double> > gradient; gradient.resize(dimension);
565  for (int i = 0; i < gradient.size(); i++) { gradient[i].resize(axes[0].size(), 0.0); }
566  
567  double sumDij = 0.0;
568  for (int i = 0; i < eDists.size(); i++) {  sumDij += (eDists[i].dist * eDists[i].dist); }
569  
570  for (int i = 0; i < eDists.size(); i++) {
571  
572  for (int j = 0; j < dimension; j++) {
573  
574  if (m->control_pressed) { return gradient; }
575  
576  double firstTerm1 = (stress / rawStress) * (eDists[i].dist - D[i].dist);
577  double firstTerm2 = (stress / sumDij) * eDists[i].dist;
578  double firstTerm = firstTerm1 - firstTerm2;
579  
580  float r = (dimension-1.0);
581  double temp = 1.0 / (pow(eDists[i].dist, r));
582  float absTemp = abs(axes[j][eDists[i].seq1] - axes[j][eDists[i].seq2]);
583  double secondTerm = pow(absTemp, r) * temp; 
584  
585  double sigNum = 1.0;
586  if ((axes[j][eDists[i].seq1] - axes[j][eDists[i].seq2]) == 0) { sigNum = 0.0; }
587  else if ((axes[j][eDists[i].seq1] - axes[j][eDists[i].seq2]) < 0) { sigNum = -1.0; }
588  
589  double results = (firstTerm * secondTerm * sigNum);
590  cout << i << '\t' << j << '\t' << "results = " << results << endl;     
591  gradient[j][eDists[i].seq1] += results;
592  gradient[j][eDists[i].seq2] -= results;
593  }
594  }
595  
596  return gradient;
597  }
598  catch(exception& e) {
599  m->errorOut(e, "NMDSCommand", "calculateStressGradientVector");
600  exit(1);
601  }
602  }
603  //**********************************************************************************************************************
604  double NMDSCommand::calculateMagnitude(vector< vector<double> >& gradient) {
605  try {
606  double magnitude = 0.0;
607  
608  double sum = 0.0;
609  for (int i = 0; i < gradient.size(); i++) {
610  for (int j = 0; j < gradient[i].size(); j++) {
611  sum += (gradient[i][j] * gradient[i][j]);
612  }
613  }
614  
615  magnitude = sqrt(((1.0/(float)gradient[0].size()) * sum));
616  
617  return magnitude;
618  }
619  catch(exception& e) {
620  m->errorOut(e, "NMDSCommand", "calculateMagnitude");
621  exit(1);
622  }
623  }
624  //**********************************************************************************************************************
625  //described in Kruskal paper page 121 + 122
626  double NMDSCommand::calculateStep(vector< vector<double> >& prevGrad, vector< vector<double> >& grad, vector<double>& prevStress) {
627  try {
628  double newStep = step;
629  
630  //calc the cos theta
631  double sumNum = 0.0;
632  double sumDenom1 = 0.0;
633  double sumDenom2 = 0.0;
634  for (int i = 0; i < prevGrad.size(); i++) {
635  for (int j = 0; j < prevGrad[i].size(); j++) {
636  sumDenom1 += (grad[i][j] * grad[i][j]);
637  sumDenom2 += (prevGrad[i][j] * prevGrad[i][j]);
638  sumNum += (grad[i][j] * prevGrad[i][j]);
639  }
640  }
641  
642  double cosTheta = sumNum / (sqrt(sumDenom1) * sqrt(sumDenom2));
643  cosTheta *= cosTheta;
644  
645  //calc angle factor
646  double angle = pow(4.0, cosTheta);
647  
648  //calc 5 step ratio
649  double currentStress = prevStress[prevStress.size()-1];
650  double lastStress = prevStress[0];
651  if (prevStress.size() > 1) {  lastStress = prevStress[prevStress.size()-2];            }
652  double fivePrevStress = prevStress[0];
653  if (prevStress.size() > 5) {  fivePrevStress = prevStress[prevStress.size()-6]; }
654  
655  double fiveStepRatio = min(1.0, (currentStress / fivePrevStress));
656  
657  //calc relaxation factor
658  double relaxation = 1.3 / (1.0 + pow(fiveStepRatio, 5.0));
659  
660  //calc good luck factor
661  double goodLuck = min(1.0, (currentStress / lastStress));
662  
663  //calc newStep
664  //cout << "\ncos = " << cosTheta << " step = " << step << " angle = " << angle << " relaxation = " << relaxation << " goodluck = " << goodLuck << endl;
665  newStep = step * angle * relaxation * goodLuck;
666  
667  return newStep;
668  }
669  catch(exception& e) {
670  m->errorOut(e, "NMDSCommand", "calculateStep");
671  exit(1);
672  }
673  }
674  //**********************************************************************************************************************
675  vector< vector<double> > NMDSCommand::calculateNewConfiguration(double magnitude, vector< vector<double> >& axes, vector< vector<double> >& gradient) {
676  try {
677  
678  vector< vector<double> > newAxes = axes;
679  
680  for (int i = 0; i < newAxes.size(); i++) {
681  
682  if (m->control_pressed) { return newAxes; }
683  
684  for (int j = 0; j < newAxes[i].size(); j++) {
685  newAxes[i][j] = axes[i][j] + ((step / magnitude) * gradient[i][j]);
686  }
687  }
688  
689  return newAxes;
690  }
691  catch(exception& e) {
692  m->errorOut(e, "NMDSCommand", "calculateNewConfiguration");
693  exit(1);
694  }
695  }*/
696 /**********************************************************************************************************************
697  //adjust eDists so that it creates monotonically increasing series of succesive values that increase or stay the same, but never decrease
698  vector<seqDist> NMDSCommand::satisfyMonotonicity(vector<seqDist> eDists, vector<int> partitions) {
699  try {
700  
701  //find averages of each partitions
702  vector<double> sums; sums.resize(partitions.size(), 0.0);
703  vector<int> sizes; sizes.resize(partitions.size(), 0);
704  
705  for (int i = 0; i < partitions.size(); i++) {
706  //i is not the last one
707  int start = partitions[i];
708  int end;
709  if (i != (partitions.size()-1)) {  end = partitions[i+1];   }
710  else{  end = eDists.size();    }
711  
712  for (int j = start; j < end; j++) {   sums[i] += eDists[j].dist;  }
713  
714  sizes[i] = (end - start);
715  }
716  
717  
718  vector<seqDist> D = eDists;
719  
720  //i represents the "active block"
721  int i = 0;
722  while (i < partitions.size()) {
723  
724  if (m->control_pressed) { return D; }
725  
726  bool upActive = true;
727  bool upSatisfied = false;
728  bool downSatisfied = false;
729  
730  //while we are not done with this block
731  while ((!upSatisfied) || (!downSatisfied)) {
732  
733  if (upActive) {
734  
735  //are we are upSatisfied? - is the average of the next block greater than mine?
736  if (i != (partitions.size()-1))  { //if we are the last guy then we are upsatisfied
737  if ((sums[i+1]/(float)sizes[i+1]) >= (sums[i]/(float)sizes[i])) {
738  upSatisfied = true;
739  upActive = false;
740  }else {
741  //find new weighted average
742  double newSum = sums[i] + sums[i+1];
743  
744  //merge blocks - putting everything in i
745  sums[i] = newSum;
746  sizes[i] += sizes[i+1];
747  partitions[i] = partitions[i+1];
748  
749  sums.erase(sums.begin()+(i+1));
750  sizes.erase(sizes.begin()+(i+1));
751  partitions.erase(partitions.begin()+(i+1));
752  
753  upActive = false;
754  }
755  }else { upSatisfied = true; upActive = false; }
756  
757  }else { //downActive
758  
759  //are we are DownSatisfied? - is the average of the previous block less than mine?
760  if (i != 0)  { //if we are the first guy then we are downSatisfied
761  if ((sums[i-1]/(float)sizes[i-1]) <= (sums[i]/(float)sizes[i])) {
762  downSatisfied = true;
763  upActive = true;
764  }else {
765  //find new weighted average
766  double newSum = sums[i] + sums[i-1];;
767  
768  //merge blocks - putting everything in i-1
769  sums[i-1] = newSum;
770  sizes[i-1] += sizes[i];
771  
772  sums.erase(sums.begin()+i);
773  sizes.erase(sizes.begin()+i);
774  partitions.erase(partitions.begin()+i);
775  i--;
776  
777  upActive = true;
778  }
779  }else { downSatisfied = true; upActive = true; }                                       
780  }
781  }
782  
783  i++; // go to next block
784  }
785  
786  //sanity check - for rounding errors
787  vector<double> averages; averages.resize(sums.size(), 0.0);
788  for (int i = 0; i < sums.size(); i++) { averages[i] = sums[i] / (float) sizes[i];      }
789  for (int i = 0; i < averages.size(); i++) { if (averages[i+1] < averages[i]) {  averages[i+1] = averages[i]; } }
790  
791  //fill D
792  int placeHolder = 0;
793  for (int i = 0; i < averages.size(); i++) {
794  for (int j = 0; j < sizes[i]; j++) {
795  D[placeHolder].dist = averages[i];
796  placeHolder++;
797  }
798  }
799  
800  return D;
801  }
802  catch(exception& e) {
803  m->errorOut(e, "NMDSCommand", "satisfyMonotonicity");
804  exit(1);
805  }
806  }*/
807
808 //**********************************************************************************************************************
809
810