]> git.donarmstrong.com Git - mothur.git/blob - sharedace.cpp
changes while testing
[mothur.git] / sharedace.cpp
1
2 /*
3  *  sharedace.cpp
4  *  Dotur
5  *
6  *  Created by Sarah Westcott on 1/8/09.
7  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
8  *
9  */
10
11 #include "sharedace.h"
12
13 /***********************************************************************/
14
15 EstOutput SharedAce::getValues(vector<SharedRAbundVector*> shared) {
16         try {
17                 data.resize(1,0);
18                 string label;
19                 label = shared[0]->getLabel();
20
21                 double fARare, fBRare, S12Rare, S12Abund, S12, f11, tempA, tempB, t10, t01, t11, t21, t12, t22, C12Numerator;
22                 fARare = 0; fBRare = 0; S12Rare = 0; S12Abund = 0; S12 = 0; f11 = 0; t10 = 0; t01 = 0; t11= 0; t21= 0; t12= 0; t22= 0; C12Numerator = 0;
23         
24                 double Sharedace, C12, part1, part2, part3, part4, part5, Gamma1, Gamma2, Gamma3;
25         
26                 /*fARare = number of OTUs with one individual found in A and less than or equal to 10 in B. 
27                 fBRare = number of OTUs with one individual found in B and less than or equal to 10 in A. 
28                 arare = number of sequences from A that contain less than 10 sequences. 
29                 brare = number of sequences from B that contain less than 10 sequences. 
30                 S12Rare = number of shared OTUs where both of the communities are represented by less than or equal to 10 sequences 
31                 S12Abund = number of shared OTUs where at least one of the communities is represented by more than 10 sequences 
32                 S12 = number of shared OTUs in A and B
33                 This estimator was changed to reflect Caldwell's changes, eliminating the nrare / nrare - 1 */
34
35                 for (int i = 0; i < shared[0]->getNumBins(); i++) {
36                         //store in temps to avoid multiple repetitive function calls
37                         tempA = shared[0]->getAbundance(i);
38                         tempB = shared[1]->getAbundance(i);
39                         if ((tempA != 0) && (tempB != 0)) {//they are shared
40                                 S12++;
41                                 //do both A and B have one
42                                 if ((tempA == 1) && (tempB == 1))               {       f11++;   }
43                                 //is A one and B rare
44                                 if ((tempA == 1) && (tempB <= abund))   {  fARare++; }
45                                 //is B one and A rare
46                                 if ((tempB == 1) && (tempA <= abund))   {  fBRare++; }
47                         
48                                 if ((tempA <= abund) && (tempB <= abund)) { //shared and both rare
49                                         S12Rare++;
50                                         t10 += tempA;   //Sum Xi
51                                         t01 += tempB;   //Sum Yi
52                                         
53                                         //calculate top of C12
54                                         // YiI(Xi = 1)
55                                         if (tempA == 1) { C12Numerator += tempB; }
56                                         //XiI(Yi = 1)
57                                         if (tempB == 1) { C12Numerator += tempA; }
58                                         //-I(Xi=Yi=1)
59                                         if ((tempA == 1) && (tempB == 1)) { C12Numerator--; }
60                                         
61                                         //calculate t11 - Sum of XiYi
62                                         t11 += tempA * tempB;
63                                         //calculate t21  - Sum of Xi(Xi - 1)Yi
64                                         t21 += tempA * (tempA - 1) * tempB;
65                                         //calculate t12  - Sum of Xi(Yi - 1)Yi
66                                         t12 += tempA * (tempB - 1) * tempB;
67                                         //calculate t22  - Sum of Xi(Xi - 1)Yi(Yi - 1)
68                                         t22 += tempA * (tempA - 1) * tempB * (tempB - 1);
69
70                                 }
71                                 if ((tempA > 10) || (tempB > 10)) {
72                                         S12Abund++;
73                                 }
74                         }
75                 }
76         
77                 C12 = 1 - (C12Numerator /(float) t11);
78                 part1 = S12Rare / (float)C12;
79                 part2 = 1 / (float)C12;
80
81                 //calculate gammas
82                 Gamma1 = ((S12Rare * t21) / (float)((C12 * t10 * t11)) - 1);
83                 Gamma2 = ((S12Rare * t12) / (float)((C12 * t01 * t11)) - 1);
84                 Gamma3 = ((S12Rare / C12) * (S12Rare / C12)) * ( t22 / (float)(t10 * t01 * t11));
85                 Gamma3 = Gamma3 - ((S12Rare * t11) / (float)(C12 * t01 * t10)) - Gamma1 - Gamma2;       
86
87                 if (isnan(Gamma1) || isinf(Gamma1)) { Gamma1 = 0; }
88                 if (isnan(Gamma2) || isinf(Gamma2)) { Gamma2 = 0; }
89                 if (isnan(Gamma3) || isinf(Gamma3)) { Gamma3 = 0; }
90                 if (isnan(part1)  || isinf(part1))  { part1 = 0;  }
91                 if (isnan(part2)  || isinf(part2))  { part2 = 0;  }
92                 
93                 part3 = fARare * Gamma1;
94                 part4 = fBRare * Gamma2;
95                 part5 = f11 * Gamma3;
96
97                 Sharedace = S12Abund + part1 + (part2 * (part3 + part4 + part5));
98                 data[0] = Sharedace;
99         
100                 return data;
101         }
102         catch(exception& e) {
103                 m->errorOut(e, "SharedAce", "getValues");
104                 exit(1);
105         }
106 }
107 /***********************************************************************/
108
109