2 * unifracunweightedcommand.cpp
5 * Created by Sarah Westcott on 2/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "unifracunweightedcommand.h"
12 /***********************************************************/
13 UnifracUnweightedCommand::UnifracUnweightedCommand(string option) {
15 globaldata = GlobalData::getInstance();
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"groups","iters","distance","random", "outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
32 //check to make sure all parameters are valid for command
33 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
37 if (globaldata->gTree.size() == 0) {//no trees were read
38 m->mothurOut("You must execute the read.tree command, before you may execute the unifrac.unweighted command."); m->mothurOutEndLine(); abort = true; }
40 //if the user changes the output directory command factory will send this info to us in the output parameter
41 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
43 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
46 //check for optional parameter and set defaults
47 // ...at some point should added some additional type checking...
48 groups = validParameter.validFile(parameters, "groups", false);
49 if (groups == "not found") { groups = ""; }
51 splitAtDash(groups, Groups);
52 globaldata->Groups = Groups;
55 itersString = validParameter.validFile(parameters, "iters", false); if (itersString == "not found") { itersString = "1000"; }
56 convert(itersString, iters);
58 string temp = validParameter.validFile(parameters, "distance", false); if (temp == "not found") { temp = "false"; }
59 phylip = isTrue(temp);
61 temp = validParameter.validFile(parameters, "random", false); if (temp == "not found") { temp = "true"; }
62 random = isTrue(temp);
64 if (!random) { iters = 0; } //turn off random calcs
66 //if user selects distance = true and no groups it won't calc the pairwise
67 if ((phylip) && (Groups.size() == 0)) {
69 splitAtDash(groups, Groups);
70 globaldata->Groups = Groups;
74 T = globaldata->gTree;
75 tmap = globaldata->gTreemap;
76 sumFile = outputDir + getSimpleName(globaldata->getTreeFile()) + ".uwsummary";
77 outputNames.push_back(sumFile);
78 openOutputFile(sumFile, outSum);
80 util = new SharedUtil();
81 util->setGroups(globaldata->Groups, tmap->namesOfGroups, allGroups, numGroups, "unweighted"); //sets the groups the user wants to analyze
82 util->getCombos(groupComb, globaldata->Groups, numComp);
84 if (numGroups == 1) { numComp++; groupComb.push_back(allGroups); }
86 unweighted = new Unweighted(tmap);
94 m->errorOut(e, "UnifracUnweightedCommand", "UnifracUnweightedCommand");
99 //**********************************************************************************************************************
101 void UnifracUnweightedCommand::help(){
103 m->mothurOut("The unifrac.unweighted command can only be executed after a successful read.tree command.\n");
104 m->mothurOut("The unifrac.unweighted command parameters are groups, iters, distance and random. No parameters are required.\n");
105 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed. You must enter at least 1 valid group.\n");
106 m->mothurOut("The group names are separated by dashes. The iters parameter allows you to specify how many random trees you would like compared to your tree.\n");
107 m->mothurOut("The distance parameter allows you to create a distance file from the results. The default is false.\n");
108 m->mothurOut("The random parameter allows you to shut off the comparison to random trees. The default is true, meaning compare your trees with randomly generated trees.\n");
109 m->mothurOut("The unifrac.unweighted command should be in the following format: unifrac.unweighted(groups=yourGroups, iters=yourIters).\n");
110 m->mothurOut("Example unifrac.unweighted(groups=A-B-C, iters=500).\n");
111 m->mothurOut("The default value for groups is all the groups in your groupfile, and iters is 1000.\n");
112 m->mothurOut("The unifrac.unweighted command output two files: .unweighted and .uwsummary their descriptions are in the manual.\n");
113 m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
115 catch(exception& e) {
116 m->errorOut(e, "UnifracUnweightedCommand", "help");
122 /***********************************************************/
123 int UnifracUnweightedCommand::execute() {
126 if (abort == true) { return 0; }
128 userData.resize(numComp,0); //data[0] = unweightedscore
129 randomData.resize(numComp,0); //data[0] = unweightedscore
130 //create new tree with same num nodes and leaves as users
132 outSum << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" <<'\t' << "UWSig" << endl;
133 m->mothurOut("Tree#\tGroups\tUWScore\tUWSig"); m->mothurOutEndLine();
135 //get pscores for users trees
136 for (int i = 0; i < T.size(); i++) {
137 if (m->control_pressed) {
139 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
146 output = new ColumnFile(outputDir + getSimpleName(globaldata->getTreeFile()) + toString(i+1) + ".unweighted", itersString);
147 outputNames.push_back(outputDir + getSimpleName(globaldata->getTreeFile()) + toString(i+1) + ".unweighted");
151 //get unweighted for users tree
152 rscoreFreq.resize(numComp);
153 rCumul.resize(numComp);
154 utreeScores.resize(numComp);
155 UWScoreSig.resize(numComp);
157 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
159 if (m->control_pressed) {
160 if (random) { delete output; }
162 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
166 //output scores for each combination
167 for(int k = 0; k < numComp; k++) {
169 utreeScores[k].push_back(userData[k]);
171 //add users score to validscores
172 validScores[userData[k]] = userData[k];
175 //get unweighted scores for random trees - if random is false iters = 0
176 for (int j = 0; j < iters; j++) {
177 //we need a different getValues because when we swap the labels we only want to swap those in each pairwise comparison
178 randomData = unweighted->getValues(T[i], "", "");
180 if (m->control_pressed) {
181 if (random) { delete output; }
183 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
187 for(int k = 0; k < numComp; k++) {
188 //add trees unweighted score to map of scores
189 map<float,float>::iterator it = rscoreFreq[k].find(randomData[k]);
190 if (it != rscoreFreq[k].end()) {//already have that score
191 rscoreFreq[k][randomData[k]]++;
192 }else{//first time we have seen this score
193 rscoreFreq[k][randomData[k]] = 1;
196 //add randoms score to validscores
197 validScores[randomData[k]] = randomData[k];
201 for(int a = 0; a < numComp; a++) {
202 float rcumul = 1.0000;
203 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
204 for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) {
205 //make rscoreFreq map and rCumul
206 map<float,float>::iterator it2 = rscoreFreq[a].find(it->first);
207 rCumul[a][it->first] = rcumul;
208 //get percentage of random trees with that info
209 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
210 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
213 if (random) { UWScoreSig[a].push_back(rCumul[a][userData[a]]); }
214 else { UWScoreSig[a].push_back(0.0); }
218 if (m->control_pressed) {
219 if (random) { delete output; }
221 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
226 printUWSummaryFile(i);
227 if (random) { printUnweightedFile(); delete output; }
228 if (phylip) { createPhylipFile(i); }
240 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
242 m->mothurOutEndLine();
243 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
244 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
245 m->mothurOutEndLine();
250 catch(exception& e) {
251 m->errorOut(e, "UnifracUnweightedCommand", "execute");
255 /***********************************************************/
256 void UnifracUnweightedCommand::printUnweightedFile() {
261 tags.push_back("Score");
262 tags.push_back("RandFreq"); tags.push_back("RandCumul");
264 for(int a = 0; a < numComp; a++) {
265 output->initFile(groupComb[a], tags);
267 for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) {
268 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
269 output->output(data);
275 catch(exception& e) {
276 m->errorOut(e, "UnifracUnweightedCommand", "printUnweightedFile");
281 /***********************************************************/
282 void UnifracUnweightedCommand::printUWSummaryFile(int i) {
286 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
290 for(int a = 0; a < numComp; a++) {
291 outSum << i+1 << '\t';
292 m->mothurOut(toString(i+1) + "\t");
295 if (UWScoreSig[a][0] > (1/(float)iters)) {
296 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << UWScoreSig[a][0] << endl;
297 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << UWScoreSig[a][0] << endl;
298 m->mothurOutJustToLog(groupComb[a] + "\t" + toString(utreeScores[a][0]) + "\t" + toString(UWScoreSig[a][0])); m->mothurOutEndLine();
300 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl;
301 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl;
302 m->mothurOutJustToLog(groupComb[a] + "\t" + toString(utreeScores[a][0]) + "\t<" + toString((1/float(iters)))); m->mothurOutEndLine();
305 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << "0.00" << endl;
306 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << "0.00" << endl;
307 m->mothurOutJustToLog(groupComb[a] + "\t" + toString(utreeScores[a][0]) + "\t0.00"); m->mothurOutEndLine();
312 catch(exception& e) {
313 m->errorOut(e, "UnifracUnweightedCommand", "printUWSummaryFile");
317 /***********************************************************/
318 void UnifracUnweightedCommand::createPhylipFile(int i) {
320 string phylipFileName = outputDir + getSimpleName(globaldata->getTreeFile()) + toString(i+1) + ".unweighted.dist";
321 outputNames.push_back(phylipFileName);
324 openOutputFile(phylipFileName, out);
327 out << globaldata->Groups.size() << endl;
329 //make matrix with scores in it
330 vector< vector<float> > dists; dists.resize(globaldata->Groups.size());
331 for (int i = 0; i < globaldata->Groups.size(); i++) {
332 dists[i].resize(globaldata->Groups.size(), 0.0);
335 //flip it so you can print it
337 for (int r=0; r<globaldata->Groups.size(); r++) {
338 for (int l = r+1; l < globaldata->Groups.size(); l++) {
339 dists[r][l] = utreeScores[count][0];
340 dists[l][r] = utreeScores[count][0];
346 for (int r=0; r<globaldata->Groups.size(); r++) {
348 string name = globaldata->Groups[r];
349 if (name.length() < 10) { //pad with spaces to make compatible
350 while (name.length() < 10) { name += " "; }
355 for (int l = 0; l < r; l++) { out << dists[r][l] << '\t'; }
360 catch(exception& e) {
361 m->errorOut(e, "UnifracUnweightedCommand", "createPhylipFile");
365 /***********************************************************/