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++) {
88 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
89 randomData = unweighted->getValues(randT, "", "");
91 for(int k = 0; k < numComp; k++) {
92 cout << "iter " << j << " comp " << k << " = " << randomData[k] << endl;
93 //add trees unweighted score to map of scores
94 it2 = rscoreFreq[k].find(randomData[k]);
95 if (it2 != rscoreFreq[k].end()) {//already have that score
96 rscoreFreq[k][randomData[k]]++;
97 }else{//first time we have seen this score
98 rscoreFreq[k][randomData[k]] = 1;
101 //add randoms score to validscores
102 validScores[k][randomData[k]] = randomData[k];
106 for(int a = 0; a < numComp; a++) {
107 float ucumul = 1.0000;
108 float rcumul = 1.0000;
109 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
110 for (it = validScores[a].begin(); it != validScores[a].end(); it++) {
111 it2 = uscoreFreq[a].find(it->first);
113 uCumul[a][it->first] = ucumul;
114 //user data has that score
115 if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul-= it2->second; }
116 else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
118 //make rscoreFreq map and rCumul
119 it2 = rscoreFreq[a].find(it->first);
120 rCumul[a][it->first] = rcumul;
121 //get percentage of random trees with that info
122 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
123 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
125 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
128 printUnweightedFile();
129 printUWSummaryFile();
139 //reset groups parameter
140 globaldata->Groups.clear(); globaldata->setGroups("");
147 catch(exception& e) {
148 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
152 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156 /***********************************************************/
157 void UnifracUnweightedCommand::printUnweightedFile() {
160 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
162 for(int a = 0; a < numComp; a++) {
164 for (it = validScores[a].begin(); it != validScores[a].end(); it++) {
165 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;
171 catch(exception& e) {
172 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
176 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
181 /***********************************************************/
182 void UnifracUnweightedCommand::printUWSummaryFile() {
186 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
189 for (int i = 0; i< T.size(); i++) {
190 for(int a = 0; a < numComp; a++) {
191 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << utreeScores[a][i] << '\t' << UWScoreSig[a][i] << endl;
192 cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << utreeScores[a][i] << '\t' << UWScoreSig[a][i] << endl;
198 catch(exception& e) {
199 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207 /***********************************************************/
209 void UnifracUnweightedCommand::setGroups() {
211 string allGroups = "";
213 //if the user has not entered specific groups to analyze then do them all
214 if (globaldata->Groups.size() != 0) {
215 if (globaldata->Groups[0] != "all") {
216 //check that groups are valid
217 for (int i = 0; i < globaldata->Groups.size(); i++) {
218 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
219 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
220 // erase the invalid group from globaldata->Groups
221 globaldata->Groups.erase(globaldata->Groups.begin()+i);
225 //if the user only entered invalid groups
226 if (globaldata->Groups.size() == 0) {
227 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;
228 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
229 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
231 allGroups += tmap->namesOfGroups[i];
234 for (int i = 0; i < globaldata->Groups.size(); i++) {
235 allGroups += globaldata->Groups[i];
239 }else{//user has enter "all" and wants the default groups
240 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
241 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
243 allGroups += tmap->namesOfGroups[i];
245 globaldata->setGroups("");
248 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
249 allGroups += tmap->namesOfGroups[i];
254 //calculate number of comparsions
256 for (int r=0; r<numGroups; r++) {
257 for (int l = r+1; l < numGroups; l++) {
258 groupComb.push_back(globaldata->Groups[r]+globaldata->Groups[l]);
265 groupComb.push_back(allGroups);
269 catch(exception& e) {
270 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
279 /*****************************************************************/