]> git.donarmstrong.com Git - mothur.git/blobdiff - sharedjsd.cpp
added Jensen-Shannon calc. working on get.communitytype command. fixed bug in get...
[mothur.git] / sharedjsd.cpp
diff --git a/sharedjsd.cpp b/sharedjsd.cpp
new file mode 100644 (file)
index 0000000..46cb977
--- /dev/null
@@ -0,0 +1,48 @@
+//
+//  sharedjsd.cpp
+//  Mothur
+//
+//  Created by SarahsWork on 12/9/13.
+//  Copyright (c) 2013 Schloss Lab. All rights reserved.
+//
+
+#include "sharedjsd.h"
+
+/***********************************************************************/
+//KLD <- function(x,y) sum(x *log(x/y))
+//JSD<- function(x,y) sqrt(0.5 * KLD(x, (x+y)/2) + 0.5 * KLD(y, (x+y)/2))
+EstOutput JSD::getValues(vector<SharedRAbundVector*> shared) {
+       try {
+        
+               data.resize(1,0);
+        
+        double KLD1 = 0.0;
+        double KLD2 = 0.0;
+
+               for (int i = 0; i < shared[0]->getNumBins(); i++) {
+                       //store in temps to avoid multiple repetitive function calls
+                       double tempA = shared[0]->getAbundance(i);
+                       double tempB = shared[1]->getAbundance(i);
+            
+            if (tempA == 0) { tempA = 0.000001; }
+            if (tempB == 0) { tempB = 0.000001; }
+            
+            double denom = (tempA+tempB)/(double)2.0;
+           
+            if (tempA != 0) {  KLD1 += tempA * log(tempA/denom); } //KLD(x,m)
+            if (tempB != 0) {  KLD2 += tempB * log(tempB/denom); } //KLD(y,m)
+        }
+        
+               data[0] = sqrt((0.5*KLD1) + (0.5*KLD2));
+               
+               if (isnan(data[0]) || isinf(data[0])) { data[0] = 0; }
+               
+               return data;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "JSD", "getValues");
+               exit(1);
+       }
+}
+
+/***********************************************************************/