]> git.donarmstrong.com Git - mothur.git/blob - calculator.h
c11989289197661f164564024cdc8d7f0eafc768
[mothur.git] / calculator.h
1 #ifndef CALCULATOR_H
2 #define CALCULATOR_H
3
4 using namespace std;
5
6 #include <vector>
7 #include <cmath>
8 #include <iostream>
9 #include <fstream>
10 #include <math.h>
11 #include <string>
12 #include <map>
13 #include <algorithm>
14 #include "utilities.hpp"
15 #include "sabundvector.hpp"
16 #include "sharedsabundvector.h"
17 #include "rabundvector.hpp"
18 #include "uvest.h"
19
20 /* The calculator class is the parent class for all the different estimators implemented in mothur except the tree calculators.
21 It has 2 pure functions EstOutput getValues(SAbundVector*), which works on a single group, and 
22 EstOutput getValues(SharedRAbundVector* shared1, SharedRAbundVector* shared2), which compares 2 groups. */ 
23
24
25 using namespace std;
26 typedef vector<double> EstOutput;
27
28 /***********************************************************************/
29
30 class Calculator {
31
32 public:
33         Calculator(){};
34         Calculator(string n, int c) : name(n), cols(c) {};
35         virtual EstOutput getValues(SAbundVector*) = 0; 
36         virtual EstOutput getValues(SharedRAbundVector* shared1, SharedRAbundVector* shared2) = 0;
37         virtual void print(ostream& f)  { f.setf(ios::fixed, ios::floatfield); f.setf(ios::showpoint);
38                                                                           f << data[0]; for(int i=1;i<data.size();i++){ f << '\t' << data[i];   }}
39         virtual string getName()                {       return name;    }
40         virtual int getCols()           {       return cols;    }
41 protected:
42         EstOutput data;
43         string name;
44         int cols;
45
46 };
47
48 /**************************************************************************************************/
49 /*This Class holds all of the methods that manipulate vectors.
50 These methods are used in the other classes.
51 This class must be included if any of the other classes are to be used.*/
52
53 class VecCalc
54 {
55         // The methods seen in the order here is how they are ordered throughout the class.
56         public:
57                 void printElements(vector<double>); //This prints the values of the vector on one line with a space between each value.
58                 void printElements(vector<string>); //This prints the values of the vector on one line with a space between each value.
59                 int findString(vector<string>, string);//This returns the index of the given string in the given <string> vector, if the string does not exist in the vector it returns -1.
60                 double mean(vector<double>); //This returns the mean value of the vector.
61                 double stError(vector<double>); //This returns the standard error of the vector.
62                 double sumElements(vector<double>); //This returns the sum of all the values in the vector.
63                 double sumElements(vector<double>, int); //This returns the sum of all the values in the vector excluding those whose index is before the given index.  
64                 double findMax(vector<double>); //This returns the maximum value in the vector.
65                 double numNZ(vector<double>); //This returns the number of non-zero values in the vector.
66                 double numPos(vector<double>); //This returns the number of positive values in the vector.
67                 double findMaxDiff(vector<double>, vector<double>); //This returns the absolute value of the maximum difference between the two vectors.
68                 double findDStat(vector<double>, vector<double>, double); //This returns the D-Statistic of the two vectors with the given total number of species.
69                 vector<int> findQuartiles(vector<double>); //This returns a vector with the first element being the index of the lower quartile of the vector and the second element being the index of the upper quartile of the vector.
70                 vector<double> add(vector<double>, double); //This adds the given number to every element in the given vector and returns the new vector.
71                 vector<double> multiply(vector<double>, double); //This multiplies every element in the given vector by the given number and returns the new vector.
72                 vector<double> power(vector<double>, double); //This raises every element in the given vector to the given number and returns the new vector.
73                 vector<double> addVecs(vector<double>,vector<double>); //The given vectors must be the same size. This adds the ith element of the first given vector to the ith element of the second given vector and returns the new vector.
74                 vector<double> multVecs(vector<double>,vector<double>); //The given vectors must be the same size. This multiplies the ith element of the first given vector to the ith element of the second given vector and returns the new vector.
75                 vector<double> remDup(vector<double>); //This returns a vector that contains 1 of each unique element in the given vector. The order of the elements is not changed.
76                 vector<double> genCVec(vector<double>); //This returns a cumilative vector of the given vector. The ith element of the returned vector is the sum of all the elements in the given vector up to i.
77                 vector<double> genRelVec(vector<double>); //This finds the sum of all the elements in the given vector and then divides the ith element in the given vector by that sum and then puts the result into a new vector, which is returned after all of the elements in the given vector have been used.
78                 vector<double> genDiffVec(vector<double>, vector<double>);//This subtracts the ith element of the second given vector from the ith element of the first given vector and returns the new vector.
79                 vector<double> genCSVec(vector<double>);//This calculates the number of species that have the same number of individuals as the ith element of the given vector and then returns a cumulative vector.
80                 vector<double> genTotVec(vector<vector<double> >); //This adds up the ith element of all the columns and puts that value into a new vector. It those this for all the rows and then returns the new vector.
81                 vector<double> quicksort(vector<double>); //This sorts the given vector from highest to lowest and returns the sorted vector.
82                 vector<vector<double> > gen2DVec(vector<double>, int, int); //(vector, #rows/columns, 0 if the second parameter was rows, 1 if the second parameter was columns) Transforms a single vector that was formatted like a table into a 2D vector.
83                 vector<string> getSData(char[]);//This takes a file name as a parameter and reads all of the data in the file into a <string> vector.
84 };
85
86 /**************************************************************************************************/
87 /*This Class contains methods that return the B Diverstiy of two sets
88 of data. The four methods are the Whittaker's measure, the Marczewski-Stainhaus distance,
89 the Sorensen quantitative index, and the Morisita-Horn index.
90 The main method takes a number of columns of data and performs all 4 methods on each
91 combination of columns. It prints a table for every method that shows the B Diverstiy for 
92 each combination. It also calculates the overall diversity for Whittaker's measure and 
93 the Marczewski-Steinhaus distance.*/
94
95
96 class BDiversity
97 {
98         public:
99                 void doBD(vector<double>, double);//Main method
100                 double getWhitt(vector<double>,vector<double>);//Whittacker's measure
101                 double getMS(vector<double>, vector<double>);//Marczewski-Stainhaus distance
102                 double getSor(vector<double>, vector<double>);//Sorensen quantitative index
103                 double getMor(vector<double>, vector<double>);//Morisita-Horn index
104                 void printD(vector<vector<double> >, int);//This prints a table that represents the given 2D vector, the second paramter specifies which method is to be used (1 for Whitt, 2 for MS, 3 for Sor, and 4 for Mor)
105 };
106
107 /**************************************************************************************************/
108
109 /*This Class is similar to the GeometricSeries.h class. It calculates
110 the broken stick distribution of the table and prints the D-Statistic 
111 and the confidence limits for the Kolmogorov-Smirnov 1-Sample test
112 with a 95% confidence level.*/
113
114 class BrokenStick
115 {
116         public:
117                 void doBStick(vector<double>);
118 };
119
120 /**************************************************************************************************/
121 /*This Class calculates the geometric series distribution for the data.
122 It prints the D-Statistic and the critical values for the Kolmogorov-Smirnov
123 1-sample test at the 95% confidence interval.*/
124
125 class GeometricSeries
126 {
127         public:
128                 void doGeomTest(vector<double>);
129 };
130
131 /**************************************************************************************************/
132 //This Class calculates the jackknifed estimate of the data and
133 //prints it and the confidence limits at a chosen confidence level.
134
135 class Jackknifing
136 {
137         public:
138                 void doJK(vector<double>, double);
139 };
140 /**************************************************************************************************/
141 /*This Class stores calculates the Kolmogorov-Smirnov 2-Sample test between two samples.
142 It prints the D-Statistic and the critical value for the test at 
143 the 90% and 95% confidence interval.*/
144
145 class KS2SampleTest
146 {
147         public:
148                 void doKSTest(vector<double>, vector<double>);
149 };
150 /**************************************************************************************************/
151 /*This Class calculates the Log Series Distribution for the data.
152 It then generates a D-Statistic and prints the D-Statistic and
153 the critical values for the Kolmogorov-Smirnov 1 sample test.*/
154
155 class LogSD
156 {
157         public:
158                 void doLogSD(vector<double>, vector<double>);
159 };
160
161 /**************************************************************************************************/
162 //This Class calculates and prints the Q-Statistic for the data.
163 class QStatistic
164 {
165         public:
166                 void doQStat(vector<double>);
167 };
168 /**************************************************************************************************/
169 class SSBPDiversityIndices
170 {
171         public:
172                 void doSSBP(vector<double>);
173                 double getShan(vector<double> vec);//The Shannon Index
174                 double getSimp(vector<double> vec);//The Simpson Index
175                 double getBP(vector<double> vec);//The Berger-Parker Index
176 };
177 /**************************************************************************************************/
178 //This Class stores the table of the confidence limits of the Student-T distribution.
179 class TDTable
180 {
181         public:
182                 double getConfLimit(int,int);
183 };
184
185 /**************************************************************************************************/
186 /*This Class calculates the truncated lognormal for the data.
187 It then prints the D-Statistic and the critical values for the
188 Kolmogorov-Smirnov 1-Sample test.*
189
190 class TrunLN
191 {
192         public:
193                 void doTrunLN(vector<double>, vector<double>);
194 };
195 /**************************************************************************************************/
196
197 #endif
198