-
Notifications
You must be signed in to change notification settings - Fork 4
/
Fisher2x2.py
executable file
·54 lines (48 loc) · 1.92 KB
/
Fisher2x2.py
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
#=========================================================================
# This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
# License (GPL) version 3, as described at www.opensource.org.
# Copyright (C)2016 William H. Majoros (martiandna@gmail.com).
#=========================================================================
from __future__ import (absolute_import, division, print_function,
unicode_literals, generators, nested_scopes, with_statement)
from builtins import (bytes, dict, int, list, object, range, str, ascii,
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
from Pipe import Pipe
#=========================================================================
# Attributes:
# Matrix of the form:
# x00 x01
# x10 x11
# Instance Methods:
# fisher=Fisher2x2(x00,x01,x10,x11)
# P=fisher.getPvalue()
# (exp00,exp01,exp10,exp11)=fisher.getExpectedCounts()
# Class Methods:
#
#=========================================================================
class Fisher2x2:
"""Fisher2x2 performs Fisher's exact test for 2x2 contingency tables"""
def __init__(self,x00,x01,x10,x11):
self.x00=x00
self.x01=x01
self.x10=x10
self.x11=x11
def getPvalue(self):
executable=Pipe.run("which fisher-exact-test.R")
cmd=executable+" "+str(self.x00)+" "+str(self.x01)+" "+\
str(self.x10)+" "+str(self.x11)
P=float(Pipe.run(cmd))
return P
def getExpectedCounts(self):
x00=float(self.x00); x01=float(self.x01)
x10=float(self.x10); x11=float(self.x11)
N=x00+x01+x10+x11
pTop=(x00+x01)/N
pBottom=1.0-pTop
leftSum=x00+x10
rightSum=x01+x11
exp00=int(round(pTop*leftSum,0))
exp01=int(round(pTop*rightSum,0))
exp10=int(round(pBottom*leftSum,0))
exp11=int(round(pBottom*rightSum,0))
return (exp00,exp01,exp10,exp11)