5 * Created by Sarah Westcott on 11/18/08.
6 * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "calculator.h"
12 /***********************************************************************/
13 void VecCalc::printElements(vector<double> vec){
15 for(int i = 0; i < vec.size(); i++)
16 cout << vec.at(i) << " ";
20 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function printElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
24 cout << "An unknown error has occurred in the VecCalc class function printElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
29 /***********************************************************************/
31 void VecCalc::printElements(vector<string> vec){
33 for(int i = 0; i < vec.size(); i++)
34 cout << vec.at(i) << " ";
38 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function printElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
42 cout << "An unknown error has occurred in the VecCalc class function printElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
46 /***********************************************************************/
47 int VecCalc::findString(vector<string> vec, string str){
49 for(int i = 0; i < vec.size(); i++)
50 if(str.compare(vec.at(i)) == 0)
55 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function findString. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
59 cout << "An unknown error has occurred in the VecCalc class function findString. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
63 /***********************************************************************/
64 double VecCalc::mean(vector<double> vec){
65 return sumElements(vec)/vec.size();
67 /***********************************************************************/
68 double VecCalc::stError(vector<double> vec){
72 for(int i = 0; i < vec.size(); i++)
73 sum += pow(vec.at(i)-m,2);
74 return pow(sum/(vec.size()*(vec.size()-1)), .5);
77 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function stError. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
81 cout << "An unknown error has occurred in the VecCalc class function stError. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
85 /***********************************************************************/
86 int VecCalc::sumElements(vector<int> vec){
88 return sumElements(vec,0);
91 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function sumElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95 cout << "An unknown error has occurred in the VecCalc class function sumElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
99 /***********************************************************************/
100 int VecCalc::sumElements(vector<int> vec, int index){
103 for(int i = index; i < vec.size(); i++)
107 catch(exception& e) {
108 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function sumElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
112 cout << "An unknown error has occurred in the VecCalc class function sumElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
117 /***********************************************************************/
118 double VecCalc::sumElements(vector<double> vec){
121 for(int i = 0; i < vec.size(); i++)
125 catch(exception& e) {
126 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function sumElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
130 cout << "An unknown error has occurred in the VecCalc class function sumElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
134 /***********************************************************************/
135 double VecCalc::sumElements(vector<double> vec, int index){
138 for(int i = index; i < vec.size(); i++)
142 catch(exception& e) {
143 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function sumElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
147 cout << "An unknown error has occurred in the VecCalc class function sumElements. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
151 /***********************************************************************/
152 double VecCalc::findMax(vector<double> vec){
154 double max = -1000000.0;
155 for(int i = 0; i < vec.size(); i++)
160 catch(exception& e) {
161 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function findMax. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165 cout << "An unknown error has occurred in the VecCalc class function findMax. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169 /***********************************************************************/
170 int VecCalc::numNZ(vector<int> vec){
173 for(int i = 0; i < vec.size(); i++)
178 catch(exception& e) {
179 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function numNZ. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183 cout << "An unknown error has occurred in the VecCalc class function numNZ. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187 /***********************************************************************/
188 double VecCalc::numNZ(vector<double> vec){
191 for(int i = 0; i < vec.size(); i++)
196 catch(exception& e) {
197 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function numNZ. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
201 cout << "An unknown error has occurred in the VecCalc class function numNZ. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
205 /***********************************************************************/
206 double VecCalc::numPos(vector<double> vec){
209 for(int i = 0 ; i < vec.size(); i++)
214 catch(exception& e) {
215 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function numPos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
219 cout << "An unknown error has occurred in the VecCalc class function numPos. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
223 /***********************************************************************/
224 double VecCalc::findMaxDiff(vector<double> vec1, vector<double> vec2){
226 double maxDiff = -10000000000.0;
227 for(int i = 0; i < vec1.size(); i++)
228 if(fabs(vec1.at(i)-vec2.at(i)) > maxDiff)
229 maxDiff = fabs(vec1.at(i)-vec2.at(i));
232 catch(exception& e) {
233 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function findMaxDiff. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
237 cout << "An unknown error has occurred in the VecCalc class function findMaxDiff. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241 /***********************************************************************/
242 double VecCalc::findDStat(vector<double> vec1, vector<double> vec2, double num){
244 double mDiff = findMaxDiff(vec1, vec2);
245 return (mDiff+.5)/num;
247 catch(exception& e) {
248 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function findDStat. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
252 cout << "An unknown error has occurred in the VecCalc class function findDStat. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
256 /***********************************************************************/
257 vector<int> VecCalc::findQuartiles(vector<double> vec){
259 vector<int> quartiles;
260 double max = vec.at(vec.size()-1);
263 bool r1found = false;
264 bool r2found = false;
265 for(int i = 0; i < vec.size(); i++)
267 if(vec.at(i) > r1 && !r1found)
269 quartiles.push_back(i);
272 if(vec.at(i) > r2 && !r2found)
274 quartiles.push_back(i);
280 catch(exception& e) {
281 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function findQuartiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
285 cout << "An unknown error has occurred in the VecCalc class function findQuartiles. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
289 /***********************************************************************/
290 vector<double> VecCalc::add(vector<double> vec, double x){
292 vector<double> newVec;
293 for(int i = 0; i < vec.size(); i++)
294 newVec.push_back(vec.at(i)+x);
297 catch(exception& e) {
298 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function add. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
302 cout << "An unknown error has occurred in the VecCalc class function add. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
306 /***********************************************************************/
307 vector<double> VecCalc::multiply(vector<double> vec, double x){
309 vector<double> newVec;
310 for(int i = 0; i < vec.size(); i++)
311 newVec.push_back(vec.at(i)*x);
314 catch(exception& e) {
315 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function multiply. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
319 cout << "An unknown error has occurred in the VecCalc class function multiply. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
323 /***********************************************************************/
324 vector<double> VecCalc::power(vector<double> vec, double p){
326 vector<double> newVec;
327 for(int i = 0; i < vec.size(); i++)
328 newVec.push_back(pow(vec.at(i), p));
331 catch(exception& e) {
332 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function power. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
336 cout << "An unknown error has occurred in the VecCalc class function power. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
340 /***********************************************************************/
341 vector<double> VecCalc::addVecs(vector<double> vec1, vector<double> vec2) //Vectors must be the same size.
343 vector<double> newVec;
344 for(int i = 0; i < vec1.size(); i++)
345 newVec.push_back(vec1.at(i)+vec2.at(i));
348 catch(exception& e) {
349 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function addVecs. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
353 cout << "An unknown error has occurred in the VecCalc class function addVecs. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
357 /***********************************************************************/
358 vector<double> VecCalc::multVecs(vector<double> vec1, vector<double> vec2) //Vectors must be the same size.
360 vector<double> newVec;
361 for(int i = 0; i < vec1.size(); i++)
362 newVec.push_back(vec1.at(i)*vec2.at(i));
365 catch(exception& e) {
366 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function multVecs. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
370 cout << "An unknown error has occurred in the VecCalc class function multVecs. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
374 /***********************************************************************/
375 vector<double> VecCalc::remDup(vector<double> vec){
377 vector<double> newVec;
378 newVec.push_back(vec.at(0));
379 for(int i = 1; i < vec.size(); i++)
380 if(vec.at(i) != vec.at(i-1))
381 newVec.push_back(vec.at(i));
384 catch(exception& e) {
385 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function remDup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
389 cout << "An unknown error has occurred in the VecCalc class function remDup. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
393 /***********************************************************************/
394 vector<double> VecCalc::genCVec(vector<double> vec1){
398 for(int i = 0; i < vec1.size(); i++)
405 catch(exception& e) {
406 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function genCVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
410 cout << "An unknown error has occurred in the VecCalc class function genCVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
414 /***********************************************************************/
415 vector<double> VecCalc::genRelVec(vector<double> vec){
417 vector<double> newVec;
418 double sum = sumElements(vec);
419 for(int i = 0; i < vec.size(); i++)
420 newVec.push_back(vec.at(i)/sum);
423 catch(exception& e) {
424 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function genRelVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
428 cout << "An unknown error has occurred in the VecCalc class function genRelVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
432 /***********************************************************************/
433 vector<double> VecCalc::genDiffVec(vector<double> vec1, vector<double> vec2){
435 vector<double> newVec;
436 for(int i = 0; i < vec1.size(); i++)
437 newVec.push_back(vec1.at(i)-vec2.at(i));
440 catch(exception& e) {
441 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function genDiffVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
445 cout << "An unknown error has occurred in the VecCalc class function genDiffVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
449 /***********************************************************************/
450 vector<double> VecCalc::genCSVec(vector<double> vec){
452 vector<double> newVec;
453 double curInd = vec.at(vec.size()-1);
456 for(int i = vec.size()-1; i >= 0; i--)
458 if(vec.at(i) == curInd)
463 newVec.push_back(cSumSpec);
468 newVec.push_back(cSumSpec + sumSpec);
472 catch(exception& e) {
473 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function genCSVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
477 cout << "An unknown error has occurred in the VecCalc class function genCSVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
481 /***********************************************************************/
482 vector<double> VecCalc::genTotVec(vector<vector<double> > vec){
484 vector<double> newVec = vec.at(0);
485 for(int i = 1; i < vec.size(); i++)
486 newVec = addVecs(newVec, vec.at(i));
489 catch(exception& e) {
490 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function genTotVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
494 cout << "An unknown error has occurred in the VecCalc class function genTotVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
498 /***********************************************************************/
499 vector<double> VecCalc::quicksort(vector<double> vec){
501 sort(vec.rbegin(), vec.rend());
504 catch(exception& e) {
505 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function quicksort. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
509 cout << "An unknown error has occurred in the VecCalc class function quicksort. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
513 /***********************************************************************/
514 vector<vector<double> > VecCalc::gen2DVec(vector<double> vec, int div, int type){
516 vector<vector<double> > vec2D;
517 int gap = vec.size()/div;
518 for(int i = 0; i < div; i++)
520 vector<double> inVec;
521 for(int j = 0; j < gap; j++)
523 inVec.push_back(vec.at(j + i*gap)); //Rows
525 inVec.push_back(vec.at(i + j*div)); //Columns
526 vec2D.push_back(inVec);
530 catch(exception& e) {
531 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function gen2DVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
535 cout << "An unknown error has occurred in the VecCalc class function gen2DVec. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
539 /***********************************************************************/
540 vector<string> VecCalc::getSData(char name[]){
553 catch(exception& e) {
554 cout << "Standard Error: " << e.what() << " has occurred in the VecCalc class Function getSData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
558 cout << "An unknown error has occurred in the VecCalc class function getSData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
562 /***********************************************************************/
564 /***********************************************************************/
565 void BrokenStick::doBStick(vector<double> vec)//vec = The data vector.
568 vec = vecCalc.quicksort(vec);
569 double numInd = vecCalc.sumElements(vec);
570 double numSpec = vecCalc.numNZ(vec);
571 vector<double> cObsVec = vecCalc.genCVec(vec);
572 vector<double> cExpVec;
573 vec = vecCalc.power(vec, -1);
575 for(int i = 0; i < numSpec; i++)
578 for(int j = i; j < numSpec; j++)
579 n += 1/(double)(j+1);
581 sumExp += numInd/numSpec*n;
582 cExpVec.push_back(sumExp);
585 //Statistical analysis
586 double maxDiff = vecCalc.findMaxDiff(cObsVec, cExpVec);
587 double DStatistic = maxDiff/numInd;
589 cout << "D-Statistic = " << DStatistic << "\n";
591 cout << "Critical value for 95% confidence interval = " << .886/sqrt(vec.size()) << "\n";
593 catch(exception& e) {
594 cout << "Standard Error: " << e.what() << " has occurred in the BrokenStick class Function doBStick. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
598 cout << "An unknown error has occurred in the BrokenStick class function doBStick. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
603 /***********************************************************************/
604 double kEq(double k, double spec)
606 return k/(1-k)*pow(1-k, spec)/(1-pow(1-k, spec));
608 /***********************************************************************/
609 /*void GeometricSeries::doGeomTest(vector<double> vec)//vec = the data vector
612 vec = vecCalc.quicksort(vec);//Sorts vec
613 double numInd = vecCalc.sumElements(vec);//numInd = The total number of individuals in the data set.
614 double numSpec = vecCalc.numNZ(vec);//numSpec = the number of nonzero elements in the data set.
615 double min = -1.0*vecCalc.findMax(vecCalc.multiply(vec, -1));
617 double step = .49999;
618 while(fabs(min - numInd*kEq(k, numSpec)) > .0001)//This uses a binary search to find the value of k.
620 if(numInd*kEq(k, numSpec) > min)
626 cout << "k = " << k << "\n";
627 double cK = 1/(1-pow(1-k, numSpec));
629 vector<double> cObsVec = vecCalc.genCVec(vec);
630 vector<double> cExpVec;
632 for(int i = 0; i < vec.size(); i++)
634 sumExp += numInd*cK*k*pow(1-k, i);
635 cExpVec.push_back(sumExp);
637 double maxDiff = vecCalc.findMaxDiff(cObsVec, cExpVec);
638 double DStatistic = maxDiff/numInd;
640 //Statistical Analysis
641 cout << "D-Statistic = " << DStatistic << "\n";
643 cout << "Critical value for 95% confidence interval = " << .886/sqrt(vec.size()) << "\n";
645 catch(exception& e) {
646 cout << "Standard Error: " << e.what() << " has occurred in the GeometricSeries class Function doGeomTest. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
650 cout << "An unknown error has occurred in the GeometricSeries class function doGeomTest. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
655 /***********************************************************************/
656 void Jackknifing::doJK(vector<double> vec, double cols)//vec = the data vector if the data table was read left-to-right, top-to-bottom. cols = The number of columns in the data table.
659 SSBPDiversityIndices ssbp;
660 double rows = vec.size()/cols;
661 vector<vector<double> > species = vecCalc.gen2DVec(vec, rows, 0);//converts vec into a 2 dimensional vector
662 vector<double> sumRows;
663 vector<double> pVals;
665 for(int i = 0; i < rows; i++)
666 sumRows.push_back(vecCalc.sumElements(species.at(i)));
667 double st = 1/ssbp.getSimp(sumRows);//The observed estimate using the Simpson Index. Can be changed to use other indexes of diversity.
668 for(int i = 0; i < cols; i++)
670 vector<double> newVec;
671 for(int j = 0; j < rows; j++)
672 newVec.push_back(vecCalc.sumElements(species.at(j))-species.at(j).at(i));
673 pVals.push_back(cols*st-((cols-1)/ssbp.getSimp(newVec)));
676 double mean = vecCalc.mean(pVals);
677 double stErr = vecCalc.stError(pVals);//stErr = standard error
679 double confidence = .95;
681 cout << "Confidence Level (.8, .9, .95, .98, .99, .998, .999): ";
683 double confLevels[] = {.80,.90,.95,.98,.99,.998,.999};
684 for(int i = 0; i < 7; i++)
685 if(confidence == confLevels[i])
687 confLimit = table.getConfLimit(cols-2,i);
691 //Statistical Analysis
692 cout << "Lower limit = " << mean - confLimit*stErr << "\n";
693 cout << "Upper limit = " << mean + confLimit*stErr << "\n";
694 cout << "Observed estimate = " << st << "\n\n";
695 cout << "Jackknifed estimate = " << mean << "\n";
697 catch(exception& e) {
698 cout << "Standard Error: " << e.what() << " has occurred in the Jackknifing class Function doJK. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
702 cout << "An unknown error has occurred in the Jackknifing class function doJK. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
706 /***********************************************************************/
707 void KS2SampleTest::doKSTest(vector<double> abun1, vector<double> abun2)//abun1 = 1st vector of abundances, abun2 = 2nd vector of abundances
710 double numNZ1 = vecCalc.numNZ(abun1);//Number of nonzero elements in abun1
711 double numNZ2 = vecCalc.numNZ(abun2);//Number of nonzero elements in abun2
712 abun1 = vecCalc.quicksort(abun1);//Sorts abun1
713 abun2 = vecCalc.quicksort(abun2);//Sorts abun2
714 abun1 = vecCalc.genRelVec(abun1);//Generates the relative vector for abun1
715 abun2 = vecCalc.genRelVec(abun2);//Generates the relative vector for abun2
717 abun1 = vecCalc.genCVec(abun1);//Generates the cumulative vector for abun1
718 abun2 = vecCalc.genCVec(abun2);//Generates the cumulative vector for abun2
720 double maxDiff = vecCalc.findMaxDiff(abun1, abun2);
721 double DStatistic = maxDiff*numNZ1*numNZ2;
723 cout << "Null Hypothesis = There is no difference.\n";
724 cout << "D-Statistic = " << DStatistic << "\n";
726 double a = pow((numNZ1 + numNZ2)/(numNZ1*numNZ2),.5);
727 double pVal = exp(-2*pow(maxDiff/a,2));
729 if(numNZ1 > 25 && numNZ2 > 25) //If the sample sizes are both bigger than 25.
730 cout << "P-Value = " << pVal << "\n\n";
733 cout << "D-Statistic must be higher than the critical value to reject the null hypothesis.\n" ;
734 cout << "90% Confidence Critical Value = " << 1.22*a*numNZ1*numNZ2 << "\n";
735 cout << "95% Confidence Critical Value = " << 1.36*a*numNZ1*numNZ2 << "\n";
738 catch(exception& e) {
739 cout << "Standard Error: " << e.what() << " has occurred in the KS2SampleTest class Function doKSTest. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
743 cout << "An unknown error has occurred in the KS2SampleTest class function doKSTest. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
748 /***********************************************************************/
749 double logS(double num)
751 return -(1-num)*log(1-num)/num;
754 /***********************************************************************/
755 /*void LogSD::doLogSD(vector<double> indVec, vector<double> specVec) //indVec = individuals vector, specVec = species vector
758 double numSpec = vecCalc.sumElements(specVec);//numSpec = The total number of species
759 cout << "number of species = " << numSpec << "\n";
760 double numInd = vecCalc.sumElements(vecCalc.multVecs(indVec, specVec));
761 double snRatio = numSpec/numInd;
763 double step = .4999999999;
764 while(fabs(snRatio - logS(x)) > .00001) //This uses a binary search to find the value of x.
766 if(logS(x) > snRatio)
772 double alpha = numInd*(1-x)/x;
775 cout << "Number of individuals:"; //Ask the user for the number of individuals.
777 double spec = alpha*pow(x, ind)/ind;
778 cout << "Number of species expected = " << spec << "\n" << "X value = " << x << "\n" << "Alpha value= " << alpha << "\n";//Outputs the number of species expected with the given number of individuals.
780 vector<double> obsSpec;
781 vector<double> cObsSpec;
782 vector<double> expSpec;
783 vector<double> cExpSpec;
784 vector<double> cDiff;
786 // Generates the cumulative observed species vector.
788 double octSumObs = 0;
789 for(int y = 0; y < specVec.size(); y++)
791 if(indVec.at(y) - .5 < pow(2.0, oct))
792 octSumObs += specVec.at(y);
795 obsSpec.push_back(octSumObs);
796 octSumObs = specVec.at(y);
799 if(y == specVec.size()-1)
800 obsSpec.push_back(octSumObs);
802 cObsSpec = vecCalc.genCVec(obsSpec);
803 cObsSpec = vecCalc.add(cObsSpec,-.5);
805 // Generates the cumulative expected species vector.
807 double octSumExp = 0;
808 for(int g = 1; g <= indVec.at(indVec.size()-1); g++)
810 if(g - .5 < pow(2.0, oct))
811 octSumExp += alpha*pow(x,g)/(g);
814 expSpec.push_back(octSumExp);
815 octSumExp = alpha*pow(x,g)/(g);
818 if(g == indVec.at(indVec.size()-1))
819 expSpec.push_back(octSumExp);
821 cExpSpec = vecCalc.genCVec(expSpec);
823 // Statistical Analysis
824 double dTStat = vecCalc.findDStat(cObsSpec, cExpSpec, numSpec);
825 cout << "D Test Statistic = " << dTStat << "\n";
826 cout << ".05 confidence value = " << .89196/sqrt(numSpec) << "\n";
827 cout << ".01 confidence value = " << 1.0471/sqrt(numSpec) << "\n\n";
829 catch(exception& e) {
830 cout << "Standard Error: " << e.what() << " has occurred in the LogSD class Function doLogSD. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
834 cout << "An unknown error has occurred in the LogSD class function doLogSD. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
838 /***********************************************************************/
839 void QStatistic::doQStat(vector<double> vec)//vec = The data vector.
842 vector<double> cVec = vecCalc.genCSVec(vec);
843 vector<int> q = vecCalc.findQuartiles(cVec);//q.at(0) is the index of the first quartile; q.at(1) is the index of the second quartile.
844 vector<double> nDupVec = vecCalc.remDup(vec);//nDupVec only contains one of every unique element in cVec.
846 if(q.at(0) != 0)//The case if neither quartile is 0 or 1
847 Q = (.5*(cVec.at(q.at(0))-cVec.at(q.at(0)-1)) + (cVec.at(q.at(1)-1)-cVec.at(q.at(0))) + .5*(cVec.at(q.at(1))-cVec.at(q.at(1)-1)))/log(nDupVec.at(nDupVec.size()-1-q.at(1))/nDupVec.at(nDupVec.size()-1-q.at(0)));
848 else if(q.at(0) == 0 && (q.at(1) == 0 || q.at(1) == 1))//The case if the quartiles are both at index 0 or one is at 0 and the other at 1.
849 Q = (.5*cVec.at(0) + .5*(cVec.at(1)-cVec.at(0)))/log(nDupVec.at(nDupVec.size()-2)/nDupVec.at(nDupVec.size()-1));
850 else if(q.at(0) == 0 && q.at(1) > 1) //The case if the lower quartile is at index 0 and upper quartile index is above index 1.
851 Q = (.5*cVec.at(0) + (cVec.at(q.at(1)-1)-cVec.at(q.at(0))) + .5*(cVec.at(q.at(1))-cVec.at(q.at(1)-1)))/log(nDupVec.at(nDupVec.size()-1-q.at(1))/nDupVec.at(nDupVec.size()-1));
853 cout << "Q = " << Q << "\n";
855 catch(exception& e) {
856 cout << "Standard Error: " << e.what() << " has occurred in the QStatistic class Function doQStat. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
860 cout << "An unknown error has occurred in the QStatistic class function doQStat. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
864 /***********************************************************************/
865 double SSBPDiversityIndices::getShan(vector<double> vec)//vec = The data vector.
868 double nz = vecCalc.numNZ(vec);
869 double nSum = vecCalc.sumElements(vec);
871 for(int i = 0; i < nz; i++)
872 H += vec.at(i)/nSum*log(vec.at(i)/nSum);
876 catch(exception& e) {
877 cout << "Standard Error: " << e.what() << " has occurred in the SSBPDiversityIndices class Function getShan. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
881 cout << "An unknown error has occurred in the SSBPDiversityIndices class function getShan. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
885 /***********************************************************************/
886 double SSBPDiversityIndices::getSimp(vector<double> vec)//vec = The data vector.
889 double nSum = vecCalc.sumElements(vec);
891 for(int j = 0; j < vec.size(); j++)
892 D += vec.at(j)*(vec.at(j)-1)/(nSum*(nSum-1));
895 catch(exception& e) {
896 cout << "Standard Error: " << e.what() << " has occurred in the SSBPDiversityIndices class Function getSimp. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
900 cout << "An unknown error has occurred in the SSBPDiversityIndices class function getSimp. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
904 /***********************************************************************/
905 double SSBPDiversityIndices::getBP(vector<double> vec)//vec = The data vector.
908 double nSum = vecCalc.sumElements(vec);
909 return vecCalc.findMax(vec)/nSum;
911 catch(exception& e) {
912 cout << "Standard Error: " << e.what() << " has occurred in the SSBPDiversityIndices class Function getBP. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
916 cout << "An unknown error has occurred in the SSBPDiversityIndices class function getBP. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
920 /***********************************************************************/
921 void SSBPDiversityIndices::doSSBP(vector<double> vec)//vec = The data vector.
924 double nz = vecCalc.numNZ(vec);
927 double H = getShan(vec);
928 cout << "H = " << H << "\n";
929 cout << "Eveness = " << H/log(nz) << "\n\n";
932 double D = getSimp(vec);
933 cout << "D diversity = " << 1/D << "\n";
934 cout << "Eveness = " << 1/D/nz << "\n\n";
936 //Berger-Parker index
937 double BP = getBP(vec);
938 cout << "BP index = " << BP << "\n\n";
940 catch(exception& e) {
941 cout << "Standard Error: " << e.what() << " has occurred in the SSBPDiversityIndices class Function doSSBP. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
945 cout << "An unknown error has occurred in the SSBPDiversityIndices class function doSSBP. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
949 /***********************************************************************/
950 double TDTable::getConfLimit(int row, int col) //Rows are the degrees of freedom
952 //Found on http://www.vgtu.lt/leidiniai/elektroniniai/Probability.pdf/Table%203.pdf
954 //Confidence Level .90 .95 .975 .99 .995 .999 .9995
955 double values[33][7] = {{3.078, 6.314, 12.706, 31.821, 63.656, 318.289, 636.578},
956 {1.886, 2.920, 4.303, 6.965, 9.925, 22.328, 31.600},
957 {1.638, 2.353, 3.182, 4.541, 5.841, 10.214, 12.924},
958 {1.533, 2.132, 2.776, 3.747, 4.604, 7.173, 8.610},
959 {1.476, 2.015, 2.571, 3.365, 4.032, 5.894, 6.869},
960 {1.440, 1.943, 2.447, 3.143, 3.707, 5.208, 5.959},
961 {1.415, 1.895, 2.365, 2.998, 3.499, 4.785, 5.408},
962 {1.397, 1.860, 2.306, 2.896, 3.355, 4.501, 5.041},
963 {1.383, 1.833, 2.262, 2.821, 3.250, 4.297, 4.781},
964 {1.372, 1.812, 2.228, 2.764, 3.169, 4.144, 4.587},
965 {1.363, 1.796, 2.201, 2.718, 3.106, 4.025, 4.437},
966 {1.356, 1.782, 2.179, 2.681, 3.055, 3.930, 4.318},
967 {1.350, 1.771, 2.160, 2.650, 3.012, 3.852, 4.221},
968 {1.345, 1.761, 2.145, 2.624, 2.977, 3.787, 4.140},
969 {1.341, 1.753, 2.131, 2.602, 2.947, 3.733, 4.073},
970 {1.337, 1.746, 2.120, 2.583, 2.921, 3.686, 4.015},
971 {1.333, 1.740, 2.110, 2.567, 2.898, 3.646, 3.965},
972 {1.330, 1.734, 2.101, 2.552, 2.878, 3.610, 3.922},
973 {1.328, 1.729, 2.093, 2.539, 2.861, 3.579, 3.883},
974 {1.325, 1.725, 2.086, 2.528, 2.845, 3.552, 3.850},
975 {1.323, 1.721, 2.080, 2.518, 2.831, 3.527, 3.819},
976 {1.321, 1.717, 2.074, 2.508, 2.819, 3.505, 3.792},
977 {1.319, 1.714, 2.069, 2.500, 2.807, 3.485, 3.768},
978 {1.318, 1.711, 2.064, 2.492, 2.797, 3.467, 3.745},
979 {1.316, 1.708, 2.060, 2.485, 2.787, 3.450, 3.725},
980 {1.315, 1.706, 2.056, 2.479, 2.779, 3.435, 3.707},
981 {1.314, 1.703, 2.052, 2.473, 2.771, 3.421, 3.689},
982 {1.313, 1.701, 2.048, 2.467, 2.763, 3.408, 3.674},
983 {1.311, 1.699, 2.045, 2.462, 2.756, 3.396, 3.660},
984 {1.310, 1.697, 2.042, 2.457, 2.750, 3.385, 3.646},
985 {1.296, 1.671, 2.000, 2.390, 2.660, 3.232, 3.460},
986 {1.289, 1.658, 1.980, 2.358, 2.617, 3.160, 3.373},
987 {1.282, 1.645, 1.960, 2.326, 2.576, 3.091, 3.291}};
988 return values[row][col];
990 catch(exception& e) {
991 cout << "Standard Error: " << e.what() << " has occurred in the TDTable class Function getConfLimit. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
995 cout << "An unknown error has occurred in the TDTable class function getConfLimit. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
1000 /***********************************************************************/
1001 double KOSTable::getConfLimit(int index) //Table of Critical values for N = 4-20 for One-Sample Kolmogorov-Smirnov Test
1003 //Confidence Level = .05
1004 //Sample size = 4-20.
1005 //Found on http://www.utdallas.edu/~herve/MolinAbdi1998-LillieforsTechReport.pdf
1007 double values[21] = {.9011,
1029 return values[index];
1031 catch(exception& e) {
1032 cout << "Standard Error: " << e.what() << " has occurred in the TDTable class Function getConfLimit. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
1036 cout << "An unknown error has occurred in the TDTable class function getConfLimit. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
1041 /***********************************************************************
1042 void TrunLN::doTrunLN(vector<double> indVec, vector<double> specVec) //indVec = individuals vector, specVec = species vector
1045 double numSpec = vecCalc.sumElements(specVec); //numSpec = The total number of species
1046 double numInd = vecCalc.sumElements(vecCalc.multVecs(indVec,specVec)); //numInd = The total number of individuals
1049 for(int i = 0; i < indVec.size(); i++)
1050 obsMean += log10(indVec.at(i));
1051 obsMean /= numSpec; //obsMean = observed mean of the individuals vector
1052 cout << "obsMean = " << obsMean << "\n";
1053 double variance = 0;
1054 for(int t = 0; t < indVec.size(); t++)
1055 variance += pow(log10(indVec.at(t))-obsMean,2)/numSpec;
1058 for(int k = 0; k < indVec.size(); k++)
1059 rO += log10(indVec.at(k));
1060 rO /= indVec.size();
1061 double veilLine = .5;//The desired veil line.
1062 double auxFunc = -(obsMean-rO)/(obsMean-log10(veilLine));
1063 double uX = obsMean-auxFunc*(obsMean-log10(veilLine));
1064 double vX = variance + auxFunc*pow(obsMean-log10(veilLine),2);
1065 double z = (log10(veilLine)-uX)/pow(vX, .5);
1066 double p = .5*(erf(z)+1);
1067 double specRichness = numSpec/(1-p);
1069 double numUnseen = .5*(erf((log10(.5)-uX)/pow(vX,.5))+1)*specRichness;
1072 vector<double> cExp;
1073 for(int i = 1; i < 8; i++)
1075 double a = pow(10, i-1)+.5;
1076 double b = log10(a);
1077 double c = (b - uX)/pow(vX,.5);
1078 double d = .5*(erf(c)+1);
1079 double numS = d*specRichness;
1080 double toPush = numS - numUnseen;
1081 cExp.push_back(toPush);
1083 vector<double> cObs;
1085 for(int i = 0; i < 8; i++)
1088 for(int r = 0; r < indVec.size(); r++)
1090 if(indVec.at(r) < pow(10, i-1)+.5)
1091 sumOct += specVec.at(r);
1094 cObs.push_back(sumOct);
1095 sumOct = specVec.at(r);
1098 if(r == indVec.size()-1)
1099 cObs.push_back(sumOct);
1103 //Statistical Analysis
1104 double d = vecCalc.findDStat(cExp, cObs, numSpec);
1105 cout << "DStat = " << d << "\n";
1106 cout << ".05 confidence value = " << .89196/sqrt(numSpec) << "\n";
1107 cout << ".01 confidence value = " << 1.0471/sqrt(numSpec) << "\n\n";
1109 /***********************************************************************/