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 sumFile = globaldata->getTreeFile() + ".uwsummary";
20 openOutputFile(sumFile, outSum);
22 util = new SharedUtil();
23 util->setGroups(globaldata->Groups, tmap->namesOfGroups, allGroups, numGroups, "unweighted"); //sets the groups the user wants to analyze
24 util->getCombos(groupComb, globaldata->Groups, numComp);
25 globaldata->setGroups("");
27 if (numGroups == 1) { numComp++; groupComb.push_back(allGroups); }
29 convert(globaldata->getIters(), iters); //how many random trees to generate
30 unweighted = new Unweighted(tmap);
34 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
38 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
42 /***********************************************************/
43 int UnifracUnweightedCommand::execute() {
46 userData.resize(numComp,0); //data[0] = unweightedscore
47 randomData.resize(numComp,0); //data[0] = unweightedscore
48 //create new tree with same num nodes and leaves as users
50 outSum << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" <<'\t' << "UWSig" << endl;
51 cout << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" << '\t' << "UWSig" << endl;
53 //get pscores for users trees
54 for (int i = 0; i < T.size(); i++) {
57 output = new ColumnFile(globaldata->getTreeFile() + toString(i+1) + ".unweighted");
59 //get unweighted for users tree
60 rscoreFreq.resize(numComp);
61 rCumul.resize(numComp);
62 utreeScores.resize(numComp);
63 UWScoreSig.resize(numComp);
65 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
67 //output scores for each combination
68 for(int k = 0; k < numComp; k++) {
70 utreeScores[k].push_back(userData[k]);
74 //get unweighted scores for random trees
75 for (int j = 0; j < iters; j++) {
76 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
77 randomData = unweighted->getValues(T[i], "", "");
79 for(int k = 0; k < numComp; k++) {
80 //add trees unweighted score to map of scores
81 it2 = rscoreFreq[k].find(randomData[k]);
82 if (it2 != rscoreFreq[k].end()) {//already have that score
83 rscoreFreq[k][randomData[k]]++;
84 }else{//first time we have seen this score
85 rscoreFreq[k][randomData[k]] = 1;
88 //add randoms score to validscores
89 validScores[randomData[k]] = randomData[k];
94 for(int a = 0; a < numComp; a++) {
95 float rcumul = 1.0000;
96 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
97 for (it = validScores.begin(); it != validScores.end(); it++) {
98 //make rscoreFreq map and rCumul
99 it2 = rscoreFreq[a].find(it->first);
100 rCumul[a][it->first] = rcumul;
101 //get percentage of random trees with that info
102 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
103 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
105 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
110 printUnweightedFile();
111 printUWSummaryFile(i);
121 //reset groups parameter
122 globaldata->Groups.clear();
128 catch(exception& e) {
129 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
133 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
137 /***********************************************************/
138 void UnifracUnweightedCommand::printUnweightedFile() {
142 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
144 for(int a = 0; a < numComp; a++) {
145 output->initFile(groupComb[a], tags);
147 for (it = validScores.begin(); it != validScores.end(); it++) {
148 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
149 output->output(data);
155 catch(exception& e) {
156 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
160 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165 /***********************************************************/
166 void UnifracUnweightedCommand::printUWSummaryFile(int i) {
170 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
174 for(int a = 0; a < numComp; a++) {
175 outSum << i+1 << '\t';
178 if (UWScoreSig[a][0] > (1/(float)iters)) {
179 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
180 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
182 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
183 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
188 catch(exception& e) {
189 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
198 /***********************************************************/