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);
23 distFile = globaldata->getTreeFile() + ".wdistrib";
24 openOutputFile(distFile, outDist);
26 //if the user has not entered specific groups to analyze then do them all
27 if (globaldata->Groups.size() == 0) {
28 numGroups = tmap->getNumGroups();
30 //check that groups are valid
31 for (int i = 0; i < globaldata->Groups.size(); i++) {
32 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
33 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
34 // erase the invalid group from globaldata->Groups
35 globaldata->Groups.erase (globaldata->Groups.begin()+i);
39 //if the user only entered invalid groups
40 if (globaldata->Groups.size() == 0) {
41 numGroups = tmap->getNumGroups();
42 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;
43 }else if (globaldata->Groups.size() == 1) {
44 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;
45 numGroups = tmap->getNumGroups();
46 globaldata->Groups.clear();
47 }else { numGroups = globaldata->Groups.size(); }
50 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
53 for (int i=1; i<numGroups; i++) {
55 for (int l = n; l < numGroups; l++) {
56 //set group comparison labels
57 if (globaldata->Groups.size() != 0) {
58 groupComb.push_back(globaldata->Groups[i-1]+globaldata->Groups[l]);
60 groupComb.push_back(tmap->namesOfGroups[i-1]+tmap->namesOfGroups[l]);
66 convert(globaldata->getIters(), iters); //how many random trees to generate
67 weighted = new Weighted(tmap);
71 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
75 cout << "An unknown error has occurred in the UnifracWeightedCommand class function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79 /***********************************************************/
80 int UnifracWeightedCommand::execute() {
83 //get weighted for users tree
84 userData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
85 randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
86 uscoreFreq.resize(numComp);
87 validScores.resize(numComp);
88 totalrscoreFreq.resize(numComp);
89 uCumul.resize(numComp);
92 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
93 outDist << "Tree#" << '\t' << "Iter" << '\t' << "Groups"<< '\t' << "WScore" << endl;
96 //create new tree with same num nodes and leaves as users
99 //get pscores for users trees
100 for (int i = 0; i < T.size(); i++) {
101 rscoreFreq.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
102 rCumul.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...
104 cout << "Processing tree " << i+1 << endl;
105 userData = weighted->getValues(T[i]); //userData[0] = weightedscore
108 for (int s=0; s<numComp; s++) {
110 it = uscoreFreq[s].find(userData[s]);
111 if (it == uscoreFreq[s].end()) {//new score
112 uscoreFreq[s][userData[s]] = 1;
113 }else{ uscoreFreq[s][userData[s]]++; }
115 //add user score to valid scores
116 validScores[s][userData[s]] = userData[s];
118 //save users tree score for summary file
119 utreeScores.push_back(userData[s]);
123 randT->getCopy(T[i]);
125 //get pscores for random trees
126 for (int j = 0; j < iters; j++) {
127 //create a random tree with same topology as T[i], but different labels
128 randT->assembleRandomUnifracTree();
129 //get pscore of random tree
130 randomData = weighted->getValues(randT);
133 for (int p=0; p<numComp; p++) {
134 //add trees weighted score random score freq
135 it2 = rscoreFreq[p].find(randomData[p]);
136 if (it2 != rscoreFreq[p].end()) {//already have that score
137 rscoreFreq[p][randomData[p]]++;
138 }else{//first time we have seen this score
139 rscoreFreq[p][randomData[p]] = 1;
142 //add random score to valid scores
143 validScores[p][randomData[p]] = randomData[p];
145 //output info to uwdistrib file
146 outDist << i+1 << '\t' << '\t'<< j+1 << '\t' << '\t' << groupComb[p] << '\t'<< randomData[p] << endl;
150 saveRandomScores(); //save all random scores for weighted file
152 //find the signifigance of the score for summary file
153 for (int t = 0; t < numComp; t++) {
154 float rcumul = 0.0000;
155 for (it = validScores[t].begin(); it != validScores[t].end(); it++) {
156 //make rscoreFreq map and rCumul
157 it2 = rscoreFreq[t].find(it->first);
158 //get percentage of random trees with that info
159 if (it2 != rscoreFreq[t].end()) { rscoreFreq[t][it->first] /= iters; rcumul+= it2->second; }
160 else { rscoreFreq[t][it->first] = 0.0000; } //no random trees with that score
161 rCumul[t][it->first] = rcumul;
165 //save the signifigance of the users score for printing later
166 for (int f = 0; f < numComp; f++) {
167 WScoreSig.push_back(rCumul[f][userData[f]]);
176 rCumul.resize(numComp);
177 for (int b = 0; b < numComp; b++) {
178 float ucumul = 0.0000;
179 float rcumul = 0.0000;
180 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
181 for (it = validScores[b].begin(); it != validScores[b].end(); it++) {
182 it2 = uscoreFreq[b].find(it->first);
183 //user data has that score
184 if (it2 != uscoreFreq[b].end()) { uscoreFreq[b][it->first] /= T.size(); ucumul+= it2->second; }
185 else { uscoreFreq[b][it->first] = 0.0000; } //no user trees with that score
187 uCumul[b][it->first] = ucumul;
189 //make rscoreFreq map and rCumul
190 it2 = totalrscoreFreq[b].find(it->first);
191 //get percentage of random trees with that info
192 if (it2 != totalrscoreFreq[b].end()) { totalrscoreFreq[b][it->first] /= (iters * T.size()); rcumul+= it2->second; }
193 else { totalrscoreFreq[b][it->first] = 0.0000; } //no random trees with that score
194 rCumul[b][it->first] = rcumul;
201 //reset randomTree parameter to 0
202 globaldata->setRandomTree("0");
203 //clear out users groups
204 globaldata->Groups.clear();
211 catch(exception& e) {
212 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
216 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
220 /***********************************************************/
221 void UnifracWeightedCommand::printWeightedFile() {
225 out << "Group" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
228 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
231 for (int e = 0; e < numComp; e++) {
232 //print each line in that group
233 for (it = validScores[e].begin(); it != validScores[e].end(); it++) {
234 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;
241 catch(exception& e) {
242 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
246 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
252 /***********************************************************/
253 void UnifracWeightedCommand::printWSummaryFile() {
256 outSum << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" << endl;
259 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
263 for (int i = 0; i < T.size(); i++) {
264 for (int j = 0; j < numComp; j++) {
265 outSum << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl;
271 catch(exception& e) {
272 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
276 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
281 /***********************************************************/
282 void UnifracWeightedCommand::saveRandomScores() {
284 for (int e = 0; e < numComp; e++) {
285 //update total map with new random scores
286 for (it = rscoreFreq[e].begin(); it != rscoreFreq[e].end(); it++) {
287 //does this score already exist in the total map
288 it2 = totalrscoreFreq[e].find(it->first);
289 //if yes then add them
290 if (it2 != totalrscoreFreq[e].end()) {
291 totalrscoreFreq[e][it->first] += rscoreFreq[e][it->first];
292 }else{ //its a new score
293 totalrscoreFreq[e][it->first] = rscoreFreq[e][it->first];
298 catch(exception& e) {
299 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function saveRandomScores. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
303 cout << "An unknown error has occurred in the UnifracWeightedCommand class function saveRandomScores. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
308 /***********************************************************/