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);
22 out << "Group" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
24 sumFile = globaldata->getTreeFile() + ".wsummary";
25 openOutputFile(sumFile, outSum);
27 setGroups(); //sets the groups the user wants to analyze
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 //get weighted for users tree
46 userData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
47 randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
49 //create new tree with same num nodes and leaves as users
52 //get pscores for users trees
53 for (int i = 0; i < T.size(); i++) {
54 rScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
55 uScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
56 validScores.resize(numComp);
58 cout << "Processing tree " << i+1 << endl;
59 userData = weighted->getValues(T[i]); //userData[0] = weightedscore
62 for (int s=0; s<numComp; s++) {
63 //add users score to vector of user scores
64 uScores[s].push_back(userData[s]);
66 //add users score to vector of valid scores
67 validScores[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 if (globaldata->Groups.size() != 0) {
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]);
87 //create a random tree with same topology as T[i], but different labels
88 randT->assembleRandomUnifracTree(tmap->namesOfGroups[r], tmap->namesOfGroups[l]);
89 //get wscore of random tree
90 randomData = weighted->getValues(randT, tmap->namesOfGroups[r], tmap->namesOfGroups[l]);
93 rScores[count].push_back(randomData[0]);
94 validScores[count][randomData[0]] = randomData[0];
100 removeValidScoresDuplicates();
101 //find the signifigance of the score for summary file
102 for (int f = 0; f < numComp; f++) {
104 sort(rScores[f].begin(), rScores[f].end());
106 //the index of the score higher than yours is returned
107 //so if you have 1000 random trees the index returned is 100
108 //then there are 900 trees with a score greater then you.
109 //giving you a signifigance of 0.900
110 int index = findIndex(userData[f], f); if (index == -1) { cout << "error in UnifracWeightedCommand" << endl; exit(1); } //error code
112 //the signifigance is the number of trees with the users score or higher
113 WScoreSig.push_back((iters-index)/(float)iters);
116 out << "Tree# " << i << endl;
117 //printWeightedFile();
127 //clear out users groups
128 globaldata->Groups.clear();
135 catch(exception& e) {
136 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
140 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
144 /***********************************************************
145 void UnifracWeightedCommand::printWeightedFile() {
149 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
152 for (int e = 0; e < numComp; e++) {
153 //print each line in that group
154 for (i = 0; i < validScores[e].size(); i++) {
155 out << setprecision(6) << groupComb[e] << '\t' << validScores[e][i] << '\t' << '\t' << uscoreFreq[e][it->first] << '\t' << uCumul[e][it->first] << '\t' << rscoreFreq[e][it->first] << '\t' << rCumul[e][it->first] << endl;
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' << '\t' << "WScore" << '\t' << '\t' << "WSig" << endl;
178 cout << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\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 outSum << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl;
188 cout << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl;
194 catch(exception& e) {
195 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204 /***********************************************************/
205 void UnifracWeightedCommand::removeValidScoresDuplicates() {
207 for (int e = 0; e < numComp; e++) {
209 sort(validScores[e].begin(), validScores[e].end());
211 for (int i = 0; i< validScores[e].size()-1; i++) {
212 if (validScores[e][i] == validScores[e][i+1]) { validScores[e].erase(validScores[e].begin()+i); }
216 catch(exception& e) {
217 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function removeValidScoresDuplicates. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
221 cout << "An unknown error has occurred in the UnifracWeightedCommand class function removeValidScoresDuplicates. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
226 /***********************************************************/
227 int UnifracWeightedCommand::findIndex(float score, int index) {
229 for (int i = 0; i < rScores[index].size(); i++) {
230 if (rScores[index][i] >= score) { return i; }
232 return rScores[index].size();
234 catch(exception& e) {
235 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239 cout << "An unknown error has occurred in the UnifracWeightedCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 /***********************************************************/
245 void UnifracWeightedCommand::setGroups() {
247 //if the user has not entered specific groups to analyze then do them all
248 if (globaldata->Groups.size() == 0) {
249 numGroups = tmap->getNumGroups();
251 if (globaldata->getGroups() != "all") {
252 //check that groups are valid
253 for (int i = 0; i < globaldata->Groups.size(); i++) {
254 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
255 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
256 // erase the invalid group from globaldata->Groups
257 globaldata->Groups.erase (globaldata->Groups.begin()+i);
261 //if the user only entered invalid groups
262 if (globaldata->Groups.size() == 0) {
263 numGroups = tmap->getNumGroups();
264 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;
265 }else if (globaldata->Groups.size() == 1) {
266 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;
267 numGroups = tmap->getNumGroups();
268 globaldata->Groups.clear();
269 }else { numGroups = globaldata->Groups.size(); }
270 }else { //users wants all groups
271 numGroups = tmap->getNumGroups();
272 globaldata->Groups.clear();
273 globaldata->setGroups("");
277 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
280 for (int i=1; i<numGroups; i++) {
282 for (int l = n; l < numGroups; l++) {
283 //set group comparison labels
284 if (globaldata->Groups.size() != 0) {
285 groupComb.push_back(globaldata->Groups[i-1]+globaldata->Groups[l]);
287 groupComb.push_back(tmap->namesOfGroups[i-1]+tmap->namesOfGroups[l]);
293 catch(exception& e) {
294 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
298 cout << "An unknown error has occurred in the UnifracWeightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";