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"};
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 mothurOut("You must execute the read.tree command, before you may execute the unifrac.unweighted command."); mothurOutEndLine(); abort = true; }
40 //check for optional parameter and set defaults
41 // ...at some point should added some additional type checking...
42 groups = validParameter.validFile(parameters, "groups", false);
43 if (groups == "not found") { groups = ""; }
45 splitAtDash(groups, Groups);
46 globaldata->Groups = Groups;
49 itersString = validParameter.validFile(parameters, "iters", false); if (itersString == "not found") { itersString = "1000"; }
50 convert(itersString, iters);
54 T = globaldata->gTree;
55 tmap = globaldata->gTreemap;
56 sumFile = globaldata->getTreeFile() + ".uwsummary";
57 openOutputFile(sumFile, outSum);
59 util = new SharedUtil();
60 util->setGroups(globaldata->Groups, tmap->namesOfGroups, allGroups, numGroups, "unweighted"); //sets the groups the user wants to analyze
61 util->getCombos(groupComb, globaldata->Groups, numComp);
63 if (numGroups == 1) { numComp++; groupComb.push_back(allGroups); }
65 unweighted = new Unweighted(tmap);
73 errorOut(e, "UnifracUnweightedCommand", "UnifracUnweightedCommand");
78 //**********************************************************************************************************************
80 void UnifracUnweightedCommand::help(){
82 mothurOut("The unifrac.unweighted command can only be executed after a successful read.tree command.\n");
83 mothurOut("The unifrac.unweighted command parameters are groups and iters. No parameters are required.\n");
84 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");
85 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");
86 mothurOut("The unifrac.unweighted command should be in the following format: unifrac.unweighted(groups=yourGroups, iters=yourIters).\n");
87 mothurOut("Example unifrac.unweighted(groups=A-B-C, iters=500).\n");
88 mothurOut("The default value for groups is all the groups in your groupfile, and iters is 1000.\n");
89 mothurOut("The unifrac.unweighted command output two files: .unweighted and .uwsummary their descriptions are in the manual.\n");
90 mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
93 errorOut(e, "UnifracUnweightedCommand", "help");
99 /***********************************************************/
100 int UnifracUnweightedCommand::execute() {
103 if (abort == true) { return 0; }
105 userData.resize(numComp,0); //data[0] = unweightedscore
106 randomData.resize(numComp,0); //data[0] = unweightedscore
107 //create new tree with same num nodes and leaves as users
109 outSum << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" <<'\t' << "UWSig" << endl;
110 mothurOut("Tree#\tGroups\tUWScore\tUWSig"); mothurOutEndLine();
112 //get pscores for users trees
113 for (int i = 0; i < T.size(); i++) {
116 output = new ColumnFile(globaldata->getTreeFile() + toString(i+1) + ".unweighted", itersString);
118 //get unweighted for users tree
119 rscoreFreq.resize(numComp);
120 rCumul.resize(numComp);
121 utreeScores.resize(numComp);
122 UWScoreSig.resize(numComp);
124 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
126 //output scores for each combination
127 for(int k = 0; k < numComp; k++) {
129 utreeScores[k].push_back(userData[k]);
133 //get unweighted scores for random trees
134 for (int j = 0; j < iters; j++) {
135 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
136 randomData = unweighted->getValues(T[i], "", "");
138 for(int k = 0; k < numComp; k++) {
139 //add trees unweighted score to map of scores
140 map<float,float>::iterator it = rscoreFreq[k].find(randomData[k]);
141 if (it != rscoreFreq[k].end()) {//already have that score
142 rscoreFreq[k][randomData[k]]++;
143 }else{//first time we have seen this score
144 rscoreFreq[k][randomData[k]] = 1;
147 //add randoms score to validscores
148 validScores[randomData[k]] = randomData[k];
153 for(int a = 0; a < numComp; a++) {
154 float rcumul = 1.0000;
155 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
156 for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) {
157 //make rscoreFreq map and rCumul
158 map<float,float>::iterator it2 = rscoreFreq[a].find(it->first);
159 rCumul[a][it->first] = rcumul;
160 //get percentage of random trees with that info
161 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
162 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
164 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
169 printUnweightedFile();
170 printUWSummaryFile(i);
180 //reset groups parameter
181 globaldata->Groups.clear();
187 catch(exception& e) {
188 errorOut(e, "UnifracUnweightedCommand", "execute");
192 /***********************************************************/
193 void UnifracUnweightedCommand::printUnweightedFile() {
197 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
199 for(int a = 0; a < numComp; a++) {
200 output->initFile(groupComb[a], tags);
202 for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) {
203 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
204 output->output(data);
210 catch(exception& e) {
211 errorOut(e, "UnifracUnweightedCommand", "printUnweightedFile");
216 /***********************************************************/
217 void UnifracUnweightedCommand::printUWSummaryFile(int i) {
221 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
225 for(int a = 0; a < numComp; a++) {
226 outSum << i+1 << '\t';
227 mothurOut(toString(i+1) + "\t");
229 if (UWScoreSig[a][0] > (1/(float)iters)) {
230 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << UWScoreSig[a][0] << endl;
231 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << UWScoreSig[a][0] << endl;
232 mothurOutJustToLog(groupComb[a] + "\t" + toString(utreeScores[a][0]) + "\t" + toString(UWScoreSig[a][0])); mothurOutEndLine();
234 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl;
235 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl;
236 mothurOutJustToLog(groupComb[a] + "\t" + toString(utreeScores[a][0]) + "\t<" + toString((1/float(iters)))); mothurOutEndLine();
241 catch(exception& e) {
242 errorOut(e, "UnifracUnweightedCommand", "printUWSummaryFile");
247 /***********************************************************/