-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiphenotypeanalysispipeline.py
455 lines (402 loc) · 15.5 KB
/
multiphenotypeanalysispipeline.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# -*- coding: utf-8 -*-
import pandas
import fileinput
import math
import numpy as np
from scipy.stats import chisquare
import random
alldatafile = "P:\\modeling\\new\\multi_phenos\\all_phenotype_snp_file.dat"
phenotype_matrix_file_prefix = "P:\\modeling\\new\\multi_phenos\\all_phenotype_snp_file_"
file2writelst = []
mainpf = "P:\\modeling\\new\mainphenoype_logistic\\new_finalnumericgenotype.dat"
rsgenetypelogfile = "P:\\modeling\\rsnumericlog.dat"
# ======================================函数定义区
# 抽样函数区
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
# ===============
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 getrateDicfromfile(f):
d = {}
i = 0
errSNPset = set()
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' or e == 0:
zeronumber = zeronumber + 1
if e == '1' or e == 1:
onenumber = onenumber + 1
if e == '0.5' or e == 0.5:
halfnumber = halfnumber + 1
if not s == onenumber + zeronumber + halfnumber:
print 'not match'
if zeronumber == 0 or onenumber == 0 or halfnumber == 0:
errSNPset.add(id)
else:
sd['0'] = zeronumber
sd['1'] = onenumber
sd['0.5'] = halfnumber
sd['sum'] = s
d[id] = sd
return d, errSNPset
def writeDic2file(d, f):
fw = open(f, "w")
fw.write("rsID\tpValue\t-LOG10pValue\n")
for k in d.keys():
info = d[k]
newline = k + "\t" + str(info[0]) + "\t" + str(info[1]) + "\n"
fw.write(newline)
fw.close()
def transposefile(srcfile, targetfile):
f2 = open(targetfile, "w")
newlst = []
col = 0
row = 0
for line in fileinput.input(srcfile):
row = row + 1
patcharr = line.strip().split("\t")
col = len(patcharr)
for e in patcharr:
newlst.append(e)
for i in range(col):
newlineitemlst = []
for j in range(row):
newlineitemlst.append(newlst[i + j * col])
newline = "\t".join(newlineitemlst) + "\n"
f2.write(newline)
def writelist2f(lst, f, h):
with open(f, "w") as fw:
fw.write(h + "\n")
for e in lst:
fw.write(e + "\n")
fw.close()
def getsamplefile(file1, file2, dataclass):
caselinelst = []
controllinelst = []
head = ""
i = 0
for line in fileinput.input(file1):
i = i + 1
if i == 1:
head = line.strip()
else:
arr = line.strip().split("\t")
if arr[1] == '0':
caselinelst.append(line.strip())
else:
controllinelst.append(line.strip())
if dataclass == "case":
writelist2f(caselinelst, file2, head)
if dataclass == "control":
writelist2f(controllinelst, file2, head)
# 下面从得到的file里随机抽样(待补充)
def reduceDimension(old, reduced, seed):
fw = open(reduced, "w")
i = 0
for line in fileinput.input(old):
i = i + 1
if i == 1:
fw.write(line.strip() + "\n")
elif i == 2:
arr = line.strip().split("\t")
elst = []
j=0
for e in arr:
j=j+1
if j==1:
elst.append(e)
elif str(e).find("1"):
elst.append("diseased")
else:
elst.append("healthy")
l = "\t".join(elst)
fw.write(l + "\n")
else:
arr = line.strip().split("\t")
if arr[0].strip() in seed:
fw.write(line.strip() + "\n")
else:
continue
def getlstfromfile(o,l):
lst = []
for line in fileinput.input(o):
if line.startswith("\"rs"):
arr = line.strip().split("\t")
id = arr[0].strip("\"")
lst.append(id)
if len(lst)>l:
break
fileinput.close()
return lst
def gettimesinmultiplesbset(slst,e):
i = 0
lst = []
for sub in slst:
if e in sub:
lst.append('1')
i = i+1
else:
lst.append('0')
continue
return i,lst
def writematrix2file(matrix, f):
newlst = []
for line in matrix:
strline=[]
for e in line:
newe = str(e)
strline.append(newe)
newline = "\t".join(strline)
newlst.append(newline)
lines = "\n".join(newlst)
fw = open(f,"w")
fw.write(lines)
fw.close()
# =====================================写十个性状的源数据文件
for i in range(1, 11):
file2write = phenotype_matrix_file_prefix + str(i) + "th_phenotype.dat"
file2writelst.append(file2write)
# fw = open(file2write,"w")
# for line in fileinput.input(alldatafile):
# arr = line.strip().split("\t")
# snplst = arr[11:len(arr)-1]
# newline = arr[0] +"\t"+arr[i]+"\t"+"\t".join(snplst)+"\n"
# fw.write(newline)
# fw.close()
# =====================================================================================================#
# --------------每个文件进行分组
# file2writelst.append(mainpf)
pvalue_threthhold = 5*math.pow(10,-3)
phenotypeallfilelstDic = {}
for pf in file2writelst:
fileDicForthisPhenotype = {}
fallcase = pf + ".allcase.dat"
fallcontrol = pf + ".allcontrol.dat"
fileDicForthisPhenotype["allcase"] = fallcase
fileDicForthisPhenotype["allcontrol"] = fallcontrol
# getsamplefile(pf, fallcase, "case")
# getsamplefile(pf, fallcontrol, "control")
phenotypeallfilelstDic[pf] = fileDicForthisPhenotype
# 下面生成每个文件的转置文件
for pf in file2writelst:
case_control_dic = phenotypeallfilelstDic[pf]
trans_all_case_file = case_control_dic["allcase"] + ".transpose.dat"
trans_all_control_file = case_control_dic["allcontrol"] + ".transpose.dat"
case_control_dic["trans_allcase"] = trans_all_case_file
case_control_dic["trans_allcontrol"] = trans_all_control_file
# transposefile(case_control_dic["allcase"], trans_all_case_file)
# transposefile(case_control_dic["allcontrol"], trans_all_control_file)
#下面生成每个性状的卡方检验
for pf in file2writelst:
snpSignificancePvaluefile = pf + ".snpSignificancePvaluefile.dat"
case_control_dic = phenotypeallfilelstDic[pf]
case_control_dic["snpSignificancePvaluefile"] = snpSignificancePvaluefile
# casersDic, caseErrsnp = getrateDicfromfile(case_control_dic["trans_allcase"])
# controlDic, controlErrsnp = getrateDicfromfile(case_control_dic["trans_allcontrol"])
# sumErrsnp = caseErrsnp | controlErrsnp
# remainsnp = set(casersDic.keys()) - sumErrsnp
# rs_pvalue_dic = {}
# frecasedic = {}
# frecontroldic = {}
# for c in remainsnp:
# if not c.startswith('rs'):
# continue
# else:
# vcase = casersDic[c]
# vcontrol = controlDic[c]
# casearr = []
# controlarr = []
# if vcase.has_key('1') and vcase.has_key('0.5') and vcase.has_key('0'):
# casearr = [vcase['1'], vcase['0'], vcase['0.5']]
# frecasedic[c] = casearr
# else:
# print 'case: ' + c + " key error"
#
# if vcontrol.has_key('1') and vcontrol.has_key('0.5') and vcontrol.has_key('0') and vcontrol:
# controlarr = [vcontrol['1'], vcontrol['0'], vcontrol['0.5']]
# frecontroldic[c] = controlarr
# else:
# print 'control: ' + c + " key error"
# if len(casearr) == 3 and len(controlarr) == 3:
# ch, p = chisquare(casearr, controlarr)
# minuslogPvalue = 0.0
# if p <= 0:
# print "pVALUE IS ZERO :" + c + " " + str(p) + c + ":" + str(vcase) + ";" + str(vcontrol) + str(ch)
# minuslogPvalue = 400
# rs_pvalue_dic[c] = [p, minuslogPvalue]
# else:
# minuslogPvalue = -math.log10(p)
# rs_pvalue_dic[c] = [p, minuslogPvalue]
# if p < math.pow(10, -8):
# # print "strong significance: " +c + ":"+ str(vcase) + ";"+ str(vcontrol) +str(p) +" " +str(ch)
# pass
#
# else:
# print 'there is sth wrong in array '
# writeDic2file(rs_pvalue_dic, snpSignificancePvaluefile)
# candidatesnp_rf = []
# for snp in rs_pvalue_dic:
# lst = rs_pvalue_dic[snp]
# if lst[0] <= pvalue_threthhold:
# candidatesnp_rf.append(snp)
# case_control_dic["snp_for_rf"] = candidatesnp_rf
# print len(candidatesnp_rf)
#==========批量跑随机森林step1:利用已选择的rsID对总文件进行降维,首先转置,再姜维,再转置回来
for pf in file2writelst:
transposesumfile = pf + ".transpose.dat"
reduced_snp_sumfile_by_chi_transpose = pf + ".reduced_snp.transpose.dat"
reduced_snp_sumfile_by_chi = pf + ".reduced_snp.dat"
case_control_dic["reduced_snp_sumfile_by_chi"] = reduced_snp_sumfile_by_chi
case_control_dic = phenotypeallfilelstDic[pf]
case_control_dic["sumfile_transpose"] = transposesumfile
# transposefile(pf, transposesumfile)
# reduceDimension(transposesumfile, reduced_snp_sumfile_by_chi_transpose, case_control_dic["snp_for_rf"])
# transposefile(reduced_snp_sumfile_by_chi_transpose, reduced_snp_sumfile_by_chi)
# # ==========批量跑随机森林step2:调用R脚本选择了ntree=600,800,1000三个重复
#
#
# # ==== 每个重复选m个排名靠前的snp FREQUENCY代表
m = 50
FREQUENCY=3
sumset = set()
setlst = []
for pf in file2writelst:
case_control_dic = phenotypeallfilelstDic[pf]
importancefile_600 = case_control_dic["reduced_snp_sumfile_by_chi"] + ".ntree600.importance.dat"
importancefile_800 = case_control_dic["reduced_snp_sumfile_by_chi"] + ".ntree800.importance.dat"
importancefile_1000 = case_control_dic["reduced_snp_sumfile_by_chi"] + ".ntree1000.importance.dat"
# USE R to order file
# for (i in 1:30){filedir = paste(root,sep = "",filearr[i]); rdata <-read.table(filedir,header = TRUE);ordereddata<- rdata[order(rdata[,4],decreasing=T),];duishuzhi<-log10(ordereddata$MeanDecreaseGini);newdata<-cbind(ordereddata,duishuzhi);write.table(newdata,file = paste(root2,sep = "",filearr[i],"ordered_by_gene"),sep = "\t",row.names = TRUE,col.names = TRUE)}
rs600_ordered = case_control_dic["reduced_snp_sumfile_by_chi"] + " .ntree600.importance.datordered_by_gini.dat"
rs800_ordered = case_control_dic["reduced_snp_sumfile_by_chi"] + " .ntree800.importance.datordered_by_gini.dat"
rs1000_ordered = case_control_dic["reduced_snp_sumfile_by_chi"] + " .ntree1000.importance.datordered_by_gini.dat"
rs600orderedlst=getlstfromfile(rs600_ordered,m)
rs800orderedlst = getlstfromfile(rs800_ordered,m)
rs1000orderedlst = getlstfromfile(rs1000_ordered,m)
sharedset = set(rs600orderedlst) & set(rs800orderedlst) & set(rs1000orderedlst)
case_control_dic["significant_snp_from_3_rf"] = sharedset
sumset = sumset | sharedset
setlst.append(sharedset)
print "for " +pf +": " +str(len(sharedset)) +" snp significant"
n=0
data2writeDic = {}
for snp in sumset:
frequency,yesornolst = gettimesinmultiplesbset(setlst,snp)
#统计总集中RS在字迹出现的频率 结果有待商榷
if frequency >=FREQUENCY:
n=n+1
data2writeDic[snp] = yesornolst
print snp +": "+str(frequency)
print str(n) +" snp has more than " +str(FREQUENCY) +" times"
iset = sumset
for s in setlst:
iset =iset & s
print len(iset)
print "the last number is : " + str(len(iset)) +" the snp set is : " + str(iset)
#
# #写频次数据到文件里
# frequencyfile = "P:\\modeling\\new\\multi_phenos\\frequencyfile.dat"
# frequencyfile_trans = "P:\\modeling\\new\\multi_phenos\\frequencyfile_transpose.dat"
# fw = open(frequencyfile,"w")
# fw.write("snpID pan fang jie biye huojiang healthy happy growth coding love\n")
# for snp in data2writeDic.keys():
# number = "\t".join(data2writeDic[snp])
# newline = snp +"\t" +number+"\n"
# fw.write(newline)
# fw.close()
# transposefile(frequencyfile,frequencyfile_trans)
#
#
# intersectionmatrix =[]
# numbermatrix = []
# for subset1 in setlst:
# line=[]
# noline = []
# for subset2 in setlst:
# inter = subset1 &subset2
# line.append(inter)
# noline.append(len(inter))
# intersectionmatrix.append(line)
# numbermatrix.append(noline)
# print numbermatrix
# subsetinterfile = "P:\\modeling\\new\\multi_phenos\\subsetinterfile.dat"
# writematrix2file(numbermatrix,subsetinterfile)