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 unweightedFile = globaldata->getTreeFile() + ".unweighted";
20 openOutputFile(unweightedFile, out);
21 sumFile = globaldata->getTreeFile() + ".uwsummary";
22 openOutputFile(sumFile, outSum);
23 distFile = globaldata->getTreeFile() + ".uwdistrib";
24 openOutputFile(distFile, outDist);
26 convert(globaldata->getIters(), iters); //how many random trees to generate
27 unweighted = new Unweighted(tmap);
31 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
35 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
39 /***********************************************************/
40 int UnifracUnweightedCommand::execute() {
43 //get unweighted for users tree
44 userData.resize(1,0); //data[0] = unweightedscore
45 randomData.resize(1,0); //data[0] = unweightedscore
48 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
49 outDist << "Tree#" << '\t' << "Iter" << '\t' << "UWScore" << endl;
51 //create new tree with same num nodes and leaves as users
54 //get pscores for users trees
55 for (int i = 0; i < T.size(); i++) {
56 cout << "Processing tree " << i+1 << endl;
57 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
60 it = uscoreFreq.find(userData[0]);
61 if (it == uscoreFreq.end()) {//new score
62 uscoreFreq[userData[0]] = 1;
63 }else{ uscoreFreq[userData[0]]++; }
65 //add users score to valid scores
66 validScores[userData[0]] = userData[0];
69 utreeScores.push_back(userData[0]);
74 //get unweighted scores for random trees
75 for (int j = 0; j < iters; j++) {
76 //create a random tree with same topology as T[i], but different labels
77 randT->assembleRandomUnifracTree();
78 //get pscore of random tree
79 randomData = unweighted->getValues(randT);
81 //add trees unweighted score to map of scores
82 it2 = rscoreFreq.find(randomData[0]);
83 if (it2 != rscoreFreq.end()) {//already have that score
84 rscoreFreq[randomData[0]]++;
85 }else{//first time we have seen this score
86 rscoreFreq[randomData[0]] = 1;
89 //add randoms score to validscores
90 validScores[randomData[0]] = randomData[0];
92 //output info to uwdistrib file
93 outDist << i+1 << '\t' << '\t'<< j+1 << '\t' << '\t' << randomData[0] << endl;
96 //find the signifigance of the score
97 float rcumul = 0.0000;
98 for (it = rscoreFreq.begin(); it != rscoreFreq.end(); it++) {
99 //get percentage of random trees with that info
100 rscoreFreq[it->first] /= iters;
102 rCumul[it->first] = rcumul;
105 //save the signifigance of the users score for printing later
106 UWScoreSig.push_back(rCumul[userData[0]]);
108 saveRandomScores(); //save all random scores for unweighted file
111 rscoreFreq.clear(); //you clear this because in the summary file you want the unweighted signifinance to be relative to these 1000 trees.
115 float ucumul = 0.0000;
116 float rcumul = 0.0000;
117 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
118 for (it = validScores.begin(); it != validScores.end(); it++) {
119 it2 = uscoreFreq.find(it->first);
120 //user data has that score
121 if (it2 != uscoreFreq.end()) { uscoreFreq[it->first] /= T.size(); ucumul+= it2->second; }
122 else { uscoreFreq[it->first] = 0.0000; } //no user trees with that score
124 uCumul[it->first] = ucumul;
126 //make rscoreFreq map and rCumul
127 it2 = totalrscoreFreq.find(it->first);
128 //get percentage of random trees with that info
129 if (it2 != totalrscoreFreq.end()) { totalrscoreFreq[it->first] /= (iters*T.size()); rcumul+= it2->second; }
130 else { totalrscoreFreq[it->first] = 0.0000; } //no random trees with that score
131 rCumul[it->first] = rcumul;
134 printUnweightedFile();
135 printUWSummaryFile();
137 //reset randomTree parameter to 0
138 globaldata->setRandomTree("0");
145 catch(exception& e) {
146 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
150 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
154 /***********************************************************/
155 void UnifracUnweightedCommand::printUnweightedFile() {
159 out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
162 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
165 for (it = validScores.begin(); it != validScores.end(); it++) {
166 out << setprecision(6) << it->first << '\t' << '\t' << uscoreFreq[it->first] << '\t' << uCumul[it->first] << '\t' << totalrscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
172 catch(exception& e) {
173 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182 /***********************************************************/
183 void UnifracUnweightedCommand::printUWSummaryFile() {
186 outSum << "Tree#" << '\t' << "UWScore" << '\t' << '\t' << "UWSig" << endl;
189 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
192 for (int i = 0; i< T.size(); i++) {
193 outSum << setprecision(6) << i+1 << '\t' << '\t' << utreeScores[i] << '\t' << UWScoreSig[i] << endl;
198 catch(exception& e) {
199 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207 /***********************************************************/
208 void UnifracUnweightedCommand::saveRandomScores() {
210 //update total map with new random scores
211 for (it = rscoreFreq.begin(); it != rscoreFreq.end(); it++) {
212 //does this score already exist in the total map
213 it2 = totalrscoreFreq.find(it->first);
214 //if yes then add them
215 if (it2 != totalrscoreFreq.end()) {
216 it2->second += it->second;
217 }else{ //its a new score
218 totalrscoreFreq[it->first] = 1;
222 catch(exception& e) {
223 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function saveRandomScores. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function saveRandomScores. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232 /***********************************************************/