]> git.donarmstrong.com Git - mothur.git/blob - parsimonycommand.cpp
fixed valid parameters to include shared parameter for read.shared command.
[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 != "") { 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' << "Groups" << '\t'  <<  "ParsScore" << '\t' << "ParsSig" <<  endl;
230                 cout << "Tree#" << '\t' << "Groups" << '\t'  <<  "ParsScore" << '\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(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
241                                         cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\t' << UScoreSig[a][i] << endl;
242                                 }else {
243                                         outSum << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length())  << '\t' << "<" << (1/float(iters)) << endl;
244                                         cout << setprecision(6) << i+1 << '\t' << groupComb[a] << '\t' << userTreeScores[a][i] << setprecision(globaldata->getIters().length()) << '\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                                 globaldata->Groups.clear();
344                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
345                                         globaldata->Groups.push_back(tmap->namesOfGroups[i]);
346                                         numGroups++;
347                                         allGroups += tmap->namesOfGroups[i] + "-";
348                                 }
349                                 allGroups = allGroups.substr(0, allGroups.length()-1);
350                                 globaldata->setGroups("");
351                         }
352                 }else {
353                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
354                                 allGroups += tmap->namesOfGroups[i] + "-";
355                         }
356                         allGroups = allGroups.substr(0, allGroups.length()-1);
357                         numGroups = 1;
358                 }
359                 
360                 //calculate number of comparsions
361                 numComp = 0;
362                 for (int r=0; r<numGroups; r++) { 
363                         for (int l = r+1; l < numGroups; l++) {
364                                 groupComb.push_back(globaldata->Groups[r]+ "-" +globaldata->Groups[l]);
365                                 numComp++;
366                         }
367                 }
368                 
369                 //ABC
370                 if (numComp != 1) {
371                         groupComb.push_back(allGroups);
372                         numComp++;
373                 }
374                 
375         }
376         catch(exception& e) {
377                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378                 exit(1);
379         }
380         catch(...) {
381                 cout << "An unknown error has occurred in the ParsimonyCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
382                 exit(1);
383         }               
384
385 }
386 /*****************************************************************/
387
388 void ParsimonyCommand::initFile(string label){
389         try {
390                 if(counter != 0){
391                         openOutputFile(parsFileout, out);
392                         openInputFile(parsFile, inFile);
393
394                         string inputBuffer;
395                         getline(inFile, inputBuffer);
396                         
397                         if (randomtree == "") {
398                                 out <<  inputBuffer << '\t' << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
399                         }else {
400                                 out <<  inputBuffer << '\t' << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
401                         }
402                 }else{
403                         openOutputFile(parsFileout, out);
404                         //column headers
405                         if (randomtree == "") {
406                                 out << label + "Score" << '\t' << label + "UserFreq" << '\t' << label + "UserCumul" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
407                         }else {
408                                 out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
409                         }
410                 }
411
412                 out.setf(ios::fixed, ios::floatfield);
413                 out.setf(ios::showpoint);
414         }
415         catch(exception& e) {
416                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
417                 exit(1);
418         }
419         catch(...) {
420                 cout << "An unknown error has occurred in the ParsimonyCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
421                 exit(1);
422         }
423 }
424
425 /***********************************************************************/
426
427 void ParsimonyCommand::output(vector<double> data){
428         try {
429                 if(counter != 0){               
430                         string inputBuffer;
431                         getline(inFile, inputBuffer);
432                 
433                         if (randomtree == "") {
434                                 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
435                         }else{
436                                 out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
437                         }
438                 }
439                 else{
440                         if (randomtree == "") {
441                                 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << '\t' << data[3] << '\t' << data[4] << endl;
442                         }else{
443                                 out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
444                         }
445                 }
446
447         }
448         catch(exception& e) {
449                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
450                 exit(1);
451         }
452         catch(...) {
453                 cout << "An unknown error has occurred in the ParsimonyCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
454                 exit(1);
455         }
456 }
457
458 /***********************************************************************/
459
460 void ParsimonyCommand::resetFile(){
461         try {
462                 if(counter != 0){
463                         out.close();
464                         inFile.close();
465                 }
466                 else{
467                         out.close();
468                 }
469                 counter = 1;
470                 
471                 remove(parsFile.c_str());
472                 rename(parsFileout.c_str(), parsFile.c_str());
473         }
474         catch(exception& e) {
475                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
476                 exit(1);
477         }
478         catch(...) {
479                 cout << "An unknown error has occurred in the ParsimonyCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
480                 exit(1);
481         }       
482 }
483
484