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 + ".rd_parsimony";
32 parsFileout = globaldata->getTreeFile() + "temp" + ".rd_parsimony";
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 != "") { globaldata->gTreemap = savetmap; }
171 //reset randomTree parameter to ""
172 globaldata->setRandomTree("");
173 //reset groups parameter
174 globaldata->Groups.clear(); globaldata->setGroups("");
179 catch(exception& e) {
180 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189 /***********************************************************/
190 void ParsimonyCommand::printParsimonyFile() {
195 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
197 for(int a = 0; a < numComp; a++) {
198 initFile(groupComb[a]);
200 for (it = validScores.begin(); it != validScores.end(); it++) {
201 if (randomtree == "") {
202 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]);
204 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
214 remove(parsFileout.c_str());
216 catch(exception& e) {
217 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
221 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
225 /***********************************************************/
226 void ParsimonyCommand::printUSummaryFile() {
229 outSum << "Tree#" << '\t' << "Comb" << '\t' << "ParsScore" << '\t' << '\t' << "ParsSig" << endl;
230 cout << "Tree#" << '\t' << "Comb" << '\t' << "ParsScore" << '\t' << '\t' << "ParsSig" << endl;
233 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
237 for (int i = 0; i< T.size(); i++) {
238 for(int a = 0; a < numComp; a++) {
239 if (UScoreSig[a][i] > (1/(float)iters)) {
240 outSum << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl;
241 cout << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl;
243 outSum << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << "<" << (1/float(iters)) << endl;
244 cout << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << "<" << (1/float(iters)) << endl;
251 catch(exception& e) {
252 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
256 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261 /***********************************************************/
262 void ParsimonyCommand::getUserInput() {
266 tmap = new TreeMap();
268 cout << "Please enter the number of groups you would like to analyze: ";
273 numEachGroup.resize(numGroups, 0);
275 for (int i = 1; i <= numGroups; i++) {
276 cout << "Please enter the number of sequences in group " << i << ": ";
279 //set tmaps seqsPerGroup
280 tmap->seqsPerGroup[toString(i)] = num;
281 tmap->namesOfGroups.push_back(toString(i));
283 //set tmaps namesOfSeqs
284 for (int j = 0; j < num; j++) {
285 tmap->namesOfSeqs.push_back(toString(count));
286 tmap->treemap[toString(count)].groupname = toString(i);
291 //clears buffer so next command doesn't have error
295 //save tmap for later
296 globaldata->gTreemap = tmap;
299 catch(exception& e) {
300 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
304 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
308 /***********************************************************/
310 void ParsimonyCommand::setGroups() {
312 string allGroups = "";
314 //if the user has not entered specific groups to analyze then do them all
315 if (globaldata->Groups.size() != 0) {
316 if (globaldata->Groups[0] != "all") {
317 //check that groups are valid
318 for (int i = 0; i < globaldata->Groups.size(); i++) {
319 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
320 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
321 // erase the invalid group from globaldata->Groups
322 globaldata->Groups.erase(globaldata->Groups.begin()+i);
326 //if the user only entered invalid groups
327 if (globaldata->Groups.size() == 0) {
328 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;
329 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
330 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
332 allGroups += tmap->namesOfGroups[i] + "-";
334 allGroups = allGroups.substr(0, allGroups.length()-1);
336 for (int i = 0; i < globaldata->Groups.size(); i++) {
337 allGroups += globaldata->Groups[i] + "-";
340 allGroups = allGroups.substr(0, allGroups.length()-1);
342 }else{//user has enter "all" and wants the default groups
343 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
344 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
346 allGroups += tmap->namesOfGroups[i] + "-";
348 allGroups = allGroups.substr(0, allGroups.length()-1);
349 globaldata->setGroups("");
352 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
353 allGroups += tmap->namesOfGroups[i] + "-";
355 allGroups = allGroups.substr(0, allGroups.length()-1);
359 //calculate number of comparsions
361 for (int r=0; r<numGroups; r++) {
362 for (int l = r+1; l < numGroups; l++) {
363 groupComb.push_back(globaldata->Groups[r]+ "-" +globaldata->Groups[l]);
370 groupComb.push_back(allGroups);
375 catch(exception& e) {
376 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
380 cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
385 /*****************************************************************/
387 void ParsimonyCommand::initFile(string label){
390 openOutputFile(parsFileout, out);
391 openInputFile(parsFile, inFile);
394 getline(inFile, inputBuffer);
396 if (randomtree == "") {
397 out << inputBuffer << '\t' << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
399 out << inputBuffer << '\t' << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
402 openOutputFile(parsFileout, out);
404 if (randomtree == "") {
405 out << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
407 out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
411 out.setf(ios::fixed, ios::floatfield);
412 out.setf(ios::showpoint);
414 catch(exception& e) {
415 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
419 cout << "An unknown error has occurred in the ParsimonyCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
424 /***********************************************************************/
426 void ParsimonyCommand::output(vector<double> data){
430 getline(inFile, inputBuffer);
432 if (randomtree == "") {
433 out << inputBuffer << '\t' << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
435 out << inputBuffer << '\t' << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
439 if (randomtree == "") {
440 out << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
442 out << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
447 catch(exception& e) {
448 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
452 cout << "An unknown error has occurred in the ParsimonyCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
457 /***********************************************************************/
459 void ParsimonyCommand::resetFile(){
470 remove(parsFile.c_str());
471 rename(parsFileout.c_str(), parsFile.c_str());
473 catch(exception& e) {
474 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
478 cout << "An unknown error has occurred in the ParsimonyCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";