2 * unifracweightedcommand.cpp
5 * Created by Sarah Westcott on 2/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "unifracweightedcommand.h"
12 /***********************************************************/
13 UnifracWeightedCommand::UnifracWeightedCommand() {
15 globaldata = GlobalData::getInstance();
17 T = globaldata->gTree;
18 tmap = globaldata->gTreemap;
19 weightedFile = globaldata->getTreeFile() + ".weighted";
20 openOutputFile(weightedFile, out);
21 sumFile = globaldata->getTreeFile() + ".wsummary";
22 openOutputFile(sumFile, outSum);
23 distFile = globaldata->getTreeFile() + ".wdistrib";
24 openOutputFile(distFile, outDist);
26 numGroups = tmap->getNumGroups();
28 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
31 for (int i=1; i<numGroups; i++) {
33 for (int l = n; l < numGroups; l++) {
34 //set group comparison labels
35 groupComb.push_back(tmap->namesOfGroups[i-1]+tmap->namesOfGroups[l]);
40 convert(globaldata->getIters(), iters); //how many random trees to generate
41 weighted = new Weighted(tmap);
45 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
49 cout << "An unknown error has occurred in the UnifracWeightedCommand class function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
53 /***********************************************************/
54 int UnifracWeightedCommand::execute() {
57 //get weighted for users tree
58 userData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
59 randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
60 uscoreFreq.resize(numComp);
61 validScores.resize(numComp);
62 totalrscoreFreq.resize(numComp);
63 uCumul.resize(numComp);
66 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
67 outDist << "Tree#" << '\t' << "Iter" << '\t' << "Groups"<< '\t' << "WScore" << endl;
70 //create new tree with same num nodes and leaves as users
73 //get pscores for users trees
74 for (int i = 0; i < T.size(); i++) {
75 rscoreFreq.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
76 rCumul.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
78 cout << "Processing tree " << i+1 << endl;
79 userData = weighted->getValues(T[i]); //userData[0] = weightedscore
82 for (int s=0; s<numComp; s++) {
84 it = uscoreFreq[s].find(userData[s]);
85 if (it == uscoreFreq[s].end()) {//new score
86 uscoreFreq[s][userData[s]] = 1;
87 }else{ uscoreFreq[s][userData[s]]++; }
89 //add user score to valid scores
90 validScores[s][userData[s]] = userData[s];
92 //save users tree score for summary file
93 utreeScores.push_back(userData[s]);
99 //get pscores for random trees
100 for (int j = 0; j < iters; j++) {
101 //create a random tree with same topology as T[i], but different labels
102 randT->assembleRandomUnifracTree();
103 //get pscore of random tree
104 randomData = weighted->getValues(randT);
107 for (int p=0; p<numComp; p++) {
108 //add trees weighted score random score freq
109 it2 = rscoreFreq[p].find(randomData[p]);
110 if (it2 != rscoreFreq[p].end()) {//already have that score
111 rscoreFreq[p][randomData[p]]++;
112 }else{//first time we have seen this score
113 rscoreFreq[p][randomData[p]] = 1;
116 //add random score to valid scores
117 validScores[p][randomData[p]] = randomData[p];
119 //output info to uwdistrib file
120 outDist << i+1 << '\t' << '\t'<< j+1 << '\t' << '\t' << groupComb[p] << '\t'<< randomData[p] << endl;
124 saveRandomScores(); //save all random scores for weighted file
126 //find the signifigance of the score for summary file
127 for (int t = 0; t < numComp; t++) {
128 float rcumul = 0.0000;
129 for (it = validScores[t].begin(); it != validScores[t].end(); it++) {
130 //make rscoreFreq map and rCumul
131 it2 = rscoreFreq[t].find(it->first);
132 //get percentage of random trees with that info
133 if (it2 != rscoreFreq[t].end()) { rscoreFreq[t][it->first] /= iters; rcumul+= it2->second; }
134 else { rscoreFreq[t][it->first] = 0.0000; } //no random trees with that score
135 rCumul[t][it->first] = rcumul;
139 //save the signifigance of the users score for printing later
140 for (int f = 0; f < numComp; f++) {
141 WScoreSig.push_back(rCumul[f][userData[f]]);
150 rCumul.resize(numComp);
151 for (int b = 0; b < numComp; b++) {
152 float ucumul = 0.0000;
153 float rcumul = 0.0000;
154 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
155 for (it = validScores[b].begin(); it != validScores[b].end(); it++) {
156 it2 = uscoreFreq[b].find(it->first);
157 //user data has that score
158 if (it2 != uscoreFreq[b].end()) { uscoreFreq[b][it->first] /= T.size(); ucumul+= it2->second; }
159 else { uscoreFreq[b][it->first] = 0.0000; } //no user trees with that score
161 uCumul[b][it->first] = ucumul;
163 //make rscoreFreq map and rCumul
164 it2 = totalrscoreFreq[b].find(it->first);
165 //get percentage of random trees with that info
166 if (it2 != totalrscoreFreq[b].end()) { totalrscoreFreq[b][it->first] /= (iters * T.size()); rcumul+= it2->second; }
167 else { totalrscoreFreq[b][it->first] = 0.0000; } //no random trees with that score
168 rCumul[b][it->first] = rcumul;
175 //reset randomTree parameter to 0
176 globaldata->setRandomTree("0");
183 catch(exception& e) {
184 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
192 /***********************************************************/
193 void UnifracWeightedCommand::printWeightedFile() {
197 out << "Group" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
200 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
203 for (int e = 0; e < numComp; e++) {
204 //print each line in that group
205 for (it = validScores[e].begin(); it != validScores[e].end(); it++) {
206 out << setprecision(6) << groupComb[e] << '\t' << it->first << '\t' << '\t' << uscoreFreq[e][it->first] << '\t' << uCumul[e][it->first] << '\t' << totalrscoreFreq[e][it->first] << '\t' << rCumul[e][it->first] << endl;
213 catch(exception& e) {
214 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
218 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
224 /***********************************************************/
225 void UnifracWeightedCommand::printWSummaryFile() {
228 outSum << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" << endl;
231 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
235 for (int i = 0; i < T.size(); i++) {
236 for (int j = 0; j < numComp; j++) {
237 outSum << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl;
243 catch(exception& e) {
244 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
253 /***********************************************************/
254 void UnifracWeightedCommand::saveRandomScores() {
256 for (int e = 0; e < numComp; e++) {
257 //update total map with new random scores
258 for (it = rscoreFreq[e].begin(); it != rscoreFreq[e].end(); it++) {
259 //does this score already exist in the total map
260 it2 = totalrscoreFreq[e].find(it->first);
261 //if yes then add them
262 if (it2 != totalrscoreFreq[e].end()) {
263 totalrscoreFreq[e][it->first] += rscoreFreq[e][it->first];
264 }else{ //its a new score
265 totalrscoreFreq[e][it->first] = rscoreFreq[e][it->first];
270 catch(exception& e) {
271 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function saveRandomScores. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275 cout << "An unknown error has occurred in the UnifracWeightedCommand class function saveRandomScores. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
280 /***********************************************************/