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 //get weighted for users tree
41 userData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
42 randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
44 //create new tree with same num nodes and leaves as users
47 //get weighted scores for users trees
48 for (int i = 0; i < T.size(); i++) {
50 rScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
51 uScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
52 weightedFile = globaldata->getTreeFile() + toString(i+1) + ".weighted";
53 weightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".weighted";
55 userData = weighted->getValues(T[i]); //userData[0] = weightedscore
58 for (int s=0; s<numComp; s++) {
59 //add users score to vector of user scores
60 uScores[s].push_back(userData[s]);
62 //save users tree score for summary file
63 utreeScores.push_back(userData[s]);
66 //get scores for random trees
67 for (int j = 0; j < iters; j++) {
69 for (int r=0; r<numGroups; r++) {
70 for (int l = r+1; l < numGroups; l++) {
74 //create a random tree with same topology as T[i], but different labels
75 randT->assembleRandomUnifracTree(globaldata->Groups[r], globaldata->Groups[l]);
76 //get wscore of random tree
77 randomData = weighted->getValues(randT, globaldata->Groups[r], globaldata->Groups[l]);
80 rScores[count].push_back(randomData[0]);
86 //removeValidScoresDuplicates();
87 //find the signifigance of the score for summary file
88 for (int f = 0; f < numComp; f++) {
90 sort(rScores[f].begin(), rScores[f].end());
92 //the index of the score higher than yours is returned
93 //so if you have 1000 random trees the index returned is 100
94 //then there are 900 trees with a score greater then you.
95 //giving you a signifigance of 0.900
96 int index = findIndex(userData[f], f); if (index == -1) { cout << "error in UnifracWeightedCommand" << endl; exit(1); } //error code
98 //the signifigance is the number of trees with the users score or higher
99 WScoreSig.push_back((iters-index)/(float)iters);
102 //out << "Tree# " << i << endl;
103 calculateFreqsCumuls();
114 //clear out users groups
115 globaldata->Groups.clear();
122 catch(exception& e) {
123 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
127 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
131 /***********************************************************/
132 void UnifracWeightedCommand::printWeightedFile() {
136 for(int a = 0; a < numComp; a++) {
137 initFile(groupComb[a]);
139 for (it = validScores.begin(); it != validScores.end(); it++) {
140 data.push_back(it->first); data.push_back(rScoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
149 remove(weightedFileout.c_str());
152 catch(exception& e) {
153 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
157 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
163 /***********************************************************/
164 void UnifracWeightedCommand::printWSummaryFile() {
167 outSum << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" << endl;
168 cout << "Tree#" << '\t' << "Groups" << '\t' << "WScore" << '\t' << "WSig" << endl;
171 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
175 for (int i = 0; i < T.size(); i++) {
176 for (int j = 0; j < numComp; j++) {
177 if (WScoreSig[count] > (1/(float)iters)) {
178 outSum << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << WScoreSig[count] << endl;
179 cout << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << WScoreSig[count] << endl;
181 outSum << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
182 cout << setprecision(6) << i+1 << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
189 catch(exception& e) {
190 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
194 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199 /***********************************************************/
200 int UnifracWeightedCommand::findIndex(float score, int index) {
202 for (int i = 0; i < rScores[index].size(); i++) {
203 if (rScores[index][i] >= score) { return i; }
205 return rScores[index].size();
207 catch(exception& e) {
208 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
212 cout << "An unknown error has occurred in the UnifracWeightedCommand class function findIndex. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
217 /***********************************************************/
218 void UnifracWeightedCommand::setGroups() {
220 //if the user has not entered specific groups to analyze then do them all
221 if (globaldata->Groups.size() == 0) {
222 numGroups = tmap->getNumGroups();
223 for (int i=0; i < numGroups; i++) {
224 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
227 if (globaldata->getGroups() != "all") {
228 //check that groups are valid
229 for (int i = 0; i < globaldata->Groups.size(); i++) {
230 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
231 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
232 // erase the invalid group from globaldata->Groups
233 globaldata->Groups.erase (globaldata->Groups.begin()+i);
237 //if the user only entered invalid groups
238 if (globaldata->Groups.size() == 0) {
239 numGroups = tmap->getNumGroups();
240 for (int i=0; i < numGroups; i++) {
241 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
243 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;
244 }else if (globaldata->Groups.size() == 1) {
245 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;
246 numGroups = tmap->getNumGroups();
247 globaldata->Groups.clear();
248 for (int i=0; i < numGroups; i++) {
249 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
251 }else { numGroups = globaldata->Groups.size(); }
252 }else { //users wants all groups
253 numGroups = tmap->getNumGroups();
254 globaldata->Groups.clear();
255 globaldata->setGroups("");
256 for (int i=0; i < numGroups; i++) {
257 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
262 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
264 for (int i=0; i<numGroups; i++) {
266 for (int l = i+1; l < numGroups; l++) {
267 //set group comparison labels
268 groupComb.push_back(globaldata->Groups[i] + "-" + globaldata->Groups[l]);
272 catch(exception& e) {
273 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
277 cout << "An unknown error has occurred in the UnifracWeightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282 /***********************************************************/
284 void UnifracWeightedCommand::calculateFreqsCumuls() {
286 //clear out old tree values
288 rScoreFreq.resize(numComp);
290 rCumul.resize(numComp);
293 //calculate frequency
294 for (int f = 0; f < numComp; f++) {
295 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...
296 validScores[rScores[f][i]] = rScores[f][i];
297 it = rScoreFreq[f].find(rScores[f][i]);
298 if (it != rScoreFreq[f].end()) {
299 rScoreFreq[f][rScores[f][i]]++;
301 rScoreFreq[f][rScores[f][i]] = 1;
307 for(int a = 0; a < numComp; a++) {
308 float rcumul = 1.0000;
309 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
310 for (it = validScores.begin(); it != validScores.end(); it++) {
311 //make rscoreFreq map and rCumul
312 it2 = rScoreFreq[a].find(it->first);
313 rCumul[a][it->first] = rcumul;
314 //get percentage of random trees with that info
315 if (it2 != rScoreFreq[a].end()) { rScoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
316 else { rScoreFreq[a][it->first] = 0.0000; } //no random trees with that score
321 catch(exception& e) {
322 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
326 cout << "An unknown error has occurred in the UnifracWeightedCommand class function calculateFreqsCums. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
332 /*****************************************************************/
334 void UnifracWeightedCommand::initFile(string label){
337 openOutputFile(weightedFileout, out);
338 openInputFile(weightedFile, inFile);
341 getline(inFile, inputBuffer);
343 out << inputBuffer << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
345 openOutputFile(weightedFileout, out);
346 out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
349 out.setf(ios::fixed, ios::floatfield);
350 out.setf(ios::showpoint);
352 catch(exception& e) {
353 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
357 cout << "An unknown error has occurred in the UnifracWeightedCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
362 /***********************************************************************/
364 void UnifracWeightedCommand::output(vector<double> data){
368 getline(inFile, inputBuffer);
370 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
373 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
378 catch(exception& e) {
379 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
383 cout << "An unknown error has occurred in the UnifracWeightedCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
388 /***********************************************************************/
390 void UnifracWeightedCommand::resetFile(){
401 remove(weightedFile.c_str());
402 rename(weightedFileout.c_str(), weightedFile.c_str());
404 catch(exception& e) {
405 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
409 cout << "An unknown error has occurred in the UnifracWeightedCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";