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
93 it = uscoreFreq.find(userData[0]);
94 if (it == uscoreFreq.end()) {//new score
95 uscoreFreq[userData[0]] = 1;
96 }else{ uscoreFreq[userData[0]]++; }
98 //add users score to valid scores
99 validScores[userData[0]] = userData[0];
101 //save score for summary file
102 userTreeScores.push_back(userData[0]);
106 //get pscores for random trees
107 for (int j = 0; j < iters; j++) {
108 //create new tree with same num nodes and leaves as users
110 //create random relationships between nodes
111 randT->assembleRandomTree();
112 //get pscore of random tree
113 randomData = pars->getValues(randT);
115 //add trees pscore to map of scores
116 it2 = rscoreFreq.find(randomData[0]);
117 if (it2 != rscoreFreq.end()) {//already have that score
118 rscoreFreq[randomData[0]]++;
119 }else{//first time we have seen this score
120 rscoreFreq[randomData[0]] = 1;
123 //add randoms score to validscores
124 validScores[randomData[0]] = randomData[0];
126 //output info to pdistrib file
127 outDist << j+1 << '\t'<< '\t' << randomData[0] << endl;
132 //get pscores for random trees
133 for (int j = 0; j < iters; j++) {
134 //create new tree with same num nodes and leaves as users
136 //create random relationships between nodes
137 randT->assembleRandomTree();
138 //get pscore of random tree
139 randomData = pars->getValues(randT);
141 //add trees pscore to map of scores
142 it2 = rscoreFreq.find(randomData[0]);
143 if (it2 != rscoreFreq.end()) {//already have that score
144 rscoreFreq[randomData[0]]++;
145 }else{//first time we have seen this score
146 rscoreFreq[randomData[0]] = 1;
149 //add randoms score to validscores
150 validScores[randomData[0]] = randomData[0];
156 float rcumul = 0.0000;
157 float ucumul = 0.0000;
159 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
160 for (it = validScores.begin(); it != validScores.end(); it++) {
161 if (randomtree == "") {
162 it2 = uscoreFreq.find(it->first);
163 //user data has that score
164 if (it2 != uscoreFreq.end()) { uscoreFreq[it->first] /= T.size(); ucumul+= it2->second; }
165 else { uscoreFreq[it->first] = 0.0000; } //no user trees with that score
167 uCumul[it->first] = ucumul;
170 //make rscoreFreq map and rCumul
171 it2 = rscoreFreq.find(it->first);
172 //get percentage of random trees with that info
173 if (it2 != rscoreFreq.end()) { rscoreFreq[it->first] /= iters; rcumul+= it2->second; }
174 else { rscoreFreq[it->first] = 0.0000; } //no random trees with that score
175 rCumul[it->first] = rcumul;
178 //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
179 for (int h = 0; h < userTreeScores.size(); h++) {
180 UScoreSig.push_back(rCumul[userTreeScores[h]]);
183 printParsimonyFile();
186 //reset globaldata's treemap if you just did random distrib
187 if (randomtree != "") { globaldata->gTreemap = savetmap; }
189 //reset randomTree parameter to ""
190 globaldata->setRandomTree("");
191 //reset groups parameter
192 globaldata->Groups.clear();
197 catch(exception& e) {
198 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
202 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207 /***********************************************************/
208 void ParsimonyCommand::printParsimonyFile() {
211 if (randomtree == "") {
212 out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
214 out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
218 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
221 for (it = validScores.begin(); it != validScores.end(); it++) {
222 if (randomtree == "") {
223 out << setprecision(6) << it->first << '\t' << '\t' << uscoreFreq[it->first] << '\t' << uCumul[it->first] << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
225 out << setprecision(6) << it->first << '\t' << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
232 catch(exception& e) {
233 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241 /***********************************************************/
242 void ParsimonyCommand::printUSummaryFile() {
245 outSum << "Tree#" << '\t' << "ParsScore" << '\t' << '\t' << "ParsSig" << endl;
248 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
251 for (int i = 0; i< T.size(); i++) {
252 outSum << setprecision(6) << i+1 << '\t' << '\t' << userTreeScores[i] << '\t' << UScoreSig[i] << endl;
257 catch(exception& e) {
258 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
262 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
267 /***********************************************************/
268 void ParsimonyCommand::getUserInput() {
272 tmap = new TreeMap();
274 cout << "Please enter the number of groups you would like to analyze: ";
279 numEachGroup.resize(numGroups, 0);
281 for (int i = 1; i <= numGroups; i++) {
282 cout << "Please enter the number of sequences in group " << i << ": ";
285 //set tmaps seqsPerGroup
286 tmap->seqsPerGroup[toString(i)] = num;
288 //set tmaps namesOfSeqs
289 for (int j = 0; j < num; j++) {
290 tmap->namesOfSeqs.push_back(toString(count));
291 tmap->treemap[toString(count)].groupname = toString(i);
296 //clears buffer so next command doesn't have error
300 //save tmap for later
301 globaldata->gTreemap = tmap;
304 catch(exception& e) {
305 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
309 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
313 /***********************************************************/