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