File: sharedjsd.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (51 lines) | stat: -rwxr-xr-x 1,463 bytes parent folder | download | duplicates (4)
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
//
//  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;

        double totalA = shared[0]->getNumSeqs();
        double totalB = shared[1]->getNumSeqs();
        
        for (int i = 0; i < shared[0]->getNumBins(); i++) {
            double tempA = shared[0]->get(i) / totalA;
            double tempB = shared[1]->get(i) / totalB;
            
            if (util.isEqual(tempA, 0)) { tempA = 0.000001; }
            if (util.isEqual(tempB, 0)) { tempB = 0.000001; }

            double denom = (tempA+tempB)/(double)2.0;

            if (!util.isEqual(tempA, 0)) {  KLD1 += tempA * log(tempA/denom); } //KLD(x,m)
            if (!util.isEqual(tempB, 0)) {  KLD2 += tempB * log(tempB/denom); } //KLD(y,m)
        }

            
        data[0] = ((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);
	}
}

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