]> git.donarmstrong.com Git - mothur.git/blob - parsimonycommand.cpp
parsimony with 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                 }else { //user wants random distribution
29                         savetmap = globaldata->gTreemap;
30                         getUserInput();
31                         parsFile = randomtree + ".rd_parsimony";
32                         openOutputFile(parsFile, out);
33                 }
34                 
35                 //set users groups to analyze
36                 setGroups();
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(numComp,0);  //data = AB, AC, BC, ABC.
56                 randomData.resize(numComp,0);  //data = AB, AC, BC, ABC.
57                 rscoreFreq.resize(numComp);  
58                 uscoreFreq.resize(numComp);  
59                 rCumul.resize(numComp);  
60                 uCumul.resize(numComp);  
61                 validScores.resize(numComp); 
62                 userTreeScores.resize(numComp);  
63                 UScoreSig.resize(numComp); 
64                                 
65                 if (randomtree == "") {
66                         //get pscores for users trees
67                         for (int i = 0; i < T.size(); i++) {
68                                 cout << "Processing tree " << i+1 << endl;
69                                 userData = pars->getValues(T[i]);  //data = AB, AC, BC, ABC.
70                                 
71                                 //output scores for each combination
72                                 for(int k = 0; k < numComp; k++) {
73                                         cout << "Tree " << i+1 << " Combination " << groupComb[k] << " parsimony score = " << userData[k] << endl;
74                                         //update uscoreFreq
75                                         it = uscoreFreq[k].find(userData[k]);
76                                         if (it == uscoreFreq[k].end()) {//new score
77                                                 uscoreFreq[k][userData[k]] = 1;
78                                         }else{ uscoreFreq[k][userData[k]]++; }
79                                         
80                                         //add users score to valid scores
81                                         validScores[k][userData[k]] = userData[k];
82                                         
83                                         //save score for summary file
84                                         userTreeScores[k].push_back(userData[k]);
85                                 }
86                         }
87                         
88                         //get pscores for random trees
89                         for (int j = 0; j < iters; j++) {
90                                 //create new tree with same num nodes and leaves as users
91                                 randT = new Tree();
92                                 //create random relationships between nodes
93                                 randT->assembleRandomTree();
94                                 //get pscore of random tree
95                                 randomData = pars->getValues(randT);
96                                 
97                                 for(int r = 0; r < numComp; r++) {
98                                         //add trees pscore to map of scores
99                                         it2 = rscoreFreq[r].find(randomData[r]);
100                                         if (it2 != rscoreFreq[r].end()) {//already have that score
101                                                 rscoreFreq[r][randomData[r]]++;
102                                         }else{//first time we have seen this score
103                                                 rscoreFreq[r][randomData[r]] = 1;
104                                         }
105                         
106                                         //add randoms score to validscores
107                                         validScores[r][randomData[r]] = randomData[r];
108                                 }
109                                 
110                                 delete randT;
111                         }
112                 }else {
113                         //get pscores for random trees
114                         for (int j = 0; j < iters; j++) {
115                                 //create new tree with same num nodes and leaves as users
116                                 randT = new Tree();
117                                 //create random relationships between nodes
118                                 randT->assembleRandomTree();
119                                 //get pscore of random tree
120                                 randomData = pars->getValues(randT);
121                                 
122                                 for(int r = 0; r < numComp; r++) {
123                                         //add trees pscore to map of scores
124                                         it2 = rscoreFreq[r].find(randomData[r]);
125                                         if (it2 != rscoreFreq[r].end()) {//already have that score
126                                                 rscoreFreq[r][randomData[r]]++;
127                                         }else{//first time we have seen this score
128                                                 rscoreFreq[r][randomData[r]] = 1;
129                                         }
130                         
131                                         //add randoms score to validscores
132                                         validScores[r][randomData[r]] = randomData[r];
133                                 }
134                                 
135                                 delete randT;
136                         }
137                 }
138                 
139                 float rcumul = 0.0000;
140                 float ucumul = 0.0000;
141                 
142                 for(int a = 0; a < numComp; a++) {
143                 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
144                         for (it = validScores[a].begin(); it != validScores[a].end(); it++) { 
145                                 if (randomtree == "") {
146                                         it2 = uscoreFreq[a].find(it->first);
147                                         //user data has that score 
148                                         if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul+= it2->second;  }
149                                         else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
150                                         //make uCumul map
151                                         uCumul[a][it->first] = ucumul-a;
152                                 }
153                         
154                                 //make rscoreFreq map and rCumul
155                                 it2 = rscoreFreq[a].find(it->first);
156                                 //get percentage of random trees with that info
157                                 if (it2 != rscoreFreq[a].end()) {  rscoreFreq[a][it->first] /= iters; rcumul+= it2->second;  }
158                                 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
159                                 rCumul[a][it->first] = rcumul-a;
160                         }
161                         
162                         //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
163                         for (int h = 0; h < userTreeScores[a].size(); h++) {
164                                 UScoreSig[a].push_back(rCumul[a][userTreeScores[a][h]]);
165                         }
166                 }
167                 
168                 printParsimonyFile();
169                 if (randomtree != "") { printUSummaryFile(); }
170                 
171                 //reset globaldata's treemap if you just did random distrib
172                 if (randomtree == "") { globaldata->gTreemap = savetmap; }
173                 
174                 //reset randomTree parameter to ""
175                 globaldata->setRandomTree("");
176                 //reset groups parameter
177                 globaldata->Groups.clear();  globaldata->setGroups("");
178                 
179                 return 0;
180                 
181         }
182         catch(exception& e) {
183                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184                 exit(1);
185         }
186         catch(...) {
187                 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188                 exit(1);
189         }
190 }
191
192 /***********************************************************/
193 void ParsimonyCommand::printParsimonyFile() {
194         try {
195                 //column headers
196                 if (randomtree == "") {
197                         out << "Comb" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
198                 }else {
199                         out << "Comb" << '\t' << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
200                 }
201                 
202                 //format output
203                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
204                 
205                 for(int a = 0; a < numComp; a++) {
206                         //print each line
207                         for (it = validScores[a].begin(); it != validScores[a].end(); it++) { 
208                                 if (randomtree == "") {
209                                         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; 
210                                 }else{
211                                         out << setprecision(6) << groupComb[a] << '\t' << it->first << '\t' << '\t' << rscoreFreq[a][it->first] << '\t' << rCumul[a][it->first] << endl;        
212                                 }
213                         } 
214                 }
215                 out.close();
216                 
217         }
218         catch(exception& e) {
219                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
220                 exit(1);
221         }
222         catch(...) {
223                 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
224                 exit(1);
225         }
226 }
227 /***********************************************************/
228 void ParsimonyCommand::printUSummaryFile() {
229         try {
230                 //column headers
231                 outSum << "Tree#" << '\t' << "Comb" << '\t'  <<  "ParsScore" << '\t' << '\t' << "ParsSig" <<  endl;
232                 
233                 //format output
234                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
235                 
236                 
237                 //print each line
238                 for (int i = 0; i< T.size(); i++) {
239                         for(int a = 0; a < numComp; a++) {
240                                 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl;
241                         }
242                 }
243                 
244                 outSum.close();
245         }
246         catch(exception& e) {
247                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248                 exit(1);
249         }
250         catch(...) {
251                 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
252                 exit(1);
253         }
254 }
255
256 /***********************************************************/
257 void ParsimonyCommand::getUserInput() {
258         try {
259         
260                 //create treemap
261                 tmap = new TreeMap();
262
263                 cout << "Please enter the number of groups you would like to analyze: ";
264                 cin >> numGroups;
265                         
266                 int num, count;
267                 count = 1;
268                 numEachGroup.resize(numGroups, 0);  
269                 
270                 for (int i = 1; i <= numGroups; i++) {
271                         cout << "Please enter the number of sequences in group " << i <<  ": ";
272                         cin >> num;
273                                 
274                         //set tmaps seqsPerGroup
275                         tmap->seqsPerGroup[toString(i)] = num;
276                         tmap->namesOfGroups.push_back(toString(i));
277                         
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);
282                                 count++;
283                         }
284                 }
285                 
286                 //clears buffer so next command doesn't have error
287                 string s;       
288                 getline(cin, s);
289                 
290                 //save tmap for later
291                 globaldata->gTreemap = tmap;
292                 
293         }
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";
296                 exit(1);
297         }
298         catch(...) {
299                 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300                 exit(1);
301         }
302 }
303 /***********************************************************/
304
305 void ParsimonyCommand::setGroups() {
306         try {
307                 string allGroups = "";
308                 numGroups = 0;
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);
318                                         }
319                                 }
320                         
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]);
326                                                 numGroups++;
327                                                 allGroups += tmap->namesOfGroups[i];
328                                         }
329                                 }else {
330                                         for (int i = 0; i < globaldata->Groups.size(); i++) {
331                                                 allGroups += tmap->namesOfGroups[i];
332                                                 numGroups++;
333                                         }
334                                 }
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]);
338                                         numGroups++;
339                                         allGroups += tmap->namesOfGroups[i];
340                                 }
341                                 globaldata->setGroups("");
342                         }
343                 }else {
344                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
345                                 allGroups += tmap->namesOfGroups[i];
346                         }
347                         numGroups = 1;
348                 }
349                 
350                 //calculate number of comparsions
351                 numComp = 0;
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]);
355                                 numComp++;
356                         }
357                 }
358                 
359                 //ABC
360                 groupComb.push_back(allGroups);
361                 numComp++;
362                 
363         }
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";
366                 exit(1);
367         }
368         catch(...) {
369                 cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
370                 exit(1);
371         }               
372
373 }
374 /*****************************************************************/
375
376