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 outSum << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" <<'\t' << "UWSig" << endl;
45 cout << "Tree#" << '\t' << "Groups" << '\t' << "UWScore" << '\t' << "UWSig" << endl;
47 //get pscores for users trees
48 for (int i = 0; i < T.size(); i++) {
50 unweightedFile = globaldata->getTreeFile() + toString(i+1) + ".unweighted";
51 unweightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".unweighted";
53 outSum << i+1 << '\t';
56 //get unweighted for users tree
57 rscoreFreq.resize(numComp);
58 rCumul.resize(numComp);
59 utreeScores.resize(numComp);
60 UWScoreSig.resize(numComp);
62 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
64 //output scores for each combination
65 for(int k = 0; k < numComp; k++) {
67 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 //add trees unweighted score to map of scores
78 it2 = rscoreFreq[k].find(randomData[k]);
79 if (it2 != rscoreFreq[k].end()) {//already have that score
80 rscoreFreq[k][randomData[k]]++;
81 }else{//first time we have seen this score
82 rscoreFreq[k][randomData[k]] = 1;
85 //add randoms score to validscores
86 validScores[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.begin(); it != validScores.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]]);
107 printUnweightedFile();
108 printUWSummaryFile();
116 //reset groups parameter
117 globaldata->Groups.clear(); globaldata->setGroups("");
123 catch(exception& e) {
124 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
128 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
132 /***********************************************************/
133 void UnifracUnweightedCommand::printUnweightedFile() {
137 for(int a = 0; a < numComp; a++) {
138 initFile(groupComb[a]);
140 for (it = validScores.begin(); it != validScores.end(); it++) {
141 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
150 remove(unweightedFileout.c_str());
153 catch(exception& e) {
154 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
158 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
163 /***********************************************************/
164 void UnifracUnweightedCommand::printUWSummaryFile() {
168 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
172 for(int a = 0; a < numComp; a++) {
173 if (UWScoreSig[a][0] > (1/(float)iters)) {
174 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
175 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
177 outSum << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
178 cout << setprecision(6) << groupComb[a] << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
183 catch(exception& e) {
184 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
192 /***********************************************************/
194 void UnifracUnweightedCommand::setGroups() {
196 string allGroups = "";
198 //if the user has not entered specific groups to analyze then do them all
199 if (globaldata->Groups.size() != 0) {
200 if (globaldata->Groups[0] != "all") {
201 //check that groups are valid
202 for (int i = 0; i < globaldata->Groups.size(); i++) {
203 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
204 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
205 // erase the invalid group from globaldata->Groups
206 globaldata->Groups.erase(globaldata->Groups.begin()+i);
210 //if the user only entered invalid groups
211 if (globaldata->Groups.size() == 0) {
212 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;
213 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
214 if (tmap->namesOfGroups[i] != "xxx") {
215 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
217 allGroups += tmap->namesOfGroups[i] + "-";
220 allGroups = allGroups.substr(0, allGroups.length()-1);
222 for (int i = 0; i < globaldata->Groups.size(); i++) {
223 allGroups += globaldata->Groups[i] + "-";
226 allGroups = allGroups.substr(0, allGroups.length()-1);
228 }else{//user has enter "all" and wants the default groups
229 globaldata->Groups.clear();
230 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
231 if (tmap->namesOfGroups[i] != "xxx") {
232 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
234 allGroups += tmap->namesOfGroups[i] + "-";
237 allGroups = allGroups.substr(0, allGroups.length()-1);
238 globaldata->setGroups("");
241 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
242 if (tmap->namesOfGroups[i] != "xxx") {
243 allGroups += tmap->namesOfGroups[i] + "-";
246 allGroups = allGroups.substr(0, allGroups.length()-1);
250 //calculate number of comparsions
252 for (int r=0; r<numGroups; r++) {
253 for (int l = r+1; l < numGroups; l++) {
254 groupComb.push_back(globaldata->Groups[r]+ "-" + globaldata->Groups[l]);
261 groupComb.push_back(allGroups);
265 catch(exception& e) {
266 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275 /*****************************************************************/
277 void UnifracUnweightedCommand::initFile(string label){
280 openOutputFile(unweightedFileout, out);
281 openInputFile(unweightedFile, inFile);
284 getline(inFile, inputBuffer);
286 out << inputBuffer << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
288 openOutputFile(unweightedFileout, out);
289 out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
292 out.setf(ios::fixed, ios::floatfield);
293 out.setf(ios::showpoint);
295 catch(exception& e) {
296 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
305 /***********************************************************************/
307 void UnifracUnweightedCommand::output(vector<double> data){
311 getline(inFile, inputBuffer);
312 // out << inputBuffer << setprecision(6) << '\t' << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
314 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
317 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
321 catch(exception& e) {
322 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
326 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
331 /***********************************************************************/
333 void UnifracUnweightedCommand::resetFile(){
344 remove(unweightedFile.c_str());
345 rename(unweightedFileout.c_str(), unweightedFile.c_str());
347 catch(exception& e) {
348 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
352 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";