+int UnifracWeightedCommand::getAverageSTDMatrices(vector< vector<double> >& dists, int treeNum) {
+ try {
+ //we need to find the average distance and standard deviation for each groups distance
+
+ //finds sum
+ vector<double> averages; averages.resize(numComp, 0);
+ for (int thisIter = 0; thisIter < subsampleIters; thisIter++) {
+ for (int i = 0; i < dists[thisIter].size(); i++) {
+ averages[i] += dists[thisIter][i];
+ }
+ }
+
+ //finds average.
+ for (int i = 0; i < averages.size(); i++) { averages[i] /= (float) subsampleIters; }
+
+ //find standard deviation
+ vector<double> stdDev; stdDev.resize(numComp, 0);
+
+ for (int thisIter = 0; thisIter < iters; thisIter++) { //compute the difference of each dist from the mean, and square the result of each
+ for (int j = 0; j < dists[thisIter].size(); j++) {
+ stdDev[j] += ((dists[thisIter][j] - averages[j]) * (dists[thisIter][j] - averages[j]));
+ }
+ }
+ for (int i = 0; i < stdDev.size(); i++) {
+ stdDev[i] /= (float) subsampleIters;
+ stdDev[i] = sqrt(stdDev[i]);
+ }
+
+ //make matrix with scores in it
+ vector< vector<double> > avedists; avedists.resize(m->getNumGroups());
+ for (int i = 0; i < m->getNumGroups(); i++) {
+ avedists[i].resize(m->getNumGroups(), 0.0);
+ }
+
+ //make matrix with scores in it
+ vector< vector<double> > stddists; stddists.resize(m->getNumGroups());
+ for (int i = 0; i < m->getNumGroups(); i++) {
+ stddists[i].resize(m->getNumGroups(), 0.0);
+ }
+
+ //flip it so you can print it
+ int count = 0;
+ for (int r=0; r<m->getNumGroups(); r++) {
+ for (int l = 0; l < r; l++) {
+ avedists[r][l] = averages[count];
+ avedists[l][r] = averages[count];
+ stddists[r][l] = stdDev[count];
+ stddists[l][r] = stdDev[count];
+ count++;
+ }
+ }
+
+ string aveFileName = outputDir + m->getSimpleName(treefile) + toString(treeNum+1) + ".weighted.ave." + getOutputFileNameTag("phylip");
+ if (outputForm != "column") { outputNames.push_back(aveFileName); outputTypes["phylip"].push_back(aveFileName); }
+ else { outputNames.push_back(aveFileName); outputTypes["column"].push_back(aveFileName); }
+ ofstream out;
+ m->openOutputFile(aveFileName, out);
+
+ string stdFileName = outputDir + m->getSimpleName(treefile) + toString(treeNum+1) + ".weighted.std." + getOutputFileNameTag("phylip");
+ if (outputForm != "column") { outputNames.push_back(stdFileName); outputTypes["phylip"].push_back(stdFileName); }
+ else { outputNames.push_back(stdFileName); outputTypes["column"].push_back(stdFileName); }
+ ofstream outStd;
+ m->openOutputFile(stdFileName, outStd);
+
+ if ((outputForm == "lt") || (outputForm == "square")) {
+ //output numSeqs
+ out << m->getNumGroups() << endl;
+ outStd << m->getNumGroups() << endl;
+ }
+
+ //output to file
+ for (int r=0; r<m->getNumGroups(); r++) {
+ //output name
+ string name = (m->getGroups())[r];
+ if (name.length() < 10) { //pad with spaces to make compatible
+ while (name.length() < 10) { name += " "; }
+ }
+
+ if (outputForm == "lt") {
+ out << name << '\t';
+ outStd << name << '\t';
+
+ //output distances
+ for (int l = 0; l < r; l++) { out << avedists[r][l] << '\t'; outStd << stddists[r][l] << '\t';}
+ out << endl; outStd << endl;
+ }else if (outputForm == "square") {
+ out << name << '\t';
+ outStd << name << '\t';
+
+ //output distances
+ for (int l = 0; l < m->getNumGroups(); l++) { out << avedists[r][l] << '\t'; outStd << stddists[r][l] << '\t'; }
+ out << endl; outStd << endl;
+ }else{
+ //output distances
+ for (int l = 0; l < r; l++) {
+ string otherName = (m->getGroups())[l];
+ if (otherName.length() < 10) { //pad with spaces to make compatible
+ while (otherName.length() < 10) { otherName += " "; }
+ }
+
+ out << name << '\t' << otherName << avedists[r][l] << endl;
+ outStd << name << '\t' << otherName << stddists[r][l] << endl;
+ }
+ }
+ }
+ out.close();
+ outStd.close();
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "UnifracWeightedCommand", "getAverageSTDMatrices");
+ exit(1);
+ }
+}
+
+/**************************************************************************************************/
+int UnifracWeightedCommand::getConsensusTrees(vector< vector<double> >& dists, int treeNum) {
+ try {
+
+ //used in tree constructor
+ m->runParse = false;
+
+ //create treemap class from groupmap for tree class to use
+ TreeMap newTmap;
+ newTmap.makeSim(m->getGroups());
+
+ //clear old tree names if any
+ m->Treenames.clear();
+
+ //fills globaldatas tree names
+ m->Treenames = m->getGroups();
+
+ vector<Tree*> newTrees = buildTrees(dists, treeNum, newTmap); //also creates .all.tre file containing the trees created
+
+ if (m->control_pressed) { return 0; }
+
+ Consensus con;
+ Tree* conTree = con.getTree(newTrees);
+
+ //create a new filename
+ string conFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + toString(treeNum+1) + ".weighted.cons." + getOutputFileNameTag("tree");
+ outputNames.push_back(conFile); outputTypes["tree"].push_back(conFile);
+ ofstream outTree;
+ m->openOutputFile(conFile, outTree);
+
+ if (conTree != NULL) { conTree->print(outTree, "boot"); delete conTree; }
+ outTree.close();
+
+ return 0;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "UnifracWeightedCommand", "getConsensusTrees");
+ exit(1);
+ }
+}
+/**************************************************************************************************/
+
+vector<Tree*> UnifracWeightedCommand::buildTrees(vector< vector<double> >& dists, int treeNum, TreeMap& mytmap) {
+ try {
+
+ vector<Tree*> trees;
+
+ //create a new filename
+ string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + toString(treeNum+1) + ".weighted.all." + getOutputFileNameTag("tree");
+ outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
+
+ ofstream outAll;
+ m->openOutputFile(outputFile, outAll);
+
+
+ for (int i = 0; i < dists.size(); i++) { //dists[0] are the dists for the first subsampled tree.
+
+ if (m->control_pressed) { break; }
+
+ //make matrix with scores in it
+ vector< vector<double> > sims; sims.resize(m->getNumGroups());
+ for (int j = 0; j < m->getNumGroups(); j++) {
+ sims[j].resize(m->getNumGroups(), 0.0);
+ }
+
+ int count = 0;
+ for (int r=0; r<m->getNumGroups(); r++) {
+ for (int l = 0; l < r; l++) {
+ double sim = -(dists[i][count]-1.0);
+ sims[r][l] = sim;
+ sims[l][r] = sim;
+ count++;
+ }
+ }
+
+ //create tree
+ Tree* tempTree = new Tree(&mytmap, sims);
+ map<string, string> empty;
+ tempTree->assembleTree(empty);
+
+ trees.push_back(tempTree);
+
+ //print tree
+ tempTree->print(outAll);
+ }
+
+ outAll.close();
+
+ if (m->control_pressed) { for (int i = 0; i < trees.size(); i++) { delete trees[i]; trees[i] = NULL; } m->mothurRemove(outputFile); }
+
+ return trees;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "UnifracWeightedCommand", "buildTrees");
+ exit(1);
+ }
+}
+/**************************************************************************************************/
+
+int UnifracWeightedCommand::runRandomCalcs(Tree* thisTree, vector<double> usersScores) {
+ try {
+
+ //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
+ vector< vector<string> > namesOfGroupCombos;
+ for (int a=0; a<numGroups; a++) {
+ for (int l = 0; l < a; l++) {
+ vector<string> groups; groups.push_back((m->getGroups())[a]); groups.push_back((m->getGroups())[l]);
+ namesOfGroupCombos.push_back(groups);
+ }
+ }
+
+ lines.clear();
+
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+ if(processors != 1){
+ int numPairs = namesOfGroupCombos.size();
+ int numPairsPerProcessor = numPairs / processors;
+
+ for (int i = 0; i < processors; i++) {
+ int startPos = i * numPairsPerProcessor;
+ if(i == processors - 1){
+ numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
+ }
+ lines.push_back(linePair(startPos, numPairsPerProcessor));
+ }
+ }
+#endif
+
+
+ //get scores for random trees
+ for (int j = 0; j < iters; j++) {
+
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+ if(processors == 1){
+ driver(thisTree, namesOfGroupCombos, 0, namesOfGroupCombos.size(), rScores);
+ }else{
+ createProcesses(thisTree, namesOfGroupCombos, rScores);
+ }
+#else
+ driver(thisTree, namesOfGroupCombos, 0, namesOfGroupCombos.size(), rScores);
+#endif
+
+ if (m->control_pressed) { delete tmap; for (int i = 0; i < T.size(); i++) { delete T[i]; } delete output; outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
+
+ //report progress
+ // m->mothurOut("Iter: " + toString(j+1)); m->mothurOutEndLine();
+ }
+ lines.clear();
+
+ //find the signifigance of the score for summary file
+ for (int f = 0; f < numComp; f++) {
+ //sort random scores
+ sort(rScores[f].begin(), rScores[f].end());
+
+ //the index of the score higher than yours is returned
+ //so if you have 1000 random trees the index returned is 100
+ //then there are 900 trees with a score greater then you.
+ //giving you a signifigance of 0.900
+ int index = findIndex(usersScores[f], f); if (index == -1) { m->mothurOut("error in UnifracWeightedCommand"); m->mothurOutEndLine(); exit(1); } //error code
+
+ //the signifigance is the number of trees with the users score or higher
+ WScoreSig.push_back((iters-index)/(float)iters);
+ }
+
+ //out << "Tree# " << i << endl;
+ calculateFreqsCumuls();
+ printWeightedFile();
+
+ delete output;
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "UnifracWeightedCommand", "runRandomCalcs");
+ exit(1);
+ }
+}
+/**************************************************************************************************/