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 cout << "You must execute the read.tree command, before you may execute the unifrac.unweighted command." << endl; 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 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
77 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
82 //**********************************************************************************************************************
84 void UnifracUnweightedCommand::help(){
86 cout << "The unifrac.unweighted command can only be executed after a successful read.tree command." << "\n";
87 cout << "The unifrac.unweighted command parameters are groups and iters. No parameters are required." << "\n";
88 cout << "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";
89 cout << "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";
90 cout << "The unifrac.unweighted command should be in the following format: unifrac.unweighted(groups=yourGroups, iters=yourIters)." << "\n";
91 cout << "Example unifrac.unweighted(groups=A-B-C, iters=500)." << "\n";
92 cout << "The default value for groups is all the groups in your groupfile, and iters is 1000." << "\n";
93 cout << "The unifrac.unweighted command output two files: .unweighted and .uwsummary their descriptions are in the manual." << "\n";
94 cout << "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups)." << "\n" << "\n";
97 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
101 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
107 /***********************************************************/
108 int UnifracUnweightedCommand::execute() {
111 if (abort == true) { return 0; }
113 userData.resize(numComp,0); //data[0] = unweightedscore
114 randomData.resize(numComp,0); //data[0] = unweightedscore
115 //create new tree with same num nodes and leaves as users
117 outSum << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" <<'\t' << "UWSig" << endl;
118 cout << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" << '\t' << "UWSig" << endl;
120 //get pscores for users trees
121 for (int i = 0; i < T.size(); i++) {
124 output = new ColumnFile(globaldata->getTreeFile() + toString(i+1) + ".unweighted", itersString);
126 //get unweighted for users tree
127 rscoreFreq.resize(numComp);
128 rCumul.resize(numComp);
129 utreeScores.resize(numComp);
130 UWScoreSig.resize(numComp);
132 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
134 //output scores for each combination
135 for(int k = 0; k < numComp; k++) {
137 utreeScores[k].push_back(userData[k]);
141 //get unweighted scores for random trees
142 for (int j = 0; j < iters; j++) {
143 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
144 randomData = unweighted->getValues(T[i], "", "");
146 for(int k = 0; k < numComp; k++) {
147 //add trees unweighted score to map of scores
148 map<float,float>::iterator it = rscoreFreq[k].find(randomData[k]);
149 if (it != rscoreFreq[k].end()) {//already have that score
150 rscoreFreq[k][randomData[k]]++;
151 }else{//first time we have seen this score
152 rscoreFreq[k][randomData[k]] = 1;
155 //add randoms score to validscores
156 validScores[randomData[k]] = randomData[k];
161 for(int a = 0; a < numComp; a++) {
162 float rcumul = 1.0000;
163 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
164 for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) {
165 //make rscoreFreq map and rCumul
166 map<float,float>::iterator it2 = rscoreFreq[a].find(it->first);
167 rCumul[a][it->first] = rcumul;
168 //get percentage of random trees with that info
169 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
170 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
172 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
177 printUnweightedFile();
178 printUWSummaryFile(i);
188 //reset groups parameter
189 globaldata->Groups.clear();
195 catch(exception& e) {
196 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204 /***********************************************************/
205 void UnifracUnweightedCommand::printUnweightedFile() {
209 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
211 for(int a = 0; a < numComp; a++) {
212 output->initFile(groupComb[a], tags);
214 for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) {
215 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
216 output->output(data);
222 catch(exception& e) {
223 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232 /***********************************************************/
233 void UnifracUnweightedCommand::printUWSummaryFile(int i) {
237 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
241 for(int a = 0; a < numComp; a++) {
242 outSum << i+1 << '\t';
245 if (UWScoreSig[a][0] > (1/(float)iters)) {
246 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << UWScoreSig[a][0] << endl;
247 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << UWScoreSig[a][0] << endl;
249 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl;
250 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl;
255 catch(exception& e) {
256 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
260 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
265 /***********************************************************/