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 sumFile = globaldata->getTreeFile() + ".wsummary";
20 openOutputFile(sumFile, outSum);
22 util = new SharedUtil();
23 string s; //to make work with setgroups
24 util->setGroups(globaldata->Groups, tmap->namesOfGroups, s, numGroups, "weighted"); //sets the groups the user wants to analyze
25 util->getCombos(groupComb, globaldata->Groups, numComp);
26 globaldata->setGroups("");
28 convert(globaldata->getIters(), iters); //how many random trees to generate
29 weighted = new Weighted(tmap);
33 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
37 cout << "An unknown error has occurred in the UnifracWeightedCommand class function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
41 /***********************************************************/
42 int UnifracWeightedCommand::execute() {
45 reading = new Progress("Comparing to random:", iters);
47 //get weighted for users tree
48 userData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
49 randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
51 //create new tree with same num nodes and leaves as users
54 //get weighted scores for users trees
55 for (int i = 0; i < T.size(); i++) {
57 rScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
58 uScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
60 output = new ColumnFile(globaldata->getTreeFile() + toString(i+1) + ".weighted");
62 userData = weighted->getValues(T[i]); //userData[0] = weightedscore
65 for (int s=0; s<numComp; s++) {
66 //add users score to vector of user scores
67 uScores[s].push_back(userData[s]);
69 //save users tree score for summary file
70 utreeScores.push_back(userData[s]);
73 //get scores for random trees
74 for (int j = 0; j < iters; j++) {
76 for (int r=0; r<numGroups; r++) {
77 for (int l = r+1; l < numGroups; l++) {
81 //create a random tree with same topology as T[i], but different labels
82 randT->assembleRandomUnifracTree(globaldata->Groups[r], globaldata->Groups[l]);
83 //get wscore of random tree
84 randomData = weighted->getValues(randT, globaldata->Groups[r], globaldata->Groups[l]);
87 rScores[count].push_back(randomData[0]);
97 //removeValidScoresDuplicates();
98 //find the signifigance of the score for summary file
99 for (int f = 0; f < numComp; f++) {
101 sort(rScores[f].begin(), rScores[f].end());
103 //the index of the score higher than yours is returned
104 //so if you have 1000 random trees the index returned is 100
105 //then there are 900 trees with a score greater then you.
106 //giving you a signifigance of 0.900
107 int index = findIndex(userData[f], f); if (index == -1) { cout << "error in UnifracWeightedCommand" << endl; exit(1); } //error code
109 //the signifigance is the number of trees with the users score or higher
110 WScoreSig.push_back((iters-index)/(float)iters);
113 //out << "Tree# " << i << endl;
114 calculateFreqsCumuls();
125 //finish progress bar
131 //clear out users groups
132 globaldata->Groups.clear();
139 catch(exception& e) {
140 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
144 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148 /***********************************************************/
149 void UnifracWeightedCommand::printWeightedFile() {
153 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
155 for(int a = 0; a < numComp; a++) {
156 output->initFile(groupComb[a], tags);
158 for (it = validScores.begin(); it != validScores.end(); it++) {
159 data.push_back(it->first); data.push_back(rScoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
160 output->output(data);
166 catch(exception& e) {
167 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
171 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177 /***********************************************************/
178 void UnifracWeightedCommand::printWSummaryFile() {
181 outSum << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" << endl;
182 cout << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" << endl;
185 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
189 for (int i = 0; i < T.size(); i++) {
190 for (int j = 0; j < numComp; j++) {
191 if (WScoreSig[count] > (1/(float)iters)) {
192 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << WScoreSig[count] << endl;
193 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << WScoreSig[count] << endl;
195 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
196 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
203 catch(exception& e) {
204 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
213 /***********************************************************/
214 int UnifracWeightedCommand::findIndex(float score, int index) {
216 for (int i = 0; i < rScores[index].size(); i++) {
217 if (rScores[index][i] >= score) { return i; }
219 return rScores[index].size();
221 catch(exception& e) {
222 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
226 cout << "An unknown error has occurred in the UnifracWeightedCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
231 /***********************************************************/
233 void UnifracWeightedCommand::calculateFreqsCumuls() {
235 //clear out old tree values
237 rScoreFreq.resize(numComp);
239 rCumul.resize(numComp);
242 //calculate frequency
243 for (int f = 0; f < numComp; f++) {
244 for (int i = 0; i < rScores[f].size(); i++) { //looks like 0,0,1,1,1,2,4,7... you want to make a map that say rScoreFreq[0] = 2, rScoreFreq[1] = 3...
245 validScores[rScores[f][i]] = rScores[f][i];
246 it = rScoreFreq[f].find(rScores[f][i]);
247 if (it != rScoreFreq[f].end()) {
248 rScoreFreq[f][rScores[f][i]]++;
250 rScoreFreq[f][rScores[f][i]] = 1;
256 for(int a = 0; a < numComp; a++) {
257 float rcumul = 1.0000;
258 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
259 for (it = validScores.begin(); it != validScores.end(); it++) {
260 //make rscoreFreq map and rCumul
261 it2 = rScoreFreq[a].find(it->first);
262 rCumul[a][it->first] = rcumul;
263 //get percentage of random trees with that info
264 if (it2 != rScoreFreq[a].end()) { rScoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
265 else { rScoreFreq[a][it->first] = 0.0000; } //no random trees with that score
270 catch(exception& e) {
271 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275 cout << "An unknown error has occurred in the UnifracWeightedCommand class function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
281 /***********************************************************/