5 * Created by Sarah Westcott on 1/26/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "parsimonycommand.h"
12 /***********************************************************/
13 ParsimonyCommand::ParsimonyCommand() {
15 globaldata = GlobalData::getInstance();
17 //randomtree will tell us if user had their own treefile or if they just want the random distribution
18 randomtree = globaldata->getRandomTree();
20 //user has entered their own tree
21 if (randomtree == "") {
22 T = globaldata->gTree;
23 tmap = globaldata->gTreemap;
24 parsFile = globaldata->getTreeFile() + ".parsimony";
25 parsFileout = globaldata->getTreeFile() + "temp" + ".parsimony";
26 sumFile = globaldata->getTreeFile() + ".psummary";
27 openOutputFile(sumFile, outSum);
28 }else { //user wants random distribution
29 savetmap = globaldata->gTreemap;
31 parsFile = randomtree;
32 parsFileout = globaldata->getTreeFile() + "temp";
35 //set users groups to analyze
36 util = new SharedUtil();
37 util->setGroups(globaldata->Groups, tmap->namesOfGroups, allGroups, numGroups, "unweighted"); //sets the groups the user wants to analyze
38 util->getCombos(groupComb, globaldata->Groups, numComp);
39 globaldata->setGroups("");
43 groupComb.push_back(allGroups);
47 convert(globaldata->getIters(), iters); //how many random trees to generate
48 pars = new Parsimony(tmap);
53 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
57 cout << "An unknown error has occurred in the ParsimonyCommand class function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
61 /***********************************************************/
62 int ParsimonyCommand::execute() {
65 reading = new Progress("Comparing to random:", iters);
67 //get pscore for users tree
68 userData.resize(numComp,0); //data = AB, AC, BC, ABC.
69 randomData.resize(numComp,0); //data = AB, AC, BC, ABC.
70 rscoreFreq.resize(numComp);
71 uscoreFreq.resize(numComp);
72 rCumul.resize(numComp);
73 uCumul.resize(numComp);
74 userTreeScores.resize(numComp);
75 UScoreSig.resize(numComp);
77 if (randomtree == "") {
78 //get pscores for users trees
79 for (int i = 0; i < T.size(); i++) {
80 userData = pars->getValues(T[i]); //data = AB, AC, BC, ABC.
82 //output scores for each combination
83 for(int k = 0; k < numComp; k++) {
86 it = uscoreFreq[k].find(userData[k]);
87 if (it == uscoreFreq[k].end()) {//new score
88 uscoreFreq[k][userData[k]] = 1;
89 }else{ uscoreFreq[k][userData[k]]++; }
91 //add users score to valid scores
92 validScores[userData[k]] = userData[k];
94 //save score for summary file
95 userTreeScores[k].push_back(userData[k]);
99 //get pscores for random trees
100 for (int j = 0; j < iters; j++) {
101 //create new tree with same num nodes and leaves as users
104 //create random relationships between nodes
105 randT->assembleRandomTree();
107 //get pscore of random tree
108 randomData = pars->getValues(randT);
110 for(int r = 0; r < numComp; r++) {
111 //add trees pscore to map of scores
112 it2 = rscoreFreq[r].find(randomData[r]);
113 if (it2 != rscoreFreq[r].end()) {//already have that score
114 rscoreFreq[r][randomData[r]]++;
115 }else{//first time we have seen this score
116 rscoreFreq[r][randomData[r]] = 1;
119 //add randoms score to validscores
120 validScores[randomData[r]] = randomData[r];
123 //update progress bar
130 //get pscores for random trees
131 for (int j = 0; j < iters; j++) {
132 //create new tree with same num nodes and leaves as users
134 //create random relationships between nodes
136 randT->assembleRandomTree();
138 //get pscore of random tree
139 randomData = pars->getValues(randT);
141 for(int r = 0; r < numComp; r++) {
142 //add trees pscore to map of scores
143 it2 = rscoreFreq[r].find(randomData[r]);
144 if (it2 != rscoreFreq[r].end()) {//already have that score
145 rscoreFreq[r][randomData[r]]++;
146 }else{//first time we have seen this score
147 rscoreFreq[r][randomData[r]] = 1;
150 //add randoms score to validscores
151 validScores[randomData[r]] = randomData[r];
154 //update progress bar
161 for(int a = 0; a < numComp; a++) {
162 float rcumul = 0.0000;
163 float ucumul = 0.0000;
164 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
165 for (it = validScores.begin(); it != validScores.end(); it++) {
166 if (randomtree == "") {
167 it2 = uscoreFreq[a].find(it->first);
168 //user data has that score
169 if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul+= it2->second; }
170 else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
172 uCumul[a][it->first] = ucumul;
175 //make rscoreFreq map and rCumul
176 it2 = rscoreFreq[a].find(it->first);
177 //get percentage of random trees with that info
178 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul+= it2->second; }
179 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
180 rCumul[a][it->first] = rcumul;
183 //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
184 for (int h = 0; h < userTreeScores[a].size(); h++) {
185 UScoreSig[a].push_back(rCumul[a][userTreeScores[a][h]]);
189 //finish progress bar
194 printParsimonyFile();
195 if (randomtree == "") { printUSummaryFile(); }
197 //reset globaldata's treemap if you just did random distrib
198 if (randomtree != "") {
199 //memory leak prevention
200 //if (globaldata->gTreemap != NULL) { delete globaldata->gTreemap; }
201 globaldata->gTreemap = savetmap;
204 //reset randomTree parameter to ""
205 globaldata->setRandomTree("");
206 //reset groups parameter
207 globaldata->Groups.clear();
212 catch(exception& e) {
213 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
217 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
222 /***********************************************************/
223 void ParsimonyCommand::printParsimonyFile() {
228 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
230 for(int a = 0; a < numComp; a++) {
231 initFile(groupComb[a]);
233 for (it = validScores.begin(); it != validScores.end(); it++) {
234 if (randomtree == "") {
235 data.push_back(it->first); data.push_back(uscoreFreq[a][it->first]); data.push_back(uCumul[a][it->first]); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
237 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
247 remove(parsFileout.c_str());
249 catch(exception& e) {
250 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
258 /***********************************************************/
259 void ParsimonyCommand::printUSummaryFile() {
262 outSum << "Tree#" << '\t' << "Groups" << '\t' << "ParsScore" << '\t' << "ParsSig" << endl;
263 cout << "Tree#" << '\t' << "Groups" << '\t' << "ParsScore" << '\t' << "ParsSig" << endl;
266 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
270 for (int i = 0; i< T.size(); i++) {
271 for(int a = 0; a < numComp; a++) {
272 if (UScoreSig[a][i] > (1/(float)iters)) {
273 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
274 cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
276 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << "<" << (1/float(iters)) << endl;
277 cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << "<" << (1/float(iters)) << endl;
284 catch(exception& e) {
285 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
289 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
294 /***********************************************************/
295 void ParsimonyCommand::getUserInput() {
299 tmap = new TreeMap();
301 cout << "Please enter the number of groups you would like to analyze: ";
306 numEachGroup.resize(numGroups, 0);
308 for (int i = 1; i <= numGroups; i++) {
309 cout << "Please enter the number of sequences in group " << i << ": ";
312 //set tmaps seqsPerGroup
313 tmap->seqsPerGroup[toString(i)] = num;
314 tmap->namesOfGroups.push_back(toString(i));
316 //set tmaps namesOfSeqs
317 for (int j = 0; j < num; j++) {
318 tmap->namesOfSeqs.push_back(toString(count));
319 tmap->treemap[toString(count)].groupname = toString(i);
324 //clears buffer so next command doesn't have error
328 //save tmap for later
329 //memory leak prevention
330 //if (globaldata->gTreemap != NULL) { delete globaldata->gTreemap; }
331 globaldata->gTreemap = tmap;
334 catch(exception& e) {
335 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
339 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
343 /*****************************************************************/
345 void ParsimonyCommand::initFile(string label){
348 openOutputFile(parsFileout, out);
349 openInputFile(parsFile, inFile);
352 getline(inFile, inputBuffer);
354 if (randomtree == "") {
355 out << inputBuffer << '\t' << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
357 out << inputBuffer << '\t' << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
360 openOutputFile(parsFileout, out);
362 if (randomtree == "") {
363 out << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
365 out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
369 out.setf(ios::fixed, ios::floatfield);
370 out.setf(ios::showpoint);
372 catch(exception& e) {
373 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
377 cout << "An unknown error has occurred in the ParsimonyCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
382 /***********************************************************************/
384 void ParsimonyCommand::output(vector<double> data){
388 getline(inFile, inputBuffer);
390 if (randomtree == "") {
391 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
393 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
397 if (randomtree == "") {
398 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
400 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
405 catch(exception& e) {
406 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
410 cout << "An unknown error has occurred in the ParsimonyCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
415 /***********************************************************************/
417 void ParsimonyCommand::resetFile(){
428 remove(parsFile.c_str());
429 rename(parsFileout.c_str(), parsFile.c_str());
431 catch(exception& e) {
432 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
436 cout << "An unknown error has occurred in the ParsimonyCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";