]> git.donarmstrong.com Git - mothur.git/blob - parsimonycommand.cpp
4632019151fa0a93d7dba5c14017940d04aa1653
[mothur.git] / parsimonycommand.cpp
1 /*
2  *  parsimonycommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/26/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "parsimonycommand.h"
11
12 /***********************************************************/
13 ParsimonyCommand::ParsimonyCommand() {
14         try {
15                 globaldata = GlobalData::getInstance();
16                 
17                 //randomtree will tell us if user had their own treefile or if they just want the random distribution
18                 convert(globaldata->getRandomTree(), randomtree);
19                 
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);
30
31                 }else { //user wants random distribution
32                         getUserInput();
33                         parsFile = "rd_parsimony";
34                         openOutputFile(parsFile, out);
35                 }
36                 
37                 convert(globaldata->getIters(), iters);  //how many random trees to generate
38                 pars = new Parsimony(tmap);
39
40         }
41         catch(exception& e) {
42                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
43                 exit(1);
44         }
45         catch(...) {
46                 cout << "An unknown error has occurred in the ParsimonyCommand class function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47                 exit(1);
48         }
49 }
50 /***********************************************************/
51 int ParsimonyCommand::execute() {
52         try {
53                 
54                 //get pscore for users tree
55                 userData.resize(1,0);  //data[0] = pscore.
56                 randomData.resize(1,0);  //data[0] = pscore.
57                 
58                 //format output
59                 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
60                 outDist << "RandomTree#" << '\t' << "ParsScore" << endl;
61                 
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
67                                 //update uscoreFreq
68                                 it = uscoreFreq.find(userData[0]);
69                                 if (it == uscoreFreq.end()) {//new score
70                                         uscoreFreq[userData[0]] = 1;
71                                 }else{ uscoreFreq[userData[0]]++; }
72                         
73                                 //add users score to valid scores
74                                 validScores[userData[0]] = userData[0];
75                                 
76                                 //save score for summary file
77                                 userTreeScores.push_back(userData[0]);
78                         
79                         }
80                         
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
84                                 randT = new Tree();
85                                 //create random relationships between nodes
86                                 randT->assembleRandomTree();
87                                 //get pscore of random tree
88                                 randomData = pars->getValues(randT);
89                         
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;
96                                 }
97                         
98                                 //add randoms score to validscores
99                                 validScores[randomData[0]] = randomData[0];
100                                 
101                                 //output info to pdistrib file
102                                 outDist << j+1 << '\t'<< '\t' << randomData[0] << endl;
103                                         
104                                 delete randT;
105                         }
106                 }else {
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
110                                 randT = new Tree();
111                                 //create random relationships between nodes
112                                 randT->assembleRandomTree();
113                                 //get pscore of random tree
114                                 randomData = pars->getValues(randT);
115                         
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;
122                                 }
123                         
124                                 //add randoms score to validscores
125                                 validScores[randomData[0]] = randomData[0];
126                                         
127                                 delete randT;
128                         }
129                 }
130                 
131                 float rcumul = 0.0000;
132                 float ucumul = 0.0000;
133                 
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
141                                 //make uCumul map
142                                 uCumul[it->first] = ucumul;
143                         }
144                         
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;
151                 }
152                 
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]]);
156                 }
157
158                 printParsimonyFile();
159                 printUSummaryFile();
160                 
161                 //reset randomTree parameter to 0
162                 globaldata->setRandomTree("0");
163                 
164                 return 0;
165                 
166         }
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";
169                 exit(1);
170         }
171         catch(...) {
172                 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173                 exit(1);
174         }
175 }
176
177 /***********************************************************/
178 void ParsimonyCommand::printParsimonyFile() {
179         try {
180                 //column headers
181                 if (randomtree == 0) {
182                         out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
183                 }else {
184                         out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
185                 }
186                 
187                 //format output
188                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
189                 
190                 //print each line
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; 
194                         }else{
195                                 out << setprecision(6) << it->first << '\t' << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;      
196                         }
197                 } 
198                 
199                 out.close();
200                 
201         }
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";
204                 exit(1);
205         }
206         catch(...) {
207                 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208                 exit(1);
209         }
210 }
211 /***********************************************************/
212 void ParsimonyCommand::printUSummaryFile() {
213         try {
214                 //column headers
215                 outSum << "Tree#" << '\t'  <<  "ParsScore" << '\t' << '\t' << "ParsSig" <<  endl;
216                 
217                 //format output
218                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
219                 
220                 //print each line
221                 for (int i = 0; i< T.size(); i++) {
222                         outSum << setprecision(6) << i+1 << '\t' << '\t' << userTreeScores[i] << '\t' << UScoreSig[i] << endl; 
223                 }
224                 
225                 outSum.close();
226         }
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";
229                 exit(1);
230         }
231         catch(...) {
232                 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
233                 exit(1);
234         }
235 }
236
237 /***********************************************************/
238 void ParsimonyCommand::getUserInput() {
239         try {
240         
241                 //create treemap
242                 tmap = new TreeMap();
243
244                 cout << "Please enter the number of groups you would like to analyze: ";
245                 cin >> numGroups;
246                         
247                 int num, count;
248                 count = 1;
249                 numEachGroup.resize(numGroups, 0);  
250                 
251                 for (int i = 1; i <= numGroups; i++) {
252                         cout << "Please enter the number of sequences in group " << i <<  ": ";
253                         cin >> num;
254                                 
255                         //set tmaps seqsPerGroup
256                         tmap->seqsPerGroup[toString(i)] = num;
257                         
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);
262                                 count++;
263                         }
264                 }
265                 
266                 //clears buffer so next command doesn't have error
267                 string s;       
268                 getline(cin, s);
269                 
270                 //save tmap for later
271                 globaldata->gTreemap = tmap;
272                 
273         }
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";
276                 exit(1);
277         }
278         catch(...) {
279                 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
280                 exit(1);
281         }
282 }
283 /***********************************************************/
284