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