]> git.donarmstrong.com Git - mothur.git/blob - parsimonycommand.cpp
minor bugs fixes and added line and label options to read.otu's parselist and shared...
[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                         openOutputFile(parsFile, out);
26                         sumFile = globaldata->getTreeFile() + ".psummary";
27                         openOutputFile(sumFile, outSum);
28                         distFile = globaldata->getTreeFile() + ".pdistrib";
29                         openOutputFile(distFile, outDist);
30
31                 }else { //user wants random distribution
32                         savetmap = globaldata->gTreemap;
33                         getUserInput();
34                         parsFile = randomtree + ".rd_parsimony";
35                         openOutputFile(parsFile, out);
36                 }
37                 
38                 convert(globaldata->getIters(), iters);  //how many random trees to generate
39                 pars = new Parsimony(tmap);
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(1,0);  //data[0] = pscore.
57                 randomData.resize(1,0);  //data[0] = pscore.
58                 
59                 //format output
60                 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
61                 outDist << "RandomTree#" << '\t' << "ParsScore" << endl;
62                 
63                 if (randomtree == "") {
64                         //get pscores for users trees
65                         for (int i = 0; i < T.size(); i++) {
66                                 cout << "Processing tree " << i+1 << endl;
67                                 userData = pars->getValues(T[i]);  //userData[0] = pscore
68                                 //update uscoreFreq
69                                 it = uscoreFreq.find(userData[0]);
70                                 if (it == uscoreFreq.end()) {//new score
71                                         uscoreFreq[userData[0]] = 1;
72                                 }else{ uscoreFreq[userData[0]]++; }
73                         
74                                 //add users score to valid scores
75                                 validScores[userData[0]] = userData[0];
76                                 
77                                 //save score for summary file
78                                 userTreeScores.push_back(userData[0]);
79                         
80                         }
81                         
82                         //get pscores for random trees
83                         for (int j = 0; j < iters; j++) {
84                                 //create new tree with same num nodes and leaves as users
85                                 randT = new Tree();
86                                 //create random relationships between nodes
87                                 randT->assembleRandomTree();
88                                 //get pscore of random tree
89                                 randomData = pars->getValues(randT);
90                         
91                                 //add trees pscore to map of scores
92                                 it2 = rscoreFreq.find(randomData[0]);
93                                 if (it2 != rscoreFreq.end()) {//already have that score
94                                         rscoreFreq[randomData[0]]++;
95                                 }else{//first time we have seen this score
96                                         rscoreFreq[randomData[0]] = 1;
97                                 }
98                         
99                                 //add randoms score to validscores
100                                 validScores[randomData[0]] = randomData[0];
101                                 
102                                 //output info to pdistrib file
103                                 outDist << j+1 << '\t'<< '\t' << randomData[0] << endl;
104                                         
105                                 delete randT;
106                         }
107                 }else {
108                         //get pscores for random trees
109                         for (int j = 0; j < iters; j++) {
110                                 //create new tree with same num nodes and leaves as users
111                                 randT = new Tree();
112                                 //create random relationships between nodes
113                                 randT->assembleRandomTree();
114                                 //get pscore of random tree
115                                 randomData = pars->getValues(randT);
116                         
117                                 //add trees pscore to map of scores
118                                 it2 = rscoreFreq.find(randomData[0]);
119                                 if (it2 != rscoreFreq.end()) {//already have that score
120                                         rscoreFreq[randomData[0]]++;
121                                 }else{//first time we have seen this score
122                                         rscoreFreq[randomData[0]] = 1;
123                                 }
124                         
125                                 //add randoms score to validscores
126                                 validScores[randomData[0]] = randomData[0];
127                                         
128                                 delete randT;
129                         }
130                 }
131                 
132                 float rcumul = 0.0000;
133                 float ucumul = 0.0000;
134                 
135                 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
136                 for (it = validScores.begin(); it != validScores.end(); it++) { 
137                         if (randomtree == "") {
138                                 it2 = uscoreFreq.find(it->first);
139                                 //user data has that score 
140                                 if (it2 != uscoreFreq.end()) { uscoreFreq[it->first] /= T.size(); ucumul+= it2->second;  }
141                                 else { uscoreFreq[it->first] = 0.0000; } //no user trees with that score
142                                 //make uCumul map
143                                 uCumul[it->first] = ucumul;
144                         }
145                         
146                         //make rscoreFreq map and rCumul
147                         it2 = rscoreFreq.find(it->first);
148                         //get percentage of random trees with that info
149                         if (it2 != rscoreFreq.end()) {  rscoreFreq[it->first] /= iters; rcumul+= it2->second;  }
150                         else { rscoreFreq[it->first] = 0.0000; } //no random trees with that score
151                         rCumul[it->first] = rcumul;
152                 }
153                 
154                 //find the signifigance of each user trees score when compared to the random trees and save for printing the summary file
155                 for (int h = 0; h < userTreeScores.size(); h++) {
156                         UScoreSig.push_back(rCumul[userTreeScores[h]]);
157                 }
158
159                 printParsimonyFile();
160                 printUSummaryFile();
161                 
162                 //reset globaldata's treemap if you just did random distrib
163                 if (randomtree != "") { globaldata->gTreemap = savetmap; }
164                 
165                 //reset randomTree parameter to ""
166                 globaldata->setRandomTree("");
167                 
168                 return 0;
169                 
170         }
171         catch(exception& e) {
172                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173                 exit(1);
174         }
175         catch(...) {
176                 cout << "An unknown error has occurred in the ParsimonyCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177                 exit(1);
178         }
179 }
180
181 /***********************************************************/
182 void ParsimonyCommand::printParsimonyFile() {
183         try {
184                 //column headers
185                 if (randomtree == "") {
186                         out << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
187                 }else {
188                         out << "Score" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
189                 }
190                 
191                 //format output
192                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
193                 
194                 //print each line
195                 for (it = validScores.begin(); it != validScores.end(); it++) { 
196                         if (randomtree == "") {
197                                 out << setprecision(6) << it->first << '\t' << '\t' << uscoreFreq[it->first] << '\t' << uCumul[it->first] << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl; 
198                         }else{
199                                 out << setprecision(6) << it->first << '\t' << '\t' << rscoreFreq[it->first] << '\t' << rCumul[it->first] << endl;      
200                         }
201                 } 
202                 
203                 out.close();
204                 
205         }
206         catch(exception& e) {
207                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208                 exit(1);
209         }
210         catch(...) {
211                 cout << "An unknown error has occurred in the ParsimonyCommand class function printParsimonyFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
212                 exit(1);
213         }
214 }
215 /***********************************************************/
216 void ParsimonyCommand::printUSummaryFile() {
217         try {
218                 //column headers
219                 outSum << "Tree#" << '\t'  <<  "ParsScore" << '\t' << '\t' << "ParsSig" <<  endl;
220                 
221                 //format output
222                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
223                 
224                 //print each line
225                 for (int i = 0; i< T.size(); i++) {
226                         outSum << setprecision(6) << i+1 << '\t' << '\t' << userTreeScores[i] << '\t' << UScoreSig[i] << endl; 
227                 }
228                 
229                 outSum.close();
230         }
231         catch(exception& e) {
232                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
233                 exit(1);
234         }
235         catch(...) {
236                 cout << "An unknown error has occurred in the ParsimonyCommand class function printUSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237                 exit(1);
238         }
239 }
240
241 /***********************************************************/
242 void ParsimonyCommand::getUserInput() {
243         try {
244         
245                 //create treemap
246                 tmap = new TreeMap();
247
248                 cout << "Please enter the number of groups you would like to analyze: ";
249                 cin >> numGroups;
250                         
251                 int num, count;
252                 count = 1;
253                 numEachGroup.resize(numGroups, 0);  
254                 
255                 for (int i = 1; i <= numGroups; i++) {
256                         cout << "Please enter the number of sequences in group " << i <<  ": ";
257                         cin >> num;
258                                 
259                         //set tmaps seqsPerGroup
260                         tmap->seqsPerGroup[toString(i)] = num;
261                         
262                         //set tmaps namesOfSeqs
263                         for (int j = 0; j < num; j++) {
264                                 tmap->namesOfSeqs.push_back(toString(count));
265                                 tmap->treemap[toString(count)].groupname = toString(i);
266                                 count++;
267                         }
268                 }
269                 
270                 //clears buffer so next command doesn't have error
271                 string s;       
272                 getline(cin, s);
273                 
274                 //save tmap for later
275                 globaldata->gTreemap = tmap;
276                 
277         }
278         catch(exception& e) {
279                 cout << "Standard Error: " << e.what() << " has occurred in the ParsimonyCommand class Function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
280                 exit(1);
281         }
282         catch(...) {
283                 cout << "An unknown error has occurred in the ParsimonyCommand class function getUserInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
284                 exit(1);
285         }
286 }
287 /***********************************************************/
288