File: memchi2.cpp

package info (click to toggle)
mothur 1.33.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,248 kB
  • ctags: 12,231
  • sloc: cpp: 152,046; fortran: 665; makefile: 74; sh: 34
file content (61 lines) | stat: -rw-r--r-- 1,413 bytes parent folder | download | duplicates (3)
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
52
53
54
55
56
57
58
59
60
61
/*
 *  memchi2.cpp
 *  Mothur
 *
 *  Created by westcott on 12/17/10.
 *  Copyright 2010 Schloss Lab. All rights reserved.
 *
 */

#include "memchi2.h"

/***********************************************************************/
EstOutput MemChi2::getValues(vector<SharedRAbundVector*> 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 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;

			int incidence = 0;
			for(int j=0;j<shared.size();j++){
				if(shared[j]->getAbundance(i) != 0){	incidence++;	}
			}
			
			if(incidence != 0){
				sum += (((Aterm-Bterm)*(Aterm-Bterm))/incidence);
			}
		}
		
		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);
	}
}
/***********************************************************************/