+}
+/**************************************************************************************************/
+int UnifracUnweightedCommand::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 < subsampleIters; 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) + ".unweighted.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) + ".unweighted.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, "UnifracUnweightedCommand", "getAverageSTDMatrices");
+ exit(1);
+ }
+}
+
+/**************************************************************************************************/
+int UnifracUnweightedCommand::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
+ CountTable newCt;
+ set<string> nameMap;
+ map<string, string> groupMap;
+ set<string> gps;
+ for (int i = 0; i < m->getGroups().size(); i++) {
+ nameMap.insert(m->getGroups()[i]);
+ gps.insert(m->getGroups()[i]);
+ groupMap[m->getGroups()[i]] = m->getGroups()[i];
+ }
+ newCt.createTable(nameMap, groupMap, gps);
+
+ //clear old tree names if any
+ m->Treenames.clear();
+
+ //fills globaldatas tree names
+ m->Treenames = m->getGroups();
+
+ vector<Tree*> newTrees = buildTrees(dists, treeNum, newCt); //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) + ".unweighted.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, "UnifracUnweightedCommand", "getConsensusTrees");
+ exit(1);
+ }
+}
+/**************************************************************************************************/
+
+vector<Tree*> UnifracUnweightedCommand::buildTrees(vector< vector<double> >& dists, int treeNum, CountTable& myct) {
+ try {
+
+ vector<Tree*> trees;
+
+ //create a new filename
+ string outputFile = outputDir + m->getRootName(m->getSimpleName(treefile)) + toString(treeNum+1) + ".unweighted.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(&myct, sims);
+ tempTree->assembleTree();
+
+ 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, "UnifracUnweightedCommand", "buildTrees");
+ exit(1);
+ }
+}
+/**************************************************************************************************/
+
+int UnifracUnweightedCommand::runRandomCalcs(Tree* thisTree, vector<double> usersScores) {
+ try {
+ vector<double> randomData; randomData.resize(numComp,0); //weighted score info for random trees. data[0] = weightedscore AB, data[1] = weightedscore AC...
+
+ Unweighted unweighted(includeRoot);
+
+ //get unweighted scores for random trees - if random is false iters = 0
+ for (int j = 0; j < iters; j++) {
+
+ //we need a different getValues because when we swap the labels we only want to swap those in each pairwise comparison
+ randomData = unweighted.getValues(thisTree, "", "", processors, outputDir);
+
+ if (m->control_pressed) { return 0; }
+
+ for(int k = 0; k < numComp; k++) {
+ //add trees unweighted score to map of scores
+ map<float,float>::iterator it = rscoreFreq[k].find(randomData[k]);
+ if (it != rscoreFreq[k].end()) {//already have that score
+ rscoreFreq[k][randomData[k]]++;
+ }else{//first time we have seen this score
+ rscoreFreq[k][randomData[k]] = 1;
+ }
+
+ //add randoms score to validscores
+ validScores[randomData[k]] = randomData[k];
+ }
+ }
+
+ for(int a = 0; a < numComp; a++) {
+ float rcumul = 1.0000;
+
+ //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
+ for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) {
+ //make rscoreFreq map and rCumul
+ map<float,float>::iterator it2 = rscoreFreq[a].find(it->first);
+ rCumul[a][it->first] = rcumul;
+ //get percentage of random trees with that info
+ if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
+ else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
+ }
+ UWScoreSig[a].push_back(rCumul[a][usersScores[a]]);
+ }
+
+ return 0;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "UnifracUnweightedCommand", "runRandomCalcs");