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() {
54 //get pscore for users tree
55 userData.resize(1,0); //data[0] = pscore.
56 randomData.resize(1,0); //data[0] = pscore.
58 if (randomtree == "") {
59 copyUserTree = new Tree();
60 //get pscores for users trees
61 for (int i = 0; i < T.size(); i++) {
62 //copy users tree so that you can redo pgroups
63 copyUserTree->getCopy(T[i]);
64 cout << "Processing tree " << i+1 << endl;
65 userData = pars->getValues(copyUserTree); //userData[0] = pscore
66 cout << "Tree " << i+1 << " parsimony score = " << userData[0] << endl;
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];
104 //get pscores for random trees
105 for (int j = 0; j < iters; j++) {
106 //create new tree with same num nodes and leaves as users
108 //create random relationships between nodes
109 randT->assembleRandomTree();
110 //get pscore of random tree
111 randomData = pars->getValues(randT);
113 //add trees pscore to map of scores
114 it2 = rscoreFreq.find(randomData[0]);
115 if (it2 != rscoreFreq.end()) {//already have that score
116 rscoreFreq[randomData[0]]++;
117 }else{//first time we have seen this score
118 rscoreFreq[randomData[0]] = 1;
121 //add randoms score to validscores
122 validScores[randomData[0]] = randomData[0];
128 float rcumul = 0.0000;
129 float ucumul = 0.0000;
131 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
132 for (it = validScores.begin(); it != validScores.end(); it++) {
133 if (randomtree == "") {
134 it2 = uscoreFreq.find(it->first);
135 //user data has that score
136 if (it2 != uscoreFreq.end()) { uscoreFreq[it->first] /= T.size(); ucumul+= it2->second; }
137 else { uscoreFreq[it->first] = 0.0000; } //no user trees with that score
139 uCumul[it->first] = ucumul;
142 //make rscoreFreq map and rCumul
143 it2 = rscoreFreq.find(it->first);
144 //get percentage of random trees with that info
145 if (it2 != rscoreFreq.end()) { rscoreFreq[it->first] /= iters; rcumul+= it2->second; }
146 else { rscoreFreq[it->first] = 0.0000; } //no random trees with that score
147 rCumul[it->first] = rcumul;
150 //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
151 for (int h = 0; h < userTreeScores.size(); h++) {
152 UScoreSig.push_back(rCumul[userTreeScores[h]]);
155 printParsimonyFile();
158 //reset globaldata's treemap if you just did random distrib
159 if (randomtree != "") { globaldata->gTreemap = savetmap; }
161 //reset randomTree parameter to ""
162 globaldata->setRandomTree("");
163 //reset groups parameter
164 globaldata->Groups.clear(); globaldata->setGroups("");
169 catch(exception& e) {
170 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
174 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179 /***********************************************************/
180 void ParsimonyCommand::printParsimonyFile() {
183 if (randomtree == "") {
184 out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
186 out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
190 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
193 for (it = validScores.begin(); it != validScores.end(); it++) {
194 if (randomtree == "") {
195 out << setprecision(6) << it->first << '\t' << '\t' << uscoreFreq[it->first] << '\t' << uCumul[it->first] << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
197 out << setprecision(6) << it->first << '\t' << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;
204 catch(exception& e) {
205 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
209 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
213 /***********************************************************/
214 void ParsimonyCommand::printUSummaryFile() {
217 outSum << "Tree#" << '\t' << "ParsScore" << '\t' << '\t' << "ParsSig" << endl;
220 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
223 for (int i = 0; i< T.size(); i++) {
224 outSum << setprecision(6) << i+1 << '\t' << '\t' << userTreeScores[i] << '\t' << UScoreSig[i] << endl;
229 catch(exception& e) {
230 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
234 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239 /***********************************************************/
240 void ParsimonyCommand::getUserInput() {
244 tmap = new TreeMap();
246 cout << "Please enter the number of groups you would like to analyze: ";
251 numEachGroup.resize(numGroups, 0);
253 for (int i = 1; i <= numGroups; i++) {
254 cout << "Please enter the number of sequences in group " << i << ": ";
257 //set tmaps seqsPerGroup
258 tmap->seqsPerGroup[toString(i)] = num;
260 //set tmaps namesOfSeqs
261 for (int j = 0; j < num; j++) {
262 tmap->namesOfSeqs.push_back(toString(count));
263 tmap->treemap[toString(count)].groupname = toString(i);
268 //clears buffer so next command doesn't have error
272 //save tmap for later
273 globaldata->gTreemap = tmap;
276 catch(exception& e) {
277 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
281 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
285 /***********************************************************/
287 void ParsimonyCommand::setGroups() {
289 //if the user has not entered specific groups to analyze then do them all
290 if (globaldata->Groups.size() != 0) {
291 if (globaldata->Groups[0] != "all") {
292 //check that groups are valid
293 for (int i = 0; i < globaldata->Groups.size(); i++) {
294 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
295 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
296 // erase the invalid group from globaldata->Groups
297 globaldata->Groups.erase (globaldata->Groups.begin()+i);
301 //if the user only entered invalid groups
302 if (globaldata->Groups.size() == 0) {
303 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;
304 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
305 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
308 }else{//user has enter "all" and wants the default groups
309 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
310 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
312 globaldata->setGroups("");
315 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
316 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
320 catch(exception& e) {
321 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
325 cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
330 /*****************************************************************/