X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=memchi2.cpp;fp=memchi2.cpp;h=07edd66475c9680cdacd5b9ff7920863323dd525;hb=7d38edc137a66a33f67f8cf55cce88331290aaf7;hp=0000000000000000000000000000000000000000;hpb=75c5a235ac3eb22e0f97d36874f4b2dcf9591f2e;p=mothur.git diff --git a/memchi2.cpp b/memchi2.cpp new file mode 100644 index 0000000..07edd66 --- /dev/null +++ b/memchi2.cpp @@ -0,0 +1,55 @@ +/* + * memchi2.cpp + * Mothur + * + * Created by westcott on 12/17/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "memchi2.h" + +/***********************************************************************/ +EstOutput MemChi2::getValues(vector shared) { + try { + data.resize(1,0); + + int nonZeroA = 0; + int nonZeroB = 0; + int totalOtus = shared[0]->getNumBins(); + int totalGroups = shared.size(); + + //for each otu + for (int i = 0; i < shared[0]->getNumBins(); i++) { + if (shared[0]->getAbundance(i) != 0) { nonZeroA++; } + if (shared[1]->getAbundance(i) != 0) { nonZeroB++; } + } + + double totalTerm = 1 / (float) totalGroups; + double sum = 0.0; + for (int i = 0; i < shared[0]->getNumBins(); i++) { + int A = shared[0]->getAbundance(i); + int B = shared[1]->getAbundance(i); + + if (A > 0) { A = 1; } + if (B > 0) { B = 1; } + + double Aterm = A / (float) nonZeroA; + double Bterm = B / (float) nonZeroB; + + sum += (totalTerm * ((Aterm-Bterm)*(Aterm-Bterm))); + } + + data[0] = sqrt((totalOtus * sum)); + + if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; } + + return data; + } + catch(exception& e) { + m->errorOut(e, "MemChi2", "getValues"); + exit(1); + } +} +/***********************************************************************/ +