]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
adding treeclimber and unifrac pieces
[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         
17                 //clear out old values
18                 data.resize(1,0); 
19                 penalty.resize(t->getNumLeaves(), 0);
20                 
21                 map<string,double> unique;  //group, total of all branch lengths of nodes with that group.
22                 double shared = 0.0000;
23                 double UW=0.0000;
24                 
25                 //add up the branch lengths for each group. 
26                 for(int i=0;i<t->getNumLeaves();i++){
27                         if(t->tree[i].pGroups.size() > 0){
28                                 unique[t->tree[i].pGroups.begin()->first] += t->tree[i].getBranchLength();
29                         }
30                 }
31                 
32                 //for each non-leaf node
33                 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
34                 
35                         int lc = t->tree[i].getLChild();  //lc = vector index of left child
36                         int rc = t->tree[i].getRChild();  //rc = vector index of right child
37                         
38                         //get penalty values
39                         if(t->tree[rc].pGroups.size() == 0 || t->tree[lc].pGroups.size() == 0){
40                                 penalty.push_back(penalty[t->tree[rc].getIndex()]+penalty[t->tree[lc].getIndex()]);
41                         }
42                         else if(t->tree[i].pGroups.size() > t->tree[rc].pGroups.size() || t->tree[i].pGroups.size() > t->tree[lc].pGroups.size()){
43                                 penalty.push_back(penalty[t->tree[rc].getIndex()]+penalty[t->tree[lc].getIndex()]+1);
44                         }
45                         else{
46                                 penalty.push_back(penalty[t->tree[rc].getIndex()]+penalty[t->tree[lc].getIndex()]);
47                         }
48
49                         //not sure when this would ever be true??? if your parent is root could be, but pGroups.size() should never be 0.
50                         if(t->tree[i].getParent() == -1 && (t->tree[lc].pGroups.size() == 0 || t->tree[rc].pGroups.size() == 0)){
51                                 shared -= 1; 
52                         }
53                         else if(penalty[i] != 0 && t->tree[i].pGroups.size() != 0){
54                                 shared += t->tree[i].getBranchLength();
55                         }
56                         else if( t->tree[i].pGroups.size() != 0){
57                                 unique[t->tree[i].pGroups.begin()->first] += t->tree[i].getBranchLength();          
58                         }
59                 }
60     
61                 map<string,double>::iterator pos;
62                 for(pos=unique.begin();pos!=unique.end();pos++){
63                         if(pos->first!="xxx"){      
64                                 UW += unique[pos->first];
65                         }
66                 }
67         
68                 UW /= (UW + shared);
69         
70                 if (isnan(UW) || isinf(UW)) { UW = 0; }
71         
72                 data[0] = UW;
73                 
74                 return data;
75         
76         }
77         catch(exception& e) {
78                 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79                 exit(1);
80         }
81         catch(...) {
82                 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
83                 exit(1);
84         }
85
86 }
87
88 /**************************************************************************************************/