File: spearman.cpp

package info (click to toggle)
mothur 1.33.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,248 kB
  • ctags: 12,231
  • sloc: cpp: 152,046; fortran: 665; makefile: 74; sh: 34
file content (71 lines) | stat: -rw-r--r-- 1,974 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 *  spearman.cpp
 *  Mothur
 *
 *  Created by westcott on 12/15/10.
 *  Copyright 2010 Schloss Lab. All rights reserved.
 *
 */

#include "spearman.h"

/***********************************************************************/

EstOutput Spearman::getValues(vector<SharedRAbundVector*> shared) {
	try {
		data.resize(1,0);
		
		SAbundVector savA = shared[0]->getSAbundVector();
		SAbundVector savB = shared[1]->getSAbundVector();
		
		double sumRanks = 0.0;
		int numOTUS = shared[0]->getNumBins();
		
		vector<double> rankVectorA(savA.getMaxRank()+1, 0);
		int currentRankA = 0;
		for(int i=savA.getMaxRank();i>0;i--){	
			int numWithAbundanceI = savA.get(i);

			if(numWithAbundanceI > 1){	rankVectorA[i] = (currentRankA + 1 + currentRankA + numWithAbundanceI) / 2.0;	}
			else					{	rankVectorA[i] = currentRankA+numWithAbundanceI;	}
			currentRankA += numWithAbundanceI;
		}
		rankVectorA[0] = (numOTUS + currentRankA + 1) / 2.0;
		
		
		vector<double> rankVectorB(savB.getMaxRank()+1, 0);
		int currentRankB = 0;
		for(int i=savB.getMaxRank();i>0;i--){	
			int numWithAbundanceI = savB.get(i);
			
			if(numWithAbundanceI > 1){	rankVectorB[i] = (currentRankB + 1 + currentRankB + numWithAbundanceI) / 2.0;	}
			else					{	rankVectorB[i] = currentRankB+numWithAbundanceI;	}
			currentRankB += numWithAbundanceI;
		}
		rankVectorB[0] = (numOTUS + currentRankB + 1) / 2.0;
		
		

		for (int i = 0; i < shared[0]->getNumBins(); i++) { 
			int Aij = shared[0]->getAbundance(i);
			int Bij = shared[1]->getAbundance(i);
			
			float rankA = rankVectorA[Aij];
			float rankB = rankVectorB[Bij];
			
			sumRanks += ((rankA - rankB) * (rankA - rankB));
		}
		data[0] = 1.0 - ((6 * sumRanks) / (float) (numOTUS * ((numOTUS*numOTUS)-1)));
		
		if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
		
		return data;
	}
	catch(exception& e) {
		m->errorOut(e, "Spearman", "getValues");
		exit(1);
	}
}

/***********************************************************************/