]> git.donarmstrong.com Git - mothur.git/blob - parsimony.cpp
fixed memory leak in parsimony calculator and added progress bars to parsimony and...
[mothur.git] / parsimony.cpp
1 /*
2  *  parsimony.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 "parsimony.h"
11
12 /**************************************************************************************************/
13
14 EstOutput Parsimony::getValues(Tree* t) {
15         try {
16                 globaldata = GlobalData::getInstance();
17                 vector<string> groups;
18                 
19                 copyTree = new Tree();
20                 
21                 //if the users enters no groups then give them the score of all groups
22                 int numGroups = globaldata->Groups.size();
23                 
24                 //calculate number of comparsions
25                 int numComp = 0;
26                 for (int r=0; r<numGroups; r++) { 
27                         for (int l = r+1; l < numGroups; l++) {
28                                 numComp++;
29                         }
30                 }
31
32                 //numComp+1 for AB, AC, BC, ABC
33                 data.resize(numComp+1,0);
34                 
35                 int count = 0;
36                 for (int a=0; a<numGroups; a++) { 
37                         for (int l = a+1; l < numGroups; l++) {
38                                 int score = 0;
39                                 
40                                 //groups in this combo
41                                 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
42                                 
43                                 //copy users tree so that you can redo pgroups 
44                                 copyTree->getCopy(t);
45
46                                 //create pgroups that reflect the groups the user want to use
47                                 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
48                                         copyTree->tree[i].pGroups = (copyTree->mergeUserGroups(i, groups));
49                                 }
50                 
51                                 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
52                                         int lc = copyTree->tree[i].getLChild();
53                                         int rc = copyTree->tree[i].getRChild();
54                         
55                                         int iSize = copyTree->tree[i].pGroups.size();
56                                         int rcSize = copyTree->tree[rc].pGroups.size();
57                                         int lcSize = copyTree->tree[lc].pGroups.size();
58                 
59                                         //if isize are 0 then that branch is to be ignored
60                                         if (iSize == 0) { }
61                                         else if ((rcSize == 0) || (lcSize == 0)) { }
62                                         //if you have more groups than either of your kids then theres been a change.
63                                         else if(iSize > rcSize || iSize > lcSize){
64                                                 score++;
65                                         }
66                                 } 
67                                 
68                                 data[count] = score;
69                                 count++;
70                                 groups.clear();
71                         }
72                 }
73                 
74                 if (numComp != 1) {
75                         if (numGroups == 0) {
76                                 //get score for all users groups
77                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
78                                         groups.push_back(tmap->namesOfGroups[i]);
79                                 }
80                         }else {
81                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
82                                         groups.push_back(globaldata->Groups[i]);
83                                 }
84                         }
85                         
86                         //copy users tree so that you can redo pgroups 
87                         copyTree->getCopy(t);
88                         int score = 0;
89                 
90                         //create pgroups that reflect the groups the user want to use
91                         for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
92 //                              cout << i << "..." << endl;
93                                 copyTree->tree[i].pGroups = (copyTree->mergeUserGroups(i, groups));
94                         }
95                 
96 //                      map<string,int>::iterator it;
97                         
98                         for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
99                                 int lc = copyTree->tree[i].getLChild();
100                                 int rc = copyTree->tree[i].getRChild();
101                         
102                                 int iSize = copyTree->tree[i].pGroups.size();
103                                 int rcSize = copyTree->tree[rc].pGroups.size();
104                                 int lcSize = copyTree->tree[lc].pGroups.size();
105                                 
106 //                              cout << i+1 << '\t' << lc+1 << '\t' << rc+1 << ":\t";
107                                 
108 //                              for(it=copyTree->tree[i].pGroups.begin();it!=copyTree->tree[i].pGroups.end();it++){
109 //                                      cout << it->first << '\t';
110 //                              }
111                                 
112 //                              cout << " : " << iSize << '\t' << rcSize << '\t' << lcSize << '\t';
113                                         
114                                 //if isize are 0 then that branch is to be ignored
115                                 if (iSize == 0) { }
116                                 else if ((rcSize == 0) || (lcSize == 0)) { }
117                                 //if you have more groups than either of your kids then theres been a change.
118                                 else if(iSize > rcSize || iSize > lcSize){
119                                         score++;
120                                 }
121 //                              cout << score << endl;
122                         } 
123                 
124                         data[count] = score;
125                         string hold;
126 //                      cin >> hold;
127                 }
128                 
129                 delete copyTree;
130                 
131                 return data;
132         }
133         catch(exception& e) {
134                 cout << "Standard Error: " << e.what() << " has occurred in the Parsimony class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
135                 exit(1);
136         }
137         catch(...) {
138                 cout << "An unknown error has occurred in the Parsimony class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
139                 exit(1);
140         }
141
142 }
143
144 /**************************************************************************************************/
145