]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
fixed unweighted calculator
[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                 
21                 float UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
22                 float totalBL = 0.00;   //all branch lengths
23                 float UW = 0.00;                //Unweighted Value = UniqueBL / totalBL;
24                 
25                 map<string, int>::iterator it;  //iterator to traverse pgroups
26                 map<string, int> copyLCpcount;
27                 map<string, int> copyRCpcount;
28                 map<string, int> copyIpcount;
29         
30                 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
31                 
32                         int lc = t->tree[i].getLChild();  //lc = vector index of left child
33                         int rc = t->tree[i].getRChild();  //rc = vector index of right child
34                         
35                         /**********************************************************************/
36                         //This section adds in all lengths that are non leaf
37                         
38                         //copy left childs pGroups and remove groups that the user doesn't want
39                         copyIpcount = t->tree[i].pcount;
40                         for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
41                                 if (inUsersGroups(it->first, globaldata->Groups) != true) {     copyIpcount.erase(it->first);   }
42                         }
43
44                         //copy left childs pGroups and remove groups that the user doesn't want
45                         copyLCpcount = t->tree[lc].pcount;
46                         for (it = copyLCpcount.begin(); it != copyLCpcount.end(); it++) {
47                                 if (inUsersGroups(it->first, globaldata->Groups) != true) {     copyLCpcount.erase(it->first);  }
48                         }
49
50                         //copy right childs pGroups and remove groups that the user doesn't want
51                         copyRCpcount = t->tree[rc].pcount;
52                         for (it = copyRCpcount.begin(); it != copyRCpcount.end(); it++) {
53                                 if (inUsersGroups(it->first, globaldata->Groups) != true) {     copyRCpcount.erase(it->first);  }
54                         }
55         
56                         //if i's children are from the same group and i has a BL then add i's length to unique
57                         //if copyRCpcount.size() = 0 && copyLCpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
58                         if ((copyRCpcount.size() == 0) && (copyLCpcount.size() == 0)) { }
59                         else {
60                                 if ((copyRCpcount == copyLCpcount) && (t->tree[i].getBranchLength() != -1)) {  UniqueBL += t->tree[i].getBranchLength();        }
61                                 //if either childs groups = 0 then all of there groups were not valid making the parent unique
62                                 else if (((copyRCpcount.size() == 0) || (copyLCpcount.size() == 0)) && (t->tree[i].getBranchLength() != -1)) {  UniqueBL += t->tree[i].getBranchLength();       }
63                         }
64                         
65                         //add i's BL to total if it is from the groups the user wants
66                         if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
67                                 totalBL += t->tree[i].getBranchLength(); 
68                         }
69                         
70                         /**********************************************************************/
71                         //This section adds in all lengths that are leaf
72                         
73                         //if i's chidren are leaves
74                         if (t->tree[rc].getRChild() == -1) {
75                                 //if rc is a valid group and rc has a BL
76                                 if ((inUsersGroups(t->tree[rc].getGroup(), globaldata->Groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
77                                         UniqueBL += t->tree[rc].getBranchLength();
78                                         totalBL += t->tree[rc].getBranchLength(); 
79                                 }
80                         }
81                         
82                         if (t->tree[lc].getLChild() == -1) {
83                                 //if lc is a valid group and lc has a BL
84                                 if ((inUsersGroups(t->tree[lc].getGroup(), globaldata->Groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
85                                         UniqueBL += t->tree[lc].getBranchLength();
86                                         totalBL += t->tree[lc].getBranchLength(); 
87                                 }
88                         }
89                         
90                         /**********************************************************************/
91                 
92                 }
93                 
94                 UW = (UniqueBL / totalBL);  
95         
96                 if (isnan(UW) || isinf(UW)) { UW = 0; }
97         
98                 data[0] = UW;
99                 
100                 return data;
101         
102         }
103         catch(exception& e) {
104                 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105                 exit(1);
106         }
107         catch(...) {
108                 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
109                 exit(1);
110         }
111
112 }
113