]> git.donarmstrong.com Git - mothur.git/blob - gower.cpp
working on pam
[mothur.git] / gower.cpp
1 /*
2  *  gower.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 "gower.h"
11
12 /***********************************************************************/
13 EstOutput Gower::getValues(vector<SharedRAbundVector*> shared) {
14         try {
15                 data.resize(1,0);
16                 
17                 vector<int> maxOtus; maxOtus.resize(shared[0]->getNumBins());
18                 vector<int> minOtus; minOtus.resize(shared[0]->getNumBins());
19                 //for each otu
20                 for (int i = 0; i < shared[0]->getNumBins(); i++) {
21                         
22                         //set otus min and max to first one
23                         minOtus[i] = shared[0]->getAbundance(i);
24                         maxOtus[i] = shared[0]->getAbundance(i);
25                         
26                         //for each group
27                         for (int j = 1; j < shared.size(); j++) { 
28                                 maxOtus[i] = max(shared[j]->getAbundance(i), maxOtus[i]);
29                                 minOtus[i] = min(shared[j]->getAbundance(i), minOtus[i]);
30                         }
31                 }
32                 
33                 double sum = 0.0;
34                 for (int i = 0; i < shared[0]->getNumBins(); i++) {
35                         int A = shared[0]->getAbundance(i);
36                         int B = shared[1]->getAbundance(i);
37                         
38                         double numerator = abs(A - B);
39                         double denominator = maxOtus[i] - minOtus[i];
40                                 
41                         if (denominator != 0) { sum += (numerator / denominator); }
42                 }
43                 
44                 data[0] = 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, "Gower", "getValues");
52                 exit(1);
53         }
54 }
55 /***********************************************************************/
56