X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=nmdscommand.cpp;h=4adb57c6147c544ab9284996d85a8bf4db9e55e2;hb=e6c4a7223199d800d496356604eb34dfe273673d;hp=cfd70c2b246beb62da1d651c0b23f2a6005f2ae6;hpb=41f654a317afba7f4dba3fcd0a6557773528845a;p=mothur.git diff --git a/nmdscommand.cpp b/nmdscommand.cpp index cfd70c2..4adb57c 100644 --- a/nmdscommand.cpp +++ b/nmdscommand.cpp @@ -30,6 +30,7 @@ NMDSCommand::NMDSCommand(){ vector tempOutNames; outputTypes["nmds"] = tempOutNames; outputTypes["stress"] = tempOutNames; + outputTypes["iters"] = tempOutNames; } catch(exception& e) { m->errorOut(e, "NMDSCommand", "NMDSCommand"); @@ -108,6 +109,7 @@ NMDSCommand::NMDSCommand(string option) { //initialize outputTypes vector tempOutNames; outputTypes["nmds"] = tempOutNames; + outputTypes["iters"] = tempOutNames; outputTypes["stress"] = tempOutNames; //required parameters @@ -193,25 +195,28 @@ int NMDSCommand::execute(){ vector< vector > axes; if (axesfile != "") { axes = readAxes(names); } + string outputFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "nmds.iters"; + string stressFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "stress.nmds"; + outputNames.push_back(outputFileName); outputTypes["iters"].push_back(outputFileName); + outputNames.push_back(stressFileName); outputTypes["stress"].push_back(stressFileName); + + ofstream out, out2; + m->openOutputFile(outputFileName, out); + m->openOutputFile(stressFileName, out2); + + out2.setf(ios::fixed, ios::floatfield); + out2.setf(ios::showpoint); + out.setf(ios::fixed, ios::floatfield); + out.setf(ios::showpoint); + + out2 << "Dimension\tIter\tStress\tCorr" << endl; + + double bestStress = 10000000; + vector< vector > bestConfig; + for (int i = mindim; i <= maxdim; i++) { m->mothurOut("Processing Dimension: " + toString(i)); m->mothurOutEndLine(); - string outputFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "dim" + toString(i) + ".nmds"; - string stressFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "dim" + toString(i) + ".stress.nmds"; - outputNames.push_back(outputFileName); outputTypes["nmds"].push_back(outputFileName); - outputNames.push_back(stressFileName); outputTypes["stress"].push_back(stressFileName); - - ofstream out, out2; - m->openOutputFile(outputFileName, out); - m->openOutputFile(stressFileName, out2); - - out2.setf(ios::fixed, ios::floatfield); - out2.setf(ios::showpoint); - out.setf(ios::fixed, ios::floatfield); - out.setf(ios::showpoint); - - out2 << "Iter\tStress\tCorr" << endl; - for (int j = 0; j < iters; j++) { m->mothurOut(toString(j+1)); m->mothurOutEndLine(); @@ -231,25 +236,47 @@ int NMDSCommand::execute(){ if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; } //calc correlation between original distances and euclidean distances from this config - double corr = linearCalc.calcPearson(matrix, newEuclid); + double corr = linearCalc.calcPearson(newEuclid, matrix); corr *= corr; if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; } //output results out << "Config" << (j+1) << '\t'; - for (int k = 0; k < i; k++) { out << "X" << (k+1) << '\t'; } + for (int k = 0; k < i; k++) { out << "axis" << (k+1) << '\t'; } out << endl; - out2 << (j+1) << '\t' << stress << '\t' << corr << endl; + out2 << i << '\t' << (j+1) << '\t' << stress << '\t' << corr << endl; output(endConfig, names, out); + //save best + if (stress < bestStress) { + bestStress = stress; + bestConfig = endConfig; + } + if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; } - } - - out.close(); out2.close(); } + out.close(); out2.close(); + + //output best config + string BestFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "nmds.axes"; + outputNames.push_back(BestFileName); outputTypes["nmds"].push_back(BestFileName); + + ofstream outBest; + m->openOutputFile(BestFileName, outBest); + outBest.setf(ios::fixed, ios::floatfield); + outBest.setf(ios::showpoint); + + outBest << '\t'; + for (int k = 0; k < bestConfig.size(); k++) { outBest << "axis" << (k+1) << '\t'; } + outBest << endl; + + output(bestConfig, names, outBest); + + outBest.close(); + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; } m->mothurOutEndLine();