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++) {
47 unweightedFile = globaldata->getTreeFile() + toString(i+1) + ".unweighted";
48 unweightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".unweighted";
51 outSum << "Tree# " << i+1 << endl;
52 outSum << "Comb" << '\t' << "UWScore" << '\t' << '\t' << "UWSig" << endl;
53 cout << "Tree# " << i+1 << endl;
54 cout << "Comb" << '\t' << "UWScore" << '\t' << '\t' << "UWSig" << endl;
57 //get unweighted for users tree
58 rscoreFreq.resize(numComp);
59 rCumul.resize(numComp);
60 utreeScores.resize(numComp);
61 UWScoreSig.resize(numComp);
63 userData = unweighted->getValues(T[i]); //userData[0] = unweightedscore
65 //output scores for each combination
66 for(int k = 0; k < numComp; 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 //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];
90 for(int a = 0; a < numComp; a++) {
91 float rcumul = 1.0000;
92 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
93 for (it = validScores.begin(); it != validScores.end(); it++) {
94 //make rscoreFreq map and rCumul
95 it2 = rscoreFreq[a].find(it->first);
96 rCumul[a][it->first] = rcumul;
97 //get percentage of random trees with that info
98 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul-= it2->second; }
99 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
101 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
104 printUnweightedFile();
105 printUWSummaryFile();
113 //reset groups parameter
114 globaldata->Groups.clear(); globaldata->setGroups("");
120 catch(exception& e) {
121 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
125 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
129 /***********************************************************/
130 void UnifracUnweightedCommand::printUnweightedFile() {
134 for(int a = 0; a < numComp; a++) {
135 initFile(groupComb[a]);
137 for (it = validScores.begin(); it != validScores.end(); it++) {
138 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
147 remove(unweightedFileout.c_str());
150 catch(exception& e) {
151 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
155 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
160 /***********************************************************/
161 void UnifracUnweightedCommand::printUWSummaryFile() {
165 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
168 for(int a = 0; a < numComp; a++) {
169 if (UWScoreSig[a][0] > (1/(float)iters)) {
170 outSum << setprecision(globaldata->getIters().length()) << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << UWScoreSig[a][0] << endl;
171 cout << setprecision(globaldata->getIters().length()) << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << UWScoreSig[a][0] << endl;
173 outSum << setprecision(globaldata->getIters().length()) << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << "<" << (1/float(iters)) << endl;
174 cout << setprecision(globaldata->getIters().length()) << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << "<" << (1/float(iters)) << endl;
179 catch(exception& e) {
180 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188 /***********************************************************/
190 void UnifracUnweightedCommand::setGroups() {
192 string allGroups = "";
194 //if the user has not entered specific groups to analyze then do them all
195 if (globaldata->Groups.size() != 0) {
196 if (globaldata->Groups[0] != "all") {
197 //check that groups are valid
198 for (int i = 0; i < globaldata->Groups.size(); i++) {
199 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
200 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
201 // erase the invalid group from globaldata->Groups
202 globaldata->Groups.erase(globaldata->Groups.begin()+i);
206 //if the user only entered invalid groups
207 if (globaldata->Groups.size() == 0) {
208 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;
209 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
210 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
212 allGroups += tmap->namesOfGroups[i] + "-";
214 allGroups = allGroups.substr(0, allGroups.length()-1);
216 for (int i = 0; i < globaldata->Groups.size(); i++) {
217 allGroups += globaldata->Groups[i] + "-";
220 allGroups = allGroups.substr(0, allGroups.length()-1);
222 }else{//user has enter "all" and wants the default groups
223 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
224 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
226 allGroups += tmap->namesOfGroups[i] + "-";
228 allGroups = allGroups.substr(0, allGroups.length()-1);
229 globaldata->setGroups("");
232 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
233 allGroups += tmap->namesOfGroups[i] + "-";
235 allGroups = allGroups.substr(0, allGroups.length()-1);
239 //calculate number of comparsions
241 for (int r=0; r<numGroups; r++) {
242 for (int l = r+1; l < numGroups; l++) {
243 groupComb.push_back(globaldata->Groups[r]+ "-" + globaldata->Groups[l]);
250 groupComb.push_back(allGroups);
254 catch(exception& e) {
255 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
259 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
264 /*****************************************************************/
266 void UnifracUnweightedCommand::initFile(string label){
269 openOutputFile(unweightedFileout, out);
270 openInputFile(unweightedFile, inFile);
273 getline(inFile, inputBuffer);
275 out << inputBuffer << '\t' << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
277 openOutputFile(unweightedFileout, out);
278 out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
281 out.setf(ios::fixed, ios::floatfield);
282 out.setf(ios::showpoint);
284 catch(exception& e) {
285 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
289 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294 /***********************************************************************/
296 void UnifracUnweightedCommand::output(vector<double> data){
300 getline(inFile, inputBuffer);
302 out << inputBuffer << setprecision(globaldata->getIters().length()) << '\t' << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
305 out << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
309 catch(exception& e) {
310 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
314 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
319 /***********************************************************************/
321 void UnifracUnweightedCommand::resetFile(){
332 remove(unweightedFile.c_str());
333 rename(unweightedFileout.c_str(), unweightedFile.c_str());
335 catch(exception& e) {
336 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
340 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";