]> git.donarmstrong.com Git - mothur.git/blob - parsimony.cpp
created mothurOut class to handle logfiles
[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                                         if (tmap->namesOfGroups[i] != "xxx") {
79                                                 groups.push_back(tmap->namesOfGroups[i]);
80                                         }
81                                 }
82                         }else {
83                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
84                                         groups.push_back(globaldata->Groups[i]);
85                                 }
86                         }
87                         
88                         //copy users tree so that you can redo pgroups 
89                         copyTree->getCopy(t);
90                         int score = 0;
91                 
92                         //create pgroups that reflect the groups the user want to use
93                         for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
94                                 copyTree->tree[i].pGroups = (copyTree->mergeUserGroups(i, groups));
95                         }
96                 
97 //                      map<string,int>::iterator it;
98                         
99                         for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
100                                 int lc = copyTree->tree[i].getLChild();
101                                 int rc = copyTree->tree[i].getRChild();
102                         
103                                 int iSize = copyTree->tree[i].pGroups.size();
104                                 int rcSize = copyTree->tree[rc].pGroups.size();
105                                 int lcSize = copyTree->tree[lc].pGroups.size();
106                                 
107                                         
108                                 //if isize are 0 then that branch is to be ignored
109                                 if (iSize == 0) { }
110                                 else if ((rcSize == 0) || (lcSize == 0)) { }
111                                 //if you have more groups than either of your kids then theres been a change.
112                                 else if(iSize > rcSize || iSize > lcSize){
113                                         score++;
114                                 }
115                         } 
116                 
117                         data[count] = score;
118
119                 }
120                 
121                 delete copyTree;
122                 
123                 return data;
124         }
125         catch(exception& e) {
126                 m->errorOut(e, "Parsimony", "getValues");
127                 exit(1);
128         }
129 }
130
131 /**************************************************************************************************/
132