]> git.donarmstrong.com Git - mothur.git/blob - sharedbraycurtis.cpp
changing command name classify.shared to classifyrf.shared
[mothur.git] / sharedbraycurtis.cpp
1 /*
2  *  sharedbraycurtis.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 3/24/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedbraycurtis.h"
11
12 /***********************************************************************/
13 //This is used by SharedJAbund and SharedSorAbund
14 EstOutput BrayCurtis::getValues(vector<SharedRAbundVector*> shared) {
15         try {   
16                 data.resize(1,0);
17                 
18                 double sumSharedA, sumSharedB, sumSharedAB, tempA, tempB;
19                 sumSharedA = 0; sumSharedB = 0; sumSharedAB = 0; 
20                 
21                 /*Xi, Yi = abundance of the ith shared OTU in A and B 
22                 sumSharedA = the number of otus in A
23                 sumSharedB = the sum of all shared otus in B
24                 sumSharedAB = the sum of the minimum otus int all shared otus in AB.
25                 */
26                 
27                 for (int i = 0; i < shared[0]->getNumBins(); i++) {
28                         //store in temps to avoid multiple repetitive function calls
29                         tempA = shared[0]->getAbundance(i);
30                         tempB = shared[1]->getAbundance(i);
31                         
32                         sumSharedA += tempA;
33                         sumSharedB += tempB;
34                                 
35                         //sum the min of tempA and tempB
36                         if (tempA < tempB) { sumSharedAB += tempA; }
37                         else  { sumSharedAB += tempB; }                         
38                 }
39                 
40                 data[0] = 1.0 - (2 * sumSharedAB) / (float)( sumSharedA + sumSharedB);
41                 
42                 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
43                                 
44                 return data;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "BrayCurtis", "getValues");
48                 exit(1);
49         }
50 }
51
52 /***********************************************************************/