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 //set users groups to analyze
30 }else { //user wants random distribution
31 savetmap = globaldata->gTreemap;
33 parsFile = randomtree + ".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() {
55 if (randomtree == "") {
56 //get pscore for users tree
57 userData.resize(numComp,0); //data = AB, AC, BC, ABC.
58 randomData.resize(numComp,0); //data = AB, AC, BC, ABC.
59 rscoreFreq.resize(numComp);
60 uscoreFreq.resize(numComp);
61 rCumul.resize(numComp);
62 uCumul.resize(numComp);
63 validScores.resize(numComp);
64 userTreeScores.resize(numComp);
65 UScoreSig.resize(numComp);
67 //get pscores for users trees
68 for (int i = 0; i < T.size(); i++) {
69 cout << "Processing tree " << i+1 << endl;
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++) {
74 cout << "Tree " << i+1 << " Combination " << groupComb[k] << " parsimony score = " << userData[k] << endl;
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[k][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
93 //create random relationships between nodes
94 randT->assembleRandomTree();
95 //get pscore of random tree
96 randomData = pars->getValues(randT);
98 for(int r = 0; r < numComp; r++) {
99 //add trees pscore to map of scores
100 it2 = rscoreFreq[r].find(randomData[r]);
101 if (it2 != rscoreFreq[r].end()) {//already have that score
102 rscoreFreq[r][randomData[r]]++;
103 }else{//first time we have seen this score
104 rscoreFreq[r][randomData[r]] = 1;
107 //add randoms score to validscores
108 validScores[r][randomData[r]] = randomData[r];
114 //get pscores for random trees
115 for (int j = 0; j < iters; j++) {
116 //create new tree with same num nodes and leaves as users
118 //create random relationships between nodes
119 randT->assembleRandomTree();
120 //get pscore of random tree
121 randomData = pars->getValues(randT);
123 for(int r = 0; r < numComp; r++) {
124 //add trees pscore to map of scores
125 it2 = rscoreFreq[r].find(randomData[r]);
126 if (it2 != rscoreFreq[r].end()) {//already have that score
127 rscoreFreq[r][randomData[r]]++;
128 }else{//first time we have seen this score
129 rscoreFreq[r][randomData[r]] = 1;
132 //add randoms score to validscores
133 validScores[r][randomData[r]] = randomData[r];
140 float rcumul = 0.0000;
141 float ucumul = 0.0000;
143 for(int a = 0; a < numComp; a++) {
144 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
145 for (it = validScores[a].begin(); it != validScores[a].end(); it++) {
146 if (randomtree == "") {
147 it2 = uscoreFreq[a].find(it->first);
148 //user data has that score
149 if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul+= it2->second; }
150 else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
152 uCumul[a][it->first] = ucumul-a;
155 //make rscoreFreq map and rCumul
156 it2 = rscoreFreq[a].find(it->first);
157 //get percentage of random trees with that info
158 if (it2 != rscoreFreq[a].end()) { rscoreFreq[a][it->first] /= iters; rcumul+= it2->second; }
159 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
160 rCumul[a][it->first] = rcumul-a;
163 //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
164 for (int h = 0; h < userTreeScores[a].size(); h++) {
165 UScoreSig[a].push_back(rCumul[a][userTreeScores[a][h]]);
169 printParsimonyFile();
172 //reset globaldata's treemap if you just did random distrib
173 if (randomtree != "") { globaldata->gTreemap = savetmap; }
175 //reset randomTree parameter to ""
176 globaldata->setRandomTree("");
177 //reset groups parameter
178 globaldata->Groups.clear(); globaldata->setGroups("");
183 catch(exception& e) {
184 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 /***********************************************************/
194 void ParsimonyCommand::printParsimonyFile() {
197 if (randomtree == "") {
198 out << "Comb" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
200 out << "Comb" << '\t' << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
204 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
206 for(int a = 0; a < numComp; a++) {
208 for (it = validScores[a].begin(); it != validScores[a].end(); it++) {
209 if (randomtree == "") {
210 out << setprecision(6) << groupComb[a] << '\t' << it->first << '\t' << '\t'<< uscoreFreq[a][it->first] << '\t' << uCumul[a][it->first] << '\t' << rscoreFreq[a][it->first] << '\t' << rCumul[a][it->first] << endl;
212 out << setprecision(6) << groupComb[a] << '\t' << it->first << '\t' << '\t' << rscoreFreq[a][it->first] << '\t' << rCumul[a][it->first] << endl;
219 catch(exception& e) {
220 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
224 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
228 /***********************************************************/
229 void ParsimonyCommand::printUSummaryFile() {
232 outSum << "Tree#" << '\t' << "Comb" << '\t' << "ParsScore" << '\t' << '\t' << "ParsSig" << endl;
235 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
239 for (int i = 0; i< T.size(); i++) {
240 for(int a = 0; a < numComp; a++) {
241 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl;
247 catch(exception& e) {
248 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
252 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257 /***********************************************************/
258 void ParsimonyCommand::getUserInput() {
262 tmap = new TreeMap();
264 cout << "Please enter the number of groups you would like to analyze: ";
269 numEachGroup.resize(numGroups, 0);
271 for (int i = 1; i <= numGroups; i++) {
272 cout << "Please enter the number of sequences in group " << i << ": ";
275 //set tmaps seqsPerGroup
276 tmap->seqsPerGroup[toString(i)] = num;
278 //set tmaps namesOfSeqs
279 for (int j = 0; j < num; j++) {
280 tmap->namesOfSeqs.push_back(toString(count));
281 tmap->treemap[toString(count)].groupname = toString(i);
286 //clears buffer so next command doesn't have error
290 //save tmap for later
291 globaldata->gTreemap = tmap;
294 catch(exception& e) {
295 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
299 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
303 /***********************************************************/
305 void ParsimonyCommand::setGroups() {
307 string allGroups = "";
309 //if the user has not entered specific groups to analyze then do them all
310 if (globaldata->Groups.size() != 0) {
311 if (globaldata->Groups[0] != "all") {
312 //check that groups are valid
313 for (int i = 0; i < globaldata->Groups.size(); i++) {
314 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
315 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
316 // erase the invalid group from globaldata->Groups
317 globaldata->Groups.erase(globaldata->Groups.begin()+i);
321 //if the user only entered invalid groups
322 if (globaldata->Groups.size() == 0) {
323 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;
324 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
325 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
327 allGroups += tmap->namesOfGroups[i];
330 for (int i = 0; i < globaldata->Groups.size(); i++) {
331 allGroups += tmap->namesOfGroups[i];
335 }else{//user has enter "all" and wants the default groups
336 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
337 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
339 allGroups += tmap->namesOfGroups[i];
341 globaldata->setGroups("");
344 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
345 allGroups += tmap->namesOfGroups[i];
350 //calculate number of comparsions
352 for (int r=0; r<numGroups; r++) {
353 for (int l = r+1; l < numGroups; l++) {
354 groupComb.push_back(globaldata->Groups[r]+globaldata->Groups[l]);
360 groupComb.push_back(allGroups);
364 catch(exception& e) {
365 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
369 cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374 /*****************************************************************/