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 setGroups(); //sets users groups to analyze
23 convert(globaldata->getIters(), iters); //how many random trees to generate
24 unweighted = new Unweighted(tmap);
28 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
32 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
36 /***********************************************************/
37 int UnifracUnweightedCommand::execute() {
40 userData.resize(numComp,0); //data[0] = unweightedscore
41 randomData.resize(numComp,0); //data[0] = unweightedscore
42 //create new tree with same num nodes and leaves as users
44 //get pscores for users trees
45 for (int i = 0; i < T.size(); i++) {
46 unweightedFile = globaldata->getTreeFile() + toString(i+1) + ".unweighted";
47 unweightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".unweighted";
49 outSum << "Tree# " << i+1 << endl;
50 outSum << "Comb" << '\t' << "UWScore" << '\t' << '\t' << "UWSig" << endl;
52 //get unweighted for users tree
53 rscoreFreq.resize(numComp);
54 rCumul.resize(numComp);
55 validScores.resize(numComp);
56 utreeScores.resize(numComp);
57 UWScoreSig.resize(numComp);
59 cout << "Processing tree " << i+1 << endl;
60 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
62 //output scores for each combination
63 for(int k = 0; k < numComp; k++) {
64 //add users score to valid scores
65 validScores[k][userData[k]] = userData[k];
68 utreeScores[k].push_back(userData[k]);
71 //get unweighted scores for random trees
72 for (int j = 0; j < iters; j++) {
73 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
74 randomData = unweighted->getValues(T[i], "", "");
76 for(int k = 0; k < numComp; k++) {
77 //cout << "iter " << j << " comp " << k << " = " << randomData[k] << endl;
78 //add trees unweighted score to map of scores
79 it2 = rscoreFreq[k].find(randomData[k]);
80 if (it2 != rscoreFreq[k].end()) {//already have that score
81 rscoreFreq[k][randomData[k]]++;
82 }else{//first time we have seen this score
83 rscoreFreq[k][randomData[k]] = 1;
86 //add randoms score to validscores
87 validScores[k][randomData[k]] = randomData[k];
91 for(int a = 0; a < numComp; a++) {
92 float rcumul = 1.0000;
93 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
94 for (it = validScores[a].begin(); it != validScores[a].end(); it++) {
95 //make rscoreFreq map and rCumul
96 it2 = rscoreFreq[a].find(it->first);
97 rCumul[a][it->first] = rcumul;
98 //get percentage of random trees with that info
99 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
100 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
102 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
105 printUnweightedFile();
106 printUWSummaryFile();
114 //reset groups parameter
115 globaldata->Groups.clear(); globaldata->setGroups("");
121 catch(exception& e) {
122 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
126 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
130 /***********************************************************/
131 void UnifracUnweightedCommand::printUnweightedFile() {
135 for(int a = 0; a < numComp; a++) {
136 initFile(groupComb[a]);
138 for (it = validScores[a].begin(); it != validScores[a].end(); it++) {
139 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
148 remove(unweightedFileout.c_str());
151 catch(exception& e) {
152 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161 /***********************************************************/
162 void UnifracUnweightedCommand::printUWSummaryFile() {
166 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
169 for(int a = 0; a < numComp; a++) {
170 outSum << setprecision(6) << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << UWScoreSig[a][0] << endl;
171 cout << setprecision(6) << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << UWScoreSig[a][0] << endl;
175 catch(exception& e) {
176 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
180 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184 /***********************************************************/
186 void UnifracUnweightedCommand::setGroups() {
188 string allGroups = "";
190 //if the user has not entered specific groups to analyze then do them all
191 if (globaldata->Groups.size() != 0) {
192 if (globaldata->Groups[0] != "all") {
193 //check that groups are valid
194 for (int i = 0; i < globaldata->Groups.size(); i++) {
195 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
196 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
197 // erase the invalid group from globaldata->Groups
198 globaldata->Groups.erase(globaldata->Groups.begin()+i);
202 //if the user only entered invalid groups
203 if (globaldata->Groups.size() == 0) {
204 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;
205 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
206 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
208 allGroups += tmap->namesOfGroups[i];
211 for (int i = 0; i < globaldata->Groups.size(); i++) {
212 allGroups += globaldata->Groups[i];
216 }else{//user has enter "all" and wants the default groups
217 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
218 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
220 allGroups += tmap->namesOfGroups[i];
222 globaldata->setGroups("");
225 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
226 allGroups += tmap->namesOfGroups[i];
231 //calculate number of comparsions
233 for (int r=0; r<numGroups; r++) {
234 for (int l = r+1; l < numGroups; l++) {
235 groupComb.push_back(globaldata->Groups[r]+globaldata->Groups[l]);
242 groupComb.push_back(allGroups);
246 catch(exception& e) {
247 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
251 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
256 /*****************************************************************/
258 void UnifracUnweightedCommand::initFile(string label){
261 openOutputFile(unweightedFileout, out);
262 openInputFile(unweightedFile, inFile);
265 getline(inFile, inputBuffer);
267 out << inputBuffer << '\t' << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
269 openOutputFile(unweightedFileout, out);
270 out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
273 out.setf(ios::fixed, ios::floatfield);
274 out.setf(ios::showpoint);
276 catch(exception& e) {
277 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
281 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
286 /***********************************************************************/
288 void UnifracUnweightedCommand::output(vector<double> data){
292 getline(inFile, inputBuffer);
294 out << inputBuffer << setprecision(6) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
297 out << setprecision(6) << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
301 catch(exception& e) {
302 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
306 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
311 /***********************************************************************/
313 void UnifracUnweightedCommand::resetFile(){
324 remove(unweightedFile.c_str());
325 rename(unweightedFileout.c_str(), unweightedFile.c_str());
327 catch(exception& e) {
328 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
332 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";