+ }
+ catch(exception& e) {
+ m->errorOut(e, "UnifracUnweightedCommand", "UnifracUnweightedCommand");
+ exit(1);
+ }
+}
+
+/***********************************************************/
+int UnifracUnweightedCommand::execute() {
+ try {
+
+ if (abort == true) { if (calledHelp) { return 0; } return 2; }
+
+ m->setTreeFile(treefile);
+
+ TreeReader* reader;
+ if (countfile == "") { reader = new TreeReader(treefile, groupfile, namefile); }
+ else { reader = new TreeReader(treefile, countfile); }
+ T = reader->getTrees();
+ ct = T[0]->getCountTable();
+ delete reader;
+
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(treefile));
+ sumFile = getOutputFileName("uwsummary",variables);
+ outputNames.push_back(sumFile); outputTypes["uwsummary"].push_back(sumFile);
+ m->openOutputFile(sumFile, outSum);
+
+ SharedUtil util;
+ Groups = m->getGroups();
+ vector<string> namesGroups = ct->getNamesOfGroups();
+ util.setGroups(Groups, namesGroups, allGroups, numGroups, "unweighted"); //sets the groups the user wants to analyze
+
+ Unweighted unweighted(includeRoot);
+
+ int start = time(NULL);
+
+ //set or check size
+ if (subsample) {
+ //user has not set size, set size = smallest samples size
+ if (subsampleSize == -1) {
+ vector<string> temp; temp.push_back(Groups[0]);
+ subsampleSize = ct->getGroupCount(Groups[0]); //num in first group
+ for (int i = 1; i < Groups.size(); i++) {
+ int thisSize = ct->getGroupCount(Groups[i]);
+ if (thisSize < subsampleSize) { subsampleSize = thisSize; }
+ }
+ m->mothurOut("\nSetting subsample size to " + toString(subsampleSize) + ".\n\n");
+ }else { //eliminate any too small groups
+ vector<string> newGroups = Groups;
+ Groups.clear();
+ for (int i = 0; i < newGroups.size(); i++) {
+ int thisSize = ct->getGroupCount(newGroups[i]);
+
+ if (thisSize >= subsampleSize) { Groups.push_back(newGroups[i]); }
+ else { m->mothurOut("You have selected a size that is larger than "+newGroups[i]+" number of sequences, removing "+newGroups[i]+".\n"); }
+ }
+ m->setGroups(Groups);
+ }
+ }
+
+ util.getCombos(groupComb, Groups, numComp);
+ m->setGroups(Groups);
+
+ if (numGroups == 1) { numComp++; groupComb.push_back(allGroups); }
+
+ if (numComp < processors) { processors = numComp; }
+
+ if (consensus && (numComp < 2)) { m->mothurOut("consensus can only be used with numComparisions greater than 1, setting consensus=f.\n"); consensus=false; }
+
+ outSum << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" <<'\t';
+ m->mothurOut("Tree#\tGroups\tUWScore\t");
+ if (random) { outSum << "UWSig"; m->mothurOut("UWSig"); }
+ outSum << endl; m->mothurOutEndLine();
+
+ //get pscores for users trees
+ for (int i = 0; i < T.size(); i++) {
+ if (m->control_pressed) { delete ct; for (int i = 0; i < T.size(); i++) { delete T[i]; }outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
+
+ counter = 0;
+
+ if (random) {
+ variables["[filename]"] = outputDir + m->getSimpleName(treefile);
+ variables["[tag]"] = toString(i+1);
+ string unFileName = getOutputFileName("unweighted", variables);
+ output = new ColumnFile(unFileName, itersString);
+ outputNames.push_back(unFileName); outputTypes["unweighted"].push_back(unFileName);
+ }
+
+
+ //get unweighted for users tree
+ rscoreFreq.resize(numComp);
+ rCumul.resize(numComp);
+ utreeScores.resize(numComp);
+ UWScoreSig.resize(numComp);
+
+ vector<double> userData; userData.resize(numComp,0); //weighted score info for user tree. data[0] = weightedscore AB, data[1] = weightedscore AC...
+
+ userData = unweighted.getValues(T[i], processors, outputDir); //userData[0] = unweightedscore
+
+ if (m->control_pressed) { delete ct; for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); }return 0; }
+
+ //output scores for each combination
+ for(int k = 0; k < numComp; k++) {
+ //saves users score
+ utreeScores[k].push_back(userData[k]);
+
+ //add users score to validscores
+ validScores[userData[k]] = userData[k];
+
+ if (!random) { UWScoreSig[k].push_back(0.0); }
+ }
+
+ if (random) { runRandomCalcs(T[i], userData); }
+
+ if (m->control_pressed) { delete ct; for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
+
+ int startSubsample = time(NULL);
+
+ //subsample loop
+ vector< vector<double> > calcDistsTotals; //each iter, each groupCombos dists. this will be used to make .dist files
+ for (int thisIter = 0; thisIter < subsampleIters; thisIter++) { //subsampleIters=0, if subsample=f.
+ if (m->control_pressed) { break; }
+
+ //copy to preserve old one - would do this in subsample but memory cleanup becomes messy.
+ CountTable* newCt = new CountTable();
+
+ //uses method of setting groups to doNotIncludeMe
+ int sampleTime = 0;
+ if (m->debug) { sampleTime = time(NULL); }
+ SubSample sample;
+ Tree* subSampleTree = sample.getSample(T[i], ct, newCt, subsampleSize);
+
+ if (m->debug) { m->mothurOut("[DEBUG]: iter " + toString(thisIter) + " took " + toString(time(NULL) - sampleTime) + " seconds to sample tree.\n"); }
+
+ //call new weighted function
+ vector<double> iterData; iterData.resize(numComp,0);
+ Unweighted thisUnweighted(includeRoot);
+ iterData = thisUnweighted.getValues(subSampleTree, processors, outputDir); //userData[0] = weightedscore
+
+ //save data to make ave dist, std dist
+ calcDistsTotals.push_back(iterData);
+
+ delete newCt;
+ delete subSampleTree;
+
+ if((thisIter+1) % 100 == 0){ m->mothurOut(toString(thisIter+1)); m->mothurOutEndLine(); }
+ }
+ if (subsample) { m->mothurOut("It took " + toString(time(NULL) - startSubsample) + " secs to run the subsampling."); m->mothurOutEndLine(); }
+
+ if (m->control_pressed) { delete ct; for (int i = 0; i < T.size(); i++) { delete T[i]; }if (random) { delete output; } outSum.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
+
+ if (subsample) { getAverageSTDMatrices(calcDistsTotals, i); }
+ if (consensus) { getConsensusTrees(calcDistsTotals, i); }
+
+ //print output files
+ printUWSummaryFile(i);
+ if (random) { printUnweightedFile(); delete output; }
+ if (phylip) { createPhylipFile(i); }