]> git.donarmstrong.com Git - mothur.git/blob - parsimonycommand.cpp
parsimony command now uses groups, fixed bug with unweighted groups
[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                                 //update uscoreFreq
93                                 it = uscoreFreq.find(userData[0]);
94                                 if (it == uscoreFreq.end()) {//new score
95                                         uscoreFreq[userData[0]] = 1;
96                                 }else{ uscoreFreq[userData[0]]++; }
97                         
98                                 //add users score to valid scores
99                                 validScores[userData[0]] = userData[0];
100                                 
101                                 //save score for summary file
102                                 userTreeScores.push_back(userData[0]);
103                         
104                         }
105                         
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
109                                 randT = new Tree();
110                                 //create random relationships between nodes
111                                 randT->assembleRandomTree();
112                                 //get pscore of random tree
113                                 randomData = pars->getValues(randT);
114                         
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;
121                                 }
122                         
123                                 //add randoms score to validscores
124                                 validScores[randomData[0]] = randomData[0];
125                                 
126                                 //output info to pdistrib file
127                                 outDist << j+1 << '\t'<< '\t' << randomData[0] << endl;
128                                         
129                                 delete randT;
130                         }
131                 }else {
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
135                                 randT = new Tree();
136                                 //create random relationships between nodes
137                                 randT->assembleRandomTree();
138                                 //get pscore of random tree
139                                 randomData = pars->getValues(randT);
140                         
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;
147                                 }
148                         
149                                 //add randoms score to validscores
150                                 validScores[randomData[0]] = randomData[0];
151                                         
152                                 delete randT;
153                         }
154                 }
155                 
156                 float rcumul = 0.0000;
157                 float ucumul = 0.0000;
158                 
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
166                                 //make uCumul map
167                                 uCumul[it->first] = ucumul;
168                         }
169                         
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;
176                 }
177                 
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]]);
181                 }
182
183                 printParsimonyFile();
184                 printUSummaryFile();
185                 
186                 //reset globaldata's treemap if you just did random distrib
187                 if (randomtree != "") { globaldata->gTreemap = savetmap; }
188                 
189                 //reset randomTree parameter to ""
190                 globaldata->setRandomTree("");
191                 //reset groups parameter
192                 globaldata->Groups.clear();
193                 
194                 return 0;
195                 
196         }
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";
199                 exit(1);
200         }
201         catch(...) {
202                 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203                 exit(1);
204         }
205 }
206
207 /***********************************************************/
208 void ParsimonyCommand::printParsimonyFile() {
209         try {
210                 //column headers
211                 if (randomtree == "") {
212                         out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
213                 }else {
214                         out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
215                 }
216                 
217                 //format output
218                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
219                 
220                 //print each line
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; 
224                         }else{
225                                 out << setprecision(6) << it->first << '\t' << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;      
226                         }
227                 } 
228                 
229                 out.close();
230                 
231         }
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";
234                 exit(1);
235         }
236         catch(...) {
237                 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
238                 exit(1);
239         }
240 }
241 /***********************************************************/
242 void ParsimonyCommand::printUSummaryFile() {
243         try {
244                 //column headers
245                 outSum << "Tree#" << '\t'  <<  "ParsScore" << '\t' << '\t' << "ParsSig" <<  endl;
246                 
247                 //format output
248                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
249                 
250                 //print each line
251                 for (int i = 0; i< T.size(); i++) {
252                         outSum << setprecision(6) << i+1 << '\t' << '\t' << userTreeScores[i] << '\t' << UScoreSig[i] << endl; 
253                 }
254                 
255                 outSum.close();
256         }
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";
259                 exit(1);
260         }
261         catch(...) {
262                 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263                 exit(1);
264         }
265 }
266
267 /***********************************************************/
268 void ParsimonyCommand::getUserInput() {
269         try {
270         
271                 //create treemap
272                 tmap = new TreeMap();
273
274                 cout << "Please enter the number of groups you would like to analyze: ";
275                 cin >> numGroups;
276                         
277                 int num, count;
278                 count = 1;
279                 numEachGroup.resize(numGroups, 0);  
280                 
281                 for (int i = 1; i <= numGroups; i++) {
282                         cout << "Please enter the number of sequences in group " << i <<  ": ";
283                         cin >> num;
284                                 
285                         //set tmaps seqsPerGroup
286                         tmap->seqsPerGroup[toString(i)] = num;
287                         
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);
292                                 count++;
293                         }
294                 }
295                 
296                 //clears buffer so next command doesn't have error
297                 string s;       
298                 getline(cin, s);
299                 
300                 //save tmap for later
301                 globaldata->gTreemap = tmap;
302                 
303         }
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";
306                 exit(1);
307         }
308         catch(...) {
309                 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
310                 exit(1);
311         }
312 }
313 /***********************************************************/
314