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