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
37 convert(globaldata->getIters(), iters); //how many random trees to generate
38 pars = new Parsimony(tmap);
43 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47 cout << "An unknown error has occurred in the ParsimonyCommand class function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
51 /***********************************************************/
52 int ParsimonyCommand::execute() {
55 //get pscore for users tree
56 userData.resize(numComp,0); //data = AB, AC, BC, ABC.
57 randomData.resize(numComp,0); //data = AB, AC, BC, ABC.
58 rscoreFreq.resize(numComp);
59 uscoreFreq.resize(numComp);
60 rCumul.resize(numComp);
61 uCumul.resize(numComp);
62 userTreeScores.resize(numComp);
63 UScoreSig.resize(numComp);
65 if (randomtree == "") {
66 //get pscores for users trees
67 for (int i = 0; i < T.size(); i++) {
68 userData = pars->getValues(T[i]); //data = AB, AC, BC, ABC.
70 //output scores for each combination
71 for(int k = 0; k < numComp; k++) {
73 it = uscoreFreq[k].find(userData[k]);
74 if (it == uscoreFreq[k].end()) {//new score
75 uscoreFreq[k][userData[k]] = 1;
76 }else{ uscoreFreq[k][userData[k]]++; }
78 //add users score to valid scores
79 validScores[userData[k]] = userData[k];
81 //save score for summary file
82 userTreeScores[k].push_back(userData[k]);
86 //get pscores for random trees
87 for (int j = 0; j < iters; j++) {
88 //create new tree with same num nodes and leaves as users
90 //create random relationships between nodes
91 randT->assembleRandomTree();
92 //get pscore of random tree
93 randomData = pars->getValues(randT);
95 for(int r = 0; r < numComp; r++) {
96 //add trees pscore to map of scores
97 it2 = rscoreFreq[r].find(randomData[r]);
98 if (it2 != rscoreFreq[r].end()) {//already have that score
99 rscoreFreq[r][randomData[r]]++;
100 }else{//first time we have seen this score
101 rscoreFreq[r][randomData[r]] = 1;
104 //add randoms score to validscores
105 validScores[randomData[r]] = randomData[r];
111 //get pscores for random trees
112 for (int j = 0; j < iters; j++) {
113 //create new tree with same num nodes and leaves as users
115 //create random relationships between nodes
116 randT->assembleRandomTree();
117 //get pscore of random tree
118 randomData = pars->getValues(randT);
120 for(int r = 0; r < numComp; r++) {
121 //add trees pscore to map of scores
122 it2 = rscoreFreq[r].find(randomData[r]);
123 if (it2 != rscoreFreq[r].end()) {//already have that score
124 rscoreFreq[r][randomData[r]]++;
125 }else{//first time we have seen this score
126 rscoreFreq[r][randomData[r]] = 1;
129 //add randoms score to validscores
130 validScores[randomData[r]] = randomData[r];
137 for(int a = 0; a < numComp; a++) {
138 float rcumul = 0.0000;
139 float ucumul = 0.0000;
140 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
141 for (it = validScores.begin(); it != validScores.end(); it++) {
142 if (randomtree == "") {
143 it2 = uscoreFreq[a].find(it->first);
144 //user data has that score
145 if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul+= it2->second; }
146 else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
148 uCumul[a][it->first] = ucumul;
151 //make rscoreFreq map and rCumul
152 it2 = rscoreFreq[a].find(it->first);
153 //get percentage of random trees with that info
154 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul+= it2->second; }
155 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
156 rCumul[a][it->first] = rcumul;
159 //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
160 for (int h = 0; h < userTreeScores[a].size(); h++) {
161 UScoreSig[a].push_back(rCumul[a][userTreeScores[a][h]]);
165 printParsimonyFile();
166 if (randomtree == "") { printUSummaryFile(); }
168 //reset globaldata's treemap if you just did random distrib
169 if (randomtree != "") {
170 //memory leak prevention
171 if (globaldata->gTreemap != NULL) { delete globaldata->gTreemap; }
172 globaldata->gTreemap = savetmap;
175 //reset randomTree parameter to ""
176 globaldata->setRandomTree("");
177 //reset groups parameter
178 globaldata->Groups.clear(); globaldata->setGroups("");
183 catch(exception& e) {
184 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 /***********************************************************/
194 void ParsimonyCommand::printParsimonyFile() {
199 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
201 for(int a = 0; a < numComp; a++) {
202 initFile(groupComb[a]);
204 for (it = validScores.begin(); it != validScores.end(); it++) {
205 if (randomtree == "") {
206 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]);
208 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
218 remove(parsFileout.c_str());
220 catch(exception& e) {
221 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
225 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
229 /***********************************************************/
230 void ParsimonyCommand::printUSummaryFile() {
233 outSum << "Tree#" << '\t' << "Groups" << '\t' << "ParsScore" << '\t' << "ParsSig" << endl;
234 cout << "Tree#" << '\t' << "Groups" << '\t' << "ParsScore" << '\t' << "ParsSig" << endl;
237 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
241 for (int i = 0; i< T.size(); i++) {
242 for(int a = 0; a < numComp; a++) {
243 if (UScoreSig[a][i] > (1/(float)iters)) {
244 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
245 cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
247 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << "<" << (1/float(iters)) << endl;
248 cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << "<" << (1/float(iters)) << endl;
255 catch(exception& e) {
256 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
260 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
265 /***********************************************************/
266 void ParsimonyCommand::getUserInput() {
270 tmap = new TreeMap();
272 cout << "Please enter the number of groups you would like to analyze: ";
277 numEachGroup.resize(numGroups, 0);
279 for (int i = 1; i <= numGroups; i++) {
280 cout << "Please enter the number of sequences in group " << i << ": ";
283 //set tmaps seqsPerGroup
284 tmap->seqsPerGroup[toString(i)] = num;
285 tmap->namesOfGroups.push_back(toString(i));
287 //set tmaps namesOfSeqs
288 for (int j = 0; j < num; j++) {
289 tmap->namesOfSeqs.push_back(toString(count));
290 tmap->treemap[toString(count)].groupname = toString(i);
295 //clears buffer so next command doesn't have error
299 //save tmap for later
300 //memory leak prevention
301 if (globaldata->gTreemap != NULL) { delete globaldata->gTreemap; }
302 globaldata->gTreemap = tmap;
305 catch(exception& e) {
306 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
310 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
314 /***********************************************************/
316 void ParsimonyCommand::setGroups() {
318 string allGroups = "";
320 //if the user has not entered specific groups to analyze then do them all
321 if (globaldata->Groups.size() != 0) {
322 if (globaldata->Groups[0] != "all") {
323 //check that groups are valid
324 for (int i = 0; i < globaldata->Groups.size(); i++) {
325 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
326 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
327 // erase the invalid group from globaldata->Groups
328 globaldata->Groups.erase(globaldata->Groups.begin()+i);
332 //if the user only entered invalid groups
333 if (globaldata->Groups.size() == 0) {
334 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;
335 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
336 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
338 allGroups += tmap->namesOfGroups[i] + "-";
340 allGroups = allGroups.substr(0, allGroups.length()-1);
342 for (int i = 0; i < globaldata->Groups.size(); i++) {
343 allGroups += globaldata->Groups[i] + "-";
346 allGroups = allGroups.substr(0, allGroups.length()-1);
348 }else{//user has enter "all" and wants the default groups
349 globaldata->Groups.clear();
350 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
351 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
353 allGroups += tmap->namesOfGroups[i] + "-";
355 allGroups = allGroups.substr(0, allGroups.length()-1);
356 globaldata->setGroups("");
359 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
360 allGroups += tmap->namesOfGroups[i] + "-";
362 allGroups = allGroups.substr(0, allGroups.length()-1);
366 //calculate number of comparsions
368 for (int r=0; r<numGroups; r++) {
369 for (int l = r+1; l < numGroups; l++) {
370 groupComb.push_back(globaldata->Groups[r]+ "-" +globaldata->Groups[l]);
377 groupComb.push_back(allGroups);
382 catch(exception& e) {
383 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
387 cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
392 /*****************************************************************/
394 void ParsimonyCommand::initFile(string label){
397 openOutputFile(parsFileout, out);
398 openInputFile(parsFile, inFile);
401 getline(inFile, inputBuffer);
403 if (randomtree == "") {
404 out << inputBuffer << '\t' << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
406 out << inputBuffer << '\t' << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
409 openOutputFile(parsFileout, out);
411 if (randomtree == "") {
412 out << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
414 out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
418 out.setf(ios::fixed, ios::floatfield);
419 out.setf(ios::showpoint);
421 catch(exception& e) {
422 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
426 cout << "An unknown error has occurred in the ParsimonyCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
431 /***********************************************************************/
433 void ParsimonyCommand::output(vector<double> data){
437 getline(inFile, inputBuffer);
439 if (randomtree == "") {
440 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
442 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
446 if (randomtree == "") {
447 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
449 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
454 catch(exception& e) {
455 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
459 cout << "An unknown error has occurred in the ParsimonyCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
464 /***********************************************************************/
466 void ParsimonyCommand::resetFile(){
477 remove(parsFile.c_str());
478 rename(parsFileout.c_str(), parsFile.c_str());
480 catch(exception& e) {
481 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
485 cout << "An unknown error has occurred in the ParsimonyCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";