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...
59 //weightedFile = globaldata->getTreeFile() + toString(i+1) + ".weighted";
60 //weightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".weighted";
61 output = new ThreeColumnFile2(globaldata->getTreeFile() + toString(i+1) + ".weighted");
63 userData = weighted->getValues(T[i]); //userData[0] = weightedscore
66 for (int s=0; s<numComp; s++) {
67 //add users score to vector of user scores
68 uScores[s].push_back(userData[s]);
70 //save users tree score for summary file
71 utreeScores.push_back(userData[s]);
74 //get scores for random trees
75 for (int j = 0; j < iters; j++) {
77 for (int r=0; r<numGroups; r++) {
78 for (int l = r+1; l < numGroups; l++) {
82 //create a random tree with same topology as T[i], but different labels
83 randT->assembleRandomUnifracTree(globaldata->Groups[r], globaldata->Groups[l]);
84 //get wscore of random tree
85 randomData = weighted->getValues(randT, globaldata->Groups[r], globaldata->Groups[l]);
88 rScores[count].push_back(randomData[0]);
98 //removeValidScoresDuplicates();
99 //find the signifigance of the score for summary file
100 for (int f = 0; f < numComp; f++) {
102 sort(rScores[f].begin(), rScores[f].end());
104 //the index of the score higher than yours is returned
105 //so if you have 1000 random trees the index returned is 100
106 //then there are 900 trees with a score greater then you.
107 //giving you a signifigance of 0.900
108 int index = findIndex(userData[f], f); if (index == -1) { cout << "error in UnifracWeightedCommand" << endl; exit(1); } //error code
110 //the signifigance is the number of trees with the users score or higher
111 WScoreSig.push_back((iters-index)/(float)iters);
114 //out << "Tree# " << i << endl;
115 calculateFreqsCumuls();
126 //finish progress bar
132 //clear out users groups
133 globaldata->Groups.clear();
140 catch(exception& e) {
141 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
145 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
149 /***********************************************************/
150 void UnifracWeightedCommand::printWeightedFile() {
154 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
156 for(int a = 0; a < numComp; a++) {
157 output->initFile(groupComb[a], tags);
159 for (it = validScores.begin(); it != validScores.end(); it++) {
160 data.push_back(it->first); data.push_back(rScoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
161 output->output(data);
169 //remove(weightedFileout.c_str());
172 catch(exception& e) {
173 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183 /***********************************************************/
184 void UnifracWeightedCommand::printWSummaryFile() {
187 outSum << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" << endl;
188 cout << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" << endl;
191 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
195 for (int i = 0; i < T.size(); i++) {
196 for (int j = 0; j < numComp; j++) {
197 if (WScoreSig[count] > (1/(float)iters)) {
198 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << WScoreSig[count] << endl;
199 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << WScoreSig[count] << endl;
201 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
202 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
209 catch(exception& e) {
210 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
214 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
219 /***********************************************************/
220 int UnifracWeightedCommand::findIndex(float score, int index) {
222 for (int i = 0; i < rScores[index].size(); i++) {
223 if (rScores[index][i] >= score) { return i; }
225 return rScores[index].size();
227 catch(exception& e) {
228 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232 cout << "An unknown error has occurred in the UnifracWeightedCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237 /***********************************************************/
239 void UnifracWeightedCommand::calculateFreqsCumuls() {
241 //clear out old tree values
243 rScoreFreq.resize(numComp);
245 rCumul.resize(numComp);
248 //calculate frequency
249 for (int f = 0; f < numComp; f++) {
250 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...
251 validScores[rScores[f][i]] = rScores[f][i];
252 it = rScoreFreq[f].find(rScores[f][i]);
253 if (it != rScoreFreq[f].end()) {
254 rScoreFreq[f][rScores[f][i]]++;
256 rScoreFreq[f][rScores[f][i]] = 1;
262 for(int a = 0; a < numComp; a++) {
263 float rcumul = 1.0000;
264 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
265 for (it = validScores.begin(); it != validScores.end(); it++) {
266 //make rscoreFreq map and rCumul
267 it2 = rScoreFreq[a].find(it->first);
268 rCumul[a][it->first] = rcumul;
269 //get percentage of random trees with that info
270 if (it2 != rScoreFreq[a].end()) { rScoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
271 else { rScoreFreq[a][it->first] = 0.0000; } //no random trees with that score
276 catch(exception& e) {
277 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
281 cout << "An unknown error has occurred in the UnifracWeightedCommand class function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 /*****************************************************************
289 void UnifracWeightedCommand::initFile(string label){
292 openOutputFile(weightedFileout, out);
293 openInputFile(weightedFile, inFile);
296 getline(inFile, inputBuffer);
298 out << inputBuffer << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
300 openOutputFile(weightedFileout, out);
301 out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
304 out.setf(ios::fixed, ios::floatfield);
305 out.setf(ios::showpoint);
307 catch(exception& e) {
308 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
312 cout << "An unknown error has occurred in the UnifracWeightedCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317 /***********************************************************************
319 void UnifracWeightedCommand::output(vector<double> data){
323 getline(inFile, inputBuffer);
325 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
328 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
333 catch(exception& e) {
334 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
338 cout << "An unknown error has occurred in the UnifracWeightedCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
343 /***********************************************************************
345 void UnifracWeightedCommand::resetFile(){
356 remove(weightedFile.c_str());
357 rename(weightedFileout.c_str(), weightedFile.c_str());
359 catch(exception& e) {
360 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
364 cout << "An unknown error has occurred in the UnifracWeightedCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";