]> git.donarmstrong.com Git - mothur.git/blob - parsimony.cpp
added checks for ^C to quit command instead of program
[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                                 
53                                         if (m->control_pressed) { return data; }
54                                         
55                                         int lc = copyTree->tree[i].getLChild();
56                                         int rc = copyTree->tree[i].getRChild();
57                         
58                                         int iSize = copyTree->tree[i].pGroups.size();
59                                         int rcSize = copyTree->tree[rc].pGroups.size();
60                                         int lcSize = copyTree->tree[lc].pGroups.size();
61                 
62                                         //if isize are 0 then that branch is to be ignored
63                                         if (iSize == 0) { }
64                                         else if ((rcSize == 0) || (lcSize == 0)) { }
65                                         //if you have more groups than either of your kids then theres been a change.
66                                         else if(iSize > rcSize || iSize > lcSize){
67                                                 score++;
68                                         }
69                                 } 
70                                 
71                                 data[count] = score;
72                                 count++;
73                                 groups.clear();
74                         }
75                 }
76                 
77                 if (numComp != 1) {
78                         if (numGroups == 0) {
79                                 //get score for all users groups
80                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
81                                         if (tmap->namesOfGroups[i] != "xxx") {
82                                                 groups.push_back(tmap->namesOfGroups[i]);
83                                         }
84                                 }
85                         }else {
86                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
87                                         groups.push_back(globaldata->Groups[i]);
88                                 }
89                         }
90                         
91                         //copy users tree so that you can redo pgroups 
92                         copyTree->getCopy(t);
93                         int score = 0;
94                 
95                         //create pgroups that reflect the groups the user want to use
96                         for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
97                                 copyTree->tree[i].pGroups = (copyTree->mergeUserGroups(i, groups));
98                         }
99                 
100 //                      map<string,int>::iterator it;
101                         
102                         for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
103                         
104                                 if (m->control_pressed) { return data; }
105                                 
106                                 int lc = copyTree->tree[i].getLChild();
107                                 int rc = copyTree->tree[i].getRChild();
108                         
109                                 int iSize = copyTree->tree[i].pGroups.size();
110                                 int rcSize = copyTree->tree[rc].pGroups.size();
111                                 int lcSize = copyTree->tree[lc].pGroups.size();
112                                 
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                         } 
122                 
123                         data[count] = score;
124
125                 }
126                 
127                 delete copyTree;
128                 
129                 return data;
130         }
131         catch(exception& e) {
132                 m->errorOut(e, "Parsimony", "getValues");
133                 exit(1);
134         }
135 }
136
137 /**************************************************************************************************/
138