From: westcott Date: Tue, 8 Feb 2011 16:24:38 +0000 (+0000) Subject: fixed homova command X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=commitdiff_plain;h=c887ae6cd9f1c5b6e1b73a1384f154392be4efa3 fixed homova command --- diff --git a/homovacommand.cpp b/homovacommand.cpp index f9bfb10..86a900a 100644 --- a/homovacommand.cpp +++ b/homovacommand.cpp @@ -822,21 +822,17 @@ int HomovaCommand::calcHomova(ofstream& out, int numTreatments, vector t map SSWithin; map::iterator it; - //SSTotal = 0.0; - SSTotal = calcTotal(numTreatments); - cout << "sstotal = " << SSTotal << " (n-P) = " << (matrix.size() - numTreatments) << endl; + SSTotal = calcWithin(matrix, numTreatments, thisCombosLookupSets); int numSamples = matrix.size(); //calc BValue map counts; - SSWithin = calcWithin(matrix, numTreatments, thisCombosLookupSets, counts); + SSWithin = calcWithinEach(matrix, numTreatments, thisCombosLookupSets, counts); double sum = 0.0; double sumDenom = 0.0; for (it = SSWithin.begin(); it != SSWithin.end(); it++) { - cout << it->first << '\t' << it->second << '\t' << (counts[it->first] - numTreatments) << endl; double temp2 = (it->second) / (double) (counts[it->first] - 1); - cout << "sumTerm = " << temp2 << '\t' << "ln sumTerm = " << log(temp2) << endl; sum += ((counts[it->first] - 1) * log(temp2)); sumDenom += ((1 / (double) (counts[it->first] - 1)) - ( 1 / (double) (numSamples - numTreatments) )); } @@ -847,7 +843,6 @@ int HomovaCommand::calcHomova(ofstream& out, int numTreatments, vector t double denom = 1 + ((1 / (double) (3 * (numTreatments - 1))) * sumDenom); BValue = numerator / denom; - cout << "numeratorTerm1 = " << numeratorTerm1 << " sum = " << sum << " sumDenom = " << sumDenom << " numerator = " << numerator << " denom = " << denom << " B = " << BValue << endl; //calc Pvalue int count = 0; @@ -858,8 +853,10 @@ int HomovaCommand::calcHomova(ofstream& out, int numTreatments, vector t vector copyNames = thisCombosLookupSets; random_shuffle(copyNames.begin(), copyNames.end()); + SSTotal = calcWithin(matrix, numTreatments, copyNames); + counts.clear(); - map randomSSWithin = calcWithin(matrix, numTreatments, copyNames, counts); + map randomSSWithin = calcWithinEach(matrix, numTreatments, copyNames, counts); sum = 0.0; sumDenom = 0.0; @@ -869,12 +866,14 @@ int HomovaCommand::calcHomova(ofstream& out, int numTreatments, vector t sumDenom += ((1 / (double) (counts[it->first] - 1)) - ( 1 / (double) (numSamples - numTreatments) )); } + temp = SSTotal / (double) (numSamples - numTreatments); + numeratorTerm1 = (numSamples - numTreatments) * log(temp); numerator = numeratorTerm1 - sum; denom = 1 + ((1 / (double) (3 * (numTreatments - 1))) * sumDenom); double randomBValue = numerator / denom; - if (randomBValue >= BValue) { count++; } + if (randomBValue <= BValue) { count++; } } pValue = count / (float) iters; @@ -889,7 +888,7 @@ int HomovaCommand::calcHomova(ofstream& out, int numTreatments, vector t } } //********************************************************************************************************************** -map HomovaCommand::calcWithin(vector< vector >& thisMatrix, int numTreatments, vector thisCombosLookupSets, map& count) { +map HomovaCommand::calcWithinEach(vector< vector >& thisMatrix, int numTreatments, vector thisCombosLookupSets, map& count) { try { map within; //maps treatment to within value map::iterator it; @@ -928,29 +927,33 @@ map HomovaCommand::calcWithin(vector< vector >& thisMatr return within; } catch(exception& e) { - m->errorOut(e, "HomovaCommand", "calcWithin"); + m->errorOut(e, "HomovaCommand", "calcWithinEach"); exit(1); } } //********************************************************************************************************************** -double HomovaCommand::calcTotal(int numTreatments) { +double HomovaCommand::calcWithin(vector< vector >& thisMatrix, int numTreatments, vector thisCombosLookupSets) { try { double total = 0.0; //traverse lower triangle - for (int k = 0; k < matrix.size(); k++) { - for (int l = k; l < matrix[k].size(); l++) { - total += (matrix[k][l] * matrix[k][l]); //dij^2 + for (int k = 0; k < thisMatrix.size(); k++) { + for (int l = k; l < thisMatrix[k].size(); l++) { + + //if you are from the same treatment then eij is 1 so add, else eij = 0 + if (thisCombosLookupSets[k] == thisCombosLookupSets[l]) { + total += (thisMatrix[k][l] * thisMatrix[k][l]); //dij^2 + } } } - //1 / numSamples - total *= (1.0 / (float) matrix.size()); + //1 / (numSamples / numTreatments) + total *= (1.0 / (float) (thisMatrix.size() / (float) numTreatments)); return total; } catch(exception& e) { - m->errorOut(e, "HomovaCommand", "calcTotal"); + m->errorOut(e, "HomovaCommand", "calcWithin"); exit(1); } } diff --git a/homovacommand.h b/homovacommand.h index 1ac2aa0..55e4eac 100644 --- a/homovacommand.h +++ b/homovacommand.h @@ -57,7 +57,8 @@ private: int driver(int, int, vector, string, vector< vector >&); int process(vector); int calcHomova(ofstream&, int, vector); - map calcWithin(vector< vector >&, int, vector, map&); + map calcWithinEach(vector< vector >&, int, vector, map&); + double calcWithin(vector< vector >&, int, vector); double calcTotal(int); };