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);
21 sumFile = globaldata->getTreeFile() + ".wsummary";
22 openOutputFile(sumFile, outSum);
24 setGroups(); //sets the groups the user wants to analyze
25 convert(globaldata->getIters(), iters); //how many random trees to generate
26 weighted = new Weighted(tmap);
30 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
34 cout << "An unknown error has occurred in the UnifracWeightedCommand class function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
38 /***********************************************************/
39 int UnifracWeightedCommand::execute() {
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...
45 uscoreFreq.resize(numComp);
46 validScores.resize(numComp);
47 totalrscoreFreq.resize(numComp);
48 uCumul.resize(numComp);
50 //create new tree with same num nodes and leaves as users
53 //get pscores for users trees
54 for (int i = 0; i < T.size(); i++) {
55 rscoreFreq.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
56 rCumul.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
58 cout << "Processing tree " << i+1 << endl;
59 userData = weighted->getValues(T[i]); //userData[0] = weightedscore
62 for (int s=0; s<numComp; s++) {
64 it = uscoreFreq[s].find(userData[s]);
65 if (it == uscoreFreq[s].end()) {//new score
66 uscoreFreq[s][userData[s]] = 1;
67 }else{ uscoreFreq[s][userData[s]]++; }
69 //add user score to valid scores
70 validScores[s][userData[s]] = userData[s];
72 //save users tree score for summary file
73 utreeScores.push_back(userData[s]);
79 //get scores for random trees
80 for (int j = 0; j < iters; j++) {
81 //create a random tree with same topology as T[i], but different labels
82 randT->assembleRandomUnifracTree();
83 //get pscore of random tree
84 randomData = weighted->getValues(randT);
87 for (int p=0; p<numComp; p++) {
88 //add trees weighted score random score freq
89 it2 = rscoreFreq[p].find(randomData[p]);
90 if (it2 != rscoreFreq[p].end()) {//already have that score
91 rscoreFreq[p][randomData[p]]++;
92 }else{//first time we have seen this score
93 rscoreFreq[p][randomData[p]] = 1;
96 //add random score to valid scores
97 validScores[p][randomData[p]] = randomData[p];
101 saveRandomScores(); //save all random scores for weighted file
103 //find the signifigance of the score for summary file
104 for (int t = 0; t < numComp; t++) {
105 float rcumul = 1.0000;
106 for (it = validScores[t].begin(); it != validScores[t].end(); it++) {
107 //make rscoreFreq map and rCumul
108 it2 = rscoreFreq[t].find(it->first);
109 rCumul[t][it->first] = rcumul;
110 //get percentage of random trees with that info
111 if (it2 != rscoreFreq[t].end()) { rscoreFreq[t][it->first] /= iters; rcumul-= it2->second; }
112 else { rscoreFreq[t][it->first] = 0.0000; } //no random trees with that score
116 //save the signifigance of the users score for printing later
117 for (int f = 0; f < numComp; f++) {
118 WScoreSig.push_back(rCumul[f][userData[f]]);
127 rCumul.resize(numComp);
128 for (int b = 0; b < numComp; b++) {
129 float ucumul = 1.0000;
130 float rcumul = 1.0000;
131 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
132 for (it = validScores[b].begin(); it != validScores[b].end(); it++) {
133 it2 = uscoreFreq[b].find(it->first);
135 uCumul[b][it->first] = ucumul;
136 //user data has that score
137 if (it2 != uscoreFreq[b].end()) { uscoreFreq[b][it->first] /= T.size(); ucumul-= it2->second; }
138 else { uscoreFreq[b][it->first] = 0.0000; } //no user trees with that score
140 //make rscoreFreq map and rCumul
141 it2 = totalrscoreFreq[b].find(it->first);
142 rCumul[b][it->first] = rcumul;
143 //get percentage of random trees with that info
144 if (it2 != totalrscoreFreq[b].end()) { totalrscoreFreq[b][it->first] /= (iters * T.size()); rcumul-= it2->second; }
145 else { totalrscoreFreq[b][it->first] = 0.0000; } //no random trees with that score
152 //clear out users groups
153 globaldata->Groups.clear();
160 catch(exception& e) {
161 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169 /***********************************************************/
170 void UnifracWeightedCommand::printWeightedFile() {
174 out << "Group" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
177 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
180 for (int e = 0; e < numComp; e++) {
181 //print each line in that group
182 for (it = validScores[e].begin(); it != validScores[e].end(); it++) {
183 out << setprecision(6) << groupComb[e] << '\t' << it->first << '\t' << '\t' << uscoreFreq[e][it->first] << '\t' << uCumul[e][it->first] << '\t' << totalrscoreFreq[e][it->first] << '\t' << rCumul[e][it->first] << endl;
190 catch(exception& e) {
191 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
195 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
201 /***********************************************************/
202 void UnifracWeightedCommand::printWSummaryFile() {
205 outSum << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" << endl;
206 cout << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" << endl;
209 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
213 for (int i = 0; i < T.size(); i++) {
214 for (int j = 0; j < numComp; j++) {
215 outSum << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl;
216 cout << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl;
222 catch(exception& e) {
223 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232 /***********************************************************/
233 void UnifracWeightedCommand::saveRandomScores() {
235 for (int e = 0; e < numComp; e++) {
236 //update total map with new random scores
237 for (it = rscoreFreq[e].begin(); it != rscoreFreq[e].end(); it++) {
238 //does this score already exist in the total map
239 it2 = totalrscoreFreq[e].find(it->first);
240 //if yes then add them
241 if (it2 != totalrscoreFreq[e].end()) {
242 totalrscoreFreq[e][it->first] += rscoreFreq[e][it->first];
243 }else{ //its a new score
244 totalrscoreFreq[e][it->first] = rscoreFreq[e][it->first];
249 catch(exception& e) {
250 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function saveRandomScores. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254 cout << "An unknown error has occurred in the UnifracWeightedCommand class function saveRandomScores. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
259 /***********************************************************/
260 void UnifracWeightedCommand::setGroups() {
262 //if the user has not entered specific groups to analyze then do them all
263 if (globaldata->Groups.size() == 0) {
264 numGroups = tmap->getNumGroups();
266 if (globaldata->getGroups() != "all") {
267 //check that groups are valid
268 for (int i = 0; i < globaldata->Groups.size(); i++) {
269 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
270 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
271 // erase the invalid group from globaldata->Groups
272 globaldata->Groups.erase (globaldata->Groups.begin()+i);
276 //if the user only entered invalid groups
277 if (globaldata->Groups.size() == 0) {
278 numGroups = tmap->getNumGroups();
279 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;
280 }else if (globaldata->Groups.size() == 1) {
281 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;
282 numGroups = tmap->getNumGroups();
283 globaldata->Groups.clear();
284 }else { numGroups = globaldata->Groups.size(); }
285 }else { //users wants all groups
286 numGroups = tmap->getNumGroups();
287 globaldata->Groups.clear();
288 globaldata->setGroups("");
292 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
295 for (int i=1; i<numGroups; i++) {
297 for (int l = n; l < numGroups; l++) {
298 //set group comparison labels
299 if (globaldata->Groups.size() != 0) {
300 groupComb.push_back(globaldata->Groups[i-1]+globaldata->Groups[l]);
302 groupComb.push_back(tmap->namesOfGroups[i-1]+tmap->namesOfGroups[l]);
308 catch(exception& e) {
309 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
313 cout << "An unknown error has occurred in the UnifracWeightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";