2 * unifracunweightedcommand.cpp
5 * Created by Sarah Westcott on 2/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "unifracunweightedcommand.h"
12 /***********************************************************/
13 UnifracUnweightedCommand::UnifracUnweightedCommand() {
15 globaldata = GlobalData::getInstance();
17 T = globaldata->gTree;
18 tmap = globaldata->gTreemap;
19 unweightedFile = globaldata->getTreeFile() + ".unweighted";
20 openOutputFile(unweightedFile, out);
22 out << "Comb" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
24 sumFile = globaldata->getTreeFile() + ".uwsummary";
25 openOutputFile(sumFile, outSum);
27 outSum << "Tree#" << '\t' << "Comb" << '\t' << "UWScore" << '\t' << '\t' << "UWSig" << endl;
29 setGroups(); //sets users groups to analyze
30 convert(globaldata->getIters(), iters); //how many random trees to generate
31 unweighted = new Unweighted(tmap);
35 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
39 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
43 /***********************************************************/
44 int UnifracUnweightedCommand::execute() {
47 userData.resize(numComp,0); //data[0] = unweightedscore
48 randomData.resize(numComp,0); //data[0] = unweightedscore
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 //get unweighted for users tree
55 rscoreFreq.resize(numComp);
56 uscoreFreq.resize(numComp);
57 rCumul.resize(numComp);
58 uCumul.resize(numComp);
59 validScores.resize(numComp);
60 utreeScores.resize(numComp);
61 UWScoreSig.resize(numComp);
63 cout << "Processing tree " << i+1 << endl;
64 outSum << "Tree#" << i+1 << endl;
65 out << "Tree#" << i+1 << endl;
66 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
68 //output scores for each combination
69 for(int k = 0; k < numComp; k++) {
71 it = uscoreFreq[k].find(userData[k]);
72 if (it == uscoreFreq[k].end()) {//new score
73 uscoreFreq[k][userData[k]] = 1;
74 }else{ uscoreFreq[k][userData[k]]++; }
76 //add users score to valid scores
77 validScores[k][userData[k]] = userData[k];
80 utreeScores[k].push_back(userData[k]);
86 //get unweighted scores for random trees
87 for (int j = 0; j < iters; j++) {
89 for (int r=0; r<numGroups; r++) {
90 for (int l = r+1; l < numGroups; l++) {
91 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
92 randomData = unweighted->getValues(randT, "", "");
94 //add trees unweighted score to map of scores
95 it2 = rscoreFreq[count].find(randomData[count]);
96 if (it2 != rscoreFreq[count].end()) {//already have that score
97 rscoreFreq[count][randomData[count]]++;
98 }else{//first time we have seen this score
99 rscoreFreq[count][randomData[count]] = 1;
102 //add randoms score to validscores
103 validScores[count][randomData[count]] = randomData[count];
109 for(int a = 0; a < numComp; a++) {
110 float ucumul = 1.0000;
111 float rcumul = 1.0000;
112 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
113 for (it = validScores[a].begin(); it != validScores[a].end(); it++) {
114 it2 = uscoreFreq[a].find(it->first);
116 uCumul[a][it->first] = ucumul;
117 //user data has that score
118 if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul-= it2->second; }
119 else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
121 //make rscoreFreq map and rCumul
122 it2 = rscoreFreq[a].find(it->first);
123 rCumul[a][it->first] = rcumul;
124 //get percentage of random trees with that info
125 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
126 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
128 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
131 printUnweightedFile();
132 printUWSummaryFile();
142 //reset groups parameter
143 globaldata->Groups.clear(); globaldata->setGroups("");
150 catch(exception& e) {
151 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
155 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
159 /***********************************************************/
160 void UnifracUnweightedCommand::printUnweightedFile() {
163 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
165 for(int a = 0; a < numComp; a++) {
167 for (it = validScores[a].begin(); it != validScores[a].end(); it++) {
168 out << setprecision(6) << groupComb[a] << '\t' << it->first << '\t' << '\t' << uscoreFreq[a][it->first] << '\t' << uCumul[a][it->first] << '\t' << rscoreFreq[a][it->first] << '\t' << rCumul[a][it->first] << endl;
174 catch(exception& e) {
175 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184 /***********************************************************/
185 void UnifracUnweightedCommand::printUWSummaryFile() {
189 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
192 for (int i = 0; i< T.size(); i++) {
193 for(int a = 0; a < numComp; a++) {
194 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << utreeScores[a][i] << '\t' << UWScoreSig[a][i] << endl;
195 cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << utreeScores[a][i] << '\t' << UWScoreSig[a][i] << endl;
201 catch(exception& e) {
202 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
206 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
210 /***********************************************************/
212 void UnifracUnweightedCommand::setGroups() {
214 string allGroups = "";
216 //if the user has not entered specific groups to analyze then do them all
217 if (globaldata->Groups.size() != 0) {
218 if (globaldata->Groups[0] != "all") {
219 //check that groups are valid
220 for (int i = 0; i < globaldata->Groups.size(); i++) {
221 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
222 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
223 // erase the invalid group from globaldata->Groups
224 globaldata->Groups.erase(globaldata->Groups.begin()+i);
228 //if the user only entered invalid groups
229 if (globaldata->Groups.size() == 0) {
230 cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using all the groups in your groupfile." << endl;
231 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
232 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
234 allGroups += tmap->namesOfGroups[i];
237 for (int i = 0; i < globaldata->Groups.size(); i++) {
238 allGroups += tmap->namesOfGroups[i];
242 }else{//user has enter "all" and wants the default groups
243 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
244 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
246 allGroups += tmap->namesOfGroups[i];
248 globaldata->setGroups("");
251 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
252 allGroups += tmap->namesOfGroups[i];
257 //calculate number of comparsions
259 for (int r=0; r<numGroups; r++) {
260 for (int l = r+1; l < numGroups; l++) {
261 groupComb.push_back(globaldata->Groups[r]+globaldata->Groups[l]);
268 groupComb.push_back(allGroups);
272 catch(exception& e) {
273 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
277 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282 /*****************************************************************/