]> git.donarmstrong.com Git - mothur.git/blob - parsimonycommand.cpp
fe5a1dd251899818715cb52c14ac7c73a40630b2
[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                 randomtree = globaldata->getRandomTree();
19                 
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);
30                         
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);
39                                         }
40                                 }
41                         
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]);
47                                         }
48                                 }               
49                         }else {
50                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
51                                         globaldata->Groups.push_back(tmap->namesOfGroups[i]);
52                                 }
53                         }
54
55                 }else { //user wants random distribution
56                         savetmap = globaldata->gTreemap;
57                         getUserInput();
58                         parsFile = randomtree + ".rd_parsimony";
59                         openOutputFile(parsFile, out);
60                 }
61                 
62                 convert(globaldata->getIters(), iters);  //how many random trees to generate
63                 pars = new Parsimony(tmap);
64
65         }
66         catch(exception& e) {
67                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
68                 exit(1);
69         }
70         catch(...) {
71                 cout << "An unknown error has occurred in the ParsimonyCommand class function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72                 exit(1);
73         }
74 }
75 /***********************************************************/
76 int ParsimonyCommand::execute() {
77         try {
78                 
79                 //get pscore for users tree
80                 userData.resize(1,0);  //data[0] = pscore.
81                 randomData.resize(1,0);  //data[0] = pscore.
82                 
83                 //format output
84                 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
85                 outDist << "RandomTree#" << '\t' << "ParsScore" << endl;
86                 
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;
93                                 //update uscoreFreq
94                                 it = uscoreFreq.find(userData[0]);
95                                 if (it == uscoreFreq.end()) {//new score
96                                         uscoreFreq[userData[0]] = 1;
97                                 }else{ uscoreFreq[userData[0]]++; }
98                         
99                                 //add users score to valid scores
100                                 validScores[userData[0]] = userData[0];
101                                 
102                                 //save score for summary file
103                                 userTreeScores.push_back(userData[0]);
104                         
105                         }
106                         
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                                 //output info to pdistrib file
128                                 outDist << j+1 << '\t'<< '\t' << randomData[0] << endl;
129                                         
130                                 delete randT;
131                         }
132                 }else {
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
136                                 randT = new Tree();
137                                 //create random relationships between nodes
138                                 randT->assembleRandomTree();
139                                 //get pscore of random tree
140                                 randomData = pars->getValues(randT);
141                         
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;
148                                 }
149                         
150                                 //add randoms score to validscores
151                                 validScores[randomData[0]] = randomData[0];
152                                         
153                                 delete randT;
154                         }
155                 }
156                 
157                 float rcumul = 0.0000;
158                 float ucumul = 0.0000;
159                 
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
167                                 //make uCumul map
168                                 uCumul[it->first] = ucumul;
169                         }
170                         
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;
177                 }
178                 
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]]);
182                 }
183
184                 printParsimonyFile();
185                 printUSummaryFile();
186                 
187                 //reset globaldata's treemap if you just did random distrib
188                 if (randomtree != "") { globaldata->gTreemap = savetmap; }
189                 
190                 //reset randomTree parameter to ""
191                 globaldata->setRandomTree("");
192                 //reset groups parameter
193                 globaldata->Groups.clear();
194                 
195                 return 0;
196                 
197         }
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";
200                 exit(1);
201         }
202         catch(...) {
203                 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204                 exit(1);
205         }
206 }
207
208 /***********************************************************/
209 void ParsimonyCommand::printParsimonyFile() {
210         try {
211                 //column headers
212                 if (randomtree == "") {
213                         out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
214                 }else {
215                         out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
216                 }
217                 
218                 //format output
219                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
220                 
221                 //print each line
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; 
225                         }else{
226                                 out << setprecision(6) << it->first << '\t' << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;      
227                         }
228                 } 
229                 
230                 out.close();
231                 
232         }
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";
235                 exit(1);
236         }
237         catch(...) {
238                 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
239                 exit(1);
240         }
241 }
242 /***********************************************************/
243 void ParsimonyCommand::printUSummaryFile() {
244         try {
245                 //column headers
246                 outSum << "Tree#" << '\t'  <<  "ParsScore" << '\t' << '\t' << "ParsSig" <<  endl;
247                 
248                 //format output
249                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
250                 
251                 //print each line
252                 for (int i = 0; i< T.size(); i++) {
253                         outSum << setprecision(6) << i+1 << '\t' << '\t' << userTreeScores[i] << '\t' << UScoreSig[i] << endl; 
254                 }
255                 
256                 outSum.close();
257         }
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";
260                 exit(1);
261         }
262         catch(...) {
263                 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
264                 exit(1);
265         }
266 }
267
268 /***********************************************************/
269 void ParsimonyCommand::getUserInput() {
270         try {
271         
272                 //create treemap
273                 tmap = new TreeMap();
274
275                 cout << "Please enter the number of groups you would like to analyze: ";
276                 cin >> numGroups;
277                         
278                 int num, count;
279                 count = 1;
280                 numEachGroup.resize(numGroups, 0);  
281                 
282                 for (int i = 1; i <= numGroups; i++) {
283                         cout << "Please enter the number of sequences in group " << i <<  ": ";
284                         cin >> num;
285                                 
286                         //set tmaps seqsPerGroup
287                         tmap->seqsPerGroup[toString(i)] = num;
288                         
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);
293                                 count++;
294                         }
295                 }
296                 
297                 //clears buffer so next command doesn't have error
298                 string s;       
299                 getline(cin, s);
300                 
301                 //save tmap for later
302                 globaldata->gTreemap = tmap;
303                 
304         }
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";
307                 exit(1);
308         }
309         catch(...) {
310                 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
311                 exit(1);
312         }
313 }
314 /***********************************************************/
315