]> git.donarmstrong.com Git - mothur.git/blob - bstick.cpp
added bootstrap.shared command and fixed some bugs with heatmap
[mothur.git] / bstick.cpp
1 /*
2  *  bstick.cpp
3  *  Mothur
4  *
5  *  Created by Thomas Ryabin on 3/6/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "bstick.h"
11
12
13 /***********************************************************************/
14 double BStick::invSum(int index, double numSpec)
15 {
16         double sum = 0;
17         for(int i = index; i <= numSpec; i++)
18                 sum += 1/(double)i;
19         return sum;
20 }
21
22 RAbundVector BStick::getRAbundVector(SAbundVector* rank){
23                 vector <int> rData;
24                 int mr = 1;
25                 int nb = 0;
26                 int ns = 0;
27                 
28                 for(int i = rank->size()-1; i > 0; i--)
29                 {
30                         int cur = rank->get(i);
31                         if(mr == 1 && cur > 0)
32                                 mr = i;
33                         nb += cur;
34                         ns += i*cur;
35                         for(int j = 0; j < cur; j++)
36                                 rData.push_back(i);
37                 }
38                 
39                 RAbundVector rav = RAbundVector(rData, mr, nb, ns);
40                 rav.setLabel(rank->getLabel());
41                 return rav;
42 }
43
44 /***************************************************************************/
45
46 /***************************************************************************/
47 EstOutput BStick::getValues(SAbundVector* rank){
48         try {
49                 data.resize(2,0);
50                 rdata = getRAbundVector(rank);
51                 double numInd = (double)rdata.getNumSeqs();
52                 double numSpec = (double)rdata.getNumBins();
53                 
54                 double sumExp = 0;
55                 double sumObs = 0;
56                 double maxDiff = 0;
57
58                 for(int i = 0; i < rdata.size(); i++)
59                 {
60                         sumObs += rdata.get(i);
61                         sumExp += numInd/numSpec*invSum(i+1,numSpec);
62                         double diff = fabs(sumObs-sumExp);
63                         if(diff > maxDiff)
64                                 maxDiff = diff;
65                 }
66                 
67                 double DStatistic = maxDiff/numInd;
68                 double critVal = 0;
69                 /*cout << "BStick:\n";
70                 cout << "D-Statistic = " << DStatistic << "\n";
71                 cout << "Critical value for 95% confidence interval = ";*/
72                 if(rdata.size() > 20)
73                 {
74                         critVal = .886/sqrt(rdata.size());
75                 }
76                 else
77                 {
78                         KOSTable table;
79                         critVal = table.getConfLimit(numSpec);
80                 }
81                 /*cout << critVal << "\n";
82                 cout << "If D-Statistic is less than the critical value then the data fits the Broken Stick model w/ 95% confidence.\n\n";*/
83                 
84                 data[0] = DStatistic;
85                 data[1] = critVal;
86                 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
87                 if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
88                 
89                 return data;
90         }
91         catch(exception& e) {
92                 cout << "Standard Error: " << e.what() << " has occurred in the NPShannon class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
93                 exit(1);
94         }
95         catch(...) {
96                 cout << "An unknown error has occurred in the NPShannon class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
97                 exit(1);
98         }       
99 }
100
101 /***********************************************************************/
102
103