Repository navigation
Expand file tree
/
Copy pathstats.py
More file actions
142 lines (119 loc) · 4.53 KB
/
Copy pathstats.py
File metadata and controls
142 lines (119 loc) · 4.53 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from decimal import *
import math
import numpy
import scipy
class GTStats():
'Class for calculating summary statistics'
def __init__(self):
self.l = list()
self.mean = 0.0
self.stdev = 0.0
self.med = 0.0
self.mmin = 0.0
self.mmax = 0.0
def calcStats(self, l):
self.l = l
if len(self.l)!=0:
self.mean = self.calcMean(self.l)
self.stdev = self.calcStdev(self.l, self.mean)
self.med = self.calcMed(self.l)
self.mmin = min(self.l)
self.mmax = max(self.l)
def calcMean(self,l):
total = self.calcSum(l)
mean = (total/len(l))
return mean
def calcSum(self, l):
total = Decimal()
for item in l:
total+=item
return total
def calcStdev(self, l, mean):
vals = list()
for val in l:
dev = (val-mean)**2
vals.append(dev)
total = self.calcSum(vals)
if len(l)-1 == 0:
return 0
else:
temp = total/Decimal((len(l)-1))
stdev = Decimal(math.sqrt(temp))
return stdev
def calcMed(self,l):
sl = sorted(l)
llen = len(l)
i = (llen-1) // 2
if(llen % 2):
return sl[i]
else:
return Decimal((sl[i] + sl[i+1])/Decimal(2))
def printStats(self, fn, mode1, mode2):
if mode1 == "ifi scores":
print(f"IFI score summary for {mode2} individuals:")
elif mode1 == "heterozygosity":
prepost = mode2.capitalize()
print(f"{prepost} heterozygosity summary for individuals:")
else:
print(f"Proportion of missing data summary for {mode1} {mode2}:")
print("Mean\tStDev\tMedian\tMin\tMax")
fh = open(fn, 'a')
if mode1 == "ifi scores":
fh.write("IFI score stats for " + str(len(self.l)) + " individuals " + mode2 + ":\n")
else:
fh.write(str(len(self.l)) + " " + mode2 + " were detected " + mode1 + ".\n")
fh.write("Missing data stats for " + mode1 + " " + mode2 + ":\n")
fh.write("Mean\tStDev\tMedian\tMin\tMax\n")
print(round(self.mean,3), "\t", round(self.stdev,3), "\t", round(self.med,3), "\t", round(self.mmin,3), "\t", round(self.mmax,3), "\n")
fh.write(str(round(self.mean,3)) + "\t" + str(round(self.stdev,3)) + "\t" + str(round(self.med,3)) + "\t" + str(round(self.mmin,3)) + "\t" + str(round(self.mmax,3)) + "\n\n")
fh.close()
def chisq(self, start, end, fn):
fh = open( fn, 'a')
totalIn = start.sum() # total samples input
totalOut = end.total() # total samples output
pctRetained = float(totalOut / totalIn) # percentage of retained individuals
obsList = list()
expList = list()
for k,v in start.items():
if k in end:
exp = self.expected(start[k], pctRetained)
print("{}\t{}\t{}\t{}".format(k, v, end[k], "{:.2f}".format(exp)))
fh.write(str(k) + "\t" + str(v) + "\t" + str(end[k]) + "\t" + "{:.2f}".format(exp) + "\n")
obsList.append(float(end[k]))
expList.append(float(exp))
else:
exp = self.expected(start[k], pctRetained)
print("{}\t{}\t{}\t{}".format(k, v, "0", "{:.2f}".format(exp)))
fh.write(str(k) + "\t" + str(v) + "\t" + str("0") + "\t" + "{:.2f}".format(exp) + "\n")
obsList.append(float(0))
expList.append(float(exp))
print("{}\t{}\t{}\t{}".format("Total", str(totalIn), str(totalOut), "N/A"))
print("")
fh.write(str("Total\t") + str(totalIn) + "\t" + str(totalOut) + "\tN/A" + "\n\n")
# test if more than one population input and/or retained before performing chisquare test
if len(obsList) > 1:
try:
# chisquare test using scipy library
#chisq = scipy.stats.chisquare(obsList, f_exp=expList) # old code
## folowing example from here: https://github.com/scipy/scipy/issues/12282
expList_scaled = numpy.array(expList) * (numpy.sum(obsList) / numpy.sum(expList)) # new code
chisq = scipy.stats.chisquare(f_obs=obsList, f_exp=expList_scaled) # new code
df = len(obsList)-1
print("Performing chi squared test to evaluate if missing individuals are evenly distributed among sample groups.")
print("chisq\tdf\tp")
print(str("{:.3f}".format(chisq[0])), "\t", str(df), "\t", str("{:.3f}".format(chisq[1])))
print("")
fh.write("Performing chi squared test to evaluate if missing individuals are evenly distributed among sample groups.\n")
fh.write("chisq\tdf\tp\n")
fh.write(str("{:.3f}".format(chisq[0])) + "\t" + str(df) + "\t" + str("{:.3f}".format(chisq[1])) + "\n\n")
except ValueError as e:
print("ERROR: chisquare test failed.")
print("Error message: " + str(e))
print("")
else:
print("Chi squared test not performed because only one population was analyzed.\n")
fh.write("Chi squared test not performed because only one population was analyzed.\n\n")
fh.close()
def expected(self, inInds, pctRet):
exp = inInds * pctRet
return exp