]> git.donarmstrong.com Git - mothur.git/blob - parsimonycommand.cpp
4529f777358ece0b03a6f53d522a54bd7698ac87
[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                                 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                                         //update uscoreFreq
74                                         it = uscoreFreq[k].find(userData[k]);
75                                         if (it == uscoreFreq[k].end()) {//new score
76                                                 uscoreFreq[k][userData[k]] = 1;
77                                         }else{ uscoreFreq[k][userData[k]]++; }
78                                         
79                                         //add users score to valid scores
80                                         validScores[userData[k]] = userData[k];
81                                         
82                                         //save score for summary file
83                                         userTreeScores[k].push_back(userData[k]);
84                                 }
85                         }
86                         
87                         //get pscores for random trees
88                         for (int j = 0; j < iters; j++) {
89                                 //create new tree with same num nodes and leaves as users
90                                 randT = new Tree();
91                                 //create random relationships between nodes
92                                 randT->assembleRandomTree();
93                                 //get pscore of random tree
94                                 randomData = pars->getValues(randT);
95                                 
96                                 for(int r = 0; r < numComp; r++) {
97                                         //add trees pscore to map of scores
98                                         it2 = rscoreFreq[r].find(randomData[r]);
99                                         if (it2 != rscoreFreq[r].end()) {//already have that score
100                                                 rscoreFreq[r][randomData[r]]++;
101                                         }else{//first time we have seen this score
102                                                 rscoreFreq[r][randomData[r]] = 1;
103                                         }
104                         
105                                         //add randoms score to validscores
106                                         validScores[randomData[r]] = randomData[r];
107                                 }
108                                 
109                                 delete randT;
110                         }
111                 }else {
112                         //get pscores for random trees
113                         for (int j = 0; j < iters; j++) {
114                                 //create new tree with same num nodes and leaves as users
115                                 randT = new Tree();
116                                 //create random relationships between nodes
117                                 randT->assembleRandomTree();
118                                 //get pscore of random tree
119                                 randomData = pars->getValues(randT);
120                                 
121                                 for(int r = 0; r < numComp; r++) {
122                                         //add trees pscore to map of scores
123                                         it2 = rscoreFreq[r].find(randomData[r]);
124                                         if (it2 != rscoreFreq[r].end()) {//already have that score
125                                                 rscoreFreq[r][randomData[r]]++;
126                                         }else{//first time we have seen this score
127                                                 rscoreFreq[r][randomData[r]] = 1;
128                                         }
129                         
130                                         //add randoms score to validscores
131                                         validScores[randomData[r]] = randomData[r];
132                                 }
133                                 
134                                 delete randT;
135                         }
136                 }
137                 
138                 for(int a = 0; a < numComp; a++) {
139                         float rcumul = 0.0000;
140                         float ucumul = 0.0000;
141                         //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
142                         for (it = validScores.begin(); it != validScores.end(); it++) { 
143                                 if (randomtree == "") {
144                                         it2 = uscoreFreq[a].find(it->first);
145                                         //user data has that score 
146                                         if (it2 != uscoreFreq[a].end()) { uscoreFreq[a][it->first] /= T.size(); ucumul+= it2->second;  }
147                                         else { uscoreFreq[a][it->first] = 0.0000; } //no user trees with that score
148                                         //make uCumul map
149                                         uCumul[a][it->first] = ucumul;
150                                 }
151                         
152                                 //make rscoreFreq map and rCumul
153                                 it2 = rscoreFreq[a].find(it->first);
154                                 //get percentage of random trees with that info
155                                 if (it2 != rscoreFreq[a].end()) {  rscoreFreq[a][it->first] /= iters; rcumul+= it2->second;  }
156                                 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
157                                 rCumul[a][it->first] = rcumul;
158                         }
159                         
160                         //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
161                         for (int h = 0; h < userTreeScores[a].size(); h++) {
162                                 UScoreSig[a].push_back(rCumul[a][userTreeScores[a][h]]);
163                         }
164                 }
165                 
166                 printParsimonyFile();
167                 if (randomtree == "") { printUSummaryFile(); }
168                 
169                 //reset globaldata's treemap if you just did random distrib
170                 if (randomtree != "") { globaldata->gTreemap = savetmap; }
171                 
172                 //reset randomTree parameter to ""
173                 globaldata->setRandomTree("");
174                 //reset groups parameter
175                 globaldata->Groups.clear();  globaldata->setGroups("");
176                 
177                 return 0;
178                 
179         }
180         catch(exception& e) {
181                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182                 exit(1);
183         }
184         catch(...) {
185                 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
186                 exit(1);
187         }
188 }
189
190 /***********************************************************/
191 void ParsimonyCommand::printParsimonyFile() {
192         try {
193                 vector<double> data;
194                 
195                 //format output
196                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
197
198                 for(int a = 0; a < numComp; a++) {
199                         initFile(groupComb[a]);
200                         //print each line
201                         for (it = validScores.begin(); it != validScores.end(); it++) { 
202                                 if (randomtree == "") {
203                                         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]); 
204                                 }else{
205                                         data.push_back(it->first);  data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
206                                 }
207                                 output(data);
208                                 data.clear();
209                         } 
210                         resetFile();
211                 }
212                 
213                 out.close();
214                 inFile.close();
215                 remove(parsFileout.c_str());
216         }
217         catch(exception& e) {
218                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
219                 exit(1);
220         }
221         catch(...) {
222                 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
223                 exit(1);
224         }
225 }
226 /***********************************************************/
227 void ParsimonyCommand::printUSummaryFile() {
228         try {
229                 //column headers
230                 outSum << "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                                 outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << '\t' << UScoreSig[a][i] << endl;
240                                 cout << 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                 if (numComp != 1) {
361                         groupComb.push_back(allGroups);
362                         numComp++;
363                 }
364                 
365         }
366         catch(exception& e) {
367                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
368                 exit(1);
369         }
370         catch(...) {
371                 cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
372                 exit(1);
373         }               
374
375 }
376 /*****************************************************************/
377
378 void ParsimonyCommand::initFile(string label){
379         try {
380                 if(counter != 0){
381                         openOutputFile(parsFileout, out);
382                         openInputFile(parsFile, inFile);
383
384                         string inputBuffer;
385                         getline(inFile, inputBuffer);
386                         
387                         if (randomtree == "") {
388                                 out <<  inputBuffer << '\t' << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
389                         }else {
390                                 out <<  inputBuffer << '\t' << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
391                         }
392                 }else{
393                         openOutputFile(parsFileout, out);
394                         //column headers
395                         if (randomtree == "") {
396                                 out << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
397                         }else {
398                                 out << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
399                         }
400                 }
401
402                 out.setf(ios::fixed, ios::floatfield);
403                 out.setf(ios::showpoint);
404         }
405         catch(exception& e) {
406                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
407                 exit(1);
408         }
409         catch(...) {
410                 cout << "An unknown error has occurred in the ParsimonyCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
411                 exit(1);
412         }
413 }
414
415 /***********************************************************************/
416
417 void ParsimonyCommand::output(vector<double> data){
418         try {
419                 if(counter != 0){               
420                         string inputBuffer;
421                         getline(inFile, inputBuffer);
422                 
423                         if (randomtree == "") {
424                                 out << inputBuffer << '\t' << setprecision(6) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
425                         }else{
426                                 out << inputBuffer << '\t' << setprecision(6) << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
427                         }
428                 }
429                 else{
430                         if (randomtree == "") {
431                                 out << setprecision(6) << data[0] << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
432                         }else{
433                                 out << setprecision(6) << data[0] << '\t' << data[1] << '\t' << data[2] << endl;
434                         }
435                 }
436
437         }
438         catch(exception& e) {
439                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
440                 exit(1);
441         }
442         catch(...) {
443                 cout << "An unknown error has occurred in the ParsimonyCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
444                 exit(1);
445         }
446 }
447
448 /***********************************************************************/
449
450 void ParsimonyCommand::resetFile(){
451         try {
452                 if(counter != 0){
453                         out.close();
454                         inFile.close();
455                 }
456                 else{
457                         out.close();
458                 }
459                 counter = 1;
460                 
461                 remove(parsFile.c_str());
462                 rename(parsFileout.c_str(), parsFile.c_str());
463         }
464         catch(exception& e) {
465                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
466                 exit(1);
467         }
468         catch(...) {
469                 cout << "An unknown error has occurred in the ParsimonyCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
470                 exit(1);
471         }       
472 }
473
474