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