]> git.donarmstrong.com Git - mothur.git/blob - memchi2.cpp
changes while testing
[mothur.git] / memchi2.cpp
1 /*
2  *  memchi2.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 "memchi2.h"
11
12 /***********************************************************************/
13 EstOutput MemChi2::getValues(vector<SharedRAbundVector*> shared) {
14         try {
15                 data.resize(1,0);
16                 
17                 int nonZeroA = 0;
18                 int nonZeroB = 0;
19                 int totalOtus = shared[0]->getNumBins();
20                 //int totalGroups = shared.size();
21                 
22                 //for each otu
23                 for (int i = 0; i < shared[0]->getNumBins(); i++) {
24                         if (shared[0]->getAbundance(i) != 0) { nonZeroA++; }
25                         if (shared[1]->getAbundance(i) != 0) { nonZeroB++; }
26                 }
27                 
28                 double sum = 0.0;
29                 for (int i = 0; i < shared[0]->getNumBins(); i++) {
30                         int A = shared[0]->getAbundance(i);
31                         int B = shared[1]->getAbundance(i);
32                         
33                         if (A > 0) { A = 1; }
34                         if (B > 0) { B = 1; }
35                         
36                         double Aterm = A / (float) nonZeroA;
37                         double Bterm = B / (float) nonZeroB;
38
39                         int incidence = 0;
40                         for(int j=0;j<shared.size();j++){
41                                 if(shared[j]->getAbundance(i) != 0){    incidence++;    }
42                         }
43                         
44                         if(incidence != 0){
45                                 sum += (((Aterm-Bterm)*(Aterm-Bterm))/incidence);
46                         }
47                 }
48                 
49                 data[0] = sqrt(totalOtus * sum);
50                 
51                 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
52                 
53                 return data;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "MemChi2", "getValues");
57                 exit(1);
58         }
59 }
60 /***********************************************************************/
61