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 //if the user has not entered specific groups to analyze then do them all
32 if (globaldata->Groups.size() != 0) {
33 //check that groups are valid
34 for (int i = 0; i < globaldata->Groups.size(); i++) {
35 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
36 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
37 // erase the invalid group from globaldata->Groups
38 globaldata->Groups.erase (globaldata->Groups.begin()+i);
42 //if the user only entered invalid groups
43 if (globaldata->Groups.size() == 0) {
44 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;
45 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
46 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
50 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
51 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
55 }else { //user wants random distribution
56 savetmap = globaldata->gTreemap;
58 parsFile = randomtree + ".rd_parsimony";
59 openOutputFile(parsFile, out);
62 convert(globaldata->getIters(), iters); //how many random trees to generate
63 pars = new Parsimony(tmap);
67 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
71 cout << "An unknown error has occurred in the ParsimonyCommand class function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
75 /***********************************************************/
76 int ParsimonyCommand::execute() {
79 //get pscore for users tree
80 userData.resize(1,0); //data[0] = pscore.
81 randomData.resize(1,0); //data[0] = pscore.
84 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
85 outDist << "RandomTree#" << '\t' << "ParsScore" << endl;
87 if (randomtree == "") {
88 //get pscores for users trees
89 for (int i = 0; i < T.size(); i++) {
90 cout << "Processing tree " << i+1 << endl;
91 userData = pars->getValues(T[i]); //userData[0] = pscore
92 cout << "Tree " << i+1 << " parsimony score = " << userData[0] << endl;
94 it = uscoreFreq.find(userData[0]);
95 if (it == uscoreFreq.end()) {//new score
96 uscoreFreq[userData[0]] = 1;
97 }else{ uscoreFreq[userData[0]]++; }
99 //add users score to valid scores
100 validScores[userData[0]] = userData[0];
102 //save score for summary file
103 userTreeScores.push_back(userData[0]);
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];
127 //output info to pdistrib file
128 outDist << j+1 << '\t'<< '\t' << randomData[0] << endl;
133 //get pscores for random trees
134 for (int j = 0; j < iters; j++) {
135 //create new tree with same num nodes and leaves as users
137 //create random relationships between nodes
138 randT->assembleRandomTree();
139 //get pscore of random tree
140 randomData = pars->getValues(randT);
142 //add trees pscore to map of scores
143 it2 = rscoreFreq.find(randomData[0]);
144 if (it2 != rscoreFreq.end()) {//already have that score
145 rscoreFreq[randomData[0]]++;
146 }else{//first time we have seen this score
147 rscoreFreq[randomData[0]] = 1;
150 //add randoms score to validscores
151 validScores[randomData[0]] = randomData[0];
157 float rcumul = 0.0000;
158 float ucumul = 0.0000;
160 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
161 for (it = validScores.begin(); it != validScores.end(); it++) {
162 if (randomtree == "") {
163 it2 = uscoreFreq.find(it->first);
164 //user data has that score
165 if (it2 != uscoreFreq.end()) { uscoreFreq[it->first] /= T.size(); ucumul+= it2->second; }
166 else { uscoreFreq[it->first] = 0.0000; } //no user trees with that score
168 uCumul[it->first] = ucumul;
171 //make rscoreFreq map and rCumul
172 it2 = rscoreFreq.find(it->first);
173 //get percentage of random trees with that info
174 if (it2 != rscoreFreq.end()) { rscoreFreq[it->first] /= iters; rcumul+= it2->second; }
175 else { rscoreFreq[it->first] = 0.0000; } //no random trees with that score
176 rCumul[it->first] = rcumul;
179 //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
180 for (int h = 0; h < userTreeScores.size(); h++) {
181 UScoreSig.push_back(rCumul[userTreeScores[h]]);
184 printParsimonyFile();
187 //reset globaldata's treemap if you just did random distrib
188 if (randomtree != "") { globaldata->gTreemap = savetmap; }
190 //reset randomTree parameter to ""
191 globaldata->setRandomTree("");
192 //reset groups parameter
193 globaldata->Groups.clear();
198 catch(exception& e) {
199 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208 /***********************************************************/
209 void ParsimonyCommand::printParsimonyFile() {
212 if (randomtree == "") {
213 out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
215 out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
219 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
222 for (it = validScores.begin(); it != validScores.end(); it++) {
223 if (randomtree == "") {
224 out << setprecision(6) << it->first << '\t' << '\t' << uscoreFreq[it->first] << '\t' << uCumul[it->first] << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
226 out << setprecision(6) << it->first << '\t' << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
233 catch(exception& e) {
234 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
238 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
242 /***********************************************************/
243 void ParsimonyCommand::printUSummaryFile() {
246 outSum << "Tree#" << '\t' << "ParsScore" << '\t' << '\t' << "ParsSig" << endl;
249 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
252 for (int i = 0; i< T.size(); i++) {
253 outSum << setprecision(6) << i+1 << '\t' << '\t' << userTreeScores[i] << '\t' << UScoreSig[i] << endl;
258 catch(exception& e) {
259 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
268 /***********************************************************/
269 void ParsimonyCommand::getUserInput() {
273 tmap = new TreeMap();
275 cout << "Please enter the number of groups you would like to analyze: ";
280 numEachGroup.resize(numGroups, 0);
282 for (int i = 1; i <= numGroups; i++) {
283 cout << "Please enter the number of sequences in group " << i << ": ";
286 //set tmaps seqsPerGroup
287 tmap->seqsPerGroup[toString(i)] = num;
289 //set tmaps namesOfSeqs
290 for (int j = 0; j < num; j++) {
291 tmap->namesOfSeqs.push_back(toString(count));
292 tmap->treemap[toString(count)].groupname = toString(i);
297 //clears buffer so next command doesn't have error
301 //save tmap for later
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 /***********************************************************/