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 convert(globaldata->getRandomTree(), randomtree);
20 //user has entered their own tree
21 if (randomtree == 0) {
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
33 parsFile = "rd_parsimony";
34 openOutputFile(parsFile, out);
37 convert(globaldata->getIters(), iters); //how many random trees to generate
38 pars = new Parsimony(tmap);
42 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
46 cout << "An unknown error has occurred in the ParsimonyCommand class function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
50 /***********************************************************/
51 int ParsimonyCommand::execute() {
54 //get pscore for users tree
55 userData.resize(1,0); //data[0] = pscore.
56 randomData.resize(1,0); //data[0] = pscore.
59 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
60 outDist << "RandomTree#" << '\t' << "ParsScore" << endl;
62 if (randomtree == 0) {
63 //get pscores for users trees
64 for (int i = 0; i < T.size(); i++) {
65 cout << "Processing tree " << i+1 << endl;
66 userData = pars->getValues(T[i]); //userData[0] = pscore
68 it = uscoreFreq.find(userData[0]);
69 if (it == uscoreFreq.end()) {//new score
70 uscoreFreq[userData[0]] = 1;
71 }else{ uscoreFreq[userData[0]]++; }
73 //add users score to valid scores
74 validScores[userData[0]] = userData[0];
76 //save score for summary file
77 userTreeScores.push_back(userData[0]);
81 //get pscores for random trees
82 for (int j = 0; j < iters; j++) {
83 //create new tree with same num nodes and leaves as users
85 //create random relationships between nodes
86 randT->assembleRandomTree();
87 //get pscore of random tree
88 randomData = pars->getValues(randT);
90 //add trees pscore to map of scores
91 it2 = rscoreFreq.find(randomData[0]);
92 if (it2 != rscoreFreq.end()) {//already have that score
93 rscoreFreq[randomData[0]]++;
94 }else{//first time we have seen this score
95 rscoreFreq[randomData[0]] = 1;
98 //add randoms score to validscores
99 validScores[randomData[0]] = randomData[0];
101 //output info to pdistrib file
102 outDist << j+1 << '\t'<< '\t' << randomData[0] << endl;
107 //get pscores for random trees
108 for (int j = 0; j < iters; j++) {
109 //create new tree with same num nodes and leaves as users
111 //create random relationships between nodes
112 randT->assembleRandomTree();
113 //get pscore of random tree
114 randomData = pars->getValues(randT);
116 //add trees pscore to map of scores
117 it2 = rscoreFreq.find(randomData[0]);
118 if (it2 != rscoreFreq.end()) {//already have that score
119 rscoreFreq[randomData[0]]++;
120 }else{//first time we have seen this score
121 rscoreFreq[randomData[0]] = 1;
124 //add randoms score to validscores
125 validScores[randomData[0]] = randomData[0];
131 float rcumul = 0.0000;
132 float ucumul = 0.0000;
134 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
135 for (it = validScores.begin(); it != validScores.end(); it++) {
136 if (randomtree == 0) {
137 it2 = uscoreFreq.find(it->first);
138 //user data has that score
139 if (it2 != uscoreFreq.end()) { uscoreFreq[it->first] /= T.size(); ucumul+= it2->second; }
140 else { uscoreFreq[it->first] = 0.0000; } //no user trees with that score
142 uCumul[it->first] = ucumul;
145 //make rscoreFreq map and rCumul
146 it2 = rscoreFreq.find(it->first);
147 //get percentage of random trees with that info
148 if (it2 != rscoreFreq.end()) { rscoreFreq[it->first] /= iters; rcumul+= it2->second; }
149 else { rscoreFreq[it->first] = 0.0000; } //no random trees with that score
150 rCumul[it->first] = rcumul;
153 //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
154 for (int h = 0; h < userTreeScores.size(); h++) {
155 UScoreSig.push_back(rCumul[userTreeScores[h]]);
158 printParsimonyFile();
161 //reset randomTree parameter to 0
162 globaldata->setRandomTree("0");
167 catch(exception& e) {
168 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
172 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177 /***********************************************************/
178 void ParsimonyCommand::printParsimonyFile() {
181 if (randomtree == 0) {
182 out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
184 out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
188 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
191 for (it = validScores.begin(); it != validScores.end(); it++) {
192 if (randomtree == 0) {
193 out << setprecision(6) << it->first << '\t' << '\t' << uscoreFreq[it->first] << '\t' << uCumul[it->first] << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
195 out << setprecision(6) << it->first << '\t' << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
202 catch(exception& e) {
203 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211 /***********************************************************/
212 void ParsimonyCommand::printUSummaryFile() {
215 outSum << "Tree#" << '\t' << "ParsScore" << '\t' << '\t' << "ParsSig" << endl;
218 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
221 for (int i = 0; i< T.size(); i++) {
222 outSum << setprecision(6) << i+1 << '\t' << '\t' << userTreeScores[i] << '\t' << UScoreSig[i] << endl;
227 catch(exception& e) {
228 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
232 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237 /***********************************************************/
238 void ParsimonyCommand::getUserInput() {
242 tmap = new TreeMap();
244 cout << "Please enter the number of groups you would like to analyze: ";
249 numEachGroup.resize(numGroups, 0);
251 for (int i = 1; i <= numGroups; i++) {
252 cout << "Please enter the number of sequences in group " << i << ": ";
255 //set tmaps seqsPerGroup
256 tmap->seqsPerGroup[toString(i)] = num;
258 //set tmaps namesOfSeqs
259 for (int j = 0; j < num; j++) {
260 tmap->namesOfSeqs.push_back(toString(count));
261 tmap->treemap[toString(count)].groupname = toString(i);
266 //clears buffer so next command doesn't have error
270 //save tmap for later
271 globaldata->gTreemap = tmap;
274 catch(exception& e) {
275 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
279 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
283 /***********************************************************/