]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
added errorchecking and help info on new unifrac and treeclimber code
[mothur.git] / unweighted.cpp
1 /*
2  *  unweighted.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 2/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "unweighted.h"
11
12 /**************************************************************************************************/
13
14 EstOutput Unweighted::getValues(Tree* t) {
15         try {
16                 globaldata = GlobalData::getInstance();
17                 
18                 //clear out old values
19                 data.resize(1,0); 
20                 penalty.resize(t->getNumLeaves(), 0);
21                 
22                 map<string,double> unique;  //group, total of all branch lengths of nodes with that group.
23                 double shared = 0.0000;
24                 double UW=0.0000;
25                 
26                 //add up the branch lengths for each group. 
27                 for(int i=0;i<t->getNumLeaves();i++){
28                         if(t->tree[i].pGroups.size() > 0){
29                                 unique[t->tree[i].pGroups.begin()->first] += t->tree[i].getBranchLength();
30                         }
31                 }
32                 
33                 //for each non-leaf node
34                 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
35                 
36                         int lc = t->tree[i].getLChild();  //lc = vector index of left child
37                         int rc = t->tree[i].getRChild();  //rc = vector index of right child
38                         
39                         //get penalty values
40                         if(t->tree[rc].pGroups.size() == 0 || t->tree[lc].pGroups.size() == 0){
41                                 penalty.push_back(penalty[t->tree[rc].getIndex()]+penalty[t->tree[lc].getIndex()]);
42                         }
43                         else if(t->tree[i].pGroups.size() > t->tree[rc].pGroups.size() || t->tree[i].pGroups.size() > t->tree[lc].pGroups.size()){
44                                 penalty.push_back(penalty[t->tree[rc].getIndex()]+penalty[t->tree[lc].getIndex()]+1);
45                         }
46                         else{
47                                 penalty.push_back(penalty[t->tree[rc].getIndex()]+penalty[t->tree[lc].getIndex()]);
48                         }
49
50                         //not sure when this would ever be true??? if your parent is root could be, but pGroups.size() should never be 0.
51                         if(t->tree[i].getParent() == -1 && (t->tree[lc].pGroups.size() == 0 || t->tree[rc].pGroups.size() == 0)){
52                                 shared -= 1; 
53                         }
54                         else if(penalty[i] != 0 && t->tree[i].pGroups.size() != 0){
55                                 shared += t->tree[i].getBranchLength();
56                         }
57                         else if( t->tree[i].pGroups.size() != 0){
58                                 unique[t->tree[i].pGroups.begin()->first] += t->tree[i].getBranchLength();          
59                         }
60                 }
61     
62                 map<string,double>::iterator pos;
63                 for(pos=unique.begin();pos!=unique.end();pos++){
64                         if((pos->first!="xxx") && (inUsersGroups(pos->first))){     
65                                 UW += unique[pos->first];
66                         }
67                 }
68         
69                 UW /= (UW + shared);
70         
71                 if (isnan(UW) || isinf(UW)) { UW = 0; }
72         
73                 data[0] = UW;
74                 
75                 return data;
76         
77         }
78         catch(exception& e) {
79                 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80                 exit(1);
81         }
82         catch(...) {
83                 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84                 exit(1);
85         }
86
87 }
88
89 /**************************************************************************************************/
90 bool Unweighted::inUsersGroups(string groupname) {
91         try {
92                 for (int i = 0; i < globaldata->Groups.size(); i++) {
93                         if (groupname == globaldata->Groups[i]) { return true; }
94                 }
95                 return false;
96         }
97         catch(exception& e) {
98                 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
99                 exit(1);
100         }
101         catch(...) {
102                 cout << "An unknown error has occurred in the Unweighted class function inUsersGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
103                 exit(1);
104         }
105 }