-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnpfiltering (2).py
183 lines (154 loc) · 5.84 KB
/
snpfiltering (2).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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
import pandas
import fileinput
import math
import numpy as np
from scipy.stats import chisquare
fcase = "P:\\modeling\\testcasefile_transpose.dat"
fcontrol = "P:\\modeling\\testcontrolfile_transpose.dat"
rsgenetypelogfile = "P:\\modeling\\rsnumericlog.dat"
ratethreshold = 0.9
def getrateDicfromfile(f):
d = {}
id = ''
i = 0
for line in fileinput.input(f):
sd = {}
i = i + 1
if i > 2:
arr = line.strip().split("\t")
id = arr[0]
onenumber = 0.0
zeronumber = 0.0
halfnumber = 0.0
s = len(arr) - 1
for e in arr:
if e == '0':
zeronumber = zeronumber + 1
if e == '1':
onenumber = onenumber + 1
if e == '0.5':
halfnumber = halfnumber + 1
if not s == onenumber + zeronumber + halfnumber:
print 'not match'
sd['0'] = zeronumber
sd['1'] = onenumber
sd['0.5'] = halfnumber
sd['sum'] = s
d[id] = sd
return d
def getDicfromfile(f):
d = {}
for line in fileinput.input(f):
subd = {}
arr = line.strip().split("\t")
subd['0'] = arr[1]
subd['0.5'] = arr[2]
subd['1'] = arr[3]
d[arr[0]] = subd
return d
def judgeifmatchhw(aa, ac, cc): # aa是D6,ac是D7,cc是D8
totalnumber = aa + ac + cc # d9
vdic={}
anumber = aa * 2 + ac # D11
cnumber = cc * 2 + ac # D12
totalloci = anumber + cnumber # D13
if totalloci == 0:
return -1
else:
af = float(anumber) / float(totalloci) # F11
cf = float(cnumber) / float(totalloci) # F12
preaanumber = totalnumber * math.pow(af, 2) # E6
preacnumber = totalnumber * af * 2 * cf # E7
preccnumber = totalnumber * math.pow(cf, 2) # E8
# chisquare = math.pow((aa - preaanumber), 2) / preaanumber + math.pow((ac - preacnumber),
# 2) / preacnumber + math.pow(
# (cc - preccnumber), 2) / preccnumber
expected = [preaanumber,preacnumber,preccnumber]
observed = [aa,ac,cc]
ch,p = chisquare(observed, expected)
print
vdic['chi'] = ch
vdic['pvalue'] = p
return vdic
rstypeDic = getDicfromfile(rsgenetypelogfile)
firstdiscardIDlst = []
chidiscardIDlist = []
qualifiedsnpIDlist = []
#
casersDic = getrateDicfromfile(fcase)
controlDic = getrateDicfromfile(fcontrol)
#
for c in casersDic.keys():
if not c.startswith('rs'):
continue
else:
vcase = casersDic[c]
vcontrol = controlDic[c]
vcasesum = vcase['sum']
vcontrolsum = vcontrol['sum']
casekeylst = vcase.keys()
controlkeylst = vcontrol.keys()
onecasenumber = vcase['1']
halfcasenumber = vcase['0.5']
zerocasenumber = vcase['0']
onecontrolnumber = vcontrol['1']
halfcontrolnumber = vcontrol['0.5']
zerocontrolnumber = vcontrol['0']
onecaserate = float(onecasenumber) / float(vcasesum)
halfcaserate = float(halfcasenumber) / float(vcasesum)
zerocaserate = float(zerocasenumber) / float(vcasesum)
onecontrolrate = float(onecontrolnumber) / float(vcontrolsum)
halfcontrolrate = float(halfcontrolnumber) / float(vcontrolsum)
zerocontrolrate = float(zerocontrolnumber) / float(vcontrolsum)
b1 = (onecaserate > ratethreshold) and (onecontrolrate > ratethreshold)
b2 = (zerocaserate > ratethreshold) and (zerocontrolrate > ratethreshold)
b3 = (halfcaserate > ratethreshold) and (halfcontrolrate > ratethreshold)
if b1 or b2 or b3:
firstdiscardIDlst.append(c)
typedic = rstypeDic[c]
onetype = typedic['1']
halftype = typedic['0.5']
zerotype = typedic['0']
caseaanumber = 0
caseacnumber = 0
caseccnumber = 0
controlaanumber = 0
controlacnumber = 0
controlccnumber = 0
if vcase.has_key('1') and vcase.has_key('0.5') and vcase.has_key('0'):
if len(set(onetype)) == 2:
caseacnumber = vcase['1']
caseaanumber = vcase['0']
caseccnumber = vcase['0.5']
elif len(set(halftype)) == 2:
caseacnumber = vcase['0.5']
caseaanumber = vcase['0']
caseccnumber = vcase['1']
else:
caseacnumber = vcase['0']
caseaanumber = vcase['0.5']
caseccnumber = vcase['1']
else:
print 'case: ' + c
if vcontrol.has_key('1') and vcontrol.has_key('0.5') and vcontrol.has_key('0'):
if len(set(onetype)) == 2:
controlacnumber = vcontrol['1']
controlaanumber = vcontrol['0']
controlccnumber = vcontrol['0.5']
elif len(set(halftype)) == 2:
controlacnumber = vcontrol['0.5']
controlaanumber = vcontrol['0']
controlccnumber = vcontrol['1']
else:
controlacnumber = vcontrol['0']
controlaanumber = vcontrol['0.5']
controlccnumber = vcontrol['1']
else:
print 'control: ' + c
chicase = judgeifmatchhw(caseaanumber, caseacnumber, caseccnumber)
chicontrol = judgeifmatchhw(controlaanumber, controlacnumber, controlccnumber)
if chicase['pvalue'] < math.pow(10,-4) or chicontrol['pvalue'] < math.pow(10,-7):
chidiscardIDlist.append(c)
print len(firstdiscardIDlst)
print len(chidiscardIDlist)