]> git.donarmstrong.com Git - mothur.git/blob - structchi2.cpp
changes while testing
[mothur.git] / structchi2.cpp
1 /*
2  *  structchi2.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 12/17/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "structchi2.h"
11
12 /***********************************************************************/
13 EstOutput StructChi2::getValues(vector<SharedRAbundVector*> shared) {
14         try {
15                 data.resize(1,0);
16                 
17                 double sumA = shared[0]->getNumSeqs();
18                 double sumB = shared[1]->getNumSeqs();
19                 double totalSum = 0.0;
20                 
21                 for (int i = 0; i < shared.size(); i++) { totalSum += shared[i]->getNumSeqs();  }
22                 
23                 vector<int> sumOtus; sumOtus.resize(shared[0]->getNumBins(), 0);
24                 //for each otu
25                 for (int i = 0; i < shared[0]->getNumBins(); i++) {
26                         //for each group
27                         for (int j = 0; j < shared.size(); j++) { 
28                                 sumOtus[i] += shared[j]->getAbundance(i);
29                         }
30                 }
31                 
32                 double sum = 0.0;
33                 for (int i = 0; i < shared[0]->getNumBins(); i++) {
34                         int A = shared[0]->getAbundance(i);
35                         int B = shared[1]->getAbundance(i);
36                         
37                         double totalTerm = 1 / (float) sumOtus[i];
38                         double Aterm = A / sumA;
39                         double Bterm = B / sumB;
40                         
41                         sum += (totalTerm * ((Aterm-Bterm)*(Aterm-Bterm)));
42                 }
43                                 
44                 data[0] = sqrt((totalSum * sum));
45                 
46                 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
47                 
48                 return data;
49         }
50         catch(exception& e) {
51                 m->errorOut(e, "StructChi2", "getValues");
52                 exit(1);
53         }
54 }
55 /***********************************************************************/
56