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 openOutputFile(parsFile, out);
26 sumFile = globaldata->getTreeFile() + ".psummary";
27 openOutputFile(sumFile, outSum);
28 distFile = globaldata->getTreeFile() + ".pdistrib";
29 openOutputFile(distFile, outDist);
31 }else { //user wants random distribution
32 savetmap = globaldata->gTreemap;
34 parsFile = randomtree + ".rd_parsimony";
35 openOutputFile(parsFile, out);
38 convert(globaldata->getIters(), iters); //how many random trees to generate
39 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(1,0); //data[0] = pscore.
57 randomData.resize(1,0); //data[0] = pscore.
60 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
61 outDist << "RandomTree#" << '\t' << "ParsScore" << endl;
63 if (randomtree == "") {
64 //get pscores for users trees
65 for (int i = 0; i < T.size(); i++) {
66 cout << "Processing tree " << i+1 << endl;
67 userData = pars->getValues(T[i]); //userData[0] = pscore
69 it = uscoreFreq.find(userData[0]);
70 if (it == uscoreFreq.end()) {//new score
71 uscoreFreq[userData[0]] = 1;
72 }else{ uscoreFreq[userData[0]]++; }
74 //add users score to valid scores
75 validScores[userData[0]] = userData[0];
77 //save score for summary file
78 userTreeScores.push_back(userData[0]);
82 //get pscores for random trees
83 for (int j = 0; j < iters; j++) {
84 //create new tree with same num nodes and leaves as users
86 //create random relationships between nodes
87 randT->assembleRandomTree();
88 //get pscore of random tree
89 randomData = pars->getValues(randT);
91 //add trees pscore to map of scores
92 it2 = rscoreFreq.find(randomData[0]);
93 if (it2 != rscoreFreq.end()) {//already have that score
94 rscoreFreq[randomData[0]]++;
95 }else{//first time we have seen this score
96 rscoreFreq[randomData[0]] = 1;
99 //add randoms score to validscores
100 validScores[randomData[0]] = randomData[0];
102 //output info to pdistrib file
103 outDist << j+1 << '\t'<< '\t' << randomData[0] << endl;
108 //get pscores for random trees
109 for (int j = 0; j < iters; j++) {
110 //create new tree with same num nodes and leaves as users
112 //create random relationships between nodes
113 randT->assembleRandomTree();
114 //get pscore of random tree
115 randomData = pars->getValues(randT);
117 //add trees pscore to map of scores
118 it2 = rscoreFreq.find(randomData[0]);
119 if (it2 != rscoreFreq.end()) {//already have that score
120 rscoreFreq[randomData[0]]++;
121 }else{//first time we have seen this score
122 rscoreFreq[randomData[0]] = 1;
125 //add randoms score to validscores
126 validScores[randomData[0]] = randomData[0];
132 float rcumul = 0.0000;
133 float ucumul = 0.0000;
135 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
136 for (it = validScores.begin(); it != validScores.end(); it++) {
137 if (randomtree == "") {
138 it2 = uscoreFreq.find(it->first);
139 //user data has that score
140 if (it2 != uscoreFreq.end()) { uscoreFreq[it->first] /= T.size(); ucumul+= it2->second; }
141 else { uscoreFreq[it->first] = 0.0000; } //no user trees with that score
143 uCumul[it->first] = ucumul;
146 //make rscoreFreq map and rCumul
147 it2 = rscoreFreq.find(it->first);
148 //get percentage of random trees with that info
149 if (it2 != rscoreFreq.end()) { rscoreFreq[it->first] /= iters; rcumul+= it2->second; }
150 else { rscoreFreq[it->first] = 0.0000; } //no random trees with that score
151 rCumul[it->first] = rcumul;
154 //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
155 for (int h = 0; h < userTreeScores.size(); h++) {
156 UScoreSig.push_back(rCumul[userTreeScores[h]]);
159 printParsimonyFile();
162 //reset globaldata's treemap if you just did random distrib
163 if (randomtree != "") { globaldata->gTreemap = savetmap; }
165 //reset randomTree parameter to ""
166 globaldata->setRandomTree("");
171 catch(exception& e) {
172 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
176 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
181 /***********************************************************/
182 void ParsimonyCommand::printParsimonyFile() {
185 if (randomtree == "") {
186 out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
188 out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
192 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
195 for (it = validScores.begin(); it != validScores.end(); it++) {
196 if (randomtree == "") {
197 out << setprecision(6) << it->first << '\t' << '\t' << uscoreFreq[it->first] << '\t' << uCumul[it->first] << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
199 out << setprecision(6) << it->first << '\t' << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
206 catch(exception& e) {
207 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
215 /***********************************************************/
216 void ParsimonyCommand::printUSummaryFile() {
219 outSum << "Tree#" << '\t' << "ParsScore" << '\t' << '\t' << "ParsSig" << endl;
222 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
225 for (int i = 0; i< T.size(); i++) {
226 outSum << setprecision(6) << i+1 << '\t' << '\t' << userTreeScores[i] << '\t' << UScoreSig[i] << endl;
231 catch(exception& e) {
232 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
236 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241 /***********************************************************/
242 void ParsimonyCommand::getUserInput() {
246 tmap = new TreeMap();
248 cout << "Please enter the number of groups you would like to analyze: ";
253 numEachGroup.resize(numGroups, 0);
255 for (int i = 1; i <= numGroups; i++) {
256 cout << "Please enter the number of sequences in group " << i << ": ";
259 //set tmaps seqsPerGroup
260 tmap->seqsPerGroup[toString(i)] = num;
262 //set tmaps namesOfSeqs
263 for (int j = 0; j < num; j++) {
264 tmap->namesOfSeqs.push_back(toString(count));
265 tmap->treemap[toString(count)].groupname = toString(i);
270 //clears buffer so next command doesn't have error
274 //save tmap for later
275 globaldata->gTreemap = tmap;
278 catch(exception& e) {
279 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
283 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 /***********************************************************/