]> git.donarmstrong.com Git - mothur.git/blob - parsimonycommand.cpp
474f732ef9b3c448e6bfe27753df7d59c4dec300
[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 + ".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                 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 != "") { globaldata->gTreemap = savetmap; }
170                 
171                 //reset randomTree parameter to ""
172                 globaldata->setRandomTree("");
173                 //reset groups parameter
174                 globaldata->Groups.clear();  globaldata->setGroups("");
175                 
176                 return 0;
177                 
178         }
179         catch(exception& e) {
180                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
181                 exit(1);
182         }
183         catch(...) {
184                 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
185                 exit(1);
186         }
187 }
188
189 /***********************************************************/
190 void ParsimonyCommand::printParsimonyFile() {
191         try {
192                 vector<double> data;
193                 
194                 //format output
195                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
196
197                 for(int a = 0; a < numComp; a++) {
198                         initFile(groupComb[a]);
199                         //print each line
200                         for (it = validScores.begin(); it != validScores.end(); it++) { 
201                                 if (randomtree == "") {
202                                         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]); 
203                                 }else{
204                                         data.push_back(it->first);  data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
205                                 }
206                                 output(data);
207                                 data.clear();
208                         } 
209                         resetFile();
210                 }
211                 
212                 out.close();
213                 inFile.close();
214                 remove(parsFileout.c_str());
215         }
216         catch(exception& e) {
217                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
218                 exit(1);
219         }
220         catch(...) {
221                 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
222                 exit(1);
223         }
224 }
225 /***********************************************************/
226 void ParsimonyCommand::printUSummaryFile() {
227         try {
228                 //column headers
229                 outSum << "Tree#" << '\t' << "Comb" << '\t'  <<  "ParsScore" << '\t' << '\t' << "ParsSig" <<  endl;
230                 cout << "Tree#" << '\t' << "Comb" << '\t'  <<  "ParsScore" << '\t' << '\t' << "ParsSig" <<  endl;
231                 
232                 //format output
233                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
234                 
235                 
236                 //print each line
237                 for (int i = 0; i< T.size(); i++) {
238                         for(int a = 0; a < numComp; a++) {
239                                 if (UScoreSig[a][i] > (1/(float)iters)) {
240                                         outSum << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl;
241                                         cout << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl;
242                                 }else {
243                                         outSum << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << "<" << (1/float(iters)) << endl;
244                                         cout << setprecision(globaldata->getIters().length()) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << "<" << (1/float(iters)) << endl;
245                                 }
246                         }
247                 }
248                 
249                 outSum.close();
250         }
251         catch(exception& e) {
252                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
253                 exit(1);
254         }
255         catch(...) {
256                 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257                 exit(1);
258         }
259 }
260
261 /***********************************************************/
262 void ParsimonyCommand::getUserInput() {
263         try {
264         
265                 //create treemap
266                 tmap = new TreeMap();
267
268                 cout << "Please enter the number of groups you would like to analyze: ";
269                 cin >> numGroups;
270                         
271                 int num, count;
272                 count = 1;
273                 numEachGroup.resize(numGroups, 0);  
274                 
275                 for (int i = 1; i <= numGroups; i++) {
276                         cout << "Please enter the number of sequences in group " << i <<  ": ";
277                         cin >> num;
278                                 
279                         //set tmaps seqsPerGroup
280                         tmap->seqsPerGroup[toString(i)] = num;
281                         tmap->namesOfGroups.push_back(toString(i));
282                         
283                         //set tmaps namesOfSeqs
284                         for (int j = 0; j < num; j++) {
285                                 tmap->namesOfSeqs.push_back(toString(count));
286                                 tmap->treemap[toString(count)].groupname = toString(i);
287                                 count++;
288                         }
289                 }
290                 
291                 //clears buffer so next command doesn't have error
292                 string s;       
293                 getline(cin, s);
294                 
295                 //save tmap for later
296                 globaldata->gTreemap = tmap;
297                 
298         }
299         catch(exception& e) {
300                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
301                 exit(1);
302         }
303         catch(...) {
304                 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
305                 exit(1);
306         }
307 }
308 /***********************************************************/
309
310 void ParsimonyCommand::setGroups() {
311         try {
312                 string allGroups = "";
313                 numGroups = 0;
314                 //if the user has not entered specific groups to analyze then do them all
315                 if (globaldata->Groups.size() != 0) {
316                         if (globaldata->Groups[0] != "all") {
317                                 //check that groups are valid
318                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
319                                         if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
320                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
321                                                 // erase the invalid group from globaldata->Groups
322                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
323                                         }
324                                 }
325                         
326                                 //if the user only entered invalid groups
327                                 if (globaldata->Groups.size() == 0) { 
328                                         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; 
329                                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
330                                                 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
331                                                 numGroups++;
332                                                 allGroups += tmap->namesOfGroups[i] + "-";
333                                         }
334                                         allGroups = allGroups.substr(0, allGroups.length()-1);
335                                 }else {
336                                         for (int i = 0; i < globaldata->Groups.size(); i++) {
337                                                 allGroups += globaldata->Groups[i] + "-";
338                                                 numGroups++;
339                                         }
340                                         allGroups = allGroups.substr(0, allGroups.length()-1);
341                                 }
342                         }else{//user has enter "all" and wants the default groups
343                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
344                                         globaldata->Groups.push_back(tmap->namesOfGroups[i]);
345                                         numGroups++;
346                                         allGroups += tmap->namesOfGroups[i] + "-";
347                                 }
348                                 allGroups = allGroups.substr(0, allGroups.length()-1);
349                                 globaldata->setGroups("");
350                         }
351                 }else {
352                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
353                                 allGroups += tmap->namesOfGroups[i] + "-";
354                         }
355                         allGroups = allGroups.substr(0, allGroups.length()-1);
356                         numGroups = 1;
357                 }
358                 
359                 //calculate number of comparsions
360                 numComp = 0;
361                 for (int r=0; r<numGroups; r++) { 
362                         for (int l = r+1; l < numGroups; l++) {
363                                 groupComb.push_back(globaldata->Groups[r]+ "-" +globaldata->Groups[l]);
364                                 numComp++;
365                         }
366                 }
367                 
368                 //ABC
369                 if (numComp != 1) {
370                         groupComb.push_back(allGroups);
371                         numComp++;
372                 }
373                 
374         }
375         catch(exception& e) {
376                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
377                 exit(1);
378         }
379         catch(...) {
380                 cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
381                 exit(1);
382         }               
383
384 }
385 /*****************************************************************/
386
387 void ParsimonyCommand::initFile(string label){
388         try {
389                 if(counter != 0){
390                         openOutputFile(parsFileout, out);
391                         openInputFile(parsFile, inFile);
392
393                         string inputBuffer;
394                         getline(inFile, inputBuffer);
395                         
396                         if (randomtree == "") {
397                                 out <<  inputBuffer << '\t' << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
398                         }else {
399                                 out <<  inputBuffer << '\t' << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
400                         }
401                 }else{
402                         openOutputFile(parsFileout, out);
403                         //column headers
404                         if (randomtree == "") {
405                                 out << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
406                         }else {
407                                 out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
408                         }
409                 }
410
411                 out.setf(ios::fixed, ios::floatfield);
412                 out.setf(ios::showpoint);
413         }
414         catch(exception& e) {
415                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
416                 exit(1);
417         }
418         catch(...) {
419                 cout << "An unknown error has occurred in the ParsimonyCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
420                 exit(1);
421         }
422 }
423
424 /***********************************************************************/
425
426 void ParsimonyCommand::output(vector<double> data){
427         try {
428                 if(counter != 0){               
429                         string inputBuffer;
430                         getline(inFile, inputBuffer);
431                 
432                         if (randomtree == "") {
433                                 out << inputBuffer << '\t' << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
434                         }else{
435                                 out << inputBuffer << '\t' << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
436                         }
437                 }
438                 else{
439                         if (randomtree == "") {
440                                 out << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
441                         }else{
442                                 out << setprecision(globaldata->getIters().length()) << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
443                         }
444                 }
445
446         }
447         catch(exception& e) {
448                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
449                 exit(1);
450         }
451         catch(...) {
452                 cout << "An unknown error has occurred in the ParsimonyCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
453                 exit(1);
454         }
455 }
456
457 /***********************************************************************/
458
459 void ParsimonyCommand::resetFile(){
460         try {
461                 if(counter != 0){
462                         out.close();
463                         inFile.close();
464                 }
465                 else{
466                         out.close();
467                 }
468                 counter = 1;
469                 
470                 remove(parsFile.c_str());
471                 rename(parsFileout.c_str(), parsFile.c_str());
472         }
473         catch(exception& e) {
474                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
475                 exit(1);
476         }
477         catch(...) {
478                 cout << "An unknown error has occurred in the ParsimonyCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
479                 exit(1);
480         }       
481 }
482
483