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("");
29 groupComb.push_back(allGroups);
34 convert(globaldata->getIters(), iters); //how many random trees to generate
35 unweighted = new Unweighted(tmap);
39 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
43 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47 /***********************************************************/
48 int UnifracUnweightedCommand::execute() {
51 userData.resize(numComp,0); //data[0] = unweightedscore
52 randomData.resize(numComp,0); //data[0] = unweightedscore
53 //create new tree with same num nodes and leaves as users
55 outSum << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" <<'\t' << "UWSig" << endl;
56 cout << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" << '\t' << "UWSig" << endl;
58 //get pscores for users trees
59 for (int i = 0; i < T.size(); i++) {
61 unweightedFile = globaldata->getTreeFile() + toString(i+1) + ".unweighted";
62 unweightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".unweighted";
64 outSum << i+1 << '\t';
67 //get unweighted for users tree
68 rscoreFreq.resize(numComp);
69 rCumul.resize(numComp);
70 utreeScores.resize(numComp);
71 UWScoreSig.resize(numComp);
73 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
75 //output scores for each combination
76 for(int k = 0; k < numComp; k++) {
78 utreeScores[k].push_back(userData[k]);
82 //get unweighted scores for random trees
83 for (int j = 0; j < iters; j++) {
84 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
85 randomData = unweighted->getValues(T[i], "", "");
87 for(int k = 0; k < numComp; k++) {
88 //add trees unweighted score to map of scores
89 it2 = rscoreFreq[k].find(randomData[k]);
90 if (it2 != rscoreFreq[k].end()) {//already have that score
91 rscoreFreq[k][randomData[k]]++;
92 }else{//first time we have seen this score
93 rscoreFreq[k][randomData[k]] = 1;
96 //add randoms score to validscores
97 validScores[randomData[k]] = randomData[k];
102 for(int a = 0; a < numComp; a++) {
103 float rcumul = 1.0000;
104 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
105 for (it = validScores.begin(); it != validScores.end(); it++) {
106 //make rscoreFreq map and rCumul
107 it2 = rscoreFreq[a].find(it->first);
108 rCumul[a][it->first] = rcumul;
109 //get percentage of random trees with that info
110 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
111 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
113 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
118 printUnweightedFile();
119 printUWSummaryFile();
127 //reset groups parameter
128 globaldata->Groups.clear();
134 catch(exception& e) {
135 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
139 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
143 /***********************************************************/
144 void UnifracUnweightedCommand::printUnweightedFile() {
148 for(int a = 0; a < numComp; a++) {
149 initFile(groupComb[a]);
151 for (it = validScores.begin(); it != validScores.end(); it++) {
152 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
161 remove(unweightedFileout.c_str());
164 catch(exception& e) {
165 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174 /***********************************************************/
175 void UnifracUnweightedCommand::printUWSummaryFile() {
179 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
183 for(int a = 0; a < numComp; a++) {
184 if (UWScoreSig[a][0] > (1/(float)iters)) {
185 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
186 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
188 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
189 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
194 catch(exception& e) {
195 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203 /*****************************************************************/
205 void UnifracUnweightedCommand::initFile(string label){
208 openOutputFile(unweightedFileout, out);
209 openInputFile(unweightedFile, inFile);
212 getline(inFile, inputBuffer);
214 out << inputBuffer << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
216 openOutputFile(unweightedFileout, out);
217 out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
220 out.setf(ios::fixed, ios::floatfield);
221 out.setf(ios::showpoint);
223 catch(exception& e) {
224 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
228 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
233 /***********************************************************************/
235 void UnifracUnweightedCommand::output(vector<double> data){
239 getline(inFile, inputBuffer);
240 // out << inputBuffer << setprecision(6) << '\t' << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
242 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
245 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
249 catch(exception& e) {
250 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
259 /***********************************************************************/
261 void UnifracUnweightedCommand::resetFile(){
272 remove(unweightedFile.c_str());
273 rename(unweightedFileout.c_str(), unweightedFile.c_str());
275 catch(exception& e) {
276 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
280 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";