X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=spearman.cpp;h=49d5265259bc2ceb566d6f88f71fda30130b7945;hp=72d5667e97a0e6a0a499464fe529699212ae8a91;hb=1a20e24ee786195ab0e1cccd4f5aede7a88f3f4e;hpb=75c5a235ac3eb22e0f97d36874f4b2dcf9591f2e diff --git a/spearman.cpp b/spearman.cpp index 72d5667..49d5265 100644 --- a/spearman.cpp +++ b/spearman.cpp @@ -10,6 +10,7 @@ #include "spearman.h" /***********************************************************************/ + EstOutput Spearman::getValues(vector shared) { try { data.resize(1,0); @@ -20,28 +21,51 @@ EstOutput Spearman::getValues(vector shared) { double sumRanks = 0.0; int numOTUS = shared[0]->getNumBins(); - //calc the 2 denominators - for (int i = 0; i < shared[0]->getNumBins(); i++) { + vector 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 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); - int rankA = savA.get(Aij); - int rankB = savB.get(Bij); + float rankA = rankVectorA[Aij]; + float rankB = rankVectorB[Bij]; sumRanks += ((rankA - rankB) * (rankA - rankB)); } - - data[0] = 1.0 - ((6 * sumRanks) / (float) (numOTUS * (numOTUS-1))); + 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, "Soergel", "getValues"); + m->errorOut(e, "Spearman", "getValues"); exit(1); } } + /***********************************************************************/