]> git.donarmstrong.com Git - mothur.git/blob - sharedbraycurtis.cpp
removed "shared" from some of the calculator names and classes
[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(SharedRAbundVector* shared1, SharedRAbundVector* shared2) {
15         try {   
16                 EstOutput data;
17                 data.resize(1,0);
18                 
19                 int sumSharedA, sumSharedB, sumSharedAB, tempA, tempB;
20                 sumSharedA = 0; sumSharedB = 0; sumSharedAB = 0; 
21                 
22                 /*Xi, Yi = abundance of the ith shared OTU in A and B 
23                 sumSharedA = the sum of all shared otus in A
24                 sumSharedB = the sum of all shared otus in B
25                 sumSharedAB = the sum of the minimum otus int all shared otus in AB.
26                 */
27                 
28                 for (int i = 0; i < shared1->size(); i++) {
29                         //store in temps to avoid multiple repetitive function calls
30                         tempA = shared1->getAbundance(i);
31                         tempB = shared2->getAbundance(i);
32
33                         
34                         if ((tempA != 0) && (tempB != 0)) {//they are shared
35                                 sumSharedA += tempA;
36                                 sumSharedB += tempB;
37                                 
38                                 //sum the min of tempA and tempB
39                                 if (tempA < tempB) { sumSharedAB += tempA; }
40                                 else  { sumSharedAB += tempB; }                         
41                         }
42                 }
43                 
44                 data[0] = (2 * sumSharedAB) / (float)( sumSharedA + sumSharedB);
45                 
46                 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
47                                 
48                 return data;
49         }
50         catch(exception& e) {
51                 cout << "Standard Error: " << e.what() << " has occurred in the BrayCurtis class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
52                 exit(1);
53         }
54         catch(...) {
55                 cout << "An unknown error has occurred in the BrayCurtis class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
56                 exit(1);
57         }       
58
59
60 }
61
62 /***********************************************************************/