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 setGroups(); //sets the groups the user wants to analyze
23 convert(globaldata->getIters(), iters); //how many random trees to generate
24 weighted = new Weighted(tmap);
28 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
32 cout << "An unknown error has occurred in the UnifracWeightedCommand class function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
36 /***********************************************************/
37 int UnifracWeightedCommand::execute() {
40 reading = new Progress("Comparing to random:", iters);
42 //get weighted for users tree
43 userData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
44 randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
46 //create new tree with same num nodes and leaves as users
49 //get weighted scores for users trees
50 for (int i = 0; i < T.size(); i++) {
52 rScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
53 uScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
54 weightedFile = globaldata->getTreeFile() + toString(i+1) + ".weighted";
55 weightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".weighted";
57 userData = weighted->getValues(T[i]); //userData[0] = weightedscore
60 for (int s=0; s<numComp; s++) {
61 //add users score to vector of user scores
62 uScores[s].push_back(userData[s]);
64 //save users tree score for summary file
65 utreeScores.push_back(userData[s]);
68 //get scores for random trees
69 for (int j = 0; j < iters; j++) {
71 for (int r=0; r<numGroups; r++) {
72 for (int l = r+1; l < numGroups; l++) {
76 //create a random tree with same topology as T[i], but different labels
77 randT->assembleRandomUnifracTree(globaldata->Groups[r], globaldata->Groups[l]);
78 //get wscore of random tree
79 randomData = weighted->getValues(randT, globaldata->Groups[r], globaldata->Groups[l]);
82 rScores[count].push_back(randomData[0]);
92 //removeValidScoresDuplicates();
93 //find the signifigance of the score for summary file
94 for (int f = 0; f < numComp; f++) {
96 sort(rScores[f].begin(), rScores[f].end());
98 //the index of the score higher than yours is returned
99 //so if you have 1000 random trees the index returned is 100
100 //then there are 900 trees with a score greater then you.
101 //giving you a signifigance of 0.900
102 int index = findIndex(userData[f], f); if (index == -1) { cout << "error in UnifracWeightedCommand" << endl; exit(1); } //error code
104 //the signifigance is the number of trees with the users score or higher
105 WScoreSig.push_back((iters-index)/(float)iters);
108 //out << "Tree# " << i << endl;
109 calculateFreqsCumuls();
118 //finish progress bar
124 //clear out users groups
125 globaldata->Groups.clear();
132 catch(exception& e) {
133 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
137 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
141 /***********************************************************/
142 void UnifracWeightedCommand::printWeightedFile() {
146 for(int a = 0; a < numComp; a++) {
147 initFile(groupComb[a]);
149 for (it = validScores.begin(); it != validScores.end(); it++) {
150 data.push_back(it->first); data.push_back(rScoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
159 remove(weightedFileout.c_str());
162 catch(exception& e) {
163 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
167 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173 /***********************************************************/
174 void UnifracWeightedCommand::printWSummaryFile() {
177 outSum << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" << endl;
178 cout << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" << endl;
181 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
185 for (int i = 0; i < T.size(); i++) {
186 for (int j = 0; j < numComp; j++) {
187 if (WScoreSig[count] > (1/(float)iters)) {
188 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << WScoreSig[count] << endl;
189 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << WScoreSig[count] << endl;
191 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
192 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
199 catch(exception& e) {
200 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
209 /***********************************************************/
210 int UnifracWeightedCommand::findIndex(float score, int index) {
212 for (int i = 0; i < rScores[index].size(); i++) {
213 if (rScores[index][i] >= score) { return i; }
215 return rScores[index].size();
217 catch(exception& e) {
218 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
222 cout << "An unknown error has occurred in the UnifracWeightedCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227 /***********************************************************/
228 void UnifracWeightedCommand::setGroups() {
230 //if the user has not entered specific groups to analyze then do them all
231 if (globaldata->Groups.size() == 0) {
232 numGroups = tmap->getNumGroups();
233 for (int i=0; i < numGroups; i++) {
234 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
237 if (globaldata->getGroups() != "all") {
238 //check that groups are valid
239 for (int i = 0; i < globaldata->Groups.size(); i++) {
240 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
241 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
242 // erase the invalid group from globaldata->Groups
243 globaldata->Groups.erase (globaldata->Groups.begin()+i);
247 //if the user only entered invalid groups
248 if (globaldata->Groups.size() == 0) {
249 numGroups = tmap->getNumGroups();
250 for (int i=0; i < numGroups; i++) {
251 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
253 cout << "When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile." << endl;
254 }else if (globaldata->Groups.size() == 1) {
255 cout << "When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile." << endl;
256 numGroups = tmap->getNumGroups();
257 globaldata->Groups.clear();
258 for (int i=0; i < numGroups; i++) {
259 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
261 }else { numGroups = globaldata->Groups.size(); }
262 }else { //users wants all groups
263 numGroups = tmap->getNumGroups();
264 globaldata->Groups.clear();
265 globaldata->setGroups("");
266 for (int i=0; i < numGroups; i++) {
267 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
272 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
274 for (int i=0; i<numGroups; i++) {
276 for (int l = i+1; l < numGroups; l++) {
277 //set group comparison labels
278 groupComb.push_back(globaldata->Groups[i] + "-" + globaldata->Groups[l]);
282 catch(exception& e) {
283 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 cout << "An unknown error has occurred in the UnifracWeightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
292 /***********************************************************/
294 void UnifracWeightedCommand::calculateFreqsCumuls() {
296 //clear out old tree values
298 rScoreFreq.resize(numComp);
300 rCumul.resize(numComp);
303 //calculate frequency
304 for (int f = 0; f < numComp; f++) {
305 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...
306 validScores[rScores[f][i]] = rScores[f][i];
307 it = rScoreFreq[f].find(rScores[f][i]);
308 if (it != rScoreFreq[f].end()) {
309 rScoreFreq[f][rScores[f][i]]++;
311 rScoreFreq[f][rScores[f][i]] = 1;
317 for(int a = 0; a < numComp; a++) {
318 float rcumul = 1.0000;
319 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
320 for (it = validScores.begin(); it != validScores.end(); it++) {
321 //make rscoreFreq map and rCumul
322 it2 = rScoreFreq[a].find(it->first);
323 rCumul[a][it->first] = rcumul;
324 //get percentage of random trees with that info
325 if (it2 != rScoreFreq[a].end()) { rScoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
326 else { rScoreFreq[a][it->first] = 0.0000; } //no random trees with that score
331 catch(exception& e) {
332 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
336 cout << "An unknown error has occurred in the UnifracWeightedCommand class function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
342 /*****************************************************************/
344 void UnifracWeightedCommand::initFile(string label){
347 openOutputFile(weightedFileout, out);
348 openInputFile(weightedFile, inFile);
351 getline(inFile, inputBuffer);
353 out << inputBuffer << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
355 openOutputFile(weightedFileout, out);
356 out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
359 out.setf(ios::fixed, ios::floatfield);
360 out.setf(ios::showpoint);
362 catch(exception& e) {
363 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
367 cout << "An unknown error has occurred in the UnifracWeightedCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
372 /***********************************************************************/
374 void UnifracWeightedCommand::output(vector<double> data){
378 getline(inFile, inputBuffer);
380 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
383 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
388 catch(exception& e) {
389 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
393 cout << "An unknown error has occurred in the UnifracWeightedCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
398 /***********************************************************************/
400 void UnifracWeightedCommand::resetFile(){
411 remove(weightedFile.c_str());
412 rename(weightedFileout.c_str(), weightedFile.c_str());
414 catch(exception& e) {
415 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
419 cout << "An unknown error has occurred in the UnifracWeightedCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";