File: chao1.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (77 lines) | stat: -rwxr-xr-x 2,147 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 *  chao1.cpp
 *  Dotur
 *
 *  Created by Sarah Westcott on 1/7/09.
 *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
 *
 */

#include "chao1.h"

/***********************************************************************/
EstOutput Chao1::getValues(SAbundVector* rank){
	try {
		data.resize(3,0);
	
		double sobs = (double)rank->getNumBins();
		
		//this is a modification do to a vector fill error that occurs when an empty sharedRabund creates a sabund
		//in that case there is no 1 0r 2.
		double singles;
		if (rank->size() > 1) {
			singles = (double)rank->get(1);
		}else{ singles = 0.0;  }

		double doubles;
		if (rank->size() > 2) {
			 doubles = (double)rank->get(2);
		}else{ doubles = 0.0;  }

		double chaovar = 0.0000;
		double chao = sobs + singles*(singles-1)/(2*(doubles+1));
	
		if(singles > 0 && doubles > 0){
			chaovar = singles*(singles-1)/(2*(doubles+1))
					+ singles*pow(2*singles-1, 2)/(4*pow(doubles+1,2))
					+ pow(singles, 2)*doubles*pow(singles-1, 2)/(4*pow(doubles+1,4));
		}
		else if(singles > 0 && util.isEqual(doubles,0)){
			chaovar = singles*(singles-1)/2 + singles*pow(2*singles-1, 2)/4 - pow(singles, 4)/(4*chao); 
		}
		else if(singles == 0){
			chaovar = sobs*exp(-1*rank->getNumSeqs()/sobs)*(1-exp(-1*rank->getNumSeqs()/sobs));
		}
			
		double chaohci, chaolci;
	
		if(singles>0){
			double denom = pow(chao-sobs,2);
			double c = exp(1.96*pow((log(1+chaovar/denom)),0.5));
			chaolci = sobs+(chao-sobs)/c;//chao lci
			chaohci = sobs+(chao-sobs)*c;//chao hci
		}
		else{
			double p = exp(-1*rank->getNumSeqs()/sobs);
			chaolci = sobs/(1-p)-1.96*pow(sobs*p/(1-p), 0.5);
			chaohci = sobs/(1-p)+1.96*pow(sobs*p/(1-p), 0.5);
			if(chaolci < sobs){	chaolci = sobs;	}
		}
		
		data[0] = chao;
		data[1] = chaolci;
		data[2] = chaohci;

	    if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
		if (isnan(data[1]) || isinf(data[1])) { data[1] = 0; }
		if (isnan(data[2]) || isinf(data[2])) { data[2] = 0; }
		
		return data;
	}
	catch(exception& e) {
		m->errorOut(e, "Chao1", "getValues");
		exit(1);
	}
}

/***********************************************************************/