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 reading = new Progress("Comparing to random:", iters);
57 //get pscore for users tree
58 userData.resize(numComp,0); //data = AB, AC, BC, ABC.
59 randomData.resize(numComp,0); //data = AB, AC, BC, ABC.
60 rscoreFreq.resize(numComp);
61 uscoreFreq.resize(numComp);
62 rCumul.resize(numComp);
63 uCumul.resize(numComp);
64 userTreeScores.resize(numComp);
65 UScoreSig.resize(numComp);
67 if (randomtree == "") {
68 //get pscores for users trees
69 for (int i = 0; i < T.size(); i++) {
70 userData = pars->getValues(T[i]); //data = AB, AC, BC, ABC.
72 //output scores for each combination
73 for(int k = 0; k < numComp; k++) {
76 it = uscoreFreq[k].find(userData[k]);
77 if (it == uscoreFreq[k].end()) {//new score
78 uscoreFreq[k][userData[k]] = 1;
79 }else{ uscoreFreq[k][userData[k]]++; }
81 //add users score to valid scores
82 validScores[userData[k]] = userData[k];
84 //save score for summary file
85 userTreeScores[k].push_back(userData[k]);
89 //get pscores for random trees
90 for (int j = 0; j < iters; j++) {
91 //create new tree with same num nodes and leaves as users
94 //create random relationships between nodes
95 randT->assembleRandomTree();
97 //get pscore of random tree
98 randomData = pars->getValues(randT);
100 for(int r = 0; r < numComp; r++) {
101 //add trees pscore to map of scores
102 it2 = rscoreFreq[r].find(randomData[r]);
103 if (it2 != rscoreFreq[r].end()) {//already have that score
104 rscoreFreq[r][randomData[r]]++;
105 }else{//first time we have seen this score
106 rscoreFreq[r][randomData[r]] = 1;
109 //add randoms score to validscores
110 validScores[randomData[r]] = randomData[r];
113 //update progress bar
120 //get pscores for random trees
121 for (int j = 0; j < iters; j++) {
122 //create new tree with same num nodes and leaves as users
124 //create random relationships between nodes
126 randT->assembleRandomTree();
128 //get pscore of random tree
129 randomData = pars->getValues(randT);
131 for(int r = 0; r < numComp; r++) {
132 //add trees pscore to map of scores
133 it2 = rscoreFreq[r].find(randomData[r]);
134 if (it2 != rscoreFreq[r].end()) {//already have that score
135 rscoreFreq[r][randomData[r]]++;
136 }else{//first time we have seen this score
137 rscoreFreq[r][randomData[r]] = 1;
140 //add randoms score to validscores
141 validScores[randomData[r]] = randomData[r];
144 //update progress bar
151 for(int a = 0; a < numComp; a++) {
152 float rcumul = 0.0000;
153 float ucumul = 0.0000;
154 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
155 for (it = validScores.begin(); it != validScores.end(); it++) {
156 if (randomtree == "") {
157 it2 = uscoreFreq[a].find(it->first);
158 //user data has that score
159 if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul+= it2->second; }
160 else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
162 uCumul[a][it->first] = ucumul;
165 //make rscoreFreq map and rCumul
166 it2 = rscoreFreq[a].find(it->first);
167 //get percentage of random trees with that info
168 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul+= it2->second; }
169 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
170 rCumul[a][it->first] = rcumul;
173 //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
174 for (int h = 0; h < userTreeScores[a].size(); h++) {
175 UScoreSig[a].push_back(rCumul[a][userTreeScores[a][h]]);
179 //finish progress bar
184 printParsimonyFile();
185 if (randomtree == "") { printUSummaryFile(); }
187 //reset globaldata's treemap if you just did random distrib
188 if (randomtree != "") {
189 //memory leak prevention
190 //if (globaldata->gTreemap != NULL) { delete globaldata->gTreemap; }
191 globaldata->gTreemap = savetmap;
194 //reset randomTree parameter to ""
195 globaldata->setRandomTree("");
196 //reset groups parameter
197 globaldata->Groups.clear(); globaldata->setGroups("");
202 catch(exception& e) {
203 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
212 /***********************************************************/
213 void ParsimonyCommand::printParsimonyFile() {
218 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
220 for(int a = 0; a < numComp; a++) {
221 initFile(groupComb[a]);
223 for (it = validScores.begin(); it != validScores.end(); it++) {
224 if (randomtree == "") {
225 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]);
227 data.push_back(it->first); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]);
237 remove(parsFileout.c_str());
239 catch(exception& e) {
240 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
244 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248 /***********************************************************/
249 void ParsimonyCommand::printUSummaryFile() {
252 outSum << "Tree#" << '\t' << "Groups" << '\t' << "ParsScore" << '\t' << "ParsSig" << endl;
253 cout << "Tree#" << '\t' << "Groups" << '\t' << "ParsScore" << '\t' << "ParsSig" << endl;
256 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
260 for (int i = 0; i< T.size(); i++) {
261 for(int a = 0; a < numComp; a++) {
262 if (UScoreSig[a][i] > (1/(float)iters)) {
263 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
264 cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
266 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << "<" << (1/float(iters)) << endl;
267 cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << "<" << (1/float(iters)) << endl;
274 catch(exception& e) {
275 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
279 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
284 /***********************************************************/
285 void ParsimonyCommand::getUserInput() {
289 tmap = new TreeMap();
291 cout << "Please enter the number of groups you would like to analyze: ";
296 numEachGroup.resize(numGroups, 0);
298 for (int i = 1; i <= numGroups; i++) {
299 cout << "Please enter the number of sequences in group " << i << ": ";
302 //set tmaps seqsPerGroup
303 tmap->seqsPerGroup[toString(i)] = num;
304 tmap->namesOfGroups.push_back(toString(i));
306 //set tmaps namesOfSeqs
307 for (int j = 0; j < num; j++) {
308 tmap->namesOfSeqs.push_back(toString(count));
309 tmap->treemap[toString(count)].groupname = toString(i);
314 //clears buffer so next command doesn't have error
318 //save tmap for later
319 //memory leak prevention
320 //if (globaldata->gTreemap != NULL) { delete globaldata->gTreemap; }
321 globaldata->gTreemap = tmap;
324 catch(exception& e) {
325 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
329 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
333 /***********************************************************/
335 void ParsimonyCommand::setGroups() {
337 string allGroups = "";
339 //if the user has not entered specific groups to analyze then do them all
340 if (globaldata->Groups.size() != 0) {
341 if (globaldata->Groups[0] != "all") {
342 //check that groups are valid
343 for (int i = 0; i < globaldata->Groups.size(); i++) {
344 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
345 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
346 // erase the invalid group from globaldata->Groups
347 globaldata->Groups.erase(globaldata->Groups.begin()+i);
351 //if the user only entered invalid groups
352 if (globaldata->Groups.size() == 0) {
353 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;
354 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
355 if (tmap->namesOfGroups[i] != "xxx") {
356 allGroups += tmap->namesOfGroups[i] + "-";
357 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
361 allGroups = allGroups.substr(0, allGroups.length()-1);
363 for (int i = 0; i < globaldata->Groups.size(); i++) {
364 allGroups += globaldata->Groups[i] + "-";
367 allGroups = allGroups.substr(0, allGroups.length()-1);
369 }else{//user has enter "all" and wants the default groups
370 globaldata->Groups.clear();
371 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
372 if (tmap->namesOfGroups[i] != "xxx") {
373 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
375 allGroups += tmap->namesOfGroups[i] + "-";
378 allGroups = allGroups.substr(0, allGroups.length()-1);
379 globaldata->setGroups("");
382 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
383 if (tmap->namesOfGroups[i] != "xxx") {
384 allGroups += tmap->namesOfGroups[i] + "-";
387 allGroups = allGroups.substr(0, allGroups.length()-1);
391 //calculate number of comparsions
393 for (int r=0; r<numGroups; r++) {
394 for (int l = r+1; l < numGroups; l++) {
395 groupComb.push_back(globaldata->Groups[r]+ "-" +globaldata->Groups[l]);
402 groupComb.push_back(allGroups);
407 catch(exception& e) {
408 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
412 cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
417 /*****************************************************************/
419 void ParsimonyCommand::initFile(string label){
422 openOutputFile(parsFileout, out);
423 openInputFile(parsFile, inFile);
426 getline(inFile, inputBuffer);
428 if (randomtree == "") {
429 out << inputBuffer << '\t' << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
431 out << inputBuffer << '\t' << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
434 openOutputFile(parsFileout, out);
436 if (randomtree == "") {
437 out << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
439 out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
443 out.setf(ios::fixed, ios::floatfield);
444 out.setf(ios::showpoint);
446 catch(exception& e) {
447 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
451 cout << "An unknown error has occurred in the ParsimonyCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
456 /***********************************************************************/
458 void ParsimonyCommand::output(vector<double> data){
462 getline(inFile, inputBuffer);
464 if (randomtree == "") {
465 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
467 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
471 if (randomtree == "") {
472 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
474 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
479 catch(exception& e) {
480 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
484 cout << "An unknown error has occurred in the ParsimonyCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
489 /***********************************************************************/
491 void ParsimonyCommand::resetFile(){
502 remove(parsFile.c_str());
503 rename(parsFileout.c_str(), parsFile.c_str());
505 catch(exception& e) {
506 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
510 cout << "An unknown error has occurred in the ParsimonyCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";