]> git.donarmstrong.com Git - mothur.git/blob - spearman.cpp
changes while testing
[mothur.git] / spearman.cpp
1 /*
2  *  spearman.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 12/15/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "spearman.h"
11
12 /***********************************************************************/
13
14 EstOutput Spearman::getValues(vector<SharedRAbundVector*> shared) {
15         try {
16                 data.resize(1,0);
17                 
18                 SAbundVector savA = shared[0]->getSAbundVector();
19                 SAbundVector savB = shared[1]->getSAbundVector();
20                 
21                 double sumRanks = 0.0;
22                 int numOTUS = shared[0]->getNumBins();
23                 
24                 vector<double> rankVectorA(savA.getMaxRank()+1, 0);
25                 int currentRankA = 0;
26                 for(int i=savA.getMaxRank();i>0;i--){   
27                         int numWithAbundanceI = savA.get(i);
28
29                         if(numWithAbundanceI > 1){      rankVectorA[i] = (currentRankA + 1 + currentRankA + numWithAbundanceI) / 2.0;   }
30                         else                                    {       rankVectorA[i] = currentRankA+numWithAbundanceI;        }
31                         currentRankA += numWithAbundanceI;
32                 }
33                 rankVectorA[0] = (numOTUS + currentRankA + 1) / 2.0;
34                 
35                 
36                 vector<double> rankVectorB(savB.getMaxRank()+1, 0);
37                 int currentRankB = 0;
38                 for(int i=savB.getMaxRank();i>0;i--){   
39                         int numWithAbundanceI = savB.get(i);
40                         
41                         if(numWithAbundanceI > 1){      rankVectorB[i] = (currentRankB + 1 + currentRankB + numWithAbundanceI) / 2.0;   }
42                         else                                    {       rankVectorB[i] = currentRankB+numWithAbundanceI;        }
43                         currentRankB += numWithAbundanceI;
44                 }
45                 rankVectorB[0] = (numOTUS + currentRankB + 1) / 2.0;
46                 
47                 
48
49                 for (int i = 0; i < shared[0]->getNumBins(); i++) { 
50                         int Aij = shared[0]->getAbundance(i);
51                         int Bij = shared[1]->getAbundance(i);
52                         
53                         float rankA = rankVectorA[Aij];
54                         float rankB = rankVectorB[Bij];
55                         
56                         sumRanks += ((rankA - rankB) * (rankA - rankB));
57                 }
58                 data[0] = 1.0 - ((6 * sumRanks) / (float) (numOTUS * ((numOTUS*numOTUS)-1)));
59                 
60                 if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
61                 
62                 return data;
63         }
64         catch(exception& e) {
65                 m->errorOut(e, "Spearman", "getValues");
66                 exit(1);
67         }
68 }
69
70 /***********************************************************************/
71