]> git.donarmstrong.com Git - mothur.git/blob - parsimonycommand.cpp
421cac363feb5380787c8a52fe4e5ad2fe60c9f9
[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                         parsFileout = globaldata->getTreeFile() + "temp" + ".parsimony";
26                         sumFile = globaldata->getTreeFile() + ".psummary";
27                         openOutputFile(sumFile, outSum);
28                 }else { //user wants random distribution
29                         savetmap = globaldata->gTreemap;
30                         getUserInput();
31                         parsFile = randomtree;
32                         parsFileout = globaldata->getTreeFile() + "temp";
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                 counter = 0;
40
41         }
42         catch(exception& e) {
43                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
44                 exit(1);
45         }
46         catch(...) {
47                 cout << "An unknown error has occurred in the ParsimonyCommand class function ParsimonyCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
48                 exit(1);
49         }
50 }
51 /***********************************************************/
52 int ParsimonyCommand::execute() {
53         try {
54         
55                 //get pscore for users tree
56                 userData.resize(numComp,0);  //data = AB, AC, BC, ABC.
57                 randomData.resize(numComp,0);  //data = AB, AC, BC, ABC.
58                 rscoreFreq.resize(numComp);  
59                 uscoreFreq.resize(numComp);  
60                 rCumul.resize(numComp);  
61                 uCumul.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                                 userData = pars->getValues(T[i]);  //data = AB, AC, BC, ABC.
69                                 
70                                 //output scores for each combination
71                                 for(int k = 0; k < numComp; k++) {
72                                         //update uscoreFreq
73                                         it = uscoreFreq[k].find(userData[k]);
74                                         if (it == uscoreFreq[k].end()) {//new score
75                                                 uscoreFreq[k][userData[k]] = 1;
76                                         }else{ uscoreFreq[k][userData[k]]++; }
77                                         
78                                         //add users score to valid scores
79                                         validScores[userData[k]] = userData[k];
80                                         
81                                         //save score for summary file
82                                         userTreeScores[k].push_back(userData[k]);
83                                 }
84                         }
85                         
86                         //get pscores for random trees
87                         for (int j = 0; j < iters; j++) {
88                                 //create new tree with same num nodes and leaves as users
89                                 randT = new Tree();
90                                 //create random relationships between nodes
91                                 randT->assembleRandomTree();
92                                 //get pscore of random tree
93                                 randomData = pars->getValues(randT);
94                                 
95                                 for(int r = 0; r < numComp; r++) {
96                                         //add trees pscore to map of scores
97                                         it2 = rscoreFreq[r].find(randomData[r]);
98                                         if (it2 != rscoreFreq[r].end()) {//already have that score
99                                                 rscoreFreq[r][randomData[r]]++;
100                                         }else{//first time we have seen this score
101                                                 rscoreFreq[r][randomData[r]] = 1;
102                                         }
103                         
104                                         //add randoms score to validscores
105                                         validScores[randomData[r]] = randomData[r];
106                                 }
107                                 
108                                 delete randT;
109                         }
110                 }else {
111                         //get pscores for random trees
112                         for (int j = 0; j < iters; j++) {
113                                 //create new tree with same num nodes and leaves as users
114                                 randT = new Tree();
115                                 //create random relationships between nodes
116                                 randT->assembleRandomTree();
117                                 //get pscore of random tree
118                                 randomData = pars->getValues(randT);
119                                 
120                                 for(int r = 0; r < numComp; r++) {
121                                         //add trees pscore to map of scores
122                                         it2 = rscoreFreq[r].find(randomData[r]);
123                                         if (it2 != rscoreFreq[r].end()) {//already have that score
124                                                 rscoreFreq[r][randomData[r]]++;
125                                         }else{//first time we have seen this score
126                                                 rscoreFreq[r][randomData[r]] = 1;
127                                         }
128                         
129                                         //add randoms score to validscores
130                                         validScores[randomData[r]] = randomData[r];
131                                 }
132                                 
133                                 delete randT;
134                         }
135                 }
136                 
137                 for(int a = 0; a < numComp; a++) {
138                         float rcumul = 0.0000;
139                         float ucumul = 0.0000;
140                         //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
141                         for (it = validScores.begin(); it != validScores.end(); it++) { 
142                                 if (randomtree == "") {
143                                         it2 = uscoreFreq[a].find(it->first);
144                                         //user data has that score 
145                                         if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul+= it2->second;  }
146                                         else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
147                                         //make uCumul map
148                                         uCumul[a][it->first] = ucumul;
149                                 }
150                         
151                                 //make rscoreFreq map and rCumul
152                                 it2 = rscoreFreq[a].find(it->first);
153                                 //get percentage of random trees with that info
154                                 if (it2 != rscoreFreq[a].end()) {  rscoreFreq[a][it->first] /= iters; rcumul+= it2->second;  }
155                                 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
156                                 rCumul[a][it->first] = rcumul;
157                         }
158                         
159                         //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
160                         for (int h = 0; h < userTreeScores[a].size(); h++) {
161                                 UScoreSig[a].push_back(rCumul[a][userTreeScores[a][h]]);
162                         }
163                 }
164                 
165                 printParsimonyFile();
166                 if (randomtree == "") { printUSummaryFile(); }
167                 
168                 //reset globaldata's treemap if you just did random distrib
169                 if (randomtree != "") {
170                         //memory leak prevention
171                         if (globaldata->gTreemap != NULL) { delete globaldata->gTreemap;  }
172                         globaldata->gTreemap = savetmap;
173                 }
174                 
175                 //reset randomTree parameter to ""
176                 globaldata->setRandomTree("");
177                 //reset groups parameter
178                 globaldata->Groups.clear();  globaldata->setGroups("");
179                 
180                 return 0;
181                 
182         }
183         catch(exception& e) {
184                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
185                 exit(1);
186         }
187         catch(...) {
188                 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189                 exit(1);
190         }
191 }
192
193 /***********************************************************/
194 void ParsimonyCommand::printParsimonyFile() {
195         try {
196                 vector<double> data;
197                 
198                 //format output
199                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
200
201                 for(int a = 0; a < numComp; a++) {
202                         initFile(groupComb[a]);
203                         //print each line
204                         for (it = validScores.begin(); it != validScores.end(); it++) { 
205                                 if (randomtree == "") {
206                                         data.push_back(it->first);  data.push_back(uscoreFreq[a][it->first]); data.push_back(uCumul[a][it->first]); data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
207                                 }else{
208                                         data.push_back(it->first);  data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
209                                 }
210                                 output(data);
211                                 data.clear();
212                         } 
213                         resetFile();
214                 }
215                 
216                 out.close();
217                 inFile.close();
218                 remove(parsFileout.c_str());
219         }
220         catch(exception& e) {
221                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
222                 exit(1);
223         }
224         catch(...) {
225                 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
226                 exit(1);
227         }
228 }
229 /***********************************************************/
230 void ParsimonyCommand::printUSummaryFile() {
231         try {
232                 //column headers
233                 outSum << "Tree#" << '\t' << "Groups" << '\t'  <<  "ParsScore" << '\t' << "ParsSig" <<  endl;
234                 cout << "Tree#" << '\t' << "Groups" << '\t'  <<  "ParsScore" << '\t' << "ParsSig" <<  endl;
235                 
236                 //format output
237                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
238                 
239                 
240                 //print each line
241                 for (int i = 0; i< T.size(); i++) {
242                         for(int a = 0; a < numComp; a++) {
243                                 if (UScoreSig[a][i] > (1/(float)iters)) {
244                                         outSum << setprecision(6) << i+1 << '\t' << groupComb[a]  << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
245                                         cout << setprecision(6) << i+1 << '\t' << groupComb[a]  << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
246                                 }else {
247                                         outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length())  << '\t' << "<" << (1/float(iters)) << endl;
248                                         cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << "<" << (1/float(iters)) << endl;
249                                 }
250                         }
251                 }
252                 
253                 outSum.close();
254         }
255         catch(exception& e) {
256                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257                 exit(1);
258         }
259         catch(...) {
260                 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261                 exit(1);
262         }
263 }
264
265 /***********************************************************/
266 void ParsimonyCommand::getUserInput() {
267         try {
268         
269                 //create treemap
270                 tmap = new TreeMap();
271
272                 cout << "Please enter the number of groups you would like to analyze: ";
273                 cin >> numGroups;
274                         
275                 int num, count;
276                 count = 1;
277                 numEachGroup.resize(numGroups, 0);  
278                 
279                 for (int i = 1; i <= numGroups; i++) {
280                         cout << "Please enter the number of sequences in group " << i <<  ": ";
281                         cin >> num;
282                                 
283                         //set tmaps seqsPerGroup
284                         tmap->seqsPerGroup[toString(i)] = num;
285                         tmap->namesOfGroups.push_back(toString(i));
286                         
287                         //set tmaps namesOfSeqs
288                         for (int j = 0; j < num; j++) {
289                                 tmap->namesOfSeqs.push_back(toString(count));
290                                 tmap->treemap[toString(count)].groupname = toString(i);
291                                 count++;
292                         }
293                 }
294                 
295                 //clears buffer so next command doesn't have error
296                 string s;       
297                 getline(cin, s);
298                 
299                 //save tmap for later
300                 //memory leak prevention
301                 if (globaldata->gTreemap != NULL) { delete globaldata->gTreemap;  }
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
316 void ParsimonyCommand::setGroups() {
317         try {
318                 string allGroups = "";
319                 numGroups = 0;
320                 //if the user has not entered specific groups to analyze then do them all
321                 if (globaldata->Groups.size() != 0) {
322                         if (globaldata->Groups[0] != "all") {
323                                 //check that groups are valid
324                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
325                                         if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
326                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
327                                                 // erase the invalid group from globaldata->Groups
328                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
329                                         }
330                                 }
331                         
332                                 //if the user only entered invalid groups
333                                 if (globaldata->Groups.size() == 0) { 
334                                         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; 
335                                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
336                                                 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
337                                                 numGroups++;
338                                                 allGroups += tmap->namesOfGroups[i] + "-";
339                                         }
340                                         allGroups = allGroups.substr(0, allGroups.length()-1);
341                                 }else {
342                                         for (int i = 0; i < globaldata->Groups.size(); i++) {
343                                                 allGroups += globaldata->Groups[i] + "-";
344                                                 numGroups++;
345                                         }
346                                         allGroups = allGroups.substr(0, allGroups.length()-1);
347                                 }
348                         }else{//user has enter "all" and wants the default groups
349                                 globaldata->Groups.clear();
350                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
351                                         globaldata->Groups.push_back(tmap->namesOfGroups[i]);
352                                         numGroups++;
353                                         allGroups += tmap->namesOfGroups[i] + "-";
354                                 }
355                                 allGroups = allGroups.substr(0, allGroups.length()-1);
356                                 globaldata->setGroups("");
357                         }
358                 }else {
359                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
360                                 allGroups += tmap->namesOfGroups[i] + "-";
361                         }
362                         allGroups = allGroups.substr(0, allGroups.length()-1);
363                         numGroups = 1;
364                 }
365                 
366                 //calculate number of comparsions
367                 numComp = 0;
368                 for (int r=0; r<numGroups; r++) { 
369                         for (int l = r+1; l < numGroups; l++) {
370                                 groupComb.push_back(globaldata->Groups[r]+ "-" +globaldata->Groups[l]);
371                                 numComp++;
372                         }
373                 }
374                 
375                 //ABC
376                 if (numComp != 1) {
377                         groupComb.push_back(allGroups);
378                         numComp++;
379                 }
380                 
381         }
382         catch(exception& e) {
383                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
384                 exit(1);
385         }
386         catch(...) {
387                 cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
388                 exit(1);
389         }               
390
391 }
392 /*****************************************************************/
393
394 void ParsimonyCommand::initFile(string label){
395         try {
396                 if(counter != 0){
397                         openOutputFile(parsFileout, out);
398                         openInputFile(parsFile, inFile);
399
400                         string inputBuffer;
401                         getline(inFile, inputBuffer);
402                         
403                         if (randomtree == "") {
404                                 out <<  inputBuffer << '\t' << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
405                         }else {
406                                 out <<  inputBuffer << '\t' << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
407                         }
408                 }else{
409                         openOutputFile(parsFileout, out);
410                         //column headers
411                         if (randomtree == "") {
412                                 out << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
413                         }else {
414                                 out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
415                         }
416                 }
417
418                 out.setf(ios::fixed, ios::floatfield);
419                 out.setf(ios::showpoint);
420         }
421         catch(exception& e) {
422                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
423                 exit(1);
424         }
425         catch(...) {
426                 cout << "An unknown error has occurred in the ParsimonyCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
427                 exit(1);
428         }
429 }
430
431 /***********************************************************************/
432
433 void ParsimonyCommand::output(vector<double> data){
434         try {
435                 if(counter != 0){               
436                         string inputBuffer;
437                         getline(inFile, inputBuffer);
438                 
439                         if (randomtree == "") {
440                                 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
441                         }else{
442                                 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
443                         }
444                 }
445                 else{
446                         if (randomtree == "") {
447                                 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
448                         }else{
449                                 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
450                         }
451                 }
452
453         }
454         catch(exception& e) {
455                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
456                 exit(1);
457         }
458         catch(...) {
459                 cout << "An unknown error has occurred in the ParsimonyCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
460                 exit(1);
461         }
462 }
463
464 /***********************************************************************/
465
466 void ParsimonyCommand::resetFile(){
467         try {
468                 if(counter != 0){
469                         out.close();
470                         inFile.close();
471                 }
472                 else{
473                         out.close();
474                 }
475                 counter = 1;
476                 
477                 remove(parsFile.c_str());
478                 rename(parsFileout.c_str(), parsFile.c_str());
479         }
480         catch(exception& e) {
481                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
482                 exit(1);
483         }
484         catch(...) {
485                 cout << "An unknown error has occurred in the ParsimonyCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
486                 exit(1);
487         }       
488 }
489
490