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() {
15 globaldata = GlobalData::getInstance();
17 T = globaldata->gTree;
18 tmap = globaldata->gTreemap;
19 sumFile = globaldata->getTreeFile() + ".uwsummary";
20 openOutputFile(sumFile, outSum);
22 util = new SharedUtil();
23 util->setGroups(globaldata->Groups, tmap->namesOfGroups, allGroups, numGroups, "unweighted"); //sets the groups the user wants to analyze
24 util->getCombos(groupComb, globaldata->Groups, numComp);
25 globaldata->setGroups("");
29 groupComb.push_back(allGroups);
34 convert(globaldata->getIters(), iters); //how many random trees to generate
35 unweighted = new Unweighted(tmap);
39 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
43 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47 /***********************************************************/
48 int UnifracUnweightedCommand::execute() {
51 userData.resize(numComp,0); //data[0] = unweightedscore
52 randomData.resize(numComp,0); //data[0] = unweightedscore
53 //create new tree with same num nodes and leaves as users
55 outSum << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" <<'\t' << "UWSig" << endl;
56 cout << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" << '\t' << "UWSig" << endl;
58 //get pscores for users trees
59 for (int i = 0; i < T.size(); i++) {
62 output = new ColumnFile(globaldata->getTreeFile() + toString(i+1) + ".unweighted");
64 outSum << i+1 << '\t';
67 //get unweighted for users tree
68 rscoreFreq.resize(numComp);
69 rCumul.resize(numComp);
70 utreeScores.resize(numComp);
71 UWScoreSig.resize(numComp);
73 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
75 //output scores for each combination
76 for(int k = 0; k < numComp; k++) {
78 utreeScores[k].push_back(userData[k]);
82 //get unweighted scores for random trees
83 for (int j = 0; j < iters; j++) {
84 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
85 randomData = unweighted->getValues(T[i], "", "");
87 for(int k = 0; k < numComp; k++) {
88 //add trees unweighted score to map of scores
89 it2 = rscoreFreq[k].find(randomData[k]);
90 if (it2 != rscoreFreq[k].end()) {//already have that score
91 rscoreFreq[k][randomData[k]]++;
92 }else{//first time we have seen this score
93 rscoreFreq[k][randomData[k]] = 1;
96 //add randoms score to validscores
97 validScores[randomData[k]] = randomData[k];
102 for(int a = 0; a < numComp; a++) {
103 float rcumul = 1.0000;
104 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
105 for (it = validScores.begin(); it != validScores.end(); it++) {
106 //make rscoreFreq map and rCumul
107 it2 = rscoreFreq[a].find(it->first);
108 rCumul[a][it->first] = rcumul;
109 //get percentage of random trees with that info
110 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
111 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
113 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
118 printUnweightedFile();
119 printUWSummaryFile();
129 //reset groups parameter
130 globaldata->Groups.clear();
136 catch(exception& e) {
137 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145 /***********************************************************/
146 void UnifracUnweightedCommand::printUnweightedFile() {
150 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
152 for(int a = 0; a < numComp; a++) {
153 output->initFile(groupComb[a], tags);
155 for (it = validScores.begin(); it != validScores.end(); it++) {
156 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
157 output->output(data);
163 catch(exception& e) {
164 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
168 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173 /***********************************************************/
174 void UnifracUnweightedCommand::printUWSummaryFile() {
178 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
182 for(int a = 0; a < numComp; a++) {
183 if (UWScoreSig[a][0] > (1/(float)iters)) {
184 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
185 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
187 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
188 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
193 catch(exception& e) {
194 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
198 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203 /***********************************************************/